changes from Alessandro Scotti from 20051129
[xboard.git] / winboard / wgamelist.c
index 90528aa..34339d4 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  * wgamelist.c -- Game list window for WinBoard\r
- * $Id$\r
+ * $Id: wgamelist.c,v 2.1 2003/10/27 19:21:02 mann Exp $
  *\r
  * Copyright 1995 Free Software Foundation, Inc.\r
  *\r
@@ -48,33 +48,184 @@ int gameListX, gameListY, gameListW, gameListH;
 extern HINSTANCE hInst;\r
 extern HWND hwndMain;\r
 \r
+struct GameListStats
+{
+    int white_wins;
+    int black_wins;
+    int drawn;
+    int unfinished;
+};
+
+/* [AS] Wildcard pattern matching */
+static BOOL HasPattern( const char * text, const char * pattern )
+{
+    while( *pattern != '\0' ) {
+        if( *pattern == '*' ) {
+            while( *pattern == '*' ) {
+                pattern++;
+            }
+
+            if( *pattern == '\0' ) {
+                return TRUE;
+            }
+
+            while( *text != '\0' ) {
+                if( HasPattern( text, pattern ) ) {
+                    return TRUE;
+                }
+                text++;
+            }
+        }
+        else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
+            pattern++;
+            text++;
+            continue;
+        }
+
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+static BOOL SearchPattern( const char * text, const char * pattern )
+{
+    BOOL result = TRUE;
+
+    if( pattern != NULL && *pattern != '\0' ) {
+        if( *pattern == '*' ) {
+            result = HasPattern( text, pattern );
+        }
+        else {
+            result = FALSE;
+
+            while( *text != '\0' ) {
+                if( HasPattern( text, pattern ) ) {
+                    result = TRUE;
+                    break;
+                }
+                text++;
+            }
+        }
+    }
+
+    return result;
+}
+
+/* [AS] Setup the game list according to the specified filter */
+static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )
+{
+    ListGame * lg = (ListGame *) gameList.head;
+    int nItem;
+    BOOL hasFilter = FALSE;
+    int count = 0;
+    struct GameListStats dummy;
+
+    /* Initialize stats (use a dummy variable if caller not interested in them) */
+    if( stats == NULL ) {
+        stats = &dummy;
+    }
+
+    stats->white_wins = 0;
+    stats->black_wins = 0;
+    stats->drawn = 0;
+    stats->unfinished = 0;
+
+    if( boReset ) {
+        SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);
+    }
+
+    if( pszFilter != NULL ) {
+        if( strlen( pszFilter ) > 0 ) {
+            hasFilter = TRUE;
+        }
+    }
+
+    for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
+        char * st = GameListLine(lg->number, &lg->gameInfo);
+        BOOL skip = FALSE;
+
+        if( hasFilter ) {
+            if( ! SearchPattern( st, pszFilter ) ) {
+                skip = TRUE;
+            }
+        }
+
+        if( ! skip ) {
+            SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);
+            count++;
+
+            /* Update stats */
+            if( lg->gameInfo.result == WhiteWins )
+                stats->white_wins++;
+            else if( lg->gameInfo.result == BlackWins )
+                stats->black_wins++;
+            else if( lg->gameInfo.result == GameIsDrawn )
+                stats->drawn++;
+            else
+                stats->unfinished++;
+        }
+
+        free(st);
+        lg = (ListGame *) lg->node.succ;
+    }
+
+    SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);
+
+    return count;
+}
+
+/* [AS] Show number of visible (filtered) games and total on window caption */
+static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )
+{
+    char buf[256];
+
+    sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );
+
+    if( stats != 0 ) {
+        sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );
+    }
+
+    SetWindowText( hDlg, buf );
+
+    return 0;
+}
+
+#define MAX_FILTER_LENGTH   128
 \r
 LRESULT CALLBACK\r
 GameListDialog(HWND hDlg, UINT message,        WPARAM wParam, LPARAM lParam)\r
 {\r
+  static char szDlgTitle[64];
   static HANDLE hwndText;\r
   int nItem;\r
-  ListGame *lg;\r
   RECT rect;\r
   static int sizeX, sizeY;\r
   int newSizeX, newSizeY, flags;\r
   MINMAXINFO *mmi;\r
+  static BOOL filterHasFocus = FALSE;
+  int count;
+  struct GameListStats stats;
 \r
   switch (message) {\r
   case WM_INITDIALOG: \r
+    GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );
+    szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';
+
     if (gameListDialog) {\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
     }\r
+
     /* Initialize the dialog items */\r
     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
-    lg = (ListGame *) gameList.head;\r
-    for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
-      char *st = GameListLine(lg->number, &lg->gameInfo);\r
-      SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
-      free(st);\r
-      lg = (ListGame *) lg->node.succ;\r
-    }\r
-    SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
+
+    count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );
+
+    SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );
+    SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );
+
+    filterHasFocus = FALSE;
+
     /* Size and position the dialog */\r
     if (!gameListDialog) {\r
       gameListDialog = hDlg;\r
@@ -104,6 +255,8 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
        sizeX = newSizeX;\r
        sizeY = newSizeY;\r
       }\r
