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 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
95 extern Display *xDisplay;
96 extern int squareSize;
97 extern Pixmap xMarkPixmap;
98 extern char *layoutName;
100 char gameListTranslations[] =
101 "<Btn1Up>(2): LoadSelectedProc() \n \
102 <Key>Return: LoadSelectedProc() \n";
114 static Arg layoutArgs[] = {
115 { XtNborderWidth, 0 },
116 { XtNdefaultDistance, 0 }
120 GameListCreate(name, callback, client_data)
122 XtCallbackProc callback;
123 XtPointer client_data;
126 Widget shell, form, viewport, listwidg, layout;
127 Widget b_load, b_loadprev, b_loadnext, b_close;
130 GameListClosure *glc = (GameListClosure *) client_data;
133 XtSetArg(args[j], XtNwidth, &fw_width); j++;
134 XtGetValues(formWidget, args, j);
137 XtSetArg(args[j], XtNresizable, True); j++;
138 XtSetArg(args[j], XtNallowShellResize, True); j++;
141 XtCreatePopupShell(name, topLevelShellWidgetClass,
142 shellWidget, args, j);
145 XtCreatePopupShell(name, transientShellWidgetClass,
146 shellWidget, args, j);
149 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
150 layoutArgs, XtNumber(layoutArgs));
152 XtSetArg(args[j], XtNborderWidth, 0); j++;
154 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
157 XtSetArg(args[j], XtNtop, XtChainTop); j++;
158 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
159 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
160 XtSetArg(args[j], XtNright, XtChainRight); j++;
161 XtSetArg(args[j], XtNresizable, False); j++;
162 XtSetArg(args[j], XtNwidth, fw_width); j++;
163 XtSetArg(args[j], XtNallowVert, True); j++;
165 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
168 XtSetArg(args[j], XtNlist, glc->strings); j++;
169 XtSetArg(args[j], XtNdefaultColumns, 1); j++;
170 XtSetArg(args[j], XtNforceColumns, True); j++;
171 XtSetArg(args[j], XtNverticalList, True); j++;
173 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
174 XawListHighlight(listwidg, 0);
175 XtAugmentTranslations(listwidg,
176 XtParseTranslationTable(gameListTranslations));
179 XtSetArg(args[j], XtNfromVert, viewport); j++;
180 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
181 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
182 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
183 XtSetArg(args[j], XtNright, XtChainLeft); j++;
185 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
186 XtAddCallback(b_load, XtNcallback, callback, client_data);
189 XtSetArg(args[j], XtNfromVert, viewport); j++;
190 XtSetArg(args[j], XtNfromHoriz, b_load); j++;
191 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
192 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
193 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
194 XtSetArg(args[j], XtNright, XtChainLeft); j++;
196 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
197 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
200 XtSetArg(args[j], XtNfromVert, viewport); j++;
201 XtSetArg(args[j], XtNfromHoriz, b_loadprev); j++;
202 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
203 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
204 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
205 XtSetArg(args[j], XtNright, XtChainLeft); j++;
207 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
208 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
211 XtSetArg(args[j], XtNfromVert, viewport); j++;
212 XtSetArg(args[j], XtNfromHoriz, b_loadnext); j++;
213 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
214 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
215 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
216 XtSetArg(args[j], XtNright, XtChainLeft); j++;
218 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
219 XtAddCallback(b_close, XtNcallback, callback, client_data);
228 XtSetArg(args[j], XtNheight, &h1); j++;
229 XtSetArg(args[j], XtNy, &y1); j++;
230 XtGetValues(boardWidget, args, j);
231 glc->w = fw_width * 3/4;
232 glc->h = squareSize * 3;
234 XSync(xDisplay, False);
236 /* This code seems to tickle an X bug if it is executed too soon
237 after xboard starts up. The coordinates get transformed as if
238 the main window was positioned at (0, 0).
240 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
241 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
244 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
245 RootWindowOfScreen(XtScreen(shellWidget)),
246 (fw_width - glc->w) / 2,
247 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
252 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
255 XtSetArg(args[j], XtNheight, glc->h); j++;
256 XtSetArg(args[j], XtNwidth, glc->w); j++;
257 XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset); j++;
258 XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset); j++;
259 XtSetValues(shell, args, j);
261 XtRealizeWidget(shell);
262 CatchDeleteWindow(shell, "GameListPopDown");
268 GameListCallback(w, client_data, call_data)
270 XtPointer client_data, call_data;
276 GameListClosure *glc = (GameListClosure *) client_data;
277 XawListReturnStruct *rs;
281 XtSetArg(args[j], XtNlabel, &name); j++;
282 XtGetValues(w, args, j);
284 if (strcmp(name, _("close")) == 0) {
288 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
289 rs = XawListShowCurrent(listwidg);
290 if (strcmp(name, _("load")) == 0) {
291 index = rs->list_index;
293 DisplayError(_("No game selected"), 0);
296 } else if (strcmp(name, _("next")) == 0) {
297 index = rs->list_index + 1;
298 if (index >= ((ListGame *) gameList.tailPred)->number) {
299 DisplayError(_("Can't go forward any further"), 0);
302 XawListHighlight(listwidg, index);
303 } else if (strcmp(name, _("prev")) == 0) {
304 index = rs->list_index - 1;
306 DisplayError(_("Can't back up any further"), 0);
309 XawListHighlight(listwidg, index);
311 if (cmailMsgLoaded) {
312 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
314 LoadGame(glc->fp, index + 1, glc->filename, True);
318 static GameListClosure *glc = NULL;
321 GameListPopUp(fp, filename)
332 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
333 glc->x = glc->y = -1;
336 if (glc->strings != NULL) {
344 nstrings = ((ListGame *) gameList.tailPred)->number;
345 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
347 lg = (ListGame *) gameList.head;
349 *st++ = GameListLine(lg->number, &lg->gameInfo);
350 lg = (ListGame *) lg->node.succ;
356 if (glc->filename != NULL) free(glc->filename);
357 glc->filename = StrSave(filename);
359 if (glc->shell == NULL) {
360 glc->shell = GameListCreate(filename, GameListCallback, glc);
362 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
363 XawListChange(listwidg, glc->strings, 0, 0, True);
364 XawListHighlight(listwidg, 0);
366 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
367 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
368 XtSetValues(glc->shell, args, j);
371 XtPopup(glc->shell, XtGrabNone);
374 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
375 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
382 if (glc == NULL) return;
384 if (glc->strings != NULL) {
397 ShowGameListProc(w, event, prms, nprms)
407 DisplayError(_("There is no game list"), 0);
414 XtPopup(glc->shell, XtGrabNone);
417 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
418 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
423 LoadSelectedProc(w, event, prms, nprms)
430 XawListReturnStruct *rs;
433 if (glc == NULL) return;
434 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
435 rs = XawListShowCurrent(listwidg);
436 index = rs->list_index;
437 if (index < 0) return;
438 if (cmailMsgLoaded) {
439 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
441 LoadGame(glc->fp, index + 1, glc->filename, True);
451 if (glc == NULL) return;
453 XtSetArg(args[j], XtNx, &glc->x); j++;
454 XtSetArg(args[j], XtNy, &glc->y); j++;
455 XtSetArg(args[j], XtNheight, &glc->h); j++;
456 XtSetArg(args[j], XtNwidth, &glc->w); j++;
457 XtGetValues(glc->shell, args, j);
458 XtPopdown(glc->shell);
459 XtSetKeyboardFocus(shellWidget, formWidget);
462 XtSetArg(args[j], XtNleftBitmap, None); j++;
463 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
468 GameListHighlight(index)
472 if (glc == NULL || !glc->up) return;
473 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
474 XawListHighlight(listwidg, index - 1);