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