started a bit on the GameList window
[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 <gtk/gtk.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #if STDC_HEADERS
32 # include <stdlib.h>
33 # include <string.h>
34 #else /* not STDC_HEADERS */
35 extern char *getenv();
36 # if HAVE_STRING_H
37 #  include <string.h>
38 # else /* not HAVE_STRING_H */
39 #  include <strings.h>
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
42
43 #if HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47 #include <X11/Intrinsic.h>
48 #include <X11/StringDefs.h>
49 #include <X11/Shell.h>
50 #include <X11/cursorfont.h>
51 #if USE_XAW3D
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>
64 #else
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>
77 #endif
78
79 #include "common.h"
80 #include "frontend.h"
81 #include "backend.h"
82 #include "xboard.h"
83 #include "xgamelist.h"
84 #include "gettext.h"
85
86 #ifdef ENABLE_NLS
87 # define  _(s) gettext (s)
88 # define N_(s) gettext_noop (s)
89 #else
90 # define  _(s) (s)
91 # define N_(s)  s
92 #endif
93
94 extern GtkWidget               *GUI_GameList;
95 extern GtkListStore            *LIST_GameList;
96
97
98 extern Widget formWidget, boardWidget, menuBarWidget, gameListShell;
99 extern int squareSize;
100 extern Pixmap xMarkPixmap;
101 extern char *layoutName;
102
103 char gameListTranslations[] =
104   "<Btn1Up>(2): LoadSelectedProc() \n \
105    <Key>Return: LoadSelectedProc() \n";
106
107 typedef struct {
108     Widget shell;
109     Position x, y;
110     Dimension w, h;
111     Boolean up;
112     FILE *fp;
113     char *filename;
114     char **strings;
115 } GameListClosure;
116
117 static Arg layoutArgs[] = {
118     { XtNborderWidth, 0 },
119     { XtNdefaultDistance, 0 }
120 };
121
122 Widget
123 GameListCreate(name, callback, client_data)
124      char *name;
125      XtCallbackProc callback;
126      XtPointer client_data;
127 {
128   return;
129 }
130
131 void
132 GameListCallback(w, client_data, call_data)
133      Widget w;
134      XtPointer client_data, call_data;
135 {
136     String name;
137     Arg args[16];
138     int j;
139     Widget listwidg;
140     GameListClosure *glc = (GameListClosure *) client_data;
141     XawListReturnStruct *rs;
142     int index;
143
144     j = 0;
145     XtSetArg(args[j], XtNlabel, &name);  j++;
146     XtGetValues(w, args, j);
147
148     if (strcmp(name, _("close")) == 0) {
149         GameListPopDown();
150         return;
151     }
152     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
153     rs = XawListShowCurrent(listwidg);
154     if (strcmp(name, _("load")) == 0) {
155         index = rs->list_index;
156         if (index < 0) {
157             DisplayError(_("No game selected"), 0);
158             return;
159         }
160     } else if (strcmp(name, _("next")) == 0) {
161         index = rs->list_index + 1;
162         if (index >= ((ListGame *) gameList.tailPred)->number) {
163             DisplayError(_("Can't go forward any further"), 0);
164             return;
165         }
166         XawListHighlight(listwidg, index);
167     } else if (strcmp(name, _("prev")) == 0) {
168         index = rs->list_index - 1;
169         if (index < 0) {
170             DisplayError(_("Can't back up any further"), 0);
171             return;
172         }
173         XawListHighlight(listwidg, index);
174     }
175     if (cmailMsgLoaded) {
176         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
177     } else {
178         LoadGame(glc->fp, index + 1, glc->filename, True);
179     }
180 }
181
182 static GameListClosure *glc = NULL;
183
184 void
185 GameListPopUp(fp, filename)
186      FILE *fp;
187      char *filename;
188 {
189   GtkTreeIter iter;
190   int  i=0,nstrings;
191   ListGame *lg;
192   
193   /* first clear everything, do we need this? */
194   gtk_list_store_clear(LIST_GameList);
195
196   /* fill list with information */
197   lg = (ListGame *) gameList.head;
198   nstrings = ((ListGame *) gameList.tailPred)->number;
199   while (nstrings--) 
200     {
201       gtk_list_store_append (LIST_GameList, &iter);
202       gtk_list_store_set (LIST_GameList, &iter,
203                           0, StrSave(filename),
204                           1, GameListLine(lg->number, &lg->gameInfo),
205                           2, fp,
206                           -1);
207       lg = (ListGame *) lg->node.succ;
208     }
209
210
211   /* show widget */
212   gtk_widget_show (GUI_GameList);
213
214 //    XtPopup(glc->shell, XtGrabNone);
215 //    glc->up = True;
216 //    j = 0;
217 //    XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
218 //    XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
219 //              args, j);
220
221   return;
222 }
223
224 void
225 GameListDestroy()
226 {
227   GameListPopDown();
228
229   gtk_list_store_clear(LIST_GameList);
230   return;
231 }
232
233 void
234 ShowGameListProc(w, event, prms, nprms)
235      Widget w;
236      XEvent *event;
237      String *prms;
238      Cardinal *nprms;
239 {
240     Arg args[16];
241     int j;
242
243     if (glc == NULL) {
244         DisplayError(_("There is no game list"), 0);
245         return;
246     }
247     if (glc->up) {
248         GameListPopDown();
249         return;
250     }
251     XtPopup(glc->shell, XtGrabNone);
252     glc->up = True;
253     j = 0;
254     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
255     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
256                 args, j);
257 }
258
259 void
260 LoadSelectedProc(w, event, prms, nprms)
261      Widget w;
262      XEvent *event;
263      String *prms;
264      Cardinal *nprms;
265 {
266     Widget listwidg;
267     XawListReturnStruct *rs;
268     int index;
269
270     if (glc == NULL) return;
271     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
272     rs = XawListShowCurrent(listwidg);
273     index = rs->list_index;
274     if (index < 0) return;
275     if (cmailMsgLoaded) {
276         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
277     } else {
278         LoadGame(glc->fp, index + 1, glc->filename, True);
279     }
280 }
281
282 void
283 GameListPopDown()
284 {
285   /* hides the history window */
286
287   gtk_widget_hide (GUI_GameList);
288   return;
289 }
290
291 void
292 GameListHighlight(index)
293      int index;
294 {
295     Widget listwidg;
296     if (glc == NULL || !glc->up) return;
297     listwidg = XtNameToWidget(glc->shell, "*form.viewport.list");
298     XawListHighlight(listwidg, index - 1);
299 }
300
301 Boolean
302 GameListIsUp()
303 {
304   /* return status of history window */
305   
306   return gtk_widget_get_visible (GUI_GameList);
307 }