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 /* [AS] Setup the game list according to the specified filter */
\r
52 int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats, BOOL byPos, BOOL narrow )
\r
54 ListGame * lg = (ListGame *) gameList.head;
\r
57 BOOL hasFilter = FALSE;
\r
59 struct GameListStats dummy;
\r
61 if(!hDlg) hDlg = gameListDialog; // [HGM] to allow calling from Game List Options dialog
\r
64 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
65 if( stats == NULL ) {
\r
69 stats->white_wins = 0;
\r
70 stats->black_wins = 0;
\r
72 stats->unfinished = 0;
\r
75 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
78 if( pszFilter != NULL ) {
\r
79 if( strlen( pszFilter ) > 0 ) {
\r
84 if(byPos) InitSearch();
\r
86 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
91 if(nItem % 2000 == 0) {
\r
92 snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);
\r
93 SetWindowText(hwndMain, buf); DoEvents();
\r
96 if(!narrow || lg->position >= 0) {
\r
98 st = GameListLine(lg->number, &lg->gameInfo);
\r
99 if( !SearchPattern( st, pszFilter) ) skip = TRUE;
\r
102 if( !skip && byPos) {
\r
103 if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;
\r
107 if(!st) st = GameListLine(lg->number, &lg->gameInfo);
\r
108 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
112 if( lg->gameInfo.result == WhiteWins )
\r
113 stats->white_wins++;
\r
114 else if( lg->gameInfo.result == BlackWins )
\r
115 stats->black_wins++;
\r
116 else if( lg->gameInfo.result == GameIsDrawn )
\r
119 stats->unfinished++;
\r
120 if(!byPos) pos = 0;
\r
124 lg->position = pos;
\r
127 lg = (ListGame *) lg->node.succ;
\r
130 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
131 SetWindowText(hwndMain, "WinBoard");
\r
136 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
137 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
141 snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );
\r
144 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
147 SetWindowText( hDlg, buf );
\r
152 #define MAX_FILTER_LENGTH 128
\r
155 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
157 static char szDlgTitle[64];
\r
158 static HANDLE hwndText;
\r
161 static int sizeX, sizeY;
\r
162 int newSizeX, newSizeY;
\r
164 static BOOL filterHasFocus = FALSE;
\r
166 struct GameListStats stats;
\r
167 static SnapData sd;
\r
170 case WM_INITDIALOG:
\r
171 Translate(hDlg, DLG_GameList);
\r
172 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
173 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
175 if (gameListDialog) {
\r
176 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
179 /* Initialize the dialog items */
\r
180 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
183 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
185 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE, FALSE );
\r
187 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
188 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
190 filterHasFocus = FALSE;
\r
192 /* Size and position the dialog */
\r
193 if (!gameListDialog) {
\r
194 gameListDialog = hDlg;
\r
195 GetClientRect(hDlg, &rect);
\r
196 sizeX = rect.right;
\r
197 sizeY = rect.bottom;
\r
198 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
199 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
200 WINDOWPLACEMENT wp;
\r
201 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
202 wp.length = sizeof(WINDOWPLACEMENT);
\r
204 wp.showCmd = SW_SHOW;
\r
205 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
206 wp.rcNormalPosition.left = wpGameList.x;
\r
207 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
208 wp.rcNormalPosition.top = wpGameList.y;
\r
209 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
210 SetWindowPlacement(hDlg, &wp);
\r
212 GetClientRect(hDlg, &rect);
\r
213 newSizeX = rect.right;
\r
214 newSizeY = rect.bottom;
\r
215 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
216 newSizeX, newSizeY);
\r
220 GetActualPlacement( gameListDialog, &wpGameList );
\r
223 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title
\r
224 GameListHighlight(lastLoadGameNumber);
\r
228 newSizeX = LOWORD(lParam);
\r
229 newSizeY = HIWORD(lParam);
\r
230 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
231 sizeX, sizeY, newSizeX, newSizeY);
\r
236 case WM_ENTERSIZEMOVE:
\r
237 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
240 return OnSizing( &sd, hDlg, wParam, lParam );
\r
243 return OnMoving( &sd, hDlg, wParam, lParam );
\r
245 case WM_EXITSIZEMOVE:
\r
246 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
248 case WM_GETMINMAXINFO:
\r
249 /* Prevent resizing window too small */
\r
250 mmi = (MINMAXINFO *) lParam;
\r
251 mmi->ptMinTrackSize.x = 100;
\r
252 mmi->ptMinTrackSize.y = 100;
\r
258 If <Enter> is pressed while editing the filter, it's better to apply
\r
259 the filter rather than selecting the current game.
\r
261 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
262 switch( HIWORD(wParam) ) {
\r
264 filterHasFocus = TRUE;
\r
267 filterHasFocus = FALSE;
\r
272 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
273 wParam = IDC_GameListDoFilter;
\r
275 /* [AS] End command replacement */
\r
277 switch (LOWORD(wParam)) {
\r
278 case OPT_GameListLoad:
\r
279 LoadOptionsPopup(hDlg);
\r
282 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
284 /* is this possible? */
\r
285 DisplayError(_("No game selected"), 0);
\r
288 break; /* load the game*/
\r
290 case OPT_GameListNext:
\r
291 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
293 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
294 /* [AS] Removed error message */
\r
295 /* DisplayError(_("Can't go forward any further"), 0); */
\r
298 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
299 break; /* load the game*/
\r
301 case OPT_GameListPrev:
\r
303 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
306 /* [AS] Removed error message, added return */
\r
307 /* DisplayError(_("Can't back up any further"), 0); */
\r
310 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
311 break; /* load the game*/
\r
314 case OPT_GameListFind:
\r
315 case IDC_GameListDoFilter:
\r
317 char filter[MAX_FILTER_LENGTH+1];
\r
319 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
320 filter[ sizeof(filter)-1 ] = '\0';
\r
321 count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter, LOWORD(wParam)==OPT_GameListNarrow );
\r
322 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
329 case OPT_GameListClose:
\r
333 case OPT_GameListText:
\r
334 switch (HIWORD(wParam)) {
\r
336 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
337 break; /* load the game*/
\r
348 /* Load the game */
\r
350 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
351 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
359 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
361 if( res == LB_ERR ) {
\r
365 text = (char *) malloc( res+1 );
\r
367 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
369 index = atoi( text );
\r
374 /* [AS] End: nItem has been "patched" now! */
\r
376 if (cmailMsgLoaded) {
\r
377 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
380 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
381 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
394 VOID GameListPopUp(FILE *fp, char *filename)
\r
399 if (gameFileName != filename) {
\r
400 if (gameFileName) free(gameFileName);
\r
401 gameFileName = StrSave(filename);
\r
403 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
404 EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_ENABLED);
\r
405 if (gameListDialog) {
\r
406 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
407 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
408 else SetFocus(gameListDialog);
\r
410 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
411 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
412 hwndMain, (DLGPROC)lpProc);
\r
413 FreeProcInstance(lpProc);
\r
423 VOID GameListPopDown(void)
\r
425 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
426 EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_GRAYED);
\r
427 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
428 gameListUp = FALSE;
\r
432 VOID GameListHighlight(int index)
\r
435 int i, j, k, n, res = 0;
\r
436 if (gameListDialog == NULL) return;
\r
437 for(i=64; ; i+=i) {
\r
438 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );
\r
439 if(res == LB_ERR || index < atoi( buf )) break;
\r
444 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, n, (LPARAM)buf );
\r
445 if(res == LB_ERR || index < (k = atoi( buf ))) i = n; else {
\r
447 if(index == k) break;
\r
450 SendDlgItemMessage(gameListDialog, OPT_GameListText, LB_SETCURSEL, j, 0);
\r
454 VOID GameListDestroy()
\r
457 if (gameFileName) {
\r
458 free(gameFileName);
\r
459 gameFileName = NULL;
\r
463 VOID ShowGameListProc()
\r
466 if(gameListDialog) SetFocus(gameListDialog);
\r
467 // GameListPopDown();
\r
469 if (gameFileName) {
\r
470 GameListPopUp(gameFile, gameFileName);
\r
472 DisplayError(_("No game list"), 0);
\r
477 HGLOBAL ExportGameListAsText()
\r
479 HGLOBAL result = NULL;
\r
480 LPVOID lpMem = NULL;
\r
481 ListGame * lg = (ListGame *) gameList.head;
\r
485 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
486 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
490 /* Get list size */
\r
491 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
492 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
494 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
497 lg = (ListGame *) lg->node.succ;
\r
500 /* Allocate memory for the list */
\r
501 result = GlobalAlloc(GHND, dwLen+1 );
\r
503 if( result != NULL ) {
\r
504 lpMem = GlobalLock(result);
\r
507 /* Copy the list into the global memory block */
\r
508 if( lpMem != NULL ) {
\r
509 char * dst = (char *) lpMem;
\r
512 lg = (ListGame *) gameList.head;
\r
514 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
515 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
517 len = sprintf( dst, "%s\r\n", st );
\r
521 lg = (ListGame *) lg->node.succ;
\r
524 GlobalUnlock( result );
\r