changes from H.G. Muller; version 4.3.13
[xboard.git] / winboard / whistory.c
index 60af898..6b5b857 100644 (file)
-/*
- * Move history for WinBoard
- *
- * Author: Alessandro Scotti (Dec 2005)
- *
- * ------------------------------------------------------------------------
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- * ------------------------------------------------------------------------
- */
-#include "config.h"
-
-#include <windows.h> /* required for all Windows applications */
-#include <richedit.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <malloc.h>
-#include <commdlg.h>
-#include <dlgs.h>
-
-#include "common.h"
-#include "winboard.h"
-#include "frontend.h"
-#include "backend.h"
-
-#include "wsnap.h"
-
-VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo );
-VOID MoveHistoryPopUp();
-VOID MoveHistoryPopDown();
-BOOL MoveHistoryIsUp();
-
-/* Imports from backend.c */
-char * SavePart(char *str);
-
-/* Imports from winboard.c */
-extern HWND moveHistoryDialog;
-extern BOOLEAN moveHistoryDialogUp;
-
-extern HINSTANCE hInst;
-extern HWND hwndMain;
-
-extern WindowPlacement wpMoveHistory;
-
-extern BoardSize boardSize;
-
-/* Module globals */
-typedef char MoveHistoryString[ MOVE_LEN*2 ];
-
-static int lastFirst = 0;
-static int lastLast = 0;
-static int lastCurrent = -1;
-
-static char lastLastMove[ MOVE_LEN ];
-
-static MoveHistoryString * currMovelist;
-static ChessProgramStats_Move * currPvInfo;
-static int currFirst = 0;
-static int currLast = 0;
-static int currCurrent = -1;
-
-typedef struct {
-    int memoOffset;
-    int memoLength;
-} HistoryMove;
-
-static HistoryMove histMoves[ MAX_MOVES ];
-
-#define WM_REFRESH_HISTORY  (WM_USER+4657)
-
-#define DEFAULT_COLOR       0xFFFFFFFF
-
-#define H_MARGIN            2
-#define V_MARGIN            2
-
-/* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
-
-static VOID HighlightMove( int index, BOOL highlight )
-{
-    if( index >= 0 && index < MAX_MOVES ) {
-        CHARFORMAT cf;
-        HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
-
-        SendMessage( hMemo,
-            EM_SETSEL,
-            histMoves[index].memoOffset,
-            histMoves[index].memoOffset + histMoves[index].memoLength );
-
-
-        /* Set style */
-        ZeroMemory( &cf, sizeof(cf) );
-
-        cf.cbSize = sizeof(cf);
-        cf.dwMask = CFM_BOLD | CFM_COLOR;
-
-        if( highlight ) {
-            cf.dwEffects |= CFE_BOLD;
-            cf.crTextColor = RGB( 0x00, 0x00, 0xFF );
-        }
-        else {
-            cf.dwEffects |= CFE_AUTOCOLOR;
-        }
-
-        SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
-    }
-}
-
-static BOOL OnlyCurrentPositionChanged()
-{
-    BOOL result = FALSE;
-
-    if( lastFirst >= 0 &&
-        lastLast >= lastFirst &&
-        lastCurrent >= lastFirst &&
-        currFirst == lastFirst &&
-        currLast == lastLast &&
-        currCurrent >= 0 &&
-        TRUE )
-    {
-        result = TRUE;
-
-        /* Special case: last move changed */
-        if( currCurrent == currLast-1 ) {
-            if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
-                result = FALSE;
-            }
-        }
-    }
-
-    return result;
-}
-
-static BOOL OneMoveAppended()
-{
-    BOOL result = FALSE;
-
-    if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
-        currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
-        lastFirst == currFirst &&
-        lastLast == (currLast-1) &&
-        lastCurrent == (currCurrent-1) &&
-        currCurrent == (currLast-1) &&
-        TRUE )
-    {
-        result = TRUE;
-    }
-
-    return result;
-}
-
-static VOID ClearMemo()
-{
-    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETTEXT, 0, (LPARAM) "" );
-}
-
-static int AppendToMemo( char * text, DWORD flags, DWORD color )
-{
-    CHARFORMAT cf;
-
-    HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
-
-    /* Select end of text */
-    int cbTextLen = (int) SendMessage( hMemo, WM_GETTEXTLENGTH, 0, 0 );
-
-    SendMessage( hMemo, EM_SETSEL, cbTextLen, cbTextLen );
-
-    /* Set style */
-    ZeroMemory( &cf, sizeof(cf) );
-
-    cf.cbSize = sizeof(cf);
-    cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR | CFM_UNDERLINE;
-    cf.dwEffects = flags;
-
-    if( color != DEFAULT_COLOR ) {
-        cf.crTextColor = color;
-    }
-    else {
-        cf.dwEffects |= CFE_AUTOCOLOR;
-    }
-
-    SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
-
-    /* Append text */
-    SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
-
-    /* Return offset of appended text */
-    return cbTextLen;
-}
-
-static VOID AppendMoveToMemo( int index )
-{
-    char buf[64];
-    DWORD flags = 0;
-    DWORD color = DEFAULT_COLOR;
-
-    if( index < 0 || index >= MAX_MOVES ) {
-        return;
-    }
-
-    buf[0] = '\0';
-
-    /* Move number */
-    if( (index % 2) == 0 ) {
-        sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
-        AppendToMemo( buf, CFE_BOLD, DEFAULT_COLOR );
-    }
-
-    /* Move text */
-    strcpy( buf, SavePart( currMovelist[index] ) );
-    strcat( buf, " " );
-
-    histMoves[index].memoOffset = AppendToMemo( buf, flags, color );
-    histMoves[index].memoLength = strlen(buf)-1;
-
-    /* PV info (if any) */
-    if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
-        sprintf( buf, "{%s%.2f/%d} ",
-            currPvInfo[index].score >= 0 ? "+" : "",
-            currPvInfo[index].score / 100.0,
-            currPvInfo[index].depth );
-
-        AppendToMemo( buf, flags,
-            color == DEFAULT_COLOR ? GetSysColor(COLOR_GRAYTEXT) : color );
-    }
-}
-
-static void RefreshMemoContent()
-{
-    int i;
-
-    ClearMemo();
-
-    for( i=currFirst; i<currLast; i++ ) {
-        AppendMoveToMemo( i );
-    }
-}
-
-static void MemoContentUpdated()
-{
-    int caretPos;
-
-    HighlightMove( lastCurrent, FALSE );
-    HighlightMove( currCurrent, TRUE );
-
-    lastFirst = currFirst;
-    lastLast = currLast;
-    lastCurrent = currCurrent;
-    lastLastMove[0] = '\0';
-
-    if( lastLast > 0 ) {
-        strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );
-    }
-
-    /* Deselect any text, move caret to end of memo */
-    if( currCurrent >= 0 ) {
-        caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
-    }
-    else {
-        caretPos = (int) SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_GETTEXTLENGTH, 0, 0 );
-    }
-
-    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETSEL, caretPos, caretPos );
-
-    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SCROLLCARET, 0, 0 );
-}
-
-int FindMoveByCharIndex( int char_index )
-{
-    int index;
-
-    for( index=currFirst; index<currLast; index++ ) {
-        if( char_index >= histMoves[index].memoOffset &&
-            char_index <  (histMoves[index].memoOffset + histMoves[index].memoLength) )
-        {
-            return index;
-        }
-    }
-
-    return -1;
-}
-
-LRESULT CALLBACK HistoryDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
-{
-    static SnapData sd;
-
-    switch (message) {
-    case WM_INITDIALOG:
-        if( moveHistoryDialog == NULL ) {
-            moveHistoryDialog = hDlg;
-
-            /* Enable word wrapping and notifications */
-            SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETTARGETDEVICE, 0, 0 );
-
-            SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS );
-
-            /* Set font */
-           SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
-
-            /* Restore window placement */
-            RestoreWindowPlacement( hDlg, &wpMoveHistory );
-        }
-
-        /* Update memo */
-        RefreshMemoContent();
-
-        MemoContentUpdated();
-
-        return FALSE;
-
-    case WM_COMMAND:
-        switch (LOWORD(wParam)) {
-        case IDOK:
-          EndDialog(hDlg, TRUE);
-          return TRUE;
-
-        case IDCANCEL:
-          EndDialog(hDlg, FALSE);
-          return TRUE;
-
-        default:
-          break;
-        }
-
-        break;
-
-    case WM_NOTIFY:
-        if( wParam == IDC_MoveHistory ) {
-            MSGFILTER * lpMF = (MSGFILTER *) lParam;
-
-            if( lpMF->msg == WM_LBUTTONDBLCLK && (lpMF->wParam & (MK_CONTROL | MK_SHIFT)) == 0 ) {
-                POINTL pt;
-                LRESULT index;
-
-                pt.x = LOWORD( lpMF->lParam );
-                pt.y = HIWORD( lpMF->lParam );
-
-                index = SendDlgItemMessage( hDlg, IDC_MoveHistory, EM_CHARFROMPOS, 0, (LPARAM) &pt );
-
-                index = FindMoveByCharIndex( index );
-
-                if( index >= 0 ) {
-                    ToNrEvent( index + 1 );
-                }
-
-                /* Zap the message for good: apparently, returning non-zero is not enough */
-                lpMF->msg = WM_USER;
-
-                return TRUE;
-            }
-        }
-        break;
-
-    case WM_REFRESH_HISTORY:
-        /* Update the GUI */
-        if( OnlyCurrentPositionChanged() ) {
-            /* Only "cursor" changed, no need to update memo content */
-        }
-        else if( OneMoveAppended() ) {
-            AppendMoveToMemo( currCurrent );
-        }
-        else {
-            RefreshMemoContent();
-        }
-
-        MemoContentUpdated();
-
-        break;
-
-    case WM_SIZE:
-        SetWindowPos( GetDlgItem( moveHistoryDialog, IDC_MoveHistory ),
-            HWND_TOP,
-            H_MARGIN, V_MARGIN,
-            LOWORD(lParam) - 2*H_MARGIN,
-            HIWORD(lParam) - 2*V_MARGIN,
-            SWP_NOZORDER );
-        break;
-
-    case WM_GETMINMAXINFO:
-        {
-            MINMAXINFO * mmi = (MINMAXINFO *) lParam;
-
-            mmi->ptMinTrackSize.x = 100;
-            mmi->ptMinTrackSize.y = 100;
-        }
-        break;
-
-    case WM_CLOSE:
-        MoveHistoryPopDown();
-        break;
-
-    case WM_ENTERSIZEMOVE:
-        return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
-
-    case WM_SIZING:
-        return OnSizing( &sd, hDlg, wParam, lParam );
-
-    case WM_MOVING:
-        return OnMoving( &sd, hDlg, wParam, lParam );
-
-    case WM_EXITSIZEMOVE:
-        return OnExitSizeMove( &sd, hDlg, wParam, lParam );
-    }
-
-    return FALSE;
-}
-
-VOID MoveHistoryPopUp()
-{
-  FARPROC lpProc;
-
-  CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_CHECKED);
-
-  if( moveHistoryDialog ) {
-    SendMessage( moveHistoryDialog, WM_INITDIALOG, 0, 0 );
-
-    if( ! moveHistoryDialogUp ) {
-        ShowWindow(moveHistoryDialog, SW_SHOW);
-    }
-  }
-  else {
-    lpProc = MakeProcInstance( (FARPROC) HistoryDialogProc, hInst );
-
-    /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
-    CreateDialog( hInst, MAKEINTRESOURCE(DLG_MoveHistory), hwndMain, (DLGPROC)lpProc );
-
-    FreeProcInstance(lpProc);
-  }
-
-  moveHistoryDialogUp = TRUE;
-}
-
-VOID MoveHistoryPopDown()
-{
-  CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_UNCHECKED);
-
-  if( moveHistoryDialog ) {
-      ShowWindow(moveHistoryDialog, SW_HIDE);
-  }
-
-  moveHistoryDialogUp = FALSE;
-}
-
-VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
-{
-    /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
-
-    currMovelist = movelist;
-    currFirst = first;
-    currLast = last;
-    currCurrent = current;
-    currPvInfo = pvInfo;
-
-    if( moveHistoryDialog ) {
-        SendMessage( moveHistoryDialog, WM_REFRESH_HISTORY, 0, 0 );
-    }
-}
-
-BOOL MoveHistoryIsUp()
-{
-    return moveHistoryDialogUp;
-}
+/*\r
+ * Move history for WinBoard\r
+ *\r
+ * Author: Alessandro Scotti (Dec 2005)\r
+ *\r
+ * ------------------------------------------------------------------------\r
+ * This program 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
+ *\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
+ *\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
+#include "config.h"\r
+\r
+#include <windows.h> /* required for all Windows applications */\r
+#include <richedit.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <malloc.h>\r
+#include <commdlg.h>\r
+#include <dlgs.h>\r
+\r
+#include "common.h"\r
+#include "winboard.h"\r
+#include "frontend.h"\r
+#include "backend.h"\r
+\r
+#include "wsnap.h"\r
+\r
+VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo );\r
+VOID MoveHistoryPopUp();\r
+VOID MoveHistoryPopDown();\r
+BOOL MoveHistoryIsUp();\r
+\r
+/* Imports from backend.c */\r
+char * SavePart(char *str);\r
+\r
+/* Imports from winboard.c */\r
+extern HWND moveHistoryDialog;\r
+extern BOOLEAN moveHistoryDialogUp;\r
+\r
+extern HINSTANCE hInst;\r
+extern HWND hwndMain;\r
+\r
+extern WindowPlacement wpMoveHistory;\r
+\r
+extern BoardSize boardSize;\r
+\r
+/* Module globals */\r
+typedef char MoveHistoryString[ MOVE_LEN*2 ];\r
+\r
+static int lastFirst = 0;\r
+static int lastLast = 0;\r
+static int lastCurrent = -1;\r
+\r
+static char lastLastMove[ MOVE_LEN ];\r
+\r
+static MoveHistoryString * currMovelist;\r
+static ChessProgramStats_Move * currPvInfo;\r
+static int currFirst = 0;\r
+static int currLast = 0;\r
+static int currCurrent = -1;\r
+\r
+typedef struct {\r
+    int memoOffset;\r
+    int memoLength;\r
+} HistoryMove;\r
+\r
+static HistoryMove histMoves[ MAX_MOVES ];\r
+\r
+#define WM_REFRESH_HISTORY  (WM_USER+4657)\r
+\r
+#define DEFAULT_COLOR       0xFFFFFFFF\r
+\r
+#define H_MARGIN            2\r
+#define V_MARGIN            2\r
+\r
+/* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */\r
+\r
+static VOID HighlightMove( int index, BOOL highlight )\r
+{\r
+    if( index >= 0 && index < MAX_MOVES ) {\r
+        CHARFORMAT cf;\r
+        HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );\r
+\r
+        SendMessage( hMemo, \r
+            EM_SETSEL, \r
+            histMoves[index].memoOffset, \r
+            histMoves[index].memoOffset + histMoves[index].memoLength );\r
+\r
+\r
+        /* Set style */\r
+        ZeroMemory( &cf, sizeof(cf) );\r
+\r
+        cf.cbSize = sizeof(cf);\r
+        cf.dwMask = CFM_BOLD | CFM_COLOR;\r
+\r
+        if( highlight ) {\r
+            cf.dwEffects |= CFE_BOLD;\r
+            cf.crTextColor = RGB( 0x00, 0x00, 0xFF );\r
+        }\r
+        else {\r
+            cf.dwEffects |= CFE_AUTOCOLOR;\r
+        }\r
+\r
+        SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );\r
+    }\r
+}\r
+\r
+static BOOL OnlyCurrentPositionChanged()\r
+{\r
+    BOOL result = FALSE;\r
+\r
+    if( lastFirst >= 0 &&\r
+        lastLast >= lastFirst &&\r
+        lastCurrent >= lastFirst && \r
+        currFirst == lastFirst &&\r
+        currLast == lastLast &&\r
+        currCurrent >= 0 &&\r
+        TRUE )\r
+    {\r
+        result = TRUE;\r
+\r
+        /* Special case: last move changed */\r
+        if( currCurrent == currLast-1 ) {\r
+            if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {\r
+                result = FALSE;\r
+            }\r
+        }\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+static BOOL OneMoveAppended()\r
+{\r
+    BOOL result = FALSE;\r
+\r
+    if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&\r
+        currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&\r
+        lastFirst == currFirst &&\r
+        lastLast == (currLast-1) &&\r
+        lastCurrent == (currCurrent-1) &&\r
+        currCurrent == (currLast-1) &&\r
+        TRUE )\r
+    {\r
+        result = TRUE;\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+static VOID ClearMemo()\r
+{\r
+    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETTEXT, 0, (LPARAM) "" );\r
+}\r
+\r
+static int AppendToMemo( char * text, DWORD flags, DWORD color )\r
+{\r
+    CHARFORMAT cf;\r
+\r
+    HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );\r
+\r
+    /* Select end of text */\r
+    int cbTextLen = (int) SendMessage( hMemo, WM_GETTEXTLENGTH, 0, 0 );\r
+\r
+    SendMessage( hMemo, EM_SETSEL, cbTextLen, cbTextLen );\r
+\r
+    /* Set style */\r
+    ZeroMemory( &cf, sizeof(cf) );\r
+\r
+    cf.cbSize = sizeof(cf);\r
+    cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR | CFM_UNDERLINE;\r
+    cf.dwEffects = flags;\r
+\r
+    if( color != DEFAULT_COLOR ) {\r
+        cf.crTextColor = color;\r
+    }\r
+    else {\r
+        cf.dwEffects |= CFE_AUTOCOLOR;\r
+    }\r
+\r
+    SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );\r
+\r
+    /* Append text */\r
+    SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );\r
+\r
+    /* Return offset of appended text */\r
+    return cbTextLen;\r
+}\r
+\r
+static VOID AppendMoveToMemo( int index )\r
+{\r
+    char buf[64];\r
+    DWORD flags = 0;\r
+    DWORD color = DEFAULT_COLOR;\r
+\r
+    if( index < 0 || index >= MAX_MOVES ) {\r
+        return;\r
+    }\r
+\r
+    buf[0] = '\0';\r
+\r
+    /* Move number */\r
+    if( (index % 2) == 0 ) {\r
+        sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );\r
+        AppendToMemo( buf, CFE_BOLD, DEFAULT_COLOR );\r
+    }\r
+\r
+    /* Move text */\r
+    strcpy( buf, SavePart( currMovelist[index] ) );\r
+    strcat( buf, " " );\r
+\r
+    histMoves[index].memoOffset = AppendToMemo( buf, flags, color );\r
+    histMoves[index].memoLength = strlen(buf)-1;\r
+\r
+    /* PV info (if any) */\r
+    if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {\r
+        sprintf( buf, "{%s%.2f/%d} ", \r
+            currPvInfo[index].score >= 0 ? "+" : "",\r
+            currPvInfo[index].score / 100.0,\r
+            currPvInfo[index].depth );\r
+\r
+        AppendToMemo( buf, flags, \r
+            color == DEFAULT_COLOR ? GetSysColor(COLOR_GRAYTEXT) : color );\r
+    }\r
+}\r
+\r
+static void RefreshMemoContent()\r
+{\r
+    int i;\r
+\r
+    ClearMemo();\r
+\r
+    for( i=currFirst; i<currLast; i++ ) {\r
+        AppendMoveToMemo( i );\r
+    }\r
+}\r
+\r
+static void MemoContentUpdated()\r
+{\r
+    int caretPos;\r
+\r
+    HighlightMove( lastCurrent, FALSE );\r
+    HighlightMove( currCurrent, TRUE );\r
+\r
+    lastFirst = currFirst;\r
+    lastLast = currLast;\r
+    lastCurrent = currCurrent;\r
+    lastLastMove[0] = '\0';\r
+\r
+    if( lastLast > 0 ) {\r
+        strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );\r
+    }\r
+\r
+    /* Deselect any text, move caret to end of memo */\r
+    if( currCurrent >= 0 ) {\r
+        caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;\r
+    }\r
+    else {\r
+        caretPos = (int) SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_GETTEXTLENGTH, 0, 0 );\r
+    }\r
+\r
+    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETSEL, caretPos, caretPos );\r
+\r
+    SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SCROLLCARET, 0, 0 );\r
+}\r
+\r
+int FindMoveByCharIndex( int char_index )\r
+{\r
+    int index;\r
+\r
+    for( index=currFirst; index<currLast; index++ ) {\r
+        if( char_index >= histMoves[index].memoOffset &&\r
+            char_index <  (histMoves[index].memoOffset + histMoves[index].memoLength) )\r
+        {\r
+            return index;\r
+        }\r
+    }\r
+\r
+    return -1;\r
+}\r
+\r
+LRESULT CALLBACK HistoryDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )\r
+{\r
+    static SnapData sd;\r
+\r
+    switch (message) {\r
+    case WM_INITDIALOG:\r
+        if( moveHistoryDialog == NULL ) {\r
+            moveHistoryDialog = hDlg;\r
+\r
+            /* Enable word wrapping and notifications */\r
+            SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETTARGETDEVICE, 0, 0 );\r
+\r
+            SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS );\r
+\r
+            /* Set font */\r
+           SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));\r
+\r
+            /* Restore window placement */\r
+            RestoreWindowPlacement( hDlg, &wpMoveHistory );\r
+        }\r
+\r
+        /* Update memo */\r
+        RefreshMemoContent();\r
+\r
+        MemoContentUpdated();\r
+\r
+        return FALSE;\r
+\r
+    case WM_COMMAND:\r
+        switch (LOWORD(wParam)) {\r
+        case IDOK:\r
+          EndDialog(hDlg, TRUE);\r
+          return TRUE;\r
+\r
+        case IDCANCEL:\r
+          EndDialog(hDlg, FALSE);\r
+          return TRUE;\r
+\r
+        default:\r
+          break;\r
+        }\r
+\r
+        break;\r
+\r
+    case WM_NOTIFY:\r
+        if( wParam == IDC_MoveHistory ) {\r
+            MSGFILTER * lpMF = (MSGFILTER *) lParam;\r
+\r
+            if( lpMF->msg == WM_LBUTTONDBLCLK && (lpMF->wParam & (MK_CONTROL | MK_SHIFT)) == 0 ) {\r
+                POINTL pt;\r
+                LRESULT index;\r
+\r
+                pt.x = LOWORD( lpMF->lParam );\r
+                pt.y = HIWORD( lpMF->lParam );\r
+\r
+                index = SendDlgItemMessage( hDlg, IDC_MoveHistory, EM_CHARFROMPOS, 0, (LPARAM) &pt );\r
+\r
+                index = FindMoveByCharIndex( index );\r
+\r
+                if( index >= 0 ) {\r
+                    ToNrEvent( index + 1 );\r
+                }\r
+\r
+                /* Zap the message for good: apparently, returning non-zero is not enough */\r
+                lpMF->msg = WM_USER;\r
+\r
+                return TRUE;\r
+            }\r
+        }\r
+        break;\r
+\r
+    case WM_REFRESH_HISTORY:\r
+        /* Update the GUI */\r
+        if( OnlyCurrentPositionChanged() ) {\r
+            /* Only "cursor" changed, no need to update memo content */\r
+        }\r
+        else if( OneMoveAppended() ) {\r
+            AppendMoveToMemo( currCurrent );\r
+        }\r
+        else {\r
+            RefreshMemoContent();\r
+        }\r
+\r
+        MemoContentUpdated();\r
+\r
+        break;\r
+\r
+    case WM_SIZE:\r
+        SetWindowPos( GetDlgItem( moveHistoryDialog, IDC_MoveHistory ),\r
+            HWND_TOP,\r
+            H_MARGIN, V_MARGIN,\r
+            LOWORD(lParam) - 2*H_MARGIN,\r
+            HIWORD(lParam) - 2*V_MARGIN,\r
+            SWP_NOZORDER );\r
+        break;\r
+\r
+    case WM_GETMINMAXINFO:\r
+        {\r
+            MINMAXINFO * mmi = (MINMAXINFO *) lParam;\r
+        \r
+            mmi->ptMinTrackSize.x = 100;\r
+            mmi->ptMinTrackSize.y = 100;\r
+        }\r
+        break;\r
+\r
+    case WM_CLOSE:\r
+        MoveHistoryPopDown();\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
+\r
+    return FALSE;\r
+}\r
+\r
+VOID MoveHistoryPopUp()\r
+{\r
+  FARPROC lpProc;\r
+  \r
+  CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_CHECKED);\r
+\r
+  if( moveHistoryDialog ) {\r
+    SendMessage( moveHistoryDialog, WM_INITDIALOG, 0, 0 );\r
+\r
+    if( ! moveHistoryDialogUp ) {\r
+        ShowWindow(moveHistoryDialog, SW_SHOW);\r
+    }\r
+  }\r
+  else {\r
+    lpProc = MakeProcInstance( (FARPROC) HistoryDialogProc, hInst );\r
+\r
+    /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */\r
+    CreateDialog( hInst, MAKEINTRESOURCE(DLG_MoveHistory), hwndMain, (DLGPROC)lpProc );\r
+\r
+    FreeProcInstance(lpProc);\r
+  }\r
+\r
+  moveHistoryDialogUp = TRUE;\r
+}\r
+\r
+VOID MoveHistoryPopDown()\r
+{\r
+  CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_UNCHECKED);\r
+\r
+  if( moveHistoryDialog ) {\r
+      ShowWindow(moveHistoryDialog, SW_HIDE);\r
+  }\r
+\r
+  moveHistoryDialogUp = FALSE;\r
+}\r
+\r
+VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )\r
+{\r
+    /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */\r
+\r
+    currMovelist = movelist;\r
+    currFirst = first;\r
+    currLast = last;\r
+    currCurrent = current;\r
+    currPvInfo = pvInfo;\r
+\r
+    if( moveHistoryDialog ) {\r
+        SendMessage( moveHistoryDialog, WM_REFRESH_HISTORY, 0, 0 );\r
+    }\r
+}\r
+\r
+BOOL MoveHistoryIsUp()\r
+{\r
+    return moveHistoryDialogUp;\r
+}\r