2 * Move history for WinBoard
4 * Author: Alessandro Scotti (Dec 2005)
5 * back-end part split off by HGM
7 * Copyright 2005 Alessandro Scotti
9 * Enhancements Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
11 * ------------------------------------------------------------------------
13 * GNU XBoard is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or (at
16 * your option) any later version.
18 * GNU XBoard is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see http://www.gnu.org/licenses/.
26 * ------------------------------------------------------------------------
27 ** See the file ChangeLog for a revision history. */
39 /* templates for low-level front-end tasks (requiring platform-dependent implementation) */
40 void ClearHistoryMemo P((void)); // essential
41 int AppendToHistoryMemo P(( char * text, int bold, int colorNr )); // essential (coloring / styling optional)
42 void HighlightMove P(( int from, int to, Boolean highlight )); // optional (can be dummy)
43 void ScrollToCurrent P((int caretPos)); // optional (can be dummy)
45 /* templates for front-end entry point to allow inquiring about front-end state */
46 Boolean MoveHistoryDialogExists P((void));
47 Boolean MoveHistoryIsUp P((void));
50 typedef char MoveHistoryString[ MOVE_LEN*2 ];
52 static int lastFirst = 0;
53 static int lastLast = 0;
54 static int lastCurrent = -1;
57 static char lastLastMove[ MOVE_LEN ];
59 static MoveHistoryString * currMovelist;
60 static ChessProgramStats_Move * currPvInfo;
61 static int currFirst = 0;
62 static int currLast = 0;
63 static int currCurrent = -1;
70 static HistoryMove histMoves[ MAX_MOVES ];
72 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
74 // back-end after replacing Windows data-types by equivalents
76 OnlyCurrentPositionChanged ()
78 Boolean result = FALSE;
81 lastLast >= lastFirst &&
82 lastCurrent >= lastFirst &&
83 currFirst == lastFirst &&
84 currLast == lastLast &&
86 lastGames == storedGames )
90 /* Special case: last move changed */
91 if( currCurrent == currLast-1 ) {
92 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
101 // back-end, after replacing Windows data types
105 Boolean result = FALSE;
107 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
108 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
109 lastFirst == currFirst &&
110 lastLast == (currLast-1) &&
111 lastCurrent == (currCurrent-1) &&
112 currCurrent == (currLast-1) &&
113 lastGames == storedGames )
121 // back-end, now that color and font-style are passed as numbers
123 AppendMoveToMemo (int index)
127 if( index < 0 || index >= MAX_MOVES ) {
134 if( (index % 2) == 0 ) {
135 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
136 AppendToHistoryMemo( buf, 1, 0 ); // [HGM] 1 means bold, 0 default color
140 safeStrCpy( buf, SavePart( currMovelist[index]) , sizeof( buf)/sizeof( buf[0]) );
143 histMoves[index].memoOffset = AppendToHistoryMemo( buf, 0, 0 );
144 histMoves[index].memoLength = strlen(buf)-1;
146 /* PV info (if any) */
147 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
148 sprintf( buf, "{%s%.2f/%d} ",
149 currPvInfo[index].score >= 0 ? "+" : "",
150 currPvInfo[index].score / 100.0,
151 currPvInfo[index].depth );
153 AppendToHistoryMemo( buf, 0, 1); // [HGM] 1 means gray
159 RefreshMemoContent ()
165 for( i=currFirst; i<currLast; i++ ) {
166 AppendMoveToMemo( i );
170 // back-end part taken out of HighlightMove to determine character positions
172 DoHighlight (int index, int onoff)
174 if( index >= 0 && index < MAX_MOVES ) {
175 HighlightMove( histMoves[index].memoOffset,
176 histMoves[index].memoOffset + histMoves[index].memoLength, onoff );
180 // back-end, now that a wrapper is provided for the front-end code to do the actual scrolling
182 MemoContentUpdated ()
186 if(lastCurrent <= currLast) DoHighlight( lastCurrent, FALSE );
188 lastFirst = currFirst;
190 lastCurrent = currCurrent;
191 lastGames = storedGames;
192 lastLastMove[0] = '\0';
195 safeStrCpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) , sizeof( lastLastMove)/sizeof( lastLastMove[0]) );
198 /* Deselect any text, move caret to end of memo */
199 if( currCurrent >= 0 ) {
200 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
206 ScrollToCurrent(caretPos);
207 DoHighlight( currCurrent, TRUE ); // [HGM] moved last, because in X some scrolling methods spoil highlighting
210 // back-end. Must be called as double-click call-back on move-history text edit
212 FindMoveByCharIndex (int char_index)
216 for( index=currFirst; index<currLast; index++ ) {
217 if( char_index >= histMoves[index].memoOffset &&
218 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
220 ToNrEvent( index + 1 ); // moved here from call-back
225 // back-end. In WinBoard called by call-back, but could be called directly by SetIfExists?
230 if( OnlyCurrentPositionChanged() ) {
231 /* Only "cursor" changed, no need to update memo content */
233 else if( OneMoveAppended() ) {
234 AppendMoveToMemo( currCurrent );
237 RefreshMemoContent();
240 MemoContentUpdated();
245 MoveHistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo)
247 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
249 currMovelist = movelist;
252 currCurrent = current;
255 if(MoveHistoryDialogExists())
256 UpdateMoveHistory(); // [HGM] call this directly, in stead of through call-back