2 * Move history for WinBoard
\r
4 * Author: Alessandro Scotti (Dec 2005)
\r
6 * Copyright 2005 Alessandro Scotti
\r
8 * ------------------------------------------------------------------------
\r
10 * GNU XBoard is free software: you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License as published by
\r
12 * the Free Software Foundation, either version 3 of the License, or (at
\r
13 * your option) any later version.
\r
15 * GNU XBoard is distributed in the hope that it will be useful, but
\r
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
18 * General Public License for more details.
\r
20 * You should have received a copy of the GNU General Public License
\r
21 * along with this program. If not, see http://www.gnu.org/licenses/.
\r
23 * ------------------------------------------------------------------------
\r
24 ** See the file ChangeLog for a revision history. */
\r
28 #include <windows.h> /* required for all Windows applications */
\r
29 #include <richedit.h>
\r
33 #include <commdlg.h>
\r
37 #include "frontend.h"
\r
38 #include "backend.h"
\r
39 #include "winboard.h"
\r
43 /* Module globals */
\r
44 typedef char MoveHistoryString[ MOVE_LEN*2 ];
\r
45 static BOOLEAN moveHistoryDialogUp = FALSE;
\r
47 static int lastFirst = 0;
\r
48 static int lastLast = 0;
\r
49 static int lastCurrent = -1;
\r
51 static char lastLastMove[ MOVE_LEN ];
\r
53 static MoveHistoryString * currMovelist;
\r
54 static ChessProgramStats_Move * currPvInfo;
\r
55 static int currFirst = 0;
\r
56 static int currLast = 0;
\r
57 static int currCurrent = -1;
\r
64 static HistoryMove histMoves[ MAX_MOVES ];
\r
66 #define WM_REFRESH_HISTORY (WM_USER+4657)
\r
68 #define DEFAULT_COLOR 0xFFFFFFFF
\r
73 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
\r
75 static VOID HighlightMove( int index, BOOL highlight )
\r
77 if( index >= 0 && index < MAX_MOVES ) {
\r
79 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
81 SendMessage( hMemo,
\r
83 histMoves[index].memoOffset,
\r
84 histMoves[index].memoOffset + histMoves[index].memoLength );
\r
88 ZeroMemory( &cf, sizeof(cf) );
\r
90 cf.cbSize = sizeof(cf);
\r
91 cf.dwMask = CFM_BOLD | CFM_COLOR;
\r
94 cf.dwEffects |= CFE_BOLD;
\r
95 cf.crTextColor = RGB( 0x00, 0x00, 0xFF );
\r
98 cf.dwEffects |= CFE_AUTOCOLOR;
\r
101 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
105 static BOOL OnlyCurrentPositionChanged()
\r
107 BOOL result = FALSE;
\r
109 if( lastFirst >= 0 &&
\r
110 lastLast >= lastFirst &&
\r
111 lastCurrent >= lastFirst &&
\r
112 currFirst == lastFirst &&
\r
113 currLast == lastLast &&
\r
114 currCurrent >= 0 &&
\r
119 /* Special case: last move changed */
\r
120 if( currCurrent == currLast-1 ) {
\r
121 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
\r
130 static BOOL OneMoveAppended()
\r
132 BOOL result = FALSE;
\r
134 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
\r
135 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
\r
136 lastFirst == currFirst &&
\r
137 lastLast == (currLast-1) &&
\r
138 lastCurrent == (currCurrent-1) &&
\r
139 currCurrent == (currLast-1) &&
\r
148 static VOID ClearMemo()
\r
150 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETTEXT, 0, (LPARAM) "" );
\r
153 static int AppendToMemo( char * text, DWORD flags, DWORD color )
\r
157 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
159 /* Select end of text */
\r
160 int cbTextLen = (int) SendMessage( hMemo, WM_GETTEXTLENGTH, 0, 0 );
\r
162 SendMessage( hMemo, EM_SETSEL, cbTextLen, cbTextLen );
\r
165 ZeroMemory( &cf, sizeof(cf) );
\r
167 cf.cbSize = sizeof(cf);
\r
168 cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR | CFM_UNDERLINE;
\r
169 cf.dwEffects = flags;
\r
171 if( color != DEFAULT_COLOR ) {
\r
172 cf.crTextColor = color;
\r
175 cf.dwEffects |= CFE_AUTOCOLOR;
\r
178 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
181 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
183 /* Return offset of appended text */
\r
187 static VOID AppendMoveToMemo( int index )
\r
191 DWORD color = DEFAULT_COLOR;
\r
193 if( index < 0 || index >= MAX_MOVES ) {
\r
200 if( (index % 2) == 0 ) {
\r
201 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
\r
202 AppendToMemo( buf, CFE_BOLD, DEFAULT_COLOR );
\r
206 strcpy( buf, SavePart( currMovelist[index] ) );
\r
207 strcat( buf, " " );
\r
209 histMoves[index].memoOffset = AppendToMemo( buf, flags, color );
\r
210 histMoves[index].memoLength = strlen(buf)-1;
\r
212 /* PV info (if any) */
\r
213 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
\r
214 sprintf( buf, "{%s%.2f/%d} ",
\r
215 currPvInfo[index].score >= 0 ? "+" : "",
\r
216 currPvInfo[index].score / 100.0,
\r
217 currPvInfo[index].depth );
\r
219 AppendToMemo( buf, flags,
\r
220 color == DEFAULT_COLOR ? GetSysColor(COLOR_GRAYTEXT) : color );
\r
224 static void RefreshMemoContent()
\r
230 for( i=currFirst; i<currLast; i++ ) {
\r
231 AppendMoveToMemo( i );
\r
235 static void MemoContentUpdated()
\r
239 HighlightMove( lastCurrent, FALSE );
\r
240 HighlightMove( currCurrent, TRUE );
\r
242 lastFirst = currFirst;
\r
243 lastLast = currLast;
\r
244 lastCurrent = currCurrent;
\r
245 lastLastMove[0] = '\0';
\r
247 if( lastLast > 0 ) {
\r
248 strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );
\r
251 /* Deselect any text, move caret to end of memo */
\r
252 if( currCurrent >= 0 ) {
\r
253 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
\r
256 caretPos = (int) SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_GETTEXTLENGTH, 0, 0 );
\r
259 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETSEL, caretPos, caretPos );
\r
261 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SCROLLCARET, 0, 0 );
\r
264 int FindMoveByCharIndex( int char_index )
\r
268 for( index=currFirst; index<currLast; index++ ) {
\r
269 if( char_index >= histMoves[index].memoOffset &&
\r
270 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
\r
279 LRESULT CALLBACK HistoryDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
281 static SnapData sd;
\r
284 case WM_INITDIALOG:
\r
285 if( moveHistoryDialog == NULL ) {
\r
286 moveHistoryDialog = hDlg;
\r
288 /* Enable word wrapping and notifications */
\r
289 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETTARGETDEVICE, 0, 0 );
\r
291 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS );
\r
294 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
296 /* Restore window placement */
\r
297 RestoreWindowPlacement( hDlg, &wpMoveHistory );
\r
301 RefreshMemoContent();
\r
303 MemoContentUpdated();
\r
308 switch (LOWORD(wParam)) {
\r
310 EndDialog(hDlg, TRUE);
\r
314 EndDialog(hDlg, FALSE);
\r
324 if( wParam == IDC_MoveHistory ) {
\r
325 MSGFILTER * lpMF = (MSGFILTER *) lParam;
\r
327 if( lpMF->msg == WM_LBUTTONDBLCLK && (lpMF->wParam & (MK_CONTROL | MK_SHIFT)) == 0 ) {
\r
331 pt.x = LOWORD( lpMF->lParam );
\r
332 pt.y = HIWORD( lpMF->lParam );
\r
334 index = SendDlgItemMessage( hDlg, IDC_MoveHistory, EM_CHARFROMPOS, 0, (LPARAM) &pt );
\r
336 index = FindMoveByCharIndex( index );
\r
339 ToNrEvent( index + 1 );
\r
342 /* Zap the message for good: apparently, returning non-zero is not enough */
\r
343 lpMF->msg = WM_USER;
\r
350 case WM_REFRESH_HISTORY:
\r
351 /* Update the GUI */
\r
352 if( OnlyCurrentPositionChanged() ) {
\r
353 /* Only "cursor" changed, no need to update memo content */
\r
355 else if( OneMoveAppended() ) {
\r
356 AppendMoveToMemo( currCurrent );
\r
359 RefreshMemoContent();
\r
362 MemoContentUpdated();
\r
367 SetWindowPos( GetDlgItem( moveHistoryDialog, IDC_MoveHistory ),
\r
369 H_MARGIN, V_MARGIN,
\r
370 LOWORD(lParam) - 2*H_MARGIN,
\r
371 HIWORD(lParam) - 2*V_MARGIN,
\r
375 case WM_GETMINMAXINFO:
\r
377 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
379 mmi->ptMinTrackSize.x = 100;
\r
380 mmi->ptMinTrackSize.y = 100;
\r
385 MoveHistoryPopDown();
\r
388 case WM_ENTERSIZEMOVE:
\r
389 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
392 return OnSizing( &sd, hDlg, wParam, lParam );
\r
395 return OnMoving( &sd, hDlg, wParam, lParam );
\r
397 case WM_EXITSIZEMOVE:
\r
398 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
404 VOID MoveHistoryPopUp()
\r
408 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_CHECKED);
\r
410 if( moveHistoryDialog ) {
\r
411 SendMessage( moveHistoryDialog, WM_INITDIALOG, 0, 0 );
\r
413 if( ! moveHistoryDialogUp ) {
\r
414 ShowWindow(moveHistoryDialog, SW_SHOW);
\r
418 lpProc = MakeProcInstance( (FARPROC) HistoryDialogProc, hInst );
\r
420 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
421 CreateDialog( hInst, MAKEINTRESOURCE(DLG_MoveHistory), hwndMain, (DLGPROC)lpProc );
\r
423 FreeProcInstance(lpProc);
\r
426 moveHistoryDialogUp = TRUE;
\r
429 VOID MoveHistoryPopDown()
\r
431 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_UNCHECKED);
\r
433 if( moveHistoryDialog ) {
\r
434 ShowWindow(moveHistoryDialog, SW_HIDE);
\r
437 moveHistoryDialogUp = FALSE;
\r
440 VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
\r
442 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
\r
444 currMovelist = movelist;
\r
447 currCurrent = current;
\r
448 currPvInfo = pvInfo;
\r
450 if( moveHistoryDialog ) {
\r
451 SendMessage( moveHistoryDialog, WM_REFRESH_HISTORY, 0, 0 );
\r
455 Boolean MoveHistoryIsUp()
\r
457 return moveHistoryDialogUp;
\r