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