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