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