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