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