2 * wgamelist.c -- Game list window for WinBoard
\r
3 * $Id: wgamelist.c,v 2.1 2003/10/27 19:21:02 mann Exp $
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
26 #include <windows.h> /* required for all Windows applications */
\r
32 #include <commdlg.h>
\r
36 #include "winboard.h"
\r
37 #include "frontend.h"
\r
38 #include "backend.h"
\r
40 /* Module globals */
\r
41 HWND gameListDialog = NULL;
\r
42 BOOLEAN gameListUp = FALSE;
\r
44 char* gameFileName = NULL;
\r
45 int gameListX, gameListY, gameListW, gameListH;
\r
47 /* Imports from winboard.c */
\r
48 extern HINSTANCE hInst;
\r
49 extern HWND hwndMain;
\r
59 /* [AS] Wildcard pattern matching */
60 static BOOL HasPattern( const char * text, const char * pattern )
62 while( *pattern != '\0' ) {
63 if( *pattern == '*' ) {
64 while( *pattern == '*' ) {
68 if( *pattern == '\0' ) {
72 while( *text != '\0' ) {
73 if( HasPattern( text, pattern ) ) {
79 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
91 static BOOL SearchPattern( const char * text, const char * pattern )
95 if( pattern != NULL && *pattern != '\0' ) {
96 if( *pattern == '*' ) {
97 result = HasPattern( text, pattern );
102 while( *text != '\0' ) {
103 if( HasPattern( text, pattern ) ) {
115 /* [AS] Setup the game list according to the specified filter */
116 static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
118 ListGame * lg = (ListGame *) gameList.head;
120 BOOL hasFilter = FALSE;
122 struct GameListStats dummy;
124 /* Initialize stats (use a dummy variable if caller not interested in them) */
125 if( stats == NULL ) {
129 stats->white_wins = 0;
130 stats->black_wins = 0;
132 stats->unfinished = 0;
135 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
138 if( pszFilter != NULL ) {
139 if( strlen( pszFilter ) > 0 ) {
144 for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
145 char * st = GameListLine(lg->number, &lg->gameInfo);
149 if( ! SearchPattern( st, pszFilter ) ) {
155 SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
159 if( lg->gameInfo.result == WhiteWins )
161 else if( lg->gameInfo.result == BlackWins )
163 else if( lg->gameInfo.result == GameIsDrawn )
170 lg = (ListGame *) lg->node.succ;
173 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
178 /* [AS] Show number of visible (filtered) games and total on window caption */
179 static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
183 sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
186 sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
189 SetWindowText( hDlg, buf );
194 #define MAX_FILTER_LENGTH 128
197 GameListDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
199 static char szDlgTitle[64];
200 static HANDLE hwndText;
\r
203 static int sizeX, sizeY;
\r
204 int newSizeX, newSizeY, flags;
\r
206 static BOOL filterHasFocus = FALSE;
208 struct GameListStats stats;
211 case WM_INITDIALOG:
\r
212 GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
213 szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
215 if (gameListDialog) {
\r
216 SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
\r
219 /* Initialize the dialog items */
\r
220 hwndText = GetDlgItem(hDlg, OPT_TagsText);
\r
222 count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
224 SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
225 SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
227 filterHasFocus = FALSE;
229 /* Size and position the dialog */
\r
230 if (!gameListDialog) {
\r
231 gameListDialog = hDlg;
\r
232 flags = SWP_NOZORDER;
\r
233 GetClientRect(hDlg, &rect);
\r
234 sizeX = rect.right;
\r
235 sizeY = rect.bottom;
\r
236 if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&
\r
237 gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {
\r
238 WINDOWPLACEMENT wp;
\r
239 EnsureOnScreen(&gameListX, &gameListY);
\r
240 wp.length = sizeof(WINDOWPLACEMENT);
\r
242 wp.showCmd = SW_SHOW;
\r
243 wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
\r
244 wp.rcNormalPosition.left = gameListX;
\r
245 wp.rcNormalPosition.right = gameListX + gameListW;
\r
246 wp.rcNormalPosition.top = gameListY;
\r
247 wp.rcNormalPosition.bottom = gameListY + gameListH;
\r
248 SetWindowPlacement(hDlg, &wp);
\r
250 GetClientRect(hDlg, &rect);
\r
251 newSizeX = rect.right;
\r
252 newSizeY = rect.bottom;
\r
253 ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
\r
254 newSizeX, newSizeY);
\r
259 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
264 newSizeX = LOWORD(lParam);
\r
265 newSizeY = HIWORD(lParam);
\r
266 ResizeEditPlusButtons(hDlg, GetDlgItem(hDlg, OPT_GameListText),
\r
267 sizeX, sizeY, newSizeX, newSizeY);
\r
272 case WM_GETMINMAXINFO:
\r
273 /* Prevent resizing window too small */
\r
274 mmi = (MINMAXINFO *) lParam;
\r
275 mmi->ptMinTrackSize.x = 100;
\r
276 mmi->ptMinTrackSize.y = 100;
\r
282 If <Enter> is pressed while editing the filter, it's better to apply
283 the filter rather than selecting the current game.
285 if( LOWORD(wParam) == IDC_GameListFilter ) {
286 switch( HIWORD(wParam) ) {
288 filterHasFocus = TRUE;
291 filterHasFocus = FALSE;
296 if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
297 wParam = IDC_GameListDoFilter;
299 /* [AS] End command replacement */
301 switch (LOWORD(wParam)) {
\r
303 case OPT_GameListLoad:
\r
304 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
306 /* is this possible? */
\r
307 DisplayError("No game selected", 0);
\r
310 break; /* load the game*/
\r
312 case OPT_GameListNext:
\r
313 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
315 if (nItem >= ((ListGame *) gameList.tailPred)->number) {
\r
316 /* [AS] Removed error message */
317 /* DisplayError("Can't go forward any further", 0); */
320 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
321 break; /* load the game*/
\r
323 case OPT_GameListPrev:
\r
324 nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
\r
327 /* [AS] Removed error message, added return */
328 /* DisplayError("Can't back up any further", 0); */
331 SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);
\r
332 break; /* load the game*/
\r
335 case IDC_GameListDoFilter:
337 char filter[MAX_FILTER_LENGTH+1];
339 if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
340 filter[ sizeof(filter)-1 ] = '\0';
341 count = GameListToListBox( hDlg, TRUE, filter, &stats );
342 GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
349 case OPT_GameListClose:
\r
353 case OPT_GameListText:
\r
354 switch (HIWORD(wParam)) {
\r
356 nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
\r
357 break; /* load the game*/
\r
368 /* Load the game */
\r
370 /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
371 int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
379 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
381 if( res == LB_ERR ) {
385 text = (char *) malloc( res+1 );
387 res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
389 index = atoi( text );
394 /* [AS] End: nItem has been "patched" now! */
396 if (cmailMsgLoaded) {
\r
397 CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
400 LoadGame(gameFile, nItem + 1, gameFileName, TRUE);
\r
413 VOID GameListPopUp(FILE *fp, char *filename)
\r
418 if (gameFileName != filename) {
\r
419 if (gameFileName) free(gameFileName);
\r
420 gameFileName = StrSave(filename);
\r
422 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);
\r
423 if (gameListDialog) {
\r
424 SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);
\r
425 if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);
\r
427 lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);
\r
428 CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),
\r
429 hwndMain, (DLGPROC)lpProc);
\r
430 FreeProcInstance(lpProc);
\r
435 VOID GameListPopDown(void)
\r
437 CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);
\r
438 if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);
\r
439 gameListUp = FALSE;
\r
443 VOID GameListHighlight(int index)
\r
445 if (gameListDialog == NULL) return;
\r
446 SendDlgItemMessage(gameListDialog, OPT_GameListText,
\r
447 LB_SETCURSEL, index - 1, 0);
\r
451 VOID GameListDestroy()
\r
454 if (gameFileName) {
\r
455 free(gameFileName);
\r
456 gameFileName = NULL;
\r
460 VOID ShowGameListProc()
\r
465 if (gameFileName) {
\r
466 GameListPopUp(gameFile, gameFileName);
\r
468 DisplayError("No game list", 0);
\r