2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995, 2009, 2010 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] Setup the game list according to the specified filter */
\r
58 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
60 ListGame * lg = (ListGame *) gameList.head;
\r
62 BOOL hasFilter = FALSE;
\r
64 struct GameListStats dummy;
\r
66 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
67 if( stats == NULL ) {
\r
71 stats->white_wins = 0;
\r
72 stats->black_wins = 0;
\r
74 stats->unfinished = 0;
\r
77 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
80 if( pszFilter != NULL ) {
\r
81 if( strlen( pszFilter ) > 0 ) {
\r
86 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
87 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
91 if( ! SearchPattern( st, pszFilter ) ) {
\r
97 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
101 if( lg->gameInfo.result == WhiteWins )
\r
102 stats->white_wins++;
\r
103 else if( lg->gameInfo.result == BlackWins )
\r
104 stats->black_wins++;
\r
105 else if( lg->gameInfo.result == GameIsDrawn )
\r
108 stats->unfinished++;
\r
112 lg = (ListGame *) lg->node.succ;
\r
115 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
120 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
121 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
125 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
128 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
131 SetWindowText( hDlg, buf );
\r
136 #define MAX_FILTER_LENGTH 128
\r
139 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
141 static char szDlgTitle[64];
\r
142 static HANDLE hwndText;
\r
145 static int sizeX, sizeY;
\r
146 int newSizeX, newSizeY, flags;
\r
148 static BOOL filterHasFocus = FALSE;
\r
150 struct GameListStats stats;
\r
151 static SnapData sd;
\r
154 case WM_INITDIALOG:
\r
155 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
156 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
158 if (gameListDialog) {
\r
159 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
162 /* Initialize the dialog items */
\r
163 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
166 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
168 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
170 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
171 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
173 filterHasFocus = FALSE;
\r
175 /* Size and position the dialog */
\r
176 if (!gameListDialog) {
\r
177 gameListDialog = hDlg;
\r
178 flags = SWP_NOZORDER;
\r
179 GetClientRect(hDlg, &rect);
\r
180 sizeX = rect.right;
\r
181 sizeY = rect.bottom;
\r
182 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
183 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
184 WINDOWPLACEMENT wp;
\r
185 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
186 wp.length = sizeof(WINDOWPLACEMENT);
\r
188 wp.showCmd = SW_SHOW;
\r
189 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
190 wp.rcNormalPosition.left = wpGameList.x;
\r
191 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
192 wp.rcNormalPosition.top = wpGameList.y;
\r
193 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
194 SetWindowPlacement(hDlg, &wp);
\r
196 GetClientRect(hDlg, &rect);
\r
197 newSizeX = rect.right;
\r
198 newSizeY = rect.bottom;
\r
199 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
200 newSizeX, newSizeY);
\r
205 GetActualPlacement( gameListDialog, &wpGameList );
\r
207 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
212 newSizeX = LOWORD(lParam);
\r
213 newSizeY = HIWORD(lParam);
\r
214 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
215 sizeX, sizeY, newSizeX, newSizeY);
\r
220 case WM_ENTERSIZEMOVE:
\r
221 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
224 return OnSizing( &sd, hDlg, wParam, lParam );
\r
227 return OnMoving( &sd, hDlg, wParam, lParam );
\r
229 case WM_EXITSIZEMOVE:
\r
230 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
232 case WM_GETMINMAXINFO:
\r
233 /* Prevent resizing window too small */
\r
234 mmi = (MINMAXINFO *) lParam;
\r
235 mmi->ptMinTrackSize.x = 100;
\r
236 mmi->ptMinTrackSize.y = 100;
\r
242 If <Enter> is pressed while editing the filter, it's better to apply
\r
243 the filter rather than selecting the current game.
\r
245 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
246 switch( HIWORD(wParam) ) {
\r
248 filterHasFocus = TRUE;
\r
251 filterHasFocus = FALSE;
\r
256 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
257 wParam = IDC_GameListDoFilter;
\r
259 /* [AS] End command replacement */
\r
261 switch (LOWORD(wParam)) {
\r
263 case OPT_GameListLoad:
\r
264 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
266 /* is this possible? */
\r
267 DisplayError("No game selected", 0);
\r
270 break; /* load the game*/
\r
272 case OPT_GameListNext:
\r
273 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
275 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
276 /* [AS] Removed error message */
\r
277 /* DisplayError("Can't go forward any further", 0); */
\r
280 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
281 break; /* load the game*/
\r
283 case OPT_GameListPrev:
\r
284 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
287 /* [AS] Removed error message, added return */
\r
288 /* DisplayError("Can't back up any further", 0); */
\r
291 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
292 break; /* load the game*/
\r
295 case IDC_GameListDoFilter:
\r
297 char filter[MAX_FILTER_LENGTH+1];
\r
299 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
300 filter[ sizeof(filter)-1 ] = '\0';
\r
301 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
302 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
309 case OPT_GameListClose:
\r
313 case OPT_GameListText:
\r
314 switch (HIWORD(wParam)) {
\r
316 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
317 break; /* load the game*/
\r
328 /* Load the game */
\r
330 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
331 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
339 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
341 if( res == LB_ERR ) {
\r
345 text = (char *) malloc( res+1 );
\r
347 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
349 index = atoi( text );
\r
354 /* [AS] End: nItem has been "patched" now! */
\r
356 if (cmailMsgLoaded) {
\r
357 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
360 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
361 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
374 VOID GameListPopUp(FILE *fp, char *filename)
\r
379 if (gameFileName != filename) {
\r
380 if (gameFileName) free(gameFileName);
\r
381 gameFileName = StrSave(filename);
\r
383 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
384 if (gameListDialog) {
\r
385 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
386 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
387 else SetFocus(gameListDialog);
\r
389 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
390 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
391 hwndMain, (DLGPROC)lpProc);
\r
392 FreeProcInstance(lpProc);
\r
397 VOID GameListPopDown(void)
\r
399 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
400 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
401 gameListUp = FALSE;
\r
405 VOID GameListHighlight(int index)
\r
407 if (gameListDialog == NULL) return;
\r
408 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
409 LB_SETCURSEL, index - 1, 0);
\r
413 VOID GameListDestroy()
\r
416 if (gameFileName) {
\r
417 free(gameFileName);
\r
418 gameFileName = NULL;
\r
422 VOID ShowGameListProc()
\r
425 if(gameListDialog) SetFocus(gameListDialog);
\r
426 // GameListPopDown();
\r
428 if (gameFileName) {
\r
429 GameListPopUp(gameFile, gameFileName);
\r
431 DisplayError("No game list", 0);
\r
436 HGLOBAL ExportGameListAsText()
\r
438 HGLOBAL result = NULL;
\r
439 LPVOID lpMem = NULL;
\r
440 ListGame * lg = (ListGame *) gameList.head;
\r
444 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
445 DisplayError("Game list not loaded or empty", 0);
\r
449 /* Get list size */
\r
450 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
451 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
453 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
456 lg = (ListGame *) lg->node.succ;
\r
459 /* Allocate memory for the list */
\r
460 result = GlobalAlloc(GHND, dwLen+1 );
\r
462 if( result != NULL ) {
\r
463 lpMem = GlobalLock(result);
\r
466 /* Copy the list into the global memory block */
\r
467 if( lpMem != NULL ) {
\r
468 char * dst = (char *) lpMem;
\r
471 lg = (ListGame *) gameList.head;
\r
473 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
474 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
476 len = sprintf( dst, "%s\r\n", st );
\r
480 lg = (ListGame *) lg->node.succ;
\r
483 GlobalUnlock( result );
\r