2 * wgamelist.c -- Game list window for WinBoard
\r
4 * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
\r
5 * Software Foundation, Inc.
\r
7 * Enhancements Copyright 2005 Alessandro Scotti
\r
9 * ------------------------------------------------------------------------
\r
11 * GNU XBoard is free software: you can redistribute it and/or modify
\r
12 * it under the terms of the GNU General Public License as published by
\r
13 * the Free Software Foundation, either version 3 of the License, or (at
\r
14 * your option) any later version.
\r
16 * GNU XBoard is distributed in the hope that it will be useful, but
\r
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
19 * General Public License for more details.
\r
21 * You should have received a copy of the GNU General Public License
\r
22 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
24 *------------------------------------------------------------------------
\r
25 ** See the file ChangeLog for a revision history. */
\r
29 #include <windows.h> /* required for all Windows applications */
\r
35 #include <commdlg.h>
\r
39 #include "frontend.h"
\r
40 #include "backend.h"
\r
41 #include "winboard.h"
\r
47 /* Module globals */
\r
48 static BOOLEAN gameListUp = FALSE;
\r
49 static FILE* gameFile;
\r
50 static char* gameFileName = NULL;
\r
52 /* [AS] Setup the game list according to the specified filter */
\r
53 int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats, BOOL byPos, BOOL narrow )
\r
55 ListGame * lg = (ListGame *) gameList.head;
\r
58 BOOL hasFilter = FALSE;
\r
60 struct GameListStats dummy;
\r
62 if(!hDlg) hDlg = gameListDialog; // [HGM] to allow calling from Game List Options dialog
\r
65 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
66 if( stats == NULL ) {
\r
70 stats->white_wins = 0;
\r
71 stats->black_wins = 0;
\r
73 stats->unfinished = 0;
\r
76 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
79 if( pszFilter != NULL ) {
\r
80 if( strlen( pszFilter ) > 0 ) {
\r
85 if(byPos) InitSearch();
\r
87 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
92 if(nItem % 2000 == 0) {
\r
93 snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);
\r
94 SetWindowText(hwndMain, buf); DoEvents();
\r
97 if(!narrow || lg->position >= 0) {
\r
99 st = GameListLine(lg->number, &lg->gameInfo);
\r
100 if( !SearchPattern( st, pszFilter) ) skip = TRUE;
\r
103 if( !skip && byPos) {
\r
104 if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;
\r
108 if(!st) st = GameListLine(lg->number, &lg->gameInfo);
\r
109 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
113 if( lg->gameInfo.result == WhiteWins )
\r
114 stats->white_wins++;
\r
115 else if( lg->gameInfo.result == BlackWins )
\r
116 stats->black_wins++;
\r
117 else if( lg->gameInfo.result == GameIsDrawn )
\r
120 stats->unfinished++;
\r
121 if(!byPos) pos = 0;
\r
125 lg->position = pos;
\r
128 lg = (ListGame *) lg->node.succ;
\r
131 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
132 SetWindowText(hwndMain, "WinBoard");
\r
137 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
138 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
142 snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );
\r
145 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
148 SetWindowText( hDlg, buf );
\r
153 #define MAX_FILTER_LENGTH 128
\r
156 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
158 static char szDlgTitle[64];
\r
159 static HANDLE hwndText;
\r
162 static int sizeX, sizeY;
\r
163 int newSizeX, newSizeY;
\r
165 static BOOL filterHasFocus = FALSE;
\r
167 struct GameListStats stats;
\r
168 static SnapData sd;
\r
171 case WM_INITDIALOG:
\r
172 Translate(hDlg, DLG_GameList);
\r
173 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
174 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
176 if (gameListDialog) {
\r
177 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
180 /* Initialize the dialog items */
\r
181 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
184 SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
186 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE, FALSE );
\r
188 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
189 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
191 filterHasFocus = FALSE;
\r
193 /* Size and position the dialog */
\r
194 if (!gameListDialog) {
\r
195 gameListDialog = hDlg;
\r
196 GetClientRect(hDlg, &rect);
\r
197 sizeX = rect.right;
\r
198 sizeY = rect.bottom;
\r
199 if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&
\r
200 wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {
\r
201 WINDOWPLACEMENT wp;
\r
202 EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);
\r
203 wp.length = sizeof(WINDOWPLACEMENT);
\r
205 wp.showCmd = SW_SHOW;
\r
206 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
207 wp.rcNormalPosition.left = wpGameList.x;
\r
208 wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;
\r
209 wp.rcNormalPosition.top = wpGameList.y;
\r
210 wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;
\r
211 SetWindowPlacement(hDlg, &wp);
\r
213 GetClientRect(hDlg, &rect);
\r
214 newSizeX = rect.right;
\r
215 newSizeY = rect.bottom;
\r
216 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
217 newSizeX, newSizeY);
\r
221 GetActualPlacement( gameListDialog, &wpGameList );
\r
224 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title
\r
225 GameListHighlight(lastLoadGameNumber);
\r
229 newSizeX = LOWORD(lParam);
\r
230 newSizeY = HIWORD(lParam);
\r
231 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
232 sizeX, sizeY, newSizeX, newSizeY);
\r
237 case WM_ENTERSIZEMOVE:
\r
238 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
241 return OnSizing( &sd, hDlg, wParam, lParam );
\r
244 return OnMoving( &sd, hDlg, wParam, lParam );
\r
246 case WM_EXITSIZEMOVE:
\r
247 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
249 case WM_GETMINMAXINFO:
\r
250 /* Prevent resizing window too small */
\r
251 mmi = (MINMAXINFO *) lParam;
\r
252 mmi->ptMinTrackSize.x = 100;
\r
253 mmi->ptMinTrackSize.y = 100;
\r
259 If <Enter> is pressed while editing the filter, it's better to apply
\r
260 the filter rather than selecting the current game.
\r
262 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
263 switch( HIWORD(wParam) ) {
\r
265 filterHasFocus = TRUE;
\r
268 filterHasFocus = FALSE;
\r
273 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
274 wParam = IDC_GameListDoFilter;
\r
276 /* [AS] End command replacement */
\r
278 switch (LOWORD(wParam)) {
\r
279 case OPT_GameListLoad:
\r
280 LoadOptionsPopup(hDlg);
\r
283 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
285 /* is this possible? */
\r
286 DisplayError(_("No game selected"), 0);
\r
289 break; /* load the game*/
\r
291 case OPT_GameListNext:
\r
292 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
294 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
295 /* [AS] Removed error message */
\r
296 /* DisplayError(_("Can't go forward any further"), 0); */
\r
299 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
300 break; /* load the game*/
\r
302 case OPT_GameListPrev:
\r
304 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
307 /* [AS] Removed error message, added return */
\r
308 /* DisplayError(_("Can't back up any further"), 0); */
\r
311 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
312 break; /* load the game*/
\r
315 case OPT_GameListFind:
\r
316 case IDC_GameListDoFilter:
\r
318 char filter[MAX_FILTER_LENGTH+1];
\r
320 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
321 filter[ sizeof(filter)-1 ] = '\0';
\r
322 count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter, LOWORD(wParam)==OPT_GameListNarrow );
\r
323 GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
330 case OPT_GameListClose:
\r
334 case OPT_GameListText:
\r
335 switch (HIWORD(wParam)) {
\r
337 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
338 break; /* load the game*/
\r
349 /* Load the game */
\r
351 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
352 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
360 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
362 if( res == LB_ERR ) {
\r
366 text = (char *) malloc( res+1 );
\r
368 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
370 index = atoi( text );
\r
375 /* [AS] End: nItem has been "patched" now! */
\r
377 if (cmailMsgLoaded) {
\r
378 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
381 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
382 SetFocus(hwndMain); // [HGM] automatic focus switch
\r
395 VOID GameListPopUp(FILE *fp, char *filename)
\r
400 if (gameFileName != filename) {
\r
401 if (gameFileName) free(gameFileName);
\r
402 gameFileName = StrSave(filename);
\r
404 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
405 EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_ENABLED);
\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
424 VOID GameListPopDown(void)
\r
426 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
427 EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_GRAYED);
\r
428 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
429 gameListUp = FALSE;
\r
433 VOID GameListHighlight(int index)
\r
436 int i, j, k, n, res = 0;
\r
437 if (gameListDialog == NULL) return;
\r
438 for(i=64; ; i+=i) {
\r
439 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );
\r
440 if(res == LB_ERR || index < atoi( buf )) break;
\r
445 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, n, (LPARAM)buf );
\r
446 if(res == LB_ERR || index < (k = atoi( buf ))) i = n; else {
\r
448 if(index == k) break;
\r
451 SendDlgItemMessage(gameListDialog, OPT_GameListText, LB_SETCURSEL, j, 0);
\r
455 VOID GameListDestroy()
\r
458 if (gameFileName) {
\r
459 free(gameFileName);
\r
460 gameFileName = NULL;
\r
464 VOID ShowGameListProc()
\r
467 if(gameListDialog) SetFocus(gameListDialog);
\r
468 // GameListPopDown();
\r
470 if (gameFileName) {
\r
471 GameListPopUp(gameFile, gameFileName);
\r
473 DisplayError(_("No game list"), 0);
\r
478 HGLOBAL ExportGameListAsText()
\r
480 HGLOBAL result = NULL;
\r
481 LPVOID lpMem = NULL;
\r
482 ListGame * lg = (ListGame *) gameList.head;
\r
486 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
487 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
491 /* Get list size */
\r
492 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
493 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
495 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
498 lg = (ListGame *) lg->node.succ;
\r
501 /* Allocate memory for the list */
\r
502 result = GlobalAlloc(GHND, dwLen+1 );
\r
504 if( result != NULL ) {
\r
505 lpMem = GlobalLock(result);
\r
508 /* Copy the list into the global memory block */
\r
509 if( lpMem != NULL ) {
\r
510 char * dst = (char *) lpMem;
\r
513 lg = (ListGame *) gameList.head;
\r
515 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
516 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
518 len = sprintf( dst, "%s\r\n", st );
\r
522 lg = (ListGame *) lg->node.succ;
\r
525 GlobalUnlock( result );
\r