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