Updated copyright notice to 2013
[xboard.git] / winboard / wgamelist.c
index 90528aa..2512bc8 100644 (file)
@@ -1,25 +1,27 @@
 /*\r
  * wgamelist.c -- Game list window for WinBoard\r
- * $Id$\r
  *\r
- * Copyright 1995 Free Software Foundation, Inc.\r
+ * Copyright 1995, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.\r
+ *\r
+ * Enhancements Copyright 2005 Alessandro Scotti\r
  *\r
  * ------------------------------------------------------------------------\r
- * This program is free software; you can redistribute it and/or modify\r
+ *\r
+ * GNU XBoard is free software: you can redistribute it and/or modify\r
  * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
+ * the Free Software Foundation, either version 3 of the License, or (at\r
+ * your option) any later version.\r
  *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
+ * GNU XBoard is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details.\r
  *\r
  * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
- * ------------------------------------------------------------------------\r
- */\r
+ * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
+ *\r
+ *------------------------------------------------------------------------\r
+ ** See the file ChangeLog for a revision history.  */\r
 \r
 #include "config.h"\r
 \r
 #include <dlgs.h>\r
 \r
 #include "common.h"\r
-#include "winboard.h"\r
 #include "frontend.h"\r
 #include "backend.h"\r
+#include "winboard.h"\r
+\r
+#include "wsnap.h"\r
+\r
+#define _(s) T_(s)\r
 \r
 /* Module globals */\r
-HWND gameListDialog = NULL;\r
-BOOLEAN gameListUp = FALSE;\r
-FILE* gameFile;\r
-char* gameFileName = NULL;\r
-int gameListX, gameListY, gameListW, gameListH;\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] Setup the game list according to the specified filter */\r
+static 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
+    /* Initialize stats (use a dummy variable if caller not interested in them) */\r
+    if( stats == NULL ) {\r
+        stats = &dummy;\r
+    }\r
+\r
+    stats->white_wins = 0;\r
+    stats->black_wins = 0;\r
+    stats->drawn = 0;\r
+    stats->unfinished = 0;\r
+\r
+    if( boReset ) {\r
+        SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
+    }\r
+\r
+    if( pszFilter != NULL ) {\r
+        if( strlen( pszFilter ) > 0 ) {\r
+            hasFilter = TRUE;\r
+        }\r
+    }\r
+\r
+    if(byPos) InitSearch();\r
+\r
+    for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\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);\r
+        }\r
+\r
+      if(!narrow || lg->position >= 0) {\r
+        if( hasFilter ) {\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
+            /* Update stats */\r
+            if( lg->gameInfo.result == WhiteWins )\r
+                stats->white_wins++;\r
+            else if( lg->gameInfo.result == BlackWins )\r
+                stats->black_wins++;\r
+            else if( lg->gameInfo.result == GameIsDrawn )\r
+                stats->drawn++;\r
+            else\r
+                stats->unfinished++;\r
+           if(!byPos) pos = 0;\r
+        }\r
+      }\r
 \r
