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 }
127 GameListCreate(name, callback, client_data)
129 XtCallbackProc callback;
130 XtPointer client_data;
133 Widget shell, form, viewport, listwidg, layout, label;
134 Widget b_load, b_loadprev, b_loadnext, b_close, b_filter;
137 GameListClosure *glc = (GameListClosure *) client_data;
140 XtSetArg(args[j], XtNwidth, &fw_width); j++;
141 XtGetValues(formWidget, args, j);
144 XtSetArg(args[j], XtNresizable, True); j++;
145 XtSetArg(args[j], XtNallowShellResize, True); j++;
147 shell = gameListShell =
148 XtCreatePopupShell(name, topLevelShellWidgetClass,
149 shellWidget, args, j);
151 shell = gameListShell =
152 XtCreatePopupShell(name, transientShellWidgetClass,
153 shellWidget, args, j);
156 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
157 layoutArgs, XtNumber(layoutArgs));
159 XtSetArg(args[j], XtNborderWidth, 0); j++;
161 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
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++;
172 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
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++;
180 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
181 XawListHighlight(listwidg, 0);
182 XtAugmentTranslations(listwidg,
183 XtParseTranslationTable(gameListTranslations));
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++;
192 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
193 XtAddCallback(b_load, XtNcallback, callback, client_data);
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++;
203 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
204 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
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++;
214 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
215 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
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++;
225 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
226 XtAddCallback(b_close, XtNcallback, callback, client_data);
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++;
237 XtCreateManagedWidget(_("Filter:"), labelWidgetClass, form, args, j);
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++;
255 XtCreateManagedWidget(_("filtertext"), asciiTextWidgetClass, form, args, j);
256 XtAddEventHandler(filterText, ButtonPressMask, False, SetFocus, (XtPointer) shell);
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++;
266 XtCreateManagedWidget(_("apply"), commandWidgetClass, form, args, j);
267 XtAddCallback(b_filter, XtNcallback, callback, client_data);
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;
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;
290 XSync(xDisplay, False);
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).
296 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
297 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
300 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
301 RootWindowOfScreen(XtScreen(shellWidget)),
302 (fw_width - glc->w) / 2,
303 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
308 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
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);
317 XtRealizeWidget(shell);
318 CatchDeleteWindow(shell, "GameListPopDown");
325 { // [HGM] filter: put in separate routine, to make callable from call-back
330 nstrings = ((ListGame *) gameList.tailPred)->number;
331 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
333 lg = (ListGame *) gameList.head;
336 line = GameListLine(lg->number, &lg->gameInfo);
337 if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) ) {
338 *st++ = line; // [HGM] filter: make adding line conditional
341 lg = (ListGame *) lg->node.succ;
349 { // [HGM] filter: put in separate routine, to make callable from call-back
354 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
355 XawListChange(listwidg, glc->strings, 0, 0, True);
356 XawListHighlight(listwidg, 0);
360 GameListCallback(w, client_data, call_data)
362 XtPointer client_data, call_data;
368 GameListClosure *glc = (GameListClosure *) client_data;
369 XawListReturnStruct *rs;
373 XtSetArg(args[j], XtNlabel, &name); j++;
374 XtGetValues(w, args, j);
376 if (strcmp(name, _("close")) == 0) {
380 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
381 rs = XawListShowCurrent(listwidg);
382 if (strcmp(name, _("load")) == 0) {
383 index = rs->list_index;
385 DisplayError(_("No game selected"), 0);
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);
394 XawListHighlight(listwidg, index);
395 } else if (strcmp(name, _("prev")) == 0) {
396 index = rs->list_index - 1;
398 DisplayError(_("Can't back up any further"), 0);
401 XawListHighlight(listwidg, index);
402 } else if (strcmp(name, _("apply")) == 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...
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);
416 LoadGame(glc->fp, index + 1, glc->filename, True);
421 GameListPopUp(fp, filename)
432 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
433 glc->x = glc->y = -1;
436 if (glc->strings != NULL) {
444 GameListPrepare(); // [HGM] filter: code put in separate routine
448 if (glc->filename != NULL) free(glc->filename);
449 glc->filename = StrSave(filename);
452 if (glc->shell == NULL) {
453 glc->shell = GameListCreate(filename, GameListCallback, glc);
455 GameListReplace(); // [HGM] filter: code put in separate routine
457 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
458 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
459 XtSetValues(glc->shell, args, j);
462 XtPopup(glc->shell, XtGrabNone);
465 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
466 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
473 if (glc == NULL) return;
475 if (glc->strings != NULL) {
488 ShowGameListProc(w, event, prms, nprms)
498 DisplayError(_("There is no game list"), 0);
505 XtPopup(glc->shell, XtGrabNone);
508 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
509 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
514 LoadSelectedProc(w, event, prms, nprms)
521 XawListReturnStruct *rs;
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);
533 LoadGame(glc->fp, index + 1, glc->filename, True);
543 if (glc == NULL) return;
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);
558 XtSetArg(args[j], XtNleftBitmap, None); j++;
559 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
564 GameListHighlight(index)
569 if (glc == NULL || !glc->up) return;
570 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
572 while(*st && atoi(*st)<index) st++,i++;
573 XawListHighlight(listwidg, i);
579 return glc && glc->up;