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";
109 char filterTranslations[] =
110 "<Key>Return: SetFilterProc() \n";
121 static GameListClosure *glc = NULL;
123 static Arg layoutArgs[] = {
124 { XtNborderWidth, 0 },
125 { XtNdefaultDistance, 0 }
129 GameListCreate(name, callback, client_data)
131 XtCallbackProc callback;
132 XtPointer client_data;
135 Widget shell, form, viewport, listwidg, layout, label;
136 Widget b_load, b_loadprev, b_loadnext, b_close, b_filter;
139 GameListClosure *glc = (GameListClosure *) client_data;
142 XtSetArg(args[j], XtNwidth, &fw_width); j++;
143 XtGetValues(formWidget, args, j);
146 XtSetArg(args[j], XtNresizable, True); j++;
147 XtSetArg(args[j], XtNallowShellResize, True); j++;
149 shell = gameListShell =
150 XtCreatePopupShell(name, topLevelShellWidgetClass,
151 shellWidget, args, j);
153 shell = gameListShell =
154 XtCreatePopupShell(name, transientShellWidgetClass,
155 shellWidget, args, j);
158 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
159 layoutArgs, XtNumber(layoutArgs));
161 XtSetArg(args[j], XtNborderWidth, 0); j++;
163 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
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++;
174 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
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++;
182 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
183 XawListHighlight(listwidg, 0);
184 XtAugmentTranslations(listwidg,
185 XtParseTranslationTable(gameListTranslations));
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++;
194 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
195 XtAddCallback(b_load, XtNcallback, callback, client_data);
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++;
205 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
206 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
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++;
216 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
217 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
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++;
227 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
228 XtAddCallback(b_close, XtNcallback, callback, client_data);
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++;
239 XtCreateManagedWidget(_("Filter:"), labelWidgetClass, form, args, j);
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++;
257 XtCreateManagedWidget(_("filtertext"), asciiTextWidgetClass, form, args, j);
258 XtAddEventHandler(filterText, ButtonPressMask, False, SetFocus, (XtPointer) shell);
259 XtOverrideTranslations(filterText,
260 XtParseTranslationTable(filterTranslations));
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++;
270 XtCreateManagedWidget(_("apply"), commandWidgetClass, form, args, j);
271 XtAddCallback(b_filter, XtNcallback, callback, client_data);
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;
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;
294 XSync(xDisplay, False);
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).
300 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
301 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
304 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
305 RootWindowOfScreen(XtScreen(shellWidget)),
306 (fw_width - glc->w) / 2,
307 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
312 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
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);
321 XtRealizeWidget(shell);
322 CatchDeleteWindow(shell, "GameListPopDown");
329 { // [HGM] filter: put in separate routine, to make callable from call-back
334 nstrings = ((ListGame *) gameList.tailPred)->number;
335 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
337 lg = (ListGame *) gameList.head;
340 line = GameListLine(lg->number, &lg->gameInfo);
341 if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) ) {
342 *st++ = line; // [HGM] filter: make adding line conditional
345 lg = (ListGame *) lg->node.succ;
353 { // [HGM] filter: put in separate routine, to make callable from call-back
358 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
359 XawListChange(listwidg, glc->strings, 0, 0, True);
360 XawListHighlight(listwidg, 0);
364 GameListCallback(w, client_data, call_data)
366 XtPointer client_data, call_data;
372 GameListClosure *glc = (GameListClosure *) client_data;
373 XawListReturnStruct *rs;
377 XtSetArg(args[j], XtNlabel, &name); j++;
378 XtGetValues(w, args, j);
380 if (strcmp(name, _("close")) == 0) {
384 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
385 rs = XawListShowCurrent(listwidg);
386 if (strcmp(name, _("load")) == 0) {
387 index = rs->list_index;
389 DisplayError(_("No game selected"), 0);
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);
398 XawListHighlight(listwidg, index);
399 } else if (strcmp(name, _("prev")) == 0) {
400 index = rs->list_index - 1;
402 DisplayError(_("Can't back up any further"), 0);
405 XawListHighlight(listwidg, index);
406 } else if (strcmp(name, _("apply")) == 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...
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);
420 LoadGame(glc->fp, index + 1, glc->filename, True);
425 GameListPopUp(fp, filename)
436 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
437 glc->x = glc->y = -1;
440 if (glc->strings != NULL) {
448 GameListPrepare(); // [HGM] filter: code put in separate routine
452 if (glc->filename != NULL) free(glc->filename);
453 glc->filename = StrSave(filename);
456 if (glc->shell == NULL) {
457 glc->shell = GameListCreate(filename, GameListCallback, glc);
459 GameListReplace(); // [HGM] filter: code put in separate routine
461 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
462 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
463 XtSetValues(glc->shell, args, j);
466 XtPopup(glc->shell, XtGrabNone);
469 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
470 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
477 if (glc == NULL) return;
479 if (glc->strings != NULL) {
492 ShowGameListProc(w, event, prms, nprms)
502 DisplayError(_("There is no game list"), 0);
509 XtPopup(glc->shell, XtGrabNone);
512 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
513 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
518 LoadSelectedProc(w, event, prms, nprms)
525 XawListReturnStruct *rs;
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);
537 LoadGame(glc->fp, index + 1, glc->filename, True);
542 SetFilterProc(w, event, prms, nprms)
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);
559 XtSetArg(args[j], XtNdisplayCaret, False); j++;
560 XtSetValues(filterText, args, j);
561 XtSetKeyboardFocus(glc->shell, list);
570 if (glc == NULL) return;
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);
585 XtSetArg(args[j], XtNleftBitmap, None); j++;
586 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
591 GameListHighlight(index)
596 if (glc == NULL || !glc->up) return;
597 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
599 while(*st && atoi(*st)<index) st++,i++;
600 XawListHighlight(listwidg, i);
606 return glc && glc->up;