Fix multi-leg promotions
[xboard.git] / winboard / wgamelist.c
index 48aa85d..19917e6 100644 (file)
@@ -1,7 +1,8 @@
 /*\r
  * wgamelist.c -- Game list window for WinBoard\r
  *\r
- * Copyright 1995, 2009, 2010, 2011 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
@@ -48,23 +49,19 @@ static BOOLEAN gameListUp = FALSE;
 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] 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
         stats = &dummy;\r
@@ -85,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
@@ -108,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
@@ -145,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
@@ -166,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
@@ -178,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
@@ -208,6 +222,7 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
 \r
     }\r
       GameListUpdateTitle( hDlg, _("Game List"), count, ((ListGame *) gameList.tailPred)->number, &stats ); // [HGM] always update title\r
+    GameListHighlight(lastLoadGameNumber);\r
     return FALSE;\r
 \r
   case WM_SIZE:\r
@@ -261,8 +276,10 @@ 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
@@ -283,6 +300,7 @@ GameListDialog(HWND hDlg, UINT message,     WPARAM wParam, LPARAM lParam)
       break; /* load the game*/\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
@@ -292,15 +310,16 @@ GameListDialog(HWND hDlg, UINT message,   WPARAM wParam, LPARAM lParam)
       }\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
             if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {\r
                 filter[ sizeof(filter)-1 ] = '\0';\r
-                count = GameListToListBox( hDlg, TRUE, filter, &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
@@ -383,6 +402,7 @@ VOID GameListPopUp(FILE *fp, char *filename)
     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
@@ -396,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
@@ -406,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