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