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
46 /* Module globals */
\r
47 static BOOLEAN gameListUp = FALSE;
\r
48 static FILE* gameFile;
\r
49 static char* gameFileName = NULL;
\r
51 struct GameListStats
\r
59 /* [AS] Setup the game list according to the specified filter */
\r
60 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
62 ListGame * lg = (ListGame *) gameList.head;
\r
64 BOOL hasFilter = FALSE;
\r
66 struct GameListStats dummy;
\r
68 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
69 if( stats == NULL ) {
\r
73 stats->white_wins = 0;
\r
74 stats->black_wins = 0;
\r
76 stats->unfinished = 0;
\r
79 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
82 if( pszFilter != NULL ) {
\r
83 if( strlen( pszFilter ) > 0 ) {
\r
88 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
89 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
93 if( ! SearchPattern( st, pszFilter ) ) {
\r
99 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
103 if( lg->gameInfo.result == WhiteWins )
\r
104 stats->white_wins++;
\r
105 else if( lg->gameInfo.result == BlackWins )
\r
106 stats->black_wins++;
\r
107 else if( lg->gameInfo.result == GameIsDrawn )
\r
110 stats->unfinished++;
\r
114 lg = (ListGame *) lg->node.succ;
\r
117 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
122 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
123 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
127 snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );
\r
130 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
133 SetWindowText( hDlg, buf );
\r
138 #define MAX_FILTER_LENGTH 128
\r
141 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
143 static char szDlgTitle[64];
\r
144 static HANDLE hwndText;
\r
147 static int sizeX, sizeY;
\r
148 int newSizeX, newSizeY, flags;
\r
150 static BOOL filterHasFocus = FALSE;
\r
152 struct GameListStats stats;
\r
153 static SnapData sd;
\r
156 case WM_INITDIALOG:
\r
157 Translate(hDlg, DLG_GameList);
\r
158 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
159 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
161 if (gameListDialog) {
\r
162 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
165 /* Initialize the dialog items */
\r
166 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
169 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
171 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
173 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
174 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
176 filterHasFocus = FALSE;
\r
178 /* Size and position the dialog */
\r
179 if (!gameListDialog) {
\r
180 gameListDialog = hDlg;
\r
181 flags = SWP_NOZORDER;
\r
182 GetClientRect(hDlg, &rect);
\r
183 sizeX = rect.right;
\r
184 sizeY = rect.bottom;
\r
185 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
186 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
187 WINDOWPLACEMENT wp;
\r
188 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
189 wp.length = sizeof(WINDOWPLACEMENT);
\r
191 wp.showCmd = SW_SHOW;
\r
192 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
193 wp.rcNormalPosition.left = wpGameList.x;
\r
194 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
195 wp.rcNormalPosition.top = wpGameList.y;
\r
196 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
197 SetWindowPlacement(hDlg, &wp);
\r
199 GetClientRect(hDlg, &rect);
\r
200 newSizeX = rect.right;
\r
201 newSizeY = rect.bottom;
\r
202 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
203 newSizeX, newSizeY);
\r
207 GetActualPlacement( gameListDialog, &wpGameList );
\r
210 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title
\r
214 newSizeX = LOWORD(lParam);
\r
215 newSizeY = HIWORD(lParam);
\r
216 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
217 sizeX, sizeY, newSizeX, newSizeY);
\r
222 case WM_ENTERSIZEMOVE:
\r
223 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
226 return OnSizing( &sd, hDlg, wParam, lParam );
\r
229 return OnMoving( &sd, hDlg, wParam, lParam );
\r
231 case WM_EXITSIZEMOVE:
\r
232 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
234 case WM_GETMINMAXINFO:
\r
235 /* Prevent resizing window too small */
\r
236 mmi = (MINMAXINFO *) lParam;
\r
237 mmi->ptMinTrackSize.x = 100;
\r
238 mmi->ptMinTrackSize.y = 100;
\r
244 If <Enter> is pressed while editing the filter, it's better to apply
\r
245 the filter rather than selecting the current game.
\r
247 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
248 switch( HIWORD(wParam) ) {
\r
250 filterHasFocus = TRUE;
\r
253 filterHasFocus = FALSE;
\r
258 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
259 wParam = IDC_GameListDoFilter;
\r
261 /* [AS] End command replacement */
\r
263 switch (LOWORD(wParam)) {
\r
265 case OPT_GameListLoad:
\r
266 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
268 /* is this possible? */
\r
269 DisplayError(_("No game selected"), 0);
\r
272 break; /* load the game*/
\r
274 case OPT_GameListNext:
\r
275 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
277 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
278 /* [AS] Removed error message */
\r
279 /* DisplayError(_("Can't go forward any further"), 0); */
\r
282 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
283 break; /* load the game*/
\r
285 case OPT_GameListPrev:
\r
286 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
289 /* [AS] Removed error message, added return */
\r
290 /* DisplayError(_("Can't back up any further"), 0); */
\r
293 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
294 break; /* load the game*/
\r
297 case IDC_GameListDoFilter:
\r
299 char filter[MAX_FILTER_LENGTH+1];
\r
301 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
302 filter[ sizeof(filter)-1 ] = '\0';
\r
303 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
304 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
311 case OPT_GameListClose:
\r
315 case OPT_GameListText:
\r
316 switch (HIWORD(wParam)) {
\r
318 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
319 break; /* load the game*/
\r
330 /* Load the game */
\r
332 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
333 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
341 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
343 if( res == LB_ERR ) {
\r
347 text = (char *) malloc( res+1 );
\r
349 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
351 index = atoi( text );
\r
356 /* [AS] End: nItem has been "patched" now! */
\r
358 if (cmailMsgLoaded) {
\r
359 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
362 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
363 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
376 VOID GameListPopUp(FILE *fp, char *filename)
\r
381 if (gameFileName != filename) {
\r
382 if (gameFileName) free(gameFileName);
\r
383 gameFileName = StrSave(filename);
\r
385 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
386 if (gameListDialog) {
\r
387 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
388 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
389 else SetFocus(gameListDialog);
\r
391 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
392 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
393 hwndMain, (DLGPROC)lpProc);
\r
394 FreeProcInstance(lpProc);
\r
399 VOID GameListPopDown(void)
\r
401 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
402 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
403 gameListUp = FALSE;
\r
407 VOID GameListHighlight(int index)
\r
409 if (gameListDialog == NULL) return;
\r
410 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
411 LB_SETCURSEL, index - 1, 0);
\r
415 VOID GameListDestroy()
\r
418 if (gameFileName) {
\r
419 free(gameFileName);
\r
420 gameFileName = NULL;
\r
424 VOID ShowGameListProc()
\r
427 if(gameListDialog) SetFocus(gameListDialog);
\r
428 // GameListPopDown();
\r
430 if (gameFileName) {
\r
431 GameListPopUp(gameFile, gameFileName);
\r
433 DisplayError(_("No game list"), 0);
\r
438 HGLOBAL ExportGameListAsText()
\r
440 HGLOBAL result = NULL;
\r
441 LPVOID lpMem = NULL;
\r
442 ListGame * lg = (ListGame *) gameList.head;
\r
446 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
447 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
451 /* Get list size */
\r
452 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
453 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
455 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
458 lg = (ListGame *) lg->node.succ;
\r
461 /* Allocate memory for the list */
\r
462 result = GlobalAlloc(GHND, dwLen+1 );
\r
464 if( result != NULL ) {
\r
465 lpMem = GlobalLock(result);
\r
468 /* Copy the list into the global memory block */
\r
469 if( lpMem != NULL ) {
\r
470 char * dst = (char *) lpMem;
\r
473 lg = (ListGame *) gameList.head;
\r
475 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
476 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
478 len = sprintf( dst, "%s\r\n", st );
\r
482 lg = (ListGame *) lg->node.succ;
\r
485 GlobalUnlock( result );
\r