Merge commit 'master-20091122' into gtk
[xboard.git] / xhistory.c
1 /*
2  * xhistory.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000,2009 Free Software Foundation, Inc.
5  * ------------------------------------------------------------------------
6  *
7  * GNU XBoard is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * GNU XBoard is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.  *
19  *
20  *------------------------------------------------------------------------
21  ** See the file ChangeLog for a revision history.  */
22
23 #include "config.h"
24
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #if STDC_HEADERS
32 # include <stdlib.h>
33 # include <string.h>
34 #else /* not STDC_HEADERS */
35 extern char *getenv();
36 # if HAVE_STRING_H
37 #  include <string.h>
38 # else /* not HAVE_STRING_H */
39 #  include <strings.h>
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
42
43 #if HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47 #include "common.h"
48 #include "frontend.h"
49 #include "backend.h"
50 #include "xboard.h"
51 #include "xhistory.h"
52 #include "gettext.h"
53
54 #ifdef ENABLE_NLS
55 # define  _(s) gettext (s)
56 # define N_(s) gettext_noop (s)
57 #else
58 # define  _(s) (s)
59 # define N_(s)  s
60 #endif
61
62 extern GtkWidget               *GUI_History;
63 extern GtkListStore            *LIST_MoveHistory;
64
65 String dots=" ... ";
66 Position gameHistoryX, gameHistoryY;
67 Dimension gameHistoryW;
68
69 void
70 HistoryPopDown(object, user_data)
71      GtkObject *object;
72      gpointer user_data;
73 {
74   gtk_widget_hide (GUI_History);
75   return;
76 }
77
78 void HistoryMoveProc(Widget w, XtPointer closure, XtPointer call_data)
79 {
80     int to;
81     /*
82     XawListReturnStruct *R = (XawListReturnStruct *) call_data;
83     if (w == hist->mvn || w == hist->mvw) {
84       to=2*R->list_index-1;
85       ToNrEvent(to);
86     }
87     else if (w == hist->mvb) {
88       to=2*R->list_index;
89       ToNrEvent(to);
90     }
91     */
92 }
93
94
95 void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current)
96 {
97   int i,b,m;
98   char movewhite[2*MOVE_LEN],moveblack[2*MOVE_LEN],move[2*MOVE_LEN];
99   GtkTreeIter iter;
100
101   /* TODO need to add highlights for current move */
102   /* TODO need to add navigation by keyboard or mouse (double click on move) */
103
104   /* first clear everything, do we need this? */
105   gtk_list_store_clear(LIST_MoveHistory);
106
107   /* copy move list into history window */
108
109   /* go through all moves */
110   for(i=0;i<last;i++) 
111     {
112       /* test if there is a move */
113       if(movelist[i][0]) 
114         {
115           /* only copy everything before a  ' ' */
116           char* p = strchr(movelist[i], ' ');
117           if (p) 
118             {
119               strncpy(move, movelist[i], p-movelist[i]);
120               move[p-movelist[i]] = NULLCHAR;
121             } 
122           else 
123             {
124               strcpy(move,movelist[i]);
125             }
126         } 
127       else
128         strcpy(move,dots);
129       
130       if((i%2)==0) 
131         {
132           /* white move */
133           strcpy(movewhite,move);
134         }
135       else
136         {
137           /* black move */
138           strcpy(moveblack,move);
139
140           /* save move */
141           gtk_list_store_append (LIST_MoveHistory, &iter);
142           gtk_list_store_set (LIST_MoveHistory, &iter,
143                               0, i,
144                               1, movewhite,
145                               2, moveblack,
146                               -1);
147
148           strcpy(movewhite,"");
149           strcpy(moveblack,"");
150         };
151     }
152   /* check if ther is a white move left */
153   if(movewhite[0])
154     {
155       i++;
156       strcpy(moveblack,"");
157       /* save move */
158       gtk_list_store_append (LIST_MoveHistory, &iter);
159       gtk_list_store_set (LIST_MoveHistory, &iter,
160                           0, i,
161                           1, movewhite,
162                           2, moveblack,
163                           -1);
164     };
165   
166   return;
167 }
168
169 void HistoryCreate()
170 {
171     String trstr=
172              "<Key>Up: BackwardProc() \n \
173              <Key>Left: BackwardProc() \n \
174              <Key>Down: ForwardProc() \n \
175              <Key>Right: ForwardProc() \n";
176
177     return;
178     /*-------- create the widgets ---------------*/
179 //    j = 0;
180 //    XtSetArg(args[j], XtNresizable, True);  j++;
181 //    XtSetArg(args[j], XtNallowShellResize, True);  j++;
182 //#if TOPLEVEL
183 //    hist->sh =
184 //      XtCreatePopupShell(_("Move list"), topLevelShellWidgetClass,
185 //                       shellWidget, args, j);
186 //#else
187 //    hist->sh =
188 //      XtCreatePopupShell(_("Move list"), transientShellWidgetClass,
189 //                       shellWidget, args, j);
190 //#endif
191 //    j = 0;
192 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
193 //    XtSetArg(args[j], XtNdefaultDistance, 0);  j++;
194 //      layout =
195 //      XtCreateManagedWidget(layoutName, formWidgetClass, hist->sh,
196 //                          args, j);
197 //
198 //    j = 0;
199 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
200 //    XtSetArg(args[j], XtNresizable, True);  j++;
201 //
202 //    form =
203 //      XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
204 //     j=0;
205 //
206 //    j = 0;
207 //
208 //    XtSetArg(args[j], XtNtop, XtChainTop);  j++;
209 //    XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
210 //    XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
211 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
212 //
213 //    XtSetArg(args[j], XtNborderWidth, 1); j++;
214 //    XtSetArg(args[j], XtNresizable, False);  j++;
215 //    XtSetArg(args[j], XtNallowVert, True); j++;
216 //    XtSetArg(args[j], XtNallowHoriz, True);  j++;
217 //    XtSetArg(args[j], XtNforceBars, False); j++;
218 //    XtSetArg(args[j], XtNheight, 280); j++;
219 //    hist->viewport =
220 //      XtCreateManagedWidget("viewport", viewportWidgetClass,
221 //                          form, args, j);
222 //    j=0;
223 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
224 //    XtSetArg(args[j], XtNorientation,XtorientHorizontal);j++;
225 //    hist->vbox =
226 //      XtCreateManagedWidget("vbox", formWidgetClass, hist->viewport, args, j);
227 //
228 //    j=0;
229 //    XtSetArg(args[j], XtNtop, XtChainTop);  j++;
230 //    XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
231 //    XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
232 //    XtSetArg(args[j], XtNright, XtChainLeft);  j++;
233 //
234 //    XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
235 //    XtSetArg(args[j], XtNforceColumns, True);  j++;
236 //    XtSetArg(args[j], XtNverticalList, True);  j++;
237 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
238 //    XtSetArg(args[j], XtNresizable,True);j++;
239 //    XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
240 //    hist->mvn = XtCreateManagedWidget("movesn", listWidgetClass,
241 //                                    hist->vbox, args, j);
242 //    XtAddCallback(hist->mvn, XtNcallback, HistoryMoveProc, (XtPointer) hist);
243 //
244 //    j=0;
245 //    XtSetArg(args[j], XtNtop, XtChainTop);  j++;
246 //    XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
247 //    XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
248 //    XtSetArg(args[j], XtNright, XtRubber);  j++;
249 //
250 //    XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
251 //    XtSetArg(args[j], XtNforceColumns, True);  j++;
252 //    XtSetArg(args[j], XtNverticalList, True);  j++;
253 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
254 //    XtSetArg(args[j], XtNresizable,True);j++;
255 //    XtSetArg(args[j], XtNfromHoriz, hist->mvn);  j++;
256 //    hist->mvw = XtCreateManagedWidget("movesw", listWidgetClass,
257 //                                    hist->vbox, args, j);
258 //    XtAddCallback(hist->mvw, XtNcallback, HistoryMoveProc, (XtPointer) hist);
259 //
260 //    j=0;
261 //    XtSetArg(args[j], XtNtop, XtChainTop);  j++;
262 //    XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
263 //    XtSetArg(args[j], XtNleft, XtRubber);  j++;
264 //    XtSetArg(args[j], XtNright,  XtRubber);  j++;
265 //
266 //    XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
267 //    XtSetArg(args[j], XtNforceColumns, True);  j++;
268 //    XtSetArg(args[j], XtNverticalList, True);  j++;
269 //    XtSetArg(args[j], XtNborderWidth, 0); j++;
270 //    XtSetArg(args[j], XtNresizable,True);j++;
271 //    XtSetArg(args[j], XtNfromHoriz, hist->mvw);  j++;
272 //    hist->mvb = XtCreateManagedWidget("movesb", listWidgetClass,
273 //                                    hist->vbox, args, j);
274 //    XtAddCallback(hist->mvb, XtNcallback, HistoryMoveProc, (XtPointer) hist);
275 //
276 //    j=0;
277 //    XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
278 //    XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
279 //    XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
280 //    XtSetArg(args[j], XtNright, XtChainLeft);  j++;
281 //    XtSetArg(args[j], XtNfromVert, hist->viewport);  j++;
282 //    b_close= XtCreateManagedWidget(_("Close"), commandWidgetClass,
283 //                                 form, args, j);
284 //    XtAddCallback(b_close, XtNcallback, HistoryPopDown, (XtPointer) 0);
285 //
286 //    XtAugmentTranslations(hist->sh,XtParseTranslationTable (trstr));
287 //
288 //    XtRealizeWidget(hist->sh);
289 //    CatchDeleteWindow(hist->sh, "HistoryPopDown");
290 //
291 //    for(i=1;i<hist->aNr;i++){
292 //      strcpy(hist->white[i],dots);
293 //      strcpy(hist->black[i],"");
294 //     }
295 //
296 //  // [HGM] restore old position
297 //  j = 0;
298 //  XtSetArg(args[j], XtNx, &gameHistoryX);  j++;
299 //  XtSetArg(args[j], XtNy, &gameHistoryY);  j++;
300 //  XtSetArg(args[j], XtNwidth, &gameHistoryW);  j++;
301 //  XtGetValues(shellWidget, args, j);
302 //  j = 0;
303 //  XtSetArg(args[j], XtNx, gameHistoryX + gameHistoryW);  j++;
304 //  XtSetArg(args[j], XtNy, gameHistoryY);  j++;
305 //  XtSetValues(hist->sh, args, j);
306 //    XtRealizeWidget(hist->sh);
307 //
308 //    return hist->sh;
309 }
310
311 void
312 HistoryPopUp()
313 {
314   //  if(!hist) HistoryCreate();
315
316   gtk_widget_show (GUI_History);
317   
318   return;
319 }
320
321
322 void
323 HistoryShowProc(object, user_data)
324      GtkObject *object;
325      gpointer user_data;
326 {
327   HistoryCreate();
328   HistoryPopUp();
329   //TODO:  ToNrEvent(currentMove);
330
331   return;
332 }
333