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