+
+      GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
     }\r
     return FALSE;\r
     \r
@@ -124,6 +277,27 @@ GameListDialog(HWND hDlg, UINT message,    WPARAM wParam, LPARAM lParam)
     break;\r
 \r
   case WM_COMMAND:\r
+      /*
+        [AS]
+        If <Enter> is pressed while editing the filter, it's better to apply
+        the filter rather than selecting the current game.
+      */
+      if( LOWORD(wParam) == IDC_GameListFilter ) {
+          switch( HIWORD(wParam) ) {
+          case EN_SETFOCUS:
+              filterHasFocus = TRUE;
+              break;
+          case EN_KILLFOCUS:
+              filterHasFocus = FALSE;
+              break;
+          }
+      }
+
+      if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {
+          wParam = IDC_GameListDoFilter;
+      }
+      /* [AS] End command replacement */
+
     switch (LOWORD(wParam)) {\r
     case IDOK:\r
     case OPT_GameListLoad:\r
@@ -139,7 +313,8 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
       nItem++;\r
       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
-       DisplayError("Can't go forward any further", 0);\r
+        /* [AS] Removed error message */
+       /* DisplayError("Can't go forward any further", 0); */
        return TRUE;\r
       }\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
@@ -149,11 +324,27 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
       nItem--;\r
       if (nItem < 0) {\r
-       DisplayError("Can't back up any further", 0);\r
+        /* [AS] Removed error message, added return */
+       /* DisplayError("Can't back up any further", 0); */
+        return TRUE;
       }\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
       break; /* load the game*/\r
       \r
+    /* [AS] */
+    case IDC_GameListDoFilter:
+        {
+            char filter[MAX_FILTER_LENGTH+1];
+
+            if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {
+                filter[ sizeof(filter)-1 ] = '\0';
+                count = GameListToListBox( hDlg, TRUE, filter, &stats );
+                GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );
+            }
+        }
+        return FALSE;
+        break;
+
     case IDCANCEL:\r
     case OPT_GameListClose:\r
       GameListPopDown();\r
@@ -173,12 +364,43 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
     default:\r
       return FALSE;\r
     }\r
+
     /* Load the game */\r
+    {
+        /* [AS] Get index from the item itself, because filtering makes original order unuseable. */
+        int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);
+        char * text;
+        LRESULT res;
+
+        if( index < 0 ) {
+            return TRUE;
+        }
+
+        res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );
+
+        if( res == LB_ERR ) {
+            return TRUE;
+        }
+
+        text = (char *) malloc( res+1 );
+
+        res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );
+
+        index = atoi( text );
+
+        nItem = index - 1;
+
+        free( text );
+        /* [AS] End: nItem has been "patched" now! */
+
     if (cmailMsgLoaded) {\r
       CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
-    } else {\r
+        }
+        else {
       LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
     }\r
+    }
+
     return TRUE;\r
 \r
   default:\r