2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995,2009 Free Software Foundation, Inc.
\r
6 * Enhancements Copyright 2005 Alessandro Scotti
\r
8 * ------------------------------------------------------------------------
\r
10 * GNU XBoard is free software: you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License as published by
\r
12 * the Free Software Foundation, either version 3 of the License, or (at
\r
13 * your option) any later version.
\r
15 * GNU XBoard is distributed in the hope that it will be useful, but
\r
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
18 * General Public License for more details.
\r
20 * You should have received a copy of the GNU General Public License
\r
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
23 *------------------------------------------------------------------------
\r
24 ** See the file ChangeLog for a revision history. */
\r
28 #include <windows.h> /* required for all Windows applications */
\r
34 #include <commdlg.h>
\r
38 #include "winboard.h"
\r
39 #include "frontend.h"
\r
40 #include "backend.h"
\r
43 #include "wgamelist.h"
\r
45 extern BoardSize boardSize;
\r
47 /* Module globals */
\r
48 HWND gameListDialog = NULL;
\r
49 BOOLEAN gameListUp = FALSE;
\r
51 char* gameFileName = NULL;
\r
53 /* Imports from winboard.c */
\r
54 extern HINSTANCE hInst;
\r
55 extern HWND hwndMain;
\r
56 extern WindowPlacement wpGameList;
\r
58 struct GameListStats
\r
66 /* [AS] Wildcard pattern matching */
\r
67 static BOOL HasPattern( const char * text, const char * pattern )
\r
69 while( *pattern != '\0' ) {
\r
70 if( *pattern == '*' ) {
\r
71 while( *pattern == '*' ) {
\r
75 if( *pattern == '\0' ) {
\r
79 while( *text != '\0' ) {
\r
80 if( HasPattern( text, pattern ) ) {
\r
86 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
98 static BOOL SearchPattern( const char * text, const char * pattern )
\r
100 BOOL result = TRUE;
\r
102 if( pattern != NULL && *pattern != '\0' ) {
\r
103 if( *pattern == '*' ) {
\r
104 result = HasPattern( text, pattern );
\r
109 while( *text != '\0' ) {
\r
110 if( HasPattern( text, pattern ) ) {
\r
122 /* [AS] Setup the game list according to the specified filter */
\r
123 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
125 ListGame * lg = (ListGame *) gameList.head;
\r
127 BOOL hasFilter = FALSE;
\r
129 struct GameListStats dummy;
\r
131 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
132 if( stats == NULL ) {
\r
136 stats->white_wins = 0;
\r
137 stats->black_wins = 0;
\r
139 stats->unfinished = 0;
\r
142 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
145 if( pszFilter != NULL ) {
\r
146 if( strlen( pszFilter ) > 0 ) {
\r
151 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
152 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
156 if( ! SearchPattern( st, pszFilter ) ) {
\r
162 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
166 if( lg->gameInfo.result == WhiteWins )
\r
167 stats->white_wins++;
\r
168 else if( lg->gameInfo.result == BlackWins )
\r
169 stats->black_wins++;
\r
170 else if( lg->gameInfo.result == GameIsDrawn )
\r
173 stats->unfinished++;
\r
177 lg = (ListGame *) lg->node.succ;
\r
180 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
185 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
186 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
190 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
193 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
196 SetWindowText( hDlg, buf );
\r
201 #define MAX_FILTER_LENGTH 128
\r
204 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
206 static char szDlgTitle[64];
\r
207 static HANDLE hwndText;
\r
210 static int sizeX, sizeY;
\r
211 int newSizeX, newSizeY, flags;
\r
213 static BOOL filterHasFocus = FALSE;
\r
215 struct GameListStats stats;
\r
216 static SnapData sd;
\r
219 case WM_INITDIALOG:
\r
220 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
221 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
223 if (gameListDialog) {
\r
224 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
227 /* Initialize the dialog items */
\r
228 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
231 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
233 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
235 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
236 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
238 filterHasFocus = FALSE;
\r
240 /* Size and position the dialog */
\r
241 if (!gameListDialog) {
\r
242 gameListDialog = hDlg;
\r
243 flags = SWP_NOZORDER;
\r
244 GetClientRect(hDlg, &rect);
\r
245 sizeX = rect.right;
\r
246 sizeY = rect.bottom;
\r
247 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
248 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
249 WINDOWPLACEMENT wp;
\r
250 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
251 wp.length = sizeof(WINDOWPLACEMENT);
\r
253 wp.showCmd = SW_SHOW;
\r
254 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
255 wp.rcNormalPosition.left = wpGameList.x;
\r
256 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
257 wp.rcNormalPosition.top = wpGameList.y;
\r
258 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
259 SetWindowPlacement(hDlg, &wp);
\r
261 GetClientRect(hDlg, &rect);
\r
262 newSizeX = rect.right;
\r
263 newSizeY = rect.bottom;
\r
264 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
265 newSizeX, newSizeY);
\r
270 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
275 newSizeX = LOWORD(lParam);
\r
276 newSizeY = HIWORD(lParam);
\r
277 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
278 sizeX, sizeY, newSizeX, newSizeY);
\r
283 case WM_ENTERSIZEMOVE:
\r
284 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
287 return OnSizing( &sd, hDlg, wParam, lParam );
\r
290 return OnMoving( &sd, hDlg, wParam, lParam );
\r
292 case WM_EXITSIZEMOVE:
\r
293 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
295 case WM_GETMINMAXINFO:
\r
296 /* Prevent resizing window too small */
\r
297 mmi = (MINMAXINFO *) lParam;
\r
298 mmi->ptMinTrackSize.x = 100;
\r
299 mmi->ptMinTrackSize.y = 100;
\r
305 If <Enter> is pressed while editing the filter, it's better to apply
\r
306 the filter rather than selecting the current game.
\r
308 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
309 switch( HIWORD(wParam) ) {
\r
311 filterHasFocus = TRUE;
\r
314 filterHasFocus = FALSE;
\r
319 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
320 wParam = IDC_GameListDoFilter;
\r
322 /* [AS] End command replacement */
\r
324 switch (LOWORD(wParam)) {
\r
326 case OPT_GameListLoad:
\r
327 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
329 /* is this possible? */
\r
330 DisplayError("No game selected", 0);
\r
333 break; /* load the game*/
\r
335 case OPT_GameListNext:
\r
336 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
338 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
339 /* [AS] Removed error message */
\r
340 /* DisplayError("Can't go forward any further", 0); */
\r
343 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
344 break; /* load the game*/
\r
346 case OPT_GameListPrev:
\r
347 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
350 /* [AS] Removed error message, added return */
\r
351 /* DisplayError("Can't back up any further", 0); */
\r
354 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
355 break; /* load the game*/
\r
358 case IDC_GameListDoFilter:
\r
360 char filter[MAX_FILTER_LENGTH+1];
\r
362 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
363 filter[ sizeof(filter)-1 ] = '\0';
\r
364 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
365 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
372 case OPT_GameListClose:
\r
376 case OPT_GameListText:
\r
377 switch (HIWORD(wParam)) {
\r
379 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
380 break; /* load the game*/
\r
391 /* Load the game */
\r
393 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
394 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
402 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
404 if( res == LB_ERR ) {
\r
408 text = (char *) malloc( res+1 );
\r
410 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
412 index = atoi( text );
\r
417 /* [AS] End: nItem has been "patched" now! */
\r
419 if (cmailMsgLoaded) {
\r
420 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
423 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
436 VOID GameListPopUp(FILE *fp, char *filename)
\r
441 if (gameFileName != filename) {
\r
442 if (gameFileName) free(gameFileName);
\r
443 gameFileName = StrSave(filename);
\r
445 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
446 if (gameListDialog) {
\r
447 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
448 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
450 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
451 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
452 hwndMain, (DLGPROC)lpProc);
\r
453 FreeProcInstance(lpProc);
\r
458 VOID GameListPopDown(void)
\r
460 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
461 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
462 gameListUp = FALSE;
\r
466 VOID GameListHighlight(int index)
\r
468 if (gameListDialog == NULL) return;
\r
469 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
470 LB_SETCURSEL, index - 1, 0);
\r
474 VOID GameListDestroy()
\r
477 if (gameFileName) {
\r
478 free(gameFileName);
\r
479 gameFileName = NULL;
\r
483 VOID ShowGameListProc()
\r
488 if (gameFileName) {
\r
489 GameListPopUp(gameFile, gameFileName);
\r
491 DisplayError("No game list", 0);
\r
496 HGLOBAL ExportGameListAsText()
\r
498 HGLOBAL result = NULL;
\r
499 LPVOID lpMem = NULL;
\r
500 ListGame * lg = (ListGame *) gameList.head;
\r
504 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
505 DisplayError("Game list not loaded or empty", 0);
\r
509 /* Get list size */
\r
510 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
511 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
513 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
516 lg = (ListGame *) lg->node.succ;
\r
519 /* Allocate memory for the list */
\r
520 result = GlobalAlloc(GHND, dwLen+1 );
\r
522 if( result != NULL ) {
\r
523 lpMem = GlobalLock(result);
\r
526 /* Copy the list into the global memory block */
\r
527 if( lpMem != NULL ) {
\r
528 char * dst = (char *) lpMem;
\r
531 lg = (ListGame *) gameList.head;
\r
533 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
534 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
536 len = sprintf( dst, "%s\r\n", st );
\r
540 lg = (ListGame *) lg->node.succ;
\r
543 GlobalUnlock( result );
\r