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