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