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