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, 2013, 2014, 2015,
10 * 2016 Free Software Foundation, Inc.
12 * ------------------------------------------------------------------------
14 * GNU XBoard is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or (at
17 * your option) any later version.
19 * GNU XBoard is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see http://www.gnu.org/licenses/.
27 * ------------------------------------------------------------------------
28 ** See the file ChangeLog for a revision history. */
40 /* templates for low-level front-end tasks (requiring platform-dependent implementation) */
41 void ClearHistoryMemo P((void)); // essential
42 int AppendToHistoryMemo P(( char * text, int bold, int colorNr )); // essential (coloring / styling optional)
43 void HighlightMove P(( int from, int to, Boolean highlight )); // optional (can be dummy)
44 void ScrollToCurrent P((int caretPos)); // optional (can be dummy)
46 /* templates for front-end entry point to allow inquiring about front-end state */
47 Boolean MoveHistoryDialogExists P((void));
48 Boolean MoveHistoryIsUp P((void));
51 typedef char MoveHistoryString[ MOVE_LEN*2 ];
53 static int lastFirst = 0;
54 static int lastLast = 0;
55 static int lastCurrent = -1;
58 static char lastLastMove[ MOVE_LEN ];
60 static MoveHistoryString * currMovelist;
61 static ChessProgramStats_Move * currPvInfo;
62 static int currFirst = 0;
63 static int currLast = 0;
64 static int currCurrent = -1;
71 static HistoryMove histMoves[ MAX_MOVES ];
73 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
75 // back-end after replacing Windows data-types by equivalents
77 OnlyCurrentPositionChanged ()
79 Boolean result = FALSE;
82 lastLast >= lastFirst &&
83 lastCurrent >= lastFirst &&
84 currFirst == lastFirst &&
85 currLast == lastLast &&
87 lastGames == storedGames )
91 /* Special case: last move changed */
92 if( currCurrent == currLast-1 ) {
93 if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
102 // back-end, after replacing Windows data types
106 Boolean result = FALSE;
108 if( lastCurrent >= 0 && lastCurrent >= lastFirst && lastLast >= lastFirst &&
109 currCurrent >= 0 && currCurrent >= currFirst && currLast >= currFirst &&
110 lastFirst == currFirst &&
111 lastLast == (currLast-1) &&
112 lastCurrent == (currCurrent-1) &&
113 currCurrent == (currLast-1) &&
114 lastGames == storedGames )
122 // back-end, now that color and font-style are passed as numbers
124 AppendMoveToMemo (int index)
128 if( index < 0 || index >= MAX_MOVES ) {
135 if( (index % 2) == 0 ) {
136 sprintf( buf, "%d.%s ", (index / 2)+1, index & 1 ? ".." : "" );
137 AppendToHistoryMemo( buf, 1, 0 ); // [HGM] 1 means bold, 0 default color
141 safeStrCpy( buf, SavePart( currMovelist[index]) , sizeof( buf)/sizeof( buf[0]) );
144 histMoves[index].memoOffset = AppendToHistoryMemo( buf, 0, 0 );
145 histMoves[index].memoLength = strlen(buf)-1;
147 /* PV info (if any) */
148 if( appData.showEvalInMoveHistory && currPvInfo[index].depth > 0 ) {
149 sprintf( buf, "{%s%.2f/%d} ",
150 currPvInfo[index].score >= 0 ? "+" : "",
151 currPvInfo[index].score / 100.0,
152 currPvInfo[index].depth );
154 AppendToHistoryMemo( buf, 0, 1); // [HGM] 1 means gray
160 RefreshMemoContent ()
166 for( i=currFirst; i<currLast; i++ ) {
167 AppendMoveToMemo( i );
171 // back-end part taken out of HighlightMove to determine character positions
173 DoHighlight (int index, int onoff)
175 if( index >= 0 && index < MAX_MOVES ) {
176 HighlightMove( histMoves[index].memoOffset,
177 histMoves[index].memoOffset + histMoves[index].memoLength, onoff );
181 // back-end, now that a wrapper is provided for the front-end code to do the actual scrolling
183 MemoContentUpdated ()
187 if(lastCurrent <= currLast) DoHighlight( lastCurrent, FALSE );
189 lastFirst = currFirst;
191 lastCurrent = currCurrent;
192 lastGames = storedGames;
193 lastLastMove[0] = '\0';
196 safeStrCpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) , sizeof( lastLastMove)/sizeof( lastLastMove[0]) );
199 /* Deselect any text, move caret to end of memo */
200 if( currCurrent >= 0 ) {
201 caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
207 ScrollToCurrent(caretPos);
208 DoHighlight( currCurrent, TRUE ); // [HGM] moved last, because in X some scrolling methods spoil highlighting
211 // back-end. Must be called as double-click call-back on move-history text edit
213 FindMoveByCharIndex (int char_index)
217 for( index=currFirst; index<currLast; index++ ) {
218 if( char_index >= histMoves[index].memoOffset &&
219 char_index < (histMoves[index].memoOffset + histMoves[index].memoLength) )
221 ToNrEvent( index + 1 ); // moved here from call-back
226 // back-end. In WinBoard called by call-back, but could be called directly by SetIfExists?
231 if( OnlyCurrentPositionChanged() ) {
232 /* Only "cursor" changed, no need to update memo content */
234 else if( OneMoveAppended() ) {
235 AppendMoveToMemo( currCurrent );
238 RefreshMemoContent();
241 MemoContentUpdated();
246 MoveHistorySet (char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo)
248 /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
250 currMovelist = movelist;
253 currCurrent = current;
256 if(MoveHistoryDialogExists())
257 UpdateMoveHistory(); // [HGM] call this directly, in stead of through call-back