c75263fddb887391729d836ce811424d0361c63b
[xboard.git] / xoptions.c
1 /*
2  * xoptions.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000, 2009, 2010 Free Software Foundation, Inc.
5  * ------------------------------------------------------------------------
6  *
7  * GNU XBoard is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * GNU XBoard is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.  *
19  *
20  *------------------------------------------------------------------------
21  ** See the file ChangeLog for a revision history.  */
22
23 // [HGM] this file is the counterpart of woptions.c, containing xboard popup menus
24 // similar to those of WinBoard, to set the most common options interactively.
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if STDC_HEADERS
34 # include <stdlib.h>
35 # include <string.h>
36 #else /* not STDC_HEADERS */
37 extern char *getenv();
38 # if HAVE_STRING_H
39 #  include <string.h>
40 # else /* not HAVE_STRING_H */
41 #  include <strings.h>
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
44
45 #if HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #include <stdint.h>
49
50 #include <X11/Intrinsic.h>
51 #include <X11/StringDefs.h>
52 #include <X11/Shell.h>
53 #include <X11/Xaw/Dialog.h>
54 #include <X11/Xaw/Form.h>
55 #include <X11/Xaw/List.h>
56 #include <X11/Xaw/Label.h>
57 #include <X11/Xaw/SimpleMenu.h>
58 #include <X11/Xaw/SmeBSB.h>
59 #include <X11/Xaw/SmeLine.h>
60 #include <X11/Xaw/Box.h>
61 #include <X11/Xaw/Paned.h>
62 #include <X11/Xaw/MenuButton.h>
63 #include <X11/cursorfont.h>
64 #include <X11/Xaw/Text.h>
65 #include <X11/Xaw/AsciiText.h>
66 #include <X11/Xaw/Viewport.h>
67 #include <X11/Xaw/Toggle.h>
68
69 #include "common.h"
70 #include "backend.h"
71 #include "xboard.h"
72 #include "gettext.h"
73
74 #ifdef ENABLE_NLS
75 # define  _(s) gettext (s)
76 # define N_(s) gettext_noop (s)
77 #else
78 # define  _(s) (s)
79 # define N_(s)  s
80 #endif
81
82 extern void SendToProgram P((char *message, ChessProgramState *cps));
83
84 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
85 extern Display *xDisplay;
86 extern int squareSize;
87 extern Pixmap xMarkPixmap;
88 extern char *layoutName;
89 extern Window xBoardWindow;
90 extern Arg layoutArgs[2], formArgs[2];
91 Pixel timerForegroundPixel, timerBackgroundPixel;
92 extern int searchTime;
93
94 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
95
96 static Widget previous = NULL;
97
98 void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
99 {
100     Arg args;
101
102     if(previous) {
103         XtSetArg(args, XtNdisplayCaret, False);
104         XtSetValues(previous, &args, 1);
105     }
106     XtSetArg(args, XtNdisplayCaret, True);
107     XtSetValues(w, &args, 1);
108     XawTextSetInsertionPoint(w, 9999); // position cursor at end
109     XtSetKeyboardFocus((Widget) data, w);
110     previous = w;
111 }
112
113 //--------------------------- New Shuffle Game --------------------------------------------
114 extern int shuffleOpenings;
115 extern int startedFromPositionFile;
116 int shuffleUp;
117 Widget shuffleShell;
118
119 void ShufflePopDown()
120 {
121     if (!shuffleUp) return;
122     XtPopdown(shuffleShell);
123     XtDestroyWidget(shuffleShell);
124     shuffleUp = False;
125     ModeHighlight();
126 }
127
128 void ShuffleCallback(w, client_data, call_data)
129      Widget w;
130      XtPointer client_data, call_data;
131 {
132     String name;
133     Widget w2;
134     Arg args[16];
135     char buf[80];
136     
137     XtSetArg(args[0], XtNlabel, &name);
138     XtGetValues(w, args, 1);
139     
140     if (strcmp(name, _("cancel")) == 0) {
141         ShufflePopDown();
142         return;
143     }
144     if (strcmp(name, _("off")) == 0) {
145         ShufflePopDown();
146         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
147         ResetGameEvent();
148         return;
149     }
150     if (strcmp(name, _("random")) == 0) {
151         sprintf(buf, "%d", rand());
152         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
153         XtSetValues(XtParent(w), args, 1);
154         return;
155     }
156     if (strcmp(name, _("ok")) == 0) {
157         int nr; String name;
158         name = XawDialogGetValueString(w2 = XtParent(w));
159         if(sscanf(name ,"%d",&nr) != 1) {
160             sprintf(buf, "%d", appData.defaultFrcPosition);
161             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
162             XtSetValues(w2, args, 1);
163             return;
164         }
165         appData.defaultFrcPosition = nr;
166         shuffleOpenings = True;
167         ShufflePopDown();
168         ResetGameEvent();
169         return;
170     }
171 }
172
173 void ShufflePopUp()
174 {
175     Arg args[16];
176     Widget popup, layout, dialog, edit;
177     Window root, child;
178     int x, y, i;
179     int win_x, win_y;
180     unsigned int mask;
181     char def[80];
182     
183     i = 0;
184     XtSetArg(args[i], XtNresizable, True); i++;
185     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
186     shuffleShell = popup =
187       XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
188                          shellWidget, args, i);
189     
190     layout =
191       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
192                             layoutArgs, XtNumber(layoutArgs));
193   
194     sprintf(def, "%d\n", appData.defaultFrcPosition);
195     i = 0;
196     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
197     XtSetArg(args[i], XtNvalue, def); i++;
198     XtSetArg(args[i], XtNborderWidth, 0); i++;
199     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
200                                    layout, args, i);
201     
202 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
203 //    XtSetArg(args[1], XtNuseStringInPlace, False);
204 //    XtSetValues(dialog, args, 2);
205
206     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
207     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
208     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
209     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
210     
211     XtRealizeWidget(popup);
212     CatchDeleteWindow(popup, "ShufflePopDown");
213     
214     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
215                   &x, &y, &win_x, &win_y, &mask);
216     
217     XtSetArg(args[0], XtNx, x - 10);
218     XtSetArg(args[1], XtNy, y - 30);
219     XtSetValues(popup, args, 2);
220     
221     XtPopup(popup, XtGrabExclusive);
222     shuffleUp = True;
223     
224     edit = XtNameToWidget(dialog, "*value");
225
226     XtSetKeyboardFocus(popup, edit);
227 }
228
229 void ShuffleMenuProc(w, event, prms, nprms)
230      Widget w;
231      XEvent *event;
232      String *prms;
233      Cardinal *nprms;
234 {
235 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
236 //      Reset(FALSE, TRUE);
237 //    }
238     ShufflePopUp();
239 }
240
241 //--------------------------- Time-Control Menu Popup ----------------------------------
242 int TimeControlUp;
243 Widget TimeControlShell;
244 int tcInc;
245 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
246 int tcIncrement, tcMoves;
247
248 void TimeControlPopDown()
249 {
250     if (!TimeControlUp) return;
251     previous = NULL;
252     XtPopdown(TimeControlShell);
253     XtDestroyWidget(TimeControlShell);
254     TimeControlUp = False;
255     ModeHighlight();
256 }
257
258 void TimeControlCallback(w, client_data, call_data)
259      Widget w;
260      XtPointer client_data, call_data;
261 {
262     String name, txt;
263     Widget w2;
264     Arg args[16];
265     char buf[80];
266     int j;
267
268     XtSetArg(args[0], XtNlabel, &name);
269     XtGetValues(w, args, 1);
270     
271     if (strcmp(name, _("classical")) == 0) {
272         if(tcInc == 0) return;
273         j=0;
274         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
275         XtSetValues(tcMess1, args, j);
276         j=0;
277         XtSetArg(args[j], XtNlabel, _("moves")); j++;
278         XtSetValues(tcMess2, args, j);
279         if(tcInc == 1) {
280             j=0;
281             XtSetArg(args[j], XtNstring, &name); j++;
282             XtGetValues(tcData, args, j);
283             tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
284         }
285         sprintf(buf, "%d", tcMoves);
286         j=0;
287         XtSetArg(args[j], XtNstring, buf); j++;
288         XtSetValues(tcData, args, j);
289         tcInc = 0;
290         return;
291     }
292     if (strcmp(name, _("incremental")) == 0) {
293         if(tcInc == 1) return;
294         j=0;
295         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
296         XtSetValues(tcMess1, args, j);
297         j=0;
298         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
299         XtSetValues(tcMess2, args, j);
300         if(tcInc == 0) {
301             j=0;
302             XtSetArg(args[j], XtNstring, &name); j++;
303             XtGetValues(tcData, args, j);
304             tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
305         }
306         sprintf(buf, "%d", tcIncrement);
307         j=0;
308         XtSetArg(args[j], XtNstring, buf); j++;
309         XtSetValues(tcData, args, j);
310         tcInc = 1;
311         return;
312     }
313     if (strcmp(name, _("fixed time")) == 0) {
314         if(tcInc == 2) return;
315         j=0;
316         XtSetArg(args[j], XtNlabel, _("sec/move (max)")); j++;
317         XtSetValues(tcMess1, args, j);
318         j=0;
319         XtSetArg(args[j], XtNlabel, _("")); j++;
320         XtSetValues(tcMess2, args, j);
321         j=0;
322         XtSetArg(args[j], XtNstring, ""); j++;
323         XtSetValues(tcData, args, j);
324         tcInc = 2;
325         return;
326     }
327     if (strcmp(name, _(" OK ")) == 0) {
328         int inc, mps, tc, ok;
329         XtSetArg(args[0], XtNstring, &txt);
330         XtGetValues(tcData, args, 1);
331         switch(tcInc) {
332           case 1:
333             ok = sscanf(txt, "%d", &inc); mps = 0;
334             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
335             ok &= (inc >= 0);
336             break;
337           case 0:
338             ok = sscanf(txt, "%d", &mps); inc = -1;
339             ok &= (mps > 0);
340             break;
341           case 2:
342             ok = 1; inc = -1; mps = 40;
343         }
344         if(ok != 1) {
345             XtSetArg(args[0], XtNstring, ""); // erase any offending input
346             XtSetValues(tcData, args, 1);
347             return;
348         }
349         XtSetArg(args[0], XtNstring, &txt);
350         XtGetValues(tcTime, args, 1);
351         if(tcInc == 2) {
352             if(sscanf(txt, "%d", &inc) != 1) {
353                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
354                 XtSetValues(tcTime, args, 1);
355                 DisplayError(_("Bad Time-Control String"), 0);
356                 return;
357             }
358             searchTime = inc;
359         } else {
360             if(!ParseTimeControl(txt, inc, mps)) {
361                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
362                 XtSetValues(tcTime, args, 1);
363                 DisplayError(_("Bad Time-Control String"), 0);
364                 return;
365             }
366             searchTime = 0;
367             appData.movesPerSession = mps;
368             appData.timeIncrement = inc;
369             appData.timeControl = strdup(txt);
370         }
371         XtSetArg(args[0], XtNstring, &txt);
372         XtGetValues(tcOdds1, args, 1);
373         appData.firstTimeOdds = first.timeOdds 
374                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
375         XtGetValues(tcOdds2, args, 1);
376         appData.secondTimeOdds = second.timeOdds 
377                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
378
379         Reset(True, True);
380         TimeControlPopDown();
381         return;
382     }
383 }
384
385 void TimeControlPopUp()
386 {
387     Arg args[16];
388     Widget popup, layout, form, edit, b_ok, b_cancel, b_clas, b_inc, mess;
389     Window root, child;
390     int x, y, i, j;
391     int win_x, win_y;
392     unsigned int mask;
393     char def[80];
394     
395     tcInc = searchTime > 0 ? 2 : (appData.timeIncrement >= 0);
396     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
397     if(!tcInc) tcIncrement = 0;
398     sprintf(def, "%d", tcInc ? tcIncrement : tcMoves);
399
400     i = 0;
401     XtSetArg(args[i], XtNresizable, True); i++;
402 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
403     TimeControlShell = popup =
404       XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
405                          shellWidget, args, i);
406     
407     layout =
408       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
409                             layoutArgs, XtNumber(layoutArgs));
410   
411     form =
412       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
413                             formArgs, XtNumber(formArgs));
414   
415     j = 0;
416 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
417 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
418     XtSetValues(popup, args, j);
419
420     j= 0;
421     XtSetArg(args[j], XtNborderWidth, 1); j++;
422     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
423     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
424     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
425     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
426     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
427     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
428     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
429     XtSetArg(args[j], XtNright, XtChainRight);  j++;
430     XtSetArg(args[j], XtNresizable, True);  j++;
431     XtSetArg(args[j], XtNwidth,  85);  j++;
432 //    XtSetArg(args[j], XtNheight, 20);  j++;
433     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
434     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
435
436     j= 0;
437     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("sec/move (max)   ") : _("   minutes, plus   ") : _("minutes for each")); j++;
438     XtSetArg(args[j], XtNborderWidth, 0); j++;
439     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
440     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
441     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
442     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
443     XtSetArg(args[j], XtNright, XtChainRight);  j++;
444   //  XtSetArg(args[j], XtNwidth,  100);  j++;
445   //  XtSetArg(args[j], XtNheight, 20);  j++;
446     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
447
448     j= 0;
449     XtSetArg(args[j], XtNborderWidth, 1); j++;
450     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
451     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
452     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
453     XtSetArg(args[j], XtNstring, def);  j++;
454     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
455     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
456     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
457     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
458     XtSetArg(args[j], XtNright, XtChainRight);  j++;
459     XtSetArg(args[j], XtNresizable, True);  j++;
460     XtSetArg(args[j], XtNwidth,  40);  j++;
461 //    XtSetArg(args[j], XtNheight, 20);  j++;
462     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
463     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
464
465     j= 0;
466     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("             ") : _("sec/move") : _("moves     ")); j++;
467     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
468     XtSetArg(args[j], XtNborderWidth, 0); j++;
469     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
470     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
471     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
472     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
473     XtSetArg(args[j], XtNright, XtChainRight);  j++;
474 //    XtSetArg(args[j], XtNwidth,  80);  j++;
475 //    XtSetArg(args[j], XtNheight, 20);  j++;
476     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
477                                    form, args, j);
478
479     j= 0;
480     XtSetArg(args[j], XtNborderWidth, 1); j++;
481     XtSetArg(args[j], XtNfromVert, tcTime); j++;
482     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
483     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
484     XtSetArg(args[j], XtNstring, "1");  j++;
485     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
486     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
487     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
488     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
489     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
490     XtSetArg(args[j], XtNresizable, True);  j++;
491     XtSetArg(args[j], XtNwidth,  40);  j++;
492 //    XtSetArg(args[j], XtNheight, 20);  j++;
493     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
494     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
495
496     j= 0;
497     XtSetArg(args[j], XtNborderWidth, 1); j++;
498     XtSetArg(args[j], XtNfromVert, tcTime); j++;
499     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
500     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
501     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
502     XtSetArg(args[j], XtNstring, "1");  j++;
503     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
504     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
505     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
506     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
507     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
508     XtSetArg(args[j], XtNresizable, True);  j++;
509     XtSetArg(args[j], XtNwidth,  40);  j++;
510 //    XtSetArg(args[j], XtNheight, 20);  j++;
511     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
512     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
513
514     j= 0;
515     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
516     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
517     XtSetArg(args[j], XtNborderWidth, 0); j++;
518     XtSetArg(args[j], XtNfromVert, tcTime); j++;
519     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
520     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
521     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
522     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
523     XtSetArg(args[j], XtNright, XtChainRight);  j++;
524 //    XtSetArg(args[j], XtNwidth,  200);  j++;
525 //    XtSetArg(args[j], XtNheight, 20);  j++;
526     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
527                                    form, args, j);
528     j=0;
529     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
530     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
531     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
532     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
533     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
534     XtSetArg(args[j], XtNstate, tcInc==0); j++;
535     b_clas= XtCreateManagedWidget(_("classical"), toggleWidgetClass,
536                                    form, args, j);   
537     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
538
539     j=0;
540     XtSetArg(args[j], XtNradioGroup, b_clas); j++;
541     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
542     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
543     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
544     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
545     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
546     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
547     XtSetArg(args[j], XtNstate, tcInc==1); j++;
548     b_inc = XtCreateManagedWidget(_("incremental"), toggleWidgetClass,
549                                    form, args, j);   
550     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
551
552     j=0;
553     XtSetArg(args[j], XtNradioGroup, b_inc); j++;
554     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
555     XtSetArg(args[j], XtNfromHoriz, b_inc);  j++;
556     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
557     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
558     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
559     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
560     XtSetArg(args[j], XtNstate, tcInc==2); j++;
561     b_inc = XtCreateManagedWidget(_("fixed time"), toggleWidgetClass,
562                                    form, args, j);   
563     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
564
565     j=0;
566     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
567     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
568     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
569     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
570     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
571     XtSetArg(args[j], XtNright, XtChainRight);  j++;
572     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
573                                    form, args, j);   
574     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
575
576     j=0;
577     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
578     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
579     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
580     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
581     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
582     XtSetArg(args[j], XtNright, XtChainRight);  j++;
583     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
584                                    form, args, j);   
585     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
586
587     XtRealizeWidget(popup);
588     CatchDeleteWindow(popup, "TimeControlPopDown");
589     
590     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
591                   &x, &y, &win_x, &win_y, &mask);
592     
593     XtSetArg(args[0], XtNx, x - 10);
594     XtSetArg(args[1], XtNy, y - 30);
595     XtSetValues(popup, args, 2);
596     
597     XtPopup(popup, XtGrabExclusive);
598     TimeControlUp = True;
599     
600     previous = NULL;
601     SetFocus(tcTime, popup, (XEvent*) NULL, False);
602 //    XtSetKeyboardFocus(popup, tcTime);
603 }
604
605 void TimeControlProc(w, event, prms, nprms)
606      Widget w;
607      XEvent *event;
608      String *prms;
609      Cardinal *nprms;
610 {
611    TimeControlPopUp();
612 }
613
614 //--------------------------- Engine-Options Menu Popup ----------------------------------
615 int EngineUp;
616 Widget EngineShell;
617 extern int adjudicateLossThreshold;
618
619 Widget engDrawMoves, engThreshold, engRule, engRepeat;
620
621 void EnginePopDown()
622 {
623     if (!EngineUp) return;
624     previous = NULL;
625     XtPopdown(EngineShell);
626     XtDestroyWidget(EngineShell);
627     EngineUp = False;
628     ModeHighlight();
629 }
630
631 int ReadToggle(Widget w)
632 {
633     Arg args; Boolean res;
634
635     XtSetArg(args, XtNstate, &res);
636     XtGetValues(w, &args, 1);
637
638     return res;
639 }
640
641 Widget w1, w2, w3, w4, w5, w6, w7, w8;
642
643 void EngineCallback(w, client_data, call_data)
644      Widget w;
645      XtPointer client_data, call_data;
646 {
647     String name;
648     Widget s2;
649     Arg args[16];
650     char buf[80];
651     int j;
652     
653     XtSetArg(args[0], XtNlabel, &name);
654     XtGetValues(w, args, 1);
655     
656     if (strcmp(name, _("OK")) == 0) {
657         // read all switches
658         appData.periodicUpdates = ReadToggle(w1);
659 //      appData.hideThinkingFromHuman = ReadToggle(w2);
660         first.scoreIsAbsolute  = appData.firstScoreIsAbsolute  = ReadToggle(w3);
661         second.scoreIsAbsolute = appData.secondScoreIsAbsolute = ReadToggle(w4);
662         appData.testClaims    = ReadToggle(w5);
663         appData.checkMates    = ReadToggle(w6);
664         appData.materialDraws = ReadToggle(w7);
665         appData.trivialDraws  = ReadToggle(w8);
666
667         // adjust setting in other menu for duplicates 
668         // (perhaps duplicates should be removed from general Option Menu?)
669 //      XtSetArg(args[0], XtNleftBitmap, appData.showThinking ? xMarkPixmap : None);
670 //      XtSetValues(XtNameToWidget(menuBarWidget,
671 //                                 "menuOptions.Show Thinking"), args, 1);
672
673         // read out numeric controls, simply ignore bad formats for now
674         XtSetArg(args[0], XtNstring, &name);
675         XtGetValues(engDrawMoves, args, 1);
676         if(sscanf(name, "%d", &j) == 1) appData.adjudicateDrawMoves = j;
677         XtGetValues(engThreshold, args, 1);
678         if(sscanf(name, "%d", &j) == 1) 
679                 adjudicateLossThreshold = appData.adjudicateLossThreshold = -j; // inverted!
680         XtGetValues(engRule, args, 1);
681         if(sscanf(name, "%d", &j) == 1) appData.ruleMoves = j;
682         XtGetValues(engRepeat, args, 1);
683         if(sscanf(name, "%d", &j) == 1) appData.drawRepeats = j;
684
685         EnginePopDown();
686         ShowThinkingEvent(); // [HGM] thinking: score adjudication might need thinking output
687         return;
688     }
689 }
690
691 void EnginePopUp()
692 {
693     Arg args[16];
694     Widget popup, layout, form, edit, b_ok, b_cancel, b_clas, b_inc, s1; 
695     Window root, child;
696     int x, y, i, j, width;
697     int win_x, win_y;
698     unsigned int mask;
699     char def[80];
700     
701     tcInc = (appData.timeIncrement >= 0);
702     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
703     if(!tcInc) tcIncrement = 0;
704     sprintf(def, "%d", tcInc ? tcIncrement : tcMoves);
705
706     i = 0;
707     XtSetArg(args[i], XtNresizable, True); i++;
708 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
709     EngineShell = popup =
710       XtCreatePopupShell(_("Adjudications"), transientShellWidgetClass,
711                          shellWidget, args, i);
712     
713     layout =
714       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
715                             layoutArgs, XtNumber(layoutArgs));
716   
717     form =
718       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
719                             formArgs, XtNumber(formArgs));
720   
721     j = 0;
722 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 250); j++;
723 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 400); j++;
724 //    XtSetValues(popup, args, j);
725
726     j = 0;
727 //    XtSetArg(args[j], XtNwidth,       (XtArgVal) 250); j++;
728 //    XtSetArg(args[j], XtNheight,      (XtArgVal) 20); j++;
729     XtSetArg(args[j], XtNleft,        (XtArgVal) XtChainLeft); j++;
730     XtSetArg(args[j], XtNright,       (XtArgVal) XtChainRight); j++;
731     XtSetArg(args[j], XtNstate,       appData.periodicUpdates); j++;
732 //    XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
733     w1 = XtCreateManagedWidget(_("Periodic Updates (Analysis Mode)"), toggleWidgetClass, form, args, j);
734
735     XtSetArg(args[j], XtNwidth,       (XtArgVal) &width);
736     XtGetValues(w1, &args[j], 1);
737
738 //    XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w1);
739 //    XtSetArg(args[j-3], XtNstate,       appData.hideThinkingFromHuman);
740 //    w2 = XtCreateManagedWidget(_("Hide Thinking from Human"), toggleWidgetClass, form, args, j);
741
742     XtSetArg(args[j], XtNwidth,       (XtArgVal) width); j++;
743     XtSetArg(args[j-2], XtNstate,     appData.firstScoreIsAbsolute);
744     XtSetArg(args[j], XtNfromVert,    (XtArgVal) w1); j++;
745     w3 = XtCreateManagedWidget(_("Engine #1 Score is Absolute"), toggleWidgetClass, form, args, j);
746
747     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w3);
748     XtSetArg(args[j-3], XtNstate,       appData.secondScoreIsAbsolute);
749     w4 = XtCreateManagedWidget(_("Engine #2 Score is Absolute"), toggleWidgetClass, form, args, j);
750
751     s1 = XtCreateManagedWidget(_("\nAdjudications in non-ICS games:"), labelWidgetClass, form, args, 3);
752
753     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) s1);
754     XtSetArg(args[j-3], XtNstate,       appData.testClaims);
755     w5 = XtCreateManagedWidget(_("Verify Engine Result Claims"), toggleWidgetClass, form, args, j);
756
757     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w5);
758     XtSetArg(args[j-3], XtNstate,       appData.checkMates);
759     w6 = XtCreateManagedWidget(_("Detect All Mates"), toggleWidgetClass, form, args, j);
760
761     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w6);
762     XtSetArg(args[j-3], XtNstate,       appData.materialDraws);
763     w7 = XtCreateManagedWidget(_("Draw when Insuff. Mating Material"), toggleWidgetClass, form, args, j);
764
765     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w7);
766     XtSetArg(args[j-3], XtNstate,       appData.trivialDraws);
767     w8 = XtCreateManagedWidget(_("Adjudicate Trivial Draws"), toggleWidgetClass, form, args, j);
768
769     XtSetArg(args[0], XtNfromVert,  (XtArgVal) w4);
770     XtSetArg(args[1], XtNborderWidth, (XtArgVal) 0);
771     XtSetValues(s1, args, 2);
772
773     sprintf(def, "%d", appData.adjudicateDrawMoves);
774     j= 0;
775     XtSetArg(args[j], XtNborderWidth, 1); j++;
776     XtSetArg(args[j], XtNfromVert, w8); j++;
777     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
778     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
779     XtSetArg(args[j], XtNstring, def);  j++;
780     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
781     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
782     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
783     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
784     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
785     XtSetArg(args[j], XtNresizable, True);  j++;
786     XtSetArg(args[j], XtNwidth,  60);  j++;
787 //    XtSetArg(args[j], XtNheight, 20);  j++;
788     engDrawMoves = XtCreateManagedWidget("Length", asciiTextWidgetClass, form, args, j);
789     XtAddEventHandler(engDrawMoves, ButtonPressMask, False, SetFocus, (XtPointer) popup);
790
791     j= 0;
792     XtSetArg(args[j], XtNlabel, _(" moves maximum, then draw")); j++;
793     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
794     XtSetArg(args[j], XtNborderWidth, 0); j++;
795     XtSetArg(args[j], XtNfromVert, w8); j++;
796     XtSetArg(args[j], XtNfromHoriz, engDrawMoves); j++;
797     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
798     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
799     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
800     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
801 //    XtSetArg(args[j], XtNwidth,  170);  j++;
802 //    XtSetArg(args[j], XtNheight, 20);  j++;
803     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
804
805     sprintf(def, "%d", -appData.adjudicateLossThreshold); // inverted!
806     j= 0;
807     XtSetArg(args[j], XtNborderWidth, 1); j++;
808     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
809     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
810     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
811     XtSetArg(args[j], XtNstring, def);  j++;
812     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
813     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
814     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
815     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
816     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
817     XtSetArg(args[j], XtNresizable, True);  j++;
818     XtSetArg(args[j], XtNwidth,  60);  j++;
819 //    XtSetArg(args[j], XtNheight, 20);  j++;
820     engThreshold = XtCreateManagedWidget("Threshold", asciiTextWidgetClass, form, args, j);
821     XtAddEventHandler(engThreshold, ButtonPressMask, False, SetFocus, (XtPointer) popup);
822
823     j= 0;
824     XtSetArg(args[j], XtNlabel, _("-centiPawn lead is win")); j++;
825     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
826     XtSetArg(args[j], XtNborderWidth, 0); j++;
827     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
828     XtSetArg(args[j], XtNfromHoriz, engThreshold); j++;
829     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
830     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
831     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
832     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
833 //    XtSetArg(args[j], XtNwidth,  150);  j++;
834 //    XtSetArg(args[j], XtNheight, 20);  j++;
835     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
836
837     sprintf(def, "%d", appData.ruleMoves);
838     j= 0;
839     XtSetArg(args[j], XtNborderWidth, 1); j++;
840     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
841     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
842     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
843     XtSetArg(args[j], XtNstring, def);  j++;
844     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
845     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
846     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
847     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
848     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
849     XtSetArg(args[j], XtNresizable, True);  j++;
850     XtSetArg(args[j], XtNwidth,  30);  j++;
851 //    XtSetArg(args[j], XtNheight, 20);  j++;
852     engRule = XtCreateManagedWidget("Rule", asciiTextWidgetClass, form, args, j);
853     XtAddEventHandler(engRule, ButtonPressMask, False, SetFocus, (XtPointer) popup);
854
855     j= 0;
856     XtSetArg(args[j], XtNlabel, _("-move rule applied")); j++;
857     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
858     XtSetArg(args[j], XtNborderWidth, 0); j++;
859     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
860     XtSetArg(args[j], XtNfromHoriz, engRule); j++;
861     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
862     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
863     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
864     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
865 //    XtSetArg(args[j], XtNwidth,  130);  j++;
866 //    XtSetArg(args[j], XtNheight, 20);  j++;
867     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
868
869     sprintf(def, "%d", appData.drawRepeats);
870     j= 0;
871     XtSetArg(args[j], XtNborderWidth, 1); j++;
872     XtSetArg(args[j], XtNfromVert, engRule); j++;
873     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
874     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
875     XtSetArg(args[j], XtNstring, def);  j++;
876     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
877     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
878     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
879     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
880     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
881     XtSetArg(args[j], XtNresizable, True);  j++;
882     XtSetArg(args[j], XtNwidth,  30);  j++;
883 //    XtSetArg(args[j], XtNheight, 20);  j++;
884     engRepeat = XtCreateManagedWidget("Repeats", asciiTextWidgetClass, form, args, j);
885     XtAddEventHandler(engRepeat, ButtonPressMask, False, SetFocus, (XtPointer) popup);
886
887     j= 0;
888     XtSetArg(args[j], XtNlabel, _("-fold repeat is draw")); j++;
889     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
890     XtSetArg(args[j], XtNborderWidth, 0); j++;
891     XtSetArg(args[j], XtNfromVert, engRule); j++;
892     XtSetArg(args[j], XtNfromHoriz, engRepeat); j++;
893     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
894     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
895     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
896     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
897 //    XtSetArg(args[j], XtNwidth,  130);  j++;
898 //    XtSetArg(args[j], XtNheight, 20);  j++;
899     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
900
901     j=0;
902     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
903     XtSetArg(args[j], XtNfromHoriz, tcMess2);  j++;
904     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
905     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
906     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
907     XtSetArg(args[j], XtNright, XtChainRight);  j++;
908     b_ok= XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
909     XtAddCallback(b_ok, XtNcallback, EngineCallback, (XtPointer) 0);
910
911     j=0;
912     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
913     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
914     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
915     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
916     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
917     XtSetArg(args[j], XtNright, XtChainRight);  j++;
918     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
919                                    form, args, j);   
920     XtAddCallback(b_cancel, XtNcallback, EnginePopDown, (XtPointer) 0);
921
922     XtRealizeWidget(popup);
923     CatchDeleteWindow(popup, "EnginePopDown");
924     
925     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
926                   &x, &y, &win_x, &win_y, &mask);
927     
928     XtSetArg(args[0], XtNx, x - 10);
929     XtSetArg(args[1], XtNy, y - 30);
930     XtSetValues(popup, args, 2);
931     
932     XtPopup(popup, XtGrabExclusive);
933     EngineUp = True;
934     
935     previous = NULL;
936     SetFocus(engThreshold, popup, (XEvent*) NULL, False);
937 }
938
939 void EngineMenuProc(w, event, prms, nprms)
940      Widget w;
941      XEvent *event;
942      String *prms;
943      Cardinal *nprms;
944 {
945    EnginePopUp();
946 }
947
948 //--------------------------- New-Variant Menu PopUp -----------------------------------
949 struct NewVarButton {
950   char   *name;
951   char *color;
952   Widget handle;
953   VariantClass variant;
954 };
955
956 struct NewVarButton buttonDesc[] = {
957     {N_("normal"),            "#FFFFFF", 0, VariantNormal},
958     {N_("FRC"),               "#FFFFFF", 0, VariantFischeRandom},
959     {N_("wild castle"),       "#FFFFFF", 0, VariantWildCastle},
960     {N_("no castle"),         "#FFFFFF", 0, VariantNoCastle},
961     {N_("knightmate"),        "#FFFFFF", 0, VariantKnightmate},
962     {N_("berolina"),          "#FFFFFF", 0, VariantBerolina},
963     {N_("cylinder"),          "#FFFFFF", 0, VariantCylinder},
964     {N_("shatranj"),          "#FFFFFF", 0, VariantShatranj},
965     {N_("makruk"),            "#FFFFFF", 0, VariantMakruk},
966     {N_("atomic"),            "#FFFFFF", 0, VariantAtomic},
967     {N_("two kings"),         "#FFFFFF", 0, VariantTwoKings},
968     {N_("3-checks"),          "#FFFFFF", 0, Variant3Check},
969     {N_("suicide"),           "#FFFFBF", 0, VariantSuicide},
970     {N_("give-away"),         "#FFFFBF", 0, VariantGiveaway},
971     {N_("losers"),            "#FFFFBF", 0, VariantLosers},
972     {N_("fairy"),             "#BFBFBF", 0, VariantFairy},
973     {N_("Superchess"),        "#FFBFBF", 0, VariantSuper},
974     {N_("crazyhouse"),        "#FFBFBF", 0, VariantCrazyhouse},
975     {N_("bughouse"),          "#FFBFBF", 0, VariantBughouse},
976     {N_("shogi (9x9)"),       "#BFFFFF", 0, VariantShogi},
977     {N_("xiangqi (9x10)"),    "#BFFFFF", 0, VariantXiangqi},
978     {N_("courier (12x8)"),    "#BFFFBF", 0, VariantCourier},
979     {N_("janus (10x8)"),      "#BFBFFF", 0, VariantJanus},
980     {N_("Capablanca (10x8)"), "#BFBFFF", 0, VariantCapablanca},
981     {N_("CRC (10x8)"),        "#BFBFFF", 0, VariantCapaRandom},
982 #ifdef GOTHIC
983     {N_("Gothic (10x8)"),     "#BFBFFF", 0, VariantGothic},
984 #endif
985 #ifdef FALCON
986     {N_("Falcon (10x8)"),     "#BFBFFF", 0, VariantFalcon},
987 #endif
988     {NULL,                0, 0, (VariantClass) 0}
989 };
990
991 int NewVariantUp;
992 Widget NewVariantShell;
993
994 void NewVariantPopDown()
995 {
996     if (!NewVariantUp) return;
997     XtPopdown(NewVariantShell);
998     XtDestroyWidget(NewVariantShell);
999     NewVariantUp = False;
1000     ModeHighlight();
1001 }
1002
1003 void NewVariantCallback(w, client_data, call_data)
1004      Widget w;
1005      XtPointer client_data, call_data;
1006 {
1007     String name;
1008     Widget w2;
1009     Arg args[16];
1010     char buf[80];
1011     VariantClass v;
1012     
1013     XtSetArg(args[0], XtNlabel, &name);
1014     XtGetValues(w, args, 1);
1015     
1016     if (strcmp(name, _("  OK  ")) == 0) {
1017         int nr = (intptr_t) XawToggleGetCurrent(buttonDesc[0].handle) - 1;
1018         if(nr < 0) return;
1019         v = buttonDesc[nr].variant;
1020         if(!appData.noChessProgram) { 
1021             char *name = VariantName(v), buf[MSG_SIZ];
1022             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
1023                 /* [HGM] in protocol 2 we check if variant is suported by engine */
1024                 sprintf(buf, _("Variant %s not supported by %s"), name, first.tidy);
1025                 DisplayError(buf, 0);
1026 //              NewVariantPopDown();
1027                 return; /* ignore OK if first engine does not support it */
1028             } else
1029             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
1030                 sprintf(buf, _("Warning: second engine (%s) does not support this!"), second.tidy);
1031                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
1032             }
1033         }
1034
1035         gameInfo.variant = v;
1036         appData.variant = VariantName(v);
1037
1038         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
1039         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
1040         appData.pieceToCharTable = NULL;
1041         Reset(True, True);
1042         NewVariantPopDown();
1043         return;
1044     }
1045 }
1046
1047 void NewVariantPopUp()
1048 {
1049     Arg args[16];
1050     Widget popup, layout, dialog, edit, form, last = NULL, b_ok, b_cancel;
1051     Window root, child;
1052     int x, y, i, j;
1053     int win_x, win_y;
1054     unsigned int mask;
1055     char def[80];
1056     XrmValue vFrom, vTo;
1057
1058     i = 0;
1059     XtSetArg(args[i], XtNresizable, True); i++;
1060 //    XtSetArg(args[i], XtNwidth, 250); i++;
1061 //    XtSetArg(args[i], XtNheight, 300); i++;
1062     NewVariantShell = popup =
1063       XtCreatePopupShell(_("NewVariant Menu"), transientShellWidgetClass,
1064                          shellWidget, args, i);
1065     
1066     layout =
1067       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1068                             layoutArgs, XtNumber(layoutArgs));
1069   
1070     form =
1071       XtCreateManagedWidget("form", formWidgetClass, layout,
1072                             formArgs, XtNumber(formArgs));
1073   
1074     for(i = 0; buttonDesc[i].name != NULL; i++) {
1075         Pixel buttonColor;
1076         if (!appData.monoMode) {
1077             vFrom.addr = (caddr_t) buttonDesc[i].color;
1078             vFrom.size = strlen(buttonDesc[i].color);
1079             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1080             if (vTo.addr == NULL) {
1081                 buttonColor = (Pixel) -1;
1082             } else {
1083                 buttonColor = *(Pixel *) vTo.addr;
1084             }
1085         }
1086     
1087         j = 0;
1088         XtSetArg(args[j], XtNradioGroup, last); j++;
1089         XtSetArg(args[j], XtNwidth, 125); j++;
1090 //      XtSetArg(args[j], XtNheight, 16); j++;
1091         XtSetArg(args[j], XtNfromVert, i == 15 ? NULL : last); j++;
1092         XtSetArg(args[j], XtNfromHoriz, i < 15 ? NULL : buttonDesc[i-15].handle); j++;
1093         XtSetArg(args[j], XtNradioData, i+1); j++;
1094         XtSetArg(args[j], XtNbackground, buttonColor); j++;
1095         XtSetArg(args[j], XtNstate, gameInfo.variant == buttonDesc[i].variant); j++;
1096         buttonDesc[i].handle = last =
1097             XtCreateManagedWidget(buttonDesc[i].name, toggleWidgetClass, form, args, j);
1098     }
1099
1100     j=0;
1101     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1102     XtSetArg(args[j], XtNfromHoriz, buttonDesc[12].handle);  j++;
1103     XtSetArg(args[j], XtNheight, 35); j++;
1104 //    XtSetArg(args[j], XtNwidth, 60); j++;
1105     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1106     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1107     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1108     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1109     b_cancel= XtCreateManagedWidget(_("CANCEL"), commandWidgetClass, form, args, j);   
1110     XtAddCallback(b_cancel, XtNcallback, NewVariantPopDown, (XtPointer) 0);
1111
1112     j=0;
1113     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
1114     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1115     XtSetArg(args[j], XtNheight, 35); j++;
1116 //    XtSetArg(args[j], XtNwidth, 60); j++;
1117     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1118     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1119     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1120     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1121     b_ok= XtCreateManagedWidget(_("  OK  "), commandWidgetClass, form, args, j);   
1122     XtAddCallback(b_ok, XtNcallback, NewVariantCallback, (XtPointer) 0);
1123
1124     j=0;
1125     XtSetArg(args[j], XtNfromVert, buttonDesc[14].handle);  j++;
1126 //    XtSetArg(args[j], XtNheight, 70); j++;
1127     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1128     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1129     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1130     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1131     XtSetArg(args[j], XtNlabel, _("WARNING: variants with un-orthodox\n"
1132                                   "pieces only have built-in bitmaps\n"
1133                                   "for -boardSize middling, bulky and\n"
1134                                   "petite, and substitute king or amazon\n"
1135                                   "for missing bitmaps. (See manual.)")); j++;
1136     XtCreateManagedWidget("warning", labelWidgetClass, form, args, j);
1137
1138             XtRealizeWidget(popup);
1139     CatchDeleteWindow(popup, "NewVariantPopDown");
1140     
1141     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1142                   &x, &y, &win_x, &win_y, &mask);
1143     
1144     XtSetArg(args[0], XtNx, x - 10);
1145     XtSetArg(args[1], XtNy, y - 30);
1146     XtSetValues(popup, args, 2);
1147     
1148     XtPopup(popup, XtGrabExclusive);
1149     NewVariantUp = True;
1150 }
1151
1152 void NewVariantProc(w, event, prms, nprms)
1153      Widget w;
1154      XEvent *event;
1155      String *prms;
1156      Cardinal *nprms;
1157 {
1158    NewVariantPopUp();
1159 }
1160
1161 //--------------------------- UCI Menu Popup ------------------------------------------
1162 int UciUp;
1163 Widget UciShell;
1164
1165 struct UciControl {
1166   char *name;
1167   Widget handle;
1168   void *ptr;
1169 };
1170
1171 struct UciControl controlDesc[] = {
1172   {N_("maximum nr of CPUs:"), 0, &appData.smpCores},
1173   {N_("Polyglot Directory:"), 0, &appData.polyglotDir},
1174   {N_("Hash Size (MB):"),     0, &appData.defaultHashSize},
1175   {N_("EGTB Path:"),          0, &appData.defaultPathEGTB},
1176   {N_("EGTB Cache (MB):"),    0, &appData.defaultCacheSizeEGTB},
1177   {N_("Polyglot Book:"),      0, &appData.polyglotBook},
1178   {NULL, 0, NULL},
1179 };
1180
1181 void UciPopDown()
1182 {
1183     if (!UciUp) return;
1184     previous = NULL;
1185     XtPopdown(UciShell);
1186     XtDestroyWidget(UciShell);
1187     UciUp = False;
1188     ModeHighlight();
1189 }
1190
1191 void UciCallback(w, client_data, call_data)
1192      Widget w;
1193      XtPointer client_data, call_data;
1194 {
1195     String name;
1196     Arg args[16];
1197     char buf[80];
1198     int oldCores = appData.smpCores, ponder = 0;
1199     
1200     XtSetArg(args[0], XtNlabel, &name);
1201     XtGetValues(w, args, 1);
1202     
1203     if (strcmp(name, _("OK")) == 0) {
1204         int nr, i, j; String name;
1205         for(i=0; i<6; i++) {
1206             XtSetArg(args[0], XtNstring, &name);
1207             XtGetValues(controlDesc[i].handle, args, 1);
1208             if(i&1) {
1209                 if(name)
1210                     *(char**) controlDesc[i].ptr = strdup(name);
1211             } else {
1212                 if(sscanf(name, "%d", &j) == 1) 
1213                     *(int*) controlDesc[i].ptr = j;
1214             }
1215         }
1216         XtSetArg(args[0], XtNstate, &appData.usePolyglotBook);
1217         XtGetValues(w1, args, 1);
1218         XtSetArg(args[0], XtNstate, &appData.firstHasOwnBookUCI);
1219         XtGetValues(w2, args, 1);
1220         XtSetArg(args[0], XtNstate, &appData.secondHasOwnBookUCI);
1221         XtGetValues(w3, args, 1);
1222         XtSetArg(args[0], XtNstate, &ponder);
1223         XtGetValues(w4, args, 1);
1224
1225         // adjust setting in other menu for duplicates 
1226         // (perhaps duplicates should be removed from general Option Menu?)
1227         XtSetArg(args[0], XtNleftBitmap, ponder ? xMarkPixmap : None);
1228         XtSetValues(XtNameToWidget(menuBarWidget,
1229                                    "menuOptions.Ponder Next Move"), args, 1);
1230
1231         // make sure changes are sent to first engine by re-initializing it
1232         // if it was already started pre-emptively at end of previous game
1233         if(gameMode == BeginningOfGame) Reset(True, True); else {
1234             // Some changed setting need immediate sending always.
1235             PonderNextMoveEvent(ponder);
1236             if(oldCores != appData.smpCores)
1237                 NewSettingEvent(False, "cores", appData.smpCores);
1238       }
1239       UciPopDown();
1240       return;
1241     }
1242 }
1243
1244 void UciPopUp()
1245 {
1246     Arg args[16];
1247     Widget popup, layout, dialog, edit, form, b_ok, b_cancel, last = NULL, new, upperLeft;
1248     Window root, child;
1249     int x, y, i, j;
1250     int win_x, win_y;
1251     unsigned int mask;
1252     char def[80];
1253     
1254     i = 0;
1255     XtSetArg(args[i], XtNresizable, True); i++;
1256 //    XtSetArg(args[i], XtNwidth, 300); i++;
1257     UciShell = popup =
1258       XtCreatePopupShell(_("Engine Settings"), transientShellWidgetClass,
1259                          shellWidget, args, i);
1260     
1261     layout =
1262       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1263                             layoutArgs, XtNumber(layoutArgs));
1264   
1265     
1266     form =
1267       XtCreateManagedWidget("form", formWidgetClass, layout,
1268                             formArgs, XtNumber(formArgs));
1269   
1270     j = 0;
1271     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1272     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1273     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1274 //    XtSetArg(args[j], XtNheight, 20); j++;
1275     for(i = 0; controlDesc[i].name != NULL; i++) {
1276         j = 3;
1277         XtSetArg(args[j], XtNfromVert, last); j++;
1278 //      XtSetArg(args[j], XtNwidth, 130); j++;
1279         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1280         XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1281         XtSetArg(args[j], XtNborderWidth, 0); j++;
1282         new = XtCreateManagedWidget(controlDesc[i].name, labelWidgetClass, form, args, j);
1283         if(i==0) upperLeft = new;
1284
1285         j = 4;
1286         XtSetArg(args[j], XtNborderWidth, 1); j++;
1287         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1288         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1289         XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1290         XtSetArg(args[j], XtNright, XtChainRight);  j++;
1291         XtSetArg(args[j], XtNresizable, True);  j++;
1292         XtSetArg(args[j], XtNwidth, i&1 ? 245 : 50); j++;
1293         if(i&1) {
1294             XtSetArg(args[j], XtNstring, * (char**) controlDesc[i].ptr ? 
1295                                          * (char**) controlDesc[i].ptr : ""); j++;
1296         } else {
1297             sprintf(def, "%d", * (int*) controlDesc[i].ptr);
1298             XtSetArg(args[j], XtNstring, def); j++;
1299         }
1300         XtSetArg(args[j], XtNfromHoriz, upperLeft); j++;
1301         controlDesc[i].handle = last =
1302             XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
1303         XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1304     }
1305
1306     j=0;
1307     XtSetArg(args[j], XtNfromHoriz, controlDesc[0].handle);  j++;
1308     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1309     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1310     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1311     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1312     XtSetArg(args[j], XtNstate, appData.ponderNextMove);  j++;
1313     w4 = XtCreateManagedWidget(_("Ponder"), toggleWidgetClass, form, args, j);   
1314
1315     j=0;
1316     XtSetArg(args[j], XtNfromVert, last);  j++;
1317     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1318     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1319     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1320     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1321     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
1322     XtAddCallback(b_ok, XtNcallback, UciCallback, (XtPointer) 0);
1323
1324     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
1325     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);   
1326     XtAddCallback(b_cancel, XtNcallback, UciPopDown, (XtPointer) 0);
1327
1328     j = 5;
1329     XtSetArg(args[j], XtNfromHoriz, upperLeft);  j++;
1330     XtSetArg(args[j], XtNstate, appData.usePolyglotBook);  j++;
1331     w1 = XtCreateManagedWidget(_(" use book "), toggleWidgetClass, form, args, j);   
1332 //    XtAddCallback(w1, XtNcallback, UciCallback, (XtPointer) 0);
1333
1334     j = 5;
1335     XtSetArg(args[j], XtNfromHoriz, w1);  j++;
1336     XtSetArg(args[j], XtNstate, appData.firstHasOwnBookUCI);  j++;
1337     w2 = XtCreateManagedWidget(_("own book 1"), toggleWidgetClass, form, args, j);   
1338 //    XtAddCallback(w2, XtNcallback, UciCallback, (XtPointer) 0);
1339
1340     j = 5;
1341     XtSetArg(args[j], XtNfromHoriz, w2);  j++;
1342     XtSetArg(args[j], XtNstate, appData.secondHasOwnBookUCI);  j++;
1343     w3 = XtCreateManagedWidget(_("own book 2"), toggleWidgetClass, form, args, j);   
1344 //    XtAddCallback(w3, XtNcallback, UciCallback, (XtPointer) 0);
1345
1346     XtRealizeWidget(popup);
1347     CatchDeleteWindow(popup, "UciPopDown");
1348     
1349     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1350                   &x, &y, &win_x, &win_y, &mask);
1351     
1352     XtSetArg(args[0], XtNx, x - 10);
1353     XtSetArg(args[1], XtNy, y - 30);
1354     XtSetValues(popup, args, 2);
1355     
1356     XtPopup(popup, XtGrabExclusive);
1357     UciUp = True;
1358
1359     previous = NULL;
1360     SetFocus(controlDesc[2].handle, popup, (XEvent*) NULL, False);
1361 //    XtSetKeyboardFocus(popup, controlDesc[1].handle);
1362 }
1363
1364 void UciMenuProc(w, event, prms, nprms)
1365      Widget w;
1366      XEvent *event;
1367      String *prms;
1368      Cardinal *nprms;
1369 {
1370    UciPopUp();
1371 }
1372
1373 //--------------------------- Engine-specific options menu ----------------------------------
1374
1375 int SettingsUp;
1376 Widget SettingsShell;
1377 int values[MAX_OPTIONS];
1378 ChessProgramState *currentCps;
1379
1380 void SettingsPopDown()
1381 {
1382     if (!SettingsUp) return;
1383     previous = NULL;
1384     XtPopdown(SettingsShell);
1385     XtDestroyWidget(SettingsShell);
1386     SettingsUp = False;
1387     ModeHighlight();
1388 }
1389
1390 void SpinCallback(w, client_data, call_data)
1391      Widget w;
1392      XtPointer client_data, call_data;
1393 {
1394     String name, val;
1395     Widget w2;
1396     Arg args[16];
1397     char buf[MSG_SIZ];
1398     int i, j;
1399     int data = (intptr_t) client_data;
1400     
1401     XtSetArg(args[0], XtNlabel, &name);
1402     XtGetValues(w, args, 1);
1403     
1404     j = 0;
1405     XtSetArg(args[0], XtNstring, &val);
1406     XtGetValues(currentCps->option[data].handle, args, 1);
1407     sscanf(val, "%d", &j);
1408     if (strcmp(name, "+") == 0) {
1409         if(++j > currentCps->option[data].max) return;
1410     } else
1411     if (strcmp(name, "-") == 0) {
1412         if(--j < currentCps->option[data].min) return;
1413     } else return;
1414     sprintf(buf, "%d", j);
1415     XtSetArg(args[0], XtNstring, buf);
1416     XtSetValues(currentCps->option[data].handle, args, 1);
1417 }
1418
1419 void SettingsCallback(w, client_data, call_data)
1420      Widget w;
1421      XtPointer client_data, call_data;
1422 {
1423     String name, val;
1424     Widget w2;
1425     Arg args[16];
1426     char buf[MSG_SIZ];
1427     int i, j;
1428     int data = (intptr_t) client_data;
1429     
1430     XtSetArg(args[0], XtNlabel, &name);
1431     XtGetValues(w, args, 1);
1432     
1433     if (strcmp(name, _("cancel")) == 0) {
1434         SettingsPopDown();
1435         return;
1436     }
1437     if (strcmp(name, _("OK")) == 0 || data) { // save buttons imply OK
1438         int nr;
1439
1440         for(i=0; i<currentCps->nrOptions; i++) { // send all options that had to be OK-ed to engine
1441             switch(currentCps->option[i].type) {
1442                 case TextBox:
1443                     XtSetArg(args[0], XtNstring, &val);
1444                     XtGetValues(currentCps->option[i].handle, args, 1);
1445                     if(strcmp(currentCps->option[i].textValue, val)) {
1446                         strcpy(currentCps->option[i].textValue, val);
1447                         sprintf(buf, "option %s=%s\n", currentCps->option[i].name, val);
1448                         SendToProgram(buf, currentCps);
1449                     }
1450                     break;
1451                 case Spin:
1452                     XtSetArg(args[0], XtNstring, &val);
1453                     XtGetValues(currentCps->option[i].handle, args, 1);
1454                     sscanf(val, "%d", &j);
1455                     if(j > currentCps->option[i].max) j = currentCps->option[i].max;
1456                     if(j < currentCps->option[i].min) j = currentCps->option[i].min;
1457                     if(currentCps->option[i].value != j) {
1458                         currentCps->option[i].value = j;
1459                         sprintf(buf, "option %s=%d\n", currentCps->option[i].name, j);
1460                         SendToProgram(buf, currentCps);
1461                     }
1462                     break;
1463                 case CheckBox:
1464                     j = 0;
1465                     XtSetArg(args[0], XtNstate, &j);
1466                     XtGetValues(currentCps->option[i].handle, args, 1);
1467                     if(currentCps->option[i].value != j) {
1468                         currentCps->option[i].value = j;
1469                         sprintf(buf, "option %s=%d\n", currentCps->option[i].name, j);
1470                         SendToProgram(buf, currentCps);
1471                     }
1472                     break;
1473                 case ComboBox:
1474                     if(currentCps->option[i].value != values[i]) {
1475                         currentCps->option[i].value = values[i];
1476                         sprintf(buf, "option %s=%s\n", currentCps->option[i].name, 
1477                                 ((char**)currentCps->option[i].textValue)[values[i]]);
1478                         SendToProgram(buf, currentCps);
1479                     }
1480                     break;
1481             }
1482         }
1483         if(data) { // send save-button command to engine
1484             sprintf(buf, "option %s\n", name);
1485             SendToProgram(buf, currentCps);
1486         }
1487         SettingsPopDown();
1488         return;
1489     }
1490     sprintf(buf, "option %s\n", name);
1491     SendToProgram(buf, currentCps);
1492 }
1493
1494 void ComboSelect(w, addr, index) // callback for all combo items
1495      Widget w;
1496      caddr_t addr;
1497      caddr_t index;
1498 {
1499     Arg args[16];
1500     int i = ((intptr_t)addr)>>8;
1501     int j = 255 & (intptr_t) addr;
1502
1503     values[i] = j; // store in temporary, for transfer at OK
1504     XtSetArg(args[0], XtNlabel, ((char**)currentCps->option[i].textValue)[j]);
1505     XtSetValues(currentCps->option[i].handle, args, 1);
1506 }
1507
1508 void CreateComboPopup(parent, name, n, mb)
1509      Widget parent;
1510      String name;
1511      int n;
1512      char *mb[];
1513 {
1514     int i=0, j;
1515     Widget menu, entry;
1516     Arg args[16];
1517
1518     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
1519                               parent, NULL, 0);
1520     j = 0;
1521     XtSetArg(args[j], XtNwidth, 100);  j++;
1522 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
1523     while (mb[i] != NULL) {
1524             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
1525                                           menu, args, j);
1526             XtAddCallback(entry, XtNcallback,
1527                           (XtCallbackProc) ComboSelect,
1528                           (caddr_t)(intptr_t) (256*n+i));
1529         i++;
1530     }
1531 }       
1532
1533 void SettingsPopUp(ChessProgramState *cps)
1534 {
1535     Arg args[16];
1536     Widget popup, layout, dialog, edit=NULL, form, oldform, last, b_ok, b_cancel, leftMargin = NULL;
1537     Window root, child;
1538     int x, y, i, j, height, width, h, c;
1539     int win_x, win_y, maxWidth, maxTextWidth;
1540     unsigned int mask;
1541     char def[80], *p, *q;
1542     static char pane[6] = "paneX";
1543     Widget texts[100], forelast = NULL, anchor, widest;
1544
1545     // to do: start up second engine if needed
1546     if(!cps->initDone || !cps->nrOptions) return; // nothing to be done
1547     currentCps = cps;
1548
1549     if(cps->nrOptions > 50) width = 4; else if(cps->nrOptions>24) width = 2; else width = 1;
1550     height = cps->nrOptions / width + 1;
1551      i = 0;
1552     XtSetArg(args[i], XtNresizable, True); i++;
1553     SettingsShell = popup =
1554       XtCreatePopupShell(_("Settings Menu"), transientShellWidgetClass,
1555                          shellWidget, args, i);
1556     
1557     layout =
1558       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1559                             layoutArgs, XtNumber(layoutArgs));
1560   for(c=0; c<width; c++) {
1561     pane[4] = 'A'+c;
1562     form =
1563       XtCreateManagedWidget(pane, formWidgetClass, layout,
1564                             formArgs, XtNumber(formArgs));
1565     j=0;
1566     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1567     XtSetValues(form, args, j);
1568     leftMargin = form;
1569  
1570     last = widest = NULL; anchor = forelast;
1571     for(h=0; h<height; h++) {
1572         forelast = last;
1573         i = h + c*height;
1574         if(i >= cps->nrOptions) break;
1575         switch(cps->option[i].type) {
1576           case Spin:
1577             sprintf(def, "%d", cps->option[i].value);
1578           case TextBox:
1579             j=0;
1580             XtSetArg(args[j], XtNfromVert, last);  j++;
1581             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1582             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1583             texts[h] =
1584             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);   
1585             j=0;
1586             XtSetArg(args[j], XtNfromVert, last);  j++;
1587             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1588             XtSetArg(args[j], XtNborderWidth, 1); j++;
1589             XtSetArg(args[j], XtNwidth, cps->option[i].type == Spin ? 40 : 175); j++;
1590             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1591             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1592             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1593             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1594             XtSetArg(args[j], XtNresizable, True);  j++;
1595             XtSetArg(args[j], XtNstring, cps->option[i].type==Spin ? def : cps->option[i].textValue);  j++;
1596             edit = last;
1597             cps->option[i].handle = (void*)
1598                 (last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));   
1599             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1600             if(cps->option[i].type == TextBox) break;
1601
1602             // add increment and decrement controls for spin
1603             j=0;
1604             XtSetArg(args[j], XtNfromVert, edit);  j++;
1605             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1606             XtSetArg(args[j], XtNheight, 10);  j++;
1607             XtSetArg(args[j], XtNwidth, 20);  j++;
1608             edit = XtCreateManagedWidget("+", commandWidgetClass, form, args, j);
1609             XtAddCallback(edit, XtNcallback, SpinCallback,
1610                           (XtPointer)(intptr_t) i);
1611
1612             j=0;
1613             XtSetArg(args[j], XtNfromVert, edit);  j++;
1614             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1615             XtSetArg(args[j], XtNheight, 10);  j++;
1616             XtSetArg(args[j], XtNwidth, 20);  j++;
1617             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1618             XtAddCallback(last, XtNcallback, SpinCallback,
1619                           (XtPointer)(intptr_t) i);
1620             break;
1621           case CheckBox:
1622             j=0;
1623             XtSetArg(args[j], XtNfromVert, last);  j++;
1624             XtSetArg(args[j], XtNwidth, 10);  j++;
1625             XtSetArg(args[j], XtNheight, 10);  j++;
1626             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1627             cps->option[i].handle = (void*) 
1628                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));   
1629             j=0;
1630             XtSetArg(args[j], XtNfromVert, last);  j++;
1631             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1632             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1633             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1634             last = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1635             break;
1636           case SaveButton:
1637           case Button:
1638             j=0;
1639             XtSetArg(args[j], XtNfromVert, last);  j++;
1640             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1641             cps->option[i].handle = (void*) 
1642                 (dialog = last = XtCreateManagedWidget(cps->option[i].name, commandWidgetClass, form, args, j));   
1643             XtAddCallback(last, XtNcallback, SettingsCallback,
1644                           (XtPointer)(intptr_t) (cps->option[i].type == SaveButton));
1645             break;
1646           case ComboBox:
1647             j=0;
1648             XtSetArg(args[j], XtNfromVert, last);  j++;
1649             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1650             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1651             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1652
1653             j=0;
1654             XtSetArg(args[j], XtNfromVert, last);  j++;
1655             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1656             XtSetArg(args[j], XtNwidth, 100);  j++;
1657             XtSetArg(args[j], XtNmenuName, XtNewString(cps->option[i].name));  j++;
1658             XtSetArg(args[j], XtNlabel, ((char**)cps->option[i].textValue)[cps->option[i].value]);  j++;
1659             cps->option[i].handle = (void*) 
1660                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));   
1661             CreateComboPopup(last, cps->option[i].name, i, (char **) cps->option[i].textValue);
1662             values[i] = cps->option[i].value;
1663             break;
1664         }
1665     }
1666
1667     // make an attempt to align all spins and textbox controls
1668     maxWidth = maxTextWidth = 0;
1669     for(h=0; h<height; h++) {
1670         i = h + c*height;
1671         if(i >= cps->nrOptions) break;
1672         if(cps->option[i].type == Spin || cps->option[i].type == TextBox) {
1673             Dimension w;
1674             j=0;
1675             XtSetArg(args[j], XtNwidth, &w);  j++;
1676             XtGetValues(texts[h], args, j);
1677             if(cps->option[i].type == Spin) {
1678                 if(w > maxWidth) maxWidth = w;
1679                 widest = texts[h];
1680             } else {
1681                 if(w > maxTextWidth) maxTextWidth = w;
1682                 if(!widest) widest = texts[h];
1683             }
1684         }
1685     }
1686     if(maxTextWidth + 110 < maxWidth)
1687          maxTextWidth = maxWidth - 110;
1688     else maxWidth = maxTextWidth + 110;
1689     for(h=0; h<height; h++) {
1690         i = h + c*height;
1691         if(i >= cps->nrOptions) break;
1692         j=0;
1693         if(cps->option[i].type == Spin) {
1694             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1695             XtSetValues(texts[h], args, j);
1696         } else
1697         if(cps->option[i].type == TextBox) {
1698             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1699             XtSetValues(texts[h], args, j);
1700         }
1701     }
1702   }
1703     j=0;
1704     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1705     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1706     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1707     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1708     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1709     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1710     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
1711     XtAddCallback(b_ok, XtNcallback, SettingsCallback, (XtPointer) 0);
1712
1713     XtSetArg(args[j-1], XtNfromHoriz, b_ok);
1714     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);   
1715     XtAddCallback(b_cancel, XtNcallback, SettingsPopDown, (XtPointer) 0);
1716
1717     XtRealizeWidget(popup);
1718     CatchDeleteWindow(popup, "SettingsPopDown");
1719     
1720     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1721                   &x, &y, &win_x, &win_y, &mask);
1722     
1723     XtSetArg(args[0], XtNx, x - 10);
1724     XtSetArg(args[1], XtNy, y - 30);
1725     XtSetValues(popup, args, 2);
1726     
1727     XtPopup(popup, XtGrabExclusive);
1728     SettingsUp = True;
1729
1730     previous = NULL;
1731     if(edit)SetFocus(edit, popup, (XEvent*) NULL, False);
1732 }
1733
1734 void FirstSettingsProc(w, event, prms, nprms)
1735      Widget w;
1736      XEvent *event;
1737      String *prms;
1738      Cardinal *nprms;
1739 {
1740    SettingsPopUp(&first);
1741 }
1742
1743 void SecondSettingsProc(w, event, prms, nprms)
1744      Widget w;
1745      XEvent *event;
1746      String *prms;
1747      Cardinal *nprms;
1748 {
1749    SettingsPopUp(&second);
1750 }
1751
1752 //---------------------------- Chat Windows ----------------------------------------------
1753
1754 void OutputChatMessage(int partner, char *mess)
1755 {
1756     return; // dummy
1757 }
1758