Updated all files to GPL version 3.
[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,2009 Free Software Foundation, Inc.\r
6  *\r
7  * ------------------------------------------------------------------------\r
8  *\r
9  * GNU XBoard is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or (at\r
12  * your option) any later version.\r
13  *\r
14  * GNU XBoard is distributed in the hope that it will be useful, but\r
15  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
17  * General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
21  *\r
22  *------------------------------------------------------------------------\r
23  ** See the file ChangeLog for a revision history.  */\r
24 \r
25 #include "config.h"\r
26 \r
27 #include <windows.h> /* required for all Windows applications */\r
28 #include <stdio.h>\r
29 #include <stdlib.h>\r
30 #include <malloc.h>\r
31 #include <fcntl.h>\r
32 #include <math.h>\r
33 #include <commdlg.h>\r
34 #include <dlgs.h>\r
35 \r
36 #include "common.h"\r
37 #include "winboard.h"\r
38 #include "frontend.h"\r
39 #include "backend.h"\r
40 \r
41 #include "wsnap.h"\r
42 \r
43 /* Module globals */\r
44 HWND gameListDialog = NULL;\r
45 BOOLEAN gameListUp = FALSE;\r
46 FILE* gameFile;\r
47 char* gameFileName = NULL;\r
48 int gameListX, gameListY, gameListW, gameListH;\r
49 \r
50 /* Imports from winboard.c */\r
51 extern HINSTANCE hInst;\r
52 extern HWND hwndMain;\r
53 \r
54 struct GameListStats\r
55 {\r
56     int white_wins;\r
57     int black_wins;\r
58     int drawn;\r
59     int unfinished;\r
60 };\r
61 \r
62 /* [AS] Wildcard pattern matching */\r
63 static BOOL HasPattern( const char * text, const char * pattern )\r
64 {\r
65     while( *pattern != '\0' ) {\r
66         if( *pattern == '*' ) {\r
67             while( *pattern == '*' ) {\r
68                 pattern++;\r
69             }\r
70 \r
71             if( *pattern == '\0' ) {\r
72                 return TRUE;\r
73             }\r
74 \r
75             while( *text != '\0' ) {\r
76                 if( HasPattern( text, pattern ) ) {\r
77                     return TRUE;\r
78                 }\r
79                 text++;\r
80             }\r
81         }\r
82         else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {\r
83             pattern++;\r
84             text++;\r
85             continue;\r
86         }\r
87 \r
88         return FALSE;\r
89     }\r
90 \r
91     return TRUE;\r
92 }\r
93 \r
94 static BOOL SearchPattern( const char * text, const char * pattern )\r
95 {\r
96     BOOL result = TRUE;\r
97 \r
98     if( pattern != NULL && *pattern != '\0' ) {\r
99         if( *pattern == '*' ) {\r
100             result = HasPattern( text, pattern );\r
101         }\r
102         else {\r
103             result = FALSE;\r
104 \r
105             while( *text != '\0' ) {\r
106                 if( HasPattern( text, pattern ) ) {\r
107                     result = TRUE;\r
108                     break;\r
109                 }\r
110                 text++;\r
111             }\r
112         }\r
113     }\r
114 \r
115     return result;\r
116 }\r
117 \r
118 /* [AS] Setup the game list according to the specified filter */\r
119 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )\r
120 {\r
121     ListGame * lg = (ListGame *) gameList.head;\r
122     int nItem;\r
123     BOOL hasFilter = FALSE;\r
124     int count = 0;\r
125     struct GameListStats dummy;\r
126 \r
127     /* Initialize stats (use a dummy variable if caller not interested in them) */\r
128     if( stats == NULL ) { \r
129         stats = &dummy;\r
130     }\r
131 \r
132     stats->white_wins = 0;\r
133     stats->black_wins = 0;\r
134     stats->drawn = 0;\r
135     stats->unfinished = 0;\r
136 \r
137     if( boReset ) {\r
138         SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
139     }\r
140 \r
141     if( pszFilter != NULL ) {\r
142         if( strlen( pszFilter ) > 0 ) {\r
143             hasFilter = TRUE;\r
144         }\r
145     }\r
146 \r
147     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
148         char * st = GameListLine(lg->number, &lg->gameInfo);\r
149         BOOL skip = FALSE;\r
150 \r
151         if( hasFilter ) {\r
152             if( ! SearchPattern( st, pszFilter ) ) {\r
153                 skip = TRUE;\r
154             }\r
155         }\r
156 \r
157         if( ! skip ) {\r
158             SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
159             count++;\r
160 \r
161             /* Update stats */\r
162             if( lg->gameInfo.result == WhiteWins )\r
163                 stats->white_wins++;\r
164             else if( lg->gameInfo.result == BlackWins )\r
165                 stats->black_wins++;\r
166             else if( lg->gameInfo.result == GameIsDrawn )\r
167                 stats->drawn++;\r
168             else\r
169                 stats->unfinished++;\r
170         }\r
171 \r
172         free(st);\r
173         lg = (ListGame *) lg->node.succ;\r
174     }\r
175 \r
176     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
177 \r
178     return count;\r
179 }\r
180 \r
181 /* [AS] Show number of visible (filtered) games and total on window caption */\r
182 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )\r
183 {\r
184     char buf[256];\r
185 \r
186     sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );\r
187 \r
188     if( stats != 0 ) {\r
189         sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );\r
190     }\r
191 \r
192     SetWindowText( hDlg, buf );\r
193 \r
194     return 0;\r
195 }\r
196 \r
197 #define MAX_FILTER_LENGTH   128\r
198 \r
199 LRESULT CALLBACK\r
200 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
201 {\r
202   static char szDlgTitle[64];\r
203   static HANDLE hwndText;\r
204   int nItem;\r
205   RECT rect;\r
206   static int sizeX, sizeY;\r
207   int newSizeX, newSizeY, flags;\r
208   MINMAXINFO *mmi;\r
209   static BOOL filterHasFocus = FALSE;\r
210   int count;\r
211   struct GameListStats stats;\r
212   static SnapData sd;\r
213 \r
214   switch (message) {\r
215   case WM_INITDIALOG: \r
216     GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );\r
217     szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';\r
218 \r
219     if (gameListDialog) {\r
220       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
221     }\r
222 \r
223     /* Initialize the dialog items */\r
224     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
225 \r
226     count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );\r
227 \r
228     SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );\r
229     SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );\r
230 \r
231     filterHasFocus = FALSE;\r
232 \r
233     /* Size and position the dialog */\r
234     if (!gameListDialog) {\r
235       gameListDialog = hDlg;\r
236       flags = SWP_NOZORDER;\r
237       GetClientRect(hDlg, &rect);\r
238       sizeX = rect.right;\r
239       sizeY = rect.bottom;\r
240       if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&\r
241           gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {\r
242         WINDOWPLACEMENT wp;\r
243         EnsureOnScreen(&gameListX, &gameListY);\r
244         wp.length = sizeof(WINDOWPLACEMENT);\r
245         wp.flags = 0;\r
246         wp.showCmd = SW_SHOW;\r
247         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
248         wp.rcNormalPosition.left = gameListX;\r
249         wp.rcNormalPosition.right = gameListX + gameListW;\r
250         wp.rcNormalPosition.top = gameListY;\r
251         wp.rcNormalPosition.bottom = gameListY + gameListH;\r
252         SetWindowPlacement(hDlg, &wp);\r
253 \r
254         GetClientRect(hDlg, &rect);\r
255         newSizeX = rect.right;\r
256         newSizeY = rect.bottom;\r
257         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,\r
258                               newSizeX, newSizeY);\r
259         sizeX = newSizeX;\r
260         sizeY = newSizeY;\r
261       }\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             LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
417         }\r
418     }\r
419 \r
420     return TRUE;\r
421 \r
422   default:\r
423     break;\r
424   }\r
425   return FALSE;\r
426 }\r
427 \r
428 \r
429 VOID GameListPopUp(FILE *fp, char *filename)\r
430 {\r
431   FARPROC lpProc;\r
432   \r
433   gameFile = fp;\r
434   if (gameFileName != filename) {\r
435     if (gameFileName) free(gameFileName);\r
436     gameFileName = StrSave(filename);\r
437   }\r
438   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
439   if (gameListDialog) {\r
440     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
441     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
442   } else {\r
443     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
444     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
445       hwndMain, (DLGPROC)lpProc);\r
446     FreeProcInstance(lpProc);\r
447   }\r
448   gameListUp = TRUE;\r
449 }\r
450 \r
451 VOID GameListPopDown(void)\r
452 {\r
453   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
454   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
455   gameListUp = FALSE;\r
456 }\r
457 \r
458 \r
459 VOID GameListHighlight(int index)\r
460 {\r
461   if (gameListDialog == NULL) return;\r
462   SendDlgItemMessage(gameListDialog, OPT_GameListText, \r
463     LB_SETCURSEL, index - 1, 0);\r
464 }\r
465 \r
466 \r
467 VOID GameListDestroy()\r
468 {\r
469   GameListPopDown();\r
470   if (gameFileName) {\r
471     free(gameFileName);\r
472     gameFileName = NULL;\r
473   }\r
474 }\r
475 \r
476 VOID ShowGameListProc()\r
477 {\r
478   if (gameListUp) {\r
479     GameListPopDown();\r
480   } else {\r
481     if (gameFileName) {\r
482       GameListPopUp(gameFile, gameFileName);\r
483     } else {\r
484       DisplayError("No game list", 0);\r
485     }\r
486   }\r
487 }\r
488 \r
489 HGLOBAL ExportGameListAsText()\r
490 {\r
491     HGLOBAL result = NULL;\r
492     LPVOID lpMem = NULL;\r
493     ListGame * lg = (ListGame *) gameList.head;\r
494     int nItem;\r
495     DWORD dwLen = 0;\r
496 \r
497     if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {\r
498         DisplayError("Game list not loaded or empty", 0);\r
499         return NULL;\r
500     }\r
501 \r
502     /* Get list size */\r
503     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
504         char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
505 \r
506         dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */\r
507 \r
508         free(st);\r
509         lg = (ListGame *) lg->node.succ;\r
510     }\r
511 \r
512     /* Allocate memory for the list */\r
513     result = GlobalAlloc(GHND, dwLen+1 );\r
514 \r
515     if( result != NULL ) {\r
516         lpMem = GlobalLock(result);\r
517     }\r
518 \r
519     /* Copy the list into the global memory block */\r
520     if( lpMem != NULL ) {\r
521         char * dst = (char *) lpMem;\r
522         size_t len;\r
523 \r
524         lg = (ListGame *) gameList.head;\r
525 \r
526         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
527             char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
528 \r
529             len = sprintf( dst, "%s\r\n", st );\r
530             dst += len;\r
531 \r
532             free(st);\r
533             lg = (ListGame *) lg->node.succ;\r
534         }\r
535 \r
536         GlobalUnlock( result );\r
537     }\r
538 \r
539     return result;\r
540 }\r