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