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