2 * xgamelist.c -- Game list window, part of X front end for XBoard
5 * Copyright 1995,2009 Free Software Foundation, Inc.
6 * ------------------------------------------------------------------------
8 * GNU XBoard is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or (at
11 * your option) any later version.
13 * GNU XBoard is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses/. *
21 *------------------------------------------------------------------------
22 ** See the file ChangeLog for a revision history. */
29 #include <sys/types.h>
34 #else /* not STDC_HEADERS */
35 extern char *getenv();
38 # else /* not HAVE_STRING_H */
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
47 #include <X11/Intrinsic.h>
48 #include <X11/StringDefs.h>
49 #include <X11/Shell.h>
50 #include <X11/cursorfont.h>
52 #include <X11/Xaw3d/Dialog.h>
53 #include <X11/Xaw3d/Form.h>
54 #include <X11/Xaw3d/List.h>
55 #include <X11/Xaw3d/Label.h>
56 #include <X11/Xaw3d/SimpleMenu.h>
57 #include <X11/Xaw3d/SmeBSB.h>
58 #include <X11/Xaw3d/SmeLine.h>
59 #include <X11/Xaw3d/Box.h>
60 #include <X11/Xaw3d/MenuButton.h>
61 #include <X11/Xaw3d/Text.h>
62 #include <X11/Xaw3d/AsciiText.h>
63 #include <X11/Xaw3d/Viewport.h>
65 #include <X11/Xaw/Dialog.h>
66 #include <X11/Xaw/Form.h>
67 #include <X11/Xaw/List.h>
68 #include <X11/Xaw/Label.h>
69 #include <X11/Xaw/SimpleMenu.h>
70 #include <X11/Xaw/SmeBSB.h>
71 #include <X11/Xaw/SmeLine.h>
72 #include <X11/Xaw/Box.h>
73 #include <X11/Xaw/MenuButton.h>
74 #include <X11/Xaw/Text.h>
75 #include <X11/Xaw/AsciiText.h>
76 #include <X11/Xaw/Viewport.h>
83 #include "xgamelist.h"
87 # define _(s) gettext (s)
88 # define N_(s) gettext_noop (s)
95 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
96 extern Display *xDisplay;
97 extern int squareSize;
98 extern Pixmap xMarkPixmap;
99 extern char *layoutName;
101 char gameListTranslations[] =
102 "<Btn1Up>(2): LoadSelectedProc() \n \
103 <Key>Return: LoadSelectedProc() \n";
115 static Arg layoutArgs[] = {
116 { XtNborderWidth, 0 },
117 { XtNdefaultDistance, 0 }
121 GameListCreate(name, callback, client_data)
123 XtCallbackProc callback;
124 XtPointer client_data;
127 Widget shell, form, viewport, listwidg, layout;
128 Widget b_load, b_loadprev, b_loadnext, b_close;
131 GameListClosure *glc = (GameListClosure *) client_data;
134 XtSetArg(args[j], XtNwidth, &fw_width); j++;
135 XtGetValues(formWidget, args, j);
138 XtSetArg(args[j], XtNresizable, True); j++;
139 XtSetArg(args[j], XtNallowShellResize, True); j++;
142 XtCreatePopupShell(name, topLevelShellWidgetClass,
143 shellWidget, args, j);
146 XtCreatePopupShell(name, transientShellWidgetClass,
147 shellWidget, args, j);
150 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
151 layoutArgs, XtNumber(layoutArgs));
153 XtSetArg(args[j], XtNborderWidth, 0); j++;
155 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
158 XtSetArg(args[j], XtNtop, XtChainTop); j++;
159 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
160 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
161 XtSetArg(args[j], XtNright, XtChainRight); j++;
162 XtSetArg(args[j], XtNresizable, False); j++;
163 XtSetArg(args[j], XtNwidth, fw_width); j++;
164 XtSetArg(args[j], XtNallowVert, True); j++;
166 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
169 XtSetArg(args[j], XtNlist, glc->strings); j++;
170 XtSetArg(args[j], XtNdefaultColumns, 1); j++;
171 XtSetArg(args[j], XtNforceColumns, True); j++;
172 XtSetArg(args[j], XtNverticalList, True); j++;
174 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
175 XawListHighlight(listwidg, 0);
176 XtAugmentTranslations(listwidg,
177 XtParseTranslationTable(gameListTranslations));
180 XtSetArg(args[j], XtNfromVert, viewport); j++;
181 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
182 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
183 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
184 XtSetArg(args[j], XtNright, XtChainLeft); j++;
186 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
187 XtAddCallback(b_load, XtNcallback, callback, client_data);
190 XtSetArg(args[j], XtNfromVert, viewport); j++;
191 XtSetArg(args[j], XtNfromHoriz, b_load); j++;
192 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
193 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
194 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
195 XtSetArg(args[j], XtNright, XtChainLeft); j++;
197 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
198 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
201 XtSetArg(args[j], XtNfromVert, viewport); j++;
202 XtSetArg(args[j], XtNfromHoriz, b_loadprev); j++;
203 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
204 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
205 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
206 XtSetArg(args[j], XtNright, XtChainLeft); j++;
208 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
209 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
212 XtSetArg(args[j], XtNfromVert, viewport); j++;
213 XtSetArg(args[j], XtNfromHoriz, b_loadnext); j++;
214 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
215 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
216 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
217 XtSetArg(args[j], XtNright, XtChainLeft); j++;
219 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
220 XtAddCallback(b_close, XtNcallback, callback, client_data);
229 XtSetArg(args[j], XtNheight, &h1); j++;
230 XtSetArg(args[j], XtNy, &y1); j++;
231 XtGetValues(boardWidget, args, j);
232 glc->w = fw_width * 3/4;
233 glc->h = squareSize * 3;
235 XSync(xDisplay, False);
237 /* This code seems to tickle an X bug if it is executed too soon
238 after xboard starts up. The coordinates get transformed as if
239 the main window was positioned at (0, 0).
241 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
242 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
245 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
246 RootWindowOfScreen(XtScreen(shellWidget)),
247 (fw_width - glc->w) / 2,
248 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
253 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
256 XtSetArg(args[j], XtNheight, glc->h); j++;
257 XtSetArg(args[j], XtNwidth, glc->w); j++;
258 XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset); j++;
259 XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset); j++;
260 XtSetValues(shell, args, j);
262 XtRealizeWidget(shell);
263 CatchDeleteWindow(shell, "GameListPopDown");
269 GameListCallback(w, client_data, call_data)
271 XtPointer client_data, call_data;
277 GameListClosure *glc = (GameListClosure *) client_data;
278 XawListReturnStruct *rs;
282 XtSetArg(args[j], XtNlabel, &name); j++;
283 XtGetValues(w, args, j);
285 if (strcmp(name, _("close")) == 0) {
289 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
290 rs = XawListShowCurrent(listwidg);
291 if (strcmp(name, _("load")) == 0) {
292 index = rs->list_index;
294 DisplayError(_("No game selected"), 0);
297 } else if (strcmp(name, _("next")) == 0) {
298 index = rs->list_index + 1;
299 if (index >= ((ListGame *) gameList.tailPred)->number) {
300 DisplayError(_("Can't go forward any further"), 0);
303 XawListHighlight(listwidg, index);
304 } else if (strcmp(name, _("prev")) == 0) {
305 index = rs->list_index - 1;
307 DisplayError(_("Can't back up any further"), 0);
310 XawListHighlight(listwidg, index);
312 if (cmailMsgLoaded) {
313 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
315 LoadGame(glc->fp, index + 1, glc->filename, True);
319 static GameListClosure *glc = NULL;
322 GameListPopUp(fp, filename)
333 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
334 glc->x = glc->y = -1;
337 if (glc->strings != NULL) {
345 nstrings = ((ListGame *) gameList.tailPred)->number;
346 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
348 lg = (ListGame *) gameList.head;
350 *st++ = GameListLine(lg->number, &lg->gameInfo);
351 lg = (ListGame *) lg->node.succ;
357 if (glc->filename != NULL) free(glc->filename);
358 glc->filename = StrSave(filename);
360 if (glc->shell == NULL) {
361 glc->shell = GameListCreate(filename, GameListCallback, glc);
363 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
364 XawListChange(listwidg, glc->strings, 0, 0, True);
365 XawListHighlight(listwidg, 0);
367 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
368 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
369 XtSetValues(glc->shell, args, j);
372 XtPopup(glc->shell, XtGrabNone);
375 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
376 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
383 if (glc == NULL) return;
385 if (glc->strings != NULL) {
398 ShowGameListProc(w, event, prms, nprms)
408 DisplayError(_("There is no game list"), 0);
415 XtPopup(glc->shell, XtGrabNone);
418 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
419 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
424 LoadSelectedProc(w, event, prms, nprms)
431 XawListReturnStruct *rs;
434 if (glc == NULL) return;
435 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
436 rs = XawListShowCurrent(listwidg);
437 index = rs->list_index;
438 if (index < 0) return;
439 if (cmailMsgLoaded) {
440 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
442 LoadGame(glc->fp, index + 1, glc->filename, True);
452 if (glc == NULL) return;
454 XtSetArg(args[j], XtNx, &glc->x); j++;
455 XtSetArg(args[j], XtNy, &glc->y); j++;
456 XtSetArg(args[j], XtNheight, &glc->h); j++;
457 XtSetArg(args[j], XtNwidth, &glc->w); j++;
458 XtGetValues(glc->shell, args, j);
459 XtPopdown(glc->shell);
460 XtSetKeyboardFocus(shellWidget, formWidget);
463 XtSetArg(args[j], XtNleftBitmap, None); j++;
464 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
469 GameListHighlight(index)
473 if (glc == NULL || !glc->up) return;
474 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
475 XawListHighlight(listwidg, index - 1);