Add ICS options 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/Xaw/Dialog.h>
54 #include <X11/Xaw/Form.h>
55 #include <X11/Xaw/List.h>
56 #include <X11/Xaw/Label.h>
57 #include <X11/Xaw/SimpleMenu.h>
58 #include <X11/Xaw/SmeBSB.h>
59 #include <X11/Xaw/SmeLine.h>
60 #include <X11/Xaw/Box.h>
61 #include <X11/Xaw/Paned.h>
62 #include <X11/Xaw/MenuButton.h>
63 #include <X11/cursorfont.h>
64 #include <X11/Xaw/Text.h>
65 #include <X11/Xaw/AsciiText.h>
66 #include <X11/Xaw/Viewport.h>
67 #include <X11/Xaw/Toggle.h>
68
69 #include "common.h"
70 #include "backend.h"
71 #include "xboard.h"
72 #include "gettext.h"
73
74 #ifdef ENABLE_NLS
75 # define  _(s) gettext (s)
76 # define N_(s) gettext_noop (s)
77 #else
78 # define  _(s) (s)
79 # define N_(s)  s
80 #endif
81
82 extern void SendToProgram P((char *message, ChessProgramState *cps));
83 FILE * XsraSelFile P((Widget w, char *prompt, char *ok, char *cancel, char *failed,
84                 char *init_path, char *mode, int (*show_entry)(), char **name_return));
85
86 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
87 extern Display *xDisplay;
88 extern int squareSize;
89 extern Pixmap xMarkPixmap;
90 extern char *layoutName;
91 extern Window xBoardWindow;
92 extern Arg layoutArgs[2], formArgs[2];
93 Pixel timerForegroundPixel, timerBackgroundPixel;
94 extern int searchTime;
95 extern int lineGap;
96
97 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
98
99 static Widget previous = NULL;
100
101 void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
102 {
103     Arg args[2];
104     char *s;
105
106     if(previous) {
107         XtSetArg(args[0], XtNdisplayCaret, False);
108         XtSetValues(previous, args, 1);
109     }
110     XtSetArg(args[0], XtNstring, &s);
111     XtGetValues(w, args, 1);
112     XtSetArg(args[0], XtNdisplayCaret, True);
113     XtSetArg(args[1], XtNinsertPosition, strlen(s));
114     XtSetValues(w, args, 2);
115     XtSetKeyboardFocus((Widget) data, w);
116     previous = w;
117 }
118
119 //--------------------------- New Shuffle Game --------------------------------------------
120 extern int shuffleOpenings;
121 extern int startedFromPositionFile;
122 int shuffleUp;
123 Widget shuffleShell;
124
125 void ShufflePopDown()
126 {
127     if (!shuffleUp) return;
128     XtPopdown(shuffleShell);
129     XtDestroyWidget(shuffleShell);
130     shuffleUp = False;
131     ModeHighlight();
132 }
133
134 void ShuffleCallback(w, client_data, call_data)
135      Widget w;
136      XtPointer client_data, call_data;
137 {
138     String name;
139     Widget w2;
140     Arg args[16];
141     char buf[MSG_SIZ];
142
143     XtSetArg(args[0], XtNlabel, &name);
144     XtGetValues(w, args, 1);
145
146     if (strcmp(name, _("cancel")) == 0) {
147         ShufflePopDown();
148         return;
149     }
150     if (strcmp(name, _("off")) == 0) {
151         ShufflePopDown();
152         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
153         ResetGameEvent();
154         return;
155     }
156     if (strcmp(name, _("random")) == 0) {
157       snprintf(buf, MSG_SIZ,  "%d", rand());
158         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
159         XtSetValues(XtParent(w), args, 1);
160         return;
161     }
162     if (strcmp(name, _("ok")) == 0) {
163         int nr; String name;
164         name = XawDialogGetValueString(w2 = XtParent(w));
165         if(sscanf(name ,"%d",&nr) != 1) {
166           snprintf(buf, MSG_SIZ,  "%d", appData.defaultFrcPosition);
167             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
168             XtSetValues(w2, args, 1);
169             return;
170         }
171         appData.defaultFrcPosition = nr;
172         shuffleOpenings = True;
173         ShufflePopDown();
174         ResetGameEvent();
175         return;
176     }
177 }
178
179 void ShufflePopUp()
180 {
181     Arg args[16];
182     Widget popup, layout, dialog, edit;
183     Window root, child;
184     int x, y, i;
185     int win_x, win_y;
186     unsigned int mask;
187     char def[MSG_SIZ];
188
189     i = 0;
190     XtSetArg(args[i], XtNresizable, True); i++;
191     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
192     shuffleShell = popup =
193       XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
194                          shellWidget, args, i);
195
196     layout =
197       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
198                             layoutArgs, XtNumber(layoutArgs));
199
200     snprintf(def, MSG_SIZ,  "%d\n", appData.defaultFrcPosition);
201     i = 0;
202     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
203     XtSetArg(args[i], XtNvalue, def); i++;
204     XtSetArg(args[i], XtNborderWidth, 0); i++;
205     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
206                                    layout, args, i);
207
208 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
209 //    XtSetArg(args[1], XtNuseStringInPlace, False);
210 //    XtSetValues(dialog, args, 2);
211
212     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
213     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
214     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
215     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
216
217     XtRealizeWidget(popup);
218     CatchDeleteWindow(popup, "ShufflePopDown");
219
220     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
221                   &x, &y, &win_x, &win_y, &mask);
222
223     XtSetArg(args[0], XtNx, x - 10);
224     XtSetArg(args[1], XtNy, y - 30);
225     XtSetValues(popup, args, 2);
226
227     XtPopup(popup, XtGrabExclusive);
228     shuffleUp = True;
229
230     edit = XtNameToWidget(dialog, "*value");
231
232     XtSetKeyboardFocus(popup, edit);
233 }
234
235 void ShuffleMenuProc(w, event, prms, nprms)
236      Widget w;
237      XEvent *event;
238      String *prms;
239      Cardinal *nprms;
240 {
241 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
242 //      Reset(FALSE, TRUE);
243 //    }
244     ShufflePopUp();
245 }
246
247 //--------------------------- Time-Control Menu Popup ----------------------------------
248 int TimeControlUp;
249 Widget TimeControlShell;
250 int tcInc;
251 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
252 int tcIncrement, tcMoves;
253
254 void TimeControlPopDown()
255 {
256     if (!TimeControlUp) return;
257     previous = NULL;
258     XtPopdown(TimeControlShell);
259     XtDestroyWidget(TimeControlShell);
260     TimeControlUp = False;
261     ModeHighlight();
262 }
263
264 void TimeControlCallback(w, client_data, call_data)
265      Widget w;
266      XtPointer client_data, call_data;
267 {
268     String name, txt;
269     Arg args[16];
270     char buf[MSG_SIZ];
271     int j;
272
273     XtSetArg(args[0], XtNlabel, &name);
274     XtGetValues(w, args, 1);
275
276     if (strcmp(name, _("classical")) == 0) {
277         if(tcInc == 0) return;
278         j=0;
279         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
280         XtSetValues(tcMess1, args, j);
281         j=0;
282         XtSetArg(args[j], XtNlabel, _("moves")); j++;
283         XtSetValues(tcMess2, args, j);
284         if(tcInc == 1) {
285             j=0;
286             XtSetArg(args[j], XtNstring, &name); j++;
287             XtGetValues(tcData, args, j);
288             tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
289         }
290         snprintf(buf, MSG_SIZ,  "%d", tcMoves);
291         j=0;
292         XtSetArg(args[j], XtNstring, buf); j++;
293         XtSetValues(tcData, args, j);
294         tcInc = 0;
295         return;
296     }
297     if (strcmp(name, _("incremental")) == 0) {
298         if(tcInc == 1) return;
299         j=0;
300         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
301         XtSetValues(tcMess1, args, j);
302         j=0;
303         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
304         XtSetValues(tcMess2, args, j);
305         if(tcInc == 0) {
306             j=0;
307             XtSetArg(args[j], XtNstring, &name); j++;
308             XtGetValues(tcData, args, j);
309             tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
310         }
311         snprintf(buf, MSG_SIZ,  "%d", tcIncrement);
312         j=0;
313         XtSetArg(args[j], XtNstring, buf); j++;
314         XtSetValues(tcData, args, j);
315         tcInc = 1;
316         return;
317     }
318     if (strcmp(name, _("fixed time")) == 0) {
319         if(tcInc == 2) return;
320         j=0;
321         XtSetArg(args[j], XtNlabel, _("sec/move (max)")); j++;
322         XtSetValues(tcMess1, args, j);
323         j=0;
324         XtSetArg(args[j], XtNlabel, _("")); j++;
325         XtSetValues(tcMess2, args, j);
326         j=0;
327         XtSetArg(args[j], XtNstring, ""); j++;
328         XtSetValues(tcData, args, j);
329         tcInc = 2;
330         return;
331     }
332     if (strcmp(name, _(" OK ")) == 0) {
333         int inc, mps, ok;
334         XtSetArg(args[0], XtNstring, &txt);
335         XtGetValues(tcData, args, 1);
336         switch(tcInc) {
337           case 1:
338             ok = sscanf(txt, "%d", &inc); mps = 0;
339             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
340             ok &= (inc >= 0);
341             break;
342           case 0:
343             ok = sscanf(txt, "%d", &mps); inc = -1;
344             ok &= (mps > 0);
345             break;
346           case 2:
347             ok = 1; inc = -1; mps = 40;
348         }
349         if(ok != 1) {
350             XtSetArg(args[0], XtNstring, ""); // erase any offending input
351             XtSetValues(tcData, args, 1);
352             return;
353         }
354         XtSetArg(args[0], XtNstring, &txt);
355         XtGetValues(tcTime, args, 1);
356         if(tcInc == 2) {
357             if(sscanf(txt, "%d", &inc) != 1) {
358                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
359                 XtSetValues(tcTime, args, 1);
360                 DisplayError(_("Bad Time-Control String"), 0);
361                 return;
362             }
363             searchTime = inc;
364         } else {
365             if(!ParseTimeControl(txt, inc, mps)) {
366                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
367                 XtSetValues(tcTime, args, 1);
368                 DisplayError(_("Bad Time-Control String"), 0);
369                 return;
370             }
371             searchTime = 0;
372             appData.movesPerSession = mps;
373             appData.timeIncrement = inc;
374             appData.timeControl = strdup(txt);
375         }
376         XtSetArg(args[0], XtNstring, &txt);
377         XtGetValues(tcOdds1, args, 1);
378         appData.firstTimeOdds = first.timeOdds
379                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
380         XtGetValues(tcOdds2, args, 1);
381         appData.secondTimeOdds = second.timeOdds
382                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
383
384         Reset(True, True);
385         TimeControlPopDown();
386         return;
387     }
388 }
389
390 void TimeControlPopUp()
391 {
392     Arg args[16];
393     Widget popup, layout, form,  b_ok, b_cancel, b_clas, b_inc, mess;
394     Window root, child;
395     int x, y, i, j;
396     int win_x, win_y;
397     unsigned int mask;
398     char def[MSG_SIZ];
399
400     tcInc = searchTime > 0 ? 2 : (appData.timeIncrement >= 0);
401     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
402     if(!tcInc) tcIncrement = 0;
403     snprintf(def, MSG_SIZ,  "%d", tcInc ? tcIncrement : tcMoves);
404
405     i = 0;
406     XtSetArg(args[i], XtNresizable, True); i++;
407 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
408     TimeControlShell = popup =
409       XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
410                          shellWidget, args, i);
411
412     layout =
413       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
414                             layoutArgs, XtNumber(layoutArgs));
415
416     form =
417       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
418                             formArgs, XtNumber(formArgs));
419
420     j = 0;
421 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
422 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
423     XtSetValues(popup, args, j);
424
425     j= 0;
426     XtSetArg(args[j], XtNborderWidth, 1); j++;
427     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
428     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
429     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
430     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
431     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
432     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
433     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
434     XtSetArg(args[j], XtNright, XtChainRight);  j++;
435     XtSetArg(args[j], XtNresizable, True);  j++;
436     XtSetArg(args[j], XtNwidth,  85);  j++;
437     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
438     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
439     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
440
441     j= 0;
442     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("sec/move (max)   ") : _("   minutes, plus   ") : _("minutes for each")); j++;
443     XtSetArg(args[j], XtNborderWidth, 0); j++;
444     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
445     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
446     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
447     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
448     XtSetArg(args[j], XtNright, XtChainRight);  j++;
449   //  XtSetArg(args[j], XtNwidth,  100);  j++;
450   //  XtSetArg(args[j], XtNheight, 20);  j++;
451     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
452
453     j= 0;
454     XtSetArg(args[j], XtNborderWidth, 1); j++;
455     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
456     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
457     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
458     XtSetArg(args[j], XtNstring, def);  j++;
459     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
460     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
461     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
462     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
463     XtSetArg(args[j], XtNright, XtChainRight);  j++;
464     XtSetArg(args[j], XtNresizable, True);  j++;
465     XtSetArg(args[j], XtNwidth,  40);  j++;
466 //    XtSetArg(args[j], XtNheight, 20);  j++;
467     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
468     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
469
470     j= 0;
471     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("             ") : _("sec/move") : _("moves     ")); j++;
472     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
473     XtSetArg(args[j], XtNborderWidth, 0); j++;
474     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
475     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
476     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
477     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
478     XtSetArg(args[j], XtNright, XtChainRight);  j++;
479 //    XtSetArg(args[j], XtNwidth,  80);  j++;
480 //    XtSetArg(args[j], XtNheight, 20);  j++;
481     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
482                                    form, args, j);
483
484     j= 0;
485     XtSetArg(args[j], XtNborderWidth, 1); j++;
486     XtSetArg(args[j], XtNfromVert, tcTime); j++;
487     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
488     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
489     XtSetArg(args[j], XtNstring, "1");  j++;
490     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
491     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
492     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
493     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
494     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
495     XtSetArg(args[j], XtNresizable, True);  j++;
496     XtSetArg(args[j], XtNwidth,  40);  j++;
497 //    XtSetArg(args[j], XtNheight, 20);  j++;
498     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
499     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
500
501     j= 0;
502     XtSetArg(args[j], XtNborderWidth, 1); j++;
503     XtSetArg(args[j], XtNfromVert, tcTime); j++;
504     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
505     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
506     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
507     XtSetArg(args[j], XtNstring, "1");  j++;
508     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
509     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
510     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
511     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
512     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
513     XtSetArg(args[j], XtNresizable, True);  j++;
514     XtSetArg(args[j], XtNwidth,  40);  j++;
515 //    XtSetArg(args[j], XtNheight, 20);  j++;
516     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
517     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
518
519     j= 0;
520     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
521     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
522     XtSetArg(args[j], XtNborderWidth, 0); j++;
523     XtSetArg(args[j], XtNfromVert, tcTime); j++;
524     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
525     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
526     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
527     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
528     XtSetArg(args[j], XtNright, XtChainRight);  j++;
529 //    XtSetArg(args[j], XtNwidth,  200);  j++;
530 //    XtSetArg(args[j], XtNheight, 20);  j++;
531     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
532                                    form, args, j);
533     j=0;
534     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
535     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
536     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
537     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
538     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
539     XtSetArg(args[j], XtNstate, tcInc==0); j++;
540     b_clas= XtCreateManagedWidget(_("classical"), toggleWidgetClass,
541                                    form, args, j);
542     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
543
544     j=0;
545     XtSetArg(args[j], XtNradioGroup, b_clas); j++;
546     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
547     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
548     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
549     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
550     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
551     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
552     XtSetArg(args[j], XtNstate, tcInc==1); j++;
553     b_inc = XtCreateManagedWidget(_("incremental"), toggleWidgetClass,
554                                    form, args, j);
555     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
556
557     j=0;
558     XtSetArg(args[j], XtNradioGroup, b_inc); j++;
559     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
560     XtSetArg(args[j], XtNfromHoriz, b_inc);  j++;
561     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
562     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
563     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
564     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
565     XtSetArg(args[j], XtNstate, tcInc==2); j++;
566     b_inc = XtCreateManagedWidget(_("fixed time"), toggleWidgetClass,
567                                    form, args, j);
568     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
569
570     j=0;
571     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
572     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
573     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
574     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
575     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
576     XtSetArg(args[j], XtNright, XtChainRight);  j++;
577     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
578                                    form, args, j);
579     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
580
581     j=0;
582     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
583     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
584     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
585     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
586     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
587     XtSetArg(args[j], XtNright, XtChainRight);  j++;
588     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
589                                    form, args, j);
590     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
591
592     XtRealizeWidget(popup);
593     CatchDeleteWindow(popup, "TimeControlPopDown");
594
595     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
596                   &x, &y, &win_x, &win_y, &mask);
597
598     XtSetArg(args[0], XtNx, x - 10);
599     XtSetArg(args[1], XtNy, y - 30);
600     XtSetValues(popup, args, 2);
601
602     XtPopup(popup, XtGrabExclusive);
603     TimeControlUp = True;
604
605     previous = NULL;
606     SetFocus(tcTime, popup, (XEvent*) NULL, False);
607 //    XtSetKeyboardFocus(popup, tcTime);
608 }
609
610 void TimeControlProc(w, event, prms, nprms)
611      Widget w;
612      XEvent *event;
613      String *prms;
614      Cardinal *nprms;
615 {
616    TimeControlPopUp();
617 }
618
619 //--------------------------- Engine-Options Menu Popup ----------------------------------
620 int EngineUp;
621 Widget EngineShell;
622 extern int adjudicateLossThreshold;
623
624 Widget engDrawMoves, engThreshold, engRule, engRepeat;
625
626 void EnginePopDown()
627 {
628     if (!EngineUp) return;
629     previous = NULL;
630     XtPopdown(EngineShell);
631     XtDestroyWidget(EngineShell);
632     EngineUp = False;
633     ModeHighlight();
634 }
635
636 int ReadToggle(Widget w)
637 {
638     Arg args; Boolean res;
639
640     XtSetArg(args, XtNstate, &res);
641     XtGetValues(w, &args, 1);
642
643     return res;
644 }
645
646 Widget w1, w2, w3, w4, w5, w6, w7, w8;
647
648 void EngineCallback(w, client_data, call_data)
649      Widget w;
650      XtPointer client_data, call_data;
651 {
652     String name;
653     Arg args[16];
654     int j;
655
656     XtSetArg(args[0], XtNlabel, &name);
657     XtGetValues(w, args, 1);
658
659     if (strcmp(name, _("OK")) == 0) {
660         // read all switches
661         appData.periodicUpdates = ReadToggle(w1);
662 //      appData.hideThinkingFromHuman = ReadToggle(w2);
663         first.scoreIsAbsolute  = appData.firstScoreIsAbsolute  = ReadToggle(w3);
664         second.scoreIsAbsolute = appData.secondScoreIsAbsolute = ReadToggle(w4);
665         appData.testClaims    = ReadToggle(w5);
666         appData.checkMates    = ReadToggle(w6);
667         appData.materialDraws = ReadToggle(w7);
668         appData.trivialDraws  = ReadToggle(w8);
669
670         // adjust setting in other menu for duplicates
671         // (perhaps duplicates should be removed from general Option Menu?)
672 //      XtSetArg(args[0], XtNleftBitmap, appData.showThinking ? xMarkPixmap : None);
673 //      XtSetValues(XtNameToWidget(menuBarWidget,
674 //                                 "menuOptions.Show Thinking"), args, 1);
675
676         // read out numeric controls, simply ignore bad formats for now
677         XtSetArg(args[0], XtNstring, &name);
678         XtGetValues(engDrawMoves, args, 1);
679         if(sscanf(name, "%d", &j) == 1) appData.adjudicateDrawMoves = j;
680         XtGetValues(engThreshold, args, 1);
681         if(sscanf(name, "%d", &j) == 1)
682                 adjudicateLossThreshold = appData.adjudicateLossThreshold = -j; // inverted!
683         XtGetValues(engRule, args, 1);
684         if(sscanf(name, "%d", &j) == 1) appData.ruleMoves = j;
685         XtGetValues(engRepeat, args, 1);
686         if(sscanf(name, "%d", &j) == 1) appData.drawRepeats = j;
687
688         EnginePopDown();
689         ShowThinkingEvent(); // [HGM] thinking: score adjudication might need thinking output
690         return;
691     }
692 }
693
694 void EnginePopUp()
695 {
696     Arg args[16];
697     Widget popup, layout, form,  b_ok, b_cancel,  s1;
698     Window root, child;
699     int x, y, i, j, width;
700     int win_x, win_y;
701     unsigned int mask;
702     char def[MSG_SIZ];
703
704     tcInc = (appData.timeIncrement >= 0);
705     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
706     if(!tcInc) tcIncrement = 0;
707     snprintf(def, MSG_SIZ,  "%d", tcInc ? tcIncrement : tcMoves);
708
709     i = 0;
710     XtSetArg(args[i], XtNresizable, True); i++;
711 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
712     EngineShell = popup =
713       XtCreatePopupShell(_("Adjudications"), transientShellWidgetClass,
714                          shellWidget, args, i);
715
716     layout =
717       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
718                             layoutArgs, XtNumber(layoutArgs));
719
720     form =
721       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
722                             formArgs, XtNumber(formArgs));
723
724     j = 0;
725 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 250); j++;
726 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 400); j++;
727 //    XtSetValues(popup, args, j);
728
729     j = 0;
730 //    XtSetArg(args[j], XtNwidth,       (XtArgVal) 250); j++;
731 //    XtSetArg(args[j], XtNheight,      (XtArgVal) 20); j++;
732     XtSetArg(args[j], XtNleft,        (XtArgVal) XtChainLeft); j++;
733     XtSetArg(args[j], XtNright,       (XtArgVal) XtChainRight); j++;
734     XtSetArg(args[j], XtNstate,       appData.periodicUpdates); j++;
735 //    XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
736     w1 = XtCreateManagedWidget(_("Periodic Updates (Analysis Mode)"), toggleWidgetClass, form, args, j);
737
738     XtSetArg(args[j], XtNwidth,       (XtArgVal) &width);
739     XtGetValues(w1, &args[j], 1);
740
741 //    XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w1);
742 //    XtSetArg(args[j-3], XtNstate,       appData.hideThinkingFromHuman);
743 //    w2 = XtCreateManagedWidget(_("Hide Thinking from Human"), toggleWidgetClass, form, args, j);
744
745     XtSetArg(args[j], XtNwidth,       (XtArgVal) width); j++;
746     XtSetArg(args[j-2], XtNstate,     appData.firstScoreIsAbsolute);
747     XtSetArg(args[j], XtNfromVert,    (XtArgVal) w1); j++;
748     w3 = XtCreateManagedWidget(_("Engine #1 Score is Absolute"), toggleWidgetClass, form, args, j);
749
750     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w3);
751     XtSetArg(args[j-3], XtNstate,       appData.secondScoreIsAbsolute);
752     w4 = XtCreateManagedWidget(_("Engine #2 Score is Absolute"), toggleWidgetClass, form, args, j);
753
754     s1 = XtCreateManagedWidget(_("\nAdjudications in non-ICS games:"), labelWidgetClass, form, args, 3);
755
756     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) s1);
757     XtSetArg(args[j-3], XtNstate,       appData.testClaims);
758     w5 = XtCreateManagedWidget(_("Verify Engine Result Claims"), toggleWidgetClass, form, args, j);
759
760     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w5);
761     XtSetArg(args[j-3], XtNstate,       appData.checkMates);
762     w6 = XtCreateManagedWidget(_("Detect All Mates"), toggleWidgetClass, form, args, j);
763
764     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w6);
765     XtSetArg(args[j-3], XtNstate,       appData.materialDraws);
766     w7 = XtCreateManagedWidget(_("Draw when Insuff. Mating Material"), toggleWidgetClass, form, args, j);
767
768     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w7);
769     XtSetArg(args[j-3], XtNstate,       appData.trivialDraws);
770     w8 = XtCreateManagedWidget(_("Adjudicate Trivial Draws"), toggleWidgetClass, form, args, j);
771
772     XtSetArg(args[0], XtNfromVert,  (XtArgVal) w4);
773     XtSetArg(args[1], XtNborderWidth, (XtArgVal) 0);
774     XtSetValues(s1, args, 2);
775
776     snprintf(def, MSG_SIZ,  "%d", appData.adjudicateDrawMoves);
777     j= 0;
778     XtSetArg(args[j], XtNborderWidth, 1); j++;
779     XtSetArg(args[j], XtNfromVert, w8); j++;
780     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
781     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
782     XtSetArg(args[j], XtNstring, def);  j++;
783     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
784     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
785     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
786     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
787     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
788     XtSetArg(args[j], XtNresizable, True);  j++;
789     XtSetArg(args[j], XtNwidth,  60);  j++;
790 //    XtSetArg(args[j], XtNheight, 20);  j++;
791     engDrawMoves = XtCreateManagedWidget("Length", asciiTextWidgetClass, form, args, j);
792     XtAddEventHandler(engDrawMoves, ButtonPressMask, False, SetFocus, (XtPointer) popup);
793
794     j= 0;
795     XtSetArg(args[j], XtNlabel, _(" moves maximum, then draw")); j++;
796     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
797     XtSetArg(args[j], XtNborderWidth, 0); j++;
798     XtSetArg(args[j], XtNfromVert, w8); j++;
799     XtSetArg(args[j], XtNfromHoriz, engDrawMoves); j++;
800     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
801     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
802     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
803     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
804 //    XtSetArg(args[j], XtNwidth,  170);  j++;
805 //    XtSetArg(args[j], XtNheight, 20);  j++;
806     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
807
808     snprintf(def, MSG_SIZ,  "%d", -appData.adjudicateLossThreshold); // inverted!
809     j= 0;
810     XtSetArg(args[j], XtNborderWidth, 1); j++;
811     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
812     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
813     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
814     XtSetArg(args[j], XtNstring, def);  j++;
815     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
816     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
817     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
818     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
819     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
820     XtSetArg(args[j], XtNresizable, True);  j++;
821     XtSetArg(args[j], XtNwidth,  60);  j++;
822     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
823     engThreshold = XtCreateManagedWidget("Threshold", asciiTextWidgetClass, form, args, j);
824     XtAddEventHandler(engThreshold, ButtonPressMask, False, SetFocus, (XtPointer) popup);
825
826     j= 0;
827     XtSetArg(args[j], XtNlabel, _("-centiPawn lead is win")); j++;
828     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
829     XtSetArg(args[j], XtNborderWidth, 0); j++;
830     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
831     XtSetArg(args[j], XtNfromHoriz, engThreshold); j++;
832     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
833     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
834     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
835     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
836 //    XtSetArg(args[j], XtNwidth,  150);  j++;
837 //    XtSetArg(args[j], XtNheight, 20);  j++;
838     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
839
840     snprintf(def, MSG_SIZ,  "%d", appData.ruleMoves);
841     j= 0;
842     XtSetArg(args[j], XtNborderWidth, 1); j++;
843     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
844     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
845     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
846     XtSetArg(args[j], XtNstring, def);  j++;
847     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
848     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
849     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
850     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
851     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
852     XtSetArg(args[j], XtNresizable, True);  j++;
853     XtSetArg(args[j], XtNwidth,  30);  j++;
854 //    XtSetArg(args[j], XtNheight, 20);  j++;
855     engRule = XtCreateManagedWidget("Rule", asciiTextWidgetClass, form, args, j);
856     XtAddEventHandler(engRule, ButtonPressMask, False, SetFocus, (XtPointer) popup);
857
858     j= 0;
859     XtSetArg(args[j], XtNlabel, _("-move rule applied")); j++;
860     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
861     XtSetArg(args[j], XtNborderWidth, 0); j++;
862     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
863     XtSetArg(args[j], XtNfromHoriz, engRule); j++;
864     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
865     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
866     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
867     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
868 //    XtSetArg(args[j], XtNwidth,  130);  j++;
869 //    XtSetArg(args[j], XtNheight, 20);  j++;
870     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
871
872     snprintf(def, MSG_SIZ,  "%d", appData.drawRepeats);
873     j= 0;
874     XtSetArg(args[j], XtNborderWidth, 1); j++;
875     XtSetArg(args[j], XtNfromVert, engRule); j++;
876     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
877     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
878     XtSetArg(args[j], XtNstring, def);  j++;
879     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
880     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
881     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
882     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
883     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
884     XtSetArg(args[j], XtNresizable, True);  j++;
885     XtSetArg(args[j], XtNwidth,  30);  j++;
886 //    XtSetArg(args[j], XtNheight, 20);  j++;
887     engRepeat = XtCreateManagedWidget("Repeats", asciiTextWidgetClass, form, args, j);
888     XtAddEventHandler(engRepeat, ButtonPressMask, False, SetFocus, (XtPointer) popup);
889
890     j= 0;
891     XtSetArg(args[j], XtNlabel, _("-fold repeat is draw")); j++;
892     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
893     XtSetArg(args[j], XtNborderWidth, 0); j++;
894     XtSetArg(args[j], XtNfromVert, engRule); j++;
895     XtSetArg(args[j], XtNfromHoriz, engRepeat); j++;
896     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
897     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
898     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
899     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
900 //    XtSetArg(args[j], XtNwidth,  130);  j++;
901 //    XtSetArg(args[j], XtNheight, 20);  j++;
902     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
903
904     j=0;
905     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
906     XtSetArg(args[j], XtNfromHoriz, tcMess2);  j++;
907     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
908     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
909     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
910     XtSetArg(args[j], XtNright, XtChainRight);  j++;
911     b_ok= XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
912     XtAddCallback(b_ok, XtNcallback, EngineCallback, (XtPointer) 0);
913
914     j=0;
915     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
916     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
917     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
918     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
919     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
920     XtSetArg(args[j], XtNright, XtChainRight);  j++;
921     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
922                                    form, args, j);
923     XtAddCallback(b_cancel, XtNcallback, EnginePopDown, (XtPointer) 0);
924
925     XtRealizeWidget(popup);
926     CatchDeleteWindow(popup, "EnginePopDown");
927
928     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
929                   &x, &y, &win_x, &win_y, &mask);
930
931     XtSetArg(args[0], XtNx, x - 10);
932     XtSetArg(args[1], XtNy, y - 30);
933     XtSetValues(popup, args, 2);
934
935     XtPopup(popup, XtGrabExclusive);
936     EngineUp = True;
937
938     previous = NULL;
939     SetFocus(engThreshold, popup, (XEvent*) NULL, False);
940 }
941
942 void EngineMenuProc(w, event, prms, nprms)
943      Widget w;
944      XEvent *event;
945      String *prms;
946      Cardinal *nprms;
947 {
948    EnginePopUp();
949 }
950
951 //--------------------------- New-Variant Menu PopUp -----------------------------------
952 struct NewVarButton {
953   char   *name;
954   char *color;
955   Widget handle;
956   VariantClass variant;
957 };
958
959 struct NewVarButton buttonDesc[] = {
960     {N_("normal"),            "#FFFFFF", 0, VariantNormal},
961     {N_("FRC"),               "#FFFFFF", 0, VariantFischeRandom},
962     {N_("wild castle"),       "#FFFFFF", 0, VariantWildCastle},
963     {N_("no castle"),         "#FFFFFF", 0, VariantNoCastle},
964     {N_("knightmate"),        "#FFFFFF", 0, VariantKnightmate},
965     {N_("berolina"),          "#FFFFFF", 0, VariantBerolina},
966     {N_("cylinder"),          "#FFFFFF", 0, VariantCylinder},
967     {N_("shatranj"),          "#FFFFFF", 0, VariantShatranj},
968     {N_("makruk"),            "#FFFFFF", 0, VariantMakruk},
969     {N_("atomic"),            "#FFFFFF", 0, VariantAtomic},
970     {N_("two kings"),         "#FFFFFF", 0, VariantTwoKings},
971     {N_("3-checks"),          "#FFFFFF", 0, Variant3Check},
972     {N_("suicide"),           "#FFFFBF", 0, VariantSuicide},
973     {N_("give-away"),         "#FFFFBF", 0, VariantGiveaway},
974     {N_("losers"),            "#FFFFBF", 0, VariantLosers},
975     {N_("fairy"),             "#BFBFBF", 0, VariantFairy},
976     {N_("Seirawan"),          "#FFBFBF", 0, VariantSChess},
977     {N_("Superchess"),        "#FFBFBF", 0, VariantSuper},
978     {N_("crazyhouse"),        "#FFBFBF", 0, VariantCrazyhouse},
979     {N_("bughouse"),          "#FFBFBF", 0, VariantBughouse},
980     {N_("shogi (9x9)"),       "#BFFFFF", 0, VariantShogi},
981     {N_("xiangqi (9x10)"),    "#BFFFFF", 0, VariantXiangqi},
982     {N_("courier (12x8)"),    "#BFFFBF", 0, VariantCourier},
983     {N_("Capablanca (10x8)"), "#BFBFFF", 0, VariantCapablanca},
984 #ifdef GOTHIC
985     {N_("Gothic (10x8)"),     "#BFBFFF", 0, VariantGothic},
986 #endif
987     {N_("janus (10x8)"),      "#BFBFFF", 0, VariantJanus},
988     {N_("CRC (10x8)"),        "#BFBFFF", 0, VariantCapaRandom},
989 #ifdef FALCON
990     {N_("Falcon (10x8)"),     "#BFBFFF", 0, VariantFalcon},
991 #endif
992     {N_("Spartan"),           "#FF0000", 0, VariantSpartan},
993     {NULL,                0, 0, (VariantClass) 0}
994 };
995
996 int NewVariantUp;
997 Widget NewVariantShell;
998
999 void NewVariantPopDown()
1000 {
1001     if (!NewVariantUp) return;
1002     XtPopdown(NewVariantShell);
1003     XtDestroyWidget(NewVariantShell);
1004     NewVariantUp = False;
1005     ModeHighlight();
1006 }
1007
1008 void NewVariantCallback(w, client_data, call_data)
1009      Widget w;
1010      XtPointer client_data, call_data;
1011 {
1012     String name;
1013     Arg args[16];
1014     VariantClass v;
1015
1016     XtSetArg(args[0], XtNlabel, &name);
1017     XtGetValues(w, args, 1);
1018
1019     if (strcmp(name, _("  OK  ")) == 0) {
1020         int nr = (intptr_t) XawToggleGetCurrent(buttonDesc[0].handle) - 1;
1021         if(nr < 0) return;
1022         v = buttonDesc[nr].variant;
1023         if(!appData.noChessProgram) {
1024             char *name = VariantName(v), buf[MSG_SIZ];
1025             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
1026                 /* [HGM] in protocol 2 we check if variant is suported by engine */
1027               snprintf(buf, MSG_SIZ,  _("Variant %s not supported by %s"), name, first.tidy);
1028                 DisplayError(buf, 0);
1029 //              NewVariantPopDown();
1030                 return; /* ignore OK if first engine does not support it */
1031             } else
1032             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
1033               snprintf(buf, MSG_SIZ,  _("Warning: second engine (%s) does not support this!"), second.tidy);
1034                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
1035             }
1036         }
1037
1038         gameInfo.variant = v;
1039         appData.variant = VariantName(v);
1040
1041         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
1042         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
1043         appData.pieceToCharTable = NULL;
1044         Reset(True, True);
1045         NewVariantPopDown();
1046         return;
1047     }
1048 }
1049
1050 void NewVariantPopUp()
1051 {
1052     Arg args[16];
1053     Widget popup, layout, form, last = NULL, b_ok, b_cancel;
1054     Window root, child;
1055     int x, y, i, j;
1056     int win_x, win_y;
1057     unsigned int mask;
1058     XrmValue vFrom, vTo;
1059
1060     i = 0;
1061     XtSetArg(args[i], XtNresizable, True); i++;
1062 //    XtSetArg(args[i], XtNwidth, 250); i++;
1063 //    XtSetArg(args[i], XtNheight, 300); i++;
1064     NewVariantShell = popup =
1065       XtCreatePopupShell(_("NewVariant Menu"), transientShellWidgetClass,
1066                          shellWidget, args, i);
1067
1068     layout =
1069       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1070                             layoutArgs, XtNumber(layoutArgs));
1071
1072     form =
1073       XtCreateManagedWidget("form", formWidgetClass, layout,
1074                             formArgs, XtNumber(formArgs));
1075
1076     for(i = 0; buttonDesc[i].name != NULL; i++) {
1077         Pixel buttonColor;
1078         if (!appData.monoMode) {
1079             vFrom.addr = (caddr_t) buttonDesc[i].color;
1080             vFrom.size = strlen(buttonDesc[i].color);
1081             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1082             if (vTo.addr == NULL) {
1083                 buttonColor = (Pixel) -1;
1084             } else {
1085                 buttonColor = *(Pixel *) vTo.addr;
1086             }
1087         }
1088
1089         j = 0;
1090         XtSetArg(args[j], XtNradioGroup, last); j++;
1091         XtSetArg(args[j], XtNwidth, 125); j++;
1092 //      XtSetArg(args[j], XtNheight, 16); j++;
1093         XtSetArg(args[j], XtNfromVert, i == 15 ? NULL : last); j++;
1094         XtSetArg(args[j], XtNfromHoriz, i < 15 ? NULL : buttonDesc[i-15].handle); j++;
1095         XtSetArg(args[j], XtNradioData, i+1); j++;
1096         XtSetArg(args[j], XtNbackground, buttonColor); j++;
1097         XtSetArg(args[j], XtNstate, gameInfo.variant == buttonDesc[i].variant); j++;
1098         XtSetArg(args[j], XtNsensitive, appData.noChessProgram || strstr(first.variants, VariantName(buttonDesc[i].variant))); j++;
1099         buttonDesc[i].handle = last =
1100             XtCreateManagedWidget(buttonDesc[i].name, toggleWidgetClass, form, args, j);
1101     }
1102
1103     j=0;
1104     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1105     XtSetArg(args[j], XtNfromHoriz, buttonDesc[12].handle);  j++;
1106     XtSetArg(args[j], XtNheight, 35); j++;
1107 //    XtSetArg(args[j], XtNwidth, 60); j++;
1108     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1109     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1110     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1111     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1112     b_cancel= XtCreateManagedWidget(_("CANCEL"), commandWidgetClass, form, args, j);
1113     XtAddCallback(b_cancel, XtNcallback, NewVariantPopDown, (XtPointer) 0);
1114
1115     j=0;
1116     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
1117     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1118     XtSetArg(args[j], XtNheight, 35); j++;
1119 //    XtSetArg(args[j], XtNwidth, 60); j++;
1120     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1121     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1122     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1123     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1124     b_ok= XtCreateManagedWidget(_("  OK  "), commandWidgetClass, form, args, j);
1125     XtAddCallback(b_ok, XtNcallback, NewVariantCallback, (XtPointer) 0);
1126
1127     j=0;
1128     XtSetArg(args[j], XtNfromVert, buttonDesc[14].handle);  j++;
1129 //    XtSetArg(args[j], XtNheight, 70); j++;
1130     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1131     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1132     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1133     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1134     XtSetArg(args[j], XtNlabel, _("WARNING: variants with un-orthodox\n"
1135                                   "pieces only have built-in bitmaps\n"
1136                                   "for -boardSize middling, bulky and\n"
1137                                   "petite, and substitute king or amazon\n"
1138                                   "for missing bitmaps. (See manual.)")); j++;
1139     XtCreateManagedWidget("warning", labelWidgetClass, form, args, j);
1140
1141             XtRealizeWidget(popup);
1142     CatchDeleteWindow(popup, "NewVariantPopDown");
1143
1144     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1145                   &x, &y, &win_x, &win_y, &mask);
1146
1147     XtSetArg(args[0], XtNx, x - 10);
1148     XtSetArg(args[1], XtNy, y - 30);
1149     XtSetValues(popup, args, 2);
1150
1151     XtPopup(popup, XtGrabExclusive);
1152     NewVariantUp = True;
1153 }
1154
1155 void NewVariantProc(w, event, prms, nprms)
1156      Widget w;
1157      XEvent *event;
1158      String *prms;
1159      Cardinal *nprms;
1160 {
1161    NewVariantPopUp();
1162 }
1163
1164 //--------------------------- UCI Menu Popup ------------------------------------------
1165 int UciUp;
1166 Widget UciShell;
1167
1168 struct UciControl {
1169   char *name;
1170   Widget handle;
1171   void *ptr;
1172 };
1173
1174 struct UciControl controlDesc[] = {
1175   {N_("maximum nr of CPUs:"), 0, &appData.smpCores},
1176   {N_("Polyglot Directory:"), 0, &appData.polyglotDir},
1177   {N_("Hash Size (MB):"),     0, &appData.defaultHashSize},
1178   {N_("EGTB Path:"),          0, &appData.defaultPathEGTB},
1179   {N_("EGTB Cache (MB):"),    0, &appData.defaultCacheSizeEGTB},
1180   {N_("Polyglot Book:"),      0, &appData.polyglotBook},
1181   {NULL, 0, NULL},
1182 };
1183
1184 void UciPopDown()
1185 {
1186     if (!UciUp) return;
1187     previous = NULL;
1188     XtPopdown(UciShell);
1189     XtDestroyWidget(UciShell);
1190     UciUp = False;
1191     ModeHighlight();
1192 }
1193
1194 void UciCallback(w, client_data, call_data)
1195      Widget w;
1196      XtPointer client_data, call_data;
1197 {
1198     String name;
1199     Arg args[16];
1200     int oldCores = appData.smpCores, ponder = 0;
1201
1202     XtSetArg(args[0], XtNlabel, &name);
1203     XtGetValues(w, args, 1);
1204
1205     if (strcmp(name, _("OK")) == 0) {
1206         int i, j; String name;
1207         for(i=0; i<6; i++) {
1208             XtSetArg(args[0], XtNstring, &name);
1209             XtGetValues(controlDesc[i].handle, args, 1);
1210             if(i&1) {
1211                 if(name)
1212                     *(char**) controlDesc[i].ptr = strdup(name);
1213             } else {
1214                 if(sscanf(name, "%d", &j) == 1)
1215                     *(int*) controlDesc[i].ptr = j;
1216             }
1217         }
1218         XtSetArg(args[0], XtNstate, &appData.usePolyglotBook);
1219         XtGetValues(w1, args, 1);
1220         XtSetArg(args[0], XtNstate, &appData.firstHasOwnBookUCI);
1221         XtGetValues(w2, args, 1);
1222         XtSetArg(args[0], XtNstate, &appData.secondHasOwnBookUCI);
1223         XtGetValues(w3, args, 1);
1224         XtSetArg(args[0], XtNstate, &ponder);
1225         XtGetValues(w4, args, 1);
1226
1227         // adjust setting in other menu for duplicates
1228         // (perhaps duplicates should be removed from general Option Menu?)
1229         XtSetArg(args[0], XtNleftBitmap, ponder ? xMarkPixmap : None);
1230         XtSetValues(XtNameToWidget(menuBarWidget,
1231                                    "menuOptions.Ponder Next Move"), args, 1);
1232
1233         // make sure changes are sent to first engine by re-initializing it
1234         // if it was already started pre-emptively at end of previous game
1235         if(gameMode == BeginningOfGame) Reset(True, True); else {
1236             // Some changed setting need immediate sending always.
1237             PonderNextMoveEvent(ponder);
1238             if(oldCores != appData.smpCores)
1239                 NewSettingEvent(False, &(first.maxCores), "cores", appData.smpCores);
1240       }
1241       UciPopDown();
1242       return;
1243     }
1244 }
1245
1246 void UciPopUp()
1247 {
1248     Arg args[16];
1249     Widget popup, layout, form, b_ok, b_cancel, last = NULL, new, upperLeft;
1250     Window root, child;
1251     int x, y, i, j;
1252     int win_x, win_y;
1253     unsigned int mask;
1254     char def[MSG_SIZ];
1255
1256     i = 0;
1257     XtSetArg(args[i], XtNresizable, True); i++;
1258 //    XtSetArg(args[i], XtNwidth, 300); i++;
1259     UciShell = popup =
1260       XtCreatePopupShell(_("Engine Settings"), transientShellWidgetClass,
1261                          shellWidget, args, i);
1262
1263     layout =
1264       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1265                             layoutArgs, XtNumber(layoutArgs));
1266
1267
1268     form =
1269       XtCreateManagedWidget("form", formWidgetClass, layout,
1270                             formArgs, XtNumber(formArgs));
1271
1272     j = 0;
1273     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1274     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1275     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1276 //    XtSetArg(args[j], XtNheight, 20); j++;
1277     for(i = 0; controlDesc[i].name != NULL; i++) {
1278         j = 3;
1279         XtSetArg(args[j], XtNfromVert, last); j++;
1280 //      XtSetArg(args[j], XtNwidth, 130); j++;
1281         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1282         XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1283         XtSetArg(args[j], XtNborderWidth, 0); j++;
1284         new = XtCreateManagedWidget(controlDesc[i].name, labelWidgetClass, form, args, j);
1285         if(i==0) upperLeft = new;
1286
1287         j = 4;
1288         XtSetArg(args[j], XtNborderWidth, 1); j++;
1289         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1290         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1291         XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1292         XtSetArg(args[j], XtNright, XtChainRight);  j++;
1293         XtSetArg(args[j], XtNresizable, True);  j++;
1294         XtSetArg(args[j], XtNwidth, i&1 ? 245 : 50); j++;
1295         XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1296         if(i&1) {
1297             XtSetArg(args[j], XtNstring, * (char**) controlDesc[i].ptr ?
1298                                          * (char**) controlDesc[i].ptr : ""); j++;
1299         } else {
1300           snprintf(def, MSG_SIZ,  "%d", * (int*) controlDesc[i].ptr);
1301             XtSetArg(args[j], XtNstring, def); j++;
1302         }
1303         XtSetArg(args[j], XtNfromHoriz, upperLeft); j++;
1304         controlDesc[i].handle = last =
1305             XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
1306         XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1307     }
1308
1309     j=0;
1310     XtSetArg(args[j], XtNfromHoriz, controlDesc[0].handle);  j++;
1311     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1312     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1313     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1314     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1315     XtSetArg(args[j], XtNstate, appData.ponderNextMove);  j++;
1316     w4 = XtCreateManagedWidget(_("Ponder"), toggleWidgetClass, form, args, j);
1317
1318     j=0;
1319     XtSetArg(args[j], XtNfromVert, last);  j++;
1320     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1321     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1322     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1323     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1324     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1325     XtAddCallback(b_ok, XtNcallback, UciCallback, (XtPointer) 0);
1326
1327     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
1328     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1329     XtAddCallback(b_cancel, XtNcallback, UciPopDown, (XtPointer) 0);
1330
1331     j = 5;
1332     XtSetArg(args[j], XtNfromHoriz, upperLeft);  j++;
1333     XtSetArg(args[j], XtNstate, appData.usePolyglotBook);  j++;
1334     w1 = XtCreateManagedWidget(_(" use book "), toggleWidgetClass, form, args, j);
1335 //    XtAddCallback(w1, XtNcallback, UciCallback, (XtPointer) 0);
1336
1337     j = 5;
1338     XtSetArg(args[j], XtNfromHoriz, w1);  j++;
1339     XtSetArg(args[j], XtNstate, appData.firstHasOwnBookUCI);  j++;
1340     w2 = XtCreateManagedWidget(_("own book 1"), toggleWidgetClass, form, args, j);
1341 //    XtAddCallback(w2, XtNcallback, UciCallback, (XtPointer) 0);
1342
1343     j = 5;
1344     XtSetArg(args[j], XtNfromHoriz, w2);  j++;
1345     XtSetArg(args[j], XtNstate, appData.secondHasOwnBookUCI);  j++;
1346     w3 = XtCreateManagedWidget(_("own book 2"), toggleWidgetClass, form, args, j);
1347 //    XtAddCallback(w3, XtNcallback, UciCallback, (XtPointer) 0);
1348
1349     XtRealizeWidget(popup);
1350     CatchDeleteWindow(popup, "UciPopDown");
1351
1352     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1353                   &x, &y, &win_x, &win_y, &mask);
1354
1355     XtSetArg(args[0], XtNx, x - 10);
1356     XtSetArg(args[1], XtNy, y - 30);
1357     XtSetValues(popup, args, 2);
1358
1359     XtPopup(popup, XtGrabExclusive);
1360     UciUp = True;
1361
1362     previous = NULL;
1363     SetFocus(controlDesc[2].handle, popup, (XEvent*) NULL, False);
1364 //    XtSetKeyboardFocus(popup, controlDesc[1].handle);
1365 }
1366
1367 void UciMenuProc(w, event, prms, nprms)
1368      Widget w;
1369      XEvent *event;
1370      String *prms;
1371      Cardinal *nprms;
1372 {
1373    UciPopUp();
1374 }
1375
1376 //--------------------------- Engine-specific options menu ----------------------------------
1377
1378 int SettingsUp;
1379 Widget SettingsShell;
1380 int values[MAX_OPTIONS];
1381 ChessProgramState *currentCps;
1382
1383 void SettingsPopDown()
1384 {
1385     if (!SettingsUp) return;
1386     previous = NULL;
1387     XtPopdown(SettingsShell);
1388     XtDestroyWidget(SettingsShell);
1389     SettingsUp = False;
1390     ModeHighlight();
1391 }
1392
1393 void SpinCallback(w, client_data, call_data)
1394      Widget w;
1395      XtPointer client_data, call_data;
1396 {
1397     String name, val;
1398     Arg args[16];
1399     char buf[MSG_SIZ], *p;
1400     int j;
1401     int data = (intptr_t) client_data;
1402
1403     XtSetArg(args[0], XtNlabel, &name);
1404     XtGetValues(w, args, 1);
1405
1406     j = 0;
1407     XtSetArg(args[0], XtNstring, &val);
1408     XtGetValues(currentCps->option[data].handle, args, 1);
1409     sscanf(val, "%d", &j);
1410     if (strcmp(name, "browse") == 0) {
1411         if(XsraSelFile(SettingsShell, currentCps->option[data].name, NULL, NULL, "", "", 
1412                                   currentCps->option[data].type == PathName ? "p" : "f", NULL, &p)) {
1413                 int len = strlen(p);
1414                 if(len && p[len-1] == '/') p[len-1] = NULLCHAR;
1415                 XtSetArg(args[0], XtNstring, p);
1416                 XtSetValues(currentCps->option[data].handle, args, 1);
1417         }
1418         SetFocus(currentCps->option[data].handle, SettingsShell, (XEvent*) NULL, False);
1419         return;
1420     } else
1421     if (strcmp(name, "+") == 0) {
1422         if(++j > currentCps->option[data].max) return;
1423     } else
1424     if (strcmp(name, "-") == 0) {
1425         if(--j < currentCps->option[data].min) return;
1426     } else return;
1427     snprintf(buf, MSG_SIZ,  "%d", j);
1428     XtSetArg(args[0], XtNstring, buf);
1429     XtSetValues(currentCps->option[data].handle, args, 1);
1430 }
1431
1432 void SettingsCallback(w, client_data, call_data)
1433      Widget w;
1434      XtPointer client_data, call_data;
1435 {
1436     String name, val;
1437     Arg args[16];
1438     char buf[MSG_SIZ];
1439     int i, j;
1440     int data = (intptr_t) client_data;
1441
1442     XtSetArg(args[0], XtNlabel, &name);
1443     XtGetValues(w, args, 1);
1444
1445     if (strcmp(name, _("cancel")) == 0) {
1446         SettingsPopDown();
1447         return;
1448     }
1449     if (strcmp(name, _("OK")) == 0 || data) { // save buttons imply OK
1450         for(i=0; i<currentCps->nrOptions; i++) { // send all options that had to be OK-ed to engine
1451             switch(currentCps->option[i].type) {
1452                 case TextBox:
1453                     XtSetArg(args[0], XtNstring, &val);
1454                     XtGetValues(currentCps->option[i].handle, args, 1);
1455                     if(strcmp(currentCps->option[i].textValue, val)) {
1456                       safeStrCpy(currentCps->option[i].textValue, val, MSG_SIZ - (currentCps->option[i].textValue - currentCps->option[i].name) );
1457                       snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentCps->option[i].name, val);
1458                       SendToProgram(buf, currentCps);
1459                     }
1460                     break;
1461                 case Spin:
1462                     XtSetArg(args[0], XtNstring, &val);
1463                     XtGetValues(currentCps->option[i].handle, args, 1);
1464                     sscanf(val, "%d", &j);
1465                     if(j > currentCps->option[i].max) j = currentCps->option[i].max;
1466                     if(j < currentCps->option[i].min) j = currentCps->option[i].min;
1467                     if(currentCps->option[i].value != j) {
1468                         currentCps->option[i].value = j;
1469                         snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentCps->option[i].name, j);
1470                         SendToProgram(buf, currentCps);
1471                     }
1472                     break;
1473                 case CheckBox:
1474                     j = 0;
1475                     XtSetArg(args[0], XtNstate, &j);
1476                     XtGetValues(currentCps->option[i].handle, args, 1);
1477                     if(currentCps->option[i].value != j) {
1478                         currentCps->option[i].value = j;
1479                         snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentCps->option[i].name, j);
1480                         SendToProgram(buf, currentCps);
1481                     }
1482                     break;
1483                 case ComboBox:
1484                     if(currentCps->option[i].value != values[i]) {
1485                         currentCps->option[i].value = values[i];
1486                         snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentCps->option[i].name,
1487                                 ((char**)currentCps->option[i].textValue)[values[i]]);
1488                         SendToProgram(buf, currentCps);
1489                     }
1490                     break;
1491             default:
1492               if( appData.debugMode )
1493                 fprintf(debugFP, "SettingsPopUp: unexpected case in switch.\n");
1494               break;
1495             }
1496         }
1497         if(data) { // send save-button command to engine
1498           snprintf(buf, MSG_SIZ,  "option %s\n", name);
1499           SendToProgram(buf, currentCps);
1500         }
1501         SettingsPopDown();
1502         return;
1503     }
1504     snprintf(buf, MSG_SIZ,  "option %s\n", name);
1505     SendToProgram(buf, currentCps);
1506 }
1507
1508 void ComboSelect(w, addr, index) // callback for all combo items
1509      Widget w;
1510      caddr_t addr;
1511      caddr_t index;
1512 {
1513     Arg args[16];
1514     int i = ((intptr_t)addr)>>8;
1515     int j = 255 & (intptr_t) addr;
1516
1517     values[i] = j; // store in temporary, for transfer at OK
1518     XtSetArg(args[0], XtNlabel, ((char**)currentCps->option[i].textValue)[j]);
1519     XtSetValues(currentCps->option[i].handle, args, 1);
1520 }
1521
1522 void CreateComboPopup(parent, name, n, mb)
1523      Widget parent;
1524      String name;
1525      int n;
1526      char *mb[];
1527 {
1528     int i=0, j;
1529     Widget menu, entry;
1530     Arg args[16];
1531
1532     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
1533                               parent, NULL, 0);
1534     j = 0;
1535     XtSetArg(args[j], XtNwidth, 100);  j++;
1536 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
1537     while (mb[i] != NULL) {
1538             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
1539                                           menu, args, j);
1540             XtAddCallback(entry, XtNcallback,
1541                           (XtCallbackProc) ComboSelect,
1542                           (caddr_t)(intptr_t) (256*n+i));
1543         i++;
1544     }
1545 }
1546
1547 void
1548 SettingsPopUp(ChessProgramState *cps)
1549 {
1550     Arg args[16];
1551     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
1552     Window root, child;
1553     int x, y, i, j, height, width, h, c;
1554     int win_x, win_y, maxWidth, maxTextWidth;
1555     unsigned int mask;
1556     char def[MSG_SIZ];
1557     static char pane[6] = "paneX";
1558     Widget texts[100], forelast = NULL, anchor, widest;
1559
1560     // to do: start up second engine if needed
1561     if(!cps->initDone || !cps->nrOptions) return; // nothing to be done
1562     currentCps = cps;
1563
1564     if(cps->nrOptions > 50) width = 4; else if(cps->nrOptions>24) width = 2; else width = 1;
1565     height = cps->nrOptions / width + 1;
1566      i = 0;
1567     XtSetArg(args[i], XtNresizable, True); i++;
1568     SettingsShell = popup =
1569       XtCreatePopupShell(_("Settings Menu"), transientShellWidgetClass,
1570                          shellWidget, args, i);
1571
1572     layout =
1573       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1574                             layoutArgs, XtNumber(layoutArgs));
1575   for(c=0; c<width; c++) {
1576     pane[4] = 'A'+c;
1577     form =
1578       XtCreateManagedWidget(pane, formWidgetClass, layout,
1579                             formArgs, XtNumber(formArgs));
1580     j=0;
1581     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1582     XtSetValues(form, args, j);
1583     leftMargin = form;
1584
1585     last = widest = NULL; anchor = forelast;
1586     for(h=0; h<height; h++) {
1587         forelast = last;
1588         i = h + c*height;
1589         if(i >= cps->nrOptions) break;
1590         switch(cps->option[i].type) {
1591           case Spin:
1592             snprintf(def, MSG_SIZ,  "%d", cps->option[i].value);
1593           case TextBox:
1594             j=0;
1595             XtSetArg(args[j], XtNfromVert, last);  j++;
1596             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1597             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1598             texts[h] =
1599             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1600             j=0;
1601             XtSetArg(args[j], XtNfromVert, last);  j++;
1602             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1603             XtSetArg(args[j], XtNborderWidth, 1); j++;
1604             XtSetArg(args[j], XtNwidth, cps->option[i].type == Spin ? 40 : 175); j++;
1605             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1606             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1607             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1608             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1609             XtSetArg(args[j], XtNresizable, True);  j++;
1610             XtSetArg(args[j], XtNstring, cps->option[i].type==Spin ? def : cps->option[i].textValue);  j++;
1611             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1612             edit = last;
1613             cps->option[i].handle = (void*)
1614                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1615             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1616             if(cps->option[i].type == TextBox) break;
1617
1618             // add increment and decrement controls for spin
1619             j=0;
1620             XtSetArg(args[j], XtNfromVert, edit);  j++;
1621             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1622             XtSetArg(args[j], XtNheight, 10);  j++;
1623             XtSetArg(args[j], XtNwidth, 20);  j++;
1624             edit = XtCreateManagedWidget("+", commandWidgetClass, form, args, j);
1625             XtAddCallback(edit, XtNcallback, SpinCallback,
1626                           (XtPointer)(intptr_t) i);
1627
1628             j=0;
1629             XtSetArg(args[j], XtNfromVert, edit);  j++;
1630             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1631             XtSetArg(args[j], XtNheight, 10);  j++;
1632             XtSetArg(args[j], XtNwidth, 20);  j++;
1633             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1634             XtAddCallback(last, XtNcallback, SpinCallback,
1635                           (XtPointer)(intptr_t) i);
1636             break;
1637           case CheckBox:
1638             j=0;
1639             XtSetArg(args[j], XtNfromVert, last);  j++;
1640             XtSetArg(args[j], XtNwidth, 10);  j++;
1641             XtSetArg(args[j], XtNheight, 10);  j++;
1642             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1643             cps->option[i].handle = (void*)
1644                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1645             j=0;
1646             XtSetArg(args[j], XtNfromVert, last);  j++;
1647             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1648             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1649             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1650             last = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1651             break;
1652           case SaveButton:
1653           case Button:
1654             j=0;
1655             XtSetArg(args[j], XtNfromVert, last);  j++;
1656             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1657             cps->option[i].handle = (void*)
1658                 (dialog = last = XtCreateManagedWidget(cps->option[i].name, commandWidgetClass, form, args, j));
1659             XtAddCallback(last, XtNcallback, SettingsCallback,
1660                           (XtPointer)(intptr_t) (cps->option[i].type == SaveButton));
1661             break;
1662           case ComboBox:
1663             j=0;
1664             XtSetArg(args[j], XtNfromVert, last);  j++;
1665             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1666             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1667             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1668
1669             j=0;
1670             XtSetArg(args[j], XtNfromVert, last);  j++;
1671             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1672             XtSetArg(args[j], XtNwidth, 100);  j++;
1673             XtSetArg(args[j], XtNmenuName, XtNewString(cps->option[i].name));  j++;
1674             XtSetArg(args[j], XtNlabel, ((char**)cps->option[i].textValue)[cps->option[i].value]);  j++;
1675             cps->option[i].handle = (void*)
1676                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1677             CreateComboPopup(last, cps->option[i].name, i, (char **) cps->option[i].textValue);
1678             values[i] = cps->option[i].value;
1679             break;
1680         default:
1681           if( appData.debugMode )
1682             fprintf(debugFP, "SettingsPopUp: unexpected case in switch.\n");
1683           break;
1684         }
1685     }
1686
1687     // make an attempt to align all spins and textbox controls
1688     maxWidth = maxTextWidth = 0;
1689     for(h=0; h<height; h++) {
1690         i = h + c*height;
1691         if(i >= cps->nrOptions) break;
1692         if(cps->option[i].type == Spin || cps->option[i].type == TextBox) {
1693             Dimension w;
1694             j=0;
1695             XtSetArg(args[j], XtNwidth, &w);  j++;
1696             XtGetValues(texts[h], args, j);
1697             if(cps->option[i].type == Spin) {
1698                 if(w > maxWidth) maxWidth = w;
1699                 widest = texts[h];
1700             } else {
1701                 if(w > maxTextWidth) maxTextWidth = w;
1702                 if(!widest) widest = texts[h];
1703             }
1704         }
1705     }
1706     if(maxTextWidth + 110 < maxWidth)
1707          maxTextWidth = maxWidth - 110;
1708     else maxWidth = maxTextWidth + 110;
1709     for(h=0; h<height; h++) {
1710         i = h + c*height;
1711         if(i >= cps->nrOptions) break;
1712         j=0;
1713         if(cps->option[i].type == Spin) {
1714             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1715             XtSetValues(texts[h], args, j);
1716         } else
1717         if(cps->option[i].type == TextBox) {
1718             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1719             XtSetValues(texts[h], args, j);
1720         }
1721     }
1722   }
1723     j=0;
1724     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1725     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1726     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1727     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1728     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1729     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1730     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1731     XtAddCallback(b_ok, XtNcallback, SettingsCallback, (XtPointer) 0);
1732
1733     XtSetArg(args[j-1], XtNfromHoriz, b_ok);
1734     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1735     XtAddCallback(b_cancel, XtNcallback, SettingsPopDown, (XtPointer) 0);
1736
1737     XtRealizeWidget(popup);
1738     CatchDeleteWindow(popup, "SettingsPopDown");
1739
1740     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1741                   &x, &y, &win_x, &win_y, &mask);
1742
1743     XtSetArg(args[0], XtNx, x - 10);
1744     XtSetArg(args[1], XtNy, y - 30);
1745     XtSetValues(popup, args, 2);
1746
1747     XtPopup(popup, XtGrabExclusive);
1748     SettingsUp = True;
1749
1750     previous = NULL;
1751     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1752 }
1753
1754 void FirstSettingsProc(w, event, prms, nprms)
1755      Widget w;
1756      XEvent *event;
1757      String *prms;
1758      Cardinal *nprms;
1759 {
1760    SettingsPopUp(&first);
1761 }
1762
1763 void SecondSettingsProc(w, event, prms, nprms)
1764      Widget w;
1765      XEvent *event;
1766      String *prms;
1767      Cardinal *nprms;
1768 {
1769    if(WaitForSecond(SettingsMenuIfReady)) return;
1770    SettingsPopUp(&second);
1771 }
1772
1773 //----------------------------Generic dialog --------------------------------------------
1774
1775 // cloned from Engine Settings dialog
1776
1777 typedef void ButtonCallback(int n);
1778
1779 static Option *currentOption;
1780 int MakeColors P((void));
1781 void CreateGCs P((int redo));
1782 void CreateXPMBoard P((char *s, int kind));
1783 void CreateXPMPieces P((void));
1784 void GenericReadout();
1785
1786 void IcsOptionsOK(int n)
1787 {
1788     ParseIcsTextColors();
1789 }
1790
1791 Option icsOptions[] = {
1792 { 0, 0, 0, NULL, (void*) &appData.autoKibitz, "",  NULL, CheckBox, _("Auto-Kibitz") },
1793 { 0, 0, 0, NULL, (void*) &appData.autoComment, "", NULL, CheckBox, _("Auto-Comment") },
1794 { 0, 0, 0, NULL, (void*) &appData.autoObserve, "", NULL, CheckBox, _("Auto-Observe") },
1795 { 0, 0, 0, NULL, (void*) &appData.autoRaiseBoard, "", NULL, CheckBox, _("Auto-Raise Board") },
1796 { 0, 0, 0, NULL, (void*) &appData.bgObserve, "",   NULL, CheckBox, _("Background Observe while Playing") },
1797 { 0, 0, 0, NULL, (void*) &appData.dualBoard, "",   NULL, CheckBox, _("Dual Board for Background-Observed Game") },
1798 { 0, 0, 0, NULL, (void*) &appData.getMoveList, "", NULL, CheckBox, _("Get Move List") },
1799 { 0, 0, 0, NULL, (void*) &appData.quietPlay, "",   NULL, CheckBox, _("Quiet Play") },
1800 { 0, 0, 0, NULL, (void*) &appData.seekGraph, "",   NULL, CheckBox, _("Seek Graph") },
1801 { 0, 0, 0, NULL, (void*) &appData.autoRefresh, "", NULL, CheckBox, _("Auto-Refresh Seek Graph") },
1802 { 0, 0, 0, NULL, (void*) &appData.premove, "",     NULL, CheckBox, _("Premove") },
1803 { 0, 0, 0, NULL, (void*) &appData.premoveWhite, "", NULL, CheckBox, _("Premove for White") },
1804 { 0, 0, 0, NULL, (void*) &appData.premoveWhiteText, "", NULL, TextBox, _("First White Move:") },
1805 { 0, 0, 0, NULL, (void*) &appData.premoveBlack, "", NULL, CheckBox, _("Premove for Black") },
1806 { 0, 0, 0, NULL, (void*) &appData.premoveBlackText, "", NULL, TextBox, _("First Black Move:") },
1807 { 0, 0, 0, NULL, NULL, NULL, NULL, Break, "" },
1808 { 0, 0, 0, NULL, (void*) &appData.icsAlarm, "", NULL, CheckBox, _("Alarm") },
1809 { 0, 0, 100000000, NULL, (void*) &appData.icsAlarmTime, "", NULL, Spin, _("Alarm Time (msec):") },
1810 //{ 0, 0, 0, NULL, (void*) &appData.chatBoxes, "", NULL, TextBox, _("Startup Chat Boxes:") },
1811 { 0, 0, 0, NULL, (void*) &appData.colorize, "", NULL, CheckBox, _("Colorize Messages") },
1812 { 0, 0, 0, NULL, (void*) &appData.colorShout, "", NULL, TextBox, _("Shout Text Colors:") },
1813 { 0, 0, 0, NULL, (void*) &appData.colorSShout, "", NULL, TextBox, _("S-Shout Text Colors:") },
1814 { 0, 0, 0, NULL, (void*) &appData.colorChannel1, "", NULL, TextBox, _("Channel #1 Text Colors:") },
1815 { 0, 0, 0, NULL, (void*) &appData.colorChannel, "", NULL, TextBox, _("Other Channel Text Colors:") },
1816 { 0, 0, 0, NULL, (void*) &appData.colorKibitz, "", NULL, TextBox, _("Kibitz Text Colors:") },
1817 { 0, 0, 0, NULL, (void*) &appData.colorTell, "", NULL, TextBox, _("Tell Text Colors:") },
1818 { 0, 0, 0, NULL, (void*) &appData.colorChallenge, "", NULL, TextBox, _("Challenge Text Colors:") },
1819 { 0, 0, 0, NULL, (void*) &appData.colorRequest, "", NULL, TextBox, _("Request Text Colors:") },
1820 { 0, 0, 0, NULL, (void*) &appData.colorSeek, "", NULL, TextBox, _("Seek Text Colors:") },
1821 { 0, 0, 0, NULL, (void*) &IcsOptionsOK, "", NULL, EndMark , "" }
1822 };
1823
1824 Option loadOptions[] = {
1825 { 0, 0, 0, NULL, (void*) &appData.autoDisplayTags, "", NULL, CheckBox, _("Auto-Display Tags") },
1826 { 0, 0, 0, NULL, (void*) &appData.autoDisplayComment, "", NULL, CheckBox, _("Auto-Display Comment") },
1827 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Auto-Play speed of loaded games\n(0 = instant, -1 = off):") },
1828 { 0, -1, 10000000, NULL, (void*) &appData.timeDelay, "", NULL, Fractional, _("Seconds per Move:") },
1829 { 0,  0, 0, NULL, NULL, "", NULL, EndMark , "" }
1830 };
1831
1832 Option saveOptions[] = {
1833 { 0, 0, 0, NULL, (void*) &appData.autoSaveGames, "", NULL, CheckBox, _("Auto-Save Games") },
1834 { 0, 0, 0, NULL, (void*) &appData.saveGameFile, "", NULL, FileName,  _("Save Games on File:") },
1835 { 0, 0, 0, NULL, (void*) &appData.savePositionFile, "", NULL, FileName,  _("Save Final Positions on File:") },
1836 { 0, 0, 0, NULL, (void*) &appData.pgnEventHeader, "", NULL, TextBox,  _("PGN Event Header:") },
1837 { 0, 0, 0, NULL, (void*) &appData.oldSaveStyle, "", NULL, CheckBox, _("Old Save Style (as opposed to PGN)") },
1838 { 0, 0, 0, NULL, (void*) &appData.saveExtendedInfoInPGN, "", NULL, CheckBox, _("Save Score/Depth Info in PGN") },
1839 { 0, 0, 0, NULL, (void*) &appData.saveOutOfBookInfo, "", NULL, CheckBox, _("Save Out-of-Book Info in PGN           ") },
1840 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1841 };
1842
1843 void SetColor(char *colorName, Widget box)
1844 {
1845         Arg args[5];
1846         Pixel buttonColor;
1847         XrmValue vFrom, vTo;
1848         if (!appData.monoMode) {
1849             vFrom.addr = (caddr_t) colorName;
1850             vFrom.size = strlen(colorName);
1851             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1852             if (vTo.addr == NULL) {
1853                 buttonColor = (Pixel) -1;
1854             } else {
1855                 buttonColor = *(Pixel *) vTo.addr;
1856             }
1857         }
1858         XtSetArg(args[0], XtNbackground, buttonColor);;
1859         XtSetValues(box, args, 1);
1860 }
1861
1862 void AdjustColor(int i)
1863 {
1864     int n = currentOption[i].value, col, j, r, g, b, step = 10;
1865     char *s, buf[MSG_SIZ]; // color string
1866     Arg args[5];
1867     XtSetArg(args[0], XtNstring, &s);
1868     XtGetValues(currentOption[i-n-1].handle, args, 1);
1869     if(sscanf(s, "#%x", &col) != 1) return;   // malformed
1870     b = col & 0xFF; g = col & 0xFF00; r = col & 0xFF0000;
1871     switch(n) {
1872         case 1: g -= 0x100*step; b -= step; break;
1873         case 2: r -= 0x10000*step; b -= step; break;
1874         case 3: g -= 0x100*step; r -= 0x10000*step; break;
1875         case 4: r += 0x10000*step; g += 0x100*step; b += step; break;
1876     }
1877     if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0;
1878     if(r > 0xFF0000) r = 0xFF0000; if(g > 0xFF00) g = 0xFF00; if(b > 0xFF) b = 0xFF;
1879     col = r | g | b;
1880     snprintf(buf, MSG_SIZ, "#%06x", col);
1881     for(j=1; j<7; j++) if(buf[j] >= 'a') buf[j] -= 32; // capitalize
1882     SetColor(buf, currentOption[i-n].handle);
1883     XtSetArg(args[0], XtNstring, buf);
1884     XtSetValues(currentOption[i-n-1].handle, args, 1);
1885 }
1886
1887 void BoardOptionsOK(int n)
1888 {
1889     if(appData.overrideLineGap >= 0) lineGap = appData.overrideLineGap;
1890     MakeColors(); CreateGCs(True);
1891     CreateXPMPieces();
1892     CreateXPMBoard(appData.liteBackTextureFile, 1);
1893     CreateXPMBoard(appData.darkBackTextureFile, 0);
1894     InitDrawingSizes(-1, 0);
1895     DrawPosition(True, NULL);
1896 }
1897
1898 Option boardOptions[] = {
1899 { 0,   0, 70, NULL, (void*) &appData.whitePieceColor, "", NULL, TextBox, _("White Piece Color:") },
1900 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1901 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1902 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1903 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1904 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1905 { 0,   0, 70, NULL, (void*) &appData.blackPieceColor, "", NULL, TextBox, _("Black Piece Color:") },
1906 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1907 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1908 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1909 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1910 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1911 { 0,   0, 70, NULL, (void*) &appData.lightSquareColor, "", NULL, TextBox, _("Light Square Color:") },
1912 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1913 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1914 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1915 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1916 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1917 { 0,   0, 70, NULL, (void*) &appData.darkSquareColor, "", NULL, TextBox, _("Dark Square Color:") },
1918 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1919 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1920 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1921 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1922 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1923 { 0,   0, 70, NULL, (void*) &appData.highlightSquareColor, "", NULL, TextBox, _("Highlight Color:") },
1924 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1925 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1926 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1927 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1928 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1929 { 0,   0, 70, NULL, (void*) &appData.premoveHighlightColor, "", NULL, TextBox, _("Premove Highlight Color:") },
1930 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1931 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1932 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1933 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1934 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1935 { 0, 0, 0, NULL, (void*) &appData.upsideDown, "", NULL, CheckBox, _("Flip Pieces Shogi Style") },
1936 { 0, 0, 0, NULL, (void*) &appData.allWhite, "", NULL, CheckBox, _("Use Outline Pieces for Black") },
1937 { 0, 0, 0, NULL, (void*) &appData.monoMode, "", NULL, CheckBox, _("Mono Mode") },
1938 { 0,-1, 5, NULL, (void*) &appData.overrideLineGap, "", NULL, Spin, _("Line Gap ( -1 = default for board size):") },
1939 { 0, 0, 0, NULL, (void*) &appData.liteBackTextureFile, "", NULL, FileName, _("Light-Squares Texture File:") },
1940 { 0, 0, 0, NULL, (void*) &appData.darkBackTextureFile, "", NULL, FileName, _("Dark-Squares Texture File:") },
1941 { 0, 0, 0, NULL, (void*) &appData.bitmapDirectory, "", NULL, PathName, _("Directory with Bitmap Pieces:") },
1942 { 0, 0, 0, NULL, (void*) &appData.pixmapDirectory, "", NULL, PathName, _("Directory with Pixmap Pieces:") },
1943 { 0, 0, 0, NULL, (void*) &BoardOptionsOK, "", NULL, EndMark , "" }
1944 };
1945
1946 void GenericReadout()
1947 {
1948     int i, j;
1949     String name, val;
1950     Arg args[16];
1951     char buf[MSG_SIZ];
1952     float x;
1953         for(i=0; ; i++) { // send all options that had to be OK-ed to engine
1954             switch(currentOption[i].type) {
1955                 case TextBox:
1956                 case FileName:
1957                 case PathName:
1958                     XtSetArg(args[0], XtNstring, &val);
1959                     XtGetValues(currentOption[i].handle, args, 1);
1960                     if(*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val)) {
1961                         safeStrCpy(currentOption[i].name + 100, val, MSG_SIZ-100); // text value kept in pivate storage for each option
1962                         *(char**) currentOption[i].target = currentOption[i].name + 100; // option gets to point to that
1963                     }
1964                     break;
1965                 case Spin:
1966                 case Fractional:
1967                     XtSetArg(args[0], XtNstring, &val);
1968                     XtGetValues(currentOption[i].handle, args, 1);
1969                     sscanf(val, "%f", &x);
1970                     if(x > currentOption[i].max) x = currentOption[i].max;
1971                     if(x < currentOption[i].min) x = currentOption[i].min;
1972                     if(currentOption[i].value != x) {
1973                         currentOption[i].value = x;
1974                         if(currentOption[i].type == Spin) *(int*) currentOption[i].target = x;
1975                         else *(float*) currentOption[i].target = x;
1976                     }
1977                     break;
1978                 case CheckBox:
1979                     j = 0;
1980                     XtSetArg(args[0], XtNstate, &j);
1981                     XtGetValues(currentOption[i].handle, args, 1);
1982                     if(currentOption[i].value != j) {
1983                         currentOption[i].value = j;
1984                         *(Boolean*) currentOption[i].target = j;
1985                     }
1986                     break;
1987                 case ComboBox:
1988                     val = ((char**)currentOption[i].choice)[values[i]];
1989                     if(val && (*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val))) {
1990                       if(*(char**) currentOption[i].target) free(*(char**) currentOption[i].target);
1991                       *(char**) currentOption[i].target = strdup(val);
1992                     }
1993                     break;
1994                 case EndMark:
1995                     if(currentOption[i].target) // callback for implementing necessary actions on OK (like redraw)
1996                         ((ButtonCallback*) currentOption[i].target)(i);
1997                     break;
1998             default:
1999                 printf("GenericReadout: unexpected case in switch.\n");
2000                 case Button:
2001                 case Label:
2002               break;
2003             }
2004             if(currentOption[i].type == EndMark) break;
2005         }
2006 }
2007
2008 void GenericCallback(w, client_data, call_data)
2009      Widget w;
2010      XtPointer client_data, call_data;
2011 {
2012     String name, val;
2013     Arg args[16];
2014     char buf[MSG_SIZ];
2015     int i, j;
2016     int data = (intptr_t) client_data;
2017
2018     XtSetArg(args[0], XtNlabel, &name);
2019     XtGetValues(w, args, 1);
2020
2021     if (strcmp(name, _("cancel")) == 0) {
2022         SettingsPopDown();
2023         return;
2024     }
2025     if (strcmp(name, _("OK")) == 0) { // save buttons imply OK
2026         GenericReadout();
2027         SettingsPopDown();
2028         return;
2029     }
2030     ((ButtonCallback*) currentOption[data].target)(data);
2031 }
2032
2033 void
2034 GenericPopUp(Option *option, char *title)
2035 {
2036     Arg args[16];
2037     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
2038     Window root, child;
2039     int x, y, i, j, height=999, width=1, h, c, w;
2040     int win_x, win_y, maxWidth, maxTextWidth;
2041     unsigned int mask;
2042     char def[MSG_SIZ], *msg;
2043     static char pane[6] = "paneX";
2044     Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL;
2045
2046     currentOption = option; // make available to callback
2047     // kludge: fake address of a ChessProgramState struct that contains the options, so Spin and Combo callbacks work on it
2048     currentCps = (ChessProgramState *) ((char *) option - ((char *)&first.option - (char *)&first));
2049
2050 //    if(cps->nrOptions > 50) width = 4; else if(cps->nrOptions>24) width = 2; else width = 1;
2051 //    height = cps->nrOptions / width + 1;
2052      i = 0;
2053     XtSetArg(args[i], XtNresizable, True); i++;
2054     SettingsShell = popup =
2055       XtCreatePopupShell(title, transientShellWidgetClass,
2056                          shellWidget, args, i);
2057
2058     layout =
2059       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
2060                             layoutArgs, XtNumber(layoutArgs));
2061   for(c=0; c<width; c++) {
2062     pane[4] = 'A'+c;
2063     form =
2064       XtCreateManagedWidget(pane, formWidgetClass, layout,
2065                             formArgs, XtNumber(formArgs));
2066     j=0;
2067     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
2068     XtSetValues(form, args, j);
2069     leftMargin = form;
2070
2071     last = widest = NULL; anchor = lastrow;
2072     for(h=0; h<height; h++) {
2073         i = h + c*height;
2074         if(option[i].type == EndMark) break;
2075         lastrow = forelast;
2076         forelast = last;
2077         switch(option[i].type) {
2078           case Fractional:
2079             snprintf(def, MSG_SIZ,  "%.2f", *(float*)option[i].target);
2080             option[i].value = *(float*)option[i].target;
2081             goto tBox;
2082           case Spin:
2083             snprintf(def, MSG_SIZ,  "%d", option[i].value = *(int*)option[i].target);
2084           case TextBox:
2085           case FileName:
2086           case PathName:
2087           tBox:
2088             if(option[i].name[0]) {
2089             j=0;
2090             XtSetArg(args[j], XtNfromVert, last);  j++;
2091             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2092             XtSetArg(args[j], XtNright, XtChainLeft); j++;
2093             XtSetArg(args[j], XtNborderWidth, 0);  j++;
2094             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
2095             texts[h] =
2096             dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
2097             } else texts[h] = dialog = NULL;
2098             w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
2099             if(option[i].type == FileName || option[i].type == PathName) w -= 55;
2100             j=0;
2101             XtSetArg(args[j], XtNfromVert, last);  j++;
2102             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
2103             XtSetArg(args[j], XtNborderWidth, 1); j++;
2104             XtSetArg(args[j], XtNwidth, w); j++;
2105             if(option[i].type == TextBox && option[i].min) XtSetArg(args[j], XtNheight, option[i].min); j++;
2106             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2107             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
2108             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
2109             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
2110             XtSetArg(args[j], XtNright, XtChainRight);  j++;
2111             XtSetArg(args[j], XtNresizable, True);  j++;
2112             XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def : *(char**)option[i].target);  j++;
2113             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
2114             edit = last;
2115             option[i].handle = (void*)
2116                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
2117             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
2118
2119             if(option[i].type == TextBox || option[i].type == Fractional) break;
2120
2121             // add increment and decrement controls for spin
2122             j=0;
2123             XtSetArg(args[j], XtNfromVert, edit);  j++;
2124             XtSetArg(args[j], XtNfromHoriz, last);  j++;
2125             XtSetArg(args[j], XtNleft, XtChainRight); j++;
2126             XtSetArg(args[j], XtNright, XtChainRight); j++;
2127             if(option[i].type == FileName || option[i].type == PathName) {
2128                 w = 50; msg = "browse";
2129             } else {
2130                 XtSetArg(args[j], XtNheight, 10);  j++;
2131                 w = 20; msg = "+";
2132             }
2133             XtSetArg(args[j], XtNwidth, w);  j++;
2134             edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
2135             XtAddCallback(edit, XtNcallback, SpinCallback,
2136                           (XtPointer)(intptr_t) i);
2137
2138             if(option[i].type != Spin) break;
2139
2140             j=0;
2141             XtSetArg(args[j], XtNfromVert, edit);  j++;
2142             XtSetArg(args[j], XtNfromHoriz, last);  j++;
2143             XtSetArg(args[j], XtNheight, 10);  j++;
2144             XtSetArg(args[j], XtNwidth, 20);  j++;
2145             XtSetArg(args[j], XtNleft, XtChainRight); j++;
2146             XtSetArg(args[j], XtNright, XtChainRight); j++;
2147             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
2148             XtAddCallback(last, XtNcallback, SpinCallback,
2149                           (XtPointer)(intptr_t) i);
2150             break;
2151           case CheckBox:
2152             j=0;
2153             XtSetArg(args[j], XtNfromVert, last);  j++;
2154             XtSetArg(args[j], XtNwidth, 10);  j++;
2155             XtSetArg(args[j], XtNheight, 10);  j++;
2156             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2157             XtSetArg(args[j], XtNright, XtChainLeft); j++;
2158             XtSetArg(args[j], XtNstate, option[i].value = *(Boolean*)option[i].target);  j++;
2159             option[i].handle = (void*)
2160                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
2161           case Label:
2162             msg = option[i].name;
2163             if(*msg == NULLCHAR) msg = option[i].textValue;
2164             if(!msg) break;
2165             j=0;
2166             XtSetArg(args[j], XtNfromVert, last);  j++;
2167             XtSetArg(args[j], XtNfromHoriz, option[i].type != Label ? dialog : NULL);  j++;
2168             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2169             XtSetArg(args[j], XtNborderWidth, 0);  j++;
2170             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
2171             last = XtCreateManagedWidget(msg, labelWidgetClass, form, args, j);
2172             break;
2173           case Button:
2174             j=0;
2175             XtSetArg(args[j], XtNfromVert, option[i].min & 1 ? lastrow : last);  j++;
2176             if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last);  j++; }
2177             else  { XtSetArg(args[j], XtNfromHoriz, NULL);  j++; lastrow = forelast; }
2178             if(option[i].max) XtSetArg(args[j], XtNwidth, option[i].max);  j++;
2179             if(option[i].textValue) { // special for buttons of New Variant dialog
2180                 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
2181                                          || strstr(first.variants, VariantName(option[i].value))); j++;
2182                 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
2183             }
2184             option[i].handle = (void*)
2185                 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
2186             if(option[i].target == NULL) SetColor( *(char**) option[i-1].target, last); else
2187             XtAddCallback(last, XtNcallback, GenericCallback,
2188                           (XtPointer)(intptr_t) i);
2189             if(option[i].textValue) SetColor( option[i].textValue, last);
2190             forelast = lastrow; // next button can go on same row
2191             break;
2192           case ComboBox:
2193             j=0;
2194             XtSetArg(args[j], XtNfromVert, last);  j++;
2195             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2196             XtSetArg(args[j], XtNright, XtChainLeft); j++;
2197             XtSetArg(args[j], XtNborderWidth, 0);  j++;
2198             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
2199             texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
2200
2201             for(j=0; option[i].choice[j]; j++)
2202                 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
2203             option[i].value = j + (option[i].choice[j] == NULL);
2204
2205             j=0;
2206             XtSetArg(args[j], XtNfromVert, last);  j++;
2207             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
2208             XtSetArg(args[j], XtNwidth, option[i].max ? option[i].max : 100);  j++;
2209             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
2210             XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name));  j++;
2211             XtSetArg(args[j], XtNlabel, ((char**)option[i].textValue)[option[i].value]);  j++;
2212             option[i].handle = (void*)
2213                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
2214             CreateComboPopup(last, option[i].name, i, (char **) option[i].textValue);
2215             values[i] = option[i].value;
2216             break;
2217           case Break:
2218             width++;
2219             height = i+1;
2220             break;
2221         default:
2222             printf("GenericPopUp: unexpected case in switch.\n");
2223             break;
2224         }
2225     }
2226
2227     // make an attempt to align all spins and textbox controls
2228     maxWidth = maxTextWidth = 0;
2229     for(h=0; h<height; h++) {
2230         i = h + c*height;
2231         if(option[i].type == EndMark) break;
2232         if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
2233                                   || option[i].type == PathName || option[i].type == FileName) {
2234             Dimension w;
2235             if(!texts[h]) continue;
2236             j=0;
2237             XtSetArg(args[j], XtNwidth, &w);  j++;
2238             XtGetValues(texts[h], args, j);
2239             if(option[i].type == Spin) {
2240                 if(w > maxWidth) maxWidth = w;
2241                 widest = texts[h];
2242             } else {
2243                 if(w > maxTextWidth) maxTextWidth = w;
2244                 if(!widest) widest = texts[h];
2245             }
2246         }
2247     }
2248     if(maxTextWidth + 110 < maxWidth)
2249          maxTextWidth = maxWidth - 110;
2250     else maxWidth = maxTextWidth + 110;
2251     for(h=0; h<height; h++) {
2252         i = h + c*height;
2253         if(option[i].type == EndMark) break;
2254         if(!texts[h]) continue;
2255         j=0;
2256         if(option[i].type == Spin) {
2257             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
2258             XtSetValues(texts[h], args, j);
2259         } else
2260         if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
2261             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
2262             XtSetValues(texts[h], args, j);
2263         }
2264     }
2265   }
2266
2267   if(!(option[i].min & 2)) {
2268     j=0;
2269     if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last); last = forelast; } else
2270     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
2271     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
2272     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
2273     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
2274     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
2275     XtSetArg(args[j], XtNright, XtChainRight);  j++;
2276     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
2277     XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer) 0);
2278
2279     XtSetArg(args[0], XtNfromHoriz, b_ok);
2280     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
2281     XtAddCallback(b_cancel, XtNcallback, SettingsPopDown, (XtPointer) 0);
2282   }
2283
2284     XtRealizeWidget(popup);
2285     CatchDeleteWindow(popup, "SettingsPopDown");
2286
2287     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
2288                   &x, &y, &win_x, &win_y, &mask);
2289
2290     XtSetArg(args[0], XtNx, x - 10);
2291     XtSetArg(args[1], XtNy, y - 30);
2292     XtSetValues(popup, args, 2);
2293
2294     XtPopup(popup, XtGrabExclusive);
2295     SettingsUp = True;
2296
2297     previous = NULL;
2298     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
2299 }
2300
2301
2302 void IcsOptionsProc(w, event, prms, nprms)
2303      Widget w;
2304      XEvent *event;
2305      String *prms;
2306      Cardinal *nprms;
2307 {
2308    GenericPopUp(icsOptions, _("ICS Options"));
2309 }
2310
2311 void LoadOptionsProc(w, event, prms, nprms)
2312      Widget w;
2313      XEvent *event;
2314      String *prms;
2315      Cardinal *nprms;
2316 {
2317    GenericPopUp(loadOptions, _("Load Game Options"));
2318 }
2319
2320 void SaveOptionsProc(w, event, prms, nprms)
2321      Widget w;
2322      XEvent *event;
2323      String *prms;
2324      Cardinal *nprms;
2325 {
2326    GenericPopUp(saveOptions, _("Save Game Options"));
2327 }
2328
2329 void BoardOptionsProc(w, event, prms, nprms)
2330      Widget w;
2331      XEvent *event;
2332      String *prms;
2333      Cardinal *nprms;
2334 {
2335    GenericPopUp(boardOptions, _("Board Options"));
2336 }
2337
2338 //---------------------------- Chat Windows ----------------------------------------------
2339
2340 void OutputChatMessage(int partner, char *mess)
2341 {
2342     return; // dummy
2343 }
2344