Updated copyright notice to 2011
[xboard.git] / xgamelist.c
1 /*
2  * xgamelist.c -- Game list window, part of X front end for XBoard
3  *
4  * Copyright 1995, 2009, 2010, 2011 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 void SetFocus P((Widget w, XtPointer data, XEvent *event, Boolean *b));
95
96 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget, gameListShell;
97 extern Display *xDisplay;
98 extern int squareSize;
99 extern Pixmap xMarkPixmap;
100 extern char *layoutName;
101
102 static Widget filterText;
103 static char filterString[MSG_SIZ];
104 static int listLength;
105
106 char gameListTranslations[] =
107   "<Btn1Up>(2): LoadSelectedProc(0) \n \
108    <Key>Home: LoadSelectedProc(-2) \n \
109    <Key>End: LoadSelectedProc(2) \n \
110    <Key>Up: LoadSelectedProc(-1) \n \
111    <Key>Down: LoadSelectedProc(1) \n \
112    <Key>Left: LoadSelectedProc(-1) \n \
113    <Key>Right: LoadSelectedProc(1) \n \
114    <Key>Return: LoadSelectedProc(0) \n";
115 char filterTranslations[] =
116   "<Key>Return: SetFilterProc() \n";
117
118 typedef struct {
119     Widget shell;
120     Position x, y;
121     Dimension w, h;
122     Boolean up;
123     FILE *fp;
124     char *filename;
125     char **strings;
126 } GameListClosure;
127 static GameListClosure *glc = NULL;
128
129 static Arg layoutArgs[] = {
130     { XtNborderWidth, 0 },
131     { XtNdefaultDistance, 0 }
132 };
133
134 Widget
135 GameListCreate(name, callback, client_data)
136      char *name;
137      XtCallbackProc callback;
138      XtPointer client_data;
139 {
140     Arg args[16];
141     Widget shell, form, viewport, listwidg, layout, label;
142     Widget b_load, b_loadprev, b_loadnext, b_close, b_filter;
143     Dimension fw_width;
144     int j;
145     GameListClosure *glc = (GameListClosure *) client_data;
146
147     j = 0;
148     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
149     XtGetValues(formWidget, args, j);
150
151     j = 0;
152     XtSetArg(args[j], XtNresizable, True);  j++;
153     XtSetArg(args[j], XtNallowShellResize, True);  j++;
154 #if TOPLEVEL
155     shell = gameListShell =
156       XtCreatePopupShell(name, topLevelShellWidgetClass,
157                          shellWidget, args, j);
158 #else
159     shell = gameListShell =
160       XtCreatePopupShell(name, transientShellWidgetClass,
161                          shellWidget, args, j);
162 #endif
163     layout =
164       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
165                             layoutArgs, XtNumber(layoutArgs));
166     j = 0;
167     XtSetArg(args[j], XtNborderWidth, 0); j++;
168     form =
169       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
170
171     j = 0;
172     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
173     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
174     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
175     XtSetArg(args[j], XtNright, XtChainRight);  j++;
176     XtSetArg(args[j], XtNresizable, False);  j++;
177     XtSetArg(args[j], XtNwidth, fw_width);  j++;
178     XtSetArg(args[j], XtNallowVert, True); j++;
179     viewport =
180       XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
181
182     j = 0;
183     XtSetArg(args[j], XtNlist, glc->strings);  j++;
184     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
185     XtSetArg(args[j], XtNforceColumns, True);  j++;
186     XtSetArg(args[j], XtNverticalList, True);  j++;
187     listwidg =
188       XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
189     XawListHighlight(listwidg, 0);
190     XtAugmentTranslations(listwidg,
191                           XtParseTranslationTable(gameListTranslations));
192
193     j = 0;
194     XtSetArg(args[j], XtNfromVert, viewport);  j++;
195     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
196     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
197     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
198     XtSetArg(args[j], XtNright, XtChainLeft); j++;
199     b_load =
200       XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
201     XtAddCallback(b_load, XtNcallback, callback, client_data);
202
203     j = 0;
204     XtSetArg(args[j], XtNfromVert, viewport);  j++;
205     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
206     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
207     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
208     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
209     XtSetArg(args[j], XtNright, XtChainLeft); j++;
210     b_loadprev =
211       XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
212     XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
213
214     j = 0;
215     XtSetArg(args[j], XtNfromVert, viewport);  j++;
216     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
217     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
218     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
219     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
220     XtSetArg(args[j], XtNright, XtChainLeft); j++;
221     b_loadnext =
222       XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
223     XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
224
225     j = 0;
226     XtSetArg(args[j], XtNfromVert, viewport);  j++;
227     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
228     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
229     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
230     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
231     XtSetArg(args[j], XtNright, XtChainLeft); j++;
232     b_close =
233       XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
234     XtAddCallback(b_close, XtNcallback, callback, client_data);
235
236     j = 0;
237     XtSetArg(args[j], XtNfromVert, viewport);  j++;
238     XtSetArg(args[j], XtNfromHoriz, b_close);  j++;
239     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
240     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
241     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
242     XtSetArg(args[j], XtNright, XtChainLeft); j++;
243     XtSetArg(args[j], XtNborderWidth, 0); j++;
244     label =
245       XtCreateManagedWidget(_("Filter:"), labelWidgetClass, form, args, j);
246
247     j = 0;
248     XtSetArg(args[j], XtNfromVert, viewport);  j++;
249     XtSetArg(args[j], XtNfromHoriz, label);  j++;
250     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
251     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
252     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
253     XtSetArg(args[j], XtNright, XtChainRight); j++;
254     XtSetArg(args[j], XtNwidth, fw_width - 225 - squareSize); j++;
255     XtSetArg(args[j], XtNstring, filterString);  j++;
256     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
257     XtSetArg(args[j], XtNresizable, True);  j++;
258 //    XtSetArg(args[j], XtNwidth, bw_width);  j++; /*force wider than buttons*/
259     /* !!Work around an apparent bug in XFree86 4.0.1 (X11R6.4.3) */
260     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
261     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
262     filterText =
263       XtCreateManagedWidget(_("filtertext"), asciiTextWidgetClass, form, args, j);
264     XtAddEventHandler(filterText, ButtonPressMask, False, SetFocus, (XtPointer) shell);
265     XtOverrideTranslations(filterText,
266                           XtParseTranslationTable(filterTranslations));
267
268     j = 0;
269     XtSetArg(args[j], XtNfromVert, viewport);  j++;
270     XtSetArg(args[j], XtNfromHoriz, filterText);  j++;
271     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
272     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
273     XtSetArg(args[j], XtNleft, XtChainRight); j++;
274     XtSetArg(args[j], XtNright, XtChainRight); j++;
275     b_filter =
276       XtCreateManagedWidget(_("apply"), commandWidgetClass, form, args, j);
277     XtAddCallback(b_filter, XtNcallback, callback, client_data);
278
279
280     if(wpGameList.width > 0) {
281         glc->x = wpGameList.x;
282         glc->y = wpGameList.y;
283         glc->w = wpGameList.width;
284         glc->h = wpGameList.height;
285     }
286
287     if (glc->x == -1) {
288         Position y1;
289         Dimension h1;
290         int xx, yy;
291         Window junk;
292
293         j = 0;
294         XtSetArg(args[j], XtNheight, &h1); j++;
295         XtSetArg(args[j], XtNy, &y1); j++;
296         XtGetValues(boardWidget, args, j);
297         glc->w = fw_width * 3/4;
298         glc->h = squareSize * 3;
299
300         XSync(xDisplay, False);
301 #ifdef NOTDEF
302         /* This code seems to tickle an X bug if it is executed too soon
303            after xboard starts up.  The coordinates get transformed as if
304            the main window was positioned at (0, 0).
305         */
306         XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
307                           y1 + (h1 - glc->h + appData.borderYoffset) / 2,
308                           &glc->x, &glc->y);
309 #else /*!NOTDEF*/
310         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
311                               RootWindowOfScreen(XtScreen(shellWidget)),
312                               (fw_width - glc->w) / 2,
313                               y1 + (h1 - glc->h + appData.borderYoffset) / 2,
314                               &xx, &yy, &junk);
315         glc->x = xx;
316         glc->y = yy;
317 #endif /*!NOTDEF*/
318         if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
319     }
320     j = 0;
321     XtSetArg(args[j], XtNheight, glc->h);  j++;
322     XtSetArg(args[j], XtNwidth, glc->w);  j++;
323     XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset);  j++;
324     XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset);  j++;
325     XtSetValues(shell, args, j);
326
327     XtRealizeWidget(shell);
328     CatchDeleteWindow(shell, "GameListPopDown");
329     XtSetKeyboardFocus(shell, listwidg);
330
331     return shell;
332 }
333
334 static int
335 GameListPrepare()
336 {   // [HGM] filter: put in separate routine, to make callable from call-back
337     int nstrings;
338     ListGame *lg;
339     char **st, *line;
340
341     nstrings = ((ListGame *) gameList.tailPred)->number;
342     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
343     st = glc->strings;
344     lg = (ListGame *) gameList.head;
345     listLength = 0;
346     while (nstrings--) {
347         line = GameListLine(lg->number, &lg->gameInfo);
348         if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) ) {
349             *st++ = line; // [HGM] filter: make adding line conditional
350             listLength++;
351         }
352         lg = (ListGame *) lg->node.succ;
353      }
354     *st = NULL;
355     return listLength;
356 }
357
358 static void
359 GameListReplace()
360 {
361   // filter: put in separate routine, to make callable from call-back
362   Widget listwidg;
363
364   listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
365   XawListChange(listwidg, glc->strings, 0, 0, True);
366   XawListHighlight(listwidg, 0);
367 }
368
369 void
370 GameListCallback(w, client_data, call_data)
371      Widget w;
372      XtPointer client_data, call_data;
373 {
374     String name;
375     Arg args[16];
376     int j;
377     Widget listwidg;
378     GameListClosure *glc = (GameListClosure *) client_data;
379     XawListReturnStruct *rs;
380     int index;
381
382     j = 0;
383     XtSetArg(args[j], XtNlabel, &name);  j++;
384     XtGetValues(w, args, j);
385
386     if (strcmp(name, _("close")) == 0) {
387         GameListPopDown();
388         return;
389     }
390     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
391     rs = XawListShowCurrent(listwidg);
392     if (strcmp(name, _("load")) == 0) {
393         index = rs->list_index;
394         if (index < 0) {
395             DisplayError(_("No game selected"), 0);
396             return;
397         }
398     } else if (strcmp(name, _("next")) == 0) {
399         index = rs->list_index + 1;
400         if (index >= listLength) {
401             DisplayError(_("Can't go forward any further"), 0);
402             return;
403         }
404         XawListHighlight(listwidg, index);
405     } else if (strcmp(name, _("prev")) == 0) {
406         index = rs->list_index - 1;
407         if (index < 0) {
408             DisplayError(_("Can't back up any further"), 0);
409             return;
410         }
411         XawListHighlight(listwidg, index);
412     } else if (strcmp(name, _("apply")) == 0) {
413         String name;
414         j = 0;
415         XtSetArg(args[j], XtNstring, &name);  j++;
416         XtGetValues(filterText, args, j);
417         safeStrCpy(filterString, name, sizeof(filterString)/sizeof(filterString[0]));
418         XawListHighlight(listwidg, 0);
419         if(GameListPrepare()) GameListReplace(); // crashes on empty list...
420         return;
421     }
422     index = atoi(glc->strings[index])-1; // [HGM] filter: read true index from sequence nr of line
423     if (cmailMsgLoaded) {
424         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
425     } else {
426         LoadGame(glc->fp, index + 1, glc->filename, True);
427     }
428 }
429
430 void
431 GameListPopUp(fp, filename)
432      FILE *fp;
433      char *filename;
434 {
435     Arg args[16];
436     int j;
437     char **st;
438
439     if (glc == NULL) {
440         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
441         glc->x = glc->y = -1;
442     }
443
444     if (glc->strings != NULL) {
445         st = glc->strings;
446         while (*st) {
447             free(*st++);
448         }
449         free(glc->strings);
450     }
451
452     GameListPrepare(); // [HGM] filter: code put in separate routine
453
454     glc->fp = fp;
455
456     if (glc->filename != NULL) free(glc->filename);
457     glc->filename = StrSave(filename);
458
459
460     if (glc->shell == NULL) {
461         glc->shell = GameListCreate(filename, GameListCallback, glc);
462     } else {
463         GameListReplace(); // [HGM] filter: code put in separate routine
464         j = 0;
465         XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
466         XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
467         XtSetValues(glc->shell, args, j);
468     }
469
470     XtPopup(glc->shell, XtGrabNone);
471     glc->up = True;
472     j = 0;
473     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
474     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
475                 args, j);
476 }
477
478 void
479 GameListDestroy()
480 {
481     if (glc == NULL) return;
482     GameListPopDown();
483     if (glc->strings != NULL) {
484         char **st;
485         st = glc->strings;
486         while (*st) {
487             free(*st++);
488         }
489         free(glc->strings);
490     }
491     free(glc);
492     glc = NULL;
493 }
494
495 void
496 ShowGameListProc(w, event, prms, nprms)
497      Widget w;
498      XEvent *event;
499      String *prms;
500      Cardinal *nprms;
501 {
502     Arg args[16];
503     int j;
504
505     if (glc == NULL) {
506         DisplayError(_("There is no game list"), 0);
507         return;
508     }
509     if (glc->up) {
510         GameListPopDown();
511         return;
512     }
513     XtPopup(glc->shell, XtGrabNone);
514     glc->up = True;
515     j = 0;
516     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
517     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
518                 args, j);
519 }
520
521 void
522 LoadSelectedProc(w, event, prms, nprms)
523      Widget w;
524      XEvent *event;
525      String *prms;
526      Cardinal *nprms;
527 {
528     Widget listwidg;
529     XawListReturnStruct *rs;
530     int index, direction = atoi(prms[0]);
531
532     if (glc == NULL) return;
533     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
534     rs = XawListShowCurrent(listwidg);
535     index = rs->list_index;
536     if (index < 0) return;
537     if(direction != 0) {
538         index += direction;
539         if(direction == -2) index = 0;
540         if(direction == 2) index = listLength-1;
541         if(index < 0 || index >= listLength) return;
542         XawListHighlight(listwidg, index);
543         return;
544     }
545     index = atoi(glc->strings[index])-1; // [HGM] filter: read true index from sequence nr of line
546     if (cmailMsgLoaded) {
547         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
548     } else {
549         LoadGame(glc->fp, index + 1, glc->filename, True);
550     }
551 }
552
553 void
554 SetFilterProc(w, event, prms, nprms)
555      Widget w;
556      XEvent *event;
557      String *prms;
558      Cardinal *nprms;
559 {
560         Arg args[16];
561         String name;
562         Widget list;
563         int j = 0;
564         XtSetArg(args[j], XtNstring, &name);  j++;
565         XtGetValues(filterText, args, j);
566         safeStrCpy(filterString, name, sizeof(filterString)/sizeof(filterString[0]));
567         if(GameListPrepare()) GameListReplace(); // crashes on empty list...
568         list = XtNameToWidget(glc->shell, "*form.viewport.list");
569         XawListHighlight(list, 0);
570         j = 0;
571         XtSetArg(args[j], XtNdisplayCaret, False); j++;
572         XtSetValues(filterText, args, j);
573         XtSetKeyboardFocus(glc->shell, list);
574 }
575
576 void
577 GameListPopDown()
578 {
579     Arg args[16];
580     int j;
581
582     if (glc == NULL) return;
583     j = 0;
584     XtSetArg(args[j], XtNx, &glc->x); j++;
585     XtSetArg(args[j], XtNy, &glc->y); j++;
586     XtSetArg(args[j], XtNheight, &glc->h); j++;
587     XtSetArg(args[j], XtNwidth, &glc->w); j++;
588     XtGetValues(glc->shell, args, j);
589     wpGameList.x = glc->x - 4;
590     wpGameList.y = glc->y - 23;
591     wpGameList.width = glc->w;
592     wpGameList.height = glc->h;
593     XtPopdown(glc->shell);
594     XtSetKeyboardFocus(shellWidget, formWidget);
595     glc->up = False;
596     j = 0;
597     XtSetArg(args[j], XtNleftBitmap, None); j++;
598     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
599                 args, j);
600 }
601
602 void
603 GameListHighlight(index)
604      int index;
605 {
606     Widget listwidg;
607     int i=0; char **st;
608     if (glc == NULL || !glc->up) return;
609     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
610     st = glc->strings;
611     while(*st && atoi(*st)<index) st++,i++;
612     XawListHighlight(listwidg, i);
613 }
614
615 Boolean
616 GameListIsUp()
617 {
618     return glc && glc->up;
619 }
620
621 //--------------------------------- Game-List options dialog ------------------------------------------
622
623 Widget gameListOptShell, listwidg;
624
625 char *strings[20];
626 int stringPtr;
627
628 void GLT_ClearList()
629 {
630     strings[0] = NULL;
631     stringPtr = 0;
632 }
633
634 void GLT_AddToList(char *name)
635 {
636     strings[stringPtr++] = name;
637     strings[stringPtr] = NULL;
638 }
639
640 Boolean GLT_GetFromList(int index, char *name)
641 {
642   safeStrCpy(name, strings[index], MSG_SIZ);
643   return TRUE;
644 }
645
646 void GLT_DeSelectList()
647 {
648     XawListChange(listwidg, strings, 0, 0, True);
649     XawListHighlight(listwidg, 0);
650 }
651
652 void
653 GameListOptionsPopDown()
654 {
655   if (gameListOptShell == NULL) return;
656
657   XtPopdown(gameListOptShell);
658   XtDestroyWidget(gameListOptShell);
659   gameListOptShell = 0;
660   XtSetKeyboardFocus(shellWidget, formWidget);
661 }
662
663 void
664 GameListOptionsCallback(w, client_data, call_data)
665      Widget w;
666      XtPointer client_data, call_data;
667 {
668     String name;
669     Arg args[16];
670     int j;
671     Widget listwidg;
672     XawListReturnStruct *rs;
673     int index;
674     char *p;
675
676     j = 0;
677     XtSetArg(args[j], XtNlabel, &name);  j++;
678     XtGetValues(w, args, j);
679
680     if (strcmp(name, _("OK")) == 0) {
681         GLT_ParseList();
682         appData.gameListTags = strdup(lpUserGLT);
683         GameListOptionsPopDown();
684         return;
685     } else
686     if (strcmp(name, _("cancel")) == 0) {
687         GameListOptionsPopDown();
688         return;
689     }
690     listwidg = XtNameToWidget(gameListOptShell, "*form.list");
691     rs = XawListShowCurrent(listwidg);
692     index = rs->list_index;
693     if (index < 0) {
694         DisplayError(_("No tag selected"), 0);
695         return;
696     }
697     p = strings[index];
698     if (strcmp(name, _("down")) == 0) {
699         if(index >= strlen(GLT_ALL_TAGS)) return;
700         strings[index] = strings[index+1];
701         strings[++index] = p;
702     } else
703     if (strcmp(name, _("up")) == 0) {
704         if(index == 0) return;
705         strings[index] = strings[index-1];
706         strings[--index] = p;
707     } else
708     if (strcmp(name, _("factory")) == 0) {
709       safeStrCpy(lpUserGLT, GLT_DEFAULT_TAGS, LPUSERGLT_SIZE);
710       GLT_TagsToList(lpUserGLT);
711       index = 0;
712     }
713     XawListHighlight(listwidg, index);
714 }
715
716 Widget
717 GameListOptionsCreate()
718 {
719     Arg args[16];
720     Widget shell, form, viewport, layout;
721     Widget b_load, b_loadprev, b_loadnext, b_close, b_cancel;
722     Dimension fw_width;
723     XtPointer client_data = NULL;
724     int j;
725
726     j = 0;
727     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
728     XtGetValues(formWidget, args, j);
729
730     j = 0;
731     XtSetArg(args[j], XtNresizable, True);  j++;
732     XtSetArg(args[j], XtNallowShellResize, True);  j++;
733     shell = gameListOptShell =
734       XtCreatePopupShell("Game-list options", transientShellWidgetClass,
735                          shellWidget, args, j);
736     layout =
737       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
738                             layoutArgs, XtNumber(layoutArgs));
739     j = 0;
740     XtSetArg(args[j], XtNborderWidth, 0); j++;
741     form =
742       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
743
744     j = 0;
745     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
746     XtSetArg(args[j], XtNforceColumns, True);  j++;
747     XtSetArg(args[j], XtNverticalList, True);  j++;
748     listwidg = viewport =
749       XtCreateManagedWidget("list", listWidgetClass, form, args, j);
750     XawListHighlight(listwidg, 0);
751 //    XtAugmentTranslations(listwidg,
752 //                        XtParseTranslationTable(gameListOptTranslations));
753
754     j = 0;
755     XtSetArg(args[j], XtNfromVert, viewport);  j++;
756     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
757     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
758     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
759     XtSetArg(args[j], XtNright, XtChainLeft); j++;
760     b_load =
761       XtCreateManagedWidget(_("factory"), commandWidgetClass, form, args, j);
762     XtAddCallback(b_load, XtNcallback, GameListOptionsCallback, client_data);
763
764     j = 0;
765     XtSetArg(args[j], XtNfromVert, viewport);  j++;
766     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
767     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
768     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
769     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
770     XtSetArg(args[j], XtNright, XtChainLeft); j++;
771     b_loadprev =
772       XtCreateManagedWidget(_("up"), commandWidgetClass, form, args, j);
773     XtAddCallback(b_loadprev, XtNcallback, GameListOptionsCallback, client_data);
774
775     j = 0;
776     XtSetArg(args[j], XtNfromVert, viewport);  j++;
777     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
778     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
779     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
780     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
781     XtSetArg(args[j], XtNright, XtChainLeft); j++;
782     b_loadnext =
783       XtCreateManagedWidget(_("down"), commandWidgetClass, form, args, j);
784     XtAddCallback(b_loadnext, XtNcallback, GameListOptionsCallback, client_data);
785
786     j = 0;
787     XtSetArg(args[j], XtNfromVert, viewport);  j++;
788     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
789     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
790     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
791     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
792     XtSetArg(args[j], XtNright, XtChainLeft); j++;
793     b_cancel =
794       XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
795     XtAddCallback(b_cancel, XtNcallback, GameListOptionsCallback, client_data);
796
797     j = 0;
798     XtSetArg(args[j], XtNfromVert, viewport);  j++;
799     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
800     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
801     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
802     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
803     XtSetArg(args[j], XtNright, XtChainLeft); j++;
804     b_close =
805       XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
806     XtAddCallback(b_close, XtNcallback, GameListOptionsCallback, client_data);
807
808     safeStrCpy(lpUserGLT, appData.gameListTags, LPUSERGLT_SIZE);
809     GLT_TagsToList(lpUserGLT);
810
811     XtRealizeWidget(shell);
812     CatchDeleteWindow(shell, "GameListOptionsPopDown");
813
814     return shell;
815 }
816
817 void
818 GameListOptionsPopUp(Widget w, XEvent *event, String *prms, Cardinal *nprms)
819 {
820   if (gameListOptShell == NULL)
821     gameListOptShell = GameListOptionsCreate();
822
823   XtPopup(gameListOptShell, XtGrabNone);
824 }
825
826