2 * xgamelist.c -- Game list window, part of X front end for XBoard
4 * Copyright 1995,2009 Free Software Foundation, Inc.
5 * ------------------------------------------------------------------------
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.
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.
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/. *
20 *------------------------------------------------------------------------
21 ** See the file ChangeLog for a revision history. */
28 #include <sys/types.h>
33 #else /* not STDC_HEADERS */
34 extern char *getenv();
37 # else /* not HAVE_STRING_H */
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
46 #include <X11/Intrinsic.h>
47 #include <X11/StringDefs.h>
48 #include <X11/Shell.h>
49 #include <X11/cursorfont.h>
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>
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>
82 #include "xgamelist.h"
86 # define _(s) gettext (s)
87 # define N_(s) gettext_noop (s)
94 void SetFocus P((Widget w, XtPointer data, XEvent *event, Boolean *b));
96 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget, gameListShell;
97 extern Display *xDisplay;
98 extern int squareSize;
99 extern Pixmap xMarkPixmap;
100 extern char *layoutName;
102 static Widget filterText;
103 static char filterString[MSG_SIZ];
104 static int listLength;
106 char gameListTranslations[] =
107 "<Btn1Up>(2): LoadSelectedProc() \n \
108 <Key>Return: LoadSelectedProc() \n";
119 static GameListClosure *glc = NULL;
121 static Arg layoutArgs[] = {
122 { XtNborderWidth, 0 },
123 { XtNdefaultDistance, 0 }
126 /* [AS] Wildcard pattern matching */
\r
127 static Boolean HasPattern( const char * text, const char * pattern )
\r
129 while( *pattern != '\0' ) {
\r
130 if( *pattern == '*' ) {
\r
131 while( *pattern == '*' ) {
\r
135 if( *pattern == '\0' ) {
\r
139 while( *text != '\0' ) {
\r
140 if( HasPattern( text, pattern ) ) {
\r
146 else if( (*pattern == *text) || ((*pattern == '?') && (*text != '\0')) ) {
\r
158 static Boolean SearchPattern( const char * text, const char * pattern )
\r
160 Boolean result = TRUE;
\r
162 if( pattern != NULL && *pattern != '\0' ) {
\r
163 if( *pattern == '*' ) {
\r
164 result = HasPattern( text, pattern );
\r
169 while( *text != '\0' ) {
\r
170 if( HasPattern( text, pattern ) ) {
\r
183 GameListCreate(name, callback, client_data)
185 XtCallbackProc callback;
186 XtPointer client_data;
189 Widget shell, form, viewport, listwidg, layout, label;
190 Widget b_load, b_loadprev, b_loadnext, b_close, b_filter;
193 GameListClosure *glc = (GameListClosure *) client_data;
196 XtSetArg(args[j], XtNwidth, &fw_width); j++;
197 XtGetValues(formWidget, args, j);
200 XtSetArg(args[j], XtNresizable, True); j++;
201 XtSetArg(args[j], XtNallowShellResize, True); j++;
203 shell = gameListShell =
204 XtCreatePopupShell(name, topLevelShellWidgetClass,
205 shellWidget, args, j);
207 shell = gameListShell =
208 XtCreatePopupShell(name, transientShellWidgetClass,
209 shellWidget, args, j);
212 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
213 layoutArgs, XtNumber(layoutArgs));
215 XtSetArg(args[j], XtNborderWidth, 0); j++;
217 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
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++;
228 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
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++;
236 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
237 XawListHighlight(listwidg, 0);
238 XtAugmentTranslations(listwidg,
239 XtParseTranslationTable(gameListTranslations));
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++;
248 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
249 XtAddCallback(b_load, XtNcallback, callback, client_data);
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++;
259 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
260 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
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++;
270 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
271 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
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++;
281 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
282 XtAddCallback(b_close, XtNcallback, callback, client_data);
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++;
293 XtCreateManagedWidget(_("Filter:"), labelWidgetClass, form, args, j);
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++;
311 XtCreateManagedWidget(_("filtertext"), asciiTextWidgetClass, form, args, j);
312 XtAddEventHandler(filterText, ButtonPressMask, False, SetFocus, (XtPointer) shell);
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++;
322 XtCreateManagedWidget(_("apply"), commandWidgetClass, form, args, j);
323 XtAddCallback(b_filter, XtNcallback, callback, client_data);
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;
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;
346 XSync(xDisplay, False);
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).
352 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
353 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
356 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
357 RootWindowOfScreen(XtScreen(shellWidget)),
358 (fw_width - glc->w) / 2,
359 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
364 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
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);
373 XtRealizeWidget(shell);
374 CatchDeleteWindow(shell, "GameListPopDown");
381 { // [HGM] filter: put in separate routine, to make callable from call-back
386 nstrings = ((ListGame *) gameList.tailPred)->number;
387 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
389 lg = (ListGame *) gameList.head;
392 line = GameListLine(lg->number, &lg->gameInfo);
393 if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) ) {
394 *st++ = line; // [HGM] filter: make adding line conditional
397 lg = (ListGame *) lg->node.succ;
405 { // [HGM] filter: put in separate routine, to make callable from call-back
410 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
411 XawListChange(listwidg, glc->strings, 0, 0, True);
412 XawListHighlight(listwidg, 0);
416 GameListCallback(w, client_data, call_data)
418 XtPointer client_data, call_data;
424 GameListClosure *glc = (GameListClosure *) client_data;
425 XawListReturnStruct *rs;
429 XtSetArg(args[j], XtNlabel, &name); j++;
430 XtGetValues(w, args, j);
432 if (strcmp(name, _("close")) == 0) {
436 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
437 rs = XawListShowCurrent(listwidg);
438 if (strcmp(name, _("load")) == 0) {
439 index = rs->list_index;
441 DisplayError(_("No game selected"), 0);
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);
450 XawListHighlight(listwidg, index);
451 } else if (strcmp(name, _("prev")) == 0) {
452 index = rs->list_index - 1;
454 DisplayError(_("Can't back up any further"), 0);
457 XawListHighlight(listwidg, index);
458 } else if (strcmp(name, _("apply")) == 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...
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);
472 LoadGame(glc->fp, index + 1, glc->filename, True);
477 GameListPopUp(fp, filename)
488 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
489 glc->x = glc->y = -1;
492 if (glc->strings != NULL) {
500 GameListPrepare(); // [HGM] filter: code put in separate routine
504 if (glc->filename != NULL) free(glc->filename);
505 glc->filename = StrSave(filename);
508 if (glc->shell == NULL) {
509 glc->shell = GameListCreate(filename, GameListCallback, glc);
511 GameListReplace(); // [HGM] filter: code put in separate routine
513 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
514 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
515 XtSetValues(glc->shell, args, j);
518 XtPopup(glc->shell, XtGrabNone);
521 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
522 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
529 if (glc == NULL) return;
531 if (glc->strings != NULL) {
544 ShowGameListProc(w, event, prms, nprms)
554 DisplayError(_("There is no game list"), 0);
561 XtPopup(glc->shell, XtGrabNone);
564 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
565 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
570 LoadSelectedProc(w, event, prms, nprms)
577 XawListReturnStruct *rs;
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);
589 LoadGame(glc->fp, index + 1, glc->filename, True);
599 if (glc == NULL) return;
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);
614 XtSetArg(args[j], XtNleftBitmap, None); j++;
615 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
620 GameListHighlight(index)
625 if (glc == NULL || !glc->up) return;
626 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
628 while(*st && atoi(*st)<index) st++,i++;
629 XawListHighlight(listwidg, i);
635 return glc && glc->up;