Updated copyright notice to 2012
[xboard.git] / history.c
1 /*
2  * Move history for WinBoard
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  * back-end part split off by HGM
6  *
7  * Copyright 2005 Alessandro Scotti
8  *
9  * Enhancements Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
10  *
11  * ------------------------------------------------------------------------
12  *
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.
17  *
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.
22  *
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/. 
25  *
26  * ------------------------------------------------------------------------
27  ** See the file ChangeLog for a revision history.  */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "common.h"
36 #include "frontend.h"
37 #include "backend.h"
38
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)
44
45 /* templates for front-end entry point to allow inquiring about front-end state */
46 Boolean MoveHistoryDialogExists P((void));
47 Boolean MoveHistoryIsUp P((void));
48
49 /* Module globals */
50 typedef char MoveHistoryString[ MOVE_LEN*2 ];
51
52 static int lastFirst = 0;
53 static int lastLast = 0;
54 static int lastCurrent = -1;
55 static int lastGames;
56
57 static char lastLastMove[ MOVE_LEN ];
58
59 static MoveHistoryString * currMovelist;
60 static ChessProgramStats_Move * currPvInfo;
61 static int currFirst = 0;
62 static int currLast = 0;
63 static int currCurrent = -1;
64
65 typedef struct {
66     int memoOffset;
67     int memoLength;
68 } HistoryMove;
69
70 static HistoryMove histMoves[ MAX_MOVES ];
71
72 /* Note: in the following code a "Memo" is a Rich Edit control (it's Delphi lingo) */
73
74 // back-end after replacing Windows data-types by equivalents
75 static Boolean OnlyCurrentPositionChanged()
76 {
77     Boolean result = FALSE;
78
79     if( lastFirst >= 0 &&
80         lastLast >= lastFirst &&
81         lastCurrent >= lastFirst && 
82         currFirst == lastFirst &&
83         currLast == lastLast &&
84         currCurrent >= 0 &&
85         lastGames == storedGames )
86     {
87         result = TRUE;
88
89         /* Special case: last move changed */
90         if( currCurrent == currLast-1 ) {
91             if( strcmp( currMovelist[currCurrent], lastLastMove ) != 0 ) {
92                 result = FALSE;
93             }
94         }
95     }
96
97     return result;
98 }
99
100 // back-end, after replacing Windows data types
101 static Boolean OneMoveAppended()
102 {
103     Boolean result = FALSE;
104
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 )
112     {
113         result = TRUE;
114     }
115
116     return result;
117 }
118
119 // back-end, now that color and font-style are passed as numbers
120 static void AppendMoveToMemo( int index )
121 {
122     char buf[64];
123
124     if( index < 0 || index >= MAX_MOVES ) {
125         return;
126     }
127
128     buf[0] = '\0';
129
130     /* Move number */
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
134     }
135
136     /* Move text */
137     safeStrCpy( buf, SavePart( currMovelist[index]) , sizeof( buf)/sizeof( buf[0]) );
138     strcat( buf, " " );
139
140     histMoves[index].memoOffset = AppendToHistoryMemo( buf, 0, 0 );
141     histMoves[index].memoLength = strlen(buf)-1;
142
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 );
149
150         AppendToHistoryMemo( buf, 0, 1); // [HGM] 1 means gray
151     }
152 }
153
154 // back-end
155 void RefreshMemoContent()
156 {
157     int i;
158
159     ClearHistoryMemo();
160
161     for( i=currFirst; i<currLast; i++ ) {
162         AppendMoveToMemo( i );
163     }
164 }
165
166 // back-end part taken out of HighlightMove to determine character positions
167 static void DoHighlight(int index, int onoff)
168 {
169     if( index >= 0 && index < MAX_MOVES ) {
170         HighlightMove( histMoves[index].memoOffset, 
171             histMoves[index].memoOffset + histMoves[index].memoLength, onoff );
172     }
173 }
174
175 // back-end, now that a wrapper is provided for the front-end code to do the actual scrolling
176 void MemoContentUpdated()
177 {
178     int caretPos;
179
180     if(lastCurrent <= currLast) DoHighlight( lastCurrent, FALSE );
181
182     lastFirst = currFirst;
183     lastLast = currLast;
184     lastCurrent = currCurrent;
185     lastGames = storedGames;
186     lastLastMove[0] = '\0';
187
188     if( lastLast > 0 ) {
189       safeStrCpy( lastLastMove, SavePart( currMovelist[lastLast-1] ) , sizeof( lastLastMove)/sizeof( lastLastMove[0]) );
190     }
191
192     /* Deselect any text, move caret to end of memo */
193     if( currCurrent >= 0 ) {
194         caretPos = histMoves[currCurrent].memoOffset + histMoves[currCurrent].memoLength;
195     }
196     else {
197         caretPos = -1;
198     }
199
200     ScrollToCurrent(caretPos);
201     DoHighlight( currCurrent, TRUE ); // [HGM] moved last, because in X some scrolling methods spoil highlighting
202 }
203
204 // back-end. Must be called as double-click call-back on move-history text edit
205 void FindMoveByCharIndex( int char_index )
206 {
207     int index;
208
209     for( index=currFirst; index<currLast; index++ ) {
210         if( char_index >= histMoves[index].memoOffset &&
211             char_index <  (histMoves[index].memoOffset + histMoves[index].memoLength) )
212         {
213             ToNrEvent( index + 1 ); // moved here from call-back
214         }
215     }
216 }
217
218 // back-end. In WinBoard called by call-back, but could be called directly by SetIfExists?
219 void UpdateMoveHistory()
220 {
221         /* Update the GUI */
222         if( OnlyCurrentPositionChanged() ) {
223             /* Only "cursor" changed, no need to update memo content */
224         }
225         else if( OneMoveAppended() ) {
226             AppendMoveToMemo( currCurrent );
227         }
228         else {
229             RefreshMemoContent();
230         }
231
232         MemoContentUpdated();
233 }
234
235 // back-end
236 void MoveHistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo )
237 {
238     /* [AS] Danger! For now we rely on the movelist parameter being a static variable! */
239
240     currMovelist = movelist;
241     currFirst = first;
242     currLast = last;
243     currCurrent = current;
244     currPvInfo = pvInfo;
245
246     if(MoveHistoryDialogExists())
247         UpdateMoveHistory(); // [HGM] call this directly, in stead of through call-back
248 }
249