Allow <Enter> to apply filter in XB GameList filter edit
[xboard.git] / xgamelist.c
1 /*
2  * xgamelist.c -- Game list window, part of X front end for XBoard
3  *
4  * Copyright 1995,2009 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() \n \
108    <Key>Return: LoadSelectedProc() \n";
109 char filterTranslations[] =
110   "<Key>Return: SetFilterProc() \n";
111
112 typedef struct {
113     Widget shell;
114     Position x, y;
115     Dimension w, h;
116     Boolean up;
117     FILE *fp;
118     char *filename;
119     char **strings;
120 } GameListClosure;
121 static GameListClosure *glc = NULL;
122
123 static Arg layoutArgs[] = {
124     { XtNborderWidth, 0 },
125     { XtNdefaultDistance, 0 }
126 };
127
128 Widget
129 GameListCreate(name, callback, client_data)
130      char *name;
131      XtCallbackProc callback;
132      XtPointer client_data;
133 {
134     Arg args[16];
135     Widget shell, form, viewport, listwidg, layout, label;
136     Widget b_load, b_loadprev, b_loadnext, b_close, b_filter;
137     Dimension fw_width;
138     int j;
139     GameListClosure *glc = (GameListClosure *) client_data;
140
141     j = 0;
142     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
143     XtGetValues(formWidget, args, j);
144
145     j = 0;
146     XtSetArg(args[j], XtNresizable, True);  j++;
147     XtSetArg(args[j], XtNallowShellResize, True);  j++;
148 #if TOPLEVEL
149     shell = gameListShell =
150       XtCreatePopupShell(name, topLevelShellWidgetClass,
151                          shellWidget, args, j);
152 #else
153     shell = gameListShell =
154       XtCreatePopupShell(name, transientShellWidgetClass,
155                          shellWidget, args, j);
156 #endif
157     layout =
158       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
159                             layoutArgs, XtNumber(layoutArgs));
160     j = 0;
161     XtSetArg(args[j], XtNborderWidth, 0); j++;
162     form =
163       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
164
165     j = 0;
166     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
167     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
168     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
169     XtSetArg(args[j], XtNright, XtChainRight);  j++;
170     XtSetArg(args[j], XtNresizable, False);  j++;
171     XtSetArg(args[j], XtNwidth, fw_width);  j++;
172     XtSetArg(args[j], XtNallowVert, True); j++;
173     viewport =
174       XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
175
176     j = 0;
177     XtSetArg(args[j], XtNlist, glc->strings);  j++;
178     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
179     XtSetArg(args[j], XtNforceColumns, True);  j++;
180     XtSetArg(args[j], XtNverticalList, True);  j++;
181     listwidg = 
182       XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
183     XawListHighlight(listwidg, 0);
184     XtAugmentTranslations(listwidg,
185                           XtParseTranslationTable(gameListTranslations));
186
187     j = 0;
188     XtSetArg(args[j], XtNfromVert, viewport);  j++;
189     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
190     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
191     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
192     XtSetArg(args[j], XtNright, XtChainLeft); j++;
193     b_load =
194       XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
195     XtAddCallback(b_load, XtNcallback, callback, client_data);
196
197     j = 0;
198     XtSetArg(args[j], XtNfromVert, viewport);  j++;
199     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
200     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
201     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
202     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
203     XtSetArg(args[j], XtNright, XtChainLeft); j++;
204     b_loadprev =
205       XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
206     XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
207
208     j = 0;
209     XtSetArg(args[j], XtNfromVert, viewport);  j++;
210     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
211     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
212     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
213     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
214     XtSetArg(args[j], XtNright, XtChainLeft); j++;
215     b_loadnext =
216       XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
217     XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
218
219     j = 0;
220     XtSetArg(args[j], XtNfromVert, viewport);  j++;
221     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
222     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
223     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
224     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
225     XtSetArg(args[j], XtNright, XtChainLeft); j++;
226     b_close =
227       XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
228     XtAddCallback(b_close, XtNcallback, callback, client_data);
229
230     j = 0;
231     XtSetArg(args[j], XtNfromVert, viewport);  j++;
232     XtSetArg(args[j], XtNfromHoriz, b_close);  j++;
233     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
234     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
235     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
236     XtSetArg(args[j], XtNright, XtChainLeft); j++;
237     XtSetArg(args[j], XtNborderWidth, 0); j++;
238     label =
239       XtCreateManagedWidget(_("Filter:"), labelWidgetClass, form, args, j);
240
241     j = 0;
242     XtSetArg(args[j], XtNfromVert, viewport);  j++;
243     XtSetArg(args[j], XtNfromHoriz, label);  j++;
244     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
245     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
246     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
247     XtSetArg(args[j], XtNright, XtChainRight); j++;
248     XtSetArg(args[j], XtNwidth, 173); j++;
249     XtSetArg(args[j], XtNstring, filterString);  j++;
250     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
251     XtSetArg(args[j], XtNresizable, True);  j++;
252 //    XtSetArg(args[j], XtNwidth, bw_width);  j++; /*force wider than buttons*/
253     /* !!Work around an apparent bug in XFree86 4.0.1 (X11R6.4.3) */
254     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
255     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
256     filterText =
257       XtCreateManagedWidget(_("filtertext"), asciiTextWidgetClass, form, args, j);
258     XtAddEventHandler(filterText, ButtonPressMask, False, SetFocus, (XtPointer) shell);
259     XtOverrideTranslations(filterText,
260                           XtParseTranslationTable(filterTranslations));
261
262     j = 0;
263     XtSetArg(args[j], XtNfromVert, viewport);  j++;
264     XtSetArg(args[j], XtNfromHoriz, filterText);  j++;
265     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
266     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
267     XtSetArg(args[j], XtNleft, XtChainRight); j++;
268     XtSetArg(args[j], XtNright, XtChainRight); j++;
269     b_filter =
270       XtCreateManagedWidget(_("apply"), commandWidgetClass, form, args, j);
271     XtAddCallback(b_filter, XtNcallback, callback, client_data);
272
273
274     if(wpGameList.width > 0) {
275         glc->x = wpGameList.x;
276         glc->y = wpGameList.y;
277         glc->w = wpGameList.width;
278         glc->h = wpGameList.height;
279     }
280
281     if (glc->x == -1) {
282         Position y1;
283         Dimension h1;
284         int xx, yy;
285         Window junk;
286
287         j = 0;
288         XtSetArg(args[j], XtNheight, &h1); j++;
289         XtSetArg(args[j], XtNy, &y1); j++;
290         XtGetValues(boardWidget, args, j);
291         glc->w = fw_width * 3/4;
292         glc->h = squareSize * 3;
293
294         XSync(xDisplay, False);
295 #ifdef NOTDEF
296         /* This code seems to tickle an X bug if it is executed too soon
297            after xboard starts up.  The coordinates get transformed as if
298            the main window was positioned at (0, 0).
299         */
300         XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
301                           y1 + (h1 - glc->h + appData.borderYoffset) / 2,
302                           &glc->x, &glc->y);
303 #else /*!NOTDEF*/
304         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
305                               RootWindowOfScreen(XtScreen(shellWidget)),
306                               (fw_width - glc->w) / 2,
307                               y1 + (h1 - glc->h + appData.borderYoffset) / 2,
308                               &xx, &yy, &junk);
309         glc->x = xx;
310         glc->y = yy;
311 #endif /*!NOTDEF*/
312         if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
313     }
314     j = 0;
315     XtSetArg(args[j], XtNheight, glc->h);  j++;
316     XtSetArg(args[j], XtNwidth, glc->w);  j++;
317     XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset);  j++;
318     XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset);  j++;
319     XtSetValues(shell, args, j);
320
321     XtRealizeWidget(shell);
322     CatchDeleteWindow(shell, "GameListPopDown");
323
324     return shell;
325 }
326
327 static int
328 GameListPrepare()
329 {   // [HGM] filter: put in separate routine, to make callable from call-back
330     int nstrings;
331     ListGame *lg;
332     char **st, *line;
333
334     nstrings = ((ListGame *) gameList.tailPred)->number;
335     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
336     st = glc->strings;
337     lg = (ListGame *) gameList.head;
338     listLength = 0;
339     while (nstrings--) {
340         line = GameListLine(lg->number, &lg->gameInfo);
341         if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) ) {
342             *st++ = line; // [HGM] filter: make adding line conditional
343             listLength++;
344         }
345         lg = (ListGame *) lg->node.succ;
346      }
347     *st = NULL;
348     return listLength;
349 }
350
351 static void
352 GameListReplace()
353 {   // [HGM] filter: put in separate routine, to make callable from call-back
354     Arg args[16];
355     int j;
356     Widget listwidg;
357
358         listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
359         XawListChange(listwidg, glc->strings, 0, 0, True);
360         XawListHighlight(listwidg, 0);
361 }
362
363 void
364 GameListCallback(w, client_data, call_data)
365      Widget w;
366      XtPointer client_data, call_data;
367 {
368     String name;
369     Arg args[16];
370     int j;
371     Widget listwidg;
372     GameListClosure *glc = (GameListClosure *) client_data;
373     XawListReturnStruct *rs;
374     int index;
375
376     j = 0;
377     XtSetArg(args[j], XtNlabel, &name);  j++;
378     XtGetValues(w, args, j);
379
380     if (strcmp(name, _("close")) == 0) {
381         GameListPopDown();
382         return;
383     }
384     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
385     rs = XawListShowCurrent(listwidg);
386     if (strcmp(name, _("load")) == 0) {
387         index = rs->list_index;
388         if (index < 0) {
389             DisplayError(_("No game selected"), 0);
390             return;
391         }
392     } else if (strcmp(name, _("next")) == 0) {
393         index = rs->list_index + 1;
394         if (index >= listLength) {
395             DisplayError(_("Can't go forward any further"), 0);
396             return;
397         }
398         XawListHighlight(listwidg, index);
399     } else if (strcmp(name, _("prev")) == 0) {
400         index = rs->list_index - 1;
401         if (index < 0) {
402             DisplayError(_("Can't back up any further"), 0);
403             return;
404         }
405         XawListHighlight(listwidg, index);
406     } else if (strcmp(name, _("apply")) == 0) {
407         String name;
408         j = 0;
409         XtSetArg(args[j], XtNstring, &name);  j++;
410         XtGetValues(filterText, args, j);
411         strcpy(filterString, name);
412         XawListHighlight(listwidg, 0);
413         if(GameListPrepare()) GameListReplace(); // crashes on empty list...
414         return;
415     }
416     index = atoi(glc->strings[index])-1; // [HGM] filter: read true index from sequence nr of line
417     if (cmailMsgLoaded) {
418         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
419     } else {
420         LoadGame(glc->fp, index + 1, glc->filename, True);
421     }
422 }
423
424 void
425 GameListPopUp(fp, filename)
426      FILE *fp;
427      char *filename;
428 {
429     Arg args[16];
430     int j, nstrings;
431     Widget listwidg;
432     ListGame *lg;
433     char **st;
434
435     if (glc == NULL) {
436         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
437         glc->x = glc->y = -1;
438     }
439
440     if (glc->strings != NULL) {
441         st = glc->strings;
442         while (*st) {
443             free(*st++);
444         }
445         free(glc->strings);
446     }
447
448     GameListPrepare(); // [HGM] filter: code put in separate routine
449
450     glc->fp = fp;
451
452     if (glc->filename != NULL) free(glc->filename);
453     glc->filename = StrSave(filename);
454
455
456     if (glc->shell == NULL) {
457         glc->shell = GameListCreate(filename, GameListCallback, glc); 
458     } else {
459         GameListReplace(); // [HGM] filter: code put in separate routine
460         j = 0;
461         XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
462         XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
463         XtSetValues(glc->shell, args, j);
464     }
465
466     XtPopup(glc->shell, XtGrabNone);
467     glc->up = True;
468     j = 0;
469     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
470     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
471                 args, j);
472 }
473
474 void
475 GameListDestroy()
476 {
477     if (glc == NULL) return;
478     GameListPopDown();
479     if (glc->strings != NULL) {
480         char **st;
481         st = glc->strings;
482         while (*st) {
483             free(*st++);
484         }
485         free(glc->strings);
486     }
487     free(glc);
488     glc = NULL;
489 }
490
491 void
492 ShowGameListProc(w, event, prms, nprms)
493      Widget w;
494      XEvent *event;
495      String *prms;
496      Cardinal *nprms;
497 {
498     Arg args[16];
499     int j;
500
501     if (glc == NULL) {
502         DisplayError(_("There is no game list"), 0);
503         return;
504     }
505     if (glc->up) {
506         GameListPopDown();
507         return;
508     }
509     XtPopup(glc->shell, XtGrabNone);
510     glc->up = True;
511     j = 0;
512     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
513     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
514                 args, j);
515 }
516
517 void
518 LoadSelectedProc(w, event, prms, nprms)
519      Widget w;
520      XEvent *event;
521      String *prms;
522      Cardinal *nprms;
523 {
524     Widget listwidg;
525     XawListReturnStruct *rs;
526     int index;
527
528     if (glc == NULL) return;
529     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
530     rs = XawListShowCurrent(listwidg);
531     index = rs->list_index;
532     if (index < 0) return;
533     index = atoi(glc->strings[index])-1; // [HGM] filter: read true index from sequence nr of line
534     if (cmailMsgLoaded) {
535         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
536     } else {
537         LoadGame(glc->fp, index + 1, glc->filename, True);
538     }
539 }
540
541 void
542 SetFilterProc(w, event, prms, nprms)
543      Widget w;
544      XEvent *event;
545      String *prms;
546      Cardinal *nprms;
547 {
548         Arg args[16];
549         String name;
550         Widget list;
551         int j = 0;
552         XtSetArg(args[j], XtNstring, &name);  j++;
553         XtGetValues(filterText, args, j);
554         strcpy(filterString, name);
555         if(GameListPrepare()) GameListReplace(); // crashes on empty list...
556         list = XtNameToWidget(glc->shell, "*form.viewport.list");
557         XawListHighlight(list, 0);
558         j = 0;
559         XtSetArg(args[j], XtNdisplayCaret, False); j++;
560         XtSetValues(filterText, args, j);
561         XtSetKeyboardFocus(glc->shell, list);
562 }
563
564 void
565 GameListPopDown()
566 {
567     Arg args[16];
568     int j;
569
570     if (glc == NULL) return;
571     j = 0;
572     XtSetArg(args[j], XtNx, &glc->x); j++;
573     XtSetArg(args[j], XtNy, &glc->y); j++;
574     XtSetArg(args[j], XtNheight, &glc->h); j++;
575     XtSetArg(args[j], XtNwidth, &glc->w); j++;
576     XtGetValues(glc->shell, args, j);
577     wpGameList.x = glc->x - 4;
578     wpGameList.y = glc->y - 23;
579     wpGameList.width = glc->w;
580     wpGameList.height = glc->h;
581     XtPopdown(glc->shell);
582     XtSetKeyboardFocus(shellWidget, formWidget);
583     glc->up = False;
584     j = 0;
585     XtSetArg(args[j], XtNleftBitmap, None); j++;
586     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
587                 args, j);
588 }
589
590 void
591 GameListHighlight(index)
592      int index;
593 {
594     Widget listwidg;
595     int i=0; char **st;
596     if (glc == NULL || !glc->up) return;
597     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
598     st = glc->strings;
599     while(*st && atoi(*st)<index) st++,i++;
600     XawListHighlight(listwidg, i);
601 }
602
603 Boolean
604 GameListIsUp()
605 {
606     return glc && glc->up;
607 }