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