Game-List options dialog for XBoard
[xboard.git] / xgamelistopt.c
1 /*
2  * xgamelistopt.c -- Game list options dialog, part of X front end for XBoard
3  *
4  * Copyright 1995,2009 Free Software Foundation, Inc.
5  * ------------------------------------------------------------------------
6  *
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.
11  *
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.
16  *
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/.  *
19  *
20  *------------------------------------------------------------------------
21  ** See the file ChangeLog for a revision history.  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 # include <string.h>
33 #else /* not STDC_HEADERS */
34 extern char *getenv();
35 # if HAVE_STRING_H
36 #  include <string.h>
37 # else /* not HAVE_STRING_H */
38 #  include <strings.h>
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
41
42 #if HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45
46 #include <X11/Intrinsic.h>
47 #include <X11/StringDefs.h>
48 #include <X11/Shell.h>
49 #include <X11/cursorfont.h>
50 #if USE_XAW3D
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>
63 #else
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>
76 #endif
77
78 #include "common.h"
79 #include "frontend.h"
80 #include "backend.h"
81 #include "xboard.h"
82 #include "xgamelist.h"
83 #include "gettext.h"
84
85 #ifdef ENABLE_NLS
86 # define  _(s) gettext (s)
87 # define N_(s) gettext_noop (s)
88 #else
89 # define  _(s) (s)
90 # define N_(s)  s
91 #endif
92
93
94 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
95 extern Display *xDisplay;
96 extern int squareSize;
97 extern Pixmap xMarkPixmap;
98 extern char *layoutName;
99 extern char lpUserGLT[];
100
101 char gameListOptTranslations[] =
102   "<Btn1Up>(2): LoadSelectedProc() \n \
103    <Key>Return: LoadSelectedProc() \n";
104
105 static Arg layoutArgs[] = {
106     { XtNborderWidth, 0 },
107     { XtNdefaultDistance, 0 }
108 };
109
110 Widget gameListOptShell, listwidg;
111
112 char *strings[20];
113 int stringPtr;
114
115 void GLT_ClearList()
116 {
117     strings[0] = NULL;
118     stringPtr = 0;
119 }
120
121 void GLT_AddToList(char *name)
122 {
123     strings[stringPtr++] = name;
124     strings[stringPtr] = NULL;
125 }
126
127 void GLT_GetFromList(int index, char *name)
128 {
129     strcpy(name, strings[index]);
130 }
131
132 void GLT_DeSelectList()
133 {
134     XawListChange(listwidg, strings, 0, 0, True);
135     XawListHighlight(listwidg, 0);
136 }
137
138 void
139 GameListOptionsPopDown()
140 {
141     Arg args[16];
142     int j;
143
144     if (gameListOptShell == NULL) return;
145     XtPopdown(gameListOptShell);
146     XtDestroyWidget(gameListOptShell);
147     gameListOptShell = 0;
148     XtSetKeyboardFocus(shellWidget, formWidget);
149 }
150
151 void
152 GameListOptionsCallback(w, client_data, call_data)
153      Widget w;
154      XtPointer client_data, call_data;
155 {
156     String name;
157     Arg args[16];
158     int j;
159     Widget listwidg;
160     XawListReturnStruct *rs;
161     int index;
162     char *p;
163
164     j = 0;
165     XtSetArg(args[j], XtNlabel, &name);  j++;
166     XtGetValues(w, args, j);
167
168     if (strcmp(name, _("OK")) == 0) {
169         GLT_ParseList();
170         appData.gameListTags = strdup(lpUserGLT);
171         GameListOptionsPopDown();
172         return;
173     } else
174     if (strcmp(name, _("cancel")) == 0) {
175         GameListOptionsPopDown();
176         return;
177     }
178     listwidg = XtNameToWidget(gameListOptShell, "*form.list");
179     rs = XawListShowCurrent(listwidg);
180     index = rs->list_index;
181     if (index < 0) {
182         DisplayError(_("No tag selected"), 0);
183         return;
184     }
185     p = strings[index];
186     if (strcmp(name, _("down")) == 0) {
187         if(index >= strlen(GLT_ALL_TAGS)) return;
188         strings[index] = strings[index+1];
189         strings[++index] = p;
190     } else
191     if (strcmp(name, _("up")) == 0) {
192         if(index == 0) return;
193         strings[index] = strings[index-1];
194         strings[--index] = p;
195     } else
196     if (strcmp(name, _("factory")) == 0) {
197         strcpy(lpUserGLT, GLT_DEFAULT_TAGS);
198         GLT_TagsToList(lpUserGLT);
199         index = 0;
200     }
201     XawListHighlight(listwidg, index);
202 }
203
204 Widget
205 GameListOptionsCreate()
206 {
207     Arg args[16];
208     Widget shell, form, viewport, layout;
209     Widget b_load, b_loadprev, b_loadnext, b_close, b_cancel;
210     Dimension fw_width;
211     XtPointer client_data = NULL;
212     int j;
213
214     j = 0;
215     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
216     XtGetValues(formWidget, args, j);
217
218     j = 0;
219     XtSetArg(args[j], XtNresizable, True);  j++;
220     XtSetArg(args[j], XtNallowShellResize, True);  j++;
221     shell = gameListOptShell =
222       XtCreatePopupShell("Game-list options", transientShellWidgetClass,
223                          shellWidget, args, j);
224     layout =
225       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
226                             layoutArgs, XtNumber(layoutArgs));
227     j = 0;
228     XtSetArg(args[j], XtNborderWidth, 0); j++;
229     form =
230       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
231
232     j = 0;
233 //    XtSetArg(args[j], XtNlist, glc->strings);  j++;
234     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
235     XtSetArg(args[j], XtNforceColumns, True);  j++;
236     XtSetArg(args[j], XtNverticalList, True);  j++;
237     listwidg = viewport =
238       XtCreateManagedWidget("list", listWidgetClass, form, args, j);
239     XawListHighlight(listwidg, 0);
240     XtAugmentTranslations(listwidg,
241                           XtParseTranslationTable(gameListOptTranslations));
242
243     j = 0;
244     XtSetArg(args[j], XtNfromVert, viewport);  j++;
245     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
246     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
247     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
248     XtSetArg(args[j], XtNright, XtChainLeft); j++;
249     b_load =
250       XtCreateManagedWidget(_("factory"), commandWidgetClass, form, args, j);
251     XtAddCallback(b_load, XtNcallback, GameListOptionsCallback, client_data);
252
253     j = 0;
254     XtSetArg(args[j], XtNfromVert, viewport);  j++;
255     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
256     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
257     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
258     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
259     XtSetArg(args[j], XtNright, XtChainLeft); j++;
260     b_loadprev =
261       XtCreateManagedWidget(_("up"), commandWidgetClass, form, args, j);
262     XtAddCallback(b_loadprev, XtNcallback, GameListOptionsCallback, client_data);
263
264     j = 0;
265     XtSetArg(args[j], XtNfromVert, viewport);  j++;
266     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
267     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
268     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
269     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
270     XtSetArg(args[j], XtNright, XtChainLeft); j++;
271     b_loadnext =
272       XtCreateManagedWidget(_("down"), commandWidgetClass, form, args, j);
273     XtAddCallback(b_loadnext, XtNcallback, GameListOptionsCallback, client_data);
274
275     j = 0;
276     XtSetArg(args[j], XtNfromVert, viewport);  j++;
277     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
278     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
279     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
280     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
281     XtSetArg(args[j], XtNright, XtChainLeft); j++;
282     b_cancel =
283       XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
284     XtAddCallback(b_cancel, XtNcallback, GameListOptionsCallback, client_data);
285
286     j = 0;
287     XtSetArg(args[j], XtNfromVert, viewport);  j++;
288     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
289     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
290     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
291     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
292     XtSetArg(args[j], XtNright, XtChainLeft); j++;
293     b_close =
294       XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
295     XtAddCallback(b_close, XtNcallback, GameListOptionsCallback, client_data);
296
297     strcpy(lpUserGLT, appData.gameListTags);
298     GLT_TagsToList(lpUserGLT);
299
300     XtRealizeWidget(shell);
301     CatchDeleteWindow(shell, "GameListOptionsPopDown");
302
303     return shell;
304 }
305
306 void
307 GameListOptionsPopUp(Widget w, XEvent *event, String *prms, Cardinal *nprms)
308 {
309     Arg args[16];
310     int j, nstrings;
311     Widget listwidg;
312
313     if (gameListOptShell == NULL) {
314         gameListOptShell = GameListOptionsCreate(); 
315     }
316
317     XtPopup(gameListOptShell, XtGrabNone);
318 }
319
320