2 * Move history for WinBoard
\r
4 * Author: Alessandro Scotti (Dec 2005)
\r
6 * ------------------------------------------------------------------------
\r
8 * GNU XBoard is free software: you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation, either version 3 of the License, or (at
\r
11 * your option) any later version.
\r
13 * GNU XBoard is distributed in the hope that it will be useful, but
\r
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
16 * General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program. If not, see http://www.gnu.org/licenses/.
\r
21 * ------------------------------------------------------------------------
\r
22 ** See the file ChangeLog for a revision history. */
\r
26 #include <windows.h> /* required for all Windows applications */
\r
27 #include <richedit.h>
\r
31 #include <commdlg.h>
\r
35 #include "winboard.h"
\r
36 #include "frontend.h"
\r
37 #include "backend.h"
\r
41 VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo );
\r
42 VOID MoveHistoryPopUp();
\r
43 VOID MoveHistoryPopDown();
\r
44 BOOL MoveHistoryIsUp();
\r
46 /* Imports from backend.c */
\r
47 char * SavePart(char *str);
\r
49 /* Imports from winboard.c */
\r
50 extern HWND moveHistoryDialog;
\r
51 extern BOOLEAN moveHistoryDialogUp;
\r
53 extern HINSTANCE hInst;
\r
54 extern HWND hwndMain;
\r
56 extern WindowPlacement wpMoveHistory;
\r
58 extern BoardSize boardSize;
\r
60 /* Module globals */
\r
61 typedef char MoveHistoryString[ MOVE_LEN*2 ];
\r
63 static int lastFirst = 0;
\r
64 static int lastLast = 0;
\r
65 static int lastCurrent = -1;
\r
67 static char lastLastMove[ MOVE_LEN ];
\r
69 static MoveHistoryString * currMovelist;
\r
70 static ChessProgramStats_Move * currPvInfo;
\r
71 static int currFirst = 0;
\r
72 static int currLast = 0;
\r
73 static int currCurrent = -1;
\r
80 static HistoryMove histMoves[ MAX_MOVES ];
\r
82 #define WM_REFRESH_HISTORY (WM_USER+4657)
\r
84 #define DEFAULT_COLOR 0xFFFFFFFF
\r
89 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
\r
91 static VOID HighlightMove( int index, BOOL highlight )
\r
93 if( index >= 0 && index < MAX_MOVES ) {
\r
95 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
97 SendMessage( hMemo,
\r
99 histMoves[index].memoOffset,
\r
100 histMoves[index].memoOffset + histMoves[index].memoLength );
\r
104 ZeroMemory( &cf, sizeof(cf) );
\r
106 cf.cbSize = sizeof(cf);
\r
107 cf.dwMask = CFM_BOLD | CFM_COLOR;
\r
110 cf.dwEffects |= CFE_BOLD;
\r
111 cf.crTextColor = RGB( 0x00, 0x00, 0xFF );
\r
114 cf.dwEffects |= CFE_AUTOCOLOR;
\r
117 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
121 static BOOL OnlyCurrentPositionChanged()
\r
123 BOOL result = FALSE;
\r
125 if( lastFirst >= 0 &&
\r
126 lastLast >= lastFirst &&
\r
127 lastCurrent >= lastFirst &&
\r
128 currFirst == lastFirst &&
\r
129 currLast == lastLast &&
\r
130 currCurrent >= 0 &&
\r
135 /* Special case: last move changed */
\r
136 if( currCurrent == currLast-1 ) {
\r
137 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
\r
146 static BOOL OneMoveAppended()
\r
148 BOOL result = FALSE;
\r
150 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
\r
151 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
\r
152 lastFirst == currFirst &&
\r
153 lastLast == (currLast-1) &&
\r
154 lastCurrent == (currCurrent-1) &&
\r
155 currCurrent == (currLast-1) &&
\r
164 static VOID ClearMemo()
\r
166 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETTEXT, 0, (LPARAM) "" );
\r
169 static int AppendToMemo( char * text, DWORD flags, DWORD color )
\r
173 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
175 /* Select end of text */
\r
176 int cbTextLen = (int) SendMessage( hMemo, WM_GETTEXTLENGTH, 0, 0 );
\r
178 SendMessage( hMemo, EM_SETSEL, cbTextLen, cbTextLen );
\r
181 ZeroMemory( &cf, sizeof(cf) );
\r
183 cf.cbSize = sizeof(cf);
\r
184 cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR | CFM_UNDERLINE;
\r
185 cf.dwEffects = flags;
\r
187 if( color != DEFAULT_COLOR ) {
\r
188 cf.crTextColor = color;
\r
191 cf.dwEffects |= CFE_AUTOCOLOR;
\r
194 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
197 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
199 /* Return offset of appended text */
\r
203 static VOID AppendMoveToMemo( int index )
\r
207 DWORD color = DEFAULT_COLOR;
\r
209 if( index < 0 || index >= MAX_MOVES ) {
\r
216 if( (index % 2) == 0 ) {
\r
217 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
\r
218 AppendToMemo( buf, CFE_BOLD, DEFAULT_COLOR );
\r
222 strcpy( buf, SavePart( currMovelist[index] ) );
\r
223 strcat( buf, " " );
\r
225 histMoves[index].memoOffset = AppendToMemo( buf, flags, color );
\r
226 histMoves[index].memoLength = strlen(buf)-1;
\r
228 /* PV info (if any) */
\r
229 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
\r
230 sprintf( buf, "{%s%.2f/%d} ",
\r
231 currPvInfo[index].score >= 0 ? "+" : "",
\r
232 currPvInfo[index].score / 100.0,
\r
233 currPvInfo[index].depth );
\r
235 AppendToMemo( buf, flags,
\r
236 color == DEFAULT_COLOR ? GetSysColor(COLOR_GRAYTEXT) : color );
\r
240 static void RefreshMemoContent()
\r
246 for( i=currFirst; i<currLast; i++ ) {
\r
247 AppendMoveToMemo( i );
\r
251 static void MemoContentUpdated()
\r
255 HighlightMove( lastCurrent, FALSE );
\r
256 HighlightMove( currCurrent, TRUE );
\r
258 lastFirst = currFirst;
\r
259 lastLast = currLast;
\r
260 lastCurrent = currCurrent;
\r
261 lastLastMove[0] = '\0';
\r
263 if( lastLast > 0 ) {
\r
264 strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );
\r
267 /* Deselect any text, move caret to end of memo */
\r
268 if( currCurrent >= 0 ) {
\r
269 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
\r
272 caretPos = (int) SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_GETTEXTLENGTH, 0, 0 );
\r
275 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETSEL, caretPos, caretPos );
\r
277 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SCROLLCARET, 0, 0 );
\r
280 int FindMoveByCharIndex( int char_index )
\r
284 for( index=currFirst; index<currLast; index++ ) {
\r
285 if( char_index >= histMoves[index].memoOffset &&
\r
286 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
\r
295 LRESULT CALLBACK HistoryDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
297 static SnapData sd;
\r
300 case WM_INITDIALOG:
\r
301 if( moveHistoryDialog == NULL ) {
\r
302 moveHistoryDialog = hDlg;
\r
304 /* Enable word wrapping and notifications */
\r
305 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETTARGETDEVICE, 0, 0 );
\r
307 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS );
\r
310 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
312 /* Restore window placement */
\r
313 RestoreWindowPlacement( hDlg, &wpMoveHistory );
\r
317 RefreshMemoContent();
\r
319 MemoContentUpdated();
\r
324 switch (LOWORD(wParam)) {
\r
326 EndDialog(hDlg, TRUE);
\r
330 EndDialog(hDlg, FALSE);
\r
340 if( wParam == IDC_MoveHistory ) {
\r
341 MSGFILTER * lpMF = (MSGFILTER *) lParam;
\r
343 if( lpMF->msg == WM_LBUTTONDBLCLK && (lpMF->wParam & (MK_CONTROL | MK_SHIFT)) == 0 ) {
\r
347 pt.x = LOWORD( lpMF->lParam );
\r
348 pt.y = HIWORD( lpMF->lParam );
\r
350 index = SendDlgItemMessage( hDlg, IDC_MoveHistory, EM_CHARFROMPOS, 0, (LPARAM) &pt );
\r
352 index = FindMoveByCharIndex( index );
\r
355 ToNrEvent( index + 1 );
\r
358 /* Zap the message for good: apparently, returning non-zero is not enough */
\r
359 lpMF->msg = WM_USER;
\r
366 case WM_REFRESH_HISTORY:
\r
367 /* Update the GUI */
\r
368 if( OnlyCurrentPositionChanged() ) {
\r
369 /* Only "cursor" changed, no need to update memo content */
\r
371 else if( OneMoveAppended() ) {
\r
372 AppendMoveToMemo( currCurrent );
\r
375 RefreshMemoContent();
\r
378 MemoContentUpdated();
\r
383 SetWindowPos( GetDlgItem( moveHistoryDialog, IDC_MoveHistory ),
\r
385 H_MARGIN, V_MARGIN,
\r
386 LOWORD(lParam) - 2*H_MARGIN,
\r
387 HIWORD(lParam) - 2*V_MARGIN,
\r
391 case WM_GETMINMAXINFO:
\r
393 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
395 mmi->ptMinTrackSize.x = 100;
\r
396 mmi->ptMinTrackSize.y = 100;
\r
401 MoveHistoryPopDown();
\r
404 case WM_ENTERSIZEMOVE:
\r
405 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
408 return OnSizing( &sd, hDlg, wParam, lParam );
\r
411 return OnMoving( &sd, hDlg, wParam, lParam );
\r
413 case WM_EXITSIZEMOVE:
\r
414 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
420 VOID MoveHistoryPopUp()
\r
424 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_CHECKED);
\r
426 if( moveHistoryDialog ) {
\r
427 SendMessage( moveHistoryDialog, WM_INITDIALOG, 0, 0 );
\r
429 if( ! moveHistoryDialogUp ) {
\r
430 ShowWindow(moveHistoryDialog, SW_SHOW);
\r
434 lpProc = MakeProcInstance( (FARPROC) HistoryDialogProc, hInst );
\r
436 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
437 CreateDialog( hInst, MAKEINTRESOURCE(DLG_MoveHistory), hwndMain, (DLGPROC)lpProc );
\r
439 FreeProcInstance(lpProc);
\r
442 moveHistoryDialogUp = TRUE;
\r
445 VOID MoveHistoryPopDown()
\r
447 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_UNCHECKED);
\r
449 if( moveHistoryDialog ) {
\r
450 ShowWindow(moveHistoryDialog, SW_HIDE);
\r
453 moveHistoryDialogUp = FALSE;
\r
456 VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
\r
458 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
\r
460 currMovelist = movelist;
\r
463 currCurrent = current;
\r
464 currPvInfo = pvInfo;
\r
466 if( moveHistoryDialog ) {
\r
467 SendMessage( moveHistoryDialog, WM_REFRESH_HISTORY, 0, 0 );
\r
471 BOOL MoveHistoryIsUp()
\r
473 return moveHistoryDialogUp;
\r