Fix multi-leg promotions
[xboard.git] / winboard / wgamelist.c
index 31d36ff..19917e6 100644 (file)
@@ -1,7 +1,8 @@
 /*\r
  * wgamelist.c -- Game list window for WinBoard\r
  *\r
- * Copyright 1995,2009 Free Software Foundation, Inc.\r
+ * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free\r
+ * Software Foundation, Inc.\r
  *\r
  * Enhancements Copyright 2005 Alessandro Scotti\r
  *\r
 \r
 #include "wsnap.h"\r
 \r
+#define _(s) T_(s)\r
+\r
 /* Module globals */\r
 static BOOLEAN gameListUp = FALSE;\r
 static FILE* gameFile;\r
 static char* gameFileName = NULL;\r
 \r
-struct GameListStats\r
-{\r
-    int white_wins;\r
-    int black_wins;\r
-    int drawn;\r
-    int unfinished;\r
-};\r
-\r
-/* [AS] Wildcard pattern matching */\r
-static BOOL HasPattern( const char * text, const char * pattern )\r
-{\r
-    while( *pattern != '\0' ) {\r
-        if( *pattern == '*' ) {\r
-            while( *pattern == '*' ) {\r
-                pattern++;\r
-            }\r
-\r
-            if( *pattern == '\0' ) {\r
-                return TRUE;\r
-            }\r
-\r
-            while( *text != '\0' ) {\r
-                if( HasPattern( text, pattern ) ) {\r
-                    return TRUE;\r
-                }\r
-                text++;\r
-            }\r
-        }\r
-        else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {\r
-            pattern++;\r
-            text++;\r
-            continue;\r
-        }\r
-\r
-        return FALSE;\r
-    }\r
-\r
-    return TRUE;\r
-}\r
-\r
-static BOOL SearchPattern( const char * text, const char * pattern )\r
-{\r
-    BOOL result = TRUE;\r
-\r
-    if( pattern != NULL && *pattern != '\0' ) {\r
-        if( *pattern == '*' ) {\r
-            result = HasPattern( text, pattern );\r
-        }\r
-        else {\r
-            result = FALSE;\r
-\r
-            while( *text != '\0' ) {\r
-                if( HasPattern( text, pattern ) ) {\r
-                    result = TRUE;\r
-                    break;\r
-                }\r
-                text++;\r
-            }\r
-        }\r
-    }\r
-\r
-    return result;\r
-}\r
-\r
 /* [AS] Setup the game list according to the specified filter */\r
-static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats )\r
+int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct GameListStats * stats, BOOL byPos, BOOL narrow )\r
 {\r
     ListGame * lg = (ListGame *) gameList.head;\r
     int nItem;\r
+    char buf[MSG_SIZ];\r
     BOOL hasFilter = FALSE;\r
     int count = 0;\r
     struct GameListStats dummy;\r
 \r
+    if(!hDlg) hDlg = gameListDialog; // [HGM] to allow calling from Game List Options dialog\r
+    if(!hDlg) return 0;\r
+\r
     /* Initialize stats (use a dummy variable if caller not interested in them) */\r
-    if( stats == NULL ) { \r
+    if( stats == NULL ) {\r
         stats = &dummy;\r
     }\r
 \r
@@ -139,17 +82,30 @@ static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct
         }\r
     }\r
 \r
+    if(byPos) InitSearch();\r
+\r
     for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
-        char * st = GameListLine(lg->number, &lg->gameInfo);\r
+        char * st = NULL;\r
         BOOL skip = FALSE;\r
+       int pos = -1;\r
+\r
+        if(nItem % 2000 == 0) {\r
+          snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), nItem);\r
+          SetWindowText(hwndMain, buf); DoEvents();\r
+        }\r
 \r
+      if(!narrow || lg->position >= 0) {\r
         if( hasFilter ) {\r
-            if( ! SearchPattern( st, pszFilter ) ) {\r
-                skip = TRUE;\r
-            }\r
+            st = GameListLine(lg->number, &lg->gameInfo);\r
+           if( !SearchPattern( st, pszFilter) ) skip = TRUE;\r
+        }\r
+\r
+        if( !skip && byPos) {\r
+            if( (pos = GameContainsPosition(gameFile, lg)) < 0) skip = TRUE;\r
         }\r
 \r
         if( ! skip ) {\r
+            if(!st) st = GameListLine(lg->number, &lg->gameInfo);\r
             SendDlgItemMessage(hDlg, OPT_GameListText, LB_ADDSTRING, 0, (LPARAM) st);\r
             count++;\r
 \r
@@ -162,13 +118,18 @@ static int GameListToListBox( HWND hDlg, BOOL boReset, char * pszFilter, struct
                 stats->drawn++;\r
             else\r
                 stats->unfinished++;\r
+           if(!byPos) pos = 0;\r
         }\r
+      }\r
 \r
-        free(st);\r
+       lg->position = pos;\r
+\r
+        if(st) free(st);\r
         lg = (ListGame *) lg->node.succ;\r
     }\r
 \r
     SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, 0, 0);\r
