Updated copyright notice to 2014
[xboard.git] / winboard / wgamelist.c
1 /*\r
2  * wgamelist.c -- Game list window for WinBoard\r
3  *\r
4  * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014 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, BOOL byPos, BOOL narrow )\r
61 {\r
62     ListGame * lg = (ListGame *) gameList.head;\r
63     int nItem;\r
64     char buf[MSG_SIZ];\r
65     BOOL hasFilter = FALSE;\r
66     int count = 0;\r
67     struct GameListStats dummy;\r
68 \r
69     /* Initialize stats (use a dummy variable if caller not interested in them) */\r
70     if( stats == NULL ) {\r
71         stats = &dummy;\r
72     }\r
73 \r
74     stats->white_wins = 0;\r
75     stats->black_wins = 0;\r
76     stats->drawn = 0;\r
77     stats->unfinished = 0;\r
78 \r
79     if( boReset ) {\r
80         SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
81     }\r
82 \r
83     if( pszFilter != NULL ) {\r
84         if( strlen( pszFilter ) > 0 ) {\r
85             hasFilter = TRUE;\r
86         }\r
87     }\r
88 \r
89     if(byPos) InitSearch();\r
90 \r
91     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
92         char * st = NULL;\r
93         BOOL skip = FALSE;\r
94         int pos = -1;\r
95 \r
96         if(nItem % 2000 == 0) {\r
97           snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);\r
98           SetWindowText(hwndMain, buf);\r
99         }\r
100 \r
101       if(!narrow || lg->position >= 0) {\r
102         if( hasFilter ) {\r
103             st = GameListLine(lg->number, &lg->gameInfo);\r
104             if( !SearchPattern( st, pszFilter) ) skip = TRUE;\r
105         }\r
106 \r
107         if( !skip && byPos) {\r
108             if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;\r
109         }\r
110 \r
111         if( ! skip ) {\r
112             if(!st) st = GameListLine(lg->number, &lg->gameInfo);\r
113             SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
114             count++;\r
115 \r
116             /* Update stats */\r
117             if( lg->gameInfo.result == WhiteWins )\r
118                 stats->white_wins++;\r
119             else if( lg->gameInfo.result == BlackWins )\r
120                 stats->black_wins++;\r
121             else if( lg->gameInfo.result == GameIsDrawn )\r
122                 stats->drawn++;\r
123             else\r
124                 stats->unfinished++;\r
125             if(!byPos) pos = 0;\r
126         }\r
127       }\r
128 \r
129         lg->position = pos;\r
130 \r
131         if(st) free(st);\r
132         lg = (ListGame *) lg->node.succ;\r
133     }\r
134 \r
135     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
136     SetWindowText(hwndMain, "WinBoard");\r
137 \r
138     return count;\r
139 }\r
140 \r
141 /* [AS] Show number of visible (filtered) games and total on window caption */\r
142 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )\r
143 {\r
144     char buf[256];\r
145 \r
146     snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );\r
147 \r
148     if( stats != 0 ) {\r
149         sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );\r
150     }\r
151 \r
152     SetWindowText( hDlg, buf );\r
153 \r
154     return 0;\r
155 }\r
156 \r
157 #define MAX_FILTER_LENGTH   128\r
158 \r
159 LRESULT CALLBACK\r
160 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
161 {\r
162   static char szDlgTitle[64];\r
163   static HANDLE hwndText;\r
164   int nItem;\r
165   RECT rect;\r
166   static int sizeX, sizeY;\r
167   int newSizeX, newSizeY, flags;\r
168   MINMAXINFO *mmi;\r
169   static BOOL filterHasFocus = FALSE;\r
170   int count;\r
171   struct GameListStats stats;\r
172   static SnapData sd;\r
173 \r
174   switch (message) {\r
175   case WM_INITDIALOG:\r
176     Translate(hDlg, DLG_GameList);\r
177     GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );\r
178     szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';\r
179 \r
180     if (gameListDialog) {\r
181       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
182     }\r
183 \r
184     /* Initialize the dialog items */\r
185     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
186 \r
187     /* Set font */\r
188     SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));\r
189 \r
190     count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE, FALSE );\r
191 \r
192     SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );\r
193     SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );\r
194 \r
195     filterHasFocus = FALSE;\r
196 \r
197     /* Size and position the dialog */\r
198     if (!gameListDialog) {\r
199       gameListDialog = hDlg;\r
200       flags = SWP_NOZORDER;\r
201       GetClientRect(hDlg, &rect);\r
202       sizeX = rect.right;\r
203       sizeY = rect.bottom;\r
204       if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&\r
205           wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {\r
206         WINDOWPLACEMENT wp;\r
207         EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);\r
208         wp.length = sizeof(WINDOWPLACEMENT);\r
209         wp.flags = 0;\r
210         wp.showCmd = SW_SHOW;\r
211         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
212         wp.rcNormalPosition.left = wpGameList.x;\r
213         wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;\r
214         wp.rcNormalPosition.top = wpGameList.y;\r
215         wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;\r
216         SetWindowPlacement(hDlg, &wp);\r
217 \r
218         GetClientRect(hDlg, &rect);\r
219         newSizeX = rect.right;\r
220         newSizeY = rect.bottom;\r
221         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,\r
222                               newSizeX, newSizeY);\r
223         sizeX = newSizeX;\r
224         sizeY = newSizeY;\r
225       } else\r
226         GetActualPlacement( gameListDialog, &wpGameList );\r
227 \r
228     }\r
229       GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title\r
230     GameListHighlight(lastLoadGameNumber);\r
231     return FALSE;\r
232 \r
233   case WM_SIZE:\r
234     newSizeX = LOWORD(lParam);\r
235     newSizeY = HIWORD(lParam);\r
236     ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),\r
237       sizeX, sizeY, newSizeX, newSizeY);\r
238     sizeX = newSizeX;\r
239     sizeY = newSizeY;\r
240     break;\r
241 \r
242   case WM_ENTERSIZEMOVE:\r
243     return OnEnterSizeMove( &sd, hDlg, wParam, lParam );\r
244 \r
245   case WM_SIZING:\r
246     return OnSizing( &sd, hDlg, wParam, lParam );\r
247 \r
248   case WM_MOVING:\r
249     return OnMoving( &sd, hDlg, wParam, lParam );\r
250 \r
251   case WM_EXITSIZEMOVE:\r
252     return OnExitSizeMove( &sd, hDlg, wParam, lParam );\r
253 \r
254   case WM_GETMINMAXINFO:\r
255     /* Prevent resizing window too small */\r
256     mmi = (MINMAXINFO *) lParam;\r
257     mmi->ptMinTrackSize.x = 100;\r
258     mmi->ptMinTrackSize.y = 100;\r
259     break;\r
260 \r
261   case WM_COMMAND:\r
262       /*\r
263         [AS]\r
264         If <Enter> is pressed while editing the filter, it's better to apply\r
265         the filter rather than selecting the current game.\r
266       */\r
267       if( LOWORD(wParam) == IDC_GameListFilter ) {\r
268           switch( HIWORD(wParam) ) {\r
269           case EN_SETFOCUS:\r
270               filterHasFocus = TRUE;\r
271               break;\r
272           case EN_KILLFOCUS:\r
273               filterHasFocus = FALSE;\r
274               break;\r
275           }\r
276       }\r
277 \r
278       if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {\r
279           wParam = IDC_GameListDoFilter;\r
280       }\r
281       /* [AS] End command replacement */\r
282 \r
283     switch (LOWORD(wParam)) {\r
284     case OPT_GameListLoad:\r
285       LoadOptionsPopup(hDlg);\r
286       return TRUE;\r
287     case IDOK:\r
288       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
289       if (nItem < 0) {\r
290         /* is this possible? */\r
291         DisplayError(_("No game selected"), 0);\r
292         return TRUE;\r
293       }\r
294       break; /* load the game*/\r
295 \r
296     case OPT_GameListNext:\r
297       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
298       nItem++;\r
299       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
300         /* [AS] Removed error message */\r
301         /* DisplayError(_("Can't go forward any further"), 0); */\r
302         return TRUE;\r
303       }\r
304       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
305       break; /* load the game*/\r
306 \r
307     case OPT_GameListPrev:\r
308 #if 0\r
309       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
310       nItem--;\r
311       if (nItem < 0) {\r
312         /* [AS] Removed error message, added return */\r
313         /* DisplayError(_("Can't back up any further"), 0); */\r
314         return TRUE;\r
315       }\r
316       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
317       break; /* load the game*/\r
318 #endif\r
319     /* [AS] */\r
320     case OPT_GameListFind:\r
321     case IDC_GameListDoFilter:\r
322         {\r
323             char filter[MAX_FILTER_LENGTH+1];\r
324 \r
325             if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {\r
326                 filter[ sizeof(filter)-1 ] = '\0';\r
327                 count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter, LOWORD(wParam)==OPT_GameListNarrow );\r
328                 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );\r
329             }\r
330         }\r
331         return FALSE;\r
332         break;\r
333 \r
334     case IDCANCEL:\r
335     case OPT_GameListClose:\r
336       GameListPopDown();\r
337       return TRUE;\r
338 \r
339     case OPT_GameListText:\r
340       switch (HIWORD(wParam)) {\r
341       case LBN_DBLCLK:\r
342         nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);\r
343         break; /* load the game*/\r
344 \r
345       default:\r
346         return FALSE;\r
347       }\r
348       break;\r
349 \r
350     default:\r
351       return FALSE;\r
352     }\r
353 \r
354     /* Load the game */\r
355     {\r
356         /* [AS] Get index from the item itself, because filtering makes original order unuseable. */\r
357         int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
358         char * text;\r
359         LRESULT res;\r
360 \r
361         if( index < 0 ) {\r
362             return TRUE;\r
363         }\r
364 \r
365         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );\r
366 \r
367         if( res == LB_ERR ) {\r
368             return TRUE;\r
369         }\r
370 \r
371         text = (char *) malloc( res+1 );\r
372 \r
373         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );\r
374 \r
375         index = atoi( text );\r
376 \r
377         nItem = index - 1;\r
378 \r
379         free( text );\r
380         /* [AS] End: nItem has been "patched" now! */\r
381 \r
382         if (cmailMsgLoaded) {\r
383             CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
384         }\r
385         else {\r
386             LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
387             SetFocus(hwndMain); // [HGM] automatic focus switch\r
388         }\r
389     }\r
390 \r
391     return TRUE;\r
392 \r
393   default:\r
394     break;\r
395   }\r
396   return FALSE;\r
397 }\r
398 \r
399 \r
400 VOID GameListPopUp(FILE *fp, char *filename)\r
401 {\r
402   FARPROC lpProc;\r
403 \r
404   gameFile = fp;\r
405   if (gameFileName != filename) {\r
406     if (gameFileName) free(gameFileName);\r
407     gameFileName = StrSave(filename);\r
408   }\r
409   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
410   if (gameListDialog) {\r
411     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
412     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
413     else SetFocus(gameListDialog);\r
414   } else {\r
415     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
416     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
417       hwndMain, (DLGPROC)lpProc);\r
418     FreeProcInstance(lpProc);\r
419   }\r
420   gameListUp = TRUE;\r
421 }\r
422 \r
423 FILE *GameFile()\r
424 {\r
425   return gameFile;\r
426 }\r
427 \r
428 VOID GameListPopDown(void)\r
429 {\r
430   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
431   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
432   gameListUp = FALSE;\r
433 }\r
434 \r
435 \r
436 VOID GameListHighlight(int index)\r
437 {\r
438   char buf[MSG_SIZ];\r
439   int i, res = 0;\r
440   if (gameListDialog == NULL) return;\r
441   for(i=0; res != LB_ERR; i++) {\r
442         res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );\r
443         if(index <= atoi( buf )) break;\r
444   }\r
445   SendDlgItemMessage(gameListDialog, OPT_GameListText,\r
446     LB_SETCURSEL, i, 0);\r
447 }\r
448 \r
449 \r
450 VOID GameListDestroy()\r
451 {\r
452   GameListPopDown();\r
453   if (gameFileName) {\r
454     free(gameFileName);\r
455     gameFileName = NULL;\r
456   }\r
457 }\r
458 \r
459 VOID ShowGameListProc()\r
460 {\r
461   if (gameListUp) {\r
462     if(gameListDialog) SetFocus(gameListDialog);\r
463 //    GameListPopDown();\r
464   } else {\r
465     if (gameFileName) {\r
466       GameListPopUp(gameFile, gameFileName);\r
467     } else {\r
468       DisplayError(_("No game list"), 0);\r
469     }\r
470   }\r
471 }\r
472 \r
473 HGLOBAL ExportGameListAsText()\r
474 {\r
475     HGLOBAL result = NULL;\r
476     LPVOID lpMem = NULL;\r
477     ListGame * lg = (ListGame *) gameList.head;\r
478     int nItem;\r
479     DWORD dwLen = 0;\r
480 \r
481     if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {\r
482         DisplayError(_(_("Game list not loaded or empty")), 0);\r
483         return NULL;\r
484     }\r
485 \r
486     /* Get list size */\r
487     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
488         char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
489 \r
490         dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */\r
491 \r
492         free(st);\r
493         lg = (ListGame *) lg->node.succ;\r
494     }\r
495 \r
496     /* Allocate memory for the list */\r
497     result = GlobalAlloc(GHND, dwLen+1 );\r
498 \r
499     if( result != NULL ) {\r
500         lpMem = GlobalLock(result);\r
501     }\r
502 \r
503     /* Copy the list into the global memory block */\r
504     if( lpMem != NULL ) {\r
505         char * dst = (char *) lpMem;\r
506         size_t len;\r
507 \r
508         lg = (ListGame *) gameList.head;\r
509 \r
510         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
511             char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
512 \r
513             len = sprintf( dst, "%s\r\n", st );\r
514             dst += len;\r
515 \r
516             free(st);\r
517             lg = (ListGame *) lg->node.succ;\r
518         }\r
519 \r
520         GlobalUnlock( result );\r
521     }\r
522 \r
523     return result;\r
524 }\r