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