Updated copyright notice to 2011
[xboard.git] / winboard / wgamelist.c
1 /*\r
2  * wgamelist.c -- Game list window for WinBoard\r
3  *\r
4  * Copyright 1995, 2009, 2010, 2011 Free Software Foundation, Inc.\r
5  *\r
6  * Enhancements Copyright 2005 Alessandro Scotti\r
7  *\r
8  * ------------------------------------------------------------------------\r
9  *\r
10  * GNU XBoard is free software: you can redistribute it and/or modify\r
11  * it under the terms of the GNU General Public License as published by\r
12  * the Free Software Foundation, either version 3 of the License, or (at\r
13  * your option) any later version.\r
14  *\r
15  * GNU XBoard is distributed in the hope that it will be useful, but\r
16  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
18  * General Public License for more details.\r
19  *\r
20  * You should have received a copy of the GNU General Public License\r
21  * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
22  *\r
23  *------------------------------------------------------------------------\r
24  ** See the file ChangeLog for a revision history.  */\r
25 \r
26 #include "config.h"\r
27 \r
28 #include <windows.h> /* required for all Windows applications */\r
29 #include <stdio.h>\r
30 #include <stdlib.h>\r
31 #include <malloc.h>\r
32 #include <fcntl.h>\r
33 #include <math.h>\r
34 #include <commdlg.h>\r
35 #include <dlgs.h>\r
36 \r
37 #include "common.h"\r
38 #include "frontend.h"\r
39 #include "backend.h"\r
40 #include "winboard.h"\r
41 \r
42 #include "wsnap.h"\r
43 \r
44 #define _(s) T_(s)\r
45 \r
46 /* Module globals */\r
47 static BOOLEAN gameListUp = FALSE;\r
48 static FILE* gameFile;\r
49 static char* gameFileName = NULL;\r
50 \r
51 struct GameListStats\r
52 {\r
53     int white_wins;\r
54     int black_wins;\r
55     int drawn;\r
56     int unfinished;\r
57 };\r
58 \r
59 /* [AS] Setup the game list according to the specified filter */\r
60 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )\r
61 {\r
62     ListGame * lg = (ListGame *) gameList.head;\r
63     int nItem;\r
64     BOOL hasFilter = FALSE;\r
65     int count = 0;\r
66     struct GameListStats dummy;\r
67 \r
68     /* Initialize stats (use a dummy variable if caller not interested in them) */\r
69     if( stats == NULL ) {\r
70         stats = &dummy;\r
71     }\r
72 \r
73     stats->white_wins = 0;\r
74     stats->black_wins = 0;\r
75     stats->drawn = 0;\r
76     stats->unfinished = 0;\r
77 \r
78     if( boReset ) {\r
79         SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
80     }\r
81 \r
82     if( pszFilter != NULL ) {\r
83         if( strlen( pszFilter ) > 0 ) {\r
84             hasFilter = TRUE;\r
85         }\r
86     }\r
87 \r
88     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
89         char * st = GameListLine(lg->number, &lg->gameInfo);\r
90         BOOL skip = FALSE;\r
91 \r
92         if( hasFilter ) {\r
93             if( ! SearchPattern( st, pszFilter ) ) {\r
94                 skip = TRUE;\r
95             }\r
96         }\r
97 \r
98         if( ! skip ) {\r
99             SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
100             count++;\r
101 \r
102             /* Update stats */\r
103             if( lg->gameInfo.result == WhiteWins )\r
104                 stats->white_wins++;\r
105             else if( lg->gameInfo.result == BlackWins )\r
106                 stats->black_wins++;\r
107             else if( lg->gameInfo.result == GameIsDrawn )\r
108                 stats->drawn++;\r
109             else\r
110                 stats->unfinished++;\r
111         }\r
112 \r
113         free(st);\r
114         lg = (ListGame *) lg->node.succ;\r
115     }\r
116 \r
117     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
118 \r
119     return count;\r
120 }\r
121 \r
122 /* [AS] Show number of visible (filtered) games and total on window caption */\r
123 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )\r
124 {\r
125     char buf[256];\r
126 \r
127     snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );\r
128 \r
129     if( stats != 0 ) {\r
130         sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );\r
131     }\r
132 \r
133     SetWindowText( hDlg, buf );\r
134 \r
135     return 0;\r
136 }\r
137 \r
138 #define MAX_FILTER_LENGTH   128\r
139 \r
140 LRESULT CALLBACK\r
141 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
142 {\r
143   static char szDlgTitle[64];\r
144   static HANDLE hwndText;\r
145   int nItem;\r
146   RECT rect;\r
147   static int sizeX, sizeY;\r
148   int newSizeX, newSizeY, flags;\r
149   MINMAXINFO *mmi;\r
150   static BOOL filterHasFocus = FALSE;\r
151   int count;\r
152   struct GameListStats stats;\r
153   static SnapData sd;\r
154 \r
155   switch (message) {\r
156   case WM_INITDIALOG:\r
157     Translate(hDlg, DLG_GameList);\r
158     GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );\r
159     szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';\r
160 \r
161     if (gameListDialog) {\r
162       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
163     }\r
164 \r
165     /* Initialize the dialog items */\r
166     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
167 \r
168     /* Set font */\r
169     SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));\r
170 \r
171     count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );\r
172 \r
173     SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );\r
174     SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );\r
175 \r
176     filterHasFocus = FALSE;\r
177 \r
178     /* Size and position the dialog */\r
179     if (!gameListDialog) {\r
180       gameListDialog = hDlg;\r
181       flags = SWP_NOZORDER;\r
182       GetClientRect(hDlg, &rect);\r
183       sizeX = rect.right;\r
184       sizeY = rect.bottom;\r
185       if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&\r
186           wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {\r
187         WINDOWPLACEMENT wp;\r
188         EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);\r
189         wp.length = sizeof(WINDOWPLACEMENT);\r
190         wp.flags = 0;\r
191         wp.showCmd = SW_SHOW;\r
192         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
193         wp.rcNormalPosition.left = wpGameList.x;\r
194         wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;\r
195         wp.rcNormalPosition.top = wpGameList.y;\r
196         wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;\r
197         SetWindowPlacement(hDlg, &wp);\r
198 \r
199         GetClientRect(hDlg, &rect);\r
200         newSizeX = rect.right;\r
201         newSizeY = rect.bottom;\r
202         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,\r
203                               newSizeX, newSizeY);\r
204         sizeX = newSizeX;\r
205         sizeY = newSizeY;\r
206       } else\r
207         GetActualPlacement( gameListDialog, &wpGameList );\r
208 \r
209     }\r
210       GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title\r
211     return FALSE;\r
212 \r
213   case WM_SIZE:\r
214     newSizeX = LOWORD(lParam);\r
215     newSizeY = HIWORD(lParam);\r
216     ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),\r
217       sizeX, sizeY, newSizeX, newSizeY);\r
218     sizeX = newSizeX;\r
219     sizeY = newSizeY;\r
220     break;\r
221 \r
222   case WM_ENTERSIZEMOVE:\r
223     return OnEnterSizeMove( &sd, hDlg, wParam, lParam );\r
224 \r
225   case WM_SIZING:\r
226     return OnSizing( &sd, hDlg, wParam, lParam );\r
227 \r
228   case WM_MOVING:\r
229     return OnMoving( &sd, hDlg, wParam, lParam );\r
230 \r
231   case WM_EXITSIZEMOVE:\r
232     return OnExitSizeMove( &sd, hDlg, wParam, lParam );\r
233 \r
234   case WM_GETMINMAXINFO:\r
235     /* Prevent resizing window too small */\r
236     mmi = (MINMAXINFO *) lParam;\r
237     mmi->ptMinTrackSize.x = 100;\r
238     mmi->ptMinTrackSize.y = 100;\r
239     break;\r
240 \r
241   case WM_COMMAND:\r
242       /*\r
243         [AS]\r
244         If <Enter> is pressed while editing the filter, it's better to apply\r
245         the filter rather than selecting the current game.\r
246       */\r
247       if( LOWORD(wParam) == IDC_GameListFilter ) {\r
248           switch( HIWORD(wParam) ) {\r
249           case EN_SETFOCUS:\r
250               filterHasFocus = TRUE;\r
251               break;\r
252           case EN_KILLFOCUS:\r
253               filterHasFocus = FALSE;\r
254               break;\r
255           }\r
256       }\r
257 \r
258       if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {\r
259           wParam = IDC_GameListDoFilter;\r
260       }\r
261       /* [AS] End command replacement */\r
262 \r
263     switch (LOWORD(wParam)) {\r
264     case IDOK:\r
265     case OPT_GameListLoad:\r
266       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
267       if (nItem < 0) {\r
268         /* is this possible? */\r
269         DisplayError(_("No game selected"), 0);\r
270         return TRUE;\r
271       }\r
272       break; /* load the game*/\r
273 \r
274     case OPT_GameListNext:\r
275       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
276       nItem++;\r
277       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
278         /* [AS] Removed error message */\r
279         /* DisplayError(_("Can't go forward any further"), 0); */\r
280         return TRUE;\r
281       }\r
282       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
283       break; /* load the game*/\r
284 \r
285     case OPT_GameListPrev:\r
286       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
287       nItem--;\r
288       if (nItem < 0) {\r
289         /* [AS] Removed error message, added return */\r
290         /* DisplayError(_("Can't back up any further"), 0); */\r
291         return TRUE;\r
292       }\r
293       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
294       break; /* load the game*/\r
295 \r
296     /* [AS] */\r
297     case IDC_GameListDoFilter:\r
298         {\r
299             char filter[MAX_FILTER_LENGTH+1];\r
300 \r
301             if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {\r
302                 filter[ sizeof(filter)-1 ] = '\0';\r
303                 count = GameListToListBox( hDlg, TRUE, filter, &stats );\r
304                 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );\r
305             }\r
306         }\r
307         return FALSE;\r
308         break;\r
309 \r
310     case IDCANCEL:\r
311     case OPT_GameListClose:\r
312       GameListPopDown();\r
313       return TRUE;\r
314 \r
315     case OPT_GameListText:\r
316       switch (HIWORD(wParam)) {\r
317       case LBN_DBLCLK:\r
318         nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);\r
319         break; /* load the game*/\r
320 \r
321       default:\r
322         return FALSE;\r
323       }\r
324       break;\r
325 \r
326     default:\r
327       return FALSE;\r
328     }\r
329 \r
330     /* Load the game */\r
331     {\r
332         /* [AS] Get index from the item itself, because filtering makes original order unuseable. */\r
333         int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
334         char * text;\r
335         LRESULT res;\r
336 \r
337         if( index < 0 ) {\r
338             return TRUE;\r
339         }\r
340 \r
341         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );\r
342 \r
343         if( res == LB_ERR ) {\r
344             return TRUE;\r
345         }\r
346 \r
347         text = (char *) malloc( res+1 );\r
348 \r
349         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );\r
350 \r
351         index = atoi( text );\r
352 \r
353         nItem = index - 1;\r
354 \r
355         free( text );\r
356         /* [AS] End: nItem has been "patched" now! */\r
357 \r
358         if (cmailMsgLoaded) {\r
359             CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
360         }\r
361         else {\r
362             LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
363             SetFocus(hwndMain); // [HGM] automatic focus switch\r
364         }\r
365     }\r
366 \r
367     return TRUE;\r
368 \r
369   default:\r
370     break;\r
371   }\r
372   return FALSE;\r
373 }\r
374 \r
375 \r
376 VOID GameListPopUp(FILE *fp, char *filename)\r
377 {\r
378   FARPROC lpProc;\r
379 \r
380   gameFile = fp;\r
381   if (gameFileName != filename) {\r
382     if (gameFileName) free(gameFileName);\r
383     gameFileName = StrSave(filename);\r
384   }\r
385   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
386   if (gameListDialog) {\r
387     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
388     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
389     else SetFocus(gameListDialog);\r
390   } else {\r
391     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
392     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
393       hwndMain, (DLGPROC)lpProc);\r
394     FreeProcInstance(lpProc);\r
395   }\r
396   gameListUp = TRUE;\r
397 }\r
398 \r
399 VOID GameListPopDown(void)\r
400 {\r
401   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
402   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
403   gameListUp = FALSE;\r
404 }\r
405 \r
406 \r
407 VOID GameListHighlight(int index)\r
408 {\r
409   if (gameListDialog == NULL) return;\r
410   SendDlgItemMessage(gameListDialog, OPT_GameListText,\r
411     LB_SETCURSEL, index - 1, 0);\r
412 }\r
413 \r
414 \r
415 VOID GameListDestroy()\r
416 {\r
417   GameListPopDown();\r
418   if (gameFileName) {\r
419     free(gameFileName);\r
420     gameFileName = NULL;\r
421   }\r
422 }\r
423 \r
424 VOID ShowGameListProc()\r
425 {\r
426   if (gameListUp) {\r
427     if(gameListDialog) SetFocus(gameListDialog);\r
428 //    GameListPopDown();\r
429   } else {\r
430     if (gameFileName) {\r
431       GameListPopUp(gameFile, gameFileName);\r
432     } else {\r
433       DisplayError(_("No game list"), 0);\r
434     }\r
435   }\r
436 }\r
437 \r
438 HGLOBAL ExportGameListAsText()\r
439 {\r
440     HGLOBAL result = NULL;\r
441     LPVOID lpMem = NULL;\r
442     ListGame * lg = (ListGame *) gameList.head;\r
443     int nItem;\r
444     DWORD dwLen = 0;\r
445 \r
446     if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {\r
447         DisplayError(_(_("Game list not loaded or empty")), 0);\r
448         return NULL;\r
449     }\r
450 \r
451     /* Get list size */\r
452     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
453         char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
454 \r
455         dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */\r
456 \r
457         free(st);\r
458         lg = (ListGame *) lg->node.succ;\r
459     }\r
460 \r
461     /* Allocate memory for the list */\r
462     result = GlobalAlloc(GHND, dwLen+1 );\r
463 \r
464     if( result != NULL ) {\r
465         lpMem = GlobalLock(result);\r
466     }\r
467 \r
468     /* Copy the list into the global memory block */\r
469     if( lpMem != NULL ) {\r
470         char * dst = (char *) lpMem;\r
471         size_t len;\r
472 \r
473         lg = (ListGame *) gameList.head;\r
474 \r
475         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
476             char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
477 \r
478             len = sprintf( dst, "%s\r\n", st );\r
479             dst += len;\r
480 \r
481             free(st);\r
482             lg = (ListGame *) lg->node.succ;\r
483         }\r
484 \r
485         GlobalUnlock( result );\r
486     }\r
487 \r
488     return result;\r
489 }\r