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