+    SetWindowText(hwndMain, "WinBoard");\r
 \r
     return count;\r
 }\r
@@ -178,7 +139,7 @@ static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int
 {\r
     char buf[256];\r
 \r
-    sprintf( buf, "%s - %d/%d games", pszTitle, item_count, item_total );\r
+    snprintf( buf, sizeof(buf)/sizeof(buf[0]),_("%s - %d/%d games"), pszTitle, item_count, item_total );\r
 \r
     if( stats != 0 ) {\r
         sprintf( buf+strlen(buf), " (%d-%d-%d)", stats->white_wins, stats->black_wins, stats->drawn );\r
@@ -199,7 +160,7 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
   int nItem;\r
   RECT rect;\r
   static int sizeX, sizeY;\r
-  int newSizeX, newSizeY, flags;\r
+  int newSizeX, newSizeY;\r
   MINMAXINFO *mmi;\r
   static BOOL filterHasFocus = FALSE;\r
   int count;\r
@@ -207,7 +168,8 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
   static SnapData sd;\r
 \r
   switch (message) {\r
-  case WM_INITDIALOG: \r
+  case WM_INITDIALOG:\r
+    Translate(hDlg, DLG_GameList);\r
     GetWindowText( hDlg, szDlgTitle, sizeof(szDlgTitle) );\r
     szDlgTitle[ sizeof(szDlgTitle)-1 ] = '\0';\r
 \r
@@ -219,9 +181,9 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
     hwndText = GetDlgItem(hDlg, OPT_TagsText);\r
 \r
     /* Set font */\r
-    SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));\r
+    SendDlgItemMessage( hDlg, OPT_GameListText, WM_SETFONT, (WPARAM)font[boardSize][GAMELIST_FONT]->hf, MAKELPARAM(TRUE, 0 ));\r
 \r
-    count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats );\r
+    count = GameListToListBox( hDlg, gameListDialog ? TRUE : FALSE, NULL, &stats, FALSE, FALSE );\r
 \r
     SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );\r
     SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );\r
@@ -231,7 +193,6 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
     /* Size and position the dialog */\r
     if (!gameListDialog) {\r
       gameListDialog = hDlg;\r
-      flags = SWP_NOZORDER;\r
       GetClientRect(hDlg, &rect);\r
       sizeX = rect.right;\r
       sizeY = rect.bottom;\r
@@ -256,14 +217,14 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
                              newSizeX, newSizeY);\r
        sizeX = newSizeX;\r
        sizeY = newSizeY;\r
-      } \r
-   else \r
-     GetActualPlacement( gameListDialog, &wpGameList );\r
+      } else\r
+        GetActualPlacement( gameListDialog, &wpGameList );\r
 \r
-      GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );\r
     }\r
+      GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title\r
+    GameListHighlight(lastLoadGameNumber);\r
     return FALSE;\r
-    \r
+\r
   case WM_SIZE:\r
     newSizeX = LOWORD(lParam);\r
     newSizeY = HIWORD(lParam);\r
@@ -284,7 +245,7 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
 \r
   case WM_EXITSIZEMOVE:\r
     return OnExitSizeMove( &sd, hDlg, wParam, lParam );\r
-  \r
+\r
   case WM_GETMINMAXINFO:\r
     /* Prevent resizing window too small */\r
     mmi = (MINMAXINFO *) lParam;\r
@@ -293,7 +254,7 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
     break;\r
 \r
   case WM_COMMAND:\r
-      /* \r
+      /*\r
         [AS]\r
         If <Enter> is pressed while editing the filter, it's better to apply\r
         the filter rather than selecting the current game.\r
@@ -315,47 +276,51 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
       /* [AS] End command replacement */\r
 \r
     switch (LOWORD(wParam)) {\r
-    case IDOK:\r
     case OPT_GameListLoad:\r
+      LoadOptionsPopup(hDlg);\r
+      return TRUE;\r
+    case IDOK:\r
       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
       if (nItem < 0) {\r
        /* is this possible? */\r
-       DisplayError("No game selected", 0);\r
+       DisplayError(_("No game selected"), 0);\r
        return TRUE;\r
       }\r
       break; /* load the game*/\r
-      \r
+\r
     case OPT_GameListNext:\r
       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
       nItem++;\r
       if (nItem >= ((ListGame *) gameList.tailPred)->number) {\r
         /* [AS] Removed error message */\r
-       /* DisplayError("Can't go forward any further", 0); */\r
+       /* DisplayError(_("Can't go forward any further"), 0); */\r
        return TRUE;\r
       }\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
       break; /* load the game*/\r
-      \r
+\r
     case OPT_GameListPrev:\r
+#if 0\r
       nItem = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
       nItem--;\r
       if (nItem < 0) {\r
         /* [AS] Removed error message, added return */\r
-       /* DisplayError("Can't back up any further", 0); */\r
+       /* DisplayError(_("Can't back up any further"), 0); */\r
         return TRUE;\r
       }\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_SETCURSEL, nItem, 0);\r
       break; /* load the game*/\r
