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