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