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