new developer release
[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     XtSetKeyboardFocus((Widget) data, w);
109     previous = w;
110 }
111
112 //--------------------------- New Shuffle Game --------------------------------------------
113 extern int shuffleOpenings;
114 extern int startedFromPositionFile;
115 int shuffleUp;
116 Widget shuffleShell;
117
118 void ShufflePopDown()
119 {
120     if (!shuffleUp) return;
121     XtPopdown(shuffleShell);
122     XtDestroyWidget(shuffleShell);
123     shuffleUp = False;
124     ModeHighlight();
125 }
126
127 void ShuffleCallback(w, client_data, call_data)
128      Widget w;
129      XtPointer client_data, call_data;
130 {
131     String name;
132     Widget w2;
133     Arg args[16];
134     char buf[80];
135     
136     XtSetArg(args[0], XtNlabel, &name);
137     XtGetValues(w, args, 1);
138     
139     if (strcmp(name, _("cancel")) == 0) {
140         ShufflePopDown();
141         return;
142     }
143     if (strcmp(name, _("off")) == 0) {
144         ShufflePopDown();
145         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
146         ResetGameEvent();
147         return;
148     }
149     if (strcmp(name, _("random")) == 0) {
150         sprintf(buf, "%d", rand());
151         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
152         XtSetValues(XtParent(w), args, 1);
153         return;
154     }
155     if (strcmp(name, _("ok")) == 0) {
156         int nr; String name;
157         name = XawDialogGetValueString(w2 = XtParent(w));
158         if(sscanf(name ,"%d",&nr) != 1) {
159             sprintf(buf, "%d", appData.defaultFrcPosition);
160             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
161             XtSetValues(w2, args, 1);
162             return;
163         }
164         appData.defaultFrcPosition = nr;
165         shuffleOpenings = True;
166         ShufflePopDown();
167         ResetGameEvent();
168         return;
169     }
170 }
171
172 void ShufflePopUp()
173 {
174     Arg args[16];
175     Widget popup, layout, dialog, edit;
176     Window root, child;
177     int x, y, i;
178     int win_x, win_y;
179     unsigned int mask;
180     char def[80];
181     
182     i = 0;
183     XtSetArg(args[i], XtNresizable, True); i++;
184     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
185     shuffleShell = popup =
186       XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
187                          shellWidget, args, i);
188     
189     layout =
190       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
191                             layoutArgs, XtNumber(layoutArgs));
192   
193     sprintf(def, "%d\n", appData.defaultFrcPosition);
194     i = 0;
195     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
196     XtSetArg(args[i], XtNvalue, def); i++;
197     XtSetArg(args[i], XtNborderWidth, 0); i++;
198     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
199                                    layout, args, i);
200     
201 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
202 //    XtSetArg(args[1], XtNuseStringInPlace, False);
203 //    XtSetValues(dialog, args, 2);
204
205     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
206     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
207     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
208     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
209     
210     XtRealizeWidget(popup);
211     CatchDeleteWindow(popup, "ShufflePopDown");
212     
213     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
214                   &x, &y, &win_x, &win_y, &mask);
215     
216     XtSetArg(args[0], XtNx, x - 10);
217     XtSetArg(args[1], XtNy, y - 30);
218     XtSetValues(popup, args, 2);
219     
220     XtPopup(popup, XtGrabExclusive);
221     shuffleUp = True;
222     
223     edit = XtNameToWidget(dialog, "*value");
224
225     XtSetKeyboardFocus(popup, edit);
226 }
227
228 void ShuffleMenuProc(w, event, prms, nprms)
229      Widget w;
230      XEvent *event;
231      String *prms;
232      Cardinal *nprms;
233 {
234 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
235 //      Reset(FALSE, TRUE);
236 //    }
237     ShufflePopUp();
238 }
239
240 //--------------------------- Time-Control Menu Popup ----------------------------------
241 int TimeControlUp;
242 Widget TimeControlShell;
243 int tcInc;
244 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
245 int tcIncrement, tcMoves;
246
247 void TimeControlPopDown()
248 {
249     if (!TimeControlUp) return;
250     previous = NULL;
251     XtPopdown(TimeControlShell);
252     XtDestroyWidget(TimeControlShell);
253     TimeControlUp = False;
254     ModeHighlight();
255 }
256
257 void TimeControlCallback(w, client_data, call_data)
258      Widget w;
259      XtPointer client_data, call_data;
260 {
261     String name, txt;
262     Widget w2;
263     Arg args[16];
264     char buf[80];
265     int j;
266
267     XtSetArg(args[0], XtNlabel, &name);
268     XtGetValues(w, args, 1);
269     
270     if (strcmp(name, _("classical")) == 0) {
271         if(tcInc == 0) return;
272         j=0;
273         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
274         XtSetValues(tcMess1, args, j);
275         j=0;
276         XtSetArg(args[j], XtNlabel, _("moves")); j++;
277         XtSetValues(tcMess2, args, j);
278         if(tcInc == 1) {
279             j=0;
280             XtSetArg(args[j], XtNstring, &name); j++;
281             XtGetValues(tcData, args, j);
282             tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
283         }
284         sprintf(buf, "%d", tcMoves);
285         j=0;
286         XtSetArg(args[j], XtNstring, buf); j++;
287         XtSetValues(tcData, args, j);
288         tcInc = 0;
289         return;
290     }
291     if (strcmp(name, _("incremental")) == 0) {
292         if(tcInc == 1) return;
293         j=0;
294         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
295         XtSetValues(tcMess1, args, j);
296         j=0;
297         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
298         XtSetValues(tcMess2, args, j);
299         if(tcInc == 0) {
300             j=0;
301             XtSetArg(args[j], XtNstring, &name); j++;
302             XtGetValues(tcData, args, j);
303             tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
304         }
305         sprintf(buf, "%d", tcIncrement);
306         j=0;
307         XtSetArg(args[j], XtNstring, buf); j++;
308         XtSetValues(tcData, args, j);
309         tcInc = 1;
310         return;
311     }
312     if (strcmp(name, _("fixed time")) == 0) {
313         if(tcInc == 2) return;
314         j=0;
315         XtSetArg(args[j], XtNlabel, _("sec/move (max)")); j++;
316         XtSetValues(tcMess1, args, j);
317         j=0;
318         XtSetArg(args[j], XtNlabel, _("")); j++;
319         XtSetValues(tcMess2, args, j);
320         j=0;
321         XtSetArg(args[j], XtNstring, ""); j++;
322         XtSetValues(tcData, args, j);
323         tcInc = 2;
324         return;
325     }
326     if (strcmp(name, _(" OK ")) == 0) {
327         int inc, mps, tc, ok;
328         XtSetArg(args[0], XtNstring, &txt);
329         XtGetValues(tcData, args, 1);
330         switch(tcInc) {
331           case 1:
332             ok = sscanf(txt, "%d", &inc); mps = 0;
333             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
334             ok &= (inc >= 0);
335             break;
336           case 0:
337             ok = sscanf(txt, "%d", &mps); inc = -1;
338             ok &= (mps > 0);
339             break;
340           case 2:
341             ok = 1; inc = -1; mps = 40;
342         }
343         if(ok != 1) {
344             XtSetArg(args[0], XtNstring, ""); // erase any offending input
345             XtSetValues(tcData, args, 1);
346             return;
347         }
348         XtSetArg(args[0], XtNstring, &txt);
349         XtGetValues(tcTime, args, 1);
350         if(tcInc == 2) {
351             if(sscanf(txt, "%d", &inc) != 1) {
352                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
353                 XtSetValues(tcTime, args, 1);
354                 DisplayError(_("Bad Time-Control String"), 0);
355                 return;
356             }
357             searchTime = inc;
358         } else {
359             if(!ParseTimeControl(txt, inc, mps)) {
360                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
361                 XtSetValues(tcTime, args, 1);
362                 DisplayError(_("Bad Time-Control String"), 0);
363                 return;
364             }
365             searchTime = 0;
366             appData.movesPerSession = mps;
367             appData.timeIncrement = inc;
368             appData.timeControl = strdup(txt);
369         }
370         XtSetArg(args[0], XtNstring, &txt);
371         XtGetValues(tcOdds1, args, 1);
372         appData.firstTimeOdds = first.timeOdds 
373                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
374         XtGetValues(tcOdds2, args, 1);
375         appData.secondTimeOdds = second.timeOdds 
376                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
377
378         Reset(True, True);
379         TimeControlPopDown();
380         return;
381     }
382 }
383
384 void TimeControlPopUp()
385 {
386     Arg args[16];
387     Widget popup, layout, form, edit, b_ok, b_cancel, b_clas, b_inc, mess;
388     Window root, child;
389     int x, y, i, j;
390     int win_x, win_y;
391     unsigned int mask;
392     char def[80];
393     
394     tcInc = searchTime > 0 ? 2 : (appData.timeIncrement >= 0);
395     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
396     if(!tcInc) tcIncrement = 0;
397     sprintf(def, "%d", tcInc ? tcIncrement : tcMoves);
398
399     i = 0;
400     XtSetArg(args[i], XtNresizable, True); i++;
401 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
402     TimeControlShell = popup =
403       XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
404                          shellWidget, args, i);
405     
406     layout =
407       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
408                             layoutArgs, XtNumber(layoutArgs));
409   
410     form =
411       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
412                             formArgs, XtNumber(formArgs));
413   
414     j = 0;
415 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
416 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
417     XtSetValues(popup, args, j);
418
419     j= 0;
420     XtSetArg(args[j], XtNborderWidth, 1); j++;
421     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
422     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
423     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
424     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
425     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
426     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
427     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
428     XtSetArg(args[j], XtNright, XtChainRight);  j++;
429     XtSetArg(args[j], XtNresizable, True);  j++;
430     XtSetArg(args[j], XtNwidth,  85);  j++;
431     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
432     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
433     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
434
435     j= 0;
436     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("sec/move (max)   ") : _("   minutes, plus   ") : _("minutes for each")); j++;
437     XtSetArg(args[j], XtNborderWidth, 0); j++;
438     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
439     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
440     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
441     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
442     XtSetArg(args[j], XtNright, XtChainRight);  j++;
443   //  XtSetArg(args[j], XtNwidth,  100);  j++;
444   //  XtSetArg(args[j], XtNheight, 20);  j++;
445     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
446
447     j= 0;
448     XtSetArg(args[j], XtNborderWidth, 1); j++;
449     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
450     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
451     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
452     XtSetArg(args[j], XtNstring, def);  j++;
453     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
454     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
455     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
456     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
457     XtSetArg(args[j], XtNright, XtChainRight);  j++;
458     XtSetArg(args[j], XtNresizable, True);  j++;
459     XtSetArg(args[j], XtNwidth,  40);  j++;
460 //    XtSetArg(args[j], XtNheight, 20);  j++;
461     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
462     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
463
464     j= 0;
465     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("             ") : _("sec/move") : _("moves     ")); j++;
466     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
467     XtSetArg(args[j], XtNborderWidth, 0); j++;
468     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
469     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
470     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
471     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
472     XtSetArg(args[j], XtNright, XtChainRight);  j++;
473 //    XtSetArg(args[j], XtNwidth,  80);  j++;
474 //    XtSetArg(args[j], XtNheight, 20);  j++;
475     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
476                                    form, args, j);
477
478     j= 0;
479     XtSetArg(args[j], XtNborderWidth, 1); j++;
480     XtSetArg(args[j], XtNfromVert, tcTime); j++;
481     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
482     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
483     XtSetArg(args[j], XtNstring, "1");  j++;
484     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
485     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
486     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
487     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
488     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
489     XtSetArg(args[j], XtNresizable, True);  j++;
490     XtSetArg(args[j], XtNwidth,  40);  j++;
491 //    XtSetArg(args[j], XtNheight, 20);  j++;
492     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
493     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
494
495     j= 0;
496     XtSetArg(args[j], XtNborderWidth, 1); j++;
497     XtSetArg(args[j], XtNfromVert, tcTime); j++;
498     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
499     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
500     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
501     XtSetArg(args[j], XtNstring, "1");  j++;
502     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
503     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
504     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
505     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
506     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
507     XtSetArg(args[j], XtNresizable, True);  j++;
508     XtSetArg(args[j], XtNwidth,  40);  j++;
509 //    XtSetArg(args[j], XtNheight, 20);  j++;
510     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
511     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
512
513     j= 0;
514     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
515     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
516     XtSetArg(args[j], XtNborderWidth, 0); j++;
517     XtSetArg(args[j], XtNfromVert, tcTime); j++;
518     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
519     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
520     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
521     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
522     XtSetArg(args[j], XtNright, XtChainRight);  j++;
523 //    XtSetArg(args[j], XtNwidth,  200);  j++;
524 //    XtSetArg(args[j], XtNheight, 20);  j++;
525     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
526                                    form, args, j);
527     j=0;
528     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
529     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
530     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
531     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
532     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
533     XtSetArg(args[j], XtNstate, tcInc==0); j++;
534     b_clas= XtCreateManagedWidget(_("classical"), toggleWidgetClass,
535                                    form, args, j);   
536     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
537
538     j=0;
539     XtSetArg(args[j], XtNradioGroup, b_clas); j++;
540     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
541     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
542     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
543     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
544     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
545     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
546     XtSetArg(args[j], XtNstate, tcInc==1); j++;
547     b_inc = XtCreateManagedWidget(_("incremental"), toggleWidgetClass,
548                                    form, args, j);   
549     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
550
551     j=0;
552     XtSetArg(args[j], XtNradioGroup, b_inc); j++;
553     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
554     XtSetArg(args[j], XtNfromHoriz, b_inc);  j++;
555     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
556     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
557     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
558     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
559     XtSetArg(args[j], XtNstate, tcInc==2); j++;
560     b_inc = XtCreateManagedWidget(_("fixed time"), toggleWidgetClass,
561                                    form, args, j);   
562     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
563
564     j=0;
565     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
566     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
567     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
568     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
569     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
570     XtSetArg(args[j], XtNright, XtChainRight);  j++;
571     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
572                                    form, args, j);   
573     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
574
575     j=0;
576     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
577     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
578     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
579     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
580     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
581     XtSetArg(args[j], XtNright, XtChainRight);  j++;
582     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
583                                    form, args, j);   
584     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
585
586     XtRealizeWidget(popup);
587     CatchDeleteWindow(popup, "TimeControlPopDown");
588     
589     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
590                   &x, &y, &win_x, &win_y, &mask);
591     
592     XtSetArg(args[0], XtNx, x - 10);
593     XtSetArg(args[1], XtNy, y - 30);
594     XtSetValues(popup, args, 2);
595     
596     XtPopup(popup, XtGrabExclusive);
597     TimeControlUp = True;
598     
599     previous = NULL;
600     SetFocus(tcTime, popup, (XEvent*) NULL, False);
601 //    XtSetKeyboardFocus(popup, tcTime);
602 }
603
604 void TimeControlProc(w, event, prms, nprms)
605      Widget w;
606      XEvent *event;
607      String *prms;
608      Cardinal *nprms;
609 {
610    TimeControlPopUp();
611 }
612
613 //--------------------------- Engine-Options Menu Popup ----------------------------------
614 int EngineUp;
615 Widget EngineShell;
616 extern int adjudicateLossThreshold;
617
618 Widget engDrawMoves, engThreshold, engRule, engRepeat;
619
620 void EnginePopDown()
621 {
622     if (!EngineUp) return;
623     previous = NULL;
624     XtPopdown(EngineShell);
625     XtDestroyWidget(EngineShell);
626     EngineUp = False;
627     ModeHighlight();
628 }
629
630 int ReadToggle(Widget w)
631 {
632     Arg args; Boolean res;
633
634     XtSetArg(args, XtNstate, &res);
635     XtGetValues(w, &args, 1);
636
637     return res;
638 }
639
640 Widget w1, w2, w3, w4, w5, w6, w7, w8;
641
642 void EngineCallback(w, client_data, call_data)
643      Widget w;
644      XtPointer client_data, call_data;
645 {
646     String name;
647     Widget s2;
648     Arg args[16];
649     char buf[80];
650     int j;
651     
652     XtSetArg(args[0], XtNlabel, &name);
653     XtGetValues(w, args, 1);
654     
655     if (strcmp(name, _("OK")) == 0) {
656         // read all switches
657         appData.periodicUpdates = ReadToggle(w1);
658 //      appData.hideThinkingFromHuman = ReadToggle(w2);
659         first.scoreIsAbsolute  = appData.firstScoreIsAbsolute  = ReadToggle(w3);
660         second.scoreIsAbsolute = appData.secondScoreIsAbsolute = ReadToggle(w4);
661         appData.testClaims    = ReadToggle(w5);
662         appData.checkMates    = ReadToggle(w6);
663         appData.materialDraws = ReadToggle(w7);
664         appData.trivialDraws  = ReadToggle(w8);
665
666         // adjust setting in other menu for duplicates 
667         // (perhaps duplicates should be removed from general Option Menu?)
668 //      XtSetArg(args[0], XtNleftBitmap, appData.showThinking ? xMarkPixmap : None);
669 //      XtSetValues(XtNameToWidget(menuBarWidget,
670 //                                 "menuOptions.Show Thinking"), args, 1);
671
672         // read out numeric controls, simply ignore bad formats for now
673         XtSetArg(args[0], XtNstring, &name);
674         XtGetValues(engDrawMoves, args, 1);
675         if(sscanf(name, "%d", &j) == 1) appData.adjudicateDrawMoves = j;
676         XtGetValues(engThreshold, args, 1);
677         if(sscanf(name, "%d", &j) == 1) 
678                 adjudicateLossThreshold = appData.adjudicateLossThreshold = -j; // inverted!
679         XtGetValues(engRule, args, 1);
680         if(sscanf(name, "%d", &j) == 1) appData.ruleMoves = j;
681         XtGetValues(engRepeat, args, 1);
682         if(sscanf(name, "%d", &j) == 1) appData.drawRepeats = j;
683
684         EnginePopDown();
685         ShowThinkingEvent(); // [HGM] thinking: score adjudication might need thinking output
686         return;
687     }
688 }
689
690 void EnginePopUp()
691 {
692     Arg args[16];
693     Widget popup, layout, form, edit, b_ok, b_cancel, b_clas, b_inc, s1; 
694     Window root, child;
695     int x, y, i, j, width;
696     int win_x, win_y;
697     unsigned int mask;
698     char def[80];
699     
700     tcInc = (appData.timeIncrement >= 0);
701     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
702     if(!tcInc) tcIncrement = 0;
703     sprintf(def, "%d", tcInc ? tcIncrement : tcMoves);
704
705     i = 0;
706     XtSetArg(args[i], XtNresizable, True); i++;
707 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
708     EngineShell = popup =
709       XtCreatePopupShell(_("Adjudications"), transientShellWidgetClass,
710                          shellWidget, args, i);
711     
712     layout =
713       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
714                             layoutArgs, XtNumber(layoutArgs));
715   
716     form =
717       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
718                             formArgs, XtNumber(formArgs));
719   
720     j = 0;
721 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 250); j++;
722 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 400); j++;
723 //    XtSetValues(popup, args, j);
724
725     j = 0;
726 //    XtSetArg(args[j], XtNwidth,       (XtArgVal) 250); j++;
727 //    XtSetArg(args[j], XtNheight,      (XtArgVal) 20); j++;
728     XtSetArg(args[j], XtNleft,        (XtArgVal) XtChainLeft); j++;
729     XtSetArg(args[j], XtNright,       (XtArgVal) XtChainRight); j++;
730     XtSetArg(args[j], XtNstate,       appData.periodicUpdates); j++;
731 //    XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
732     w1 = XtCreateManagedWidget(_("Periodic Updates (Analysis Mode)"), toggleWidgetClass, form, args, j);
733
734     XtSetArg(args[j], XtNwidth,       (XtArgVal) &width);
735     XtGetValues(w1, &args[j], 1);
736
737 //    XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w1);
738 //    XtSetArg(args[j-3], XtNstate,       appData.hideThinkingFromHuman);
739 //    w2 = XtCreateManagedWidget(_("Hide Thinking from Human"), toggleWidgetClass, form, args, j);
740
741     XtSetArg(args[j], XtNwidth,       (XtArgVal) width); j++;
742     XtSetArg(args[j-2], XtNstate,     appData.firstScoreIsAbsolute);
743     XtSetArg(args[j], XtNfromVert,    (XtArgVal) w1); j++;
744     w3 = XtCreateManagedWidget(_("Engine #1 Score is Absolute"), toggleWidgetClass, form, args, j);
745
746     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w3);
747     XtSetArg(args[j-3], XtNstate,       appData.secondScoreIsAbsolute);
748     w4 = XtCreateManagedWidget(_("Engine #2 Score is Absolute"), toggleWidgetClass, form, args, j);
749
750     s1 = XtCreateManagedWidget(_("\nAdjudications in non-ICS games:"), labelWidgetClass, form, args, 3);
751
752     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) s1);
753     XtSetArg(args[j-3], XtNstate,       appData.testClaims);
754     w5 = XtCreateManagedWidget(_("Verify Engine Result Claims"), toggleWidgetClass, form, args, j);
755
756     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w5);
757     XtSetArg(args[j-3], XtNstate,       appData.checkMates);
758     w6 = XtCreateManagedWidget(_("Detect All Mates"), toggleWidgetClass, form, args, j);
759
760     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w6);
761     XtSetArg(args[j-3], XtNstate,       appData.materialDraws);
762     w7 = XtCreateManagedWidget(_("Draw when Insuff. Mating Material"), toggleWidgetClass, form, args, j);
763
764     XtSetArg(args[j-1], XtNfromVert,  (XtArgVal) w7);
765     XtSetArg(args[j-3], XtNstate,       appData.trivialDraws);
766     w8 = XtCreateManagedWidget(_("Adjudicate Trivial Draws"), toggleWidgetClass, form, args, j);
767
768     XtSetArg(args[0], XtNfromVert,  (XtArgVal) w4);
769     XtSetArg(args[1], XtNborderWidth, (XtArgVal) 0);
770     XtSetValues(s1, args, 2);
771
772     sprintf(def, "%d", appData.adjudicateDrawMoves);
773     j= 0;
774     XtSetArg(args[j], XtNborderWidth, 1); j++;
775     XtSetArg(args[j], XtNfromVert, w8); j++;
776     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
777     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
778     XtSetArg(args[j], XtNstring, def);  j++;
779     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
780     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
781     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
782     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
783     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
784     XtSetArg(args[j], XtNresizable, True);  j++;
785     XtSetArg(args[j], XtNwidth,  60);  j++;
786 //    XtSetArg(args[j], XtNheight, 20);  j++;
787     engDrawMoves = XtCreateManagedWidget("Length", asciiTextWidgetClass, form, args, j);
788     XtAddEventHandler(engDrawMoves, ButtonPressMask, False, SetFocus, (XtPointer) popup);
789
790     j= 0;
791     XtSetArg(args[j], XtNlabel, _(" moves maximum, then draw")); j++;
792     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
793     XtSetArg(args[j], XtNborderWidth, 0); j++;
794     XtSetArg(args[j], XtNfromVert, w8); j++;
795     XtSetArg(args[j], XtNfromHoriz, engDrawMoves); j++;
796     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
797     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
798     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
799     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
800 //    XtSetArg(args[j], XtNwidth,  170);  j++;
801 //    XtSetArg(args[j], XtNheight, 20);  j++;
802     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
803
804     sprintf(def, "%d", -appData.adjudicateLossThreshold); // inverted!
805     j= 0;
806     XtSetArg(args[j], XtNborderWidth, 1); j++;
807     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
808     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
809     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
810     XtSetArg(args[j], XtNstring, def);  j++;
811     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
812     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
813     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
814     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
815     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
816     XtSetArg(args[j], XtNresizable, True);  j++;
817     XtSetArg(args[j], XtNwidth,  60);  j++;
818     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
819     engThreshold = XtCreateManagedWidget("Threshold", asciiTextWidgetClass, form, args, j);
820     XtAddEventHandler(engThreshold, ButtonPressMask, False, SetFocus, (XtPointer) popup);
821
822     j= 0;
823     XtSetArg(args[j], XtNlabel, _("-centiPawn lead is win")); j++;
824     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
825     XtSetArg(args[j], XtNborderWidth, 0); j++;
826     XtSetArg(args[j], XtNfromVert, engDrawMoves); j++;
827     XtSetArg(args[j], XtNfromHoriz, engThreshold); j++;
828     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
829     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
830     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
831     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
832 //    XtSetArg(args[j], XtNwidth,  150);  j++;
833 //    XtSetArg(args[j], XtNheight, 20);  j++;
834     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
835
836     sprintf(def, "%d", appData.ruleMoves);
837     j= 0;
838     XtSetArg(args[j], XtNborderWidth, 1); j++;
839     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
840     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
841     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
842     XtSetArg(args[j], XtNstring, def);  j++;
843     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
844     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
845     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
846     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
847     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
848     XtSetArg(args[j], XtNresizable, True);  j++;
849     XtSetArg(args[j], XtNwidth,  30);  j++;
850 //    XtSetArg(args[j], XtNheight, 20);  j++;
851     engRule = XtCreateManagedWidget("Rule", asciiTextWidgetClass, form, args, j);
852     XtAddEventHandler(engRule, ButtonPressMask, False, SetFocus, (XtPointer) popup);
853
854     j= 0;
855     XtSetArg(args[j], XtNlabel, _("-move rule applied")); j++;
856     XtSetArg(args[j], XtNjustify,     (XtArgVal) XtJustifyLeft); j++;
857     XtSetArg(args[j], XtNborderWidth, 0); j++;
858     XtSetArg(args[j], XtNfromVert, engThreshold); j++;
859     XtSetArg(args[j], XtNfromHoriz, engRule); j++;
860     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
861     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
862     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
863     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
864 //    XtSetArg(args[j], XtNwidth,  130);  j++;
865 //    XtSetArg(args[j], XtNheight, 20);  j++;
866     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
867
868     sprintf(def, "%d", appData.drawRepeats);
869     j= 0;
870     XtSetArg(args[j], XtNborderWidth, 1); j++;
871     XtSetArg(args[j], XtNfromVert, engRule); j++;
872     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
873     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
874     XtSetArg(args[j], XtNstring, def);  j++;
875     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
876     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
877     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
878     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
879     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
880     XtSetArg(args[j], XtNresizable, True);  j++;
881     XtSetArg(args[j], XtNwidth,  30);  j++;
882 //    XtSetArg(args[j], XtNheight, 20);  j++;
883     engRepeat = XtCreateManagedWidget("Repeats", asciiTextWidgetClass, form, args, j);
884     XtAddEventHandler(engRepeat, ButtonPressMask, False, SetFocus, (XtPointer) popup);
885
886     j= 0;
887     XtSetArg(args[j], XtNlabel, _("-fold repeat is draw")); j++;
888     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
889     XtSetArg(args[j], XtNborderWidth, 0); j++;
890     XtSetArg(args[j], XtNfromVert, engRule); j++;
891     XtSetArg(args[j], XtNfromHoriz, engRepeat); j++;
892     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
893     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
894     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
895     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
896 //    XtSetArg(args[j], XtNwidth,  130);  j++;
897 //    XtSetArg(args[j], XtNheight, 20);  j++;
898     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass, form, args, j);
899
900     j=0;
901     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
902     XtSetArg(args[j], XtNfromHoriz, tcMess2);  j++;
903     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
904     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
905     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
906     XtSetArg(args[j], XtNright, XtChainRight);  j++;
907     b_ok= XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
908     XtAddCallback(b_ok, XtNcallback, EngineCallback, (XtPointer) 0);
909
910     j=0;
911     XtSetArg(args[j], XtNfromVert, engRepeat);  j++;
912     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
913     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
914     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
915     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
916     XtSetArg(args[j], XtNright, XtChainRight);  j++;
917     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
918                                    form, args, j);   
919     XtAddCallback(b_cancel, XtNcallback, EnginePopDown, (XtPointer) 0);
920
921     XtRealizeWidget(popup);
922     CatchDeleteWindow(popup, "EnginePopDown");
923     
924     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
925                   &x, &y, &win_x, &win_y, &mask);
926     
927     XtSetArg(args[0], XtNx, x - 10);
928     XtSetArg(args[1], XtNy, y - 30);
929     XtSetValues(popup, args, 2);
930     
931     XtPopup(popup, XtGrabExclusive);
932     EngineUp = True;
933     
934     previous = NULL;
935     SetFocus(engThreshold, popup, (XEvent*) NULL, False);
936 }
937
938 void EngineMenuProc(w, event, prms, nprms)
939      Widget w;
940      XEvent *event;
941      String *prms;
942      Cardinal *nprms;
943 {
944    EnginePopUp();
945 }
946
947 //--------------------------- New-Variant Menu PopUp -----------------------------------
948 struct NewVarButton {
949   char   *name;
950   char *color;
951   Widget handle;
952   VariantClass variant;
953 };
954
955 struct NewVarButton buttonDesc[] = {
956     {N_("normal"),            "#FFFFFF", 0, VariantNormal},
957     {N_("FRC"),               "#FFFFFF", 0, VariantFischeRandom},
958     {N_("wild castle"),       "#FFFFFF", 0, VariantWildCastle},
959     {N_("no castle"),         "#FFFFFF", 0, VariantNoCastle},
960     {N_("knightmate"),        "#FFFFFF", 0, VariantKnightmate},
961     {N_("berolina"),          "#FFFFFF", 0, VariantBerolina},
962     {N_("cylinder"),          "#FFFFFF", 0, VariantCylinder},
963     {N_("shatranj"),          "#FFFFFF", 0, VariantShatranj},
964     {N_("makruk"),            "#FFFFFF", 0, VariantMakruk},
965     {N_("atomic"),            "#FFFFFF", 0, VariantAtomic},
966     {N_("two kings"),         "#FFFFFF", 0, VariantTwoKings},
967     {N_("3-checks"),          "#FFFFFF", 0, Variant3Check},
968     {N_("suicide"),           "#FFFFBF", 0, VariantSuicide},
969     {N_("give-away"),         "#FFFFBF", 0, VariantGiveaway},
970     {N_("losers"),            "#FFFFBF", 0, VariantLosers},
971     {N_("fairy"),             "#BFBFBF", 0, VariantFairy},
972     {N_("Superchess"),        "#FFBFBF", 0, VariantSuper},
973     {N_("crazyhouse"),        "#FFBFBF", 0, VariantCrazyhouse},
974     {N_("bughouse"),          "#FFBFBF", 0, VariantBughouse},
975     {N_("shogi (9x9)"),       "#BFFFFF", 0, VariantShogi},
976     {N_("xiangqi (9x10)"),    "#BFFFFF", 0, VariantXiangqi},
977     {N_("courier (12x8)"),    "#BFFFBF", 0, VariantCourier},
978     {N_("janus (10x8)"),      "#BFBFFF", 0, VariantJanus},
979     {N_("Capablanca (10x8)"), "#BFBFFF", 0, VariantCapablanca},
980     {N_("CRC (10x8)"),        "#BFBFFF", 0, VariantCapaRandom},
981 #ifdef GOTHIC
982     {N_("Gothic (10x8)"),     "#BFBFFF", 0, VariantGothic},
983 #endif
984 #ifdef FALCON
985     {N_("Falcon (10x8)"),     "#BFBFFF", 0, VariantFalcon},
986 #endif
987     {NULL,                0, 0, (VariantClass) 0}
988 };
989
990 int NewVariantUp;
991 Widget NewVariantShell;
992
993 void NewVariantPopDown()
994 {
995     if (!NewVariantUp) return;
996     XtPopdown(NewVariantShell);
997     XtDestroyWidget(NewVariantShell);
998     NewVariantUp = False;
999     ModeHighlight();
1000 }
1001
1002 void NewVariantCallback(w, client_data, call_data)
1003      Widget w;
1004      XtPointer client_data, call_data;
1005 {
1006     String name;
1007     Widget w2;
1008     Arg args[16];
1009     char buf[80];
1010     VariantClass v;
1011     
1012     XtSetArg(args[0], XtNlabel, &name);
1013     XtGetValues(w, args, 1);
1014     
1015     if (strcmp(name, _("  OK  ")) == 0) {
1016         int nr = (intptr_t) XawToggleGetCurrent(buttonDesc[0].handle) - 1;
1017         if(nr < 0) return;
1018         v = buttonDesc[nr].variant;
1019         if(!appData.noChessProgram) { 
1020             char *name = VariantName(v), buf[MSG_SIZ];
1021             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
1022                 /* [HGM] in protocol 2 we check if variant is suported by engine */
1023                 sprintf(buf, _("Variant %s not supported by %s"), name, first.tidy);
1024                 DisplayError(buf, 0);
1025 //              NewVariantPopDown();
1026                 return; /* ignore OK if first engine does not support it */
1027             } else
1028             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
1029                 sprintf(buf, _("Warning: second engine (%s) does not support this!"), second.tidy);
1030                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
1031             }
1032         }
1033
1034         gameInfo.variant = v;
1035         appData.variant = VariantName(v);
1036
1037         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
1038         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
1039         appData.pieceToCharTable = NULL;
1040         Reset(True, True);
1041         NewVariantPopDown();
1042         return;
1043     }
1044 }
1045
1046 void NewVariantPopUp()
1047 {
1048     Arg args[16];
1049     Widget popup, layout, dialog, edit, form, last = NULL, b_ok, b_cancel;
1050     Window root, child;
1051     int x, y, i, j;
1052     int win_x, win_y;
1053     unsigned int mask;
1054     char def[80];
1055     XrmValue vFrom, vTo;
1056
1057     i = 0;
1058     XtSetArg(args[i], XtNresizable, True); i++;
1059 //    XtSetArg(args[i], XtNwidth, 250); i++;
1060 //    XtSetArg(args[i], XtNheight, 300); i++;
1061     NewVariantShell = popup =
1062       XtCreatePopupShell(_("NewVariant Menu"), transientShellWidgetClass,
1063                          shellWidget, args, i);
1064     
1065     layout =
1066       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1067                             layoutArgs, XtNumber(layoutArgs));
1068   
1069     form =
1070       XtCreateManagedWidget("form", formWidgetClass, layout,
1071                             formArgs, XtNumber(formArgs));
1072   
1073     for(i = 0; buttonDesc[i].name != NULL; i++) {
1074         Pixel buttonColor;
1075         if (!appData.monoMode) {
1076             vFrom.addr = (caddr_t) buttonDesc[i].color;
1077             vFrom.size = strlen(buttonDesc[i].color);
1078             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1079             if (vTo.addr == NULL) {
1080                 buttonColor = (Pixel) -1;
1081             } else {
1082                 buttonColor = *(Pixel *) vTo.addr;
1083             }
1084         }
1085     
1086         j = 0;
1087         XtSetArg(args[j], XtNradioGroup, last); j++;
1088         XtSetArg(args[j], XtNwidth, 125); j++;
1089 //      XtSetArg(args[j], XtNheight, 16); j++;
1090         XtSetArg(args[j], XtNfromVert, i == 15 ? NULL : last); j++;
1091         XtSetArg(args[j], XtNfromHoriz, i < 15 ? NULL : buttonDesc[i-15].handle); j++;
1092         XtSetArg(args[j], XtNradioData, i+1); j++;
1093         XtSetArg(args[j], XtNbackground, buttonColor); j++;
1094         XtSetArg(args[j], XtNstate, gameInfo.variant == buttonDesc[i].variant); j++;
1095         buttonDesc[i].handle = last =
1096             XtCreateManagedWidget(buttonDesc[i].name, toggleWidgetClass, form, args, j);
1097     }
1098
1099     j=0;
1100     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1101     XtSetArg(args[j], XtNfromHoriz, buttonDesc[12].handle);  j++;
1102     XtSetArg(args[j], XtNheight, 35); j++;
1103 //    XtSetArg(args[j], XtNwidth, 60); j++;
1104     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1105     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1106     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1107     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1108     b_cancel= XtCreateManagedWidget(_("CANCEL"), commandWidgetClass, form, args, j);   
1109     XtAddCallback(b_cancel, XtNcallback, NewVariantPopDown, (XtPointer) 0);
1110
1111     j=0;
1112     XtSetArg(args[j], XtNfromHoriz, b_cancel);  j++;
1113     XtSetArg(args[j], XtNfromVert, buttonDesc[12].handle);  j++;
1114     XtSetArg(args[j], XtNheight, 35); j++;
1115 //    XtSetArg(args[j], XtNwidth, 60); j++;
1116     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1117     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1118     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1119     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1120     b_ok= XtCreateManagedWidget(_("  OK  "), commandWidgetClass, form, args, j);   
1121     XtAddCallback(b_ok, XtNcallback, NewVariantCallback, (XtPointer) 0);
1122
1123     j=0;
1124     XtSetArg(args[j], XtNfromVert, buttonDesc[14].handle);  j++;
1125 //    XtSetArg(args[j], XtNheight, 70); j++;
1126     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1127     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1128     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1129     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1130     XtSetArg(args[j], XtNlabel, _("WARNING: variants with un-orthodox\n"
1131                                   "pieces only have built-in bitmaps\n"
1132                                   "for -boardSize middling, bulky and\n"
1133                                   "petite, and substitute king or amazon\n"
1134                                   "for missing bitmaps. (See manual.)")); j++;
1135     XtCreateManagedWidget("warning", labelWidgetClass, form, args, j);
1136
1137             XtRealizeWidget(popup);
1138     CatchDeleteWindow(popup, "NewVariantPopDown");
1139     
1140     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1141                   &x, &y, &win_x, &win_y, &mask);
1142     
1143     XtSetArg(args[0], XtNx, x - 10);
1144     XtSetArg(args[1], XtNy, y - 30);
1145     XtSetValues(popup, args, 2);
1146     
1147     XtPopup(popup, XtGrabExclusive);
1148     NewVariantUp = True;
1149 }
1150
1151 void NewVariantProc(w, event, prms, nprms)
1152      Widget w;
1153      XEvent *event;
1154      String *prms;
1155      Cardinal *nprms;
1156 {
1157    NewVariantPopUp();
1158 }
1159
1160 //--------------------------- UCI Menu Popup ------------------------------------------
1161 int UciUp;
1162 Widget UciShell;
1163
1164 struct UciControl {
1165   char *name;
1166   Widget handle;
1167   void *ptr;
1168 };
1169
1170 struct UciControl controlDesc[] = {
1171   {N_("maximum nr of CPUs:"), 0, &appData.smpCores},
1172   {N_("Polyglot Directory:"), 0, &appData.polyglotDir},
1173   {N_("Hash Size (MB):"),     0, &appData.defaultHashSize},
1174   {N_("EGTB Path:"),          0, &appData.defaultPathEGTB},
1175   {N_("EGTB Cache (MB):"),    0, &appData.defaultCacheSizeEGTB},
1176   {N_("Polyglot Book:"),      0, &appData.polyglotBook},
1177   {NULL, 0, NULL},
1178 };
1179
1180 void UciPopDown()
1181 {
1182     if (!UciUp) return;
1183     previous = NULL;
1184     XtPopdown(UciShell);
1185     XtDestroyWidget(UciShell);
1186     UciUp = False;
1187     ModeHighlight();
1188 }
1189
1190 void UciCallback(w, client_data, call_data)
1191      Widget w;
1192      XtPointer client_data, call_data;
1193 {
1194     String name;
1195     Arg args[16];
1196     char buf[80];
1197     int oldCores = appData.smpCores, ponder = 0;
1198     
1199     XtSetArg(args[0], XtNlabel, &name);
1200     XtGetValues(w, args, 1);
1201     
1202     if (strcmp(name, _("OK")) == 0) {
1203         int nr, i, j; String name;
1204         for(i=0; i<6; i++) {
1205             XtSetArg(args[0], XtNstring, &name);
1206             XtGetValues(controlDesc[i].handle, args, 1);
1207             if(i&1) {
1208                 if(name)
1209                     *(char**) controlDesc[i].ptr = strdup(name);
1210             } else {
1211                 if(sscanf(name, "%d", &j) == 1) 
1212                     *(int*) controlDesc[i].ptr = j;
1213             }
1214         }
1215         XtSetArg(args[0], XtNstate, &appData.usePolyglotBook);
1216         XtGetValues(w1, args, 1);
1217         XtSetArg(args[0], XtNstate, &appData.firstHasOwnBookUCI);
1218         XtGetValues(w2, args, 1);
1219         XtSetArg(args[0], XtNstate, &appData.secondHasOwnBookUCI);
1220         XtGetValues(w3, args, 1);
1221         XtSetArg(args[0], XtNstate, &ponder);
1222         XtGetValues(w4, args, 1);
1223
1224         // adjust setting in other menu for duplicates 
1225         // (perhaps duplicates should be removed from general Option Menu?)
1226         XtSetArg(args[0], XtNleftBitmap, ponder ? xMarkPixmap : None);
1227         XtSetValues(XtNameToWidget(menuBarWidget,
1228                                    "menuOptions.Ponder Next Move"), args, 1);
1229
1230         // make sure changes are sent to first engine by re-initializing it
1231         // if it was already started pre-emptively at end of previous game
1232         if(gameMode == BeginningOfGame) Reset(True, True); else {
1233             // Some changed setting need immediate sending always.
1234             PonderNextMoveEvent(ponder);
1235             if(oldCores != appData.smpCores)
1236                 NewSettingEvent(False, "cores", appData.smpCores);
1237       }
1238       UciPopDown();
1239       return;
1240     }
1241 }
1242
1243 void UciPopUp()
1244 {
1245     Arg args[16];
1246     Widget popup, layout, dialog, edit, form, b_ok, b_cancel, last = NULL, new, upperLeft;
1247     Window root, child;
1248     int x, y, i, j;
1249     int win_x, win_y;
1250     unsigned int mask;
1251     char def[80];
1252     
1253     i = 0;
1254     XtSetArg(args[i], XtNresizable, True); i++;
1255 //    XtSetArg(args[i], XtNwidth, 300); i++;
1256     UciShell = popup =
1257       XtCreatePopupShell(_("Engine Settings"), transientShellWidgetClass,
1258                          shellWidget, args, i);
1259     
1260     layout =
1261       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1262                             layoutArgs, XtNumber(layoutArgs));
1263   
1264     
1265     form =
1266       XtCreateManagedWidget("form", formWidgetClass, layout,
1267                             formArgs, XtNumber(formArgs));
1268   
1269     j = 0;
1270     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1271     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1272     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1273 //    XtSetArg(args[j], XtNheight, 20); j++;
1274     for(i = 0; controlDesc[i].name != NULL; i++) {
1275         j = 3;
1276         XtSetArg(args[j], XtNfromVert, last); j++;
1277 //      XtSetArg(args[j], XtNwidth, 130); j++;
1278         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1279         XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1280         XtSetArg(args[j], XtNborderWidth, 0); j++;
1281         new = XtCreateManagedWidget(controlDesc[i].name, labelWidgetClass, form, args, j);
1282         if(i==0) upperLeft = new;
1283
1284         j = 4;
1285         XtSetArg(args[j], XtNborderWidth, 1); j++;
1286         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1287         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1288         XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1289         XtSetArg(args[j], XtNright, XtChainRight);  j++;
1290         XtSetArg(args[j], XtNresizable, True);  j++;
1291         XtSetArg(args[j], XtNwidth, i&1 ? 245 : 50); j++;
1292         XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1293         if(i&1) {
1294             XtSetArg(args[j], XtNstring, * (char**) controlDesc[i].ptr ? 
1295                                          * (char**) controlDesc[i].ptr : ""); j++;
1296         } else {
1297             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, textField = 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             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1597             edit = last;
1598             cps->option[i].handle = (void*)
1599                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));   
1600             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1601             if(cps->option[i].type == TextBox) break;
1602
1603             // add increment and decrement controls for spin
1604             j=0;
1605             XtSetArg(args[j], XtNfromVert, edit);  j++;
1606             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1607             XtSetArg(args[j], XtNheight, 10);  j++;
1608             XtSetArg(args[j], XtNwidth, 20);  j++;
1609             edit = XtCreateManagedWidget("+", commandWidgetClass, form, args, j);
1610             XtAddCallback(edit, XtNcallback, SpinCallback,
1611                           (XtPointer)(intptr_t) i);
1612
1613             j=0;
1614             XtSetArg(args[j], XtNfromVert, edit);  j++;
1615             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1616             XtSetArg(args[j], XtNheight, 10);  j++;
1617             XtSetArg(args[j], XtNwidth, 20);  j++;
1618             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1619             XtAddCallback(last, XtNcallback, SpinCallback,
1620                           (XtPointer)(intptr_t) i);
1621             break;
1622           case CheckBox:
1623             j=0;
1624             XtSetArg(args[j], XtNfromVert, last);  j++;
1625             XtSetArg(args[j], XtNwidth, 10);  j++;
1626             XtSetArg(args[j], XtNheight, 10);  j++;
1627             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1628             cps->option[i].handle = (void*) 
1629                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));   
1630             j=0;
1631             XtSetArg(args[j], XtNfromVert, last);  j++;
1632             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1633             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1634             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1635             last = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1636             break;
1637           case SaveButton:
1638           case Button:
1639             j=0;
1640             XtSetArg(args[j], XtNfromVert, last);  j++;
1641             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1642             cps->option[i].handle = (void*) 
1643                 (dialog = last = XtCreateManagedWidget(cps->option[i].name, commandWidgetClass, form, args, j));   
1644             XtAddCallback(last, XtNcallback, SettingsCallback,
1645                           (XtPointer)(intptr_t) (cps->option[i].type == SaveButton));
1646             break;
1647           case ComboBox:
1648             j=0;
1649             XtSetArg(args[j], XtNfromVert, last);  j++;
1650             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1651             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1652             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1653
1654             j=0;
1655             XtSetArg(args[j], XtNfromVert, last);  j++;
1656             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1657             XtSetArg(args[j], XtNwidth, 100);  j++;
1658             XtSetArg(args[j], XtNmenuName, XtNewString(cps->option[i].name));  j++;
1659             XtSetArg(args[j], XtNlabel, ((char**)cps->option[i].textValue)[cps->option[i].value]);  j++;
1660             cps->option[i].handle = (void*) 
1661                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));   
1662             CreateComboPopup(last, cps->option[i].name, i, (char **) cps->option[i].textValue);
1663             values[i] = cps->option[i].value;
1664             break;
1665         }
1666     }
1667
1668     // make an attempt to align all spins and textbox controls
1669     maxWidth = maxTextWidth = 0;
1670     for(h=0; h<height; h++) {
1671         i = h + c*height;
1672         if(i >= cps->nrOptions) break;
1673         if(cps->option[i].type == Spin || cps->option[i].type == TextBox) {
1674             Dimension w;
1675             j=0;
1676             XtSetArg(args[j], XtNwidth, &w);  j++;
1677             XtGetValues(texts[h], args, j);
1678             if(cps->option[i].type == Spin) {
1679                 if(w > maxWidth) maxWidth = w;
1680                 widest = texts[h];
1681             } else {
1682                 if(w > maxTextWidth) maxTextWidth = w;
1683                 if(!widest) widest = texts[h];
1684             }
1685         }
1686     }
1687     if(maxTextWidth + 110 < maxWidth)
1688          maxTextWidth = maxWidth - 110;
1689     else maxWidth = maxTextWidth + 110;
1690     for(h=0; h<height; h++) {
1691         i = h + c*height;
1692         if(i >= cps->nrOptions) break;
1693         j=0;
1694         if(cps->option[i].type == Spin) {
1695             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1696             XtSetValues(texts[h], args, j);
1697         } else
1698         if(cps->option[i].type == TextBox) {
1699             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1700             XtSetValues(texts[h], args, j);
1701         }
1702     }
1703   }
1704     j=0;
1705     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1706     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1707     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1708     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1709     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1710     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1711     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
1712     XtAddCallback(b_ok, XtNcallback, SettingsCallback, (XtPointer) 0);
1713
1714     XtSetArg(args[j-1], XtNfromHoriz, b_ok);
1715     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);   
1716     XtAddCallback(b_cancel, XtNcallback, SettingsPopDown, (XtPointer) 0);
1717
1718     XtRealizeWidget(popup);
1719     CatchDeleteWindow(popup, "SettingsPopDown");
1720     
1721     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1722                   &x, &y, &win_x, &win_y, &mask);
1723     
1724     XtSetArg(args[0], XtNx, x - 10);
1725     XtSetArg(args[1], XtNy, y - 30);
1726     XtSetValues(popup, args, 2);
1727     
1728     XtPopup(popup, XtGrabExclusive);
1729     SettingsUp = True;
1730
1731     previous = NULL;
1732     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1733 }
1734
1735 void FirstSettingsProc(w, event, prms, nprms)
1736      Widget w;
1737      XEvent *event;
1738      String *prms;
1739      Cardinal *nprms;
1740 {
1741    SettingsPopUp(&first);
1742 }
1743
1744 void SecondSettingsProc(w, event, prms, nprms)
1745      Widget w;
1746      XEvent *event;
1747      String *prms;
1748      Cardinal *nprms;
1749 {
1750    SettingsPopUp(&second);
1751 }
1752
1753 //---------------------------- Chat Windows ----------------------------------------------
1754
1755 void OutputChatMessage(int partner, char *mess)
1756 {
1757     return; // dummy
1758 }
1759