Adjusted alternative joining method to obey keepLineBreaksICS
[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     /* !!Work around an apparent bug in XFree86 4.0.1 (X11R6.4.3) */
209     XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++;
210     XtSetArg(args[j], XtNautoFill, False);  j++;
211     textw =
212       XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
213
214     if (cmailMsgLoaded && !mutable) {
215         j = 0;
216         XtSetArg(args[j], XtNfromVert, textw);  j++;
217         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
218         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
219         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
220         XtSetArg(args[j], XtNright, XtChainRight); j++;
221         XtSetArg(args[j], XtNborderWidth, 0); j++;
222         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
223         XtSetArg(args[j], XtNlabel, msg); j++;
224         msgw =
225           XtCreateManagedWidget("msg", labelWidgetClass, form, args, j);
226     } else {
227         msgw = textw;
228     }
229     if (mutable) {
230         j = 0;
231         XtSetArg(args[j], XtNfromVert, msgw);  j++;
232         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
233         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
234         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
235         XtSetArg(args[j], XtNright, XtChainLeft); j++;
236         b_ok = b =
237           XtCreateManagedWidget(_("ok"), commandWidgetClass, form, args, j);
238         XtAddCallback(b_ok, XtNcallback, callback, (XtPointer) 0);
239
240         j = 0;
241         XtSetArg(args[j], XtNfromVert, msgw);  j++;
242         XtSetArg(args[j], XtNfromHoriz, b);  j++;
243         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
244         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
245         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
246         XtSetArg(args[j], XtNright, XtChainLeft); j++;
247         b_cancel = b =
248           XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
249         XtAddCallback(b_cancel, XtNcallback, callback, (XtPointer) 0);
250
251     } else {
252         j = 0;
253         XtSetArg(args[j], XtNfromVert, msgw);  j++;
254         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
255         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
256         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
257         XtSetArg(args[j], XtNright, XtChainLeft); j++;
258         b_close = b =
259           XtCreateManagedWidget(_("close"), commandWidgetClass, form, args, j);
260         XtAddCallback(b_close, XtNcallback, callback, (XtPointer) 0);
261
262         j = 0;
263         XtSetArg(args[j], XtNfromVert, msgw);  j++;
264         XtSetArg(args[j], XtNfromHoriz, b);  j++;
265         XtSetArg(args[j], XtNtop, XtChainBottom); j++;
266         XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
267         XtSetArg(args[j], XtNleft, XtChainLeft); j++;
268         XtSetArg(args[j], XtNright, XtChainLeft); j++;
269         b_edit = b =
270           XtCreateManagedWidget(_("edit"), commandWidgetClass, form, args, j);
271         XtAddCallback(b_edit, XtNcallback, callback, (XtPointer) 0);
272     }
273
274     XtRealizeWidget(shell);
275     CatchDeleteWindow(shell, "TagsPopDown");
276
277     if (tagsX == -1) {
278         j = 0;
279         XtSetArg(args[j], XtNwidth, &bw_width);  j++;
280         XtGetValues(boardWidget, args, j);
281         j = 0;
282         XtSetArg(args[j], XtNwidth, &pw_width);  j++;
283         XtSetArg(args[j], XtNheight, &pw_height);  j++;
284         XtGetValues(shell, args, j);
285
286 #ifdef NOTDEF
287         /* This code seems to tickle an X bug if it is executed too soon
288            after xboard starts up.  The coordinates get transformed as if
289            the main window was positioned at (0, 0).
290            */
291         XtTranslateCoords(boardWidget, (bw_width - pw_width) / 2,
292                           0 - pw_height + squareSize / 3, &x, &y);
293 #else
294         XTranslateCoordinates(xDisplay, XtWindow(boardWidget),
295                               RootWindowOfScreen(XtScreen(boardWidget)),
296                               (bw_width - pw_width) / 2,
297                               0 - pw_height + squareSize / 3, &xx, &yy, &junk);
298         tagsX = xx;
299         tagsY = yy;
300 #endif
301         if (tagsY < 0) tagsY = 0; /*avoid positioning top offscreen*/
302     }
303     j = 0;
304     XtSetArg(args[j], XtNx, tagsX - appData.borderXoffset);  j++;
305     XtSetArg(args[j], XtNy, tagsY - appData.borderYoffset);  j++;
306     XtSetValues(shell, args, j);
307     XtSetKeyboardFocus(shell, textw);
308
309     return shell;
310 }
311
312
313 void TagsPopUp(tags, msg)
314      char *tags, *msg;
315 {
316     Arg args[16];
317     int j;
318     Widget textw, msgw;
319
320     if (editTagsUp) TagsPopDown();
321     if (tagsShell == NULL) {
322         tagsShell =
323           TagsCreate(_("Tags"), tags, msg, False, TagsCallback);
324     } else {
325         textw = XtNameToWidget(tagsShell, "*form.text");
326         j = 0;
327         XtSetArg(args[j], XtNstring, tags); j++;
328         XtSetValues(textw, args, j);
329         j = 0;
330         XtSetArg(args[j], XtNiconName, (XtArgVal) "Tags");  j++;
331         XtSetArg(args[j], XtNtitle, (XtArgVal) _("Tags"));  j++;
332         XtSetValues(tagsShell, args, j);
333         msgw = XtNameToWidget(tagsShell, "*form.msg");
334         if (msgw) {
335             j = 0;
336             XtSetArg(args[j], XtNlabel, msg); j++;
337             XtSetValues(msgw, args, j);
338         }
339     }
340
341     XtPopup(tagsShell, XtGrabNone);
342     XSync(xDisplay, False);
343
344     tagsUp = True;
345     j = 0;
346     XtSetArg(args[j], XtNleftBitmap, None); j++;
347     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
348                 args, j);
349 }
350
351
352 void EditTagsPopUp(tags)
353      char *tags;
354 {
355     Widget textw;
356     Arg args[16];
357     int j;
358
359     if (tagsUp) TagsPopDown();
360     if (editTagsShell == NULL) {
361         editTagsShell =
362           TagsCreate(_("Edit tags"), tags, NULL, True, EditTagsCallback); 
363     } else {
364         textw = XtNameToWidget(editTagsShell, "*form.text");
365         j = 0;
366         XtSetArg(args[j], XtNstring, tags); j++;
367         XtSetValues(textw, args, j);
368         j = 0;
369         XtSetArg(args[j], XtNiconName, (XtArgVal) "Edit Tags");  j++;
370         XtSetArg(args[j], XtNtitle, (XtArgVal) _("Edit Tags"));  j++;
371         XtSetValues(editTagsShell, args, j);
372     }
373
374     XtPopup(editTagsShell, XtGrabNone);
375
376     editTagsUp = True;
377     j = 0;
378     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
379     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
380                 args, j);
381 }
382
383 void TagsPopDown()
384 {
385     Arg args[16];
386     int j;
387     Widget w;
388
389     if (tagsUp) {
390         w = tagsShell;
391     } else if (editTagsUp) {
392         w = editTagsShell;
393     } else {
394         return;
395     }
396     j = 0;
397     XtSetArg(args[j], XtNx, &tagsX); j++;
398     XtSetArg(args[j], XtNy, &tagsY); j++;
399     XtGetValues(w, args, j);
400     XtPopdown(w);
401     XSync(xDisplay, False);
402     tagsUp = editTagsUp = False;
403     j = 0;
404     XtSetArg(args[j], XtNleftBitmap, None); j++;
405     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Edit Tags"),
406                 args, j);
407 }
408
409 void
410 EditTagsProc(w, event, prms, nprms)
411      Widget w;
412      XEvent *event;
413      String *prms;
414      Cardinal *nprms;
415 {
416     if (tagsUp) TagsPopDown();
417     if (editTagsUp) {
418         TagsPopDown();
419     } else {
420         EditTagsEvent();
421     }
422 }