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
5 * Copyright 1995,2009 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\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
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
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
22 *------------------------------------------------------------------------
\r
23 ** See the file ChangeLog for a revision history. */
\r
27 #include <windows.h> /* required for all Windows applications */
\r
33 #include <commdlg.h>
\r
37 #include "winboard.h"
\r
38 #include "frontend.h"
\r
39 #include "backend.h"
\r
42 #include "wgamelist.h"
\r
44 extern BoardSize boardSize;
\r
46 /* Module globals */
\r
47 HWND gameListDialog = NULL;
\r
48 BOOLEAN gameListUp = FALSE;
\r
50 char* gameFileName = NULL;
\r
52 /* Imports from winboard.c */
\r
53 extern HINSTANCE hInst;
\r
54 extern HWND hwndMain;
\r
55 extern WindowPlacement wpGameList;
\r
57 struct GameListStats
\r
65 /* [AS] Wildcard pattern matching */
\r
66 static BOOL HasPattern( const char * text, const char * pattern )
\r
68 while( *pattern != '\0' ) {
\r
69 if( *pattern == '*' ) {
\r
70 while( *pattern == '*' ) {
\r
74 if( *pattern == '\0' ) {
\r
78 while( *text != '\0' ) {
\r
79 if( HasPattern( text, pattern ) ) {
\r
85 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
97 static BOOL SearchPattern( const char * text, const char * pattern )
\r
101 if( pattern != NULL && *pattern != '\0' ) {
\r
102 if( *pattern == '*' ) {
\r
103 result = HasPattern( text, pattern );
\r
108 while( *text != '\0' ) {
\r
109 if( HasPattern( text, pattern ) ) {
\r
121 /* [AS] Setup the game list according to the specified filter */
\r
122 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
124 ListGame * lg = (ListGame *) gameList.head;
\r
126 BOOL hasFilter = FALSE;
\r
128 struct GameListStats dummy;
\r
130 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
131 if( stats == NULL ) {
\r
135 stats->white_wins = 0;
\r
136 stats->black_wins = 0;
\r
138 stats->unfinished = 0;
\r
141 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
144 if( pszFilter != NULL ) {
\r
145 if( strlen( pszFilter ) > 0 ) {
\r
150 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
151 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
155 if( ! SearchPattern( st, pszFilter ) ) {
\r
161 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
165 if( lg->gameInfo.result == WhiteWins )
\r
166 stats->white_wins++;
\r
167 else if( lg->gameInfo.result == BlackWins )
\r
168 stats->black_wins++;
\r
169 else if( lg->gameInfo.result == GameIsDrawn )
\r
172 stats->unfinished++;
\r
176 lg = (ListGame *) lg->node.succ;
\r
179 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
184 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
185 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
189 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
192 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
195 SetWindowText( hDlg, buf );
\r
200 #define MAX_FILTER_LENGTH 128
\r
203 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
205 static char szDlgTitle[64];
\r
206 static HANDLE hwndText;
\r
209 static int sizeX, sizeY;
\r
210 int newSizeX, newSizeY, flags;
\r
212 static BOOL filterHasFocus = FALSE;
\r
214 struct GameListStats stats;
\r
215 static SnapData sd;
\r
218 case WM_INITDIALOG:
\r
219 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
220 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
222 if (gameListDialog) {
\r
223 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
226 /* Initialize the dialog items */
\r
227 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
230 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
232 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
234 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
235 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
237 filterHasFocus = FALSE;
\r
239 /* Size and position the dialog */
\r
240 if (!gameListDialog) {
\r
241 gameListDialog = hDlg;
\r
242 flags = SWP_NOZORDER;
\r
243 GetClientRect(hDlg, &rect);
\r
244 sizeX = rect.right;
\r
245 sizeY = rect.bottom;
\r
246 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
247 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
248 WINDOWPLACEMENT wp;
\r
249 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
250 wp.length = sizeof(WINDOWPLACEMENT);
\r
252 wp.showCmd = SW_SHOW;
\r
253 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
254 wp.rcNormalPosition.left = wpGameList.x;
\r
255 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
256 wp.rcNormalPosition.top = wpGameList.y;
\r
257 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
258 SetWindowPlacement(hDlg, &wp);
\r
260 GetClientRect(hDlg, &rect);
\r
261 newSizeX = rect.right;
\r
262 newSizeY = rect.bottom;
\r
263 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
264 newSizeX, newSizeY);
\r
269 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
274 newSizeX = LOWORD(lParam);
\r
275 newSizeY = HIWORD(lParam);
\r
276 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
277 sizeX, sizeY, newSizeX, newSizeY);
\r
282 case WM_ENTERSIZEMOVE:
\r
283 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
286 return OnSizing( &sd, hDlg, wParam, lParam );
\r
289 return OnMoving( &sd, hDlg, wParam, lParam );
\r
291 case WM_EXITSIZEMOVE:
\r
292 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
294 case WM_GETMINMAXINFO:
\r
295 /* Prevent resizing window too small */
\r
296 mmi = (MINMAXINFO *) lParam;
\r
297 mmi->ptMinTrackSize.x = 100;
\r
298 mmi->ptMinTrackSize.y = 100;
\r
304 If <Enter> is pressed while editing the filter, it's better to apply
\r
305 the filter rather than selecting the current game.
\r
307 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
308 switch( HIWORD(wParam) ) {
\r
310 filterHasFocus = TRUE;
\r
313 filterHasFocus = FALSE;
\r
318 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
319 wParam = IDC_GameListDoFilter;
\r
321 /* [AS] End command replacement */
\r
323 switch (LOWORD(wParam)) {
\r
325 case OPT_GameListLoad:
\r
326 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
328 /* is this possible? */
\r
329 DisplayError("No game selected", 0);
\r
332 break; /* load the game*/
\r
334 case OPT_GameListNext:
\r
335 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
337 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
338 /* [AS] Removed error message */
\r
339 /* DisplayError("Can't go forward any further", 0); */
\r
342 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
343 break; /* load the game*/
\r
345 case OPT_GameListPrev:
\r
346 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
349 /* [AS] Removed error message, added return */
\r
350 /* DisplayError("Can't back up any further", 0); */
\r
353 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
354 break; /* load the game*/
\r
357 case IDC_GameListDoFilter:
\r
359 char filter[MAX_FILTER_LENGTH+1];
\r
361 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
362 filter[ sizeof(filter)-1 ] = '\0';
\r
363 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
364 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
371 case OPT_GameListClose:
\r
375 case OPT_GameListText:
\r
376 switch (HIWORD(wParam)) {
\r
378 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
379 break; /* load the game*/
\r
390 /* Load the game */
\r
392 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
393 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
401 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
403 if( res == LB_ERR ) {
\r
407 text = (char *) malloc( res+1 );
\r
409 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
411 index = atoi( text );
\r
416 /* [AS] End: nItem has been "patched" now! */
\r
418 if (cmailMsgLoaded) {
\r
419 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
422 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
435 VOID GameListPopUp(FILE *fp, char *filename)
\r
440 if (gameFileName != filename) {
\r
441 if (gameFileName) free(gameFileName);
\r
442 gameFileName = StrSave(filename);
\r
444 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
445 if (gameListDialog) {
\r
446 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
447 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
449 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
450 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
451 hwndMain, (DLGPROC)lpProc);
\r
452 FreeProcInstance(lpProc);
\r
457 VOID GameListPopDown(void)
\r
459 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
460 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
461 gameListUp = FALSE;
\r
465 VOID GameListHighlight(int index)
\r
467 if (gameListDialog == NULL) return;
\r
468 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
469 LB_SETCURSEL, index - 1, 0);
\r
473 VOID GameListDestroy()
\r
476 if (gameFileName) {
\r
477 free(gameFileName);
\r
478 gameFileName = NULL;
\r
482 VOID ShowGameListProc()
\r
487 if (gameFileName) {
\r
488 GameListPopUp(gameFile, gameFileName);
\r
490 DisplayError("No game list", 0);
\r
495 HGLOBAL ExportGameListAsText()
\r
497 HGLOBAL result = NULL;
\r
498 LPVOID lpMem = NULL;
\r
499 ListGame * lg = (ListGame *) gameList.head;
\r
503 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
504 DisplayError("Game list not loaded or empty", 0);
\r
508 /* Get list size */
\r
509 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
510 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
512 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
515 lg = (ListGame *) lg->node.succ;
\r
518 /* Allocate memory for the list */
\r
519 result = GlobalAlloc(GHND, dwLen+1 );
\r
521 if( result != NULL ) {
\r
522 lpMem = GlobalLock(result);
\r
525 /* Copy the list into the global memory block */
\r
526 if( lpMem != NULL ) {
\r
527 char * dst = (char *) lpMem;
\r
530 lg = (ListGame *) gameList.head;
\r
532 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
533 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
535 len = sprintf( dst, "%s\r\n", st );
\r
539 lg = (ListGame *) lg->node.succ;
\r
542 GlobalUnlock( result );
\r