updated year in copyright info
[xboard.git] / xhistory.c
1 /*
2  * xhistory.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000, 2009, 2010 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 <stdio.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 # include <string.h>
33 #else /* not STDC_HEADERS */
34 extern char *getenv();
35 # if HAVE_STRING_H
36 #  include <string.h>
37 # else /* not HAVE_STRING_H */
38 #  include <strings.h>
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
41
42 #if HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45
46 #include <X11/Intrinsic.h>
47 #include <X11/StringDefs.h>
48 #include <X11/Shell.h>
49 #include <X11/Xaw/Dialog.h>
50 #include <X11/Xaw/Form.h>
51 #include <X11/Xaw/List.h>
52 #include <X11/Xaw/Label.h>
53 #include <X11/Xaw/SimpleMenu.h>
54 #include <X11/Xaw/SmeBSB.h>
55 #include <X11/Xaw/SmeLine.h>
56 #include <X11/Xaw/Box.h>
57 #include <X11/Xaw/Paned.h>
58 #include <X11/Xaw/MenuButton.h>
59 #include <X11/cursorfont.h>
60 #include <X11/Xaw/Text.h>
61 #include <X11/Xaw/AsciiText.h>
62 #include <X11/Xaw/Viewport.h>
63
64 #include "common.h"
65 #include "frontend.h"
66 #include "backend.h"
67 #include "xboard.h"
68 #include "xhistory.h"
69 #include "gettext.h"
70
71 #ifdef ENABLE_NLS
72 # define  _(s) gettext (s)
73 # define N_(s) gettext_noop (s)
74 #else
75 # define  _(s) (s)
76 # define N_(s)  s
77 #endif
78
79 #define _LL_ 100
80
81 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget, historyShell;
82 extern Display *xDisplay;
83 extern int squareSize;
84 extern Pixmap xMarkPixmap;
85 extern char *layoutName;
86
87 struct History{
88   String *Nr,*white,*black;
89   int     aNr;  /* space actually alocated */
90   Widget mvn,mvw,mvb,vbox,viewport,sh;
91   char Up;
92 };
93
94 struct History *hist=0;
95 String dots=" ... ";
96 Position gameHistoryX, gameHistoryY;
97 Dimension gameHistoryW, gameHistoryH;
98
99 void
100 HistoryPopDown(w, client_data, call_data)
101      Widget w;
102      XtPointer client_data, call_data;
103 {
104   Arg args[16];
105   int j;
106   if(hist) {
107
108     // [HGM] remember old position
109     j = 0;
110     XtSetArg(args[j], XtNx, &gameHistoryX);  j++;
111     XtSetArg(args[j], XtNy, &gameHistoryY);  j++;
112     XtSetArg(args[j], XtNwidth, &gameHistoryW);  j++;
113     XtSetArg(args[j], XtNheight, &gameHistoryH);  j++;
114     XtGetValues(hist->sh, args, j);
115     wpMoveHistory.x = gameHistoryX - 4;
116     wpMoveHistory.y = gameHistoryY - 23;
117     wpMoveHistory.width  = gameHistoryW;
118     wpMoveHistory.height = gameHistoryH;
119
120     XtPopdown(hist->sh);
121     hist->Up=False;
122   }
123   j=0;
124   XtSetArg(args[j], XtNleftBitmap, None); j++;
125   XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Move History"),
126                 args, j);
127 }
128
129 void HistoryMoveProc(Widget w, XtPointer closure, XtPointer call_data)
130 {
131     int to;
132     XawListReturnStruct *R = (XawListReturnStruct *) call_data;
133     if (w == hist->mvn || w == hist->mvw) {
134       to=2*R->list_index-1;
135       ToNrEvent(to);
136     }
137     else if (w == hist->mvb) {
138       to=2*R->list_index;
139       ToNrEvent(to);
140     }
141 }
142
143 void HistoryAlloc(int len){
144   int i;
145   if(hist){
146     free(hist->Nr[0]);free(hist->white[0]);free(hist->black[0]);
147     free(hist->Nr);free(hist->white);free(hist->black);
148   }
149   else{
150     hist=(struct History*)malloc(sizeof(struct History));
151   }
152     hist->aNr=len;
153     hist->Nr=(String*)malloc(hist->aNr*sizeof(String*));
154     hist->white=(String*)malloc(hist->aNr*sizeof(String*));
155     hist->black=(String*)malloc(hist->aNr*sizeof(String*));
156
157     hist->Nr[0]=(String)malloc(hist->aNr*6);
158     hist->white[0]=(String)malloc(hist->aNr*MOVE_LEN);
159     hist->black[0]=(String)malloc(hist->aNr*MOVE_LEN);
160
161       sprintf(hist->Nr[0],"    ");
162       sprintf(hist->white[0],_("White "));
163       sprintf(hist->black[0],_("Black "));
164     for(i=1;i<hist->aNr;i++){
165       hist->Nr[i]= hist->Nr[i-1]+6;
166       hist->white[i]= hist->white[i-1]+MOVE_LEN;
167       hist->black[i]= hist->black[i-1]+MOVE_LEN;
168       sprintf(hist->Nr[i],"%i.",i);
169       sprintf(hist->white[i],"-----");
170       sprintf(hist->black[i],"-----");
171      }
172 }
173
174
175 /* Find empty space inside vbox form widget and redistribute it amongst
176    the list widgets inside it. */
177 /* This version sort of works */
178 void
179 HistoryFill()
180 {
181   Dimension w, bw;
182   long extra;
183   Position x, x1, x2;
184   int j, dd;
185   Arg args[16];
186
187   j = 0;
188   XtSetArg(args[j], XtNx, &x);  j++;
189   XtSetArg(args[j], XtNwidth, &w);  j++;
190   XtSetArg(args[j], XtNborderWidth, &bw);  j++;
191   XtGetValues(hist->mvb, args, j);
192   x1 = x + w + 2*bw;
193
194   j = 0;
195   XtSetArg(args[j], XtNwidth, &w);  j++;
196   XtSetArg(args[j], XtNdefaultDistance, &dd);  j++;
197   XtGetValues(hist->vbox, args, j);
198   x2 = w - dd;
199
200   extra = x2 - x1;
201   if (extra < 0) {
202     extra = -((-extra)/2);
203   } else {
204     extra = extra/2;
205   }
206
207   j = 0;
208   XtSetArg(args[j], XtNwidth, &w);  j++;
209   XtGetValues(hist->mvw, args, j);
210   w += extra;
211   j = 0;
212   XtSetArg(args[j], XtNwidth, w);  j++;
213   XtSetValues(hist->mvw, args, j);
214
215   j = 0;
216   XtSetArg(args[j], XtNwidth, &w);  j++;
217   XtGetValues(hist->mvb, args, j);
218   w += extra;
219   j = 0;
220   XtSetArg(args[j], XtNwidth, w);  j++;
221   XtSetValues(hist->mvb, args, j);
222 }
223
224 void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current){
225   int i,b,m;
226   if(hist){
227     if(last >= hist->aNr) HistoryAlloc(last+_LL_);
228     for(i=0;i<last;i++) {
229       if((i%2)==0) {
230         if(movelist[i][0]) {
231           char* p = strchr(movelist[i], ' ');
232           if (p) {
233             strncpy(hist->white[i/2+1], movelist[i], p-movelist[i]);
234             hist->white[i/2+1][p-movelist[i]] = NULLCHAR;
235           } else {
236             strcpy(hist->white[i/2+1],movelist[i]);
237           }
238         } else {
239           strcpy(hist->white[i/2+1],dots);
240         }
241       } else {
242         if(movelist[i][0]) {
243           char* p = strchr(movelist[i], ' ');
244           if (p) {
245             strncpy(hist->black[i/2+1], movelist[i], p-movelist[i]);
246             hist->black[i/2+1][p-movelist[i]] = NULLCHAR;
247           } else {
248             strcpy(hist->black[i/2+1],movelist[i]);
249           }
250         } else {
251           strcpy(hist->black[i/2+1],"");
252         }
253       }
254     }
255     strcpy(hist->black[last/2+1],"");
256     b=first/2;
257     m=(last+3)/2-b;
258     XawFormDoLayout(hist->vbox, False);
259     XawListChange(hist->mvn,hist->Nr+b,m,0,True);
260     XawListChange(hist->mvw,hist->white+b,m,0,True);
261     XawListChange(hist->mvb,hist->black+b,m,0,True);
262     HistoryFill();
263     XawFormDoLayout(hist->vbox, True);
264     if(current<0){
265       XawListUnhighlight(hist->mvw);
266       XawListUnhighlight(hist->mvb);
267     }
268     else if((current%2)==0){
269       XawListHighlight(hist->mvw, current/2+1);
270       XawListUnhighlight(hist->mvb);
271     }
272     else{
273       XawListUnhighlight(hist->mvw);
274       if(current) XawListHighlight(hist->mvb, current/2+1);
275       else XawListUnhighlight(hist->mvb);
276     }
277   }
278   EvalGraphSet( first, last, current, pvInfoList ); // piggy-backed
279 }
280
281 Widget HistoryCreate()
282 {
283     Arg args[16];
284     int i,j;
285
286     Widget layout,form,b_close;
287     String trstr=
288              "<Key>Up: BackwardProc() \n \
289              <Key>Left: BackwardProc() \n \
290              <Key>Down: ForwardProc() \n \
291              <Key>Right: ForwardProc() \n";
292     /*--- allocate memory for move-strings ---*/
293     HistoryAlloc(_LL_);
294
295     /*-------- create the widgets ---------------*/
296     j = 0;
297     XtSetArg(args[j], XtNresizable, True);  j++;
298     XtSetArg(args[j], XtNallowShellResize, True);  j++;
299 #if TOPLEVEL
300     hist->sh = historyShell =
301       XtCreatePopupShell(_("Move list"), topLevelShellWidgetClass,
302                          shellWidget, args, j);
303 #else
304     hist->sh = historyShell =
305       XtCreatePopupShell(_("Move list"), transientShellWidgetClass,
306                          shellWidget, args, j);
307 #endif
308     j = 0;
309     XtSetArg(args[j], XtNborderWidth, 0); j++;
310     XtSetArg(args[j], XtNdefaultDistance, 0);  j++;
311       layout =
312       XtCreateManagedWidget(layoutName, formWidgetClass, hist->sh,
313                             args, j);
314
315     j = 0;
316     XtSetArg(args[j], XtNborderWidth, 0); j++;
317     XtSetArg(args[j], XtNresizable, True);  j++;
318
319     form =
320       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
321
322     j = 0;
323     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
324     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
325     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
326     XtSetArg(args[j], XtNright, XtChainRight);  j++;
327
328     XtSetArg(args[j], XtNborderWidth, 1); j++;
329     XtSetArg(args[j], XtNresizable, False);  j++;
330     XtSetArg(args[j], XtNallowVert, True); j++;
331     XtSetArg(args[j], XtNallowHoriz, True);  j++;
332     XtSetArg(args[j], XtNforceBars, False); j++;
333     XtSetArg(args[j], XtNheight, 280); j++;
334     hist->viewport =
335       XtCreateManagedWidget("viewport", viewportWidgetClass,
336                             form, args, j);
337     j=0;
338     XtSetArg(args[j], XtNborderWidth, 0); j++;
339     XtSetArg(args[j], XtNorientation,XtorientHorizontal);j++;
340     hist->vbox =
341       XtCreateManagedWidget("vbox", formWidgetClass, hist->viewport, args, j);
342
343     j=0;
344     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
345     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
346     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
347     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
348
349     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
350     XtSetArg(args[j], XtNforceColumns, True);  j++;
351     XtSetArg(args[j], XtNverticalList, True);  j++;
352     XtSetArg(args[j], XtNborderWidth, 0); j++;
353     XtSetArg(args[j], XtNresizable,True);j++;
354     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
355     hist->mvn = XtCreateManagedWidget("movesn", listWidgetClass,
356                                       hist->vbox, args, j);
357     XtAddCallback(hist->mvn, XtNcallback, HistoryMoveProc, (XtPointer) hist);
358
359     j=0;
360     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
361     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
362     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
363     XtSetArg(args[j], XtNright, XtRubber);  j++;
364
365     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
366     XtSetArg(args[j], XtNforceColumns, True);  j++;
367     XtSetArg(args[j], XtNverticalList, True);  j++;
368     XtSetArg(args[j], XtNborderWidth, 0); j++;
369     XtSetArg(args[j], XtNresizable,True);j++;
370     XtSetArg(args[j], XtNfromHoriz, hist->mvn);  j++;
371     hist->mvw = XtCreateManagedWidget("movesw", listWidgetClass,
372                                       hist->vbox, args, j);
373     XtAddCallback(hist->mvw, XtNcallback, HistoryMoveProc, (XtPointer) hist);
374
375     j=0;
376     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
377     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
378     XtSetArg(args[j], XtNleft, XtRubber);  j++;
379     XtSetArg(args[j], XtNright,  XtRubber);  j++;
380
381     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
382     XtSetArg(args[j], XtNforceColumns, True);  j++;
383     XtSetArg(args[j], XtNverticalList, True);  j++;
384     XtSetArg(args[j], XtNborderWidth, 0); j++;
385     XtSetArg(args[j], XtNresizable,True);j++;
386     XtSetArg(args[j], XtNfromHoriz, hist->mvw);  j++;
387     hist->mvb = XtCreateManagedWidget("movesb", listWidgetClass,
388                                       hist->vbox, args, j);
389     XtAddCallback(hist->mvb, XtNcallback, HistoryMoveProc, (XtPointer) hist);
390
391     j=0;
392     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
393     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
394     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
395     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
396     XtSetArg(args[j], XtNfromVert, hist->viewport);  j++;
397     b_close= XtCreateManagedWidget(_("Close"), commandWidgetClass,
398                                    form, args, j);
399     XtAddCallback(b_close, XtNcallback, HistoryPopDown, (XtPointer) 0);
400
401     XtAugmentTranslations(hist->sh,XtParseTranslationTable (trstr));
402
403     XtRealizeWidget(hist->sh);
404     CatchDeleteWindow(hist->sh, "HistoryPopDown");
405
406     for(i=1;i<hist->aNr;i++){
407       strcpy(hist->white[i],dots);
408       strcpy(hist->black[i],"");
409      }
410
411     if(wpMoveHistory.width > 0) {
412       gameHistoryW = wpMoveHistory.width;
413       gameHistoryH = wpMoveHistory.height;
414       gameHistoryX = wpMoveHistory.x;
415       gameHistoryY = wpMoveHistory.y;
416     }
417
418   // [HGM] restore old position
419   if(gameHistoryW > 0) {
420   j = 0;
421     XtSetArg(args[j], XtNx, gameHistoryX);  j++;
422   XtSetArg(args[j], XtNy, gameHistoryY);  j++;
423     XtSetArg(args[j], XtNwidth, gameHistoryW);  j++;
424     XtSetArg(args[j], XtNheight, gameHistoryH);  j++;
425   XtSetValues(hist->sh, args, j);
426   }
427     XtRealizeWidget(hist->sh);
428
429     return hist->sh;
430 }
431
432 void
433 HistoryPopUp()
434 {
435   Arg args[16];
436   int j;
437
438   if(!hist) HistoryCreate();
439
440   XtPopup(hist->sh, XtGrabNone);
441
442   j=0;
443   XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
444   XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Move History"),
445                 args, j);
446   hist->Up=True;
447 }
448
449
450 void
451 HistoryShowProc(w, event, prms, nprms)
452      Widget w;
453      XEvent *event;
454      String *prms;
455      Cardinal *nprms;
456 {
457   if (!hist) {
458     HistoryCreate();
459     HistoryPopUp();
460   } else if (hist->Up) {
461     HistoryPopDown(0,0,0);
462   } else {
463     HistoryPopUp();
464   }
465   ToNrEvent(currentMove);
466 }
467
468 Boolean
469 MoveHistoryIsUp()
470 {
471   return hist && hist->Up;
472 }