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