-\r
+#endif\r
     /* [AS] */\r
+    case OPT_GameListFind:\r
     case IDC_GameListDoFilter:\r
         {\r
             char filter[MAX_FILTER_LENGTH+1];\r
-            \r
+\r
             if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {\r
                 filter[ sizeof(filter)-1 ] = '\0';\r
-                count = GameListToListBox( hDlg, TRUE, filter, &stats );\r
-                GameListUpdateTitle( hDlg, szDlgTitle, count, ((ListGame *) gameList.tailPred)->number, &stats );\r
+                count = GameListToListBox( hDlg, TRUE, filter, &stats, LOWORD(wParam)!=IDC_GameListDoFilter, LOWORD(wParam)==OPT_GameListNarrow );\r
+                GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats );\r
             }\r
         }\r
         return FALSE;\r
@@ -365,13 +330,13 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
     case OPT_GameListClose:\r
       GameListPopDown();\r
       return TRUE;\r
-      \r
+\r
     case OPT_GameListText:\r
       switch (HIWORD(wParam)) {\r
       case LBN_DBLCLK:\r
        nItem = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);\r
        break; /* load the game*/\r
-       \r
+\r
       default:\r
        return FALSE;\r
       }\r
@@ -413,8 +378,8 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
             CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
         }\r
         else {\r
-           SetFocus(hwndMain); // [HGM] automatic focus switch\r
             LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
+           SetFocus(hwndMain); // [HGM] automatic focus switch\r
         }\r
     }\r
 \r
@@ -430,13 +395,14 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
 VOID GameListPopUp(FILE *fp, char *filename)\r
 {\r
   FARPROC lpProc;\r
-  \r
+\r
   gameFile = fp;\r
   if (gameFileName != filename) {\r
     if (gameFileName) free(gameFileName);\r
     gameFileName = StrSave(filename);\r
   }\r
   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_CHECKED);\r
+  EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_ENABLED);\r
   if (gameListDialog) {\r
     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
@@ -450,9 +416,15 @@ VOID GameListPopUp(FILE *fp, char *filename)
   gameListUp = TRUE;\r
 }\r
 \r
+FILE *GameFile()\r
+{\r
+  return gameFile;\r
+}\r
+\r
 VOID GameListPopDown(void)\r
 {\r
   CheckMenuItem(GetMenu(hwndMain), IDM_ShowGameList, MF_UNCHECKED);\r
+  EnableMenuItem(GetMenu(hwndMain), IDM_SaveSelected, MF_GRAYED);\r
   if (gameListDialog) ShowWindow(gameListDialog, SW_HIDE);\r
   gameListUp = FALSE;\r
 }\r
@@ -460,9 +432,23 @@ VOID GameListPopDown(void)
 \r
 VOID GameListHighlight(int index)\r
 {\r
+  char buf[MSG_SIZ];\r
+  int i, j, k, n, res = 0;\r
   if (gameListDialog == NULL) return;\r
-  SendDlgItemMessage(gameListDialog, OPT_GameListText, \r
-    LB_SETCURSEL, index - 1, 0);\r
+  for(i=64; ; i+=i) {\r
+        res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );\r
+        if(res == LB_ERR || index < atoi( buf )) break;\r
+  }\r
+  j = i/2;\r
+  while(i-j > 1) {\r
+        n = (i + j) >> 1;\r
+        res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, n, (LPARAM)buf );\r
+        if(res == LB_ERR || index < (k = atoi( buf ))) i = n; else {\r
+            j = n;\r
+            if(index == k) break;\r
+        }\r
+  }\r
+  SendDlgItemMessage(gameListDialog, OPT_GameListText, LB_SETCURSEL, j, 0);\r
 }\r
 \r
 \r
@@ -484,7 +470,7 @@ VOID ShowGameListProc()
     if (gameFileName) {\r
       GameListPopUp(gameFile, gameFileName);\r
     } else {\r
-      DisplayError("No game list", 0);\r
+      DisplayError(_("No game list"), 0);\r
     }\r
   }\r
 }\r
@@ -498,7 +484,7 @@ HGLOBAL ExportGameListAsText()
     DWORD dwLen = 0;\r
 \r
     if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {\r
-        DisplayError("Game list not loaded or empty", 0);\r
+        DisplayError(_(_("Game list not loaded or empty")), 0);\r
         return NULL;\r
     }\r
 \r