2 * Move history for WinBoard
\r
4 * Author: Alessandro Scotti (Dec 2005)
\r
6 * ------------------------------------------------------------------------
\r
7 * This program is free software; you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation; either version 2 of the License, or
\r
10 * (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program; if not, write to the Free Software
\r
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
20 * ------------------------------------------------------------------------
\r
24 #include <windows.h> /* required for all Windows applications */
\r
25 #include <richedit.h>
\r
29 #include <commdlg.h>
\r
33 #include "winboard.h"
\r
34 #include "frontend.h"
\r
35 #include "backend.h"
\r
39 VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo );
\r
40 VOID MoveHistoryPopUp();
\r
41 VOID MoveHistoryPopDown();
\r
42 BOOL MoveHistoryIsUp();
\r
44 /* Imports from backend.c */
\r
45 char * SavePart(char *str);
\r
47 /* Imports from winboard.c */
\r
48 extern HWND moveHistoryDialog;
\r
49 extern BOOLEAN moveHistoryDialogUp;
\r
51 extern HINSTANCE hInst;
\r
52 extern HWND hwndMain;
\r
54 extern WindowPlacement wpMoveHistory;
\r
56 extern BoardSize boardSize;
\r
58 /* Module globals */
\r
59 typedef char MoveHistoryString[ MOVE_LEN*2 ];
\r
61 static int lastFirst = 0;
\r
62 static int lastLast = 0;
\r
63 static int lastCurrent = -1;
\r
65 static char lastLastMove[ MOVE_LEN ];
\r
67 static MoveHistoryString * currMovelist;
\r
68 static ChessProgramStats_Move * currPvInfo;
\r
69 static int currFirst = 0;
\r
70 static int currLast = 0;
\r
71 static int currCurrent = -1;
\r
78 static HistoryMove histMoves[ MAX_MOVES ];
\r
80 #define WM_REFRESH_HISTORY (WM_USER+4657)
\r
82 #define DEFAULT_COLOR 0xFFFFFFFF
\r
87 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
\r
89 static VOID HighlightMove( int index, BOOL highlight )
\r
91 if( index >= 0 && index < MAX_MOVES ) {
\r
93 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
95 SendMessage( hMemo,
\r
97 histMoves[index].memoOffset,
\r
98 histMoves[index].memoOffset + histMoves[index].memoLength );
\r
102 ZeroMemory( &cf, sizeof(cf) );
\r
104 cf.cbSize = sizeof(cf);
\r
105 cf.dwMask = CFM_BOLD | CFM_COLOR;
\r
108 cf.dwEffects |= CFE_BOLD;
\r
109 cf.crTextColor = RGB( 0x00, 0x00, 0xFF );
\r
112 cf.dwEffects |= CFE_AUTOCOLOR;
\r
115 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
119 static BOOL OnlyCurrentPositionChanged()
\r
121 BOOL result = FALSE;
\r
123 if( lastFirst >= 0 &&
\r
124 lastLast >= lastFirst &&
\r
125 lastCurrent >= lastFirst &&
\r
126 currFirst == lastFirst &&
\r
127 currLast == lastLast &&
\r
128 currCurrent >= 0 &&
\r
133 /* Special case: last move changed */
\r
134 if( currCurrent == currLast-1 ) {
\r
135 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
\r
144 static BOOL OneMoveAppended()
\r
146 BOOL result = FALSE;
\r
148 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
\r
149 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
\r
150 lastFirst == currFirst &&
\r
151 lastLast == (currLast-1) &&
\r
152 lastCurrent == (currCurrent-1) &&
\r
153 currCurrent == (currLast-1) &&
\r
162 static VOID ClearMemo()
\r
164 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETTEXT, 0, (LPARAM) "" );
\r
167 static int AppendToMemo( char * text, DWORD flags, DWORD color )
\r
171 HWND hMemo = GetDlgItem( moveHistoryDialog, IDC_MoveHistory );
\r
173 /* Select end of text */
\r
174 int cbTextLen = (int) SendMessage( hMemo, WM_GETTEXTLENGTH, 0, 0 );
\r
176 SendMessage( hMemo, EM_SETSEL, cbTextLen, cbTextLen );
\r
179 ZeroMemory( &cf, sizeof(cf) );
\r
181 cf.cbSize = sizeof(cf);
\r
182 cf.dwMask = CFM_BOLD | CFM_ITALIC | CFM_COLOR | CFM_UNDERLINE;
\r
183 cf.dwEffects = flags;
\r
185 if( color != DEFAULT_COLOR ) {
\r
186 cf.crTextColor = color;
\r
189 cf.dwEffects |= CFE_AUTOCOLOR;
\r
192 SendMessage( hMemo, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cf );
\r
195 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
197 /* Return offset of appended text */
\r
201 static VOID AppendMoveToMemo( int index )
\r
205 DWORD color = DEFAULT_COLOR;
\r
207 if( index < 0 || index >= MAX_MOVES ) {
\r
214 if( (index % 2) == 0 ) {
\r
215 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
\r
216 AppendToMemo( buf, CFE_BOLD, DEFAULT_COLOR );
\r
220 strcpy( buf, SavePart( currMovelist[index] ) );
\r
221 strcat( buf, " " );
\r
223 histMoves[index].memoOffset = AppendToMemo( buf, flags, color );
\r
224 histMoves[index].memoLength = strlen(buf)-1;
\r
226 /* PV info (if any) */
\r
227 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
\r
228 sprintf( buf, "{%s%.2f/%d} ",
\r
229 currPvInfo[index].score >= 0 ? "+" : "",
\r
230 currPvInfo[index].score / 100.0,
\r
231 currPvInfo[index].depth );
\r
233 AppendToMemo( buf, flags,
\r
234 color == DEFAULT_COLOR ? GetSysColor(COLOR_GRAYTEXT) : color );
\r
238 static void RefreshMemoContent()
\r
244 for( i=currFirst; i<currLast; i++ ) {
\r
245 AppendMoveToMemo( i );
\r
249 static void MemoContentUpdated()
\r
253 HighlightMove( lastCurrent, FALSE );
\r
254 HighlightMove( currCurrent, TRUE );
\r
256 lastFirst = currFirst;
\r
257 lastLast = currLast;
\r
258 lastCurrent = currCurrent;
\r
259 lastLastMove[0] = '\0';
\r
261 if( lastLast > 0 ) {
\r
262 strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );
\r
265 /* Deselect any text, move caret to end of memo */
\r
266 if( currCurrent >= 0 ) {
\r
267 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
\r
270 caretPos = (int) SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_GETTEXTLENGTH, 0, 0 );
\r
273 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETSEL, caretPos, caretPos );
\r
275 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SCROLLCARET, 0, 0 );
\r
278 int FindMoveByCharIndex( int char_index )
\r
282 for( index=currFirst; index<currLast; index++ ) {
\r
283 if( char_index >= histMoves[index].memoOffset &&
\r
284 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
\r
293 LRESULT CALLBACK HistoryDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
295 static SnapData sd;
\r
298 case WM_INITDIALOG:
\r
299 if( moveHistoryDialog == NULL ) {
\r
300 moveHistoryDialog = hDlg;
\r
302 /* Enable word wrapping and notifications */
\r
303 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETTARGETDEVICE, 0, 0 );
\r
305 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS );
\r
308 SendDlgItemMessage( moveHistoryDialog, IDC_MoveHistory, WM_SETFONT, (WPARAM)font[boardSize][MOVEHISTORY_FONT]->hf, MAKELPARAM(TRUE, 0 ));
\r
310 /* Restore window placement */
\r
311 RestoreWindowPlacement( hDlg, &wpMoveHistory );
\r
315 RefreshMemoContent();
\r
317 MemoContentUpdated();
\r
322 switch (LOWORD(wParam)) {
\r
324 EndDialog(hDlg, TRUE);
\r
328 EndDialog(hDlg, FALSE);
\r
338 if( wParam == IDC_MoveHistory ) {
\r
339 MSGFILTER * lpMF = (MSGFILTER *) lParam;
\r
341 if( lpMF->msg == WM_LBUTTONDBLCLK && (lpMF->wParam & (MK_CONTROL | MK_SHIFT)) == 0 ) {
\r
345 pt.x = LOWORD( lpMF->lParam );
\r
346 pt.y = HIWORD( lpMF->lParam );
\r
348 index = SendDlgItemMessage( hDlg, IDC_MoveHistory, EM_CHARFROMPOS, 0, (LPARAM) &pt );
\r
350 index = FindMoveByCharIndex( index );
\r
353 ToNrEvent( index + 1 );
\r
356 /* Zap the message for good: apparently, returning non-zero is not enough */
\r
357 lpMF->msg = WM_USER;
\r
364 case WM_REFRESH_HISTORY:
\r
365 /* Update the GUI */
\r
366 if( OnlyCurrentPositionChanged() ) {
\r
367 /* Only "cursor" changed, no need to update memo content */
\r
369 else if( OneMoveAppended() ) {
\r
370 AppendMoveToMemo( currCurrent );
\r
373 RefreshMemoContent();
\r
376 MemoContentUpdated();
\r
381 SetWindowPos( GetDlgItem( moveHistoryDialog, IDC_MoveHistory ),
\r
383 H_MARGIN, V_MARGIN,
\r
384 LOWORD(lParam) - 2*H_MARGIN,
\r
385 HIWORD(lParam) - 2*V_MARGIN,
\r
389 case WM_GETMINMAXINFO:
\r
391 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
393 mmi->ptMinTrackSize.x = 100;
\r
394 mmi->ptMinTrackSize.y = 100;
\r
399 MoveHistoryPopDown();
\r
402 case WM_ENTERSIZEMOVE:
\r
403 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
406 return OnSizing( &sd, hDlg, wParam, lParam );
\r
409 return OnMoving( &sd, hDlg, wParam, lParam );
\r
411 case WM_EXITSIZEMOVE:
\r
412 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
418 VOID MoveHistoryPopUp()
\r
422 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_CHECKED);
\r
424 if( moveHistoryDialog ) {
\r
425 SendMessage( moveHistoryDialog, WM_INITDIALOG, 0, 0 );
\r
427 if( ! moveHistoryDialogUp ) {
\r
428 ShowWindow(moveHistoryDialog, SW_SHOW);
\r
432 lpProc = MakeProcInstance( (FARPROC) HistoryDialogProc, hInst );
\r
434 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
435 CreateDialog( hInst, MAKEINTRESOURCE(DLG_MoveHistory), hwndMain, (DLGPROC)lpProc );
\r
437 FreeProcInstance(lpProc);
\r
440 moveHistoryDialogUp = TRUE;
\r
443 VOID MoveHistoryPopDown()
\r
445 CheckMenuItem(GetMenu(hwndMain), IDM_ShowMoveHistory, MF_UNCHECKED);
\r
447 if( moveHistoryDialog ) {
\r
448 ShowWindow(moveHistoryDialog, SW_HIDE);
\r
451 moveHistoryDialogUp = FALSE;
\r
454 VOID MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
\r
456 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
\r
458 currMovelist = movelist;
\r
461 currCurrent = current;
\r
462 currPvInfo = pvInfo;
\r
464 if( moveHistoryDialog ) {
\r
465 SendMessage( moveHistoryDialog, WM_REFRESH_HISTORY, 0, 0 );
\r
469 BOOL MoveHistoryIsUp()
\r
471 return moveHistoryDialogUp;
\r