5fa191c71a6f69545edea0e34f6d82f15f3d2aee
[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 $
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"
40
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
53 {
54     int white_wins;
55     int black_wins;
56     int drawn;
57     int unfinished;
58 };
59
60 /* [AS] Wildcard pattern matching */
61 static BOOL HasPattern( const char * text, const char * pattern )
62 {
63     while( *pattern != '\0' ) {
64         if( *pattern == '*' ) {
65             while( *pattern == '*' ) {
66                 pattern++;
67             }
68
69             if( *pattern == '\0' ) {
70                 return TRUE;
71             }
72
73             while( *text != '\0' ) {
74                 if( HasPattern( text, pattern ) ) {
75                     return TRUE;
76                 }
77                 text++;
78             }
79         }
80         else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
81             pattern++;
82             text++;
83             continue;
84         }
85
86         return FALSE;
87     }
88
89     return TRUE;
90 }
91
92 static BOOL SearchPattern( const char * text, const char * pattern )
93 {
94     BOOL result = TRUE;
95
96     if( pattern != NULL && *pattern != '\0' ) {
97         if( *pattern == '*' ) {
98             result = HasPattern( text, pattern );
99         }
100         else {
101             result = FALSE;
102
103             while( *text != '\0' ) {
104                 if( HasPattern( text, pattern ) ) {
105                     result = TRUE;
106                     break;
107                 }
108                 text++;
109             }
110         }
111     }
112
113     return result;
114 }
115
116 /* [AS] Setup the game list according to the specified filter */
117 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
118 {
119     ListGame * lg = (ListGame *) gameList.head;
120     int nItem;
121     BOOL hasFilter = FALSE;
122     int count = 0;
123     struct GameListStats dummy;
124
125     /* Initialize stats (use a dummy variable if caller not interested in them) */
126     if( stats == NULL ) {
127         stats = &dummy;
128     }
129
130     stats->white_wins = 0;
131     stats->black_wins = 0;
132     stats->drawn = 0;
133     stats->unfinished = 0;
134
135     if( boReset ) {
136         SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
137     }
138
139     if( pszFilter != NULL ) {
140         if( strlen( pszFilter ) > 0 ) {
141             hasFilter = TRUE;
142         }
143     }
144
145     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
146         char * st = GameListLine(lg->number, &lg->gameInfo);
147         BOOL skip = FALSE;
148
149         if( hasFilter ) {
150             if( ! SearchPattern( st, pszFilter ) ) {
151                 skip = TRUE;
152             }
153         }
154
155         if( ! skip ) {
156             SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
157             count++;
158
159             /* Update stats */
160             if( lg->gameInfo.result == WhiteWins )
161                 stats->white_wins++;
162             else if( lg->gameInfo.result == BlackWins )
163                 stats->black_wins++;
164             else if( lg->gameInfo.result == GameIsDrawn )
165                 stats->drawn++;
166             else
167                 stats->unfinished++;
168         }
169
170         free(st);
171         lg = (ListGame *) lg->node.succ;
172     }
173
174     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
175
176     return count;
177 }
178
179 /* [AS] Show number of visible (filtered) games and total on window caption */
180 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
181 {
182     char buf[256];
183
184     sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
185
186     if( stats != 0 ) {
187         sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
188     }
189
190     SetWindowText( hDlg, buf );
191
192     return 0;
193 }
194
195 #define MAX_FILTER_LENGTH   128
196 \r
197 LRESULT CALLBACK\r
198 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
199 {\r
200   static char szDlgTitle[64];
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;
208   int count;
209   struct GameListStats stats;
210   static SnapData sd;
211 \r
212   switch (message) {\r
213   case WM_INITDIALOG: \r
214     GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
215     szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
216
217     if (gameListDialog) {\r
218       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
219     }\r
220
221     /* Initialize the dialog items */\r
222     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
223
224     count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
225
226     SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
227     SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
228
229     filterHasFocus = FALSE;
230
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
261       GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
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:
275     return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
276
277   case WM_SIZING:
278     return OnSizing( &sd, hDlg, wParam, lParam );
279
280   case WM_MOVING:
281     return OnMoving( &sd, hDlg, wParam, lParam );
282
283   case WM_EXITSIZEMOVE:
284     return OnExitSizeMove( &sd, hDlg, wParam, lParam );
285
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       /*
295         [AS]
296         If <Enter> is pressed while editing the filter, it's better to apply
297         the filter rather than selecting the current game.
298       */
299       if( LOWORD(wParam) == IDC_GameListFilter ) {
300           switch( HIWORD(wParam) ) {
301           case EN_SETFOCUS:
302               filterHasFocus = TRUE;
303               break;
304           case EN_KILLFOCUS:
305               filterHasFocus = FALSE;
306               break;
307           }
308       }
309
310       if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
311           wParam = IDC_GameListDoFilter;
312       }
313       /* [AS] End command replacement */
314
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 */
331         /* DisplayError("Can't go forward any further", 0); */
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 */
342         /* DisplayError("Can't back up any further", 0); */
343         return TRUE;
344       }\r
345       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
346       break; /* load the game*/\r
347       \r
348     /* [AS] */
349     case IDC_GameListDoFilter:
350         {
351             char filter[MAX_FILTER_LENGTH+1];
352
353             if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
354                 filter[ sizeof(filter)-1 ] = '\0';
355                 count = GameListToListBox( hDlg, TRUE, filter, &stats );
356                 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
357             }
358         }
359         return FALSE;
360         break;
361
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
382     /* Load the game */\r
383     {
384         /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
385         int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
386         char * text;
387         LRESULT res;
388
389         if( index < 0 ) {
390             return TRUE;
391         }
392
393         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
394
395         if( res == LB_ERR ) {
396             return TRUE;
397         }
398
399         text = (char *) malloc( res+1 );
400
401         res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
402
403         index = atoi( text );
404
405         nItem = index - 1;
406
407         free( text );
408         /* [AS] End: nItem has been "patched" now! */
409
410     if (cmailMsgLoaded) {\r
411       CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
412         }
413         else {
414       LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
415     }\r
416     }
417
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
487 HGLOBAL ExportGameListAsText()
488 {
489     HGLOBAL result = NULL;
490     LPVOID lpMem = NULL;
491     ListGame * lg = (ListGame *) gameList.head;
492     int nItem;
493     DWORD dwLen = 0;
494
495     if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
496         DisplayError("Game list not loaded or empty", 0);
497         return NULL;
498     }
499
500     /* Get list size */
501     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
502         char * st = GameListLineFull(lg->number, &lg->gameInfo);
503
504         dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
505
506         free(st);
507         lg = (ListGame *) lg->node.succ;
508     }
509
510     /* Allocate memory for the list */
511     result = GlobalAlloc(GHND, dwLen+1 );
512
513     if( result != NULL ) {
514         lpMem = GlobalLock(result);
515     }
516
517     /* Copy the list into the global memory block */
518     if( lpMem != NULL ) {
519         char * dst = (char *) lpMem;
520         size_t len;
521
522         lg = (ListGame *) gameList.head;
523
524         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
525             char * st = GameListLineFull(lg->number, &lg->gameInfo);
526
527             len = sprintf( dst, "%s\r\n", st );
528             dst += len;
529
530             free(st);
531             lg = (ListGame *) lg->node.succ;
532         }
533
534         GlobalUnlock( result );
535     }
536
537     return result;
538 }