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