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