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