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 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
94 if(nItem % 2000 == 0) {
\r
95 snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);
\r
96 SetWindowText(hwndMain, buf);
\r
100 st = GameListLine(lg->number, &lg->gameInfo);
\r
101 if( !SearchPattern( st, pszFilter) ) skip = TRUE;
\r
104 if( !skip && byPos) {
\r
105 if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;
\r
108 lg->position = pos;
\r
111 if(!st) st = GameListLine(lg->number, &lg->gameInfo);
\r
112 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
116 if( lg->gameInfo.result == WhiteWins )
\r
117 stats->white_wins++;
\r
118 else if( lg->gameInfo.result == BlackWins )
\r
119 stats->black_wins++;
\r
120 else if( lg->gameInfo.result == GameIsDrawn )
\r
123 stats->unfinished++;
\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, flags;
\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 );
\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 flags = SWP_NOZORDER;
\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
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_GameListPrev:
\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 );
\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 if (gameListDialog) {
\r
405 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
406 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
407 else SetFocus(gameListDialog);
\r
409 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
410 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
411 hwndMain, (DLGPROC)lpProc);
\r
412 FreeProcInstance(lpProc);
\r
417 VOID GameListPopDown(void)
\r
419 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
420 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
421 gameListUp = FALSE;
\r
425 VOID GameListHighlight(int index)
\r
429 if (gameListDialog == NULL) return;
\r
430 for(i=0; res != LB_ERR; i++) {
\r
431 res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );
\r
432 if(index <= atoi( buf )) break;
\r
434 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
435 LB_SETCURSEL, i, 0);
\r
439 VOID GameListDestroy()
\r
442 if (gameFileName) {
\r
443 free(gameFileName);
\r
444 gameFileName = NULL;
\r
448 VOID ShowGameListProc()
\r
451 if(gameListDialog) SetFocus(gameListDialog);
\r
452 // GameListPopDown();
\r
454 if (gameFileName) {
\r
455 GameListPopUp(gameFile, gameFileName);
\r
457 DisplayError(_("No game list"), 0);
\r
462 HGLOBAL ExportGameListAsText()
\r
464 HGLOBAL result = NULL;
\r
465 LPVOID lpMem = NULL;
\r
466 ListGame * lg = (ListGame *) gameList.head;
\r
470 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
471 DisplayError(_(_("Game list not loaded or empty")), 0);
\r
475 /* Get list size */
\r
476 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
477 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
479 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
482 lg = (ListGame *) lg->node.succ;
\r
485 /* Allocate memory for the list */
\r
486 result = GlobalAlloc(GHND, dwLen+1 );
\r
488 if( result != NULL ) {
\r
489 lpMem = GlobalLock(result);
\r
492 /* Copy the list into the global memory block */
\r
493 if( lpMem != NULL ) {
\r
494 char * dst = (char *) lpMem;
\r
497 lg = (ListGame *) gameList.head;
\r
499 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
500 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
502 len = sprintf( dst, "%s\r\n", st );
\r
506 lg = (ListGame *) lg->node.succ;
\r
509 GlobalUnlock( result );
\r