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 * ------------------------------------------------------------------------
11 * GNU XBoard is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or (at
14 * your option) any later version.
16 * GNU XBoard is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
24 * ------------------------------------------------------------------------
25 ** See the file ChangeLog for a revision history. */
38 /* templates for low-level front-end tasks (requiring platform-dependent implementation) */
39 void ClearHistoryMemo P((void)); // essential
40 int AppendToHistoryMemo P(( char * text, int bold, int colorNr )); // essential (coloring / styling optional)
41 void HighlightMove P(( int from, int to, Boolean highlight )); // optional (can be dummy)
42 void ScrollToCurrent P((int caretPos)); // optional (can be dummy)
44 /* templates for front-end entry point to allow inquiring about front-end state */
45 Boolean MoveHistoryDialogExists P((void));
46 Boolean MoveHistoryIsUp P((void));
49 typedef char MoveHistoryString[ MOVE_LEN*2 ];
51 static int lastFirst = 0;
52 static int lastLast = 0;
53 static int lastCurrent = -1;
55 static char lastLastMove[ MOVE_LEN ];
57 static MoveHistoryString * currMovelist;
58 static ChessProgramStats_Move * currPvInfo;
59 static int currFirst = 0;
60 static int currLast = 0;
61 static int currCurrent = -1;
68 static HistoryMove histMoves[ MAX_MOVES ];
70 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
72 // back-end after replacing Windows data-types by equivalents
73 static Boolean OnlyCurrentPositionChanged()
75 Boolean result = FALSE;
78 lastLast >= lastFirst &&
79 lastCurrent >= lastFirst &&
80 currFirst == lastFirst &&
81 currLast == lastLast &&
87 /* Special case: last move changed */
88 if( currCurrent == currLast-1 ) {
89 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
98 // back-end, after replacing Windows data types
99 static Boolean OneMoveAppended()
101 Boolean result = FALSE;
103 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
104 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
105 lastFirst == currFirst &&
106 lastLast == (currLast-1) &&
107 lastCurrent == (currCurrent-1) &&
108 currCurrent == (currLast-1) &&
117 // back-end, now that color and font-style are passed as numbers
118 static void AppendMoveToMemo( int index )
122 if( index < 0 || index >= MAX_MOVES ) {
129 if( (index % 2) == 0 ) {
130 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
131 AppendToHistoryMemo( buf, 1, 0 ); // [HGM] 1 means bold, 0 default color
135 strcpy( buf, SavePart( currMovelist[index] ) );
138 histMoves[index].memoOffset = AppendToHistoryMemo( buf, 0, 0 );
139 histMoves[index].memoLength = strlen(buf)-1;
141 /* PV info (if any) */
142 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
143 sprintf( buf, "{%s%.2f/%d} ",
144 currPvInfo[index].score >= 0 ? "+" : "",
145 currPvInfo[index].score / 100.0,
146 currPvInfo[index].depth );
148 AppendToHistoryMemo( buf, 0, 1); // [HGM] 1 means gray
153 void RefreshMemoContent()
159 for( i=currFirst; i<currLast; i++ ) {
160 AppendMoveToMemo( i );
164 // back-end part taken out of HighlightMove to determine character positions
165 static void DoHighlight(int index, int onoff)
167 if( index >= 0 && index < MAX_MOVES ) {
168 HighlightMove( histMoves[index].memoOffset,
169 histMoves[index].memoOffset + histMoves[index].memoLength, onoff );
173 // back-end, now that a wrapper is provided for the front-end code to do the actual scrolling
174 void MemoContentUpdated()
178 DoHighlight( lastCurrent, FALSE );
179 DoHighlight( currCurrent, TRUE );
181 lastFirst = currFirst;
183 lastCurrent = currCurrent;
184 lastLastMove[0] = '\0';
187 strcpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) );
190 /* Deselect any text, move caret to end of memo */
191 if( currCurrent >= 0 ) {
192 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
198 ScrollToCurrent(caretPos);
201 // back-end. Must be called as double-click call-back on move-history text edit
202 void FindMoveByCharIndex( int char_index )
206 for( index=currFirst; index<currLast; index++ ) {
207 if( char_index >= histMoves[index].memoOffset &&
208 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
210 ToNrEvent( index + 1 ); // moved here from call-back
215 // back-end. In WinBoard called by call-back, but could be called directly by SetIfExists?
216 void UpdateMoveHistory()
219 if( OnlyCurrentPositionChanged() ) {
220 /* Only "cursor" changed, no need to update memo content */
222 else if( OneMoveAppended() ) {
223 AppendMoveToMemo( currCurrent );
226 RefreshMemoContent();
229 MemoContentUpdated();
233 void MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
235 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
237 currMovelist = movelist;
240 currCurrent = current;
243 if(MoveHistoryDialogExists())
244 UpdateMoveHistory(); // [HGM] call this directly, in stead of through call-back