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 "frontend.h"
\r
39 #include "backend.h"
\r
40 #include "winboard.h"
\r
44 /* Module globals */
\r
45 static BOOLEAN gameListUp = FALSE;
\r
46 static FILE* gameFile;
\r
47 static char* gameFileName = NULL;
\r
49 struct GameListStats
\r
57 /* [AS] Wildcard pattern matching */
\r
58 static BOOL HasPattern( const char * text, const char * pattern )
\r
60 while( *pattern != '\0' ) {
\r
61 if( *pattern == '*' ) {
\r
62 while( *pattern == '*' ) {
\r
66 if( *pattern == '\0' ) {
\r
70 while( *text != '\0' ) {
\r
71 if( HasPattern( text, pattern ) ) {
\r
77 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
89 static BOOL SearchPattern( const char * text, const char * pattern )
\r
93 if( pattern != NULL && *pattern != '\0' ) {
\r
94 if( *pattern == '*' ) {
\r
95 result = HasPattern( text, pattern );
\r
100 while( *text != '\0' ) {
\r
101 if( HasPattern( text, pattern ) ) {
\r
113 /* [AS] Setup the game list according to the specified filter */
\r
114 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
116 ListGame * lg = (ListGame *) gameList.head;
\r
118 BOOL hasFilter = FALSE;
\r
120 struct GameListStats dummy;
\r
122 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
123 if( stats == NULL ) {
\r
127 stats->white_wins = 0;
\r
128 stats->black_wins = 0;
\r
130 stats->unfinished = 0;
\r
133 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
136 if( pszFilter != NULL ) {
\r
137 if( strlen( pszFilter ) > 0 ) {
\r
142 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
143 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
147 if( ! SearchPattern( st, pszFilter ) ) {
\r
153 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
157 if( lg->gameInfo.result == WhiteWins )
\r
158 stats->white_wins++;
\r
159 else if( lg->gameInfo.result == BlackWins )
\r
160 stats->black_wins++;
\r
161 else if( lg->gameInfo.result == GameIsDrawn )
\r
164 stats->unfinished++;
\r
168 lg = (ListGame *) lg->node.succ;
\r
171 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
176 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
177 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
181 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
184 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
187 SetWindowText( hDlg, buf );
\r
192 #define MAX_FILTER_LENGTH 128
\r
195 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
197 static char szDlgTitle[64];
\r
198 static HANDLE hwndText;
\r
201 static int sizeX, sizeY;
\r
202 int newSizeX, newSizeY, flags;
\r
204 static BOOL filterHasFocus = FALSE;
\r
206 struct GameListStats stats;
\r
207 static SnapData sd;
\r
210 case WM_INITDIALOG:
\r
211 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
212 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
214 if (gameListDialog) {
\r
215 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
218 /* Initialize the dialog items */
\r
219 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
222 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
224 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
226 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
227 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
229 filterHasFocus = FALSE;
\r
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 (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
239 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
240 WINDOWPLACEMENT wp;
\r
241 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
242 wp.length = sizeof(WINDOWPLACEMENT);
\r
244 wp.showCmd = SW_SHOW;
\r
245 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
246 wp.rcNormalPosition.left = wpGameList.x;
\r
247 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
248 wp.rcNormalPosition.top = wpGameList.y;
\r
249 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
250 SetWindowPlacement(hDlg, &wp);
\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
261 GetActualPlacement( gameListDialog, &wpGameList );
\r
263 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
268 newSizeX = LOWORD(lParam);
\r
269 newSizeY = HIWORD(lParam);
\r
270 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
271 sizeX, sizeY, newSizeX, newSizeY);
\r
276 case WM_ENTERSIZEMOVE:
\r
277 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
280 return OnSizing( &sd, hDlg, wParam, lParam );
\r
283 return OnMoving( &sd, hDlg, wParam, lParam );
\r
285 case WM_EXITSIZEMOVE:
\r
286 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
288 case WM_GETMINMAXINFO:
\r
289 /* Prevent resizing window too small */
\r
290 mmi = (MINMAXINFO *) lParam;
\r
291 mmi->ptMinTrackSize.x = 100;
\r
292 mmi->ptMinTrackSize.y = 100;
\r
298 If <Enter> is pressed while editing the filter, it's better to apply
\r
299 the filter rather than selecting the current game.
\r
301 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
302 switch( HIWORD(wParam) ) {
\r
304 filterHasFocus = TRUE;
\r
307 filterHasFocus = FALSE;
\r
312 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
313 wParam = IDC_GameListDoFilter;
\r
315 /* [AS] End command replacement */
\r
317 switch (LOWORD(wParam)) {
\r
319 case OPT_GameListLoad:
\r
320 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
322 /* is this possible? */
\r
323 DisplayError("No game selected", 0);
\r
326 break; /* load the game*/
\r
328 case OPT_GameListNext:
\r
329 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
331 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
332 /* [AS] Removed error message */
\r
333 /* DisplayError("Can't go forward any further", 0); */
\r
336 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
337 break; /* load the game*/
\r
339 case OPT_GameListPrev:
\r
340 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
343 /* [AS] Removed error message, added return */
\r
344 /* DisplayError("Can't back up any further", 0); */
\r
347 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
348 break; /* load the game*/
\r
351 case IDC_GameListDoFilter:
\r
353 char filter[MAX_FILTER_LENGTH+1];
\r
355 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
356 filter[ sizeof(filter)-1 ] = '\0';
\r
357 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
358 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
365 case OPT_GameListClose:
\r
369 case OPT_GameListText:
\r
370 switch (HIWORD(wParam)) {
\r
372 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
373 break; /* load the game*/
\r
384 /* Load the game */
\r
386 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
387 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
395 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
397 if( res == LB_ERR ) {
\r
401 text = (char *) malloc( res+1 );
\r
403 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
405 index = atoi( text );
\r
410 /* [AS] End: nItem has been "patched" now! */
\r
412 if (cmailMsgLoaded) {
\r
413 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
416 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
417 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
430 VOID GameListPopUp(FILE *fp, char *filename)
\r
435 if (gameFileName != filename) {
\r
436 if (gameFileName) free(gameFileName);
\r
437 gameFileName = StrSave(filename);
\r
439 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
440 if (gameListDialog) {
\r
441 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
442 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
443 else SetFocus(gameListDialog);
\r
445 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
446 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
447 hwndMain, (DLGPROC)lpProc);
\r
448 FreeProcInstance(lpProc);
\r
453 VOID GameListPopDown(void)
\r
455 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
456 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
457 gameListUp = FALSE;
\r
461 VOID GameListHighlight(int index)
\r
463 if (gameListDialog == NULL) return;
\r
464 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
465 LB_SETCURSEL, index - 1, 0);
\r
469 VOID GameListDestroy()
\r
472 if (gameFileName) {
\r
473 free(gameFileName);
\r
474 gameFileName = NULL;
\r
478 VOID ShowGameListProc()
\r
481 if(gameListDialog) SetFocus(gameListDialog);
\r
482 // GameListPopDown();
\r
484 if (gameFileName) {
\r
485 GameListPopUp(gameFile, gameFileName);
\r
487 DisplayError("No game list", 0);
\r
492 HGLOBAL ExportGameListAsText()
\r
494 HGLOBAL result = NULL;
\r
495 LPVOID lpMem = NULL;
\r
496 ListGame * lg = (ListGame *) gameList.head;
\r
500 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
501 DisplayError("Game list not loaded or empty", 0);
\r
505 /* Get list size */
\r
506 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
507 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
509 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
512 lg = (ListGame *) lg->node.succ;
\r
515 /* Allocate memory for the list */
\r
516 result = GlobalAlloc(GHND, dwLen+1 );
\r
518 if( result != NULL ) {
\r
519 lpMem = GlobalLock(result);
\r
522 /* Copy the list into the global memory block */
\r
523 if( lpMem != NULL ) {
\r
524 char * dst = (char *) lpMem;
\r
527 lg = (ListGame *) gameList.head;
\r
529 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
530 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
532 len = sprintf( dst, "%s\r\n", st );
\r
536 lg = (ListGame *) lg->node.succ;
\r
539 GlobalUnlock( result );
\r