2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014 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, BOOL narrow )
\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
101 if(!narrow || lg->position >= 0) {
\r
103 st = GameListLine(lg->number, &lg->gameInfo);
\r
104 if( !SearchPattern( st, pszFilter) ) skip = TRUE;
\r
107 if( !skip && byPos) {
\r
108 if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;
\r
112 if(!st) st = GameListLine(lg->number, &lg->gameInfo);
\r
113 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
117 if( lg->gameInfo.result == WhiteWins )
\r
118 stats->white_wins++;
\r
119 else if( lg->gameInfo.result == BlackWins )
\r
120 stats->black_wins++;
\r
121 else if( lg->gameInfo.result == GameIsDrawn )
\r
124 stats->unfinished++;
\r
125 if(!byPos) pos = 0;
\r
129 lg->position = pos;
\r
132 lg = (ListGame *) lg->node.succ;
\r
135 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
136 SetWindowText(hwndMain, "WinBoard");
\r
141 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
142 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
146 snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );
\r
149 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
152 SetWindowText( hDlg, buf );
\r
157 #define MAX_FILTER_LENGTH 128
\r
160 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
162 static char szDlgTitle[64];
\r
163 static HANDLE hwndText;
\r
166 static int sizeX, sizeY;
\r
167 int newSizeX, newSizeY;
\r
169 static BOOL filterHasFocus = FALSE;
\r
171 struct GameListStats stats;
\r
172 static SnapData sd;
\r
175 case WM_INITDIALOG:
\r
176 Translate(hDlg, DLG_GameList);
\r
177 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
178 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
180 if (gameListDialog) {
\r
181 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
184 /* Initialize the dialog items */
\r
185 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
188 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
190 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE, FALSE );
\r
192 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
193 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
195 filterHasFocus = FALSE;
\r
197 /* Size and position the dialog */
\r
198 if (!gameListDialog) {
\r
199 gameListDialog = hDlg;
\r
200 GetClientRect(hDlg, &rect);
\r
201 sizeX = rect.right;
\r
202 sizeY = rect.bottom;
\r
203 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
204 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
205 WINDOWPLACEMENT wp;
\r
206 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
207 wp.length = sizeof(WINDOWPLACEMENT);
\r
209 wp.showCmd = SW_SHOW;
\r
210 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
211 wp.rcNormalPosition.left = wpGameList.x;
\r
212 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
213 wp.rcNormalPosition.top = wpGameList.y;
\r
214 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
215 SetWindowPlacement(hDlg, &wp);
\r
217 GetClientRect(hDlg, &rect);
\r
218 newSizeX = rect.right;
\r
219 newSizeY = rect.bottom;
\r
220 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
221 newSizeX, newSizeY);
\r
225 GetActualPlacement( gameListDialog, &wpGameList );
\r
228 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title
\r
229 GameListHighlight(lastLoadGameNumber);
\r
233 newSizeX = LOWORD(lParam);
\r
234 newSizeY = HIWORD(lParam);
\r
235 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
236 sizeX, sizeY, newSizeX, newSizeY);
\r
241 case WM_ENTERSIZEMOVE:
\r
242 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
245 return OnSizing( &sd, hDlg, wParam, lParam );
\r
248 return OnMoving( &sd, hDlg, wParam, lParam );
\r
250 case WM_EXITSIZEMOVE:
\r
251 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
253 case WM_GETMINMAXINFO:
\r
254 /* Prevent resizing window too small */
\r
255 mmi = (MINMAXINFO *) lParam;
\r
256 mmi->ptMinTrackSize.x = 100;
\r
257 mmi->ptMinTrackSize.y = 100;
\r
263 If <Enter> is pressed while editing the filter, it's better to apply
\r
264 the filter rather than selecting the current game.
\r
266 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
267 switch( HIWORD(wParam) ) {
\r
269 filterHasFocus = TRUE;
\r
272 filterHasFocus = FALSE;
\r
277 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
278 wParam = IDC_GameListDoFilter;
\r
280 /* [AS] End command replacement */
\r
282 switch (LOWORD(wParam)) {
\r
283 case OPT_GameListLoad:
\r
284 LoadOptionsPopup(hDlg);
\r
287 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
289 /* is this possible? */
\r
290 DisplayError(_("No game selected"), 0);
\r
293 break; /* load the game*/
\r
295 case OPT_GameListNext:
\r
296 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
298 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
299 /* [AS] Removed error message */
\r
300 /* DisplayError(_("Can't go forward any further"), 0); */
\r
303 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
304 break; /* load the game*/
\r
306 case OPT_GameListPrev:
\r
308 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
311 /* [AS] Removed error message, added return */
\r
312 /* DisplayError(_("Can't back up any further"), 0); */
\r
315 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
316 break; /* load the game*/
\r
319 case OPT_GameListFind:
\r
320 case IDC_GameListDoFilter:
\r
322 char filter[MAX_FILTER_LENGTH+1];
\r
324 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
325 filter[ sizeof(filter)-1 ] = '\0';
\r
326 count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter, LOWORD(wParam)==OPT_GameListNarrow );
\r
327 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
334 case OPT_GameListClose:
\r
338 case OPT_GameListText:
\r
339 switch (HIWORD(wParam)) {
\r
341 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
342 break; /* load the game*/
\r
353 /* Load the game */
\r
355 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
356 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
364 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
366 if( res == LB_ERR ) {
\r
370 text = (char *) malloc( res+1 );
\r
372 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
374 index = atoi( text );
\r
379 /* [AS] End: nItem has been "patched" now! */
\r
381 if (cmailMsgLoaded) {
\r
382 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
385 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
386 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
399 VOID GameListPopUp(FILE *fp, char *filename)
\r
404 if (gameFileName != filename) {
\r
405 if (gameFileName) free(gameFileName);
\r
406 gameFileName = StrSave(filename);
\r
408 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
409 if (gameListDialog) {
\r
410 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
411 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
412 else SetFocus(gameListDialog);
\r
414 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
415 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
416 hwndMain, (DLGPROC)lpProc);
\r
417 FreeProcInstance(lpProc);
\r
427 VOID GameListPopDown(void)
\r
429 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
430 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
431 gameListUp = FALSE;
\r
435 VOID GameListHighlight(int index)
\r
439 if (gameListDialog == NULL) return;
\r
440 for(i=0; res != LB_ERR; i++) {
\r
441 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );
\r
442 if(index <= atoi( buf )) break;
\r
444 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
445 LB_SETCURSEL, i, 0);
\r
449 VOID GameListDestroy()
\r
452 if (gameFileName) {
\r
453 free(gameFileName);
\r
454 gameFileName = NULL;
\r
458 VOID ShowGameListProc()
\r
461 if(gameListDialog) SetFocus(gameListDialog);
\r
462 // GameListPopDown();
\r
464 if (gameFileName) {
\r
465 GameListPopUp(gameFile, gameFileName);
\r
467 DisplayError(_("No game list"), 0);
\r
472 HGLOBAL ExportGameListAsText()
\r
474 HGLOBAL result = NULL;
\r
475 LPVOID lpMem = NULL;
\r
476 ListGame * lg = (ListGame *) gameList.head;
\r
480 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
481 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
485 /* Get list size */
\r
486 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
487 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
489 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
492 lg = (ListGame *) lg->node.succ;
\r
495 /* Allocate memory for the list */
\r
496 result = GlobalAlloc(GHND, dwLen+1 );
\r
498 if( result != NULL ) {
\r
499 lpMem = GlobalLock(result);
\r
502 /* Copy the list into the global memory block */
\r
503 if( lpMem != NULL ) {
\r
504 char * dst = (char *) lpMem;
\r
507 lg = (ListGame *) gameList.head;
\r
509 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
510 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
512 len = sprintf( dst, "%s\r\n", st );
\r
516 lg = (ListGame *) lg->node.succ;
\r
519 GlobalUnlock( result );
\r