90528aa67532ca3e1696adfab0dfc2bacfd41082
[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 <fcntl.h>\r
31 #include <math.h>\r
32 #include <commdlg.h>\r
33 #include <dlgs.h>\r
34 \r
35 #include "common.h"\r
36 #include "winboard.h"\r
37 #include "frontend.h"\r
38 #include "backend.h"\r
39 \r
40 /* Module globals */\r
41 HWND gameListDialog = NULL;\r
42 BOOLEAN gameListUp = FALSE;\r
43 FILE* gameFile;\r
44 char* gameFileName = NULL;\r
45 int gameListX, gameListY, gameListW, gameListH;\r
46 \r
47 /* Imports from winboard.c */\r
48 extern HINSTANCE hInst;\r
49 extern HWND hwndMain;\r
50 \r
51 \r
52 LRESULT CALLBACK\r
53 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
54 {\r
55   static HANDLE hwndText;\r
56   int nItem;\r
57   ListGame *lg;\r
58   RECT rect;\r
59   static int sizeX, sizeY;\r
60   int newSizeX, newSizeY, flags;\r
61   MINMAXINFO *mmi;\r
62 \r
63   switch (message) {\r
64   case WM_INITDIALOG: \r
65     if (gameListDialog) {\r
66       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
67     }\r
68     /* Initialize the dialog items */\r
69     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
70     lg = (ListGame *) gameList.head;\r
71     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
72       char *st = GameListLine(lg->number, &lg->gameInfo);\r
73       SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
74       free(st);\r
75       lg = (ListGame *) lg->node.succ;\r
76     }\r
77     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
78     /* Size and position the dialog */\r
79     if (!gameListDialog) {\r
80       gameListDialog = hDlg;\r
81       flags = SWP_NOZORDER;\r
82       GetClientRect(hDlg, &rect);\r
83       sizeX = rect.right;\r
84       sizeY = rect.bottom;\r
85       if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&\r
86           gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {\r
87         WINDOWPLACEMENT wp;\r
88         EnsureOnScreen(&gameListX, &gameListY);\r
89         wp.length = sizeof(WINDOWPLACEMENT);\r
90         wp.flags = 0;\r
91         wp.showCmd = SW_SHOW;\r
92         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
93         wp.rcNormalPosition.left = gameListX;\r
94         wp.rcNormalPosition.right = gameListX + gameListW;\r
95         wp.rcNormalPosition.top = gameListY;\r
96         wp.rcNormalPosition.bottom = gameListY + gameListH;\r
97         SetWindowPlacement(hDlg, &wp);\r
98 \r
99         GetClientRect(hDlg, &rect);\r
100         newSizeX = rect.right;\r
101         newSizeY = rect.bottom;\r
102         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,\r
103                               newSizeX, newSizeY);\r
104         sizeX = newSizeX;\r
105         sizeY = newSizeY;\r
106       }\r
107     }\r
108     return FALSE;\r
109     \r
110   case WM_SIZE:\r
111     newSizeX = LOWORD(lParam);\r
112     newSizeY = HIWORD(lParam);\r
113     ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),\r
114       sizeX, sizeY, newSizeX, newSizeY);\r
115     sizeX = newSizeX;\r
116     sizeY = newSizeY;\r
117     break;\r
118 \r
119   case WM_GETMINMAXINFO:\r
120     /* Prevent resizing window too small */\r
121     mmi = (MINMAXINFO *) lParam;\r
122     mmi->ptMinTrackSize.x = 100;\r
123     mmi->ptMinTrackSize.y = 100;\r
124     break;\r
125 \r
126   case WM_COMMAND:\r
127     switch (LOWORD(wParam)) {\r
128     case IDOK:\r
129     case OPT_GameListLoad:\r
130       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
131       if (nItem < 0) {\r
132         /* is this possible? */\r
133         DisplayError("No game selected", 0);\r
134         return TRUE;\r
135       }\r
136       break; /* load the game*/\r
137       \r
138     case OPT_GameListNext:\r
139       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
140       nItem++;\r
141       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
142         DisplayError("Can't go forward any further", 0);\r
143         return TRUE;\r
144       }\r
145       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
146       break; /* load the game*/\r
147       \r
148     case OPT_GameListPrev:\r
149       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
150       nItem--;\r
151       if (nItem < 0) {\r
152         DisplayError("Can't back up any further", 0);\r
153       }\r
154       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
155       break; /* load the game*/\r
156       \r
157     case IDCANCEL:\r
158     case OPT_GameListClose:\r
159       GameListPopDown();\r
160       return TRUE;\r
161       \r
162     case OPT_GameListText:\r
163       switch (HIWORD(wParam)) {\r
164       case LBN_DBLCLK:\r
165         nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);\r
166         break; /* load the game*/\r
167         \r
168       default:\r
169         return FALSE;\r
170       }\r
171       break;\r
172       \r
173     default:\r
174       return FALSE;\r
175     }\r
176     /* Load the game */\r
177     if (cmailMsgLoaded) {\r
178       CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
179     } else {\r
180       LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
181     }\r
182     return TRUE;\r
183 \r
184   default:\r
185     break;\r
186   }\r
187   return FALSE;\r
188 }\r
189 \r
190 \r
191 VOID GameListPopUp(FILE *fp, char *filename)\r
192 {\r
193   FARPROC lpProc;\r
194   \r
195   gameFile = fp;\r
196   if (gameFileName != filename) {\r
197     if (gameFileName) free(gameFileName);\r
198     gameFileName = StrSave(filename);\r
199   }\r
200   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
201   if (gameListDialog) {\r
202     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
203     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
204   } else {\r
205     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
206     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
207       hwndMain, (DLGPROC)lpProc);\r
208     FreeProcInstance(lpProc);\r
209   }\r
210   gameListUp = TRUE;\r
211 }\r
212 \r
213 VOID GameListPopDown(void)\r
214 {\r
215   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
216   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
217   gameListUp = FALSE;\r
218 }\r
219 \r
220 \r
221 VOID GameListHighlight(int index)\r
222 {\r
223   if (gameListDialog == NULL) return;\r
224   SendDlgItemMessage(gameListDialog, OPT_GameListText, \r
225     LB_SETCURSEL, index - 1, 0);\r
226 }\r
227 \r
228 \r
229 VOID GameListDestroy()\r
230 {\r
231   GameListPopDown();\r
232   if (gameFileName) {\r
233     free(gameFileName);\r
234     gameFileName = NULL;\r
235   }\r
236 }\r
237 \r
238 VOID ShowGameListProc()\r
239 {\r
240   if (gameListUp) {\r
241     GameListPopDown();\r
242   } else {\r
243     if (gameFileName) {\r
244       GameListPopUp(gameFile, gameFileName);\r
245     } else {\r
246       DisplayError("No game list", 0);\r
247     }\r
248   }\r
249 }\r