Initial checkin. I created this by combining the XBoard 4.2.6 and
[xboard.git] / winboard / wgamelist.c
1 /*\r
2  * wgamelist.c -- Game list window for WinBoard\r
3  * $Id$\r
4  *\r
5  * Copyright 1995 Free Software Foundation, Inc.\r
6  *\r
7  * ------------------------------------------------------------------------\r
8  * This program is free software; you can redistribute it and/or modify\r
9  * it under the terms of the GNU General Public License as published by\r
10  * the Free Software Foundation; either version 2 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU General Public License\r
19  * along with this program; if not, write to the Free Software\r
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
21  * ------------------------------------------------------------------------\r
22  */\r
23 \r
24 #include "config.h"\r
25 \r
26 #include <windows.h> /* required for all Windows applications */\r
27 #include <stdio.h>\r
28 #include <stdlib.h>\r
29 #include <malloc.h>\r
30 #include <io.h>\r
31 #include <fcntl.h>\r
32 #include <math.h>\r
33 #include <commdlg.h>\r
34 #include <dlgs.h>\r
35 \r
36 #include "common.h"\r
37 #include "winboard.h"\r
38 #include "frontend.h"\r
39 #include "backend.h"\r
40 \r
41 /* Module globals */\r
42 HWND gameListDialog = NULL;\r
43 BOOLEAN gameListUp = FALSE;\r
44 FILE* gameFile;\r
45 char* gameFileName = NULL;\r
46 int gameListX, gameListY, gameListW, gameListH;\r
47 \r
48 /* Imports from winboard.c */\r
49 extern HINSTANCE hInst;\r
50 extern HWND hwndMain;\r
51 \r
52 \r
53 LRESULT CALLBACK\r
54 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
55 {\r
56   static HANDLE hwndText;\r
57   int nItem;\r
58   ListGame *lg;\r
59   RECT rect;\r
60   static int sizeX, sizeY;\r
61   int newSizeX, newSizeY, flags;\r
62   MINMAXINFO *mmi;\r
63 \r
64   switch (message) {\r
65   case WM_INITDIALOG: \r
66     if (gameListDialog) {\r
67       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
68     }\r
69     /* Initialize the dialog items */\r
70     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
71     lg = (ListGame *) gameList.head;\r
72     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
73       char *st = GameListLine(lg->number, &lg->gameInfo);\r
74       SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
75       free(st);\r
76       lg = (ListGame *) lg->node.succ;\r
77     }\r
78     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
79     /* Size and position the dialog */\r
80     if (!gameListDialog) {\r
81       gameListDialog = hDlg;\r
82       flags = SWP_NOZORDER;\r
83       GetClientRect(hDlg, &rect);\r
84       sizeX = rect.right;\r
85       sizeY = rect.bottom;\r
86       if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&\r
87           gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {\r
88         WINDOWPLACEMENT wp;\r
89         EnsureOnScreen(&gameListX, &gameListY);\r
90         wp.length = sizeof(WINDOWPLACEMENT);\r
91         wp.flags = 0;\r
92         wp.showCmd = SW_SHOW;\r
93         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
94         wp.rcNormalPosition.left = gameListX;\r
95         wp.rcNormalPosition.right = gameListX + gameListW;\r
96         wp.rcNormalPosition.top = gameListY;\r
97         wp.rcNormalPosition.bottom = gameListY + gameListH;\r
98         SetWindowPlacement(hDlg, &wp);\r
99 \r
100         GetClientRect(hDlg, &rect);\r
101         newSizeX = rect.right;\r
102         newSizeY = rect.bottom;\r
103         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,\r
104                               newSizeX, newSizeY);\r
105         sizeX = newSizeX;\r
106         sizeY = newSizeY;\r
107       }\r
108     }\r
109     return FALSE;\r
110     \r
111   case WM_SIZE:\r
112     newSizeX = LOWORD(lParam);\r
113     newSizeY = HIWORD(lParam);\r
114     ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),\r
115       sizeX, sizeY, newSizeX, newSizeY);\r
116     sizeX = newSizeX;\r
117     sizeY = newSizeY;\r
118     break;\r
119 \r
120   case WM_GETMINMAXINFO:\r
121     /* Prevent resizing window too small */\r
122     mmi = (MINMAXINFO *) lParam;\r
123     mmi->ptMinTrackSize.x = 100;\r
124     mmi->ptMinTrackSize.y = 100;\r
125     break;\r
126 \r
127   case WM_COMMAND:\r
128     switch (LOWORD(wParam)) {\r
129     case IDOK:\r
130     case OPT_GameListLoad:\r
131       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
132       if (nItem < 0) {\r
133         /* is this possible? */\r
134         DisplayError("No game selected", 0);\r
135         return TRUE;\r
136       }\r
137       break; /* load the game*/\r
138       \r
139     case OPT_GameListNext:\r
140       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
141       nItem++;\r
142       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
143         DisplayError("Can't go forward any further", 0);\r
144         return TRUE;\r
145       }\r
146       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
147       break; /* load the game*/\r
148       \r
149     case OPT_GameListPrev:\r
150       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
151       nItem--;\r
152       if (nItem < 0) {\r
153         DisplayError("Can't back up any further", 0);\r
154       }\r
155       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
156       break; /* load the game*/\r
157       \r
158     case IDCANCEL:\r
159     case OPT_GameListClose:\r
160       GameListPopDown();\r
161       return TRUE;\r
162       \r
163     case OPT_GameListText:\r
164       switch (HIWORD(wParam)) {\r
165       case LBN_DBLCLK:\r
166         nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);\r
167         break; /* load the game*/\r
168         \r
169       default:\r
170         return FALSE;\r
171       }\r
172       break;\r
173       \r
174     default:\r
175       return FALSE;\r
176     }\r
177     /* Load the game */\r
178     if (cmailMsgLoaded) {\r
179       CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
180     } else {\r
181       LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
182     }\r
183     return TRUE;\r
184 \r
185   default:\r
186     break;\r
187   }\r
188   return FALSE;\r
189 }\r
190 \r
191 \r
192 VOID GameListPopUp(FILE *fp, char *filename)\r
193 {\r
194   FARPROC lpProc;\r
195   \r
196   gameFile = fp;\r
197   if (gameFileName != filename) {\r
198     if (gameFileName) free(gameFileName);\r
199     gameFileName = StrSave(filename);\r
200   }\r
201   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
202   if (gameListDialog) {\r
203     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
204     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
205   } else {\r
206     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
207     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
208       hwndMain, (DLGPROC)lpProc);\r
209     FreeProcInstance(lpProc);\r
210   }\r
211   gameListUp = TRUE;\r
212 }\r
213 \r
214 VOID GameListPopDown(void)\r
215 {\r
216   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
217   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
218   gameListUp = FALSE;\r
219 }\r
220 \r
221 \r
222 VOID GameListHighlight(int index)\r
223 {\r
224   if (gameListDialog == NULL) return;\r
225   SendDlgItemMessage(gameListDialog, OPT_GameListText, \r
226     LB_SETCURSEL, index - 1, 0);\r
227 }\r
228 \r
229 \r
230 VOID GameListDestroy()\r
231 {\r
232   GameListPopDown();\r
233   if (gameFileName) {\r
234     free(gameFileName);\r
235     gameFileName = NULL;\r
236   }\r
237 }\r
238 \r
239 VOID ShowGameListProc()\r
240 {\r
241   if (gameListUp) {\r
242     GameListPopDown();\r
243   } else {\r
244     if (gameFileName) {\r
245       GameListPopUp(gameFile, gameFileName);\r
246     } else {\r
247       DisplayError("No game list", 0);\r
248     }\r
249   }\r
250 }\r