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