Fix switching between pixmap and bitmap pieces
[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 static Boolean browserUp;
628
629 void CheckCallback(Widget ww, XtPointer data, XEvent *event, Boolean *b)
630 {
631     Widget w = currentOption[(int)(intptr_t)data].handle;
632     Boolean s;
633     Arg args[16];
634
635     XtSetArg(args[0], XtNstate, &s);
636     XtGetValues(w, args, 1);
637     XtSetArg(args[0], XtNstate, !s);
638     XtSetValues(w, args, 1);
639 }
640
641 void SpinCallback(w, client_data, call_data)
642      Widget w;
643      XtPointer client_data, call_data;
644 {
645     String name, val;
646     Arg args[16];
647     char buf[MSG_SIZ], *p;
648     int j;
649     int data = (intptr_t) client_data;
650
651     XtSetArg(args[0], XtNlabel, &name);
652     XtGetValues(w, args, 1);
653
654     j = 0;
655     XtSetArg(args[0], XtNstring, &val);
656     XtGetValues(currentOption[data].handle, args, 1);
657     sscanf(val, "%d", &j);
658     if (strcmp(name, "browse") == 0) {
659         char *q, *r;
660         XtSetArg(args[0], XtNstring, &q);
661         XtGetValues(currentOption[data].handle, args, 1);
662         for(r = ""; *q; q++) if(*q == '.') r = q; else if(*q == '/') r = ""; // last dot after last slash
663         browserUp = True;
664         if(XsraSelFile(shells[0], currentOption[data].name, NULL, NULL, "", "", r,
665                                   currentOption[data].type == PathName ? "p" : "f", NULL, &p)) {
666                 int len = strlen(p);
667                 if(len && p[len-1] == '/') p[len-1] = NULLCHAR;
668                 XtSetArg(args[0], XtNstring, p);
669                 XtSetValues(currentOption[data].handle, args, 1);
670         }
671         browserUp = False;
672         SetFocus(currentOption[data].handle, shells[0], (XEvent*) NULL, False);
673         return;
674     } else
675     if (strcmp(name, "+") == 0) {
676         if(++j > currentOption[data].max) return;
677     } else
678     if (strcmp(name, "-") == 0) {
679         if(--j < currentOption[data].min) return;
680     } else return;
681     snprintf(buf, MSG_SIZ,  "%d", j);
682     XtSetArg(args[0], XtNstring, buf);
683     XtSetValues(currentOption[data].handle, args, 1);
684     SetFocus(currentOption[data].handle, shells[0], NULL, False);
685 }
686
687 void ComboSelect(w, addr, index) // callback for all combo items
688      Widget w;
689      caddr_t addr;
690      caddr_t index;
691 {
692     Arg args[16];
693     int i = ((intptr_t)addr)>>8;
694     int j = 255 & (intptr_t) addr;
695
696     values[i] = j; // store in temporary, for transfer at OK
697     XtSetArg(args[0], XtNlabel, ((char**)currentOption[i].textValue)[j]);
698     XtSetValues(currentOption[i].handle, args, 1);
699 }
700
701 void CreateComboPopup(parent, name, n, mb)
702      Widget parent;
703      String name;
704      int n;
705      char *mb[];
706 {
707     int i=0, j;
708     Widget menu, entry;
709     Arg args[16];
710
711     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
712                               parent, NULL, 0);
713     j = 0;
714     XtSetArg(args[j], XtNwidth, 100);  j++;
715 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
716     while (mb[i] != NULL) {
717             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
718                                           menu, args, j);
719             XtAddCallback(entry, XtNcallback,
720                           (XtCallbackProc) ComboSelect,
721                           (caddr_t)(intptr_t) (256*n+i));
722         i++;
723     }
724 }
725
726
727 //----------------------------Generic dialog --------------------------------------------
728
729 // cloned from Engine Settings dialog (and later merged with it)
730
731 typedef void ButtonCallback(int n);
732
733 extern WindowPlacement wpComment, wpTags;
734 char *trialSound;
735 static int oldCores, oldPonder;
736 int MakeColors P((void));
737 void CreateGCs P((int redo));
738 void CreateAnyPieces P((void));
739 void GenericReadout();
740 Widget shells[10];
741 Widget marked[10];
742 Boolean shellUp[10];
743 WindowPlacement *wp[10] = { NULL, &wpComment, &wpTags };
744 Option *dialogOptions[10];
745
746 void MarkMenu(char *item, int dlgNr)
747 {
748     Arg args[2];
749     XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
750     XtSetValues(marked[dlgNr] = XtNameToWidget(menuBarWidget, item), args, 1);
751 }
752
753 int PopDown(int n)
754 {
755     int j;
756     Arg args[10];
757     Dimension windowH, windowW; Position windowX, windowY;
758     if (!shellUp[n]) return 0;
759     if(n && wp[n]) { // remember position
760         j = 0;
761         XtSetArg(args[j], XtNx, &windowX); j++;
762         XtSetArg(args[j], XtNy, &windowY); j++;
763         XtSetArg(args[j], XtNheight, &windowH); j++;
764         XtSetArg(args[j], XtNwidth, &windowW); j++;
765         XtGetValues(shells[n], args, j);
766         wp[n]->x = windowX;
767         wp[n]->x = windowY;
768         wp[n]->width  = windowW;
769         wp[n]->height = windowH;
770     }
771     previous = NULL;
772     XtPopdown(shells[n]);
773     if(n == 0) XtDestroyWidget(shells[n]);
774     shellUp[n] = False;
775     if(marked[n]) {
776         XtSetArg(args[0], XtNleftBitmap, None);
777         XtSetValues(marked[n], args, 1);
778     }
779     if(!n) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
780     return 1;
781 }
782
783 void GenericPopDown(w, event, prms, nprms)
784      Widget w;
785      XEvent *event;
786      String *prms;
787      Cardinal *nprms;
788 {
789     int n;
790     if(browserUp) return; // prevent closing dialog when it has an open file-browse daughter
791     PopDown(prms[0][0] - '0');
792 }
793
794 Option matchOptions[] = {
795 { 0,  2, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, _("Default Number of Games in Match:") },
796 { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, _("Pause between Match Games (msec):") },
797 { 0,  0,          0, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, _("Game File with Opening Lines:") },
798 { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, _("Game Number (-1 or -2 = Auto-Increment):") },
799 { 0,  0,          0, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, _("File with Start Positions:") },
800 { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, _("Position Number (-1 or -2 = Auto-Increment):") },
801 { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, _("Rewind Index after this many Games (0 = never):") },
802 { 0, 0, 0, NULL, NULL, "", NULL, EndMark , "" }
803 };
804
805 void GeneralOptionsOK(int n)
806 {
807         int newPonder = appData.ponderNextMove;
808         appData.ponderNextMove = oldPonder;
809         PonderNextMoveEvent(newPonder);
810 }
811
812 Option generalOptions[] = {
813 { 0,  0, 0, NULL, (void*) &appData.sweepSelect, "", NULL, CheckBox, _("Almost Always Queen (Detour Under-Promote)") },
814 { 0,  0, 0, NULL, (void*) &appData.animateDragging, "", NULL, CheckBox, _("Animate Dragging") },
815 { 0,  0, 0, NULL, (void*) &appData.animate, "", NULL, CheckBox, _("Animate Moving") },
816 { 0,  0, 0, NULL, (void*) &appData.autoCallFlag, "", NULL, CheckBox, _("Auto Flag") },
817 { 0,  0, 0, NULL, (void*) &appData.autoFlipView, "", NULL, CheckBox, _("Auto Flip View") },
818 { 0,  0, 0, NULL, (void*) &appData.blindfold, "", NULL, CheckBox, _("Blindfold") },
819 { 0,  0, 0, NULL, (void*) &appData.dropMenu, "", NULL, CheckBox, _("Drop Menu") },
820 { 0,  0, 0, NULL, (void*) &appData.highlightDragging, "", NULL, CheckBox, _("Highlight Dragging (Show Move Targets)") },
821 { 0,  0, 0, NULL, (void*) &appData.highlightLastMove, "", NULL, CheckBox, _("Highlight Last Move") },
822 { 0,  0, 0, NULL, (void*) &appData.highlightMoveWithArrow, "", NULL, CheckBox, _("Highlight with Arrow") },
823 { 0,  0, 0, NULL, (void*) &appData.ringBellAfterMoves, "", NULL, CheckBox, _("Move Sound") },
824 { 0,  0, 0, NULL, (void*) &appData.oneClick, "", NULL, CheckBox, _("One-Click Moving") },
825 { 0,  0, 0, NULL, (void*) &appData.periodicUpdates, "", NULL, CheckBox, _("Periodic Updates (in Analysis Mode)") },
826 { 0,  0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
827 { 0,  0, 0, NULL, (void*) &appData.popupExitMessage, "", NULL, CheckBox, _("Popup Exit Messages") },
828 { 0,  0, 0, NULL, (void*) &appData.popupMoveErrors, "", NULL, CheckBox, _("Popup Move Errors") },
829 { 0,  0, 0, NULL, (void*) &appData.showCoords, "", NULL, CheckBox, _("Show Coordinates") },
830 { 0,  0, 0, NULL, (void*) &appData.markers, "", NULL, CheckBox, _("Show Target Squares") },
831 { 0,  0, 0, NULL, (void*) &appData.hideThinkingFromHuman, "", NULL, CheckBox, _("Hide Thinking from Human") },
832 { 0,  0, 0, NULL, (void*) &appData.testLegality, "", NULL, CheckBox, _("Test Legality") },
833 { 0, 0, 10, NULL, (void*) &appData.flashCount, "", NULL, Spin, _("Flash Moves (0 = no flashing):") },
834 { 0, 1, 10, NULL, (void*) &appData.flashRate, "", NULL, Spin, _("Flash Rate (high = fast):") },
835 { 0, 5, 100,NULL, (void*) &appData.animSpeed, "", NULL, Spin, _("Animation Speed (high = slow):") },
836 { 0,  0, 0, NULL, (void*) &GeneralOptionsOK, "", NULL, EndMark , "" }
837 };
838
839 void Pick(int n)
840 {
841         VariantClass v = currentOption[n].value;
842         if(!appData.noChessProgram) {
843             char *name = VariantName(v), buf[MSG_SIZ];
844             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
845                 /* [HGM] in protocol 2 we check if variant is suported by engine */
846               snprintf(buf, MSG_SIZ,  _("Variant %s not supported by %s"), name, first.tidy);
847                 DisplayError(buf, 0);
848                 return; /* ignore OK if first engine does not support it */
849             } else
850             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
851               snprintf(buf, MSG_SIZ,  _("Warning: second engine (%s) does not support this!"), second.tidy);
852                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
853             }
854         }
855
856         GenericReadout(); // make sure ranks and file settings are read
857
858         gameInfo.variant = v;
859         appData.variant = VariantName(v);
860
861         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
862         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
863         appData.pieceToCharTable = NULL;
864         appData.pieceNickNames = "";
865         appData.colorNickNames = "";
866         Reset(True, True);
867         PopDown(0);
868         return;
869 }
870
871 Option variantDescriptors[] = {
872 { VariantNormal, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("normal")},
873 { VariantFairy, 1, 135, NULL, (void*) &Pick, "#BFBFBF", NULL, Button, N_("fairy")},
874 { VariantFischeRandom, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("FRC")},
875 { VariantSChess, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Seirawan")},
876 { VariantWildCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("wild castle")},
877 { VariantSuper, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Superchess")},
878 { VariantNoCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("no castle")},
879 { VariantCrazyhouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("crazyhouse")},
880 { VariantKnightmate, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("knightmate")},
881 { VariantBughouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("bughouse")},
882 { VariantBerolina, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("berolina")},
883 { VariantShogi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("shogi (9x9)")},
884 { VariantCylinder, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("cylinder")},
885 { VariantXiangqi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("xiangqi (9x10)")},
886 { VariantShatranj, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("shatranj")},
887 { VariantCourier, 1, 135, NULL, (void*) &Pick, "#BFFFBF", NULL, Button, N_("courier (12x8)")},
888 { VariantMakruk, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("makruk")},
889 { VariantGreat, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Great Shatranj (10x8)")},
890 { VariantAtomic, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("atomic")},
891 { VariantCapablanca, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Capablanca (10x8)")},
892 { VariantTwoKings, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("two kings")},
893 { VariantGothic, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Gothic (10x8)")},
894 { Variant3Check, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("3-checks")},
895 { VariantJanus, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("janus (10x8)")},
896 { VariantSuicide, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("suicide")},
897 { VariantCapaRandom, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("CRC (10x8)")},
898 { VariantGiveaway, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("give-away")},
899 { VariantSpartan, 1, 135, NULL, (void*) &Pick, "#FF0000", NULL, Button, N_("Spartan")},
900 { VariantLosers, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("losers")},
901 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Board size ( -1 = default for selected variant):")},
902 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.NrRanks, "", NULL, Spin, N_("Number of Board Ranks:") },
903 { 0, -1, BOARD_FILES, NULL, (void*) &appData.NrFiles, "", NULL, Spin, N_("Number of Board Files:") },
904 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.holdingsSize, "", NULL, Spin, N_("Holdings Size:") },
905 { 0, 0, 0, NULL, NULL, NULL, NULL, Label,
906                                 _("WARNING: variants with un-orthodox\n"
907                                   "pieces only have built-in bitmaps\n"
908                                   "for -boardSize middling, bulky and\n"
909                                   "petite, and substitute king or amazon\n"
910                                   "for missing bitmaps. (See manual.)")},
911 { 0, 2, 0, NULL, NULL, "", NULL, EndMark , "" }
912 };
913
914 void CommonOptionsOK(int n)
915 {
916         int newPonder = appData.ponderNextMove;
917         // make sure changes are sent to first engine by re-initializing it
918         // if it was already started pre-emptively at end of previous game
919         if(gameMode == BeginningOfGame) Reset(True, True); else {
920             // Some changed setting need immediate sending always.
921             if(oldCores != appData.smpCores)
922                 NewSettingEvent(False, &(first.maxCores), "cores", appData.smpCores);
923             appData.ponderNextMove = oldPonder;
924             PonderNextMoveEvent(newPonder);
925         }
926 }
927
928 Option commonEngineOptions[] = {
929 { 0,     0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
930 { 0,  0, 1000, NULL, (void*) &appData.smpCores, "", NULL, Spin, _("Maximum Number of CPUs per Engine:") },
931 { 0,     0, 0, NULL, (void*) &appData.polyglotDir, "", NULL, PathName, _("Polygot Directory:") },
932 { 0, 0, 16000, NULL, (void*) &appData.defaultHashSize, "", NULL, Spin, _("Hash-Table Size (MB):") },
933 { 0,     0, 0, NULL, (void*) &appData.defaultPathEGTB, "", NULL, PathName, _("Nalimov EGTB Path:") },
934 { 0,  0, 1000, NULL, (void*) &appData.defaultCacheSizeEGTB, "", NULL, Spin, _("EGTB Cache Size (MB):") },
935 { 0,     0, 0, NULL, (void*) &appData.usePolyglotBook, "", NULL, CheckBox, _("Use GUI Book") },
936 { 0,     0, 0, NULL, (void*) &appData.polyglotBook, "", NULL, FileName, _("Opening-Book Filename:") },
937 { 0,   0, 100, NULL, (void*) &appData.bookDepth, "", NULL, Spin, _("Book Depth (moves):") },
938 { 0,   0, 100, NULL, (void*) &appData.bookStrength, "", NULL, Spin, _("Book Variety (0) vs. Strength (100):") },
939 { 0,     0, 0, NULL, (void*) &appData.firstHasOwnBookUCI, "", NULL, CheckBox, _("Engine #1 Has Own Book") },
940 { 0,     0, 0, NULL, (void*) &appData.secondHasOwnBookUCI, "", NULL, CheckBox, _("Engine #2 Has Own Book          ") },
941 { 0,     1, 0, NULL, (void*) &CommonOptionsOK, "", NULL, EndMark , "" }
942 };
943
944 Option adjudicationOptions[] = {
945 { 0, 0,    0, NULL, (void*) &appData.checkMates, "", NULL, CheckBox, _("Detect all Mates") },
946 { 0, 0,    0, NULL, (void*) &appData.testClaims, "", NULL, CheckBox, _("Verify Engine Result Claims") },
947 { 0, 0,    0, NULL, (void*) &appData.materialDraws, "", NULL, CheckBox, _("Draw if Insufficient Mating Material") },
948 { 0, 0,    0, NULL, (void*) &appData.trivialDraws, "", NULL, CheckBox, _("Adjudicate Trivial Draws (3-Move Delay)") },
949 { 0, 0,  100, NULL, (void*) &appData.ruleMoves, "", NULL, Spin, _("N-Move Rule:") },
950 { 0, 0,    6, NULL, (void*) &appData.drawRepeats, "", NULL, Spin, _("N-fold Repeats:") },
951 { 0, 0, 1000, NULL, (void*) &appData.adjudicateDrawMoves, "", NULL, Spin, _("Draw after N Moves Total:") },
952 { 0,-5000, 0, NULL, (void*) &appData.adjudicateLossThreshold, "", NULL, Spin, _("Win / Loss Threshold:") },
953 { 0, 0,    0, NULL, (void*) &first.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #1") },
954 { 0, 0,    0, NULL, (void*) &second.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #2") },
955 { 0, 1,    0, NULL, NULL, "", NULL, EndMark , "" }
956 };
957
958 void IcsOptionsOK(int n)
959 {
960     ParseIcsTextColors();
961 }
962
963 Option icsOptions[] = {
964 { 0, 0, 0, NULL, (void*) &appData.autoKibitz, "",  NULL, CheckBox, _("Auto-Kibitz") },
965 { 0, 0, 0, NULL, (void*) &appData.autoComment, "", NULL, CheckBox, _("Auto-Comment") },
966 { 0, 0, 0, NULL, (void*) &appData.autoObserve, "", NULL, CheckBox, _("Auto-Observe") },
967 { 0, 0, 0, NULL, (void*) &appData.autoRaiseBoard, "", NULL, CheckBox, _("Auto-Raise Board") },
968 { 0, 0, 0, NULL, (void*) &appData.bgObserve, "",   NULL, CheckBox, _("Background Observe while Playing") },
969 { 0, 0, 0, NULL, (void*) &appData.dualBoard, "",   NULL, CheckBox, _("Dual Board for Background-Observed Game") },
970 { 0, 0, 0, NULL, (void*) &appData.getMoveList, "", NULL, CheckBox, _("Get Move List") },
971 { 0, 0, 0, NULL, (void*) &appData.quietPlay, "",   NULL, CheckBox, _("Quiet Play") },
972 { 0, 0, 0, NULL, (void*) &appData.seekGraph, "",   NULL, CheckBox, _("Seek Graph") },
973 { 0, 0, 0, NULL, (void*) &appData.autoRefresh, "", NULL, CheckBox, _("Auto-Refresh Seek Graph") },
974 { 0, 0, 0, NULL, (void*) &appData.premove, "",     NULL, CheckBox, _("Premove") },
975 { 0, 0, 0, NULL, (void*) &appData.premoveWhite, "", NULL, CheckBox, _("Premove for White") },
976 { 0, 0, 0, NULL, (void*) &appData.premoveWhiteText, "", NULL, TextBox, _("First White Move:") },
977 { 0, 0, 0, NULL, (void*) &appData.premoveBlack, "", NULL, CheckBox, _("Premove for Black") },
978 { 0, 0, 0, NULL, (void*) &appData.premoveBlackText, "", NULL, TextBox, _("First Black Move:") },
979 { 0, 0, 0, NULL, NULL, NULL, NULL, Break, "" },
980 { 0, 0, 0, NULL, (void*) &appData.icsAlarm, "", NULL, CheckBox, _("Alarm") },
981 { 0, 0, 100000000, NULL, (void*) &appData.icsAlarmTime, "", NULL, Spin, _("Alarm Time (msec):") },
982 //{ 0, 0, 0, NULL, (void*) &appData.chatBoxes, "", NULL, TextBox, _("Startup Chat Boxes:") },
983 { 0, 0, 0, NULL, (void*) &appData.colorize, "", NULL, CheckBox, _("Colorize Messages") },
984 { 0, 0, 0, NULL, (void*) &appData.colorShout, "", NULL, TextBox, _("Shout Text Colors:") },
985 { 0, 0, 0, NULL, (void*) &appData.colorSShout, "", NULL, TextBox, _("S-Shout Text Colors:") },
986 { 0, 0, 0, NULL, (void*) &appData.colorChannel1, "", NULL, TextBox, _("Channel #1 Text Colors:") },
987 { 0, 0, 0, NULL, (void*) &appData.colorChannel, "", NULL, TextBox, _("Other Channel Text Colors:") },
988 { 0, 0, 0, NULL, (void*) &appData.colorKibitz, "", NULL, TextBox, _("Kibitz Text Colors:") },
989 { 0, 0, 0, NULL, (void*) &appData.colorTell, "", NULL, TextBox, _("Tell Text Colors:") },
990 { 0, 0, 0, NULL, (void*) &appData.colorChallenge, "", NULL, TextBox, _("Challenge Text Colors:") },
991 { 0, 0, 0, NULL, (void*) &appData.colorRequest, "", NULL, TextBox, _("Request Text Colors:") },
992 { 0, 0, 0, NULL, (void*) &appData.colorSeek, "", NULL, TextBox, _("Seek Text Colors:") },
993 { 0, 0, 0, NULL, (void*) &IcsOptionsOK, "", NULL, EndMark , "" }
994 };
995
996 Option loadOptions[] = {
997 { 0, 0, 0, NULL, (void*) &appData.autoDisplayTags, "", NULL, CheckBox, _("Auto-Display Tags") },
998 { 0, 0, 0, NULL, (void*) &appData.autoDisplayComment, "", NULL, CheckBox, _("Auto-Display Comment") },
999 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Auto-Play speed of loaded games\n(0 = instant, -1 = off):") },
1000 { 0, -1, 10000000, NULL, (void*) &appData.timeDelay, "", NULL, Fractional, _("Seconds per Move:") },
1001 { 0,  0, 0, NULL, NULL, "", NULL, EndMark , "" }
1002 };
1003
1004 Option saveOptions[] = {
1005 { 0, 0, 0, NULL, (void*) &appData.autoSaveGames, "", NULL, CheckBox, _("Auto-Save Games") },
1006 { 0, 0, 0, NULL, (void*) &appData.saveGameFile, "", NULL, FileName,  _("Save Games on File:") },
1007 { 0, 0, 0, NULL, (void*) &appData.savePositionFile, "", NULL, FileName,  _("Save Final Positions on File:") },
1008 { 0, 0, 0, NULL, (void*) &appData.pgnEventHeader, "", NULL, TextBox,  _("PGN Event Header:") },
1009 { 0, 0, 0, NULL, (void*) &appData.oldSaveStyle, "", NULL, CheckBox, _("Old Save Style (as opposed to PGN)") },
1010 { 0, 0, 0, NULL, (void*) &appData.saveExtendedInfoInPGN, "", NULL, CheckBox, _("Save Score/Depth Info in PGN") },
1011 { 0, 0, 0, NULL, (void*) &appData.saveOutOfBookInfo, "", NULL, CheckBox, _("Save Out-of-Book Info in PGN           ") },
1012 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1013 };
1014
1015 char *soundNames[] = {
1016         N_("No Sound"),
1017         N_("Default Beep"),
1018         N_("Above WAV File"),
1019         N_("Ching"),
1020         N_("Click"),
1021         N_("Ding"),
1022         N_("Gong"),
1023         N_("Laser"),
1024         N_("Penalty"),
1025         N_("Phone"),
1026         N_("Thud"),
1027         N_("Challenge"),
1028         N_("Tell"),
1029         NULL,
1030         N_("User File")
1031 };
1032
1033 char *soundFiles[] = { // sound files corresponding to above names
1034         "",
1035         "$",
1036         "*", // kludge alert: as first thing in the dialog readout this is replaced with the user-given .WAV filename
1037         "ching.wav",
1038         "click.wav",
1039         "ding1.wav",
1040         "gong.wav",
1041         "laser.wav",
1042         "penalty.wav",
1043         "phone.wav",
1044         "thud.wav",
1045         "challenge.wav",
1046         "tell.wav",
1047         NULL,
1048         NULL
1049 };
1050
1051 void Test(int n)
1052 {
1053     if(soundFiles[values[3]]) PlaySound(soundFiles[values[3]]);
1054 }
1055
1056 Option soundOptions[] = {
1057 { 0, 0, 0, NULL, (void*) &appData.soundProgram, "", NULL, TextBox, _("Sound Program:") },
1058 { 0, 0, 0, NULL, (void*) &appData.soundDirectory, "", NULL, PathName, _("Sounds Directory:") },
1059 { 0, 0, 0, NULL, (void*) (soundFiles+2) /* kludge! */, "", NULL, FileName, _("User WAV File:") },
1060 { 0, 0, 0, NULL, (void*) &trialSound, (char*) soundNames, soundFiles, ComboBox, _("Try-Out Sound:") },
1061 { 0, 1, 0, NULL, (void*) &Test, NULL, NULL, Button, _("Play") },
1062 { 0, 0, 0, NULL, (void*) &appData.soundMove, (char*) soundNames, soundFiles, ComboBox, _("Move:") },
1063 { 0, 0, 0, NULL, (void*) &appData.soundIcsWin, (char*) soundNames, soundFiles, ComboBox, _("Win:") },
1064 { 0, 0, 0, NULL, (void*) &appData.soundIcsLoss, (char*) soundNames, soundFiles, ComboBox, _("Lose:") },
1065 { 0, 0, 0, NULL, (void*) &appData.soundIcsDraw, (char*) soundNames, soundFiles, ComboBox, _("Draw:") },
1066 { 0, 0, 0, NULL, (void*) &appData.soundIcsUnfinished, (char*) soundNames, soundFiles, ComboBox, _("Unfinished:") },
1067 { 0, 0, 0, NULL, (void*) &appData.soundIcsAlarm, (char*) soundNames, soundFiles, ComboBox, _("Alarm:") },
1068 { 0, 0, 0, NULL, (void*) &appData.soundShout, (char*) soundNames, soundFiles, ComboBox, _("Shout:") },
1069 { 0, 0, 0, NULL, (void*) &appData.soundSShout, (char*) soundNames, soundFiles, ComboBox, _("S-Shout:") },
1070 { 0, 0, 0, NULL, (void*) &appData.soundChannel, (char*) soundNames, soundFiles, ComboBox, _("Channel:") },
1071 { 0, 0, 0, NULL, (void*) &appData.soundChannel1, (char*) soundNames, soundFiles, ComboBox, _("Channel 1:") },
1072 { 0, 0, 0, NULL, (void*) &appData.soundTell, (char*) soundNames, soundFiles, ComboBox, _("Tell:") },
1073 { 0, 0, 0, NULL, (void*) &appData.soundKibitz, (char*) soundNames, soundFiles, ComboBox, _("Kibitz:") },
1074 { 0, 0, 0, NULL, (void*) &appData.soundChallenge, (char*) soundNames, soundFiles, ComboBox, _("Challenge:") },
1075 { 0, 0, 0, NULL, (void*) &appData.soundRequest, (char*) soundNames, soundFiles, ComboBox, _("Request:") },
1076 { 0, 0, 0, NULL, (void*) &appData.soundSeek, (char*) soundNames, soundFiles, ComboBox, _("Seek:") },
1077 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1078 };
1079
1080 void SetColor(char *colorName, Widget box)
1081 {
1082         Arg args[5];
1083         Pixel buttonColor;
1084         XrmValue vFrom, vTo;
1085         if (!appData.monoMode) {
1086             vFrom.addr = (caddr_t) colorName;
1087             vFrom.size = strlen(colorName);
1088             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1089             if (vTo.addr == NULL) {
1090                 buttonColor = (Pixel) -1;
1091             } else {
1092                 buttonColor = *(Pixel *) vTo.addr;
1093             }
1094         }
1095         XtSetArg(args[0], XtNbackground, buttonColor);;
1096         XtSetValues(box, args, 1);
1097 }
1098
1099 void SetColorText(int n, char *buf)
1100 {
1101     Arg args[5];
1102     XtSetArg(args[0], XtNstring, buf);
1103     XtSetValues(currentOption[n-1].handle, args, 1);
1104     SetFocus(currentOption[n-1].handle, shells[0], NULL, False);
1105     SetColor(buf, currentOption[n].handle);
1106 }
1107
1108 void DefColor(int n)
1109 {
1110     SetColorText(n, (char*) currentOption[n].choice);
1111 }
1112
1113 void RefreshColor(int source, int n)
1114 {
1115     int col, j, r, g, b, step = 10;
1116     char *s, buf[MSG_SIZ]; // color string
1117     Arg args[5];
1118     XtSetArg(args[0], XtNstring, &s);
1119     XtGetValues(currentOption[source].handle, args, 1);
1120     if(sscanf(s, "#%x", &col) != 1) return;   // malformed
1121     b = col & 0xFF; g = col & 0xFF00; r = col & 0xFF0000;
1122     switch(n) {
1123         case 1: r += 0x10000*step;break;
1124         case 2: g += 0x100*step;  break;
1125         case 3: b += step;        break;
1126         case 4: r -= 0x10000*step; g -= 0x100*step; b -= step; break;
1127     }
1128     if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0;
1129     if(r > 0xFF0000) r = 0xFF0000; if(g > 0xFF00) g = 0xFF00; if(b > 0xFF) b = 0xFF;
1130     col = r | g | b;
1131     snprintf(buf, MSG_SIZ, "#%06x", col);
1132     for(j=1; j<7; j++) if(buf[j] >= 'a') buf[j] -= 32; // capitalize
1133     SetColorText(source+1, buf);
1134 }
1135
1136 void ColorChanged(Widget w, XtPointer data, XEvent *event, Boolean *b)
1137 {
1138     char buf[10];
1139     if ( (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) && *buf == '\r' )
1140         RefreshColor((int)(intptr_t) data, 0);
1141 }
1142
1143 void AdjustColor(int i)
1144 {
1145     int n = currentOption[i].value;
1146     RefreshColor(i-n-1, n);
1147 }
1148
1149 void BoardOptionsOK(int n)
1150 {
1151     extern int defaultLineGap, useImages, useImageSqs;
1152     if(appData.overrideLineGap >= 0) lineGap = appData.overrideLineGap; else lineGap = defaultLineGap;
1153     useImages = useImageSqs = 0;
1154     MakeColors(); CreateGCs(True);
1155     CreateAnyPieces();
1156     InitDrawingSizes(-1, 0);
1157     DrawPosition(True, NULL);
1158 }
1159
1160 Option boardOptions[] = {
1161 { 0,   0, 70, NULL, (void*) &appData.whitePieceColor, "", NULL, TextBox, _("White Piece Color:") },
1162 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFFCC", Button, "      " },
1163 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1164 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1165 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1166 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1167 { 0,   0, 70, NULL, (void*) &appData.blackPieceColor, "", NULL, TextBox, _("Black Piece Color:") },
1168 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#202020", Button, "      " },
1169 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1170 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1171 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1172 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1173 { 0,   0, 70, NULL, (void*) &appData.lightSquareColor, "", NULL, TextBox, _("Light Square Color:") },
1174 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#C8C365", Button, "      " },
1175 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1176 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1177 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1178 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1179 { 0,   0, 70, NULL, (void*) &appData.darkSquareColor, "", NULL, TextBox, _("Dark Square Color:") },
1180 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#77A26D", Button, "      " },
1181 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1182 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1183 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1184 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1185 { 0,   0, 70, NULL, (void*) &appData.highlightSquareColor, "", NULL, TextBox, _("Highlight Color:") },
1186 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFF00", Button, "      " },
1187 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1188 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1189 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1190 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1191 { 0,   0, 70, NULL, (void*) &appData.premoveHighlightColor, "", NULL, TextBox, _("Premove Highlight Color:") },
1192 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FF0000", Button, "      " },
1193 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1194 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1195 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1196 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1197 { 0, 0, 0, NULL, (void*) &appData.upsideDown, "", NULL, CheckBox, _("Flip Pieces Shogi Style        (Colored buttons restore default)") },
1198 //{ 0, 0, 0, NULL, (void*) &appData.allWhite, "", NULL, CheckBox, _("Use Outline Pieces for Black") },
1199 { 0, 0, 0, NULL, (void*) &appData.monoMode, "", NULL, CheckBox, _("Mono Mode") },
1200 { 0,-1, 5, NULL, (void*) &appData.overrideLineGap, "", NULL, Spin, _("Line Gap ( -1 = default for board size):") },
1201 { 0, 0, 0, NULL, (void*) &appData.liteBackTextureFile, "", NULL, FileName, _("Light-Squares Texture File:") },
1202 { 0, 0, 0, NULL, (void*) &appData.darkBackTextureFile, "", NULL, FileName, _("Dark-Squares Texture File:") },
1203 { 0, 0, 0, NULL, (void*) &appData.bitmapDirectory, "", NULL, PathName, _("Directory with Bitmap Pieces:") },
1204 { 0, 0, 0, NULL, (void*) &appData.pixmapDirectory, "", NULL, PathName, _("Directory with Pixmap Pieces:") },
1205 { 0, 0, 0, NULL, (void*) &BoardOptionsOK, "", NULL, EndMark , "" }
1206 };
1207
1208 void GenericReadout()
1209 {
1210     int i, j;
1211     String name, val;
1212     Arg args[16];
1213     char buf[MSG_SIZ], **dest;
1214     float x;
1215         for(i=0; ; i++) { // send all options that had to be OK-ed to engine
1216             switch(currentOption[i].type) {
1217                 case TextBox:
1218                 case FileName:
1219                 case PathName:
1220                     XtSetArg(args[0], XtNstring, &val);
1221                     XtGetValues(currentOption[i].handle, args, 1);
1222                     dest = currentCps ? &(currentOption[i].textValue) : (char**) currentOption[i].target;
1223                     if(*dest == NULL || strcmp(*dest, val)) {
1224                         if(currentCps) {
1225                             snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name, val);
1226                             SendToProgram(buf, currentCps);
1227                         } else *dest = currentOption[i].name + 100; // option gets to point to private storage;
1228                         safeStrCpy(*dest, val, MSG_SIZ - (*dest - currentOption[i].name)); // copy text there
1229                     }
1230                     break;
1231                 case Spin:
1232                 case Fractional:
1233                     XtSetArg(args[0], XtNstring, &val);
1234                     XtGetValues(currentOption[i].handle, args, 1);
1235                     sscanf(val, "%f", &x);
1236                     if(x > currentOption[i].max) x = currentOption[i].max;
1237                     if(x < currentOption[i].min) x = currentOption[i].min;
1238                     if(currentOption[i].type == Fractional)
1239                         *(float*) currentOption[i].target = x; // engines never have float options!
1240                     else if(currentOption[i].value != x) {
1241                         currentOption[i].value = x;
1242                         if(currentCps) {
1243                             snprintf(buf, MSG_SIZ,  "option %s=%.0f\n", currentOption[i].name, x);
1244                             SendToProgram(buf, currentCps);
1245                         } else *(int*) currentOption[i].target = x;
1246                     }
1247                     break;
1248                 case CheckBox:
1249                     j = 0;
1250                     XtSetArg(args[0], XtNstate, &j);
1251                     XtGetValues(currentOption[i].handle, args, 1);
1252                     if(currentOption[i].value != j) {
1253                         currentOption[i].value = j;
1254                         if(currentCps) {
1255                             snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentOption[i].name, j);
1256                             SendToProgram(buf, currentCps);
1257                         } else *(Boolean*) currentOption[i].target = j;
1258                     }
1259                     break;
1260                 case ComboBox:
1261                     val = ((char**)currentOption[i].choice)[values[i]];
1262                     if(currentCps) {
1263                         if(currentOption[i].value == values[i]) break; // not changed
1264                         currentOption[i].value = values[i];
1265                         snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name,
1266                                 ((char**)currentOption[i].textValue)[values[i]]);
1267                         SendToProgram(buf, currentCps);
1268                     } else if(val && (*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val))) {
1269                       if(*(char**) currentOption[i].target) free(*(char**) currentOption[i].target);
1270                       *(char**) currentOption[i].target = strdup(val);
1271                     }
1272                     break;
1273                 case EndMark:
1274                     if(currentOption[i].target) // callback for implementing necessary actions on OK (like redraw)
1275                         ((ButtonCallback*) currentOption[i].target)(i);
1276                     break;
1277             default:
1278                 printf("GenericReadout: unexpected case in switch.\n");
1279                 case Button:
1280                 case SaveButton:
1281                 case Label:
1282                 case Break:
1283               break;
1284             }
1285             if(currentOption[i].type == EndMark) break;
1286         }
1287 }
1288
1289 void GenericCallback(w, client_data, call_data)
1290      Widget w;
1291      XtPointer client_data, call_data;
1292 {
1293     String name, val;
1294     Arg args[16];
1295     char buf[MSG_SIZ];
1296     int i, j;
1297     int data = (intptr_t) client_data;
1298
1299     currentOption = dialogOptions[data>>16]; data &= 0xFFFF;
1300
1301     XtSetArg(args[0], XtNlabel, &name);
1302     XtGetValues(w, args, 1);
1303
1304     if (strcmp(name, _("cancel")) == 0) {
1305         PopDown(data);
1306         return;
1307     }
1308     if (strcmp(name, _("OK")) == 0) { // save buttons imply OK
1309         GenericReadout();
1310         PopDown(data);
1311         return;
1312     }
1313     if(currentCps) {
1314         if(currentOption[data].type == SaveButton) GenericReadout();
1315         snprintf(buf, MSG_SIZ,  "option %s\n", name);
1316         SendToProgram(buf, currentCps);
1317     } else ((ButtonCallback*) currentOption[data].target)(data);
1318 }
1319
1320 static char *oneLiner  = "<Key>Return:  redraw-display()\n";
1321
1322 int
1323 GenericPopUp(Option *option, char *title, int dlgNr)
1324 {
1325     Arg args[16];
1326     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
1327     Window root, child;
1328     int x, y, i, j, height=999, width=1, h, c, w;
1329     int win_x, win_y, maxWidth, maxTextWidth;
1330     unsigned int mask;
1331     char def[MSG_SIZ], *msg;
1332     static char pane[6] = "paneX";
1333     Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL;
1334
1335     if(shellUp[dlgNr]) return 0; // already up          
1336     if(dlgNr && shells[dlgNr]) {
1337         XtPopup(shells[dlgNr], XtGrabNone);
1338         shellUp[dlgNr] = True;
1339         return 0;
1340     }
1341
1342     dialogOptions[dlgNr] = option; // make available to callback
1343     // post currentOption globally, so Spin and Combo callbacks can already use it
1344     // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1345     currentOption = option;
1346
1347     if(currentCps) { // Settings popup for engine: format through heuristic
1348         int n = currentCps->nrOptions;
1349         if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1350         height = n / width + 1;
1351         if(currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton) currentOption[n].min = 1; // OK on same line
1352         currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1353     }
1354      i = 0;
1355     XtSetArg(args[i], XtNresizable, True); i++;
1356     popup = shells[dlgNr] =
1357       XtCreatePopupShell(title, transientShellWidgetClass,
1358                          shellWidget, args, i);
1359
1360     layout =
1361       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1362                             layoutArgs, XtNumber(layoutArgs));
1363   for(c=0; c<width; c++) {
1364     pane[4] = 'A'+c;
1365     form =
1366       XtCreateManagedWidget(pane, formWidgetClass, layout,
1367                             formArgs, XtNumber(formArgs));
1368     j=0;
1369     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1370     XtSetValues(form, args, j);
1371     leftMargin = form;
1372
1373     last = widest = NULL; anchor = lastrow;
1374     for(h=0; h<height; h++) {
1375         i = h + c*height;
1376         if(option[i].type == EndMark) break;
1377         lastrow = forelast;
1378         forelast = last;
1379         switch(option[i].type) {
1380           case Fractional:
1381             snprintf(def, MSG_SIZ,  "%.2f", *(float*)option[i].target);
1382             option[i].value = *(float*)option[i].target;
1383             goto tBox;
1384           case Spin:
1385             if(!currentCps) option[i].value = *(int*)option[i].target;
1386             snprintf(def, MSG_SIZ,  "%d", option[i].value);
1387           case TextBox:
1388           case FileName:
1389           case PathName:
1390           tBox:
1391             if(option[i].name[0]) {
1392             j=0;
1393             XtSetArg(args[j], XtNfromVert, last);  j++;
1394             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1395             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1396             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1397             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1398             texts[h] =
1399             dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1400             } else texts[h] = dialog = NULL;
1401             w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1402             if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1403             j=0;
1404             XtSetArg(args[j], XtNfromVert, last);  j++;
1405             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1406             XtSetArg(args[j], XtNborderWidth, 1); j++;
1407             XtSetArg(args[j], XtNwidth, w); j++;
1408             if(option[i].type == TextBox && option[i].min) {
1409                 XtSetArg(args[j], XtNheight, option[i].min); j++;
1410                 if(option[i].value & 1) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++; }
1411                 if(option[i].value & 2) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways);  j++; }
1412                 if(option[i].value & 4) { XtSetArg(args[j], XtNautoFill, True);  j++; }
1413                 if(option[i].value & 8) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1414             }
1415             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1416             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1417             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1418             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1419             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1420             XtSetArg(args[j], XtNresizable, True);  j++;
1421             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1422             XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def : 
1423                                 currentCps ? option[i].textValue : *(char**)option[i].target);  j++;
1424             edit = last;
1425             option[i].handle = (void*)
1426                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1427             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1428             if(option[i].min == 0 || option[i].type != TextBox)
1429                 XtOverrideTranslations(last, XtParseTranslationTable(oneLiner));
1430
1431             if(option[i].type == TextBox || option[i].type == Fractional) break;
1432
1433             // add increment and decrement controls for spin
1434             j=0;
1435             XtSetArg(args[j], XtNfromVert, edit);  j++;
1436             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1437             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1438             XtSetArg(args[j], XtNright, XtChainRight); j++;
1439             if(option[i].type == FileName || option[i].type == PathName) {
1440                 w = 50; msg = "browse";
1441             } else {
1442                 XtSetArg(args[j], XtNheight, 10);  j++;
1443                 w = 20; msg = "+";
1444             }
1445             XtSetArg(args[j], XtNwidth, w);  j++;
1446             edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1447             XtAddCallback(edit, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1448
1449             if(option[i].type != Spin) break;
1450
1451             j=0;
1452             XtSetArg(args[j], XtNfromVert, edit);  j++;
1453             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1454             XtSetArg(args[j], XtNheight, 10);  j++;
1455             XtSetArg(args[j], XtNwidth, 20);  j++;
1456             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1457             XtSetArg(args[j], XtNright, XtChainRight); j++;
1458             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1459             XtAddCallback(last, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1460             break;
1461           case CheckBox:
1462             if(!currentCps) option[i].value = *(Boolean*)option[i].target;
1463             j=0;
1464             XtSetArg(args[j], XtNfromVert, last);  j++;
1465             XtSetArg(args[j], XtNwidth, 10);  j++;
1466             XtSetArg(args[j], XtNheight, 10);  j++;
1467             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1468             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1469             XtSetArg(args[j], XtNstate, option[i].value);  j++;
1470             option[i].handle = (void*)
1471                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1472           case Label:
1473             msg = option[i].name;
1474             if(*msg == NULLCHAR) msg = option[i].textValue;
1475             if(!msg) break;
1476             j=0;
1477             XtSetArg(args[j], XtNfromVert, last);  j++;
1478             XtSetArg(args[j], XtNfromHoriz, option[i].type != Label ? dialog : NULL);  j++;
1479             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1480             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1481             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1482             last = XtCreateManagedWidget(msg, labelWidgetClass, form, args, j);
1483             if(option[i].type == CheckBox)
1484                 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i);
1485             break;
1486           case SaveButton:
1487           case Button:
1488             j=0;
1489             XtSetArg(args[j], XtNfromVert, option[i].min & 1 ? lastrow : last);  j++;
1490             if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last);  j++; }
1491             else  { XtSetArg(args[j], XtNfromHoriz, NULL);  j++; lastrow = forelast; }
1492             if(option[i].max) XtSetArg(args[j], XtNwidth, option[i].max);  j++;
1493             if(option[i].textValue) { // special for buttons of New Variant dialog
1494                 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
1495                                          || strstr(first.variants, VariantName(option[i].value))); j++;
1496                 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1497             }
1498             option[i].handle = (void*)
1499                 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1500             if(option[i].choice && ((char*)option[i].choice)[0] == '#' && !currentCps) {
1501                 SetColor( *(char**) option[i-1].target, last);
1502                 XtAddEventHandler(option[i-1].handle, KeyReleaseMask, False, ColorChanged, (XtPointer)(intptr_t) i-1);
1503             }
1504             XtAddCallback(last, XtNcallback, GenericCallback,
1505                           (XtPointer)(intptr_t) i + (dlgNr<<16));
1506             if(option[i].textValue) SetColor( option[i].textValue, last);
1507             forelast = lastrow; // next button can go on same row
1508             break;
1509           case ComboBox:
1510             j=0;
1511             XtSetArg(args[j], XtNfromVert, last);  j++;
1512             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1513             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1514             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1515             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1516             texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1517
1518             if(currentCps) option[i].choice = (char**) option[i].textValue; else {
1519               for(j=0; option[i].choice[j]; j++)
1520                 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
1521               option[i].value = j + (option[i].choice[j] == NULL);
1522             }
1523
1524             j=0;
1525             XtSetArg(args[j], XtNfromVert, last);  j++;
1526             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1527             XtSetArg(args[j], XtNwidth, option[i].max && !currentCps ? option[i].max : 100);  j++;
1528             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1529             XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name));  j++;
1530             XtSetArg(args[j], XtNlabel, ((char**)option[i].textValue)[option[i].value]);  j++;
1531             option[i].handle = (void*)
1532                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1533             CreateComboPopup(last, option[i].name, i, (char **) option[i].textValue);
1534             values[i] = option[i].value;
1535             break;
1536           case Break:
1537             width++;
1538             height = i+1;
1539             break;
1540         default:
1541             printf("GenericPopUp: unexpected case in switch.\n");
1542             break;
1543         }
1544     }
1545
1546     // make an attempt to align all spins and textbox controls
1547     maxWidth = maxTextWidth = 0;
1548     for(h=0; h<height; h++) {
1549         i = h + c*height;
1550         if(option[i].type == EndMark) break;
1551         if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1552                                   || option[i].type == PathName || option[i].type == FileName) {
1553             Dimension w;
1554             if(!texts[h]) continue;
1555             j=0;
1556             XtSetArg(args[j], XtNwidth, &w);  j++;
1557             XtGetValues(texts[h], args, j);
1558             if(option[i].type == Spin) {
1559                 if(w > maxWidth) maxWidth = w;
1560                 widest = texts[h];
1561             } else {
1562                 if(w > maxTextWidth) maxTextWidth = w;
1563                 if(!widest) widest = texts[h];
1564             }
1565         }
1566     }
1567     if(maxTextWidth + 110 < maxWidth)
1568          maxTextWidth = maxWidth - 110;
1569     else maxWidth = maxTextWidth + 110;
1570     for(h=0; h<height; h++) {
1571         i = h + c*height;
1572         if(option[i].type == EndMark) break;
1573         if(!texts[h]) continue;
1574         j=0;
1575         if(option[i].type == Spin) {
1576             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1577             XtSetValues(texts[h], args, j);
1578         } else
1579         if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1580             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1581             XtSetValues(texts[h], args, j);
1582         }
1583     }
1584   }
1585
1586   if(!(option[i].min & 2)) {
1587     j=0;
1588     if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last); last = forelast; } else
1589     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1590     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1591     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1592     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1593     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1594     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1595     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1596     XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr + (dlgNr<<16));
1597
1598     XtSetArg(args[0], XtNfromHoriz, b_ok);
1599     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1600     XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr);
1601   }
1602
1603     XtRealizeWidget(popup);
1604     XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1605     snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1606     XtAugmentTranslations(popup, XtParseTranslationTable(def));
1607     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1608                   &x, &y, &win_x, &win_y, &mask);
1609
1610     XtSetArg(args[0], XtNx, x - 10);
1611     XtSetArg(args[1], XtNy, y - 30);
1612     XtSetValues(popup, args, 2);
1613
1614     XtPopup(popup, dlgNr ? XtGrabNone : XtGrabExclusive);
1615     shellUp[dlgNr] = True;
1616     previous = NULL;
1617     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1618     if(dlgNr && wp[dlgNr] && wp[dlgNr]->width > 0) { // if persistent window-info available, reposition
1619         j = 0;
1620         XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height));  j++;
1621         XtSetArg(args[j], XtNwidth,  (Dimension) (wp[dlgNr]->width));  j++;
1622         XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x));  j++;
1623         XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y));  j++;
1624         XtSetValues(popup, args, j);
1625     }
1626     return 1;
1627 }
1628
1629
1630 void IcsOptionsProc(w, event, prms, nprms)
1631      Widget w;
1632      XEvent *event;
1633      String *prms;
1634      Cardinal *nprms;
1635 {
1636    GenericPopUp(icsOptions, _("ICS Options"), 0);
1637 }
1638
1639 void LoadOptionsProc(w, event, prms, nprms)
1640      Widget w;
1641      XEvent *event;
1642      String *prms;
1643      Cardinal *nprms;
1644 {
1645    GenericPopUp(loadOptions, _("Load Game Options"), 0);
1646 }
1647
1648 void SaveOptionsProc(w, event, prms, nprms)
1649      Widget w;
1650      XEvent *event;
1651      String *prms;
1652      Cardinal *nprms;
1653 {
1654    GenericPopUp(saveOptions, _("Save Game Options"), 0);
1655 }
1656
1657 void SoundOptionsProc(w, event, prms, nprms)
1658      Widget w;
1659      XEvent *event;
1660      String *prms;
1661      Cardinal *nprms;
1662 {
1663    soundFiles[2] = "*";
1664    GenericPopUp(soundOptions, _("Sound Options"), 0);
1665 }
1666
1667 void BoardOptionsProc(w, event, prms, nprms)
1668      Widget w;
1669      XEvent *event;
1670      String *prms;
1671      Cardinal *nprms;
1672 {
1673    GenericPopUp(boardOptions, _("Board Options"), 0);
1674 }
1675
1676 void EngineMenuProc(w, event, prms, nprms)
1677      Widget w;
1678      XEvent *event;
1679      String *prms;
1680      Cardinal *nprms;
1681 {
1682    GenericPopUp(adjudicationOptions, "Adjudicate non-ICS Games", 0);
1683 }
1684
1685 void UciMenuProc(w, event, prms, nprms)
1686      Widget w;
1687      XEvent *event;
1688      String *prms;
1689      Cardinal *nprms;
1690 {
1691    oldCores = appData.smpCores;
1692    oldPonder = appData.ponderNextMove;
1693    GenericPopUp(commonEngineOptions, _("Common Engine Settings"), 0);
1694 }
1695
1696 void NewVariantProc(w, event, prms, nprms)
1697      Widget w;
1698      XEvent *event;
1699      String *prms;
1700      Cardinal *nprms;
1701 {
1702    GenericPopUp(variantDescriptors, _("New Variant"), 0);
1703 }
1704
1705 void OptionsProc(w, event, prms, nprms)
1706      Widget w;
1707      XEvent *event;
1708      String *prms;
1709      Cardinal *nprms;
1710 {
1711    oldPonder = appData.ponderNextMove;
1712    GenericPopUp(generalOptions, _("General Options"), 0);
1713 }
1714
1715 void MatchOptionsProc(w, event, prms, nprms)
1716      Widget w;
1717      XEvent *event;
1718      String *prms;
1719      Cardinal *nprms;
1720 {
1721    GenericPopUp(matchOptions, _("Match Options"), 0);
1722 }
1723
1724 Option textOptions[100];
1725 extern char *icsTextMenuString;
1726 void PutText P((char *text, int pos));
1727
1728 SendString(char *p)
1729 {
1730     char buf[MSG_SIZ], *q;
1731     if(q = strstr(p, "$input")) {
1732         if(!shellUp[4]) return;
1733         strncpy(buf, p, MSG_SIZ);
1734         strncpy(buf + (q-p), q+6, MSG_SIZ-(q-p));
1735         PutText(buf, q-p);
1736         return;
1737     }
1738     snprintf(buf, MSG_SIZ, "%s\n", p);
1739     SendToICS(buf);
1740 }
1741
1742 /* function called when the data to Paste is ready */
1743 static void
1744 SendTextCB(Widget w, XtPointer client_data, Atom *selection,
1745            Atom *type, XtPointer value, unsigned long *len, int *format)
1746 {
1747   char buf[MSG_SIZ], *p = (char*) textOptions[(int)(intptr_t) client_data].choice, *name = (char*) value, *q;
1748   if (value==NULL || *len==0) return; /* nothing selected, abort */
1749   name[*len]='\0';
1750   strncpy(buf, p, MSG_SIZ);
1751   q = strstr(p, "$name");
1752   snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1753   SendString(buf);
1754   XtFree(value);
1755 }
1756
1757 void SendText(int n)
1758 {
1759     char *p = (char*) textOptions[n].choice;
1760     if(strstr(p, "$name")) {
1761         XtGetSelectionValue(menuBarWidget,
1762           XA_PRIMARY, XA_STRING,
1763           /* (XtSelectionCallbackProc) */ SendTextCB,
1764           (XtPointer) (intptr_t) n, /* client_data passed to PastePositionCB */
1765           CurrentTime
1766         );
1767     } else SendString(p);
1768 }
1769
1770 void IcsTextProc(w, event, prms, nprms)
1771      Widget w;
1772      XEvent *event;
1773      String *prms;
1774      Cardinal *nprms;
1775 {
1776    int i=0, j;
1777    char *p, *q, *r;
1778    if((p = icsTextMenuString) == NULL) return;
1779    do {
1780         q = r = p; while(*p && *p != ';') p++;
1781         for(j=0; j<p-q; j++) textOptions[i].name[j] = *r++;
1782         textOptions[i].name[j++] = 0;
1783         if(!*p) break;
1784         if(*++p == '\n') p++; // optional linefeed after button-text terminating semicolon
1785         q = p;
1786         textOptions[i].choice = (char**) (r = textOptions[i].name + j);
1787         while(*p && (*p != ';' || p[1] != '\n')) textOptions[i].name[j++] = *p++;
1788         textOptions[i].name[j++] = 0;
1789         if(*p) p += 2;
1790         textOptions[i].max = 135;
1791         textOptions[i].min = i&1;
1792         textOptions[i].handle = NULL;
1793         textOptions[i].target = &SendText;
1794         textOptions[i].textValue = strstr(r, "$input") ? "#80FF80" : strstr(r, "$name") ? "#FF8080" : "#FFFFFF";
1795         textOptions[i].type = Button;
1796    } while(++i < 99 && *p);
1797    if(i == 0) return;
1798    textOptions[i].type = EndMark;
1799    textOptions[i].target = NULL;
1800    textOptions[i].min = 2;
1801    MarkMenu("menuView.ICStex", 3);
1802    GenericPopUp(textOptions, _("ICS text menu"), 3);
1803 }
1804
1805 static char *commentText;
1806 static int commentIndex;
1807 void ClearComment P((int n));
1808 extern char commentTranslations[];
1809
1810 void NewComCallback(int n)
1811 {
1812     ReplaceComment(commentIndex, commentText);
1813 }
1814
1815 void SaveChanges(int n)
1816 {
1817     Arg args[16];
1818     XtSetArg(args[0], XtNstring, &commentText);
1819     XtGetValues(currentOption[0].handle, args, 1);
1820     ReplaceComment(commentIndex, commentText);
1821 }
1822
1823 Option commentOptions[] = {
1824 { 0xD, 200, 300, NULL, (void*) &commentText, "", NULL, TextBox, "" },
1825 {   0,  0,    0, NULL, (void*) &ClearComment, NULL, NULL, Button, "clear" },
1826 {   0,  1,    0, NULL, (void*) &SaveChanges, NULL, NULL, Button, "save changes" },
1827 {   0,  1,    0, NULL, (void*) &NewComCallback, "", NULL, EndMark , "" }
1828 };
1829
1830 void ClearComment(int n)
1831 {
1832     XtCallActionProc(commentOptions[0].handle, "select-all", NULL, NULL, 0);
1833     XtCallActionProc(commentOptions[0].handle, "kill-selection", NULL, NULL, 0);
1834 }
1835
1836 void NewCommentPopup(char *title, char *text, int index)
1837 {
1838     Widget edit;
1839     Arg args[16];
1840
1841     if(shells[1]) { // if already exists, alter title and content
1842         XtSetArg(args[0], XtNtitle, title);
1843         XtSetValues(shells[1], args, 1);
1844         XtSetArg(args[0], XtNstring, text);
1845         XtSetValues(commentOptions[0].handle, args, 1);
1846     }
1847     commentText = text;
1848     commentIndex = index;
1849     MarkMenu("menuView.Show Comments", 1);
1850     if(GenericPopUp(commentOptions, title, 1))
1851         XtOverrideTranslations(commentOptions[0].handle, XtParseTranslationTable(commentTranslations));
1852 }
1853
1854 static char *tagsText, *msgText;
1855
1856 void NewTagsCallback(int n)
1857 {
1858     ReplaceTags(tagsText, &gameInfo);
1859 }
1860
1861 void changeTags(int n)
1862 {
1863     Arg args[16];
1864     XtSetArg(args[0], XtNstring, &tagsText);
1865     XtGetValues(currentOption[0].handle, args, 1);
1866     ReplaceTags(tagsText, &gameInfo);
1867 }
1868
1869 Option tagsOptions[] = {
1870 { 0xD, 200, 250, NULL, (void*) &tagsText, "", NULL, TextBox, "" },
1871 {   0,  0,    0, NULL, NULL, NULL, NULL, Label,  "" },
1872 {   0,  0,    0, NULL, (void*) &changeTags, NULL, NULL, Button, "save changes" },
1873 {   0,  1,    0, NULL, (void*) &NewTagsCallback, "", NULL, EndMark , "" }
1874 };
1875
1876 void NewTagsPopup(char *text, char *msg)
1877 {
1878     Widget edit;
1879     Arg args[16];
1880
1881     if(shells[2]) { // if already exists, alter title and content
1882         XtSetArg(args[0], XtNstring, text);
1883         XtSetValues(tagsOptions[0].handle, args, 1);
1884     }
1885     tagsText = text;
1886     tagsOptions[1].textValue = msg;
1887     MarkMenu("menuView.Show Tags", 2);
1888     GenericPopUp(tagsOptions, _("Tags"), 2);
1889 }
1890
1891 extern char ICSInputTranslations[];
1892 char *icsText;
1893
1894 Option boxOptions[] = {
1895 {   0, 30,  400, NULL, (void*) &icsText, "", NULL, TextBox, "" },
1896 {   0,  3,    0, NULL, NULL, "", NULL, EndMark , "" }
1897 };
1898
1899 void PutText(char *text, int pos)
1900 {
1901     Widget edit;
1902     Arg args[16];
1903     char buf[MSG_SIZ], *p;
1904
1905     if(strstr(text, "$add ") == text) {
1906         XtSetArg(args[0], XtNstring, &p);
1907         XtGetValues(boxOptions[0].handle, args, 1);
1908         snprintf(buf, MSG_SIZ, "%s%s", p, text+5); text = buf;
1909         pos += strlen(p) - 5;
1910     }
1911     XtSetArg(args[0], XtNstring, text);
1912     XtSetValues(boxOptions[0].handle, args, 1);
1913     XtSetArg(args[0], XtNinsertPosition, pos);
1914     XtSetValues(boxOptions[0].handle, args, 1);
1915 //    SetFocus(boxOptions[0].handle, shells[4], NULL, False); // No idea why this does not work, and the following is needed:
1916     XSetInputFocus(xDisplay, XtWindow(boxOptions[0].handle), RevertToPointerRoot, CurrentTime);
1917 }
1918
1919 void InputBoxPopup()
1920 {
1921     MarkMenu("menuView.ICS Input Box", 4);
1922     if(GenericPopUp(boxOptions, _("ICS input box"), 4))
1923         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(ICSInputTranslations));
1924 }
1925
1926 void TypeInProc(w, event, prms, nprms)
1927      Widget w;
1928      XEvent *event;
1929      String *prms;
1930      Cardinal *nprms;
1931 {
1932     Arg args[2];
1933     String val;
1934
1935     if(prms[0][0] == '1') {
1936         XtSetArg(args[0], XtNstring, &val);
1937         XtGetValues(boxOptions[0].handle, args, 1);
1938         TypeInDoneEvent((char*)val);
1939     }
1940     PopDown(0);\r
1941 }
1942
1943 char moveTypeInTranslations[] =
1944     "<Key>Return: TypeInProc(1) \n"
1945     "<Key>Escape: TypeInProc(0) \n";
1946
1947 void PopUpMoveDialog(char firstchar)
1948 {
1949     static char buf[2];
1950     buf[0] = firstchar; icsText = buf;
1951     if(GenericPopUp(boxOptions, _("Type a move"), 0))
1952         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(moveTypeInTranslations));
1953 }
1954
1955 void MoveTypeInProc(Widget widget, caddr_t unused, XEvent *event)
1956 {
1957     char buf[10], keys[32];
1958     KeySym sym;
1959     KeyCode metaL, metaR;
1960     int n = XLookupString(&(event->xkey), buf, 10, &sym, NULL);
1961     XQueryKeymap(xDisplay,keys);
1962     metaL = XKeysymToKeycode(xDisplay, XK_Meta_L);
1963     metaR = XKeysymToKeycode(xDisplay, XK_Meta_R);
1964     if ( n == 1 && *buf > 32 && !(keys[metaL>>3]&1<<(metaL&7)) && !(keys[metaR>>3]&1<<(metaR&7))) // printable, no alt
1965         PopUpMoveDialog(*buf);
1966
1967 }
1968
1969 void
1970 SettingsPopUp(ChessProgramState *cps)
1971 {
1972    currentCps = cps;
1973    GenericPopUp(cps->option, _("Engine Settings"), 0);
1974 }
1975
1976 void FirstSettingsProc(w, event, prms, nprms)
1977      Widget w;
1978      XEvent *event;
1979      String *prms;
1980      Cardinal *nprms;
1981 {
1982    SettingsPopUp(&first);
1983 }
1984
1985 void SecondSettingsProc(w, event, prms, nprms)
1986      Widget w;
1987      XEvent *event;
1988      String *prms;
1989      Cardinal *nprms;
1990 {
1991    if(WaitForSecond(SettingsMenuIfReady)) return;
1992    SettingsPopUp(&second);
1993 }
1994
1995 //---------------------------- Chat Windows ----------------------------------------------
1996
1997 void OutputChatMessage(int partner, char *mess)
1998 {
1999     return; // dummy
2000 }
2001