2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995,2009 Free Software Foundation, Inc.
\r
6 * ------------------------------------------------------------------------
\r
8 * GNU XBoard 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 3 of the License, or (at
\r
11 * your option) any later version.
\r
13 * GNU XBoard is distributed in the hope that it will be useful, but
\r
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
16 * General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
21 *------------------------------------------------------------------------
\r
22 ** See the file ChangeLog for a revision history. */
\r
26 #include <windows.h> /* required for all Windows applications */
\r
32 #include <commdlg.h>
\r
36 #include "winboard.h"
\r
37 #include "frontend.h"
\r
38 #include "backend.h"
\r
41 #include "wgamelist.h"
\r
43 extern BoardSize boardSize;
\r
45 /* Module globals */
\r
46 HWND gameListDialog = NULL;
\r
47 BOOLEAN gameListUp = FALSE;
\r
49 char* gameFileName = NULL;
\r
51 /* Imports from winboard.c */
\r
52 extern HINSTANCE hInst;
\r
53 extern HWND hwndMain;
\r
54 extern WindowPlacement wpGameList;
\r
56 struct GameListStats
\r
64 /* [AS] Wildcard pattern matching */
\r
65 static BOOL HasPattern( const char * text, const char * pattern )
\r
67 while( *pattern != '\0' ) {
\r
68 if( *pattern == '*' ) {
\r
69 while( *pattern == '*' ) {
\r
73 if( *pattern == '\0' ) {
\r
77 while( *text != '\0' ) {
\r
78 if( HasPattern( text, pattern ) ) {
\r
84 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
96 static BOOL SearchPattern( const char * text, const char * pattern )
\r
100 if( pattern != NULL && *pattern != '\0' ) {
\r
101 if( *pattern == '*' ) {
\r
102 result = HasPattern( text, pattern );
\r
107 while( *text != '\0' ) {
\r
108 if( HasPattern( text, pattern ) ) {
\r
120 /* [AS] Setup the game list according to the specified filter */
\r
121 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
123 ListGame * lg = (ListGame *) gameList.head;
\r
125 BOOL hasFilter = FALSE;
\r
127 struct GameListStats dummy;
\r
129 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
130 if( stats == NULL ) {
\r
134 stats->white_wins = 0;
\r
135 stats->black_wins = 0;
\r
137 stats->unfinished = 0;
\r
140 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
143 if( pszFilter != NULL ) {
\r
144 if( strlen( pszFilter ) > 0 ) {
\r
149 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
150 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
154 if( ! SearchPattern( st, pszFilter ) ) {
\r
160 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
164 if( lg->gameInfo.result == WhiteWins )
\r
165 stats->white_wins++;
\r
166 else if( lg->gameInfo.result == BlackWins )
\r
167 stats->black_wins++;
\r
168 else if( lg->gameInfo.result == GameIsDrawn )
\r
171 stats->unfinished++;
\r
175 lg = (ListGame *) lg->node.succ;
\r
178 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
183 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
184 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
188 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
191 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
194 SetWindowText( hDlg, buf );
\r
199 #define MAX_FILTER_LENGTH 128
\r
202 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
204 static char szDlgTitle[64];
\r
205 static HANDLE hwndText;
\r
208 static int sizeX, sizeY;
\r
209 int newSizeX, newSizeY, flags;
\r
211 static BOOL filterHasFocus = FALSE;
\r
213 struct GameListStats stats;
\r
214 static SnapData sd;
\r
217 case WM_INITDIALOG:
\r
218 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
219 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
221 if (gameListDialog) {
\r
222 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
225 /* Initialize the dialog items */
\r
226 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
229 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
231 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
233 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
234 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
236 filterHasFocus = FALSE;
\r
238 /* Size and position the dialog */
\r
239 if (!gameListDialog) {
\r
240 gameListDialog = hDlg;
\r
241 flags = SWP_NOZORDER;
\r
242 GetClientRect(hDlg, &rect);
\r
243 sizeX = rect.right;
\r
244 sizeY = rect.bottom;
\r
245 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
246 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
247 WINDOWPLACEMENT wp;
\r
248 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
249 wp.length = sizeof(WINDOWPLACEMENT);
\r
251 wp.showCmd = SW_SHOW;
\r
252 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
253 wp.rcNormalPosition.left = wpGameList.x;
\r
254 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
255 wp.rcNormalPosition.top = wpGameList.y;
\r
256 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
257 SetWindowPlacement(hDlg, &wp);
\r
259 GetClientRect(hDlg, &rect);
\r
260 newSizeX = rect.right;
\r
261 newSizeY = rect.bottom;
\r
262 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
263 newSizeX, newSizeY);
\r
268 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
273 newSizeX = LOWORD(lParam);
\r
274 newSizeY = HIWORD(lParam);
\r
275 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
276 sizeX, sizeY, newSizeX, newSizeY);
\r
281 case WM_ENTERSIZEMOVE:
\r
282 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
285 return OnSizing( &sd, hDlg, wParam, lParam );
\r
288 return OnMoving( &sd, hDlg, wParam, lParam );
\r
290 case WM_EXITSIZEMOVE:
\r
291 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
293 case WM_GETMINMAXINFO:
\r
294 /* Prevent resizing window too small */
\r
295 mmi = (MINMAXINFO *) lParam;
\r
296 mmi->ptMinTrackSize.x = 100;
\r
297 mmi->ptMinTrackSize.y = 100;
\r
303 If <Enter> is pressed while editing the filter, it's better to apply
\r
304 the filter rather than selecting the current game.
\r
306 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
307 switch( HIWORD(wParam) ) {
\r
309 filterHasFocus = TRUE;
\r
312 filterHasFocus = FALSE;
\r
317 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
318 wParam = IDC_GameListDoFilter;
\r
320 /* [AS] End command replacement */
\r
322 switch (LOWORD(wParam)) {
\r
324 case OPT_GameListLoad:
\r
325 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
327 /* is this possible? */
\r
328 DisplayError("No game selected", 0);
\r
331 break; /* load the game*/
\r
333 case OPT_GameListNext:
\r
334 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
336 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
337 /* [AS] Removed error message */
\r
338 /* DisplayError("Can't go forward any further", 0); */
\r
341 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
342 break; /* load the game*/
\r
344 case OPT_GameListPrev:
\r
345 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
348 /* [AS] Removed error message, added return */
\r
349 /* DisplayError("Can't back up any further", 0); */
\r
352 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
353 break; /* load the game*/
\r
356 case IDC_GameListDoFilter:
\r
358 char filter[MAX_FILTER_LENGTH+1];
\r
360 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
361 filter[ sizeof(filter)-1 ] = '\0';
\r
362 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
363 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
370 case OPT_GameListClose:
\r
374 case OPT_GameListText:
\r
375 switch (HIWORD(wParam)) {
\r
377 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
378 break; /* load the game*/
\r
389 /* Load the game */
\r
391 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
392 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
400 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
402 if( res == LB_ERR ) {
\r
406 text = (char *) malloc( res+1 );
\r
408 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
410 index = atoi( text );
\r
415 /* [AS] End: nItem has been "patched" now! */
\r
417 if (cmailMsgLoaded) {
\r
418 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
421 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
434 VOID GameListPopUp(FILE *fp, char *filename)
\r
439 if (gameFileName != filename) {
\r
440 if (gameFileName) free(gameFileName);
\r
441 gameFileName = StrSave(filename);
\r
443 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
444 if (gameListDialog) {
\r
445 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
446 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
448 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
449 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
450 hwndMain, (DLGPROC)lpProc);
\r
451 FreeProcInstance(lpProc);
\r
456 VOID GameListPopDown(void)
\r
458 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
459 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
460 gameListUp = FALSE;
\r
464 VOID GameListHighlight(int index)
\r
466 if (gameListDialog == NULL) return;
\r
467 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
468 LB_SETCURSEL, index - 1, 0);
\r
472 VOID GameListDestroy()
\r
475 if (gameFileName) {
\r
476 free(gameFileName);
\r
477 gameFileName = NULL;
\r
481 VOID ShowGameListProc()
\r
486 if (gameFileName) {
\r
487 GameListPopUp(gameFile, gameFileName);
\r
489 DisplayError("No game list", 0);
\r
494 HGLOBAL ExportGameListAsText()
\r
496 HGLOBAL result = NULL;
\r
497 LPVOID lpMem = NULL;
\r
498 ListGame * lg = (ListGame *) gameList.head;
\r
502 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
503 DisplayError("Game list not loaded or empty", 0);
\r
507 /* Get list size */
\r
508 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
509 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
511 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
514 lg = (ListGame *) lg->node.succ;
\r
517 /* Allocate memory for the list */
\r
518 result = GlobalAlloc(GHND, dwLen+1 );
\r
520 if( result != NULL ) {
\r
521 lpMem = GlobalLock(result);
\r
524 /* Copy the list into the global memory block */
\r
525 if( lpMem != NULL ) {
\r
526 char * dst = (char *) lpMem;
\r
529 lg = (ListGame *) gameList.head;
\r
531 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
532 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
534 len = sprintf( dst, "%s\r\n", st );
\r
538 lg = (ListGame *) lg->node.succ;
\r
541 GlobalUnlock( result );
\r