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, gameListShell;
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++;
140 shell = gameListShell =
141 XtCreatePopupShell(name, topLevelShellWidgetClass,
142 shellWidget, args, j);
144 shell = gameListShell =
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);
221 if(wpGameList.width > 0) {
222 glc->x = wpGameList.x;
223 glc->y = wpGameList.y;
224 glc->w = wpGameList.width;
225 glc->h = wpGameList.height;
235 XtSetArg(args[j], XtNheight, &h1); j++;
236 XtSetArg(args[j], XtNy, &y1); j++;
237 XtGetValues(boardWidget, args, j);
238 glc->w = fw_width * 3/4;
239 glc->h = squareSize * 3;
241 XSync(xDisplay, False);
243 /* This code seems to tickle an X bug if it is executed too soon
244 after xboard starts up. The coordinates get transformed as if
245 the main window was positioned at (0, 0).
247 XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
248 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
251 XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
252 RootWindowOfScreen(XtScreen(shellWidget)),
253 (fw_width - glc->w) / 2,
254 y1 + (h1 - glc->h + appData.borderYoffset) / 2,
259 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
262 XtSetArg(args[j], XtNheight, glc->h); j++;
263 XtSetArg(args[j], XtNwidth, glc->w); j++;
264 XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset); j++;
265 XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset); j++;
266 XtSetValues(shell, args, j);
268 XtRealizeWidget(shell);
269 CatchDeleteWindow(shell, "GameListPopDown");
275 GameListCallback(w, client_data, call_data)
277 XtPointer client_data, call_data;
283 GameListClosure *glc = (GameListClosure *) client_data;
284 XawListReturnStruct *rs;
288 XtSetArg(args[j], XtNlabel, &name); j++;
289 XtGetValues(w, args, j);
291 if (strcmp(name, _("close")) == 0) {
295 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
296 rs = XawListShowCurrent(listwidg);
297 if (strcmp(name, _("load")) == 0) {
298 index = rs->list_index;
300 DisplayError(_("No game selected"), 0);
303 } else if (strcmp(name, _("next")) == 0) {
304 index = rs->list_index + 1;
305 if (index >= ((ListGame *) gameList.tailPred)->number) {
306 DisplayError(_("Can't go forward any further"), 0);
309 XawListHighlight(listwidg, index);
310 } else if (strcmp(name, _("prev")) == 0) {
311 index = rs->list_index - 1;
313 DisplayError(_("Can't back up any further"), 0);
316 XawListHighlight(listwidg, index);
318 if (cmailMsgLoaded) {
319 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
321 LoadGame(glc->fp, index + 1, glc->filename, True);
325 static GameListClosure *glc = NULL;
328 GameListPopUp(fp, filename)
339 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
340 glc->x = glc->y = -1;
343 if (glc->strings != NULL) {
351 nstrings = ((ListGame *) gameList.tailPred)->number;
352 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
354 lg = (ListGame *) gameList.head;
356 *st++ = GameListLine(lg->number, &lg->gameInfo);
357 lg = (ListGame *) lg->node.succ;
363 if (glc->filename != NULL) free(glc->filename);
364 glc->filename = StrSave(filename);
366 if (glc->shell == NULL) {
367 glc->shell = GameListCreate(filename, GameListCallback, glc);
369 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
370 XawListChange(listwidg, glc->strings, 0, 0, True);
371 XawListHighlight(listwidg, 0);
373 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
374 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
375 XtSetValues(glc->shell, args, j);
378 XtPopup(glc->shell, XtGrabNone);
381 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
382 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
389 if (glc == NULL) return;
391 if (glc->strings != NULL) {
404 ShowGameListProc(w, event, prms, nprms)
414 DisplayError(_("There is no game list"), 0);
421 XtPopup(glc->shell, XtGrabNone);
424 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
425 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
430 LoadSelectedProc(w, event, prms, nprms)
437 XawListReturnStruct *rs;
440 if (glc == NULL) return;
441 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
442 rs = XawListShowCurrent(listwidg);
443 index = rs->list_index;
444 if (index < 0) return;
445 if (cmailMsgLoaded) {
446 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
448 LoadGame(glc->fp, index + 1, glc->filename, True);
458 if (glc == NULL) return;
460 XtSetArg(args[j], XtNx, &glc->x); j++;
461 XtSetArg(args[j], XtNy, &glc->y); j++;
462 XtSetArg(args[j], XtNheight, &glc->h); j++;
463 XtSetArg(args[j], XtNwidth, &glc->w); j++;
464 XtGetValues(glc->shell, args, j);
465 wpGameList.x = glc->x - 4;
466 wpGameList.y = glc->y - 23;
467 wpGameList.width = glc->w;
468 wpGameList.height = glc->h;
469 XtPopdown(glc->shell);
470 XtSetKeyboardFocus(shellWidget, formWidget);
473 XtSetArg(args[j], XtNleftBitmap, None); j++;
474 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
479 GameListHighlight(index)
483 if (glc == NULL || !glc->up) return;
484 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
485 XawListHighlight(listwidg, index - 1);
491 return glc && glc->up;