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
75 static Boolean OnlyCurrentPositionChanged()
77 Boolean result = FALSE;
80 lastLast >= lastFirst &&
81 lastCurrent >= lastFirst &&
82 currFirst == lastFirst &&
83 currLast == lastLast &&
85 lastGames == storedGames )
89 /* Special case: last move changed */
90 if( currCurrent == currLast-1 ) {
91 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
100 // back-end, after replacing Windows data types
101 static Boolean OneMoveAppended()
103 Boolean result = FALSE;
105 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
106 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
107 lastFirst == currFirst &&
108 lastLast == (currLast-1) &&
109 lastCurrent == (currCurrent-1) &&
110 currCurrent == (currLast-1) &&
111 lastGames == storedGames )
119 // back-end, now that color and font-style are passed as numbers
120 static void AppendMoveToMemo( int index )
124 if( index < 0 || index >= MAX_MOVES ) {
131 if( (index % 2) == 0 ) {
132 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
133 AppendToHistoryMemo( buf, 1, 0 ); // [HGM] 1 means bold, 0 default color
137 safeStrCpy( buf, SavePart( currMovelist[index]) , sizeof( buf)/sizeof( buf[0]) );
140 histMoves[index].memoOffset = AppendToHistoryMemo( buf, 0, 0 );
141 histMoves[index].memoLength = strlen(buf)-1;
143 /* PV info (if any) */
144 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
145 sprintf( buf, "{%s%.2f/%d} ",
146 currPvInfo[index].score >= 0 ? "+" : "",
147 currPvInfo[index].score / 100.0,
148 currPvInfo[index].depth );
150 AppendToHistoryMemo( buf, 0, 1); // [HGM] 1 means gray
155 void RefreshMemoContent()
161 for( i=currFirst; i<currLast; i++ ) {
162 AppendMoveToMemo( i );
166 // back-end part taken out of HighlightMove to determine character positions
167 static void DoHighlight(int index, int onoff)
169 if( index >= 0 && index < MAX_MOVES ) {
170 HighlightMove( histMoves[index].memoOffset,
171 histMoves[index].memoOffset + histMoves[index].memoLength, onoff );
175 // back-end, now that a wrapper is provided for the front-end code to do the actual scrolling
176 void MemoContentUpdated()
180 if(lastCurrent <= currLast) DoHighlight( lastCurrent, FALSE );
182 lastFirst = currFirst;
184 lastCurrent = currCurrent;
185 lastGames = storedGames;
186 lastLastMove[0] = '\0';
189 safeStrCpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) , sizeof( lastLastMove)/sizeof( lastLastMove[0]) );
192 /* Deselect any text, move caret to end of memo */
193 if( currCurrent >= 0 ) {
194 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
200 ScrollToCurrent(caretPos);
201 DoHighlight( currCurrent, TRUE ); // [HGM] moved last, because in X some scrolling methods spoil highlighting
204 // back-end. Must be called as double-click call-back on move-history text edit
205 void FindMoveByCharIndex( int char_index )
209 for( index=currFirst; index<currLast; index++ ) {
210 if( char_index >= histMoves[index].memoOffset &&
211 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
213 ToNrEvent( index + 1 ); // moved here from call-back
218 // back-end. In WinBoard called by call-back, but could be called directly by SetIfExists?
219 void UpdateMoveHistory()
222 if( OnlyCurrentPositionChanged() ) {
223 /* Only "cursor" changed, no need to update memo content */
225 else if( OneMoveAppended() ) {
226 AppendMoveToMemo( currCurrent );
229 RefreshMemoContent();
232 MemoContentUpdated();
236 void MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
238 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
240 currMovelist = movelist;
243 currCurrent = current;
246 if(MoveHistoryDialogExists())
247 UpdateMoveHistory(); // [HGM] call this directly, in stead of through call-back