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