2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995, 2009, 2010, 2011 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, BOOL byPos )
\r
62 ListGame * lg = (ListGame *) gameList.head;
\r
65 BOOL hasFilter = FALSE;
\r
67 struct GameListStats dummy;
\r
69 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
70 if( stats == NULL ) {
\r
74 stats->white_wins = 0;
\r
75 stats->black_wins = 0;
\r
77 stats->unfinished = 0;
\r
80 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
83 if( pszFilter != NULL ) {
\r
84 if( strlen( pszFilter ) > 0 ) {
\r
89 if(byPos) InitSearch();
\r
91 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
96 if(nItem % 2000 == 0) {
\r
97 snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);
\r
98 SetWindowText(hwndMain, buf);
\r
102 st = GameListLine(lg->number, &lg->gameInfo);
\r
103 if( !SearchPattern( st, pszFilter) ) skip = TRUE;
\r
106 if( !skip && byPos) {
\r
107 if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;
\r
110 lg->position = pos;
\r
113 if(!st) st = GameListLine(lg->number, &lg->gameInfo);
\r
114 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
118 if( lg->gameInfo.result == WhiteWins )
\r
119 stats->white_wins++;
\r
120 else if( lg->gameInfo.result == BlackWins )
\r
121 stats->black_wins++;
\r
122 else if( lg->gameInfo.result == GameIsDrawn )
\r
125 stats->unfinished++;
\r
129 lg = (ListGame *) lg->node.succ;
\r
132 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
133 SetWindowText(hwndMain, "WinBoard");
\r
138 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
139 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
143 snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );
\r
146 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
149 SetWindowText( hDlg, buf );
\r
154 #define MAX_FILTER_LENGTH 128
\r
157 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
159 static char szDlgTitle[64];
\r
160 static HANDLE hwndText;
\r
163 static int sizeX, sizeY;
\r
164 int newSizeX, newSizeY, flags;
\r
166 static BOOL filterHasFocus = FALSE;
\r
168 struct GameListStats stats;
\r
169 static SnapData sd;
\r
172 case WM_INITDIALOG:
\r
173 Translate(hDlg, DLG_GameList);
\r
174 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
175 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
177 if (gameListDialog) {
\r
178 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
181 /* Initialize the dialog items */
\r
182 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
185 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
187 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE );
\r
189 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
190 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
192 filterHasFocus = FALSE;
\r
194 /* Size and position the dialog */
\r
195 if (!gameListDialog) {
\r
196 gameListDialog = hDlg;
\r
197 flags = SWP_NOZORDER;
\r
198 GetClientRect(hDlg, &rect);
\r
199 sizeX = rect.right;
\r
200 sizeY = rect.bottom;
\r
201 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
202 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
203 WINDOWPLACEMENT wp;
\r
204 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
205 wp.length = sizeof(WINDOWPLACEMENT);
\r
207 wp.showCmd = SW_SHOW;
\r
208 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
209 wp.rcNormalPosition.left = wpGameList.x;
\r
210 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
211 wp.rcNormalPosition.top = wpGameList.y;
\r
212 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
213 SetWindowPlacement(hDlg, &wp);
\r
215 GetClientRect(hDlg, &rect);
\r
216 newSizeX = rect.right;
\r
217 newSizeY = rect.bottom;
\r
218 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
219 newSizeX, newSizeY);
\r
223 GetActualPlacement( gameListDialog, &wpGameList );
\r
226 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title
\r
227 GameListHighlight(lastLoadGameNumber);
\r
231 newSizeX = LOWORD(lParam);
\r
232 newSizeY = HIWORD(lParam);
\r
233 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
234 sizeX, sizeY, newSizeX, newSizeY);
\r
239 case WM_ENTERSIZEMOVE:
\r
240 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
243 return OnSizing( &sd, hDlg, wParam, lParam );
\r
246 return OnMoving( &sd, hDlg, wParam, lParam );
\r
248 case WM_EXITSIZEMOVE:
\r
249 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
251 case WM_GETMINMAXINFO:
\r
252 /* Prevent resizing window too small */
\r
253 mmi = (MINMAXINFO *) lParam;
\r
254 mmi->ptMinTrackSize.x = 100;
\r
255 mmi->ptMinTrackSize.y = 100;
\r
261 If <Enter> is pressed while editing the filter, it's better to apply
\r
262 the filter rather than selecting the current game.
\r
264 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
265 switch( HIWORD(wParam) ) {
\r
267 filterHasFocus = TRUE;
\r
270 filterHasFocus = FALSE;
\r
275 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
276 wParam = IDC_GameListDoFilter;
\r
278 /* [AS] End command replacement */
\r
280 switch (LOWORD(wParam)) {
\r
281 case OPT_GameListLoad:
\r
282 LoadOptionsPopup(hDlg);
\r
285 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
287 /* is this possible? */
\r
288 DisplayError(_("No game selected"), 0);
\r
291 break; /* load the game*/
\r
293 case OPT_GameListNext:
\r
294 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
296 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
297 /* [AS] Removed error message */
\r
298 /* DisplayError(_("Can't go forward any further"), 0); */
\r
301 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
302 break; /* load the game*/
\r
304 case OPT_GameListPrev:
\r
305 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
308 /* [AS] Removed error message, added return */
\r
309 /* DisplayError(_("Can't back up any further"), 0); */
\r
312 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
313 break; /* load the game*/
\r
316 case OPT_GameListFind:
\r
317 case IDC_GameListDoFilter:
\r
319 char filter[MAX_FILTER_LENGTH+1];
\r
321 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
322 filter[ sizeof(filter)-1 ] = '\0';
\r
323 count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter );
\r
324 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
331 case OPT_GameListClose:
\r
335 case OPT_GameListText:
\r
336 switch (HIWORD(wParam)) {
\r
338 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
339 break; /* load the game*/
\r
350 /* Load the game */
\r
352 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
353 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
361 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
363 if( res == LB_ERR ) {
\r
367 text = (char *) malloc( res+1 );
\r
369 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
371 index = atoi( text );
\r
376 /* [AS] End: nItem has been "patched" now! */
\r
378 if (cmailMsgLoaded) {
\r
379 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
382 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
383 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
396 VOID GameListPopUp(FILE *fp, char *filename)
\r
401 if (gameFileName != filename) {
\r
402 if (gameFileName) free(gameFileName);
\r
403 gameFileName = StrSave(filename);
\r
405 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
406 if (gameListDialog) {
\r
407 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
408 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
409 else SetFocus(gameListDialog);
\r
411 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
412 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
413 hwndMain, (DLGPROC)lpProc);
\r
414 FreeProcInstance(lpProc);
\r
419 VOID GameListPopDown(void)
\r
421 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
422 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
423 gameListUp = FALSE;
\r
427 VOID GameListHighlight(int index)
\r
431 if (gameListDialog == NULL) return;
\r
432 for(i=0; res != LB_ERR; i++) {
\r
433 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );
\r
434 if(index <= atoi( buf )) break;
\r
436 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
437 LB_SETCURSEL, i, 0);
\r
441 VOID GameListDestroy()
\r
444 if (gameFileName) {
\r
445 free(gameFileName);
\r
446 gameFileName = NULL;
\r
450 VOID ShowGameListProc()
\r
453 if(gameListDialog) SetFocus(gameListDialog);
\r
454 // GameListPopDown();
\r
456 if (gameFileName) {
\r
457 GameListPopUp(gameFile, gameFileName);
\r
459 DisplayError(_("No game list"), 0);
\r
464 HGLOBAL ExportGameListAsText()
\r
466 HGLOBAL result = NULL;
\r
467 LPVOID lpMem = NULL;
\r
468 ListGame * lg = (ListGame *) gameList.head;
\r
472 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
473 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
477 /* Get list size */
\r
478 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
479 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
481 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
484 lg = (ListGame *) lg->node.succ;
\r
487 /* Allocate memory for the list */
\r
488 result = GlobalAlloc(GHND, dwLen+1 );
\r
490 if( result != NULL ) {
\r
491 lpMem = GlobalLock(result);
\r
494 /* Copy the list into the global memory block */
\r
495 if( lpMem != NULL ) {
\r
496 char * dst = (char *) lpMem;
\r
499 lg = (ListGame *) gameList.head;
\r
501 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
502 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
504 len = sprintf( dst, "%s\r\n", st );
\r
508 lg = (ListGame *) lg->node.succ;
\r
511 GlobalUnlock( result );
\r