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, boardWidget, menuBarWidget, gameListShell;
95 extern int squareSize;
96 extern Pixmap xMarkPixmap;
97 extern char *layoutName;
99 char gameListTranslations[] =
100 "<Btn1Up>(2): LoadSelectedProc() \n \
101 <Key>Return: LoadSelectedProc() \n";
113 static Arg layoutArgs[] = {
114 { XtNborderWidth, 0 },
115 { XtNdefaultDistance, 0 }
119 GameListCreate(name, callback, client_data)
121 XtCallbackProc callback;
122 XtPointer client_data;
125 Widget shell, form, viewport, listwidg, layout;
126 Widget b_load, b_loadprev, b_loadnext, b_close;
129 GameListClosure *glc = (GameListClosure *) client_data;
132 XtSetArg(args[j], XtNwidth, &fw_width); j++;
133 XtGetValues(formWidget, args, j);
136 XtSetArg(args[j], XtNresizable, True); j++;
137 XtSetArg(args[j], XtNallowShellResize, True); j++;
139 // shell = gameListShell =
140 // XtCreatePopupShell(name, topLevelShellWidgetClass,
141 // shellWidget, args, j);
143 // shell = gameListShell =
144 // XtCreatePopupShell(name, transientShellWidgetClass,
145 // shellWidget, args, j);
148 XtCreateManagedWidget(layoutName, formWidgetClass, shell,
149 layoutArgs, XtNumber(layoutArgs));
151 XtSetArg(args[j], XtNborderWidth, 0); j++;
153 XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
156 XtSetArg(args[j], XtNtop, XtChainTop); j++;
157 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
158 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
159 XtSetArg(args[j], XtNright, XtChainRight); j++;
160 XtSetArg(args[j], XtNresizable, False); j++;
161 XtSetArg(args[j], XtNwidth, fw_width); j++;
162 XtSetArg(args[j], XtNallowVert, True); j++;
164 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
167 XtSetArg(args[j], XtNlist, glc->strings); j++;
168 XtSetArg(args[j], XtNdefaultColumns, 1); j++;
169 XtSetArg(args[j], XtNforceColumns, True); j++;
170 XtSetArg(args[j], XtNverticalList, True); j++;
172 XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
173 XawListHighlight(listwidg, 0);
174 XtAugmentTranslations(listwidg,
175 XtParseTranslationTable(gameListTranslations));
178 XtSetArg(args[j], XtNfromVert, viewport); j++;
179 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
180 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
181 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
182 XtSetArg(args[j], XtNright, XtChainLeft); j++;
184 XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
185 XtAddCallback(b_load, XtNcallback, callback, client_data);
188 XtSetArg(args[j], XtNfromVert, viewport); j++;
189 XtSetArg(args[j], XtNfromHoriz, b_load); j++;
190 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
191 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
192 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
193 XtSetArg(args[j], XtNright, XtChainLeft); j++;
195 XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
196 XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
199 XtSetArg(args[j], XtNfromVert, viewport); j++;
200 XtSetArg(args[j], XtNfromHoriz, b_loadprev); j++;
201 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
202 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
203 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
204 XtSetArg(args[j], XtNright, XtChainLeft); j++;
206 XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
207 XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
210 XtSetArg(args[j], XtNfromVert, viewport); j++;
211 XtSetArg(args[j], XtNfromHoriz, b_loadnext); j++;
212 XtSetArg(args[j], XtNtop, XtChainBottom); j++;
213 XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
214 XtSetArg(args[j], XtNleft, XtChainLeft); j++;
215 XtSetArg(args[j], XtNright, XtChainLeft); j++;
217 XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
218 XtAddCallback(b_close, XtNcallback, callback, client_data);
220 if(wpGameList.width > 0) {
221 glc->x = wpGameList.x;
222 glc->y = wpGameList.y;
223 glc->w = wpGameList.width;
224 glc->h = wpGameList.height;
234 XtSetArg(args[j], XtNheight, &h1); j++;
235 XtSetArg(args[j], XtNy, &y1); j++;
236 XtGetValues(boardWidget, args, j);
237 glc->w = fw_width * 3/4;
238 glc->h = squareSize * 3;
240 // XSync(xDisplay, False);
242 /* This code seems to tickle an X bug if it is executed too soon
243 after xboard starts up. The coordinates get transformed as if
244 the main window was positioned at (0, 0).
246 // XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
247 // y1 + (h1 - glc->h + appData.borderYoffset) / 2,
248 // &glc->x, &glc->y);
250 // XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
251 // RootWindowOfScreen(XtScreen(shellWidget)),
252 // (fw_width - glc->w) / 2,
253 // y1 + (h1 - glc->h + appData.borderYoffset) / 2,
258 if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
261 XtSetArg(args[j], XtNheight, glc->h); j++;
262 XtSetArg(args[j], XtNwidth, glc->w); j++;
263 XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset); j++;
264 XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset); j++;
265 XtSetValues(shell, args, j);
267 XtRealizeWidget(shell);
268 // CatchDeleteWindow(shell, "GameListPopDown");
274 GameListCallback(w, client_data, call_data)
276 XtPointer client_data, call_data;
282 GameListClosure *glc = (GameListClosure *) client_data;
283 XawListReturnStruct *rs;
287 XtSetArg(args[j], XtNlabel, &name); j++;
288 XtGetValues(w, args, j);
290 if (strcmp(name, _("close")) == 0) {
294 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
295 rs = XawListShowCurrent(listwidg);
296 if (strcmp(name, _("load")) == 0) {
297 index = rs->list_index;
299 DisplayError(_("No game selected"), 0);
302 } else if (strcmp(name, _("next")) == 0) {
303 index = rs->list_index + 1;
304 if (index >= ((ListGame *) gameList.tailPred)->number) {
305 DisplayError(_("Can't go forward any further"), 0);
308 XawListHighlight(listwidg, index);
309 } else if (strcmp(name, _("prev")) == 0) {
310 index = rs->list_index - 1;
312 DisplayError(_("Can't back up any further"), 0);
315 XawListHighlight(listwidg, index);
317 if (cmailMsgLoaded) {
318 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
320 LoadGame(glc->fp, index + 1, glc->filename, True);
324 static GameListClosure *glc = NULL;
327 GameListPopUp(fp, filename)
338 glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
339 glc->x = glc->y = -1;
342 if (glc->strings != NULL) {
350 nstrings = ((ListGame *) gameList.tailPred)->number;
351 glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
353 lg = (ListGame *) gameList.head;
355 *st++ = GameListLine(lg->number, &lg->gameInfo);
356 lg = (ListGame *) lg->node.succ;
362 if (glc->filename != NULL) free(glc->filename);
363 glc->filename = StrSave(filename);
365 if (glc->shell == NULL) {
366 glc->shell = GameListCreate(filename, GameListCallback, glc);
368 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
369 XawListChange(listwidg, glc->strings, 0, 0, True);
370 XawListHighlight(listwidg, 0);
372 XtSetArg(args[j], XtNiconName, (XtArgVal) filename); j++;
373 XtSetArg(args[j], XtNtitle, (XtArgVal) filename); j++;
374 XtSetValues(glc->shell, args, j);
377 XtPopup(glc->shell, XtGrabNone);
380 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
381 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
388 if (glc == NULL) return;
390 if (glc->strings != NULL) {
403 ShowGameListProc(w, event, prms, nprms)
413 DisplayError(_("There is no game list"), 0);
420 XtPopup(glc->shell, XtGrabNone);
423 XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
424 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
429 LoadSelectedProc(w, event, prms, nprms)
436 XawListReturnStruct *rs;
439 if (glc == NULL) return;
440 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
441 rs = XawListShowCurrent(listwidg);
442 index = rs->list_index;
443 if (index < 0) return;
444 if (cmailMsgLoaded) {
445 CmailLoadGame(glc->fp, index + 1, glc->filename, True);
447 LoadGame(glc->fp, index + 1, glc->filename, True);
457 if (glc == NULL) return;
459 XtSetArg(args[j], XtNx, &glc->x); j++;
460 XtSetArg(args[j], XtNy, &glc->y); j++;
461 XtSetArg(args[j], XtNheight, &glc->h); j++;
462 XtSetArg(args[j], XtNwidth, &glc->w); j++;
463 XtGetValues(glc->shell, args, j);
464 wpGameList.x = glc->x - 4;
465 wpGameList.y = glc->y - 23;
466 wpGameList.width = glc->w;
467 wpGameList.height = glc->h;
468 XtPopdown(glc->shell);
469 // XtSetKeyboardFocus(shellWidget, formWidget);
472 XtSetArg(args[j], XtNleftBitmap, None); j++;
473 XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
478 GameListHighlight(index)
482 if (glc == NULL || !glc->up) return;
483 listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
484 XawListHighlight(listwidg, index - 1);
490 return glc && glc->up;