Add -useBoardTexture and -usePieceFont options
[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 int listEnd;
367
368 static void
369 GameListReplace(int page)
370 {
371   // filter: put in separate routine, to make callable from call-back
372   Widget listwidg;
373   Arg arg;
374   char buf[MSG_SIZ], *p, **st=list;
375   int i;
376
377   if(page) *st++ = _("previous page"); else if(listLength > 1000) *st++ = "";
378   for(i=0; i<1000; i++) if( !(*st++ = glc->strings[page+i]) ) { st--; break; }
379   listEnd = st - list;
380   if(page + 1000 <= listLength) *st++ = _("next page");
381   *st = NULL;
382
383   listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
384   XtSetArg(arg, XtNlist, listLength ? list : dummyList); // empty list displays message
385   XawListChange(listwidg, list, 0, 0, True);
386   XtSetValues(listwidg, &arg, 1);
387   XawListHighlight(listwidg, 0);
388   snprintf(buf, MSG_SIZ, "%s- %d/%d games (%d-%d-%d)", glc->filename, listLength, ((ListGame *) gameList.tailPred)->number, wins, losses, draws);
389   XtSetArg(arg, XtNtitle, buf);
390   XtSetValues(glc->shell, &arg, 1);
391 }
392
393 void
394 GameListCallback(w, client_data, call_data)
395      Widget w;
396      XtPointer client_data, call_data;
397 {
398     String name;
399     Arg args[16];
400     int j;
401     Widget listwidg;
402     GameListClosure *glc = (GameListClosure *) client_data;
403     XawListReturnStruct *rs;
404     int index;
405
406     j = 0;
407     XtSetArg(args[j], XtNlabel, &name);  j++;
408     XtGetValues(w, args, j);
409
410     if (strcmp(name, _("close")) == 0) {
411         GameListPopDown();
412         return;
413     }
414     if (strcmp(name, _("thresholds")) == 0) {
415         LoadOptionsProc();
416         return;
417     }
418     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
419     rs = XawListShowCurrent(listwidg);
420     if (strcmp(name, _("load")) == 0) {
421         index = rs->list_index;
422         if (index < 0) {
423             DisplayError(_("No game selected"), 0);
424             return;
425         }
426     } else if (strcmp(name, _("next")) == 0) {
427         index = rs->list_index + 1;
428         if (index >= listLength || !list[index]) {
429             DisplayError(_("Can't go forward any further"), 0);
430             return;
431         }
432         XawListHighlight(listwidg, index);
433     } else if (strcmp(name, _("prev")) == 0) {
434         index = rs->list_index - 1;
435         if (index < 0) {
436             DisplayError(_("Can't back up any further"), 0);
437             return;
438         }
439         XawListHighlight(listwidg, index);
440     } else if (strcmp(name, _("apply")) == 0 ||
441                strcmp(name, _("find position")) == 0) {
442         String text;
443         j = 0;
444         XtSetArg(args[j], XtNstring, &text);  j++;
445         XtGetValues(filterText, args, j);
446         safeStrCpy(filterString, text, sizeof(filterString)/sizeof(filterString[0]));
447         XawListHighlight(listwidg, 0);
448         GameListPrepare(strcmp(name, _("find position")) == 0); GameListReplace(0);
449         return;
450     }
451 #if 1
452     index = atoi(list[index])-1; // [HGM] filter: read true index from sequence nr of line
453     if (cmailMsgLoaded) {
454         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
455     } else {
456         LoadGame(glc->fp, index + 1, glc->filename, True);
457     }
458 #else
459     printf("This code should have been unreachable. Please report bug!\n");
460 #endif
461 }
462
463 void
464 GameListPopUp(fp, filename)
465      FILE *fp;
466      char *filename;
467 {
468     Arg args[16];
469     int j;
470     char **st;
471
472     if (glc == NULL) {
473         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
474         glc->x = glc->y = -1;
475     }
476
477     GameListPrepare(False); // [HGM] filter: code put in separate routine
478
479     glc->fp = fp;
480
481     if (glc->filename != NULL) free(glc->filename);
482     glc->filename = StrSave(filename);
483
484
485     if (glc->shell == NULL) {
486         glc->shell = GameListCreate(filename, GameListCallback, glc);
487     } else {
488         j = 0;
489         XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
490 //      XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
491         XtSetValues(glc->shell, args, j);
492     }
493     page = 0;
494     GameListReplace(0); // [HGM] filter: code put in separate routine, and also called to set title
495
496     XtPopup(glc->shell, XtGrabNone);
497     glc->up = True;
498     j = 0;
499     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
500     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
501                 args, j);
502 }
503
504 void
505 GameListDestroy()
506 {
507     if (glc == NULL) return;
508     GameListPopDown();
509     if (glc->strings != NULL) {
510         char **st;
511         st = glc->strings;
512         while (*st) {
513             free(*st++);
514         }
515         free(glc->strings);
516     }
517     free(glc);
518     glc = NULL;
519 }
520
521 void
522 ShowGameListProc(w, event, prms, nprms)
523      Widget w;
524      XEvent *event;
525      String *prms;
526      Cardinal *nprms;
527 {
528     Arg args[16];
529     int j;
530
531     if (glc == NULL) {
532         DisplayError(_("There is no game list"), 0);
533         return;
534     }
535     if (glc->up) {
536         GameListPopDown();
537         return;
538     }
539     XtPopup(glc->shell, XtGrabNone);
540     glc->up = True;
541     j = 0;
542     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
543     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
544                 args, j);
545     GameListHighlight(lastLoadGameNumber);
546 }
547
548 void
549 LoadSelectedProc(w, event, prms, nprms)
550      Widget w;
551      XEvent *event;
552      String *prms;
553      Cardinal *nprms;
554 {
555     Widget listwidg;
556     XawListReturnStruct *rs;
557     int index, direction = atoi(prms[0]);
558
559     if (glc == NULL || listLength == 0) return;
560     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
561     rs = XawListShowCurrent(listwidg);
562     index = rs->list_index;
563     if (index < 0) return;
564     if(page && index == 0) {
565         page -= 1000;
566         if(page < 0) page = 0; // safety
567         GameListReplace(page);
568         return;
569     }
570     if(index == 1001) {
571         page += 1000;
572         GameListReplace(page);
573         return;
574     }
575
576     if(direction != 0) {
577         int doLoad = abs(direction) > 2;
578         if(doLoad) direction /= 3;
579         index += direction;
580         if(direction == -2) index = 0;
581         if(direction == 2) index = listEnd-1;
582         if(index < 0 || index >= listEnd) return;
583         XawListHighlight(listwidg, index);
584         if(!doLoad) return;
585     }
586     index = atoi(list[index])-1; // [HGM] filter: read true index from sequence nr of line
587     if (cmailMsgLoaded) {
588         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
589     } else {
590         LoadGame(glc->fp, index + 1, glc->filename, True);
591         XSync(xDisplay, False);
592         XSetInputFocus(xDisplay, XtWindow(boardWidget), RevertToPointerRoot, CurrentTime);
593     }
594 }
595
596 void
597 SetFilterProc(w, event, prms, nprms)
598      Widget w;
599      XEvent *event;
600      String *prms;
601      Cardinal *nprms;
602 {
603         Arg args[16];
604         String name;
605         Widget list;
606         int j = 0;
607         XtSetArg(args[j], XtNstring, &name);  j++;
608         XtGetValues(filterText, args, j);
609         safeStrCpy(filterString, name, sizeof(filterString)/sizeof(filterString[0]));
610         GameListPrepare(False); GameListReplace(0);
611         list = XtNameToWidget(glc->shell, "*form.viewport.list");
612         XawListHighlight(list, 0);
613         j = 0;
614         XtSetArg(args[j], XtNdisplayCaret, False); j++;
615         XtSetValues(filterText, args, j);
616         XtSetKeyboardFocus(glc->shell, list);
617 }
618
619 void
620 GameListPopDown()
621 {
622     Arg args[16];
623     int j;
624
625     if (glc == NULL) return;
626     j = 0;
627     XtSetArg(args[j], XtNx, &glc->x); j++;
628     XtSetArg(args[j], XtNy, &glc->y); j++;
629     XtSetArg(args[j], XtNheight, &glc->h); j++;
630     XtSetArg(args[j], XtNwidth, &glc->w); j++;
631     XtGetValues(glc->shell, args, j);
632     wpGameList.x = glc->x - 4;
633     wpGameList.y = glc->y - 23;
634     wpGameList.width = glc->w;
635     wpGameList.height = glc->h;
636     XtPopdown(glc->shell);
637     XtSetKeyboardFocus(shellWidget, formWidget);
638     glc->up = False;
639     j = 0;
640     XtSetArg(args[j], XtNleftBitmap, None); j++;
641     XtSetValues(XtNameToWidget(menuBarWidget, "menuView.Show Game List"),
642                 args, j);
643 }
644
645 void
646 GameListHighlight(index)
647      int index;
648 {
649     Widget listwidg;
650     int i=0; char **st;
651     if (glc == NULL || !glc->up) return;
652     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
653     st = list;
654     while(*st && atoi(*st)<index) st++,i++;
655     XawListHighlight(listwidg, i);
656 }
657
658 Boolean
659 GameListIsUp()
660 {
661     return glc && glc->up;
662 }
663
664 int SaveGameListAsText(FILE *f)
665 {
666     ListGame * lg = (ListGame *) gameList.head;
667     int nItem;
668
669     if( !glc || ((ListGame *) gameList.tailPred)->number <= 0 ) {
670         DisplayError("Game list not loaded or empty", 0);
671         return False;
672     }
673
674     /* Copy the list into the global memory block */
675     if( f != NULL ) {
676  
677         lg = (ListGame *) gameList.head;
678
679         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
680             char * st = GameListLineFull(lg->number, &lg->gameInfo);
681             char *line = GameListLine(lg->number, &lg->gameInfo);
682             if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) )
683                     fprintf( f, "%s\n", st );
684             free(st); free(line);
685             lg = (ListGame *) lg->node.succ;
686         }
687
688         fclose(f);
689         return True;
690     }
691     return False;
692 }
693 //--------------------------------- Game-List options dialog ------------------------------------------
694
695 Widget gameListOptShell, listwidg;
696
697 char *strings[20];
698 int stringPtr;
699
700 void GLT_ClearList()
701 {
702     strings[0] = NULL;
703     stringPtr = 0;
704 }
705
706 void GLT_AddToList(char *name)
707 {
708     strings[stringPtr++] = name;
709     strings[stringPtr] = NULL;
710 }
711
712 Boolean GLT_GetFromList(int index, char *name)
713 {
714   safeStrCpy(name, strings[index], MSG_SIZ);
715   return TRUE;
716 }
717
718 void GLT_DeSelectList()
719 {
720     XawListChange(listwidg, strings, 0, 0, True);
721     XawListHighlight(listwidg, 0);
722 }
723
724 void
725 GameListOptionsPopDown()
726 {
727   if (gameListOptShell == NULL) return;
728
729   XtPopdown(gameListOptShell);
730   XtDestroyWidget(gameListOptShell);
731   gameListOptShell = 0;
732   XtSetKeyboardFocus(shellWidget, formWidget);
733 }
734
735 void
736 GameListOptionsCallback(w, client_data, call_data)
737      Widget w;
738      XtPointer client_data, call_data;
739 {
740     String name;
741     Arg args[16];
742     int j;
743     Widget listwidg;
744     XawListReturnStruct *rs;
745     int index;
746     char *p;
747
748     j = 0;
749     XtSetArg(args[j], XtNlabel, &name);  j++;
750     XtGetValues(w, args, j);
751
752     if (strcmp(name, _("OK")) == 0) {
753         GLT_ParseList();
754         appData.gameListTags = strdup(lpUserGLT);
755         GameListOptionsPopDown();
756         return;
757     } else
758     if (strcmp(name, _("cancel")) == 0) {
759         GameListOptionsPopDown();
760         return;
761     }
762     listwidg = XtNameToWidget(gameListOptShell, "*form.list");
763     rs = XawListShowCurrent(listwidg);
764     index = rs->list_index;
765     if (index < 0) {
766         DisplayError(_("No tag selected"), 0);
767         return;
768     }
769     p = strings[index];
770     if (strcmp(name, _("down")) == 0) {
771         if(index >= strlen(GLT_ALL_TAGS)) return;
772         strings[index] = strings[index+1];
773         strings[++index] = p;
774     } else
775     if (strcmp(name, _("up")) == 0) {
776         if(index == 0) return;
777         strings[index] = strings[index-1];
778         strings[--index] = p;
779     } else
780     if (strcmp(name, _("factory")) == 0) {
781       safeStrCpy(lpUserGLT, GLT_DEFAULT_TAGS, LPUSERGLT_SIZE);
782       GLT_TagsToList(lpUserGLT);
783       index = 0;
784     }
785     XawListHighlight(listwidg, index);
786 }
787
788 Widget
789 GameListOptionsCreate()
790 {
791     Arg args[16];
792     Widget shell, form, viewport, layout;
793     Widget b_load, b_loadprev, b_loadnext, b_close, b_cancel;
794     Dimension fw_width;
795     XtPointer client_data = NULL;
796     int j;
797
798     j = 0;
799     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
800     XtGetValues(formWidget, args, j);
801
802     j = 0;
803     XtSetArg(args[j], XtNresizable, True);  j++;
804     XtSetArg(args[j], XtNallowShellResize, True);  j++;
805     shell = gameListOptShell =
806       XtCreatePopupShell("Game-list options", transientShellWidgetClass,
807                          shellWidget, args, j);
808     layout =
809       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
810                             layoutArgs, XtNumber(layoutArgs));
811     j = 0;
812     XtSetArg(args[j], XtNborderWidth, 0); j++;
813     form =
814       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
815
816     j = 0;
817     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
818     XtSetArg(args[j], XtNforceColumns, True);  j++;
819     XtSetArg(args[j], XtNverticalList, True);  j++;
820     listwidg = viewport =
821       XtCreateManagedWidget("list", listWidgetClass, form, args, j);
822     XawListHighlight(listwidg, 0);
823 //    XtAugmentTranslations(listwidg,
824 //                        XtParseTranslationTable(gameListOptTranslations));
825
826     j = 0;
827     XtSetArg(args[j], XtNfromVert, viewport);  j++;
828     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
829     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
830     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
831     XtSetArg(args[j], XtNright, XtChainLeft); j++;
832     b_load =
833       XtCreateManagedWidget(_("factory"), commandWidgetClass, form, args, j);
834     XtAddCallback(b_load, XtNcallback, GameListOptionsCallback, client_data);
835
836     j = 0;
837     XtSetArg(args[j], XtNfromVert, viewport);  j++;
838     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
839     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
840     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
841     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
842     XtSetArg(args[j], XtNright, XtChainLeft); j++;
843     b_loadprev =
844       XtCreateManagedWidget(_("up"), commandWidgetClass, form, args, j);
845     XtAddCallback(b_loadprev, XtNcallback, GameListOptionsCallback, client_data);
846
847     j = 0;
848     XtSetArg(args[j], XtNfromVert, viewport);  j++;
849     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
850     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
851     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
852     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
853     XtSetArg(args[j], XtNright, XtChainLeft); j++;
854     b_loadnext =
855       XtCreateManagedWidget(_("down"), commandWidgetClass, form, args, j);
856     XtAddCallback(b_loadnext, XtNcallback, GameListOptionsCallback, client_data);
857
858     j = 0;
859     XtSetArg(args[j], XtNfromVert, viewport);  j++;
860     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
861     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
862     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
863     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
864     XtSetArg(args[j], XtNright, XtChainLeft); j++;
865     b_cancel =
866       XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
867     XtAddCallback(b_cancel, XtNcallback, GameListOptionsCallback, client_data);
868
869     j = 0;
870     XtSetArg(args[j], XtNfromVert, viewport);  j++;
871     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
872     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
873     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
874     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
875     XtSetArg(args[j], XtNright, XtChainLeft); j++;
876     b_close =
877       XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
878     XtAddCallback(b_close, XtNcallback, GameListOptionsCallback, client_data);
879
880     safeStrCpy(lpUserGLT, appData.gameListTags, LPUSERGLT_SIZE);
881     GLT_TagsToList(lpUserGLT);
882
883     XtRealizeWidget(shell);
884     CatchDeleteWindow(shell, "GameListOptionsPopDown");
885
886     return shell;
887 }
888
889 void
890 GameListOptionsPopUp(Widget w, XEvent *event, String *prms, Cardinal *nprms)
891 {
892   if (gameListOptShell == NULL)
893     gameListOptShell = GameListOptionsCreate();
894
895   XtPopup(gameListOptShell, XtGrabNone);
896 }
897
898