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