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 Free Software Foundation, Inc.
\r
7 * ------------------------------------------------------------------------
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program; if not, write to the Free Software
\r
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
21 * ------------------------------------------------------------------------
\r
25 #include <windows.h> /* required for all Windows applications */
\r
31 #include <commdlg.h>
\r
35 #include "winboard.h"
\r
36 #include "frontend.h"
\r
37 #include "backend.h"
\r
41 /* Module globals */
\r
42 HWND gameListDialog = NULL;
\r
43 BOOLEAN gameListUp = FALSE;
\r
45 char* gameFileName = NULL;
\r
46 int gameListX, gameListY, gameListW, gameListH;
\r
48 /* Imports from winboard.c */
\r
49 extern HINSTANCE hInst;
\r
50 extern HWND hwndMain;
\r
52 struct GameListStats
\r
60 /* [AS] Wildcard pattern matching */
\r
61 static BOOL HasPattern( const char * text, const char * pattern )
\r
63 while( *pattern != '\0' ) {
\r
64 if( *pattern == '*' ) {
\r
65 while( *pattern == '*' ) {
\r
69 if( *pattern == '\0' ) {
\r
73 while( *text != '\0' ) {
\r
74 if( HasPattern( text, pattern ) ) {
\r
80 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
92 static BOOL SearchPattern( const char * text, const char * pattern )
\r
96 if( pattern != NULL && *pattern != '\0' ) {
\r
97 if( *pattern == '*' ) {
\r
98 result = HasPattern( text, pattern );
\r
103 while( *text != '\0' ) {
\r
104 if( HasPattern( text, pattern ) ) {
\r
116 /* [AS] Setup the game list according to the specified filter */
\r
117 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
\r
119 ListGame * lg = (ListGame *) gameList.head;
\r
121 BOOL hasFilter = FALSE;
\r
123 struct GameListStats dummy;
\r
125 /* Initialize stats (use a dummy variable if caller not interested in them) */
\r
126 if( stats == NULL ) {
\r
130 stats->white_wins = 0;
\r
131 stats->black_wins = 0;
\r
133 stats->unfinished = 0;
\r
136 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
139 if( pszFilter != NULL ) {
\r
140 if( strlen( pszFilter ) > 0 ) {
\r
145 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
146 char * st = GameListLine(lg->number, &lg->gameInfo);
\r
150 if( ! SearchPattern( st, pszFilter ) ) {
\r
156 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
\r
160 if( lg->gameInfo.result == WhiteWins )
\r
161 stats->white_wins++;
\r
162 else if( lg->gameInfo.result == BlackWins )
\r
163 stats->black_wins++;
\r
164 else if( lg->gameInfo.result == GameIsDrawn )
\r
167 stats->unfinished++;
\r
171 lg = (ListGame *) lg->node.succ;
\r
174 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
\r
179 /* [AS] Show number of visible (filtered) games and total on window caption */
\r
180 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
\r
184 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
\r
187 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
\r
190 SetWindowText( hDlg, buf );
\r
195 #define MAX_FILTER_LENGTH 128
\r
198 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
200 static char szDlgTitle[64];
\r
201 static HANDLE hwndText;
\r
204 static int sizeX, sizeY;
\r
205 int newSizeX, newSizeY, flags;
\r
207 static BOOL filterHasFocus = FALSE;
\r
209 struct GameListStats stats;
\r
210 static SnapData sd;
\r
213 case WM_INITDIALOG:
\r
214 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
\r
215 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
\r
217 if (gameListDialog) {
\r
218 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
221 /* Initialize the dialog items */
\r
222 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
224 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
\r
226 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
\r
227 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
\r
229 filterHasFocus = FALSE;
\r
231 /* Size and position the dialog */
\r
232 if (!gameListDialog) {
\r
233 gameListDialog = hDlg;
\r
234 flags = SWP_NOZORDER;
\r
235 GetClientRect(hDlg, &rect);
\r
236 sizeX = rect.right;
\r
237 sizeY = rect.bottom;
\r
238 if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&
\r
239 gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {
\r
240 WINDOWPLACEMENT wp;
\r
241 EnsureOnScreen(&gameListX, &gameListY);
\r
242 wp.length = sizeof(WINDOWPLACEMENT);
\r
244 wp.showCmd = SW_SHOW;
\r
245 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
246 wp.rcNormalPosition.left = gameListX;
\r
247 wp.rcNormalPosition.right = gameListX + gameListW;
\r
248 wp.rcNormalPosition.top = gameListY;
\r
249 wp.rcNormalPosition.bottom = gameListY + gameListH;
\r
250 SetWindowPlacement(hDlg, &wp);
\r
252 GetClientRect(hDlg, &rect);
\r
253 newSizeX = rect.right;
\r
254 newSizeY = rect.bottom;
\r
255 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
256 newSizeX, newSizeY);
\r
261 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
266 newSizeX = LOWORD(lParam);
\r
267 newSizeY = HIWORD(lParam);
\r
268 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
269 sizeX, sizeY, newSizeX, newSizeY);
\r
274 case WM_ENTERSIZEMOVE:
\r
275 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
278 return OnSizing( &sd, hDlg, wParam, lParam );
\r
281 return OnMoving( &sd, hDlg, wParam, lParam );
\r
283 case WM_EXITSIZEMOVE:
\r
284 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
286 case WM_GETMINMAXINFO:
\r
287 /* Prevent resizing window too small */
\r
288 mmi = (MINMAXINFO *) lParam;
\r
289 mmi->ptMinTrackSize.x = 100;
\r
290 mmi->ptMinTrackSize.y = 100;
\r
296 If <Enter> is pressed while editing the filter, it's better to apply
\r
297 the filter rather than selecting the current game.
\r
299 if( LOWORD(wParam) == IDC_GameListFilter ) {
\r
300 switch( HIWORD(wParam) ) {
\r
302 filterHasFocus = TRUE;
\r
305 filterHasFocus = FALSE;
\r
310 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
\r
311 wParam = IDC_GameListDoFilter;
\r
313 /* [AS] End command replacement */
\r
315 switch (LOWORD(wParam)) {
\r
317 case OPT_GameListLoad:
\r
318 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
320 /* is this possible? */
\r
321 DisplayError("No game selected", 0);
\r
324 break; /* load the game*/
\r
326 case OPT_GameListNext:
\r
327 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
329 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
330 /* [AS] Removed error message */
\r
331 /* DisplayError("Can't go forward any further", 0); */
\r
334 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
335 break; /* load the game*/
\r
337 case OPT_GameListPrev:
\r
338 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
341 /* [AS] Removed error message, added return */
\r
342 /* DisplayError("Can't back up any further", 0); */
\r
345 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
346 break; /* load the game*/
\r
349 case IDC_GameListDoFilter:
\r
351 char filter[MAX_FILTER_LENGTH+1];
\r
353 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
\r
354 filter[ sizeof(filter)-1 ] = '\0';
\r
355 count = GameListToListBox( hDlg, TRUE, filter, &stats );
\r
356 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
\r
363 case OPT_GameListClose:
\r
367 case OPT_GameListText:
\r
368 switch (HIWORD(wParam)) {
\r
370 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
371 break; /* load the game*/
\r
382 /* Load the game */
\r
384 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
\r
385 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
393 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
\r
395 if( res == LB_ERR ) {
\r
399 text = (char *) malloc( res+1 );
\r
401 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
\r
403 index = atoi( text );
\r
408 /* [AS] End: nItem has been "patched" now! */
\r
410 if (cmailMsgLoaded) {
\r
411 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
414 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
427 VOID GameListPopUp(FILE *fp, char *filename)
\r
432 if (gameFileName != filename) {
\r
433 if (gameFileName) free(gameFileName);
\r
434 gameFileName = StrSave(filename);
\r
436 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
437 if (gameListDialog) {
\r
438 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
439 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
441 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
442 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
443 hwndMain, (DLGPROC)lpProc);
\r
444 FreeProcInstance(lpProc);
\r
449 VOID GameListPopDown(void)
\r
451 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
452 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
453 gameListUp = FALSE;
\r
457 VOID GameListHighlight(int index)
\r
459 if (gameListDialog == NULL) return;
\r
460 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
461 LB_SETCURSEL, index - 1, 0);
\r
465 VOID GameListDestroy()
\r
468 if (gameFileName) {
\r
469 free(gameFileName);
\r
470 gameFileName = NULL;
\r
474 VOID ShowGameListProc()
\r
479 if (gameFileName) {
\r
480 GameListPopUp(gameFile, gameFileName);
\r
482 DisplayError("No game list", 0);
\r
487 HGLOBAL ExportGameListAsText()
\r
489 HGLOBAL result = NULL;
\r
490 LPVOID lpMem = NULL;
\r
491 ListGame * lg = (ListGame *) gameList.head;
\r
495 if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {
\r
496 DisplayError("Game list not loaded or empty", 0);
\r
500 /* Get list size */
\r
501 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
502 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
504 dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */
\r
507 lg = (ListGame *) lg->node.succ;
\r
510 /* Allocate memory for the list */
\r
511 result = GlobalAlloc(GHND, dwLen+1 );
\r
513 if( result != NULL ) {
\r
514 lpMem = GlobalLock(result);
\r
517 /* Copy the list into the global memory block */
\r
518 if( lpMem != NULL ) {
\r
519 char * dst = (char *) lpMem;
\r
522 lg = (ListGame *) gameList.head;
\r
524 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
\r
525 char * st = GameListLineFull(lg->number, &lg->gameInfo);
\r
527 len = sprintf( dst, "%s\r\n", st );
\r
531 lg = (ListGame *) lg->node.succ;
\r
534 GlobalUnlock( result );
\r