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