allowe parsing / disambiguation of SAN moves like Xe4 in certain situations
[xboard.git] / xgamelist.c
1 /*
2  * xgamelist.c -- Game list window, part of X front end for XBoard
3  *
4  * Copyright 1995,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/cursorfont.h>
50 #if USE_XAW3D
51 #include <X11/Xaw3d/Dialog.h>
52 #include <X11/Xaw3d/Form.h>
53 #include <X11/Xaw3d/List.h>
54 #include <X11/Xaw3d/Label.h>
55 #include <X11/Xaw3d/SimpleMenu.h>
56 #include <X11/Xaw3d/SmeBSB.h>
57 #include <X11/Xaw3d/SmeLine.h>
58 #include <X11/Xaw3d/Box.h>
59 #include <X11/Xaw3d/MenuButton.h>
60 #include <X11/Xaw3d/Text.h>
61 #include <X11/Xaw3d/AsciiText.h>
62 #include <X11/Xaw3d/Viewport.h>
63 #else
64 #include <X11/Xaw/Dialog.h>
65 #include <X11/Xaw/Form.h>
66 #include <X11/Xaw/List.h>
67 #include <X11/Xaw/Label.h>
68 #include <X11/Xaw/SimpleMenu.h>
69 #include <X11/Xaw/SmeBSB.h>
70 #include <X11/Xaw/SmeLine.h>
71 #include <X11/Xaw/Box.h>
72 #include <X11/Xaw/MenuButton.h>
73 #include <X11/Xaw/Text.h>
74 #include <X11/Xaw/AsciiText.h>
75 #include <X11/Xaw/Viewport.h>
76 #endif
77
78 #include "common.h"
79 #include "frontend.h"
80 #include "backend.h"
81 #include "xboard.h"
82 #include "xgamelist.h"
83 #include "gettext.h"
84
85 #ifdef ENABLE_NLS
86 # define  _(s) gettext (s)
87 # define N_(s) gettext_noop (s)
88 #else
89 # define  _(s) (s)
90 # define N_(s)  s
91 #endif
92
93
94 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
95 extern Display *xDisplay;
96 extern int squareSize;
97 extern Pixmap xMarkPixmap;
98 extern char *layoutName;
99
100 char gameListTranslations[] =
101   "<Btn1Up>(2): LoadSelectedProc() \n \
102    <Key>Return: LoadSelectedProc() \n";
103
104 typedef struct {
105     Widget shell;
106     Position x, y;
107     Dimension w, h;
108     Boolean up;
109     FILE *fp;
110     char *filename;
111     char **strings;
112 } GameListClosure;
113
114 static Arg layoutArgs[] = {
115     { XtNborderWidth, 0 },
116     { XtNdefaultDistance, 0 }
117 };
118
119 Widget
120 GameListCreate(name, callback, client_data)
121      char *name;
122      XtCallbackProc callback;
123      XtPointer client_data;
124 {
125     Arg args[16];
126     Widget shell, form, viewport, listwidg, layout;
127     Widget b_load, b_loadprev, b_loadnext, b_close;
128     Dimension fw_width;
129     int j;
130     GameListClosure *glc = (GameListClosure *) client_data;
131
132     j = 0;
133     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
134     XtGetValues(formWidget, args, j);
135
136     j = 0;
137     XtSetArg(args[j], XtNresizable, True);  j++;
138     XtSetArg(args[j], XtNallowShellResize, True);  j++;
139 #if TOPLEVEL
140     shell =
141       XtCreatePopupShell(name, topLevelShellWidgetClass,
142                          shellWidget, args, j);
143 #else
144     shell =
145       XtCreatePopupShell(name, transientShellWidgetClass,
146                          shellWidget, args, j);
147 #endif
148     layout =
149       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
150                             layoutArgs, XtNumber(layoutArgs));
151     j = 0;
152     XtSetArg(args[j], XtNborderWidth, 0); j++;
153     form =
154       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
155
156     j = 0;
157     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
158     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
159     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
160     XtSetArg(args[j], XtNright, XtChainRight);  j++;
161     XtSetArg(args[j], XtNresizable, False);  j++;
162     XtSetArg(args[j], XtNwidth, fw_width);  j++;
163     XtSetArg(args[j], XtNallowVert, True); j++;
164     viewport =
165       XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
166
167     j = 0;
168     XtSetArg(args[j], XtNlist, glc->strings);  j++;
169     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
170     XtSetArg(args[j], XtNforceColumns, True);  j++;
171     XtSetArg(args[j], XtNverticalList, True);  j++;
172     listwidg = 
173       XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
174     XawListHighlight(listwidg, 0);
175     XtAugmentTranslations(listwidg,
176                           XtParseTranslationTable(gameListTranslations));
177
178     j = 0;
179     XtSetArg(args[j], XtNfromVert, viewport);  j++;
180     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
181     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
182     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
183     XtSetArg(args[j], XtNright, XtChainLeft); j++;
184     b_load =
185       XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
186     XtAddCallback(b_load, XtNcallback, callback, client_data);
187
188     j = 0;
189     XtSetArg(args[j], XtNfromVert, viewport);  j++;
190     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
191     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
192     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
193     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
194     XtSetArg(args[j], XtNright, XtChainLeft); j++;
195     b_loadprev =
196       XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
197     XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
198
199     j = 0;
200     XtSetArg(args[j], XtNfromVert, viewport);  j++;
201     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
202     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
203     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
204     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
205     XtSetArg(args[j], XtNright, XtChainLeft); j++;
206     b_loadnext =
207       XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
208     XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
209
210     j = 0;
211     XtSetArg(args[j], XtNfromVert, viewport);  j++;
212     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
213     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
214     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
215     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
216     XtSetArg(args[j], XtNright, XtChainLeft); j++;
217     b_close =
218       XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
219     XtAddCallback(b_close, XtNcallback, callback, client_data);
220
221     if (glc->x == -1) {
222         Position y1;
223         Dimension h1;
224         int xx, yy;
225         Window junk;
226
227         j = 0;
228         XtSetArg(args[j], XtNheight, &h1); j++;
229         XtSetArg(args[j], XtNy, &y1); j++;
230         XtGetValues(boardWidget, args, j);
231         glc->w = fw_width * 3/4;
232         glc->h = squareSize * 3;
233
234         XSync(xDisplay, False);
235 #ifdef NOTDEF
236         /* This code seems to tickle an X bug if it is executed too soon
237            after xboard starts up.  The coordinates get transformed as if
238            the main window was positioned at (0, 0).
239         */
240         XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
241                           y1 + (h1 - glc->h + appData.borderYoffset) / 2,
242                           &glc->x, &glc->y);
243 #else /*!NOTDEF*/
244         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
245                               RootWindowOfScreen(XtScreen(shellWidget)),
246                               (fw_width - glc->w) / 2,
247                               y1 + (h1 - glc->h + appData.borderYoffset) / 2,
248                               &xx, &yy, &junk);
249         glc->x = xx;
250         glc->y = yy;
251 #endif /*!NOTDEF*/
252         if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
253     }
254     j = 0;
255     XtSetArg(args[j], XtNheight, glc->h);  j++;
256     XtSetArg(args[j], XtNwidth, glc->w);  j++;
257     XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset);  j++;
258     XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset);  j++;
259     XtSetValues(shell, args, j);
260
261     XtRealizeWidget(shell);
262     CatchDeleteWindow(shell, "GameListPopDown");
263
264     return shell;
265 }
266
267 void
268 GameListCallback(w, client_data, call_data)
269      Widget w;
270      XtPointer client_data, call_data;
271 {
272     String name;
273     Arg args[16];
274     int j;
275     Widget listwidg;
276     GameListClosure *glc = (GameListClosure *) client_data;
277     XawListReturnStruct *rs;
278     int index;
279
280     j = 0;
281     XtSetArg(args[j], XtNlabel, &name);  j++;
282     XtGetValues(w, args, j);
283
284     if (strcmp(name, _("close")) == 0) {
285         GameListPopDown();
286         return;
287     }
288     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
289     rs = XawListShowCurrent(listwidg);
290     if (strcmp(name, _("load")) == 0) {
291         index = rs->list_index;
292         if (index < 0) {
293             DisplayError(_("No game selected"), 0);
294             return;
295         }
296     } else if (strcmp(name, _("next")) == 0) {
297         index = rs->list_index + 1;
298         if (index >= ((ListGame *) gameList.tailPred)->number) {
299             DisplayError(_("Can't go forward any further"), 0);
300             return;
301         }
302         XawListHighlight(listwidg, index);
303     } else if (strcmp(name, _("prev")) == 0) {
304         index = rs->list_index - 1;
305         if (index < 0) {
306             DisplayError(_("Can't back up any further"), 0);
307             return;
308         }
309         XawListHighlight(listwidg, index);
310     }
311     if (cmailMsgLoaded) {
312         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
313     } else {
314         LoadGame(glc->fp, index + 1, glc->filename, True);
315     }
316 }
317
318 static GameListClosure *glc = NULL;
319
320 void
321 GameListPopUp(fp, filename)
322      FILE *fp;
323      char *filename;
324 {
325     Arg args[16];
326     int j, nstrings;
327     Widget listwidg;
328     ListGame *lg;
329     char **st;
330
331     if (glc == NULL) {
332         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
333         glc->x = glc->y = -1;
334     }
335
336     if (glc->strings != NULL) {
337         st = glc->strings;
338         while (*st) {
339             free(*st++);
340         }
341         free(glc->strings);
342     }
343
344     nstrings = ((ListGame *) gameList.tailPred)->number;
345     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
346     st = glc->strings;
347     lg = (ListGame *) gameList.head;
348     while (nstrings--) {
349         *st++ = GameListLine(lg->number, &lg->gameInfo);
350         lg = (ListGame *) lg->node.succ;
351      }
352     *st = NULL;
353
354     glc->fp = fp;
355
356     if (glc->filename != NULL) free(glc->filename);
357     glc->filename = StrSave(filename);
358
359     if (glc->shell == NULL) {
360         glc->shell = GameListCreate(filename, GameListCallback, glc); 
361     } else {
362         listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
363         XawListChange(listwidg, glc->strings, 0, 0, True);
364         XawListHighlight(listwidg, 0);
365         j = 0;
366         XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
367         XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
368         XtSetValues(glc->shell, args, j);
369     }
370
371     XtPopup(glc->shell, XtGrabNone);
372     glc->up = True;
373     j = 0;
374     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
375     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
376                 args, j);
377 }
378
379 void
380 GameListDestroy()
381 {
382     if (glc == NULL) return;
383     GameListPopDown();
384     if (glc->strings != NULL) {
385         char **st;
386         st = glc->strings;
387         while (*st) {
388             free(*st++);
389         }
390         free(glc->strings);
391     }
392     free(glc);
393     glc = NULL;
394 }
395
396 void
397 ShowGameListProc(w, event, prms, nprms)
398      Widget w;
399      XEvent *event;
400      String *prms;
401      Cardinal *nprms;
402 {
403     Arg args[16];
404     int j;
405
406     if (glc == NULL) {
407         DisplayError(_("There is no game list"), 0);
408         return;
409     }
410     if (glc->up) {
411         GameListPopDown();
412         return;
413     }
414     XtPopup(glc->shell, XtGrabNone);
415     glc->up = True;
416     j = 0;
417     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
418     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
419                 args, j);
420 }
421
422 void
423 LoadSelectedProc(w, event, prms, nprms)
424      Widget w;
425      XEvent *event;
426      String *prms;
427      Cardinal *nprms;
428 {
429     Widget listwidg;
430     XawListReturnStruct *rs;
431     int index;
432
433     if (glc == NULL) return;
434     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
435     rs = XawListShowCurrent(listwidg);
436     index = rs->list_index;
437     if (index < 0) return;
438     if (cmailMsgLoaded) {
439         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
440     } else {
441         LoadGame(glc->fp, index + 1, glc->filename, True);
442     }
443 }
444
445 void
446 GameListPopDown()
447 {
448     Arg args[16];
449     int j;
450
451     if (glc == NULL) return;
452     j = 0;
453     XtSetArg(args[j], XtNx, &glc->x); j++;
454     XtSetArg(args[j], XtNy, &glc->y); j++;
455     XtSetArg(args[j], XtNheight, &glc->h); j++;
456     XtSetArg(args[j], XtNwidth, &glc->w); j++;
457     XtGetValues(glc->shell, args, j);
458     XtPopdown(glc->shell);
459     XtSetKeyboardFocus(shellWidget, formWidget);
460     glc->up = False;
461     j = 0;
462     XtSetArg(args[j], XtNleftBitmap, None); j++;
463     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
464                 args, j);
465 }
466
467 void
468 GameListHighlight(index)
469      int index;
470 {
471     Widget listwidg;
472     if (glc == NULL || !glc->up) return;
473     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
474     XawListHighlight(listwidg, index - 1);
475 }