-/* Imports from winboard.c */\r
-extern HINSTANCE hInst;\r
-extern HWND hwndMain;\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
+\r
+/* [AS] Show number of visible (filtered) games and total on window caption */\r
+static int GameListUpdateTitle( HWND hDlg, char * pszTitle, int item_count, int item_total, struct GameListStats * stats )\r
+{\r
+    char buf[256];\r
+\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
+    }\r
+\r
+    SetWindowText( hDlg, buf );\r
+\r
+    return 0;\r
+}\r
+\r
+#define MAX_FILTER_LENGTH   128\r
 \r
 LRESULT CALLBACK\r
 GameListDialog(HWND hDlg, UINT message,        WPARAM wParam, LPARAM lParam)\r
 {\r
+  static char szDlgTitle[64];\r
   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;\r
+  int count;\r
+  struct GameListStats stats;\r
+  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
     if (gameListDialog) {\r
       SendDlgItemMessage(hDlg, OPT_GameListText, LB_RESETCONTENT, 0, 0);\r
     }\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
+\r
+    /* Set font */\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, FALSE, FALSE );\r
+\r
+    SendDlgItemMessage( hDlg, IDC_GameListFilter, WM_SETTEXT, 0, (LPARAM) "" );\r
+    SendDlgItemMessage( hDlg, IDC_GameListFilter, EM_SETLIMITTEXT, MAX_FILTER_LENGTH, 0 );\r
+\r
+    filterHasFocus = FALSE;\r
+\r
     /* Size and position the dialog */\r
     if (!gameListDialog) {\r
       gameListDialog = hDlg;\r
@@ -82,18 +201,18 @@ GameListDialog(HWND hDlg, UINT message,    WPARAM wParam, LPARAM lParam)
       GetClientRect(hDlg, &rect);\r
       sizeX = rect.right;\r
       sizeY = rect.bottom;\r
-      if (gameListX != CW_USEDEFAULT && gameListY != CW_USEDEFAULT &&\r
-         gameListW != CW_USEDEFAULT && gameListH != CW_USEDEFAULT) {\r
+      if (wpGameList.x != CW_USEDEFAULT && wpGameList.y != CW_USEDEFAULT &&\r
+         wpGameList.width != CW_USEDEFAULT && wpGameList.height != CW_USEDEFAULT) {\r
        WINDOWPLACEMENT wp;\r
-       EnsureOnScreen(&gameListX, &gameListY);\r
+       EnsureOnScreen(&wpGameList.x, &wpGameList.y, 0, 0);\r
        wp.length = sizeof(WINDOWPLACEMENT);\r
        wp.flags = 0;\r
        wp.showCmd = SW_SHOW;\r
        wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;\r
-       wp.rcNormalPosition.left = gameListX;\r
-       wp.rcNormalPosition.right = gameListX + gameListW;\r
-       wp.rcNormalPosition.top = gameListY;\r
-       wp.rcNormalPosition.bottom = gameListY + gameListH;\r
+       wp.rcNormalPosition.left = wpGameList.x;\r
+       wp.rcNormalPosition.right = wpGameList.x + wpGameList.width;\r
+       wp.rcNormalPosition.top = wpGameList.y;\r
+       wp.rcNormalPosition.bottom = wpGameList.y + wpGameList.height;\r
        SetWindowPlacement(hDlg, &wp);\r
 \r
        GetClientRect(hDlg, &rect);\r
@@ -103,10 +222,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
+\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
@@ -116,6 +239,18 @@ GameListDialog(HWND hDlg, UINT message,    WPARAM wParam, LPARAM lParam)
     sizeY = newSizeY;\r
     break;\r
 \r
+  case WM_ENTERSIZEMOVE:\r
+    return OnEnterSizeMove( &sd, hDlg, wParam, lParam );\r
+\r
+  case WM_SIZING:\r
+    return OnSizing( &sd, hDlg, wParam, lParam );\r
+\r
+  case WM_MOVING:\r
+    return OnMoving( &sd, hDlg, wParam, lParam );\r
+\r
+  case WM_EXITSIZEMOVE:\r
+    return OnExitSizeMove( &sd, hDlg, wParam, lParam );\r
+\r
   case WM_GETMINMAXINFO:\r
     /* Prevent resizing window too small */\r
     mmi = (MINMAXINFO *) lParam;\r
@@ -124,61 +259,135 @@ GameListDialog(HWND hDlg, UINT message,  WPARAM wParam, LPARAM lParam)
     break;\r
 \r
   case WM_COMMAND:\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
+      */\r
+      if( LOWORD(wParam) == IDC_GameListFilter ) {\r
+          switch( HIWORD(wParam) ) {\r
+          case EN_SETFOCUS:\r
+              filterHasFocus = TRUE;\r
+              break;\r
+          case EN_KILLFOCUS:\r
+              filterHasFocus = FALSE;\r
+              break;\r
+          }\r
+      }\r
+\r
+      if( filterHasFocus && (LOWORD(wParam) == IDOK) ) {\r
+          wParam = IDC_GameListDoFilter;\r
+      }\r
+      /* [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
-       DisplayError("Can't go forward any further", 0);\r
+        /* [AS] Removed error message */\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
-       DisplayError("Can't back up any further", 0);\r
+        /* [AS] Removed error message, added return */\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
+            if( GetDlgItemText( hDlg, IDC_GameListFilter, filter, sizeof(filter) ) >= 0 ) {\r
+                filter[ sizeof(filter)-1 ] = '\0';\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
+        break;\r
+\r
     case IDCANCEL:\r
     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
       break;\r
-      \r
+\r
     default:\r
       return FALSE;\r
     }\r
+\r
     /* Load the game */\r
-    if (cmailMsgLoaded) {\r
-      CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
-    } else {\r
-      LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
+    {\r
+        /* [AS] Get index from the item itself, because filtering makes original order unuseable. */\r
+        int index = SendDlgItemMessage(hDlg, OPT_GameListText, LB_GETCURSEL, 0, 0);\r
+        char * text;\r
+        LRESULT res;\r
+\r
+        if( index < 0 ) {\r
+            return TRUE;\r
+        }\r
+\r
+        res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXTLEN, index, 0 );\r
+\r
+        if( res == LB_ERR ) {\r
+            return TRUE;\r
+        }\r
+\r
+        text = (char *) malloc( res+1 );\r
+\r
+        res = SendDlgItemMessage( hDlg, OPT_GameListText, LB_GETTEXT, index, (LPARAM)text );\r
+\r
+        index = atoi( text );\r
+\r
+        nItem = index - 1;\r
+\r
+        free( text );\r
+        /* [AS] End: nItem has been "patched" now! */\r
+\r
+        if (cmailMsgLoaded) {\r
+            CmailLoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
+        }\r
+        else {\r
+            LoadGame(gameFile, nItem + 1, gameFileName, TRUE);\r
+           SetFocus(hwndMain); // [HGM] automatic focus switch\r
+        }\r
     }\r
+\r
     return TRUE;\r
 \r
   default:\r
@@ -191,7 +400,7 @@ 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
@@ -201,6 +410,7 @@ VOID GameListPopUp(FILE *fp, char *filename)
   if (gameListDialog) {\r
     SendMessage(gameListDialog, WM_INITDIALOG, 0, 0);\r
     if (!gameListUp) ShowWindow(gameListDialog, SW_SHOW);\r
+    else SetFocus(gameListDialog);\r
   } else {\r
     lpProc = MakeProcInstance((FARPROC)GameListDialog, hInst);\r
     CreateDialog(hInst, MAKEINTRESOURCE(DLG_GameList),\r
@@ -210,6 +420,11 @@ 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
@@ -220,9 +435,15 @@ VOID GameListPopDown(void)
 \r
 VOID GameListHighlight(int index)\r
 {\r
+  char buf[MSG_SIZ];\r
+  int i, res = 0;\r
   if (gameListDialog == NULL) return;\r
-  SendDlgItemMessage(gameListDialog, OPT_GameListText, \r
-    LB_SETCURSEL, index - 1, 0);\r
+  for(i=0; res != LB_ERR; i++) {\r
+        res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf );\r
+        if(index <= atoi( buf )) break;\r
+  }\r
+  SendDlgItemMessage(gameListDialog, OPT_GameListText,\r
+    LB_SETCURSEL, i, 0);\r
 }\r
 \r
 \r
@@ -238,12 +459,66 @@ VOID GameListDestroy()
 VOID ShowGameListProc()\r
 {\r
   if (gameListUp) {\r
-    GameListPopDown();\r
+    if(gameListDialog) SetFocus(gameListDialog);\r
+//    GameListPopDown();\r
   } else {\r
     if (gameFileName) {\r
       GameListPopUp(gameFile, gameFileName);\r
     } else {\r
-      DisplayError("No game list", 0);\r
+      DisplayError(_("No game list"), 0);\r
     }\r
   }\r
 }\r
+\r
+HGLOBAL ExportGameListAsText()\r
+{\r
+    HGLOBAL result = NULL;\r
+    LPVOID lpMem = NULL;\r
+    ListGame * lg = (ListGame *) gameList.head;\r
+    int nItem;\r
+    DWORD dwLen = 0;\r
+\r
+    if( ! gameFileName || ((ListGame *) gameList.tailPred)->number <= 0 ) {\r
+        DisplayError(_(_("Game list not loaded or empty")), 0);\r
+        return NULL;\r
+    }\r
+\r
+    /* Get list size */\r
+    for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
+        char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
+\r
+        dwLen += strlen(st) + 2; /* Add extra characters for "\r\n" */\r
+\r
+        free(st);\r
+        lg = (ListGame *) lg->node.succ;\r
+    }\r
+\r
+    /* Allocate memory for the list */\r
+    result = GlobalAlloc(GHND, dwLen+1 );\r
+\r
+    if( result != NULL ) {\r
+        lpMem = GlobalLock(result);\r
+    }\r
+\r
+    /* Copy the list into the global memory block */\r
+    if( lpMem != NULL ) {\r
+        char * dst = (char *) lpMem;\r
+        size_t len;\r
+\r
+        lg = (ListGame *) gameList.head;\r
+\r
+        for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){\r
+            char * st = GameListLineFull(lg->number, &lg->gameInfo);\r
+\r
+            len = sprintf( dst, "%s\r\n", st );\r
+            dst += len;\r
+\r
+            free(st);\r
+            lg = (ListGame *) lg->node.succ;\r
+        }\r
+\r
+        GlobalUnlock( result );\r
+    }\r
+\r
+    return result;\r
+}\r