2 * wgamelist.c -- Game list window for WinBoard
\r
3 * $Id: wgamelist.c,v 2.1 2003/10/27 19:21:02 mann Exp $
\r
5 * Copyright 1995,2009 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\r
9 * GNU XBoard is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or (at
\r
12 * your option) any later version.
\r
14 * GNU XBoard is distributed in the hope that it will be useful, but
\r
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
17 * General Public License for more details.
\r
19 * You should have received a copy of the GNU General Public License
\r
20 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
22 *------------------------------------------------------------------------
\r
23 ** See the file ChangeLog for a revision history. */
\r
27 #include <windows.h> /* required for all Windows applications */
\r
33 #include <commdlg.h>
\r
37 #include "winboard.h"
\r
38 #include "frontend.h"
\r
39 #include "backend.h"
\r
43 /* Module globals */
\r
44 HWND gameListDialog = NULL;
\r
45 BOOLEAN gameListUp = FALSE;
\r
47 char* gameFileName = NULL;
\r
48 int gameListX, gameListY, gameListW, gameListH;
\r
50 /* Imports from winboard.c */
\r
51 extern HINSTANCE hInst;
\r
52 extern HWND hwndMain;
\r
54 struct GameListStats
\r
62 /* [AS] Wildcard pattern matching */
\r
63 static BOOL HasPattern( const char * text, const char * pattern )
\r
65 while( *pattern != '\0' ) {
\r
66 if( *pattern == '*' ) {
\r
67 while( *pattern == '*' ) {
\r
71 if( *pattern == '\0' ) {
\r
75 while( *text != '\0' ) {
\r
76 if( HasPattern( text, pattern ) ) {
\r
82 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
94 static BOOL SearchPattern( const char * text, const char * pattern )
\r
98 if( pattern != NULL && *pattern != '\0' ) {
\r
99 if( *pattern == '*' ) {
\r
100 result = HasPattern( text, pattern );
\r
105 while( *text != '\0' ) {
\r
106 if( HasPattern( text, pattern ) ) {
\r
118 /* [AS] Setup the game list according to the specified filter */
\r
119 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
121 ListGame * lg = (ListGame *) gameList.head;
\r
123 BOOL hasFilter = FALSE;
\r
125 struct GameListStats dummy;
\r
127 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
128 if( stats == NULL ) {
\r
132 stats->white_wins = 0;
\r
133 stats->black_wins = 0;
\r
135 stats->unfinished = 0;
\r
138 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
141 if( pszFilter != NULL ) {
\r
142 if( strlen( pszFilter ) > 0 ) {
\r
147 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
148 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
152 if( ! SearchPattern( st, pszFilter ) ) {
\r
158 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
162 if( lg->gameInfo.result == WhiteWins )
\r
163 stats->white_wins++;
\r
164 else if( lg->gameInfo.result == BlackWins )
\r
165 stats->black_wins++;
\r
166 else if( lg->gameInfo.result == GameIsDrawn )
\r
169 stats->unfinished++;
\r
173 lg = (ListGame *) lg->node.succ;
\r
176 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
181 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
182 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
186 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
189 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
192 SetWindowText( hDlg, buf );
\r
197 #define MAX_FILTER_LENGTH 128
\r
200 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
202 static char szDlgTitle[64];
\r
203 static HANDLE hwndText;
\r
206 static int sizeX, sizeY;
\r
207 int newSizeX, newSizeY, flags;
\r
209 static BOOL filterHasFocus = FALSE;
\r
211 struct GameListStats stats;
\r
212 static SnapData sd;
\r
215 case WM_INITDIALOG:
\r
216 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
217 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
219 if (gameListDialog) {
\r
220 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
223 /* Initialize the dialog items */
\r
224 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
226 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
228 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
229 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
231 filterHasFocus = FALSE;
\r
233 /* Size and position the dialog */
\r
234 if (!gameListDialog) {
\r
235 gameListDialog = hDlg;
\r
236 flags = SWP_NOZORDER;
\r
237 GetClientRect(hDlg, &rect);
\r
238 sizeX = rect.right;
\r
239 sizeY = rect.bottom;
\r
240 if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&
\r
241 gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {
\r
242 WINDOWPLACEMENT wp;
\r
243 EnsureOnScreen(&gameListX, &gameListY);
\r
244 wp.length = sizeof(WINDOWPLACEMENT);
\r
246 wp.showCmd = SW_SHOW;
\r
247 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
248 wp.rcNormalPosition.left = gameListX;
\r
249 wp.rcNormalPosition.right = gameListX + gameListW;
\r
250 wp.rcNormalPosition.top = gameListY;
\r
251 wp.rcNormalPosition.bottom = gameListY + gameListH;
\r
252 SetWindowPlacement(hDlg, &wp);
\r
254 GetClientRect(hDlg, &rect);
\r
255 newSizeX = rect.right;
\r
256 newSizeY = rect.bottom;
\r
257 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
258 newSizeX, newSizeY);
\r
263 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
268 newSizeX = LOWORD(lParam);
\r
269 newSizeY = HIWORD(lParam);
\r
270 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
271 sizeX, sizeY, newSizeX, newSizeY);
\r
276 case WM_ENTERSIZEMOVE:
\r
277 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
280 return OnSizing( &sd, hDlg, wParam, lParam );
\r
283 return OnMoving( &sd, hDlg, wParam, lParam );
\r
285 case WM_EXITSIZEMOVE:
\r
286 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
288 case WM_GETMINMAXINFO:
\r
289 /* Prevent resizing window too small */
\r
290 mmi = (MINMAXINFO *) lParam;
\r
291 mmi->ptMinTrackSize.x = 100;
\r
292 mmi->ptMinTrackSize.y = 100;
\r
298 If <Enter> is pressed while editing the filter, it's better to apply
\r
299 the filter rather than selecting the current game.
\r
301 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
302 switch( HIWORD(wParam) ) {
\r
304 filterHasFocus = TRUE;
\r
307 filterHasFocus = FALSE;
\r
312 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
313 wParam = IDC_GameListDoFilter;
\r
315 /* [AS] End command replacement */
\r
317 switch (LOWORD(wParam)) {
\r
319 case OPT_GameListLoad:
\r
320 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
322 /* is this possible? */
\r
323 DisplayError("No game selected", 0);
\r
326 break; /* load the game*/
\r
328 case OPT_GameListNext:
\r
329 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
331 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
332 /* [AS] Removed error message */
\r
333 /* DisplayError("Can't go forward any further", 0); */
\r
336 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
337 break; /* load the game*/
\r
339 case OPT_GameListPrev:
\r
340 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
343 /* [AS] Removed error message, added return */
\r
344 /* DisplayError("Can't back up any further", 0); */
\r
347 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
348 break; /* load the game*/
\r
351 case IDC_GameListDoFilter:
\r
353 char filter[MAX_FILTER_LENGTH+1];
\r
355 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
356 filter[ sizeof(filter)-1 ] = '\0';
\r
357 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
358 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
365 case OPT_GameListClose:
\r
369 case OPT_GameListText:
\r
370 switch (HIWORD(wParam)) {
\r
372 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
373 break; /* load the game*/
\r
384 /* Load the game */
\r
386 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
387 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
395 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
397 if( res == LB_ERR ) {
\r
401 text = (char *) malloc( res+1 );
\r
403 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
405 index = atoi( text );
\r
410 /* [AS] End: nItem has been "patched" now! */
\r
412 if (cmailMsgLoaded) {
\r
413 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
416 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
429 VOID GameListPopUp(FILE *fp, char *filename)
\r
434 if (gameFileName != filename) {
\r
435 if (gameFileName) free(gameFileName);
\r
436 gameFileName = StrSave(filename);
\r
438 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
439 if (gameListDialog) {
\r
440 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
441 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
443 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
444 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
445 hwndMain, (DLGPROC)lpProc);
\r
446 FreeProcInstance(lpProc);
\r
451 VOID GameListPopDown(void)
\r
453 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
454 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
455 gameListUp = FALSE;
\r
459 VOID GameListHighlight(int index)
\r
461 if (gameListDialog == NULL) return;
\r
462 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
463 LB_SETCURSEL, index - 1, 0);
\r
467 VOID GameListDestroy()
\r
470 if (gameFileName) {
\r
471 free(gameFileName);
\r
472 gameFileName = NULL;
\r
476 VOID ShowGameListProc()
\r
481 if (gameFileName) {
\r
482 GameListPopUp(gameFile, gameFileName);
\r
484 DisplayError("No game list", 0);
\r
489 HGLOBAL ExportGameListAsText()
\r
491 HGLOBAL result = NULL;
\r
492 LPVOID lpMem = NULL;
\r
493 ListGame * lg = (ListGame *) gameList.head;
\r
497 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
498 DisplayError("Game list not loaded or empty", 0);
\r
502 /* Get list size */
\r
503 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
504 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
506 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
509 lg = (ListGame *) lg->node.succ;
\r
512 /* Allocate memory for the list */
\r
513 result = GlobalAlloc(GHND, dwLen+1 );
\r
515 if( result != NULL ) {
\r
516 lpMem = GlobalLock(result);
\r
519 /* Copy the list into the global memory block */
\r
520 if( lpMem != NULL ) {
\r
521 char * dst = (char *) lpMem;
\r
524 lg = (ListGame *) gameList.head;
\r
526 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
527 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
529 len = sprintf( dst, "%s\r\n", st );
\r
533 lg = (ListGame *) lg->node.succ;
\r
536 GlobalUnlock( result );
\r