added book.c to Makefile.am
[xboard.git] / xedittags.c
1 /*
2  * xedittags.c -- Tags edit window, part of X front end for XBoard
3  * $Id$
4  *
5  * Copyright 1995 Free Software Foundation, Inc.
6  *
7  * The following terms apply to the enhanced version of XBoard distributed
8  * by the Free Software Foundation:
9  * ------------------------------------------------------------------------
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  * ------------------------------------------------------------------------
24  *
25  * See the file ChangeLog for a revision history.
26  */
27
28 #include "config.h"
29
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <sys/types.h>
34
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 # include <string.h>
38 #else /* not STDC_HEADERS */
39 extern char *getenv();
40 # if HAVE_STRING_H
41 #  include <string.h>
42 # else /* not HAVE_STRING_H */
43 #  include <strings.h>
44 # endif /* not HAVE_STRING_H */
45 #endif /* not STDC_HEADERS */
46
47 #if HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50
51 #include <X11/Intrinsic.h>
52 #include <X11/StringDefs.h>
53 #include <X11/Shell.h>
54 #include <X11/cursorfont.h>
55 #if USE_XAW3D
56 #include <X11/Xaw3d/Dialog.h>
57 #include <X11/Xaw3d/Form.h>
58 #include <X11/Xaw3d/List.h>
59 #include <X11/Xaw3d/Label.h>
60 #include <X11/Xaw3d/SimpleMenu.h>
61 #include <X11/Xaw3d/SmeBSB.h>
62 #include <X11/Xaw3d/SmeLine.h>
63 #include <X11/Xaw3d/Box.h>
64 #include <X11/Xaw3d/MenuButton.h>
65 #include <X11/Xaw3d/Text.h>
66 #include <X11/Xaw3d/AsciiText.h>
67 #include <X11/Xaw3d/Viewport.h>
68 #else
69 #include <X11/Xaw/Dialog.h>
70 #include <X11/Xaw/Form.h>
71 #include <X11/Xaw/List.h>
72 #include <X11/Xaw/Label.h>
73 #include <X11/Xaw/SimpleMenu.h>
74 #include <X11/Xaw/SmeBSB.h>
75 #include <X11/Xaw/SmeLine.h>
76 #include <X11/Xaw/Box.h>
77 #include <X11/Xaw/MenuButton.h>
78 #include <X11/Xaw/Text.h>
79 #include <X11/Xaw/AsciiText.h>
80 #include <X11/Xaw/Viewport.h>
81 #endif
82
83 #include "common.h"
84 #include "frontend.h"
85 #include "backend.h"
86 #include "xboard.h"
87 #include "xedittags.h"
88 #include "gettext.h"
89
90 #ifdef ENABLE_NLS
91 # define  _(s) gettext (s)
92 # define N_(s) gettext_noop (s)
93 #else
94 # define  _(s) (s)
95 # define N_(s)  s
96 #endif
97
98 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
99 extern Display *xDisplay;
100 extern int squareSize;
101 extern Pixmap xMarkPixmap;
102 extern char *layoutName;
103
104 Position tagsX = -1, tagsY = -1;
105 int tagsUp = False, editTagsUp = False;
106 Widget tagsShell, editTagsShell;
107
108 static Arg layoutArgs[] = {
109     { XtNborderWidth, 0 },
110     { XtNdefaultDistance, 0 }
111 };
112
113 void TagsCallback(w, client_data, call_data)
114      Widget w;
115      XtPointer client_data, call_data;
116 {
117     String name;
118     Arg args[16];
119     int j;
120
121     j = 0;
122     XtSetArg(args[j], XtNlabel, &name);  j++;
123     XtGetValues(w, args, j);
124
125     if (strcmp(name, _("close")) == 0) {
126         TagsPopDown();
127     } else if (strcmp(name, _("edit")) == 0) {
128         TagsPopDown();
129         EditTagsEvent();
130     }
131 }
132
133
134 void EditTagsCallback(w, client_data, call_data)
135      Widget w;
136      XtPointer client_data, call_data;
137 {
138     String name, val;
139     Arg args[16];
140     int j;
141     Widget textw;
142
143     j = 0;
144     XtSetArg(args[j], XtNlabel, &name);  j++;
145     XtGetValues(w, args, j);
146
147     if (strcmp(name, _("ok")) == 0) {
148         textw = XtNameToWidget(editTagsShell, "*form.text");
149         j = 0;
150         XtSetArg(args[j], XtNstring, &val); j++;
151         XtGetValues(textw, args, j);
152         ReplaceTags(val, &gameInfo);
153         TagsPopDown();
154     } else if (strcmp(name, _("cancel")) == 0) {
155         TagsPopDown();
156     } else if (strcmp(name, _("clear")) == 0) {
157         textw = XtNameToWidget(editTagsShell, "*form.text");
158         XtCallActionProc(textw, "select-all", NULL, NULL, 0);
159         XtCallActionProc(textw, "kill-selection", NULL, NULL, 0);
160     }
161 }
162
163 Widget TagsCreate(name, text, msg, mutable, callback)
164      char *name, *text, *msg;
165      int /*Boolean*/ mutable;
166      XtCallbackProc callback;
167 {
168     Arg args[16];
169     Widget shell, form, textw, msgw, layout;
170     Widget b_ok, b_cancel, b_close, b_edit, b;
171     Dimension bw_width, pw_width;
172     Dimension pw_height;
173     int j, xx, yy;
174     Window junk;
175
176     j = 0;
177     XtSetArg(args[j], XtNwidth, &bw_width);  j++;
178     XtGetValues(boardWidget, args, j);
179
180     j = 0;
181     XtSetArg(args[j], XtNresizable, True);  j++;
182 #if TOPLEVEL
183     shell =
184       XtCreatePopupShell(name, topLevelShellWidgetClass,
185                          shellWidget, args, j);
186 #else
187     shell =
188       XtCreatePopupShell(name, transientShellWidgetClass,
189                          shellWidget, args, j);
190 #endif
191     layout =
192       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
193                             layoutArgs, XtNumber(layoutArgs));
194     j = 0;
195     XtSetArg(args[j], XtNborderWidth, 0); j++;
196     form =
197       XtCreateManagedWidget("form", formWidgetClass, layout, args, j);
198
199     j = 0;
200     if (mutable) {
201         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
202         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
203     }
204     XtSetArg(args[j], XtNstring, text);  j++;
205     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
206     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
207     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
208     XtSetArg(args[j], XtNright, XtRubber);  j++;
209     XtSetArg(args[j], XtNresizable, True);  j++;
210     XtSetArg(args[j], XtNwidth, bw_width/2);  j++;
211     XtSetArg(args[j], XtNheight, bw_width/3);  j++;
212 #if 0
213     XtSetArg(args[j], XtNscrollVertical, XawtextScrollWhenNeeded);  j++;
214 #else
215     /* !!Work around an apparent bug in XFree86 4.0.1 (X11R6.4.3) */
216     XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++;
217 #endif
218     XtSetArg(args[j], XtNautoFill, False);  j++;
219     textw =
220       XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
221
222     if (cmailMsgLoaded && !mutable) {
223         j = 0;
224         XtSetArg(args[j], XtNfromVert, textw);  j++;
225         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
226         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
227         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
228         XtSetArg(args[j], XtNright, XtChainRight); j++;
229         XtSetArg(args[j], XtNborderWidth, 0); j++;
230         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
231         XtSetArg(args[j], XtNlabel, msg); j++;
232         msgw =
233           XtCreateManagedWidget("msg", labelWidgetClass, form, args, j);
234     } else {
235         msgw = textw;
236     }
237     if (mutable) {
238         j = 0;
239         XtSetArg(args[j], XtNfromVert, msgw);  j++;
240         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
241         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
242         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
243         XtSetArg(args[j], XtNright, XtChainLeft); j++;
244         b_ok = b =
245           XtCreateManagedWidget(_("ok"), commandWidgetClass, form, args, j);
246         XtAddCallback(b_ok, XtNcallback, callback, (XtPointer) 0);
247
248         j = 0;
249         XtSetArg(args[j], XtNfromVert, msgw);  j++;
250         XtSetArg(args[j], XtNfromHoriz, b);  j++;
251         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
252         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
253         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
254         XtSetArg(args[j], XtNright, XtChainLeft); j++;
255         b_cancel = b =
256           XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
257         XtAddCallback(b_cancel, XtNcallback, callback, (XtPointer) 0);
258
259 #if 0
260         j = 0;
261         XtSetArg(args[j], XtNfromVert, msgw);  j++;
262         XtSetArg(args[j], XtNfromHoriz, b);  j++;
263         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
264         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
265         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
266         XtSetArg(args[j], XtNright, XtChainLeft); j++;
267         b_clear = b =
268           XtCreateManagedWidget("clear", commandWidgetClass, form, args, j);
269         XtAddCallback(b_clear, XtNcallback, callback, (XtPointer) 0);
270 #endif
271     } else {
272         j = 0;
273         XtSetArg(args[j], XtNfromVert, msgw);  j++;
274         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
275         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
276         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
277         XtSetArg(args[j], XtNright, XtChainLeft); j++;
278         b_close = b =
279           XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
280         XtAddCallback(b_close, XtNcallback, callback, (XtPointer) 0);
281
282         j = 0;
283         XtSetArg(args[j], XtNfromVert, msgw);  j++;
284         XtSetArg(args[j], XtNfromHoriz, b);  j++;
285         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
286         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
287         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
288         XtSetArg(args[j], XtNright, XtChainLeft); j++;
289         b_edit = b =
290           XtCreateManagedWidget(_("edit"), commandWidgetClass, form, args, j);
291         XtAddCallback(b_edit, XtNcallback, callback, (XtPointer) 0);
292     }
293
294     XtRealizeWidget(shell);
295     CatchDeleteWindow(shell, "TagsPopDown");
296
297     if (tagsX == -1) {
298         j = 0;
299         XtSetArg(args[j], XtNwidth, &bw_width);  j++;
300         XtGetValues(boardWidget, args, j);
301         j = 0;
302         XtSetArg(args[j], XtNwidth, &pw_width);  j++;
303         XtSetArg(args[j], XtNheight, &pw_height);  j++;
304         XtGetValues(shell, args, j);
305
306 #ifdef NOTDEF
307         /* This code seems to tickle an X bug if it is executed too soon
308            after xboard starts up.  The coordinates get transformed as if
309            the main window was positioned at (0, 0).
310            */
311         XtTranslateCoords(boardWidget, (bw_width - pw_width) / 2,
312                           0 - pw_height + squareSize / 3, &x, &y);
313 #else
314         XTranslateCoordinates(xDisplay, XtWindow(boardWidget),
315                               RootWindowOfScreen(XtScreen(boardWidget)),
316                               (bw_width - pw_width) / 2,
317                               0 - pw_height + squareSize / 3, &xx, &yy, &junk);
318         tagsX = xx;
319         tagsY = yy;
320 #endif
321         if (tagsY < 0) tagsY = 0; /*avoid positioning top offscreen*/
322     }
323     j = 0;
324     XtSetArg(args[j], XtNx, tagsX - appData.borderXoffset);  j++;
325     XtSetArg(args[j], XtNy, tagsY - appData.borderYoffset);  j++;
326     XtSetValues(shell, args, j);
327     XtSetKeyboardFocus(shell, textw);
328
329     return shell;
330 }
331
332
333 void TagsPopUp(tags, msg)
334      char *tags, *msg;
335 {
336     Arg args[16];
337     int j;
338     Widget textw, msgw;
339
340     if (editTagsUp) TagsPopDown();
341     if (tagsShell == NULL) {
342         tagsShell =
343           TagsCreate(_("Tags"), tags, msg, False, TagsCallback);
344     } else {
345         textw = XtNameToWidget(tagsShell, "*form.text");
346         j = 0;
347         XtSetArg(args[j], XtNstring, tags); j++;
348         XtSetValues(textw, args, j);
349         j = 0;
350         XtSetArg(args[j], XtNiconName, (XtArgVal) "Tags");  j++;
351         XtSetArg(args[j], XtNtitle, (XtArgVal) _("Tags"));  j++;
352         XtSetValues(tagsShell, args, j);
353         msgw = XtNameToWidget(tagsShell, "*form.msg");
354         if (msgw) {
355             j = 0;
356             XtSetArg(args[j], XtNlabel, msg); j++;
357             XtSetValues(msgw, args, j);
358         }
359     }
360
361     XtPopup(tagsShell, XtGrabNone);
362     XSync(xDisplay, False);
363
364     tagsUp = True;
365     j = 0;
366     XtSetArg(args[j], XtNleftBitmap, None); j++;
367     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
368                 args, j);
369 }
370
371
372 void EditTagsPopUp(tags)
373      char *tags;
374 {
375     Widget textw;
376     Arg args[16];
377     int j;
378
379     if (tagsUp) TagsPopDown();
380     if (editTagsShell == NULL) {
381         editTagsShell =
382           TagsCreate(_("Edit tags"), tags, NULL, True, EditTagsCallback); 
383     } else {
384         textw = XtNameToWidget(editTagsShell, "*form.text");
385         j = 0;
386         XtSetArg(args[j], XtNstring, tags); j++;
387         XtSetValues(textw, args, j);
388         j = 0;
389         XtSetArg(args[j], XtNiconName, (XtArgVal) "Edit Tags");  j++;
390         XtSetArg(args[j], XtNtitle, (XtArgVal) _("Edit Tags"));  j++;
391         XtSetValues(editTagsShell, args, j);
392     }
393
394     XtPopup(editTagsShell, XtGrabNone);
395
396     editTagsUp = True;
397     j = 0;
398     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
399     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
400                 args, j);
401 }
402
403 void TagsPopDown()
404 {
405     Arg args[16];
406     int j;
407     Widget w;
408
409     if (tagsUp) {
410         w = tagsShell;
411     } else if (editTagsUp) {
412         w = editTagsShell;
413     } else {
414         return;
415     }
416     j = 0;
417     XtSetArg(args[j], XtNx, &tagsX); j++;
418     XtSetArg(args[j], XtNy, &tagsY); j++;
419     XtGetValues(w, args, j);
420     XtPopdown(w);
421     XSync(xDisplay, False);
422     tagsUp = editTagsUp = False;
423     j = 0;
424     XtSetArg(args[j], XtNleftBitmap, None); j++;
425     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
426                 args, j);
427 }
428
429 void
430 EditTagsProc(w, event, prms, nprms)
431      Widget w;
432      XEvent *event;
433      String *prms;
434      Cardinal *nprms;
435 {
436     if (tagsUp) TagsPopDown();
437     if (editTagsUp) {
438         TagsPopDown();
439     } else {
440         EditTagsEvent();
441     }
442 }