Redo Tags dialog
[xboard.git] / xoptions.c
1 /*
2  * xoptions.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000, 2009, 2010, 2011 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 // [HGM] this file is the counterpart of woptions.c, containing xboard popup menus
24 // similar to those of WinBoard, to set the most common options interactively.
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if STDC_HEADERS
34 # include <stdlib.h>
35 # include <string.h>
36 #else /* not STDC_HEADERS */
37 extern char *getenv();
38 # if HAVE_STRING_H
39 #  include <string.h>
40 # else /* not HAVE_STRING_H */
41 #  include <strings.h>
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
44
45 #if HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #include <stdint.h>
49
50 #include <X11/Intrinsic.h>
51 #include <X11/StringDefs.h>
52 #include <X11/Shell.h>
53 #include <X11/Xatom.h>
54 #include <X11/Xaw/Dialog.h>
55 #include <X11/Xaw/Form.h>
56 #include <X11/Xaw/List.h>
57 #include <X11/Xaw/Label.h>
58 #include <X11/Xaw/SimpleMenu.h>
59 #include <X11/Xaw/SmeBSB.h>
60 #include <X11/Xaw/SmeLine.h>
61 #include <X11/Xaw/Box.h>
62 #include <X11/Xaw/Paned.h>
63 #include <X11/Xaw/MenuButton.h>
64 #include <X11/cursorfont.h>
65 #include <X11/Xaw/Text.h>
66 #include <X11/Xaw/AsciiText.h>
67 #include <X11/Xaw/Viewport.h>
68 #include <X11/Xaw/Toggle.h>
69
70 #include "common.h"
71 #include "backend.h"
72 #include "xboard.h"
73 #include "gettext.h"
74
75 #ifdef ENABLE_NLS
76 # define  _(s) gettext (s)
77 # define N_(s) gettext_noop (s)
78 #else
79 # define  _(s) (s)
80 # define N_(s)  s
81 #endif
82
83 extern void SendToProgram P((char *message, ChessProgramState *cps));
84 FILE * XsraSelFile P((Widget w, char *prompt, char *ok, char *cancel, char *failed,
85                 char *init_path, char *filter, char *mode, int (*show_entry)(), char **name_return));
86
87 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
88 extern Display *xDisplay;
89 extern int squareSize;
90 extern Pixmap xMarkPixmap;
91 extern char *layoutName;
92 extern Window xBoardWindow;
93 extern Arg layoutArgs[2], formArgs[2];
94 Pixel timerForegroundPixel, timerBackgroundPixel;
95 extern int searchTime;
96 extern Atom wm_delete_window;
97 extern int lineGap;
98
99 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
100
101 static Widget previous = NULL;
102
103 void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
104 {
105     Arg args[2];
106     char *s;
107
108     if(previous) {
109         XtSetArg(args[0], XtNdisplayCaret, False);
110         XtSetValues(previous, args, 1);
111     }
112     XtSetArg(args[0], XtNstring, &s);
113     XtGetValues(w, args, 1);
114     XtSetArg(args[0], XtNdisplayCaret, True);
115     XtSetArg(args[1], XtNinsertPosition, strlen(s));
116     XtSetValues(w, args, 2);
117     XtSetKeyboardFocus((Widget) data, w);
118     previous = w;
119 }
120
121 //--------------------------- New Shuffle Game --------------------------------------------
122 extern int shuffleOpenings;
123 extern int startedFromPositionFile;
124 int shuffleUp;
125 Widget shuffleShell;
126
127 void ShufflePopDown()
128 {
129     if (!shuffleUp) return;
130     XtPopdown(shuffleShell);
131     XtDestroyWidget(shuffleShell);
132     shuffleUp = False;
133     ModeHighlight();
134 }
135
136 void ShuffleCallback(w, client_data, call_data)
137      Widget w;
138      XtPointer client_data, call_data;
139 {
140     String name;
141     Widget w2;
142     Arg args[16];
143     char buf[MSG_SIZ];
144
145     XtSetArg(args[0], XtNlabel, &name);
146     XtGetValues(w, args, 1);
147
148     if (strcmp(name, _("cancel")) == 0) {
149         ShufflePopDown();
150         return;
151     }
152     if (strcmp(name, _("off")) == 0) {
153         ShufflePopDown();
154         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
155         ResetGameEvent();
156         return;
157     }
158     if (strcmp(name, _("random")) == 0) {
159       snprintf(buf, MSG_SIZ,  "%d", rand());
160         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
161         XtSetValues(XtParent(w), args, 1);
162         return;
163     }
164     if (strcmp(name, _("ok")) == 0) {
165         int nr; String name;
166         name = XawDialogGetValueString(w2 = XtParent(w));
167         if(sscanf(name ,"%d",&nr) != 1) {
168           snprintf(buf, MSG_SIZ,  "%d", appData.defaultFrcPosition);
169             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
170             XtSetValues(w2, args, 1);
171             return;
172         }
173         appData.defaultFrcPosition = nr;
174         shuffleOpenings = True;
175         ShufflePopDown();
176         ResetGameEvent();
177         return;
178     }
179 }
180
181 void ShufflePopUp()
182 {
183     Arg args[16];
184     Widget popup, layout, dialog, edit;
185     Window root, child;
186     int x, y, i;
187     int win_x, win_y;
188     unsigned int mask;
189     char def[MSG_SIZ];
190
191     i = 0;
192     XtSetArg(args[i], XtNresizable, True); i++;
193     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
194     shuffleShell = popup =
195       XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
196                          shellWidget, args, i);
197
198     layout =
199       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
200                             layoutArgs, XtNumber(layoutArgs));
201
202     snprintf(def, MSG_SIZ,  "%d\n", appData.defaultFrcPosition);
203     i = 0;
204     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
205     XtSetArg(args[i], XtNvalue, def); i++;
206     XtSetArg(args[i], XtNborderWidth, 0); i++;
207     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
208                                    layout, args, i);
209
210 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
211 //    XtSetArg(args[1], XtNuseStringInPlace, False);
212 //    XtSetValues(dialog, args, 2);
213
214     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
215     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
216     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
217     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
218
219     XtRealizeWidget(popup);
220     CatchDeleteWindow(popup, "ShufflePopDown");
221
222     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
223                   &x, &y, &win_x, &win_y, &mask);
224
225     XtSetArg(args[0], XtNx, x - 10);
226     XtSetArg(args[1], XtNy, y - 30);
227     XtSetValues(popup, args, 2);
228
229     XtPopup(popup, XtGrabExclusive);
230     shuffleUp = True;
231
232     edit = XtNameToWidget(dialog, "*value");
233
234     XtSetKeyboardFocus(popup, edit);
235 }
236
237 void ShuffleMenuProc(w, event, prms, nprms)
238      Widget w;
239      XEvent *event;
240      String *prms;
241      Cardinal *nprms;
242 {
243 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
244 //      Reset(FALSE, TRUE);
245 //    }
246     ShufflePopUp();
247 }
248
249 //--------------------------- Time-Control Menu Popup ----------------------------------
250 int TimeControlUp;
251 Widget TimeControlShell;
252 int tcInc;
253 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
254 int tcIncrement, tcMoves;
255
256 void TimeControlPopDown()
257 {
258     if (!TimeControlUp) return;
259     previous = NULL;
260     XtPopdown(TimeControlShell);
261     XtDestroyWidget(TimeControlShell);
262     TimeControlUp = False;
263     ModeHighlight();
264 }
265
266 void TimeControlCallback(w, client_data, call_data)
267      Widget w;
268      XtPointer client_data, call_data;
269 {
270     String name, txt;
271     Arg args[16];
272     char buf[MSG_SIZ];
273     int j;
274
275     XtSetArg(args[0], XtNlabel, &name);
276     XtGetValues(w, args, 1);
277
278     if (strcmp(name, _("classical")) == 0) {
279         if(tcInc == 0) return;
280         j=0;
281         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
282         XtSetValues(tcMess1, args, j);
283         j=0;
284         XtSetArg(args[j], XtNlabel, _("moves")); j++;
285         XtSetValues(tcMess2, args, j);
286         if(tcInc == 1) {
287             j=0;
288             XtSetArg(args[j], XtNstring, &name); j++;
289             XtGetValues(tcData, args, j);
290             tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
291         }
292         snprintf(buf, MSG_SIZ,  "%d", tcMoves);
293         j=0;
294         XtSetArg(args[j], XtNstring, buf); j++;
295         XtSetValues(tcData, args, j);
296         tcInc = 0;
297         return;
298     }
299     if (strcmp(name, _("incremental")) == 0) {
300         if(tcInc == 1) return;
301         j=0;
302         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
303         XtSetValues(tcMess1, args, j);
304         j=0;
305         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
306         XtSetValues(tcMess2, args, j);
307         if(tcInc == 0) {
308             j=0;
309             XtSetArg(args[j], XtNstring, &name); j++;
310             XtGetValues(tcData, args, j);
311             tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
312         }
313         snprintf(buf, MSG_SIZ,  "%d", tcIncrement);
314         j=0;
315         XtSetArg(args[j], XtNstring, buf); j++;
316         XtSetValues(tcData, args, j);
317         tcInc = 1;
318         return;
319     }
320     if (strcmp(name, _("fixed time")) == 0) {
321         if(tcInc == 2) return;
322         j=0;
323         XtSetArg(args[j], XtNlabel, _("sec/move (max)")); j++;
324         XtSetValues(tcMess1, args, j);
325         j=0;
326         XtSetArg(args[j], XtNlabel, _("")); j++;
327         XtSetValues(tcMess2, args, j);
328         j=0;
329         XtSetArg(args[j], XtNstring, ""); j++;
330         XtSetValues(tcData, args, j);
331         tcInc = 2;
332         return;
333     }
334     if (strcmp(name, _(" OK ")) == 0) {
335         int inc, mps, ok;
336         XtSetArg(args[0], XtNstring, &txt);
337         XtGetValues(tcData, args, 1);
338         switch(tcInc) {
339           case 1:
340             ok = sscanf(txt, "%d", &inc); mps = 0;
341             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
342             ok &= (inc >= 0);
343             break;
344           case 0:
345             ok = sscanf(txt, "%d", &mps); inc = -1;
346             ok &= (mps > 0);
347             break;
348           case 2:
349             ok = 1; inc = -1; mps = 40;
350         }
351         if(ok != 1) {
352             XtSetArg(args[0], XtNstring, ""); // erase any offending input
353             XtSetValues(tcData, args, 1);
354             return;
355         }
356         XtSetArg(args[0], XtNstring, &txt);
357         XtGetValues(tcTime, args, 1);
358         if(tcInc == 2) {
359             if(sscanf(txt, "%d", &inc) != 1) {
360                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
361                 XtSetValues(tcTime, args, 1);
362                 DisplayError(_("Bad Time-Control String"), 0);
363                 return;
364             }
365             searchTime = inc;
366         } else {
367             if(!ParseTimeControl(txt, inc, mps)) {
368                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
369                 XtSetValues(tcTime, args, 1);
370                 DisplayError(_("Bad Time-Control String"), 0);
371                 return;
372             }
373             searchTime = 0;
374             appData.movesPerSession = mps;
375             appData.timeIncrement = inc;
376             appData.timeControl = strdup(txt);
377         }
378         XtSetArg(args[0], XtNstring, &txt);
379         XtGetValues(tcOdds1, args, 1);
380         appData.firstTimeOdds = first.timeOdds
381                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
382         XtGetValues(tcOdds2, args, 1);
383         appData.secondTimeOdds = second.timeOdds
384                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
385
386         Reset(True, True);
387         TimeControlPopDown();
388         return;
389     }
390 }
391
392 void TimeControlPopUp()
393 {
394     Arg args[16];
395     Widget popup, layout, form,  b_ok, b_cancel, b_clas, b_inc, mess;
396     Window root, child;
397     int x, y, i, j;
398     int win_x, win_y;
399     unsigned int mask;
400     char def[MSG_SIZ];
401
402     tcInc = searchTime > 0 ? 2 : (appData.timeIncrement >= 0);
403     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
404     if(!tcInc) tcIncrement = 0;
405     snprintf(def, MSG_SIZ,  "%d", tcInc ? tcIncrement : tcMoves);
406
407     i = 0;
408     XtSetArg(args[i], XtNresizable, True); i++;
409 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
410     TimeControlShell = popup =
411       XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
412                          shellWidget, args, i);
413
414     layout =
415       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
416                             layoutArgs, XtNumber(layoutArgs));
417
418     form =
419       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
420                             formArgs, XtNumber(formArgs));
421
422     j = 0;
423 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
424 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
425     XtSetValues(popup, args, j);
426
427     j= 0;
428     XtSetArg(args[j], XtNborderWidth, 1); j++;
429     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
430     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
431     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
432     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
433     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
434     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
435     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
436     XtSetArg(args[j], XtNright, XtChainRight);  j++;
437     XtSetArg(args[j], XtNresizable, True);  j++;
438     XtSetArg(args[j], XtNwidth,  85);  j++;
439     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
440     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
441     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
442
443     j= 0;
444     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("sec/move (max)   ") : _("   minutes, plus   ") : _("minutes for each")); j++;
445     XtSetArg(args[j], XtNborderWidth, 0); j++;
446     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
447     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
448     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
449     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
450     XtSetArg(args[j], XtNright, XtChainRight);  j++;
451   //  XtSetArg(args[j], XtNwidth,  100);  j++;
452   //  XtSetArg(args[j], XtNheight, 20);  j++;
453     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
454
455     j= 0;
456     XtSetArg(args[j], XtNborderWidth, 1); j++;
457     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
458     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
459     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
460     XtSetArg(args[j], XtNstring, def);  j++;
461     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
462     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
463     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
464     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
465     XtSetArg(args[j], XtNright, XtChainRight);  j++;
466     XtSetArg(args[j], XtNresizable, True);  j++;
467     XtSetArg(args[j], XtNwidth,  40);  j++;
468 //    XtSetArg(args[j], XtNheight, 20);  j++;
469     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
470     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
471
472     j= 0;
473     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("             ") : _("sec/move") : _("moves     ")); j++;
474     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
475     XtSetArg(args[j], XtNborderWidth, 0); j++;
476     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
477     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
478     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
479     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
480     XtSetArg(args[j], XtNright, XtChainRight);  j++;
481 //    XtSetArg(args[j], XtNwidth,  80);  j++;
482 //    XtSetArg(args[j], XtNheight, 20);  j++;
483     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
484                                    form, args, j);
485
486     j= 0;
487     XtSetArg(args[j], XtNborderWidth, 1); j++;
488     XtSetArg(args[j], XtNfromVert, tcTime); j++;
489     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
490     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
491     XtSetArg(args[j], XtNstring, "1");  j++;
492     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
493     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
494     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
495     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
496     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
497     XtSetArg(args[j], XtNresizable, True);  j++;
498     XtSetArg(args[j], XtNwidth,  40);  j++;
499 //    XtSetArg(args[j], XtNheight, 20);  j++;
500     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
501     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
502
503     j= 0;
504     XtSetArg(args[j], XtNborderWidth, 1); j++;
505     XtSetArg(args[j], XtNfromVert, tcTime); j++;
506     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
507     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
508     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
509     XtSetArg(args[j], XtNstring, "1");  j++;
510     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
511     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
512     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
513     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
514     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
515     XtSetArg(args[j], XtNresizable, True);  j++;
516     XtSetArg(args[j], XtNwidth,  40);  j++;
517 //    XtSetArg(args[j], XtNheight, 20);  j++;
518     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
519     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
520
521     j= 0;
522     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
523     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
524     XtSetArg(args[j], XtNborderWidth, 0); j++;
525     XtSetArg(args[j], XtNfromVert, tcTime); j++;
526     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
527     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
528     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
529     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
530     XtSetArg(args[j], XtNright, XtChainRight);  j++;
531 //    XtSetArg(args[j], XtNwidth,  200);  j++;
532 //    XtSetArg(args[j], XtNheight, 20);  j++;
533     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
534                                    form, args, j);
535     j=0;
536     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
537     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
538     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
539     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
540     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
541     XtSetArg(args[j], XtNstate, tcInc==0); j++;
542     b_clas= XtCreateManagedWidget(_("classical"), toggleWidgetClass,
543                                    form, args, j);
544     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
545
546     j=0;
547     XtSetArg(args[j], XtNradioGroup, b_clas); j++;
548     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
549     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
550     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
551     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
552     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
553     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
554     XtSetArg(args[j], XtNstate, tcInc==1); j++;
555     b_inc = XtCreateManagedWidget(_("incremental"), toggleWidgetClass,
556                                    form, args, j);
557     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
558
559     j=0;
560     XtSetArg(args[j], XtNradioGroup, b_inc); j++;
561     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
562     XtSetArg(args[j], XtNfromHoriz, b_inc);  j++;
563     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
564     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
565     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
566     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
567     XtSetArg(args[j], XtNstate, tcInc==2); j++;
568     b_inc = XtCreateManagedWidget(_("fixed time"), toggleWidgetClass,
569                                    form, args, j);
570     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
571
572     j=0;
573     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
574     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
575     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
576     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
577     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
578     XtSetArg(args[j], XtNright, XtChainRight);  j++;
579     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
580                                    form, args, j);
581     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
582
583     j=0;
584     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
585     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
586     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
587     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
588     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
589     XtSetArg(args[j], XtNright, XtChainRight);  j++;
590     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
591                                    form, args, j);
592     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
593
594     XtRealizeWidget(popup);
595     CatchDeleteWindow(popup, "TimeControlPopDown");
596
597     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
598                   &x, &y, &win_x, &win_y, &mask);
599
600     XtSetArg(args[0], XtNx, x - 10);
601     XtSetArg(args[1], XtNy, y - 30);
602     XtSetValues(popup, args, 2);
603
604     XtPopup(popup, XtGrabExclusive);
605     TimeControlUp = True;
606
607     previous = NULL;
608     SetFocus(tcTime, popup, (XEvent*) NULL, False);
609 //    XtSetKeyboardFocus(popup, tcTime);
610 }
611
612 void TimeControlProc(w, event, prms, nprms)
613      Widget w;
614      XEvent *event;
615      String *prms;
616      Cardinal *nprms;
617 {
618    TimeControlPopUp();
619 }
620
621 //--------------------------- Engine-specific options menu ----------------------------------
622
623 int values[MAX_OPTIONS];
624 ChessProgramState *currentCps;
625 static Option *currentOption;
626 extern Widget shells[];
627
628 void CheckCallback(Widget ww, XtPointer data, XEvent *event, Boolean *b)
629 {
630     Widget w = currentOption[(int)(intptr_t)data].handle;
631     Boolean s;
632     Arg args[16];
633
634     XtSetArg(args[0], XtNstate, &s);
635     XtGetValues(w, args, 1);
636     XtSetArg(args[0], XtNstate, !s);
637     XtSetValues(w, args, 1);
638 }
639
640 void SpinCallback(w, client_data, call_data)
641      Widget w;
642      XtPointer client_data, call_data;
643 {
644     String name, val;
645     Arg args[16];
646     char buf[MSG_SIZ], *p;
647     int j;
648     int data = (intptr_t) client_data;
649
650     XtSetArg(args[0], XtNlabel, &name);
651     XtGetValues(w, args, 1);
652
653     j = 0;
654     XtSetArg(args[0], XtNstring, &val);
655     XtGetValues(currentOption[data].handle, args, 1);
656     sscanf(val, "%d", &j);
657     if (strcmp(name, "browse") == 0) {
658         char *q, *r;
659         XtSetArg(args[0], XtNstring, &q);
660         XtGetValues(currentOption[data].handle, args, 1);
661         for(r = ""; *q; q++) if(*q == '.') r = q; else if(*q == '/') r = ""; // last dot after last slash
662         if(XsraSelFile(shells[0], currentOption[data].name, NULL, NULL, "", "", r,
663                                   currentOption[data].type == PathName ? "p" : "f", NULL, &p)) {
664                 int len = strlen(p);
665                 if(len && p[len-1] == '/') p[len-1] = NULLCHAR;
666                 XtSetArg(args[0], XtNstring, p);
667                 XtSetValues(currentOption[data].handle, args, 1);
668         }
669         SetFocus(currentOption[data].handle, shells[0], (XEvent*) NULL, False);
670         return;
671     } else
672     if (strcmp(name, "+") == 0) {
673         if(++j > currentOption[data].max) return;
674     } else
675     if (strcmp(name, "-") == 0) {
676         if(--j < currentOption[data].min) return;
677     } else return;
678     snprintf(buf, MSG_SIZ,  "%d", j);
679     XtSetArg(args[0], XtNstring, buf);
680     XtSetValues(currentOption[data].handle, args, 1);
681     SetFocus(currentOption[data].handle, shells[0], NULL, False);
682 }
683
684 void ComboSelect(w, addr, index) // callback for all combo items
685      Widget w;
686      caddr_t addr;
687      caddr_t index;
688 {
689     Arg args[16];
690     int i = ((intptr_t)addr)>>8;
691     int j = 255 & (intptr_t) addr;
692
693     values[i] = j; // store in temporary, for transfer at OK
694     XtSetArg(args[0], XtNlabel, ((char**)currentOption[i].textValue)[j]);
695     XtSetValues(currentOption[i].handle, args, 1);
696 }
697
698 void CreateComboPopup(parent, name, n, mb)
699      Widget parent;
700      String name;
701      int n;
702      char *mb[];
703 {
704     int i=0, j;
705     Widget menu, entry;
706     Arg args[16];
707
708     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
709                               parent, NULL, 0);
710     j = 0;
711     XtSetArg(args[j], XtNwidth, 100);  j++;
712 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
713     while (mb[i] != NULL) {
714             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
715                                           menu, args, j);
716             XtAddCallback(entry, XtNcallback,
717                           (XtCallbackProc) ComboSelect,
718                           (caddr_t)(intptr_t) (256*n+i));
719         i++;
720     }
721 }
722
723
724 //----------------------------Generic dialog --------------------------------------------
725
726 // cloned from Engine Settings dialog (and later merged with it)
727
728 typedef void ButtonCallback(int n);
729
730 char *trialSound;
731 static int oldCores, oldPonder;
732 int MakeColors P((void));
733 void CreateGCs P((int redo));
734 void CreateXPMBoard P((char *s, int kind));
735 void CreateXPMPieces P((void));
736 void GenericReadout();
737 Widget shells[10];
738 Widget marked[10];
739 Boolean shellUp[10];
740 WindowPlacement *wp[10];
741 Option *dialogOptions[10];
742
743 void MarkMenu(char *item, int dlgNr)
744 {
745     Arg args[2];
746     XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
747     XtSetValues(marked[dlgNr] = XtNameToWidget(menuBarWidget, item), args, 1);
748 }
749
750 int PopDown(int n)
751 {
752     int j;
753     Arg args[10];
754     Dimension windowH, windowW; Position windowX, windowY;
755     if (!shellUp[n]) return 0;
756     if(n && wp[n]) { // remember position
757         j = 0;
758         XtSetArg(args[j], XtNx, &windowX); j++;
759         XtSetArg(args[j], XtNy, &windowY); j++;
760         XtSetArg(args[j], XtNheight, &windowH); j++;
761         XtSetArg(args[j], XtNwidth, &windowW); j++;
762         XtGetValues(shells[n], args, j);
763         wp[n]->x = windowX;
764         wp[n]->x = windowY;
765         wp[n]->width  = windowW;
766         wp[n]->height = windowH;
767     }
768     previous = NULL;
769     XtPopdown(shells[n]);
770     if(n == 0) XtDestroyWidget(shells[n]);
771     shellUp[n] = False;
772     if(marked[n]) {
773         XtSetArg(args[0], XtNleftBitmap, None);
774         XtSetValues(marked[n], args, 1);
775     }
776     if(!n) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
777     return 1;
778 }
779
780 void GenericPopDown(w, event, prms, nprms)
781      Widget w;
782      XEvent *event;
783      String *prms;
784      Cardinal *nprms;
785 {
786     int n;
787     PopDown(prms[0][0] - '0');
788 }
789
790 Option matchOptions[] = {
791 { 0,  2, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, _("Default Number of Games in Match:") },
792 { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, _("Pause between Match Games (msec):") },
793 { 0,  0,          0, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, _("Game File with Opening Lines:") },
794 { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, _("Game Number (-1 or -2 = Auto-Increment):") },
795 { 0,  0,          0, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, _("File with Start Positions:") },
796 { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, _("Position Number (-1 or -2 = Auto-Increment):") },
797 { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, _("Rewind Index after this many Games (0 = never):") },
798 { 0, 0, 0, NULL, NULL, "", NULL, EndMark , "" }
799 };
800
801 void GeneralOptionsOK(int n)
802 {
803         int newPonder = appData.ponderNextMove;
804         appData.ponderNextMove = oldPonder;
805         PonderNextMoveEvent(newPonder);
806 }
807
808 Option generalOptions[] = {
809 { 0,  0, 0, NULL, (void*) &appData.alwaysPromoteToQueen, "", NULL, CheckBox, _("Always Queen") },
810 { 0,  0, 0, NULL, (void*) &appData.animateDragging, "", NULL, CheckBox, _("Animate Dragging") },
811 { 0,  0, 0, NULL, (void*) &appData.animate, "", NULL, CheckBox, _("Animate Moving") },
812 { 0,  0, 0, NULL, (void*) &appData.autoCallFlag, "", NULL, CheckBox, _("Auto Flag") },
813 { 0,  0, 0, NULL, (void*) &appData.autoFlipView, "", NULL, CheckBox, _("Auto Flip View") },
814 { 0,  0, 0, NULL, (void*) &appData.blindfold, "", NULL, CheckBox, _("Blindfold") },
815 { 0,  0, 0, NULL, (void*) &appData.dropMenu, "", NULL, CheckBox, _("Drop Menu") },
816 { 0,  0, 0, NULL, (void*) &appData.highlightDragging, "", NULL, CheckBox, _("Highlight Dragging (Show Move Targets)") },
817 { 0,  0, 0, NULL, (void*) &appData.highlightLastMove, "", NULL, CheckBox, _("Highlight Last Move") },
818 { 0,  0, 0, NULL, (void*) &appData.highlightMoveWithArrow, "", NULL, CheckBox, _("Highlight with Arrow") },
819 { 0,  0, 0, NULL, (void*) &appData.ringBellAfterMoves, "", NULL, CheckBox, _("Move Sound") },
820 { 0,  0, 0, NULL, (void*) &appData.oneClick, "", NULL, CheckBox, _("One-Click Moving") },
821 { 0,  0, 0, NULL, (void*) &appData.periodicUpdates, "", NULL, CheckBox, _("Periodic Updates (in Analysis Mode)") },
822 { 0,  0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
823 { 0,  0, 0, NULL, (void*) &appData.popupExitMessage, "", NULL, CheckBox, _("Popup Exit Messages") },
824 { 0,  0, 0, NULL, (void*) &appData.popupMoveErrors, "", NULL, CheckBox, _("Popup Move Errors") },
825 { 0,  0, 0, NULL, (void*) &appData.showCoords, "", NULL, CheckBox, _("Show Coordinates") },
826 { 0,  0, 0, NULL, (void*) &appData.markers, "", NULL, CheckBox, _("Show Target Squares") },
827 { 0,  0, 0, NULL, (void*) &appData.hideThinkingFromHuman, "", NULL, CheckBox, _("Hide Thinking from Human") },
828 { 0,  0, 0, NULL, (void*) &appData.testLegality, "", NULL, CheckBox, _("Test Legality") },
829 { 0, 0, 10, NULL, (void*) &appData.flashCount, "", NULL, Spin, _("Flash Moves (0 = no flashing):") },
830 { 0, 1, 10, NULL, (void*) &appData.flashRate, "", NULL, Spin, _("Flash Rate (high = fast):") },
831 { 0, 5, 100,NULL, (void*) &appData.animSpeed, "", NULL, Spin, _("Animation Speed (high = slow):") },
832 { 0,  0, 0, NULL, (void*) &GeneralOptionsOK, "", NULL, EndMark , "" }
833 };
834
835 void Pick(int n)
836 {
837         VariantClass v = currentOption[n].value;
838         if(!appData.noChessProgram) {
839             char *name = VariantName(v), buf[MSG_SIZ];
840             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
841                 /* [HGM] in protocol 2 we check if variant is suported by engine */
842               snprintf(buf, MSG_SIZ,  _("Variant %s not supported by %s"), name, first.tidy);
843                 DisplayError(buf, 0);
844                 return; /* ignore OK if first engine does not support it */
845             } else
846             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
847               snprintf(buf, MSG_SIZ,  _("Warning: second engine (%s) does not support this!"), second.tidy);
848                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
849             }
850         }
851
852         GenericReadout(); // make sure ranks and file settings are read
853
854         gameInfo.variant = v;
855         appData.variant = VariantName(v);
856
857         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
858         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
859         appData.pieceToCharTable = NULL;
860         appData.pieceNickNames = "";
861         appData.colorNickNames = "";
862         Reset(True, True);
863         PopDown(0);
864         return;
865 }
866
867 Option variantDescriptors[] = {
868 { VariantNormal, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("normal")},
869 { VariantFairy, 1, 135, NULL, (void*) &Pick, "#BFBFBF", NULL, Button, N_("fairy")},
870 { VariantFischeRandom, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("FRC")},
871 { VariantSChess, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Seirawan")},
872 { VariantWildCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("wild castle")},
873 { VariantSuper, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Superchess")},
874 { VariantNoCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("no castle")},
875 { VariantCrazyhouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("crazyhouse")},
876 { VariantKnightmate, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("knightmate")},
877 { VariantBughouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("bughouse")},
878 { VariantBerolina, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("berolina")},
879 { VariantShogi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("shogi (9x9)")},
880 { VariantCylinder, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("cylinder")},
881 { VariantXiangqi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("xiangqi (9x10)")},
882 { VariantShatranj, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("shatranj")},
883 { VariantCourier, 1, 135, NULL, (void*) &Pick, "#BFFFBF", NULL, Button, N_("courier (12x8)")},
884 { VariantMakruk, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("makruk")},
885 { VariantGreat, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Great Shatranj (10x8)")},
886 { VariantAtomic, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("atomic")},
887 { VariantCapablanca, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Capablanca (10x8)")},
888 { VariantTwoKings, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("two kings")},
889 { VariantGothic, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Gothic (10x8)")},
890 { Variant3Check, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("3-checks")},
891 { VariantJanus, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("janus (10x8)")},
892 { VariantSuicide, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("suicide")},
893 { VariantCapaRandom, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("CRC (10x8)")},
894 { VariantGiveaway, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("give-away")},
895 { VariantSpartan, 1, 135, NULL, (void*) &Pick, "#FF0000", NULL, Button, N_("Spartan")},
896 { VariantLosers, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("losers")},
897 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Board size ( -1 = default for selected variant):")},
898 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.NrRanks, "", NULL, Spin, N_("Number of Board Ranks:") },
899 { 0, -1, BOARD_FILES, NULL, (void*) &appData.NrFiles, "", NULL, Spin, N_("Number of Board Files:") },
900 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.holdingsSize, "", NULL, Spin, N_("Holdings Size:") },
901 { 0, 0, 0, NULL, NULL, NULL, NULL, Label,
902                                 _("WARNING: variants with un-orthodox\n"
903                                   "pieces only have built-in bitmaps\n"
904                                   "for -boardSize middling, bulky and\n"
905                                   "petite, and substitute king or amazon\n"
906                                   "for missing bitmaps. (See manual.)")},
907 { 0, 2, 0, NULL, NULL, "", NULL, EndMark , "" }
908 };
909
910 void CommonOptionsOK(int n)
911 {
912         int newPonder = appData.ponderNextMove;
913         // make sure changes are sent to first engine by re-initializing it
914         // if it was already started pre-emptively at end of previous game
915         if(gameMode == BeginningOfGame) Reset(True, True); else {
916             // Some changed setting need immediate sending always.
917             if(oldCores != appData.smpCores)
918                 NewSettingEvent(False, &(first.maxCores), "cores", appData.smpCores);
919             appData.ponderNextMove = oldPonder;
920             PonderNextMoveEvent(newPonder);
921         }
922 }
923
924 Option commonEngineOptions[] = {
925 { 0,     0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
926 { 0,  0, 1000, NULL, (void*) &appData.smpCores, "", NULL, Spin, _("Maximum Number of CPUs per Engine:") },
927 { 0,     0, 0, NULL, (void*) &appData.polyglotDir, "", NULL, PathName, _("Polygot Directory:") },
928 { 0, 0, 16000, NULL, (void*) &appData.defaultHashSize, "", NULL, Spin, _("Hash-Table Size (MB):") },
929 { 0,     0, 0, NULL, (void*) &appData.defaultPathEGTB, "", NULL, PathName, _("Nalimov EGTB Path:") },
930 { 0,  0, 1000, NULL, (void*) &appData.defaultCacheSizeEGTB, "", NULL, Spin, _("EGTB Cache Size (MB):") },
931 { 0,     0, 0, NULL, (void*) &appData.usePolyglotBook, "", NULL, CheckBox, _("Use GUI Book") },
932 { 0,     0, 0, NULL, (void*) &appData.polyglotBook, "", NULL, FileName, _("Opening-Book Filename:") },
933 { 0,   0, 100, NULL, (void*) &appData.bookDepth, "", NULL, Spin, _("Book Depth (moves):") },
934 { 0,   0, 100, NULL, (void*) &appData.bookStrength, "", NULL, Spin, _("Book Variety (0) vs. Strength (100):") },
935 { 0,     0, 0, NULL, (void*) &appData.firstHasOwnBookUCI, "", NULL, CheckBox, _("Engine #1 Has Own Book") },
936 { 0,     0, 0, NULL, (void*) &appData.secondHasOwnBookUCI, "", NULL, CheckBox, _("Engine #2 Has Own Book          ") },
937 { 0,     1, 0, NULL, (void*) &CommonOptionsOK, "", NULL, EndMark , "" }
938 };
939
940 Option adjudicationOptions[] = {
941 { 0, 0,    0, NULL, (void*) &appData.checkMates, "", NULL, CheckBox, _("Detect all Mates") },
942 { 0, 0,    0, NULL, (void*) &appData.testClaims, "", NULL, CheckBox, _("Verify Engine Result Claims") },
943 { 0, 0,    0, NULL, (void*) &appData.materialDraws, "", NULL, CheckBox, _("Draw if Insufficient Mating Material") },
944 { 0, 0,    0, NULL, (void*) &appData.trivialDraws, "", NULL, CheckBox, _("Adjudicate Trivial Draws (3-Move Delay)") },
945 { 0, 0,  100, NULL, (void*) &appData.ruleMoves, "", NULL, Spin, _("N-Move Rule:") },
946 { 0, 0,    6, NULL, (void*) &appData.drawRepeats, "", NULL, Spin, _("N-fold Repeats:") },
947 { 0, 0, 1000, NULL, (void*) &appData.adjudicateDrawMoves, "", NULL, Spin, _("Draw after N Moves Total:") },
948 { 0,-5000, 0, NULL, (void*) &appData.adjudicateLossThreshold, "", NULL, Spin, _("Win / Loss Threshold:") },
949 { 0, 0,    0, NULL, (void*) &first.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #1") },
950 { 0, 0,    0, NULL, (void*) &second.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #2") },
951 { 0, 1,    0, NULL, NULL, "", NULL, EndMark , "" }
952 };
953
954 void IcsOptionsOK(int n)
955 {
956     ParseIcsTextColors();
957 }
958
959 Option icsOptions[] = {
960 { 0, 0, 0, NULL, (void*) &appData.autoKibitz, "",  NULL, CheckBox, _("Auto-Kibitz") },
961 { 0, 0, 0, NULL, (void*) &appData.autoComment, "", NULL, CheckBox, _("Auto-Comment") },
962 { 0, 0, 0, NULL, (void*) &appData.autoObserve, "", NULL, CheckBox, _("Auto-Observe") },
963 { 0, 0, 0, NULL, (void*) &appData.autoRaiseBoard, "", NULL, CheckBox, _("Auto-Raise Board") },
964 { 0, 0, 0, NULL, (void*) &appData.bgObserve, "",   NULL, CheckBox, _("Background Observe while Playing") },
965 { 0, 0, 0, NULL, (void*) &appData.dualBoard, "",   NULL, CheckBox, _("Dual Board for Background-Observed Game") },
966 { 0, 0, 0, NULL, (void*) &appData.getMoveList, "", NULL, CheckBox, _("Get Move List") },
967 { 0, 0, 0, NULL, (void*) &appData.quietPlay, "",   NULL, CheckBox, _("Quiet Play") },
968 { 0, 0, 0, NULL, (void*) &appData.seekGraph, "",   NULL, CheckBox, _("Seek Graph") },
969 { 0, 0, 0, NULL, (void*) &appData.autoRefresh, "", NULL, CheckBox, _("Auto-Refresh Seek Graph") },
970 { 0, 0, 0, NULL, (void*) &appData.premove, "",     NULL, CheckBox, _("Premove") },
971 { 0, 0, 0, NULL, (void*) &appData.premoveWhite, "", NULL, CheckBox, _("Premove for White") },
972 { 0, 0, 0, NULL, (void*) &appData.premoveWhiteText, "", NULL, TextBox, _("First White Move:") },
973 { 0, 0, 0, NULL, (void*) &appData.premoveBlack, "", NULL, CheckBox, _("Premove for Black") },
974 { 0, 0, 0, NULL, (void*) &appData.premoveBlackText, "", NULL, TextBox, _("First Black Move:") },
975 { 0, 0, 0, NULL, NULL, NULL, NULL, Break, "" },
976 { 0, 0, 0, NULL, (void*) &appData.icsAlarm, "", NULL, CheckBox, _("Alarm") },
977 { 0, 0, 100000000, NULL, (void*) &appData.icsAlarmTime, "", NULL, Spin, _("Alarm Time (msec):") },
978 //{ 0, 0, 0, NULL, (void*) &appData.chatBoxes, "", NULL, TextBox, _("Startup Chat Boxes:") },
979 { 0, 0, 0, NULL, (void*) &appData.colorize, "", NULL, CheckBox, _("Colorize Messages") },
980 { 0, 0, 0, NULL, (void*) &appData.colorShout, "", NULL, TextBox, _("Shout Text Colors:") },
981 { 0, 0, 0, NULL, (void*) &appData.colorSShout, "", NULL, TextBox, _("S-Shout Text Colors:") },
982 { 0, 0, 0, NULL, (void*) &appData.colorChannel1, "", NULL, TextBox, _("Channel #1 Text Colors:") },
983 { 0, 0, 0, NULL, (void*) &appData.colorChannel, "", NULL, TextBox, _("Other Channel Text Colors:") },
984 { 0, 0, 0, NULL, (void*) &appData.colorKibitz, "", NULL, TextBox, _("Kibitz Text Colors:") },
985 { 0, 0, 0, NULL, (void*) &appData.colorTell, "", NULL, TextBox, _("Tell Text Colors:") },
986 { 0, 0, 0, NULL, (void*) &appData.colorChallenge, "", NULL, TextBox, _("Challenge Text Colors:") },
987 { 0, 0, 0, NULL, (void*) &appData.colorRequest, "", NULL, TextBox, _("Request Text Colors:") },
988 { 0, 0, 0, NULL, (void*) &appData.colorSeek, "", NULL, TextBox, _("Seek Text Colors:") },
989 { 0, 0, 0, NULL, (void*) &IcsOptionsOK, "", NULL, EndMark , "" }
990 };
991
992 Option loadOptions[] = {
993 { 0, 0, 0, NULL, (void*) &appData.autoDisplayTags, "", NULL, CheckBox, _("Auto-Display Tags") },
994 { 0, 0, 0, NULL, (void*) &appData.autoDisplayComment, "", NULL, CheckBox, _("Auto-Display Comment") },
995 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Auto-Play speed of loaded games\n(0 = instant, -1 = off):") },
996 { 0, -1, 10000000, NULL, (void*) &appData.timeDelay, "", NULL, Fractional, _("Seconds per Move:") },
997 { 0,  0, 0, NULL, NULL, "", NULL, EndMark , "" }
998 };
999
1000 Option saveOptions[] = {
1001 { 0, 0, 0, NULL, (void*) &appData.autoSaveGames, "", NULL, CheckBox, _("Auto-Save Games") },
1002 { 0, 0, 0, NULL, (void*) &appData.saveGameFile, "", NULL, FileName,  _("Save Games on File:") },
1003 { 0, 0, 0, NULL, (void*) &appData.savePositionFile, "", NULL, FileName,  _("Save Final Positions on File:") },
1004 { 0, 0, 0, NULL, (void*) &appData.pgnEventHeader, "", NULL, TextBox,  _("PGN Event Header:") },
1005 { 0, 0, 0, NULL, (void*) &appData.oldSaveStyle, "", NULL, CheckBox, _("Old Save Style (as opposed to PGN)") },
1006 { 0, 0, 0, NULL, (void*) &appData.saveExtendedInfoInPGN, "", NULL, CheckBox, _("Save Score/Depth Info in PGN") },
1007 { 0, 0, 0, NULL, (void*) &appData.saveOutOfBookInfo, "", NULL, CheckBox, _("Save Out-of-Book Info in PGN           ") },
1008 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1009 };
1010
1011 char *soundNames[] = {
1012         N_("No Sound"),
1013         N_("Default Beep"),
1014         N_("Above WAV File"),
1015         N_("Ching"),
1016         N_("Click"),
1017         N_("Ding"),
1018         N_("Gong"),
1019         N_("Laser"),
1020         N_("Penalty"),
1021         N_("Phone"),
1022         N_("Thud"),
1023         N_("Challenge"),
1024         N_("Tell"),
1025         NULL,
1026         N_("User File")
1027 };
1028
1029 char *soundFiles[] = { // sound files corresponding to above names
1030         "",
1031         "$",
1032         "*", // kludge alert: as first thing in the dialog readout this is replaced with the user-given .WAV filename
1033         "ching.wav",
1034         "click.wav",
1035         "ding1.wav",
1036         "gong.wav",
1037         "laser.wav",
1038         "penalty.wav",
1039         "phone.wav",
1040         "thud.wav",
1041         "challenge.wav",
1042         "tell.wav",
1043         NULL,
1044         NULL
1045 };
1046
1047 void Test(int n)
1048 {
1049     if(soundFiles[values[3]]) PlaySound(soundFiles[values[3]]);
1050 }
1051
1052 Option soundOptions[] = {
1053 { 0, 0, 0, NULL, (void*) &appData.soundProgram, "", NULL, TextBox, _("Sound Program:") },
1054 { 0, 0, 0, NULL, (void*) &appData.soundDirectory, "", NULL, PathName, _("Sounds Directory:") },
1055 { 0, 0, 0, NULL, (void*) (soundFiles+2) /* kludge! */, "", NULL, FileName, _("User WAV File:") },
1056 { 0, 0, 0, NULL, (void*) &trialSound, (char*) soundNames, soundFiles, ComboBox, _("Try-Out Sound:") },
1057 { 0, 1, 0, NULL, (void*) &Test, NULL, NULL, Button, _("Play") },
1058 { 0, 0, 0, NULL, (void*) &appData.soundMove, (char*) soundNames, soundFiles, ComboBox, _("Move:") },
1059 { 0, 0, 0, NULL, (void*) &appData.soundIcsWin, (char*) soundNames, soundFiles, ComboBox, _("Win:") },
1060 { 0, 0, 0, NULL, (void*) &appData.soundIcsLoss, (char*) soundNames, soundFiles, ComboBox, _("Lose:") },
1061 { 0, 0, 0, NULL, (void*) &appData.soundIcsDraw, (char*) soundNames, soundFiles, ComboBox, _("Draw:") },
1062 { 0, 0, 0, NULL, (void*) &appData.soundIcsUnfinished, (char*) soundNames, soundFiles, ComboBox, _("Unfinished:") },
1063 { 0, 0, 0, NULL, (void*) &appData.soundIcsAlarm, (char*) soundNames, soundFiles, ComboBox, _("Alarm:") },
1064 { 0, 0, 0, NULL, (void*) &appData.soundShout, (char*) soundNames, soundFiles, ComboBox, _("Shout:") },
1065 { 0, 0, 0, NULL, (void*) &appData.soundSShout, (char*) soundNames, soundFiles, ComboBox, _("S-Shout:") },
1066 { 0, 0, 0, NULL, (void*) &appData.soundChannel, (char*) soundNames, soundFiles, ComboBox, _("Channel:") },
1067 { 0, 0, 0, NULL, (void*) &appData.soundChannel1, (char*) soundNames, soundFiles, ComboBox, _("Channel 1:") },
1068 { 0, 0, 0, NULL, (void*) &appData.soundTell, (char*) soundNames, soundFiles, ComboBox, _("Tell:") },
1069 { 0, 0, 0, NULL, (void*) &appData.soundKibitz, (char*) soundNames, soundFiles, ComboBox, _("Kibitz:") },
1070 { 0, 0, 0, NULL, (void*) &appData.soundChallenge, (char*) soundNames, soundFiles, ComboBox, _("Challenge:") },
1071 { 0, 0, 0, NULL, (void*) &appData.soundRequest, (char*) soundNames, soundFiles, ComboBox, _("Request:") },
1072 { 0, 0, 0, NULL, (void*) &appData.soundSeek, (char*) soundNames, soundFiles, ComboBox, _("Seek:") },
1073 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1074 };
1075
1076 void SetColor(char *colorName, Widget box)
1077 {
1078         Arg args[5];
1079         Pixel buttonColor;
1080         XrmValue vFrom, vTo;
1081         if (!appData.monoMode) {
1082             vFrom.addr = (caddr_t) colorName;
1083             vFrom.size = strlen(colorName);
1084             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1085             if (vTo.addr == NULL) {
1086                 buttonColor = (Pixel) -1;
1087             } else {
1088                 buttonColor = *(Pixel *) vTo.addr;
1089             }
1090         }
1091         XtSetArg(args[0], XtNbackground, buttonColor);;
1092         XtSetValues(box, args, 1);
1093 }
1094
1095 void SetColorText(int n, char *buf)
1096 {
1097     Arg args[5];
1098     XtSetArg(args[0], XtNstring, buf);
1099     XtSetValues(currentOption[n-1].handle, args, 1);
1100     SetFocus(currentOption[n-1].handle, shells[0], NULL, False);
1101     SetColor(buf, currentOption[n].handle);
1102 }
1103
1104 void DefColor(int n)
1105 {
1106     SetColorText(n, (char*) currentOption[n].choice);
1107 }
1108
1109 void RefreshColor(int source, int n)
1110 {
1111     int col, j, r, g, b, step = 10;
1112     char *s, buf[MSG_SIZ]; // color string
1113     Arg args[5];
1114     XtSetArg(args[0], XtNstring, &s);
1115     XtGetValues(currentOption[source].handle, args, 1);
1116     if(sscanf(s, "#%x", &col) != 1) return;   // malformed
1117     b = col & 0xFF; g = col & 0xFF00; r = col & 0xFF0000;
1118     switch(n) {
1119         case 1: r += 0x10000*step;break;
1120         case 2: g += 0x100*step;  break;
1121         case 3: b += step;        break;
1122         case 4: r -= 0x10000*step; g -= 0x100*step; b -= step; break;
1123     }
1124     if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0;
1125     if(r > 0xFF0000) r = 0xFF0000; if(g > 0xFF00) g = 0xFF00; if(b > 0xFF) b = 0xFF;
1126     col = r | g | b;
1127     snprintf(buf, MSG_SIZ, "#%06x", col);
1128     for(j=1; j<7; j++) if(buf[j] >= 'a') buf[j] -= 32; // capitalize
1129     SetColorText(source+1, buf);
1130 }
1131
1132 void ColorChanged(Widget w, XtPointer data, XEvent *event, Boolean *b)
1133 {
1134     char buf[10];
1135     if ( (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) && *buf == '\r' )
1136         RefreshColor((int)(intptr_t) data, 0);
1137 }
1138
1139 void AdjustColor(int i)
1140 {
1141     int n = currentOption[i].value;
1142     RefreshColor(i-n-1, n);
1143 }
1144
1145 void BoardOptionsOK(int n)
1146 {
1147     extern int defaultLineGap;
1148     if(appData.overrideLineGap >= 0) lineGap = appData.overrideLineGap; else lineGap = defaultLineGap;
1149     MakeColors(); CreateGCs(True);
1150     CreateXPMPieces();
1151     CreateXPMBoard(appData.liteBackTextureFile, 1);
1152     CreateXPMBoard(appData.darkBackTextureFile, 0);
1153     InitDrawingSizes(-1, 0);
1154     DrawPosition(True, NULL);
1155 }
1156
1157 Option boardOptions[] = {
1158 { 0,   0, 70, NULL, (void*) &appData.whitePieceColor, "", NULL, TextBox, _("White Piece Color:") },
1159 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFFCC", Button, "      " },
1160 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1161 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1162 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1163 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1164 { 0,   0, 70, NULL, (void*) &appData.blackPieceColor, "", NULL, TextBox, _("Black Piece Color:") },
1165 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#202020", Button, "      " },
1166 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1167 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1168 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1169 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1170 { 0,   0, 70, NULL, (void*) &appData.lightSquareColor, "", NULL, TextBox, _("Light Square Color:") },
1171 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#C8C365", Button, "      " },
1172 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1173 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1174 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1175 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1176 { 0,   0, 70, NULL, (void*) &appData.darkSquareColor, "", NULL, TextBox, _("Dark Square Color:") },
1177 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#77A26D", Button, "      " },
1178 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1179 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1180 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1181 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1182 { 0,   0, 70, NULL, (void*) &appData.highlightSquareColor, "", NULL, TextBox, _("Highlight Color:") },
1183 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFF00", Button, "      " },
1184 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1185 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1186 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1187 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1188 { 0,   0, 70, NULL, (void*) &appData.premoveHighlightColor, "", NULL, TextBox, _("Premove Highlight Color:") },
1189 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FF0000", Button, "      " },
1190 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1191 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1192 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1193 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1194 { 0, 0, 0, NULL, (void*) &appData.upsideDown, "", NULL, CheckBox, _("Flip Pieces Shogi Style        (Colored buttons restore default)") },
1195 { 0, 0, 0, NULL, (void*) &appData.allWhite, "", NULL, CheckBox, _("Use Outline Pieces for Black") },
1196 { 0, 0, 0, NULL, (void*) &appData.monoMode, "", NULL, CheckBox, _("Mono Mode") },
1197 { 0,-1, 5, NULL, (void*) &appData.overrideLineGap, "", NULL, Spin, _("Line Gap ( -1 = default for board size):") },
1198 { 0, 0, 0, NULL, (void*) &appData.liteBackTextureFile, "", NULL, FileName, _("Light-Squares Texture File:") },
1199 { 0, 0, 0, NULL, (void*) &appData.darkBackTextureFile, "", NULL, FileName, _("Dark-Squares Texture File:") },
1200 { 0, 0, 0, NULL, (void*) &appData.bitmapDirectory, "", NULL, PathName, _("Directory with Bitmap Pieces:") },
1201 { 0, 0, 0, NULL, (void*) &appData.pixmapDirectory, "", NULL, PathName, _("Directory with Pixmap Pieces:") },
1202 { 0, 0, 0, NULL, (void*) &BoardOptionsOK, "", NULL, EndMark , "" }
1203 };
1204
1205 void GenericReadout()
1206 {
1207     int i, j;
1208     String name, val;
1209     Arg args[16];
1210     char buf[MSG_SIZ], **dest;
1211     float x;
1212         for(i=0; ; i++) { // send all options that had to be OK-ed to engine
1213             switch(currentOption[i].type) {
1214                 case TextBox:
1215                 case FileName:
1216                 case PathName:
1217                     XtSetArg(args[0], XtNstring, &val);
1218                     XtGetValues(currentOption[i].handle, args, 1);
1219                     dest = currentCps ? &(currentOption[i].textValue) : (char**) currentOption[i].target;
1220                     if(*dest == NULL || strcmp(*dest, val)) {
1221                         if(currentCps) {
1222                             snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name, val);
1223                             SendToProgram(buf, currentCps);
1224                         } else *dest = currentOption[i].name + 100; // option gets to point to private storage;
1225                         safeStrCpy(*dest, val, MSG_SIZ - (*dest - currentOption[i].name)); // copy text there
1226                     }
1227                     break;
1228                 case Spin:
1229                 case Fractional:
1230                     XtSetArg(args[0], XtNstring, &val);
1231                     XtGetValues(currentOption[i].handle, args, 1);
1232                     sscanf(val, "%f", &x);
1233                     if(x > currentOption[i].max) x = currentOption[i].max;
1234                     if(x < currentOption[i].min) x = currentOption[i].min;
1235                     if(currentOption[i].value != x) {
1236                         currentOption[i].value = x;
1237                         if(currentCps) { // engines never have float options, so no decimals!
1238                             snprintf(buf, MSG_SIZ,  "option %s=%.0f\n", currentOption[i].name, x);
1239                             SendToProgram(buf, currentCps);
1240                         } else if(currentOption[i].type == Spin) *(int*) currentOption[i].target = x;
1241                         else *(float*) currentOption[i].target = x;
1242                     }
1243                     break;
1244                 case CheckBox:
1245                     j = 0;
1246                     XtSetArg(args[0], XtNstate, &j);
1247                     XtGetValues(currentOption[i].handle, args, 1);
1248                     if(currentOption[i].value != j) {
1249                         currentOption[i].value = j;
1250                         if(currentCps) {
1251                             snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentOption[i].name, j);
1252                             SendToProgram(buf, currentCps);
1253                         } else *(Boolean*) currentOption[i].target = j;
1254                     }
1255                     break;
1256                 case ComboBox:
1257                     val = ((char**)currentOption[i].choice)[values[i]];
1258                     if(currentCps) {
1259                         if(currentOption[i].value == values[i]) break; // not changed
1260                         currentOption[i].value = values[i];
1261                         snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name,
1262                                 ((char**)currentOption[i].textValue)[values[i]]);
1263                         SendToProgram(buf, currentCps);
1264                     } else if(val && (*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val))) {
1265                       if(*(char**) currentOption[i].target) free(*(char**) currentOption[i].target);
1266                       *(char**) currentOption[i].target = strdup(val);
1267                     }
1268                     break;
1269                 case EndMark:
1270                     if(currentOption[i].target) // callback for implementing necessary actions on OK (like redraw)
1271                         ((ButtonCallback*) currentOption[i].target)(i);
1272                     break;
1273             default:
1274                 printf("GenericReadout: unexpected case in switch.\n");
1275                 case Button:
1276                 case SaveButton:
1277                 case Label:
1278               break;
1279             }
1280             if(currentOption[i].type == EndMark) break;
1281         }
1282 }
1283
1284 void GenericCallback(w, client_data, call_data)
1285      Widget w;
1286      XtPointer client_data, call_data;
1287 {
1288     String name, val;
1289     Arg args[16];
1290     char buf[MSG_SIZ];
1291     int i, j;
1292     int data = (intptr_t) client_data;
1293
1294     currentOption = dialogOptions[data>>16]; data &= 0xFFFF;
1295
1296     XtSetArg(args[0], XtNlabel, &name);
1297     XtGetValues(w, args, 1);
1298
1299     if (strcmp(name, _("cancel")) == 0) {
1300         PopDown(data);
1301         return;
1302     }
1303     if (strcmp(name, _("OK")) == 0) { // save buttons imply OK
1304         GenericReadout();
1305         PopDown(data);
1306         return;
1307     }
1308     if(currentCps) {
1309         if(currentOption[data].type == SaveButton) GenericReadout();
1310         snprintf(buf, MSG_SIZ,  "option %s\n", name);
1311         SendToProgram(buf, currentCps);
1312     } else ((ButtonCallback*) currentOption[data].target)(data);
1313 }
1314
1315 static char *oneLiner  = "<Key>Return:  redraw-display()\n";
1316
1317 int
1318 GenericPopUp(Option *option, char *title, int dlgNr)
1319 {
1320     Arg args[16];
1321     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
1322     Window root, child;
1323     int x, y, i, j, height=999, width=1, h, c, w;
1324     int win_x, win_y, maxWidth, maxTextWidth;
1325     unsigned int mask;
1326     char def[MSG_SIZ], *msg;
1327     static char pane[6] = "paneX";
1328     Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL;
1329
1330     if(shellUp[dlgNr]) return 0; // already up          
1331     if(dlgNr && shells[dlgNr]) {
1332         XtPopup(shells[dlgNr], XtGrabNone);
1333         shellUp[dlgNr] = True;
1334         return 0;
1335     }
1336
1337     dialogOptions[dlgNr] = option; // make available to callback
1338     // post currentOption globally, so Spin and Combo callbacks can already use it
1339     // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1340     currentOption = option;
1341
1342     if(currentCps) { // Settings popup for engine: format through heuristic
1343         int n = currentCps->nrOptions;
1344         if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1345         height = n / width + 1;
1346         if(currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton) currentOption[n].min = 1; // OK on same line
1347         currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1348     }
1349      i = 0;
1350     XtSetArg(args[i], XtNresizable, True); i++;
1351     popup = shells[dlgNr] =
1352       XtCreatePopupShell(title, transientShellWidgetClass,
1353                          shellWidget, args, i);
1354
1355     layout =
1356       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1357                             layoutArgs, XtNumber(layoutArgs));
1358   for(c=0; c<width; c++) {
1359     pane[4] = 'A'+c;
1360     form =
1361       XtCreateManagedWidget(pane, formWidgetClass, layout,
1362                             formArgs, XtNumber(formArgs));
1363     j=0;
1364     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1365     XtSetValues(form, args, j);
1366     leftMargin = form;
1367
1368     last = widest = NULL; anchor = lastrow;
1369     for(h=0; h<height; h++) {
1370         i = h + c*height;
1371         if(option[i].type == EndMark) break;
1372         lastrow = forelast;
1373         forelast = last;
1374         switch(option[i].type) {
1375           case Fractional:
1376             snprintf(def, MSG_SIZ,  "%.2f", *(float*)option[i].target);
1377             option[i].value = *(float*)option[i].target;
1378             goto tBox;
1379           case Spin:
1380             if(!currentCps) option[i].value = *(int*)option[i].target;
1381             snprintf(def, MSG_SIZ,  "%d", option[i].value);
1382           case TextBox:
1383           case FileName:
1384           case PathName:
1385           tBox:
1386             if(option[i].name[0]) {
1387             j=0;
1388             XtSetArg(args[j], XtNfromVert, last);  j++;
1389             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1390             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1391             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1392             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1393             texts[h] =
1394             dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1395             } else texts[h] = dialog = NULL;
1396             w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1397             if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1398             j=0;
1399             XtSetArg(args[j], XtNfromVert, last);  j++;
1400             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1401             XtSetArg(args[j], XtNborderWidth, 1); j++;
1402             XtSetArg(args[j], XtNwidth, w); j++;
1403             if(option[i].type == TextBox && option[i].min) {
1404                 XtSetArg(args[j], XtNheight, option[i].min); j++;
1405                 if(option[i].value & 1) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++; }
1406                 if(option[i].value & 2) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways);  j++; }
1407                 if(option[i].value & 4) { XtSetArg(args[j], XtNautoFill, True);  j++; }
1408                 if(option[i].value & 8) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1409             }
1410             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1411             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1412             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1413             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1414             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1415             XtSetArg(args[j], XtNresizable, True);  j++;
1416             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1417             XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def : 
1418                                 currentCps ? option[i].textValue : *(char**)option[i].target);  j++;
1419             edit = last;
1420             option[i].handle = (void*)
1421                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1422             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1423             if(option[i].min == 0 || option[i].type != TextBox)
1424                 XtOverrideTranslations(last, XtParseTranslationTable(oneLiner));
1425
1426             if(option[i].type == TextBox || option[i].type == Fractional) break;
1427
1428             // add increment and decrement controls for spin
1429             j=0;
1430             XtSetArg(args[j], XtNfromVert, edit);  j++;
1431             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1432             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1433             XtSetArg(args[j], XtNright, XtChainRight); j++;
1434             if(option[i].type == FileName || option[i].type == PathName) {
1435                 w = 50; msg = "browse";
1436             } else {
1437                 XtSetArg(args[j], XtNheight, 10);  j++;
1438                 w = 20; msg = "+";
1439             }
1440             XtSetArg(args[j], XtNwidth, w);  j++;
1441             edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1442             XtAddCallback(edit, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1443
1444             if(option[i].type != Spin) break;
1445
1446             j=0;
1447             XtSetArg(args[j], XtNfromVert, edit);  j++;
1448             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1449             XtSetArg(args[j], XtNheight, 10);  j++;
1450             XtSetArg(args[j], XtNwidth, 20);  j++;
1451             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1452             XtSetArg(args[j], XtNright, XtChainRight); j++;
1453             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1454             XtAddCallback(last, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1455             break;
1456           case CheckBox:
1457             if(!currentCps) option[i].value = *(Boolean*)option[i].target;
1458             j=0;
1459             XtSetArg(args[j], XtNfromVert, last);  j++;
1460             XtSetArg(args[j], XtNwidth, 10);  j++;
1461             XtSetArg(args[j], XtNheight, 10);  j++;
1462             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1463             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1464             XtSetArg(args[j], XtNstate, option[i].value);  j++;
1465             option[i].handle = (void*)
1466                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1467           case Label:
1468             msg = option[i].name;
1469             if(*msg == NULLCHAR) msg = option[i].textValue;
1470             if(!msg) break;
1471             j=0;
1472             XtSetArg(args[j], XtNfromVert, last);  j++;
1473             XtSetArg(args[j], XtNfromHoriz, option[i].type != Label ? dialog : NULL);  j++;
1474             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1475             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1476             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1477             last = XtCreateManagedWidget(msg, labelWidgetClass, form, args, j);
1478             if(option[i].type == CheckBox)
1479                 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i);
1480             break;
1481           case SaveButton:
1482           case Button:
1483             j=0;
1484             XtSetArg(args[j], XtNfromVert, option[i].min & 1 ? lastrow : last);  j++;
1485             if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last);  j++; }
1486             else  { XtSetArg(args[j], XtNfromHoriz, NULL);  j++; lastrow = forelast; }
1487             if(option[i].max) XtSetArg(args[j], XtNwidth, option[i].max);  j++;
1488             if(option[i].textValue) { // special for buttons of New Variant dialog
1489                 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
1490                                          || strstr(first.variants, VariantName(option[i].value))); j++;
1491                 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1492             }
1493             option[i].handle = (void*)
1494                 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1495             if(option[i].choice && !currentCps) {
1496                 SetColor( *(char**) option[i-1].target, last);
1497                 XtAddEventHandler(option[i-1].handle, KeyReleaseMask, False, ColorChanged, (XtPointer)(intptr_t) i-1);
1498             }
1499             XtAddCallback(last, XtNcallback, GenericCallback,
1500                           (XtPointer)(intptr_t) i + (dlgNr<<16));
1501             if(option[i].textValue) SetColor( option[i].textValue, last);
1502             forelast = lastrow; // next button can go on same row
1503             break;
1504           case ComboBox:
1505             j=0;
1506             XtSetArg(args[j], XtNfromVert, last);  j++;
1507             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1508             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1509             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1510             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1511             texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1512
1513             if(currentCps) option[i].choice = (char**) option[i].textValue; else {
1514               for(j=0; option[i].choice[j]; j++)
1515                 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
1516               option[i].value = j + (option[i].choice[j] == NULL);
1517             }
1518
1519             j=0;
1520             XtSetArg(args[j], XtNfromVert, last);  j++;
1521             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1522             XtSetArg(args[j], XtNwidth, option[i].max && !currentCps ? option[i].max : 100);  j++;
1523             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1524             XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name));  j++;
1525             XtSetArg(args[j], XtNlabel, ((char**)option[i].textValue)[option[i].value]);  j++;
1526             option[i].handle = (void*)
1527                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1528             CreateComboPopup(last, option[i].name, i, (char **) option[i].textValue);
1529             values[i] = option[i].value;
1530             break;
1531           case Break:
1532             width++;
1533             height = i+1;
1534             break;
1535         default:
1536             printf("GenericPopUp: unexpected case in switch.\n");
1537             break;
1538         }
1539     }
1540
1541     // make an attempt to align all spins and textbox controls
1542     maxWidth = maxTextWidth = 0;
1543     for(h=0; h<height; h++) {
1544         i = h + c*height;
1545         if(option[i].type == EndMark) break;
1546         if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1547                                   || option[i].type == PathName || option[i].type == FileName) {
1548             Dimension w;
1549             if(!texts[h]) continue;
1550             j=0;
1551             XtSetArg(args[j], XtNwidth, &w);  j++;
1552             XtGetValues(texts[h], args, j);
1553             if(option[i].type == Spin) {
1554                 if(w > maxWidth) maxWidth = w;
1555                 widest = texts[h];
1556             } else {
1557                 if(w > maxTextWidth) maxTextWidth = w;
1558                 if(!widest) widest = texts[h];
1559             }
1560         }
1561     }
1562     if(maxTextWidth + 110 < maxWidth)
1563          maxTextWidth = maxWidth - 110;
1564     else maxWidth = maxTextWidth + 110;
1565     for(h=0; h<height; h++) {
1566         i = h + c*height;
1567         if(option[i].type == EndMark) break;
1568         if(!texts[h]) continue;
1569         j=0;
1570         if(option[i].type == Spin) {
1571             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1572             XtSetValues(texts[h], args, j);
1573         } else
1574         if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1575             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1576             XtSetValues(texts[h], args, j);
1577         }
1578     }
1579   }
1580
1581   if(!(option[i].min & 2)) {
1582     j=0;
1583     if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last); last = forelast; } else
1584     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1585     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1586     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1587     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1588     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1589     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1590     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1591     XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr + (dlgNr<<16));
1592
1593     XtSetArg(args[0], XtNfromHoriz, b_ok);
1594     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1595     XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr);
1596   }
1597
1598     XtRealizeWidget(popup);
1599     XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1600     snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1601     XtAugmentTranslations(popup, XtParseTranslationTable(def));
1602     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1603                   &x, &y, &win_x, &win_y, &mask);
1604
1605     XtSetArg(args[0], XtNx, x - 10);
1606     XtSetArg(args[1], XtNy, y - 30);
1607     XtSetValues(popup, args, 2);
1608
1609     XtPopup(popup, dlgNr ? XtGrabNone : XtGrabExclusive);
1610     shellUp[dlgNr] = True;
1611     previous = NULL;
1612     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1613     if(dlgNr && wp[dlgNr] && wp[dlgNr]->width > 0) { // if persistent window-info available, reposition
1614         j = 0;
1615         XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height));  j++;
1616         XtSetArg(args[j], XtNwidth,  (Dimension) (wp[dlgNr]->width));  j++;
1617         XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x));  j++;
1618         XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y));  j++;
1619         XtSetValues(popup, args, j);
1620     }
1621     return 1;
1622 }
1623
1624
1625 void IcsOptionsProc(w, event, prms, nprms)
1626      Widget w;
1627      XEvent *event;
1628      String *prms;
1629      Cardinal *nprms;
1630 {
1631    GenericPopUp(icsOptions, _("ICS Options"), 0);
1632 }
1633
1634 void LoadOptionsProc(w, event, prms, nprms)
1635      Widget w;
1636      XEvent *event;
1637      String *prms;
1638      Cardinal *nprms;
1639 {
1640    GenericPopUp(loadOptions, _("Load Game Options"), 0);
1641 }
1642
1643 void SaveOptionsProc(w, event, prms, nprms)
1644      Widget w;
1645      XEvent *event;
1646      String *prms;
1647      Cardinal *nprms;
1648 {
1649    GenericPopUp(saveOptions, _("Save Game Options"), 0);
1650 }
1651
1652 void SoundOptionsProc(w, event, prms, nprms)
1653      Widget w;
1654      XEvent *event;
1655      String *prms;
1656      Cardinal *nprms;
1657 {
1658    soundFiles[2] = "*";
1659    GenericPopUp(soundOptions, _("Sound Options"), 0);
1660 }
1661
1662 void BoardOptionsProc(w, event, prms, nprms)
1663      Widget w;
1664      XEvent *event;
1665      String *prms;
1666      Cardinal *nprms;
1667 {
1668    GenericPopUp(boardOptions, _("Board Options"), 0);
1669 }
1670
1671 void EngineMenuProc(w, event, prms, nprms)
1672      Widget w;
1673      XEvent *event;
1674      String *prms;
1675      Cardinal *nprms;
1676 {
1677    GenericPopUp(adjudicationOptions, "Adjudicate non-ICS Games", 0);
1678 }
1679
1680 void UciMenuProc(w, event, prms, nprms)
1681      Widget w;
1682      XEvent *event;
1683      String *prms;
1684      Cardinal *nprms;
1685 {
1686    oldCores = appData.smpCores;
1687    oldPonder = appData.ponderNextMove;
1688    GenericPopUp(commonEngineOptions, _("Common Engine Settings"), 0);
1689 }
1690
1691 void NewVariantProc(w, event, prms, nprms)
1692      Widget w;
1693      XEvent *event;
1694      String *prms;
1695      Cardinal *nprms;
1696 {
1697    GenericPopUp(variantDescriptors, _("New Variant"), 0);
1698 }
1699
1700 void OptionsProc(w, event, prms, nprms)
1701      Widget w;
1702      XEvent *event;
1703      String *prms;
1704      Cardinal *nprms;
1705 {
1706    oldPonder = appData.ponderNextMove;
1707    GenericPopUp(generalOptions, _("General Options"), 0);
1708 }
1709
1710 void MatchOptionsProc(w, event, prms, nprms)
1711      Widget w;
1712      XEvent *event;
1713      String *prms;
1714      Cardinal *nprms;
1715 {
1716    GenericPopUp(matchOptions, _("Match Options"), 0);
1717 }
1718
1719 Option textOptions[100];
1720 extern char *icsTextMenuString;
1721 void PutText P((char *text, int pos));
1722
1723 SendString(char *p)
1724 {
1725     char buf[MSG_SIZ], *q;
1726     if(q = strstr(p, "$input")) {
1727         if(!shellUp[4]) return;
1728         strncpy(buf, p, MSG_SIZ);
1729         strncpy(buf + (q-p), q+6, MSG_SIZ-(q-p));
1730         PutText(buf, q-p);
1731         return;
1732     }
1733     snprintf(buf, MSG_SIZ, "%s\n", p);
1734     SendToICS(buf);
1735 }
1736
1737 /* function called when the data to Paste is ready */
1738 static void
1739 SendTextCB(Widget w, XtPointer client_data, Atom *selection,
1740            Atom *type, XtPointer value, unsigned long *len, int *format)
1741 {
1742   char buf[MSG_SIZ], *p = (char*) textOptions[(int)(intptr_t) client_data].choice, *name = (char*) value, *q;
1743   if (value==NULL || *len==0) return; /* nothing selected, abort */
1744   name[*len]='\0';
1745   strncpy(buf, p, MSG_SIZ);
1746   q = strstr(p, "$name");
1747   snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1748   SendString(buf);
1749   XtFree(value);
1750 }
1751
1752 void SendText(int n)
1753 {
1754     char *p = (char*) textOptions[n].choice;
1755     if(strstr(p, "$name")) {
1756         XtGetSelectionValue(menuBarWidget,
1757           XA_PRIMARY, XA_STRING,
1758           /* (XtSelectionCallbackProc) */ SendTextCB,
1759           (XtPointer) (intptr_t) n, /* client_data passed to PastePositionCB */
1760           CurrentTime
1761         );
1762     } else SendString(p);
1763 }
1764
1765 void IcsTextProc(w, event, prms, nprms)
1766      Widget w;
1767      XEvent *event;
1768      String *prms;
1769      Cardinal *nprms;
1770 {
1771    int i=0, j;
1772    char *p, *q, *r;
1773    if((p = icsTextMenuString) == NULL) return;
1774    do {
1775         q = r = p; while(*p && *p != ';') p++;
1776         for(j=0; j<p-q; j++) textOptions[i].name[j] = *r++;
1777         textOptions[i].name[j++] = 0;
1778         if(!*p) break;
1779         if(*++p == '\n') p++; // optional linefeed after button-text terminating semicolon
1780         q = p;
1781         textOptions[i].choice = (char**) (r = textOptions[i].name + j);
1782         while(*p && (*p != ';' || p[1] != '\n')) textOptions[i].name[j++] = *p++;
1783         textOptions[i].name[j++] = 0;
1784         if(*p) p += 2;
1785         textOptions[i].max = 135;
1786         textOptions[i].min = i&1;
1787         textOptions[i].handle = NULL;
1788         textOptions[i].target = &SendText;
1789         textOptions[i].textValue = strstr(r, "$input") ? "#80FF80" : strstr(r, "$name") ? "#FF8080" : "#FFFFFF";
1790         textOptions[i].type = Button;
1791    } while(++i < 99 && *p);
1792    if(i == 0) return;
1793    textOptions[i].type = EndMark;
1794    textOptions[i].target = NULL;
1795    textOptions[i].min = 2;
1796    MarkMenu("menuView.ICStex", 3);
1797    GenericPopUp(textOptions, _("ICS text menu"), 3);
1798 }
1799
1800 static char *commentText;
1801 static int commentIndex;
1802 void ClearComment P((int n));
1803 extern char commentTranslations[];
1804
1805 void NewComCallback(int n)
1806 {
1807     ReplaceComment(commentIndex, commentText);
1808 }
1809
1810 void SaveChanges(int n)
1811 {
1812     Arg args[16];
1813     XtSetArg(args[0], XtNstring, &commentText);
1814     XtGetValues(currentOption[0].handle, args, 1);
1815     ReplaceComment(commentIndex, commentText);
1816 }
1817
1818 Option commentOptions[] = {
1819 { 0xD, 200, 300, NULL, (void*) &commentText, "", NULL, TextBox, "" },
1820 {   0,  0,    0, NULL, (void*) &ClearComment, NULL, NULL, Button, "clear" },
1821 {   0,  1,    0, NULL, (void*) &SaveChanges, NULL, NULL, Button, "save changes" },
1822 {   0,  1,    0, NULL, (void*) &NewComCallback, "", NULL, EndMark , "" }
1823 };
1824
1825 void ClearComment(int n)
1826 {
1827     XtCallActionProc(commentOptions[0].handle, "select-all", NULL, NULL, 0);
1828     XtCallActionProc(commentOptions[0].handle, "kill-selection", NULL, NULL, 0);
1829 }
1830
1831 void NewCommentPopup(char *title, char *text, int index)
1832 {
1833     Widget edit;
1834     Arg args[16];
1835
1836     if(shells[1]) { // if already exists, alter title and content
1837         XtSetArg(args[0], XtNtitle, title);
1838         XtSetValues(shells[1], args, 1);
1839         XtSetArg(args[0], XtNstring, text);
1840         XtSetValues(commentOptions[0].handle, args, 1);
1841     }
1842     commentText = text;
1843     commentIndex = index;
1844     MarkMenu("menuView.Show Comments", 1);
1845     if(GenericPopUp(commentOptions, title, 1))
1846         XtOverrideTranslations(commentOptions[0].handle, XtParseTranslationTable(commentTranslations));
1847 }
1848
1849 static char *tagsText, *msgText;
1850
1851 void NewTagsCallback(int n)
1852 {
1853     ReplaceTags(tagsText, &gameInfo);
1854 }
1855
1856 void changeTags(int n)
1857 {
1858     Arg args[16];
1859     XtSetArg(args[0], XtNstring, &tagsText);
1860     XtGetValues(currentOption[0].handle, args, 1);
1861     ReplaceTags(tagsText, &gameInfo);
1862 }
1863
1864 Option tagsOptions[] = {
1865 { 0xD, 200, 250, NULL, (void*) &tagsText, "", NULL, TextBox, "" },
1866 {   0,  0,    0, NULL, NULL, NULL, NULL, Label,  "" },
1867 {   0,  0,    0, NULL, (void*) &changeTags, NULL, NULL, Button, "save changes" },
1868 {   0,  1,    0, NULL, (void*) &NewTagsCallback, "", NULL, EndMark , "" }
1869 };
1870
1871 void NewTagsPopup(char *text, char *msg)
1872 {
1873     Widget edit;
1874     Arg args[16];
1875
1876     if(shells[2]) { // if already exists, alter title and content
1877         XtSetArg(args[0], XtNstring, text);
1878         XtSetValues(tagsOptions[0].handle, args, 1);
1879     }
1880     tagsText = text;
1881     tagsOptions[1].textValue = msg;
1882     MarkMenu("menuView.Show Tags", 2);
1883     GenericPopUp(tagsOptions, _("Tags"), 2);
1884 }
1885
1886 extern char ICSInputTranslations[];
1887 char *icsText;
1888
1889 Option boxOptions[] = {
1890 {   0, 20,  400, NULL, (void*) &icsText, "", NULL, TextBox, "" },
1891 {   0,  3,    0, NULL, NULL, "", NULL, EndMark , "" }
1892 };
1893
1894 void PutText(char *text, int pos)
1895 {
1896     Widget edit;
1897     Arg args[16];
1898     char buf[MSG_SIZ], *p;
1899
1900     if(strstr(text, "$add ") == text) {
1901         XtSetArg(args[0], XtNstring, &p);
1902         XtGetValues(boxOptions[0].handle, args, 1);
1903         snprintf(buf, MSG_SIZ, "%s%s", p, text+5); text = buf;
1904         pos += strlen(p) - 5;
1905     }
1906     XtSetArg(args[0], XtNstring, text);
1907     XtSetValues(boxOptions[0].handle, args, 1);
1908     XtSetArg(args[0], XtNinsertPosition, pos);
1909     XtSetValues(boxOptions[0].handle, args, 1);
1910 //    SetFocus(boxOptions[0].handle, shells[4], NULL, False); // No idea why this does not work, and the following is needed:
1911     XSetInputFocus(xDisplay, XtWindow(boxOptions[0].handle), RevertToPointerRoot, CurrentTime);
1912 }
1913
1914 void InputBoxPopup()
1915 {
1916     MarkMenu("menuView.ICS Input Box", 4);
1917     if(GenericPopUp(boxOptions, _("ICS input box"), 4))
1918         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(ICSInputTranslations));
1919 }
1920
1921 void
1922 SettingsPopUp(ChessProgramState *cps)
1923 {
1924    currentCps = cps;
1925    GenericPopUp(cps->option, _("Engine Settings"), 0);
1926 }
1927
1928 void FirstSettingsProc(w, event, prms, nprms)
1929      Widget w;
1930      XEvent *event;
1931      String *prms;
1932      Cardinal *nprms;
1933 {
1934    SettingsPopUp(&first);
1935 }
1936
1937 void SecondSettingsProc(w, event, prms, nprms)
1938      Widget w;
1939      XEvent *event;
1940      String *prms;
1941      Cardinal *nprms;
1942 {
1943    if(WaitForSecond(SettingsMenuIfReady)) return;
1944    SettingsPopUp(&second);
1945 }
1946
1947 //---------------------------- Chat Windows ----------------------------------------------
1948
1949 void OutputChatMessage(int partner, char *mess)
1950 {
1951     return; // dummy
1952 }
1953