added missing sounds files to be able to compile on windows
[xboard.git] / xgamelist.c
1 /*
2  * xgamelist.c -- Game list window, 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, gameListShell;
95 extern Display *xDisplay;
96 extern int squareSize;
97 extern Pixmap xMarkPixmap;
98 extern char *layoutName;
99
100 char gameListTranslations[] =
101   "<Btn1Up>(2): LoadSelectedProc() \n \
102    <Key>Return: LoadSelectedProc() \n";
103
104 typedef struct {
105     Widget shell;
106     Position x, y;
107     Dimension w, h;
108     Boolean up;
109     FILE *fp;
110     char *filename;
111     char **strings;
112 } GameListClosure;
113
114 static Arg layoutArgs[] = {
115     { XtNborderWidth, 0 },
116     { XtNdefaultDistance, 0 }
117 };
118
119 Widget
120 GameListCreate(name, callback, client_data)
121      char *name;
122      XtCallbackProc callback;
123      XtPointer client_data;
124 {
125     Arg args[16];
126     Widget shell, form, viewport, listwidg, layout;
127     Widget b_load, b_loadprev, b_loadnext, b_close;
128     Dimension fw_width;
129     int j;
130     GameListClosure *glc = (GameListClosure *) client_data;
131
132     j = 0;
133     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
134     XtGetValues(formWidget, args, j);
135
136     j = 0;
137     XtSetArg(args[j], XtNresizable, True);  j++;
138     XtSetArg(args[j], XtNallowShellResize, True);  j++;
139 #if TOPLEVEL
140     shell = gameListShell =
141       XtCreatePopupShell(name, topLevelShellWidgetClass,
142                          shellWidget, args, j);
143 #else
144     shell = gameListShell =
145       XtCreatePopupShell(name, transientShellWidgetClass,
146                          shellWidget, args, j);
147 #endif
148     layout =
149       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
150                             layoutArgs, XtNumber(layoutArgs));
151     j = 0;
152     XtSetArg(args[j], XtNborderWidth, 0); j++;
153     form =
154       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
155
156     j = 0;
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++;
164     viewport =
165       XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
166
167     j = 0;
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++;
172     listwidg = 
173       XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
174     XawListHighlight(listwidg, 0);
175     XtAugmentTranslations(listwidg,
176                           XtParseTranslationTable(gameListTranslations));
177
178     j = 0;
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++;
184     b_load =
185       XtCreateManagedWidget(_("load"), commandWidgetClass, form, args, j);
186     XtAddCallback(b_load, XtNcallback, callback, client_data);
187
188     j = 0;
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++;
195     b_loadprev =
196       XtCreateManagedWidget(_("prev"), commandWidgetClass, form, args, j);
197     XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
198
199     j = 0;
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++;
206     b_loadnext =
207       XtCreateManagedWidget(_("next"), commandWidgetClass, form, args, j);
208     XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
209
210     j = 0;
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++;
217     b_close =
218       XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
219     XtAddCallback(b_close, XtNcallback, callback, client_data);
220
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;
226     }
227
228     if (glc->x == -1) {
229         Position y1;
230         Dimension h1;
231         int xx, yy;
232         Window junk;
233
234         j = 0;
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;
240
241         XSync(xDisplay, False);
242 #ifdef NOTDEF
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).
246         */
247         XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
248                           y1 + (h1 - glc->h + appData.borderYoffset) / 2,
249                           &glc->x, &glc->y);
250 #else /*!NOTDEF*/
251         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
252                               RootWindowOfScreen(XtScreen(shellWidget)),
253                               (fw_width - glc->w) / 2,
254                               y1 + (h1 - glc->h + appData.borderYoffset) / 2,
255                               &xx, &yy, &junk);
256         glc->x = xx;
257         glc->y = yy;
258 #endif /*!NOTDEF*/
259         if (glc->y < 0) glc->y = 0; /*avoid positioning top offscreen*/
260     }
261     j = 0;
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);
267
268     XtRealizeWidget(shell);
269     CatchDeleteWindow(shell, "GameListPopDown");
270
271     return shell;
272 }
273
274 void
275 GameListCallback(w, client_data, call_data)
276      Widget w;
277      XtPointer client_data, call_data;
278 {
279     String name;
280     Arg args[16];
281     int j;
282     Widget listwidg;
283     GameListClosure *glc = (GameListClosure *) client_data;
284     XawListReturnStruct *rs;
285     int index;
286
287     j = 0;
288     XtSetArg(args[j], XtNlabel, &name);  j++;
289     XtGetValues(w, args, j);
290
291     if (strcmp(name, _("close")) == 0) {
292         GameListPopDown();
293         return;
294     }
295     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
296     rs = XawListShowCurrent(listwidg);
297     if (strcmp(name, _("load")) == 0) {
298         index = rs->list_index;
299         if (index < 0) {
300             DisplayError(_("No game selected"), 0);
301             return;
302         }
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);
307             return;
308         }
309         XawListHighlight(listwidg, index);
310     } else if (strcmp(name, _("prev")) == 0) {
311         index = rs->list_index - 1;
312         if (index < 0) {
313             DisplayError(_("Can't back up any further"), 0);
314             return;
315         }
316         XawListHighlight(listwidg, index);
317     }
318     if (cmailMsgLoaded) {
319         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
320     } else {
321         LoadGame(glc->fp, index + 1, glc->filename, True);
322     }
323 }
324
325 static GameListClosure *glc = NULL;
326
327 void
328 GameListPopUp(fp, filename)
329      FILE *fp;
330      char *filename;
331 {
332     Arg args[16];
333     int j, nstrings;
334     Widget listwidg;
335     ListGame *lg;
336     char **st;
337
338     if (glc == NULL) {
339         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
340         glc->x = glc->y = -1;
341     }
342
343     if (glc->strings != NULL) {
344         st = glc->strings;
345         while (*st) {
346             free(*st++);
347         }
348         free(glc->strings);
349     }
350
351     nstrings = ((ListGame *) gameList.tailPred)->number;
352     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
353     st = glc->strings;
354     lg = (ListGame *) gameList.head;
355     while (nstrings--) {
356         *st++ = GameListLine(lg->number, &lg->gameInfo);
357         lg = (ListGame *) lg->node.succ;
358      }
359     *st = NULL;
360
361     glc->fp = fp;
362
363     if (glc->filename != NULL) free(glc->filename);
364     glc->filename = StrSave(filename);
365
366     if (glc->shell == NULL) {
367         glc->shell = GameListCreate(filename, GameListCallback, glc); 
368     } else {
369         listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
370         XawListChange(listwidg, glc->strings, 0, 0, True);
371         XawListHighlight(listwidg, 0);
372         j = 0;
373         XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
374         XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
375         XtSetValues(glc->shell, args, j);
376     }
377
378     XtPopup(glc->shell, XtGrabNone);
379     glc->up = True;
380     j = 0;
381     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
382     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
383                 args, j);
384 }
385
386 void
387 GameListDestroy()
388 {
389     if (glc == NULL) return;
390     GameListPopDown();
391     if (glc->strings != NULL) {
392         char **st;
393         st = glc->strings;
394         while (*st) {
395             free(*st++);
396         }
397         free(glc->strings);
398     }
399     free(glc);
400     glc = NULL;
401 }
402
403 void
404 ShowGameListProc(w, event, prms, nprms)
405      Widget w;
406      XEvent *event;
407      String *prms;
408      Cardinal *nprms;
409 {
410     Arg args[16];
411     int j;
412
413     if (glc == NULL) {
414         DisplayError(_("There is no game list"), 0);
415         return;
416     }
417     if (glc->up) {
418         GameListPopDown();
419         return;
420     }
421     XtPopup(glc->shell, XtGrabNone);
422     glc->up = True;
423     j = 0;
424     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
425     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
426                 args, j);
427 }
428
429 void
430 LoadSelectedProc(w, event, prms, nprms)
431      Widget w;
432      XEvent *event;
433      String *prms;
434      Cardinal *nprms;
435 {
436     Widget listwidg;
437     XawListReturnStruct *rs;
438     int index;
439
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);
447     } else {
448         LoadGame(glc->fp, index + 1, glc->filename, True);
449     }
450 }
451
452 void
453 GameListPopDown()
454 {
455     Arg args[16];
456     int j;
457
458     if (glc == NULL) return;
459     j = 0;
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);
471     glc->up = False;
472     j = 0;
473     XtSetArg(args[j], XtNleftBitmap, None); j++;
474     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
475                 args, j);
476 }
477
478 void
479 GameListHighlight(index)
480      int index;
481 {
482     Widget listwidg;
483     if (glc == NULL || !glc->up) return;
484     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
485     XawListHighlight(listwidg, index - 1);
486 }
487
488 Boolean
489 GameListIsUp()
490 {
491     return glc && glc->up;
492 }