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