fixed Makefile.am to include gtk-interface.xml and svg files
[xboard.git] / xoptions.c
1 /*
2  * xoptions.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000,2009 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,  boardWidget, menuBarWidget;
85 extern int squareSize;
86 extern Pixmap xMarkPixmap;
87 extern char *layoutName;
88 extern Window xBoardWindow;
89 extern Arg layoutArgs[2], formArgs[2];
90 Pixel timerForegroundPixel, timerBackgroundPixel;
91
92 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
93
94 static Widget previous = NULL;
95
96 void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
97 {
98     Arg args;
99
100     if(previous) {
101         XtSetArg(args, XtNdisplayCaret, False);
102         XtSetValues(previous, &args, 1);
103     }
104     XtSetArg(args, XtNdisplayCaret, True);
105     XtSetValues(w, &args, 1);
106     XtSetKeyboardFocus((Widget) data, w);
107     previous = w;
108 }
109
110 //--------------------------- New Shuffle Game --------------------------------------------
111 extern int shuffleOpenings;
112 extern int startedFromPositionFile;
113 int shuffleUp;
114 Widget shuffleShell;
115
116 void ShufflePopDown()
117 {
118     if (!shuffleUp) return;
119     XtPopdown(shuffleShell);
120     XtDestroyWidget(shuffleShell);
121     shuffleUp = False;
122     ModeHighlight();
123 }
124
125 void ShuffleCallback(w, client_data, call_data)
126      Widget w;
127      XtPointer client_data, call_data;
128 {
129     String name;
130     Widget w2;
131     Arg args[16];
132     char buf[80];
133     
134     XtSetArg(args[0], XtNlabel, &name);
135     XtGetValues(w, args, 1);
136     
137     if (strcmp(name, _("cancel")) == 0) {
138         ShufflePopDown();
139         return;
140     }
141     if (strcmp(name, _("off")) == 0) {
142         ShufflePopDown();
143         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
144         ResetGameEvent();
145         return;
146     }
147     if (strcmp(name, _("random")) == 0) {
148         sprintf(buf, "%d", rand());
149         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
150         XtSetValues(XtParent(w), args, 1);
151         return;
152     }
153     if (strcmp(name, _("ok")) == 0) {
154         int nr; String name;
155         name = XawDialogGetValueString(w2 = XtParent(w));
156         if(sscanf(name ,"%d",&nr) != 1) {
157             sprintf(buf, "%d", appData.defaultFrcPosition);
158             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
159             XtSetValues(w2, args, 1);
160             return;
161         }
162         appData.defaultFrcPosition = nr;
163         shuffleOpenings = True;
164         ShufflePopDown();
165         ResetGameEvent();
166         return;
167     }
168 }
169
170 void ShufflePopUp()
171 {
172     Arg args[16];
173     Widget popup, layout, dialog, edit;
174     Window root, child;
175     int x, y, i;
176     int win_x, win_y;
177     unsigned int mask;
178     char def[80];
179     
180     i = 0;
181     XtSetArg(args[i], XtNresizable, True); i++;
182     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
183 //    shuffleShell = popup =
184 //      XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
185 //                       shellWidget, args, i);
186 //    
187 //    layout =
188 //      XtCreateManagedWidget(layoutName, formWidgetClass, popup,
189 //                          layoutArgs, XtNumber(layoutArgs));
190 //  
191     sprintf(def, "%d\n", appData.defaultFrcPosition);
192     i = 0;
193     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
194     XtSetArg(args[i], XtNvalue, def); i++;
195     XtSetArg(args[i], XtNborderWidth, 0); i++;
196     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
197                                    layout, args, i);
198     
199 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
200 //    XtSetArg(args[1], XtNuseStringInPlace, False);
201 //    XtSetValues(dialog, args, 2);
202
203     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
204     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
205     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
206     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
207     
208     XtRealizeWidget(popup);
209     //    CatchDeleteWindow(popup, "ShufflePopDown");
210     
211 //    XQueryPointer(xDisplay, xBoardWindow, &root, &child,
212 //                &x, &y, &win_x, &win_y, &mask);
213     
214     XtSetArg(args[0], XtNx, x - 10);
215     XtSetArg(args[1], XtNy, y - 30);
216     XtSetValues(popup, args, 2);
217     
218     XtPopup(popup, XtGrabExclusive);
219     shuffleUp = True;
220     
221     edit = XtNameToWidget(dialog, "*value");
222
223     XtSetKeyboardFocus(popup, edit);
224 }
225
226 void ShuffleMenuProc(w, event, prms, nprms)
227      Widget w;
228      XEvent *event;
229      String *prms;
230      Cardinal *nprms;
231 {
232 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
233 //      Reset(FALSE, TRUE);
234 //    }
235     ShufflePopUp();
236 }
237
238 //--------------------------- Time-Control Menu Popup ----------------------------------
239 int TimeControlUp;
240 Widget TimeControlShell;
241 int tcInc;
242 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
243 int tcIncrement, tcMoves;
244
245 void TimeControlPopDown()
246 {
247     if (!TimeControlUp) return;
248     previous = NULL;
249     XtPopdown(TimeControlShell);
250     XtDestroyWidget(TimeControlShell);
251     TimeControlUp = False;
252     ModeHighlight();
253 }
254
255 void TimeControlCallback(w, client_data, call_data)
256      Widget w;
257      XtPointer client_data, call_data;
258 {
259     String name, txt;
260     Widget w2;
261     Arg args[16];
262     char buf[80];
263     int j;
264
265     XtSetArg(args[0], XtNlabel, &name);
266     XtGetValues(w, args, 1);
267     
268     if (strcmp(name, _("classical")) == 0) {
269         if(!tcInc) return;
270         j=0;
271         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
272         XtSetValues(tcMess1, args, j);
273         j=0;
274         XtSetArg(args[j], XtNlabel, _("moves")); j++;
275         XtSetValues(tcMess2, args, j);
276         j=0;
277         XtSetArg(args[j], XtNstring, &name); j++;
278         XtGetValues(tcData, args, j);
279         tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
280         sprintf(buf, "%d", tcMoves);
281         j=0;
282         XtSetArg(args[j], XtNstring, buf); j++;
283         XtSetValues(tcData, args, j);
284         tcInc = False;
285         return;
286     }
287     if (strcmp(name, _("incremental")) == 0) {
288         if(tcInc) return;
289         j=0;
290         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
291         XtSetValues(tcMess1, args, j);
292         j=0;
293         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
294         XtSetValues(tcMess2, args, j);
295         j=0;
296         XtSetArg(args[j], XtNstring, &name); j++;
297         XtGetValues(tcData, args, j);
298         tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
299         sprintf(buf, "%d", tcIncrement);
300         j=0;
301         XtSetArg(args[j], XtNstring, buf); j++;
302         XtSetValues(tcData, args, j);
303         tcInc = True;
304         return;
305     }
306     if (strcmp(name, _(" OK ")) == 0) {
307         int inc, mps, tc, ok;
308         XtSetArg(args[0], XtNstring, &txt);
309         XtGetValues(tcData, args, 1);
310         if(tcInc) {
311             ok = sscanf(txt, "%d", &inc); mps = 0;
312             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
313             ok &= (inc >= 0);
314         } else {
315             ok = sscanf(txt, "%d", &mps); inc = -1;
316             ok &= (mps > 0);
317         }
318         if(ok != 1) {
319             XtSetArg(args[0], XtNstring, ""); // erase any offending input
320             XtSetValues(tcData, args, 1);
321             return;
322         }
323         XtSetArg(args[0], XtNstring, &txt);
324         XtGetValues(tcTime, args, 1);
325         if(!ParseTimeControl(txt, inc, mps)) {
326             XtSetArg(args[0], XtNstring, ""); // erase any offending input
327             XtSetValues(tcTime, args, 1);
328             DisplayError(_("Bad Time-Control String"), 0);
329             return;
330         }
331         appData.movesPerSession = mps;
332         appData.timeIncrement = inc;
333         appData.timeControl = strdup(txt);
334         XtSetArg(args[0], XtNstring, &txt);
335         XtGetValues(tcOdds1, args, 1);
336         appData.firstTimeOdds = first.timeOdds 
337                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
338         XtGetValues(tcOdds2, args, 1);
339         appData.secondTimeOdds = second.timeOdds 
340                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
341
342         Reset(True, True);
343         TimeControlPopDown();
344         return;
345     }
346 }
347
348 void TimeControlPopUp()
349 {
350     Arg args[16];
351     Widget popup, layout, form, edit, b_ok, b_cancel, b_clas, b_inc, mess;
352     Window root, child;
353     int x, y, i, j;
354     int win_x, win_y;
355     unsigned int mask;
356     char def[80];
357     
358     tcInc = (appData.timeIncrement >= 0);
359     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
360     if(!tcInc) tcIncrement = 0;
361     sprintf(def, "%d", tcInc ? tcIncrement : tcMoves);
362
363     i = 0;
364     XtSetArg(args[i], XtNresizable, True); i++;
365 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
366 //    TimeControlShell = popup =
367 //      XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
368 //                       shellWidget, args, i);
369 //    
370 //    layout =
371 //      XtCreateManagedWidget(layoutName, formWidgetClass, popup,
372 //                          layoutArgs, XtNumber(layoutArgs));
373 //  
374 //    form =
375 //      XtCreateManagedWidget(layoutName, formWidgetClass, layout,
376 //                          formArgs, XtNumber(formArgs));
377 //  
378     j = 0;
379 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
380 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
381     XtSetValues(popup, args, j);
382
383     j= 0;
384     XtSetArg(args[j], XtNborderWidth, 1); j++;
385     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
386     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
387     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
388     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
389     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
390     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
391     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
392     XtSetArg(args[j], XtNright, XtChainRight);  j++;
393     XtSetArg(args[j], XtNresizable, True);  j++;
394     XtSetArg(args[j], XtNwidth,  85);  j++;
395 //    XtSetArg(args[j], XtNheight, 20);  j++;
396     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
397     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
398
399     j= 0;
400     XtSetArg(args[j], XtNlabel, tcInc ? _("   minutes, plus   ") : _("minutes for each")); j++;
401     XtSetArg(args[j], XtNborderWidth, 0); j++;
402     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
403     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
404     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
405     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
406     XtSetArg(args[j], XtNright, XtChainRight);  j++;
407   //  XtSetArg(args[j], XtNwidth,  100);  j++;
408   //  XtSetArg(args[j], XtNheight, 20);  j++;
409     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
410
411     j= 0;
412     XtSetArg(args[j], XtNborderWidth, 1); j++;
413     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
414     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
415     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
416     XtSetArg(args[j], XtNstring, def);  j++;
417     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
418     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
419     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
420     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
421     XtSetArg(args[j], XtNright, XtChainRight);  j++;
422     XtSetArg(args[j], XtNresizable, True);  j++;
423     XtSetArg(args[j], XtNwidth,  40);  j++;
424 //    XtSetArg(args[j], XtNheight, 20);  j++;
425     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
426     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
427
428     j= 0;
429     XtSetArg(args[j], XtNlabel, tcInc ? _("sec/move") : _("moves     ")); j++;
430     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
431     XtSetArg(args[j], XtNborderWidth, 0); j++;
432     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
433     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
434     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
435     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
436     XtSetArg(args[j], XtNright, XtChainRight);  j++;
437 //    XtSetArg(args[j], XtNwidth,  80);  j++;
438 //    XtSetArg(args[j], XtNheight, 20);  j++;
439     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
440                                    form, args, j);
441
442     j= 0;
443     XtSetArg(args[j], XtNborderWidth, 1); j++;
444     XtSetArg(args[j], XtNfromVert, tcTime); j++;
445     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
446     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
447     XtSetArg(args[j], XtNstring, "1");  j++;
448     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
449     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
450     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
451     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
452     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
453     XtSetArg(args[j], XtNresizable, True);  j++;
454     XtSetArg(args[j], XtNwidth,  40);  j++;
455 //    XtSetArg(args[j], XtNheight, 20);  j++;
456     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
457     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
458
459     j= 0;
460     XtSetArg(args[j], XtNborderWidth, 1); j++;
461     XtSetArg(args[j], XtNfromVert, tcTime); j++;
462     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
463     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
464     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
465     XtSetArg(args[j], XtNstring, "1");  j++;
466     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
467     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
468     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
469     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
470     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
471     XtSetArg(args[j], XtNresizable, True);  j++;
472     XtSetArg(args[j], XtNwidth,  40);  j++;
473 //    XtSetArg(args[j], XtNheight, 20);  j++;
474     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
475     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
476
477     j= 0;
478     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
479     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
480     XtSetArg(args[j], XtNborderWidth, 0); j++;
481     XtSetArg(args[j], XtNfromVert, tcTime); j++;
482     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
483     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
484     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
485     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
486     XtSetArg(args[j], XtNright, XtChainRight);  j++;
487 //    XtSetArg(args[j], XtNwidth,  200);  j++;
488 //    XtSetArg(args[j], XtNheight, 20);  j++;
489     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
490                                    form, args, j);
491     j=0;
492     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
493     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
494     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
495     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
496     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
497     b_clas= XtCreateManagedWidget(_("classical"), commandWidgetClass,
498                                    form, args, j);   
499     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
500
501     j=0;
502     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
503     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
504     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
505     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
506     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
507     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
508     b_inc = XtCreateManagedWidget(_("incremental"), commandWidgetClass,
509                                    form, args, j);   
510     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
511
512     j=0;
513     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
514     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
515     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
516     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
517     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
518     XtSetArg(args[j], XtNright, XtChainRight);  j++;
519     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
520                                    form, args, j);   
521     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
522
523     j=0;
524     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
525     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
526     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
527     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
528     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
529     XtSetArg(args[j], XtNright, XtChainRight);  j++;
530     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
531                                    form, args, j);   
532     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
533
534     XtRealizeWidget(popup);
535     //    CatchDeleteWindow(popup, "TimeControlPopDown");
536     
537 //    XQueryPointer(xDisplay, xBoardWindow, &root, &child,
538 //                &x, &y, &win_x, &win_y, &mask);
539     
540     XtSetArg(args[0], XtNx, x - 10);
541     XtSetArg(args[1], XtNy, y - 30);
542     XtSetValues(popup, args, 2);
543     
544     XtPopup(popup, XtGrabExclusive);
545     TimeControlUp = True;
546     
547     previous = NULL;
548     SetFocus(tcTime, popup, (XEvent*) NULL, False);
549 //    XtSetKeyboardFocus(popup, tcTime);
550 }
551
552 void TimeControlProc(w, event, prms, nprms)
553      Widget w;
554      XEvent *event;
555      String *prms;
556      Cardinal *nprms;
557 {
558    TimeControlPopUp();
559 }
560
561 //--------------------------- Engine-Options Menu Popup ----------------------------------
562 int EngineUp;
563 Widget EngineShell;
564 extern int adjudicateLossThreshold;
565
566 Widget engDrawMoves, engThreshold, engRule, engRepeat;
567
568 void EnginePopDown()
569 {
570     if (!EngineUp) return;
571     previous = NULL;
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     previous = NULL;
1132     XtPopdown(UciShell);
1133     XtDestroyWidget(UciShell);
1134     UciUp = False;
1135     ModeHighlight();
1136 }
1137
1138 void UciCallback(w, client_data, call_data)
1139      Widget w;
1140      XtPointer client_data, call_data;
1141 {
1142     String name;
1143     Arg args[16];
1144     char buf[80];
1145     int oldCores = appData.smpCores, ponder = 0;
1146     
1147     XtSetArg(args[0], XtNlabel, &name);
1148     XtGetValues(w, args, 1);
1149     
1150     if (strcmp(name, _("OK")) == 0) {
1151         int nr, i, j; String name;
1152         for(i=0; i<6; i++) {
1153             XtSetArg(args[0], XtNstring, &name);
1154             XtGetValues(controlDesc[i].handle, args, 1);
1155             if(i&1) {
1156                 if(name)
1157                     *(char**) controlDesc[i].ptr = strdup(name);
1158             } else {
1159                 if(sscanf(name, "%d", &j) == 1) 
1160                     *(int*) controlDesc[i].ptr = j;
1161             }
1162         }
1163         XtSetArg(args[0], XtNstate, &appData.usePolyglotBook);
1164         XtGetValues(w1, args, 1);
1165         XtSetArg(args[0], XtNstate, &appData.firstHasOwnBookUCI);
1166         XtGetValues(w2, args, 1);
1167         XtSetArg(args[0], XtNstate, &appData.secondHasOwnBookUCI);
1168         XtGetValues(w3, args, 1);
1169         XtSetArg(args[0], XtNstate, &ponder);
1170         XtGetValues(w4, args, 1);
1171
1172         // adjust setting in other menu for duplicates 
1173         // (perhaps duplicates should be removed from general Option Menu?)
1174         XtSetArg(args[0], XtNleftBitmap, ponder ? xMarkPixmap : None);
1175         XtSetValues(XtNameToWidget(menuBarWidget,
1176                                    "menuOptions.Ponder Next Move"), args, 1);
1177
1178         // make sure changes are sent to first engine by re-initializing it
1179         // if it was already started pre-emptively at end of previous game
1180         if(gameMode == BeginningOfGame) Reset(True, True); else {
1181             // Some changed setting need immediate sending always.
1182             PonderNextMoveEvent(ponder);
1183             if(oldCores != appData.smpCores)
1184                 NewSettingEvent(False, "cores", appData.smpCores);
1185       }
1186       UciPopDown();
1187       return;
1188     }
1189 }
1190
1191 void UciPopUp()
1192 {
1193     Arg args[16];
1194     Widget popup, layout, dialog, edit, form, b_ok, b_cancel, last = NULL, new, upperLeft;
1195     Window root, child;
1196     int x, y, i, j;
1197     int win_x, win_y;
1198     unsigned int mask;
1199     char def[80];
1200     
1201     i = 0;
1202     XtSetArg(args[i], XtNresizable, True); i++;
1203 //    XtSetArg(args[i], XtNwidth, 300); i++;
1204 //    UciShell = popup =
1205 //      XtCreatePopupShell(_("Engine Settings"), transientShellWidgetClass,
1206 //                       shellWidget, args, i);
1207 //    
1208 //    layout =
1209 //      XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1210 //                          layoutArgs, XtNumber(layoutArgs));
1211 //  
1212 //    
1213 //    form =
1214 //      XtCreateManagedWidget("form", formWidgetClass, layout,
1215 //                          formArgs, XtNumber(formArgs));
1216 //  
1217     j = 0;
1218     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1219     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1220     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1221 //    XtSetArg(args[j], XtNheight, 20); j++;
1222     for(i = 0; controlDesc[i].name != NULL; i++) {
1223         j = 3;
1224         XtSetArg(args[j], XtNfromVert, last); j++;
1225 //      XtSetArg(args[j], XtNwidth, 130); j++;
1226         XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1227         XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1228         XtSetArg(args[j], XtNborderWidth, 0); j++;
1229         new = XtCreateManagedWidget(controlDesc[i].name, labelWidgetClass, form, args, j);
1230         if(i==0) upperLeft = new;
1231
1232         j = 4;
1233         XtSetArg(args[j], XtNborderWidth, 1); j++;
1234         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1235         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1236         XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1237         XtSetArg(args[j], XtNright, XtChainRight);  j++;
1238         XtSetArg(args[j], XtNresizable, True);  j++;
1239         XtSetArg(args[j], XtNwidth, i&1 ? 245 : 50); j++;
1240         if(i&1) {
1241             XtSetArg(args[j], XtNstring, * (char**) controlDesc[i].ptr ? 
1242                                          * (char**) controlDesc[i].ptr : ""); j++;
1243         } else {
1244             sprintf(def, "%d", * (int*) controlDesc[i].ptr);
1245             XtSetArg(args[j], XtNstring, def); j++;
1246         }
1247         XtSetArg(args[j], XtNfromHoriz, upperLeft); j++;
1248         controlDesc[i].handle = last =
1249             XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
1250         XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1251     }
1252
1253     j=0;
1254     XtSetArg(args[j], XtNfromHoriz, controlDesc[0].handle);  j++;
1255     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
1256     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
1257     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1258     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1259     XtSetArg(args[j], XtNstate, appData.ponderNextMove);  j++;
1260     w4 = XtCreateManagedWidget(_("Ponder"), toggleWidgetClass, form, args, j);   
1261
1262     j=0;
1263     XtSetArg(args[j], XtNfromVert, last);  j++;
1264     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1265     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1266     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
1267     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
1268     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
1269     XtAddCallback(b_ok, XtNcallback, UciCallback, (XtPointer) 0);
1270
1271     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
1272     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);   
1273     XtAddCallback(b_cancel, XtNcallback, UciPopDown, (XtPointer) 0);
1274
1275     j = 5;
1276     XtSetArg(args[j], XtNfromHoriz, upperLeft);  j++;
1277     XtSetArg(args[j], XtNstate, appData.usePolyglotBook);  j++;
1278     w1 = XtCreateManagedWidget(_(" use book "), toggleWidgetClass, form, args, j);   
1279 //    XtAddCallback(w1, XtNcallback, UciCallback, (XtPointer) 0);
1280
1281     j = 5;
1282     XtSetArg(args[j], XtNfromHoriz, w1);  j++;
1283     XtSetArg(args[j], XtNstate, appData.firstHasOwnBookUCI);  j++;
1284     w2 = XtCreateManagedWidget(_("own book 1"), toggleWidgetClass, form, args, j);   
1285 //    XtAddCallback(w2, XtNcallback, UciCallback, (XtPointer) 0);
1286
1287     j = 5;
1288     XtSetArg(args[j], XtNfromHoriz, w2);  j++;
1289     XtSetArg(args[j], XtNstate, appData.secondHasOwnBookUCI);  j++;
1290     w3 = XtCreateManagedWidget(_("own book 2"), toggleWidgetClass, form, args, j);   
1291 //    XtAddCallback(w3, XtNcallback, UciCallback, (XtPointer) 0);
1292
1293     XtRealizeWidget(popup);
1294 //    CatchDeleteWindow(popup, "UciPopDown");
1295 //    
1296 //    XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1297 //                &x, &y, &win_x, &win_y, &mask);
1298     
1299     XtSetArg(args[0], XtNx, x - 10);
1300     XtSetArg(args[1], XtNy, y - 30);
1301     XtSetValues(popup, args, 2);
1302     
1303     XtPopup(popup, XtGrabExclusive);
1304     UciUp = True;
1305
1306     previous = NULL;
1307     SetFocus(controlDesc[2].handle, popup, (XEvent*) NULL, False);
1308 //    XtSetKeyboardFocus(popup, controlDesc[1].handle);
1309 }
1310
1311 void UciMenuProc(w, event, prms, nprms)
1312      Widget w;
1313      XEvent *event;
1314      String *prms;
1315      Cardinal *nprms;
1316 {
1317    UciPopUp();
1318 }
1319
1320 //--------------------------- Engine-specific options menu ----------------------------------
1321
1322 int SettingsUp;
1323 Widget SettingsShell;
1324 int values[MAX_OPTIONS];
1325 ChessProgramState *currentCps;
1326
1327 void SettingsPopDown()
1328 {
1329     if (!SettingsUp) return;
1330     previous = NULL;
1331     XtPopdown(SettingsShell);
1332     XtDestroyWidget(SettingsShell);
1333     SettingsUp = False;
1334     ModeHighlight();
1335 }
1336
1337 void SpinCallback(w, client_data, call_data)
1338      Widget w;
1339      XtPointer client_data, call_data;
1340 {
1341     String name, val;
1342     Widget w2;
1343     Arg args[16];
1344     char buf[MSG_SIZ];
1345     int i, j;
1346     int data = (intptr_t) client_data;
1347     
1348     XtSetArg(args[0], XtNlabel, &name);
1349     XtGetValues(w, args, 1);
1350     
1351     j = 0;
1352     XtSetArg(args[0], XtNstring, &val);
1353     XtGetValues(currentCps->option[data].handle, args, 1);
1354     sscanf(val, "%d", &j);
1355     if (strcmp(name, "+") == 0) {
1356         if(++j > currentCps->option[data].max) return;
1357     } else
1358     if (strcmp(name, "-") == 0) {
1359         if(--j < currentCps->option[data].min) return;
1360     } else return;
1361     sprintf(buf, "%d", j);
1362     XtSetArg(args[0], XtNstring, buf);
1363     XtSetValues(currentCps->option[data].handle, args, 1);
1364 }
1365
1366 void SettingsCallback(w, client_data, call_data)
1367      Widget w;
1368      XtPointer client_data, call_data;
1369 {
1370     String name, val;
1371     Widget w2;
1372     Arg args[16];
1373     char buf[MSG_SIZ];
1374     int i, j;
1375     int data = (intptr_t) client_data;
1376     
1377     XtSetArg(args[0], XtNlabel, &name);
1378     XtGetValues(w, args, 1);
1379     
1380     if (strcmp(name, _("cancel")) == 0) {
1381         SettingsPopDown();
1382         return;
1383     }
1384     if (strcmp(name, _("OK")) == 0 || data) { // save buttons imply OK
1385         int nr;
1386
1387         for(i=0; i<currentCps->nrOptions; i++) { // send all options that had to be OK-ed to engine
1388             switch(currentCps->option[i].type) {
1389                 case TextBox:
1390                     XtSetArg(args[0], XtNstring, &val);
1391                     XtGetValues(currentCps->option[i].handle, args, 1);
1392                     if(strcmp(currentCps->option[i].textValue, val)) {
1393                         strcpy(currentCps->option[i].textValue, val);
1394                         sprintf(buf, "option %s=%s\n", currentCps->option[i].name, val);
1395                         SendToProgram(buf, currentCps);
1396                     }
1397                     break;
1398                 case Spin:
1399                     XtSetArg(args[0], XtNstring, &val);
1400                     XtGetValues(currentCps->option[i].handle, args, 1);
1401                     sscanf(val, "%d", &j);
1402                     if(j > currentCps->option[i].max) j = currentCps->option[i].max;
1403                     if(j < currentCps->option[i].min) j = currentCps->option[i].min;
1404                     if(currentCps->option[i].value != j) {
1405                         currentCps->option[i].value = j;
1406                         sprintf(buf, "option %s=%d\n", currentCps->option[i].name, j);
1407                         SendToProgram(buf, currentCps);
1408                     }
1409                     break;
1410                 case CheckBox:
1411                     j = 0;
1412                     XtSetArg(args[0], XtNstate, &j);
1413                     XtGetValues(currentCps->option[i].handle, args, 1);
1414                     if(currentCps->option[i].value != j) {
1415                         currentCps->option[i].value = j;
1416                         sprintf(buf, "option %s=%d\n", currentCps->option[i].name, j);
1417                         SendToProgram(buf, currentCps);
1418                     }
1419                     break;
1420                 case ComboBox:
1421                     if(currentCps->option[i].value != values[i]) {
1422                         currentCps->option[i].value = values[i];
1423                         sprintf(buf, "option %s=%s\n", currentCps->option[i].name, 
1424                                 ((char**)currentCps->option[i].textValue)[values[i]]);
1425                         SendToProgram(buf, currentCps);
1426                     }
1427                     break;
1428             }
1429         }
1430         if(data) { // send save-button command to engine
1431             sprintf(buf, "option %s\n", name);
1432             SendToProgram(buf, currentCps);
1433         }
1434         SettingsPopDown();
1435         return;
1436     }
1437     sprintf(buf, "option %s\n", name);
1438     SendToProgram(buf, currentCps);
1439 }
1440
1441 void ComboSelect(w, addr, index) // callback for all combo items
1442      Widget w;
1443      caddr_t addr;
1444      caddr_t index;
1445 {
1446     Arg args[16];
1447     int i = ((intptr_t)addr)>>8;
1448     int j = 255 & (intptr_t) addr;
1449
1450     values[i] = j; // store in temporary, for transfer at OK
1451     XtSetArg(args[0], XtNlabel, ((char**)currentCps->option[i].textValue)[j]);
1452     XtSetValues(currentCps->option[i].handle, args, 1);
1453 }
1454
1455 void CreateComboPopup(parent, name, n, mb)
1456      Widget parent;
1457      String name;
1458      int n;
1459      char *mb[];
1460 {
1461     int i=0, j;
1462     Widget menu, entry;
1463     Arg args[16];
1464
1465     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
1466                               parent, NULL, 0);
1467     j = 0;
1468     XtSetArg(args[j], XtNwidth, 100);  j++;
1469 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
1470     while (mb[i] != NULL) {
1471             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
1472                                           menu, args, j);
1473             XtAddCallback(entry, XtNcallback,
1474                           (XtCallbackProc) ComboSelect,
1475                           (caddr_t)(intptr_t) (256*n+i));
1476         i++;
1477     }
1478 }       
1479
1480 void SettingsPopUp(ChessProgramState *cps)
1481 {
1482     Arg args[16];
1483     Widget popup, layout, dialog, edit=NULL, form, oldform, last, b_ok, b_cancel, leftMargin = NULL;
1484     Window root, child;
1485     int x, y, i, j, height, width, h, c;
1486     int win_x, win_y, maxWidth, maxTextWidth;
1487     unsigned int mask;
1488     char def[80], *p, *q;
1489     static char pane[6] = "paneX";
1490     Widget texts[100], forelast = NULL, anchor, widest;
1491
1492     // to do: start up second engine if needed
1493     if(!cps->initDone || !cps->nrOptions) return; // nothing to be done
1494     currentCps = cps;
1495
1496     if(cps->nrOptions > 50) width = 4; else if(cps->nrOptions>24) width = 2; else width = 1;
1497     height = cps->nrOptions / width + 1;
1498      i = 0;
1499     XtSetArg(args[i], XtNresizable, True); i++;
1500 //    SettingsShell = popup =
1501 //      XtCreatePopupShell(_("Settings Menu"), transientShellWidgetClass,
1502 //                       shellWidget, args, i);
1503 //    
1504 //    layout =
1505 //      XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1506 //                          layoutArgs, XtNumber(layoutArgs));
1507   for(c=0; c<width; c++) {
1508     pane[4] = 'A'+c;
1509     form =
1510       XtCreateManagedWidget(pane, formWidgetClass, layout,
1511                             formArgs, XtNumber(formArgs));
1512     j=0;
1513     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1514     XtSetValues(form, args, j);
1515     leftMargin = form;
1516  
1517     last = widest = NULL; anchor = forelast;
1518     for(h=0; h<height; h++) {
1519         forelast = last;
1520         i = h + c*height;
1521         if(i >= cps->nrOptions) break;
1522         switch(cps->option[i].type) {
1523           case Spin:
1524             sprintf(def, "%d", cps->option[i].value);
1525           case TextBox:
1526             j=0;
1527             XtSetArg(args[j], XtNfromVert, last);  j++;
1528             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1529             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1530             texts[h] =
1531             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);   
1532             j=0;
1533             XtSetArg(args[j], XtNfromVert, last);  j++;
1534             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1535             XtSetArg(args[j], XtNborderWidth, 1); j++;
1536             XtSetArg(args[j], XtNwidth, cps->option[i].type == Spin ? 40 : 175); j++;
1537             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1538             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1539             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1540             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1541             XtSetArg(args[j], XtNresizable, True);  j++;
1542             XtSetArg(args[j], XtNstring, cps->option[i].type==Spin ? def : cps->option[i].textValue);  j++;
1543             edit = last;
1544             cps->option[i].handle = (void*)
1545                 (last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));   
1546             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1547             if(cps->option[i].type == TextBox) break;
1548
1549             // add increment and decrement controls for spin
1550             j=0;
1551             XtSetArg(args[j], XtNfromVert, edit);  j++;
1552             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1553             XtSetArg(args[j], XtNheight, 10);  j++;
1554             XtSetArg(args[j], XtNwidth, 20);  j++;
1555             edit = XtCreateManagedWidget("+", commandWidgetClass, form, args, j);
1556             XtAddCallback(edit, XtNcallback, SpinCallback,
1557                           (XtPointer)(intptr_t) i);
1558
1559             j=0;
1560             XtSetArg(args[j], XtNfromVert, edit);  j++;
1561             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1562             XtSetArg(args[j], XtNheight, 10);  j++;
1563             XtSetArg(args[j], XtNwidth, 20);  j++;
1564             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1565             XtAddCallback(last, XtNcallback, SpinCallback,
1566                           (XtPointer)(intptr_t) i);
1567             break;
1568           case CheckBox:
1569             j=0;
1570             XtSetArg(args[j], XtNfromVert, last);  j++;
1571             XtSetArg(args[j], XtNwidth, 10);  j++;
1572             XtSetArg(args[j], XtNheight, 10);  j++;
1573             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1574             cps->option[i].handle = (void*) 
1575                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));   
1576             j=0;
1577             XtSetArg(args[j], XtNfromVert, last);  j++;
1578             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1579             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1580             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1581             last = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1582             break;
1583           case SaveButton:
1584           case Button:
1585             j=0;
1586             XtSetArg(args[j], XtNfromVert, last);  j++;
1587             XtSetArg(args[j], XtNstate, cps->option[i].value);  j++;
1588             cps->option[i].handle = (void*) 
1589                 (dialog = last = XtCreateManagedWidget(cps->option[i].name, commandWidgetClass, form, args, j));   
1590             XtAddCallback(last, XtNcallback, SettingsCallback,
1591                           (XtPointer)(intptr_t) (cps->option[i].type == SaveButton));
1592             break;
1593           case ComboBox:
1594             j=0;
1595             XtSetArg(args[j], XtNfromVert, last);  j++;
1596             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1597             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1598             dialog = XtCreateManagedWidget(cps->option[i].name, labelWidgetClass, form, args, j);
1599
1600             j=0;
1601             XtSetArg(args[j], XtNfromVert, last);  j++;
1602             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1603             XtSetArg(args[j], XtNwidth, 100);  j++;
1604             XtSetArg(args[j], XtNmenuName, XtNewString(cps->option[i].name));  j++;
1605             XtSetArg(args[j], XtNlabel, ((char**)cps->option[i].textValue)[cps->option[i].value]);  j++;
1606             cps->option[i].handle = (void*) 
1607                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));   
1608             CreateComboPopup(last, cps->option[i].name, i, (char **) cps->option[i].textValue);
1609             values[i] = cps->option[i].value;
1610             break;
1611         }
1612     }
1613
1614     // make an attempt to align all spins and textbox controls
1615     maxWidth = maxTextWidth = 0;
1616     for(h=0; h<height; h++) {
1617         i = h + c*height;
1618         if(i >= cps->nrOptions) break;
1619         if(cps->option[i].type == Spin || cps->option[i].type == TextBox) {
1620             Dimension w;
1621             j=0;
1622             XtSetArg(args[j], XtNwidth, &w);  j++;
1623             XtGetValues(texts[h], args, j);
1624             if(cps->option[i].type == Spin) {
1625                 if(w > maxWidth) maxWidth = w;
1626                 widest = texts[h];
1627             } else {
1628                 if(w > maxTextWidth) maxTextWidth = w;
1629                 if(!widest) widest = texts[h];
1630             }
1631         }
1632     }
1633     if(maxTextWidth + 110 < maxWidth)
1634          maxTextWidth = maxWidth - 110;
1635     else maxWidth = maxTextWidth + 110;
1636     for(h=0; h<height; h++) {
1637         i = h + c*height;
1638         if(i >= cps->nrOptions) break;
1639         j=0;
1640         if(cps->option[i].type == Spin) {
1641             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1642             XtSetValues(texts[h], args, j);
1643         } else
1644         if(cps->option[i].type == TextBox) {
1645             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1646             XtSetValues(texts[h], args, j);
1647         }
1648     }
1649   }
1650     j=0;
1651     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1652     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1653     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1654     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1655     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1656     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1657     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);   
1658     XtAddCallback(b_ok, XtNcallback, SettingsCallback, (XtPointer) 0);
1659
1660     XtSetArg(args[j-1], XtNfromHoriz, b_ok);
1661     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);   
1662     XtAddCallback(b_cancel, XtNcallback, SettingsPopDown, (XtPointer) 0);
1663
1664     XtRealizeWidget(popup);
1665 //    CatchDeleteWindow(popup, "SettingsPopDown");
1666 //    
1667 //    XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1668 //                &x, &y, &win_x, &win_y, &mask);
1669     
1670     XtSetArg(args[0], XtNx, x - 10);
1671     XtSetArg(args[1], XtNy, y - 30);
1672     XtSetValues(popup, args, 2);
1673     
1674     XtPopup(popup, XtGrabExclusive);
1675     SettingsUp = True;
1676
1677     previous = NULL;
1678     if(edit)SetFocus(edit, popup, (XEvent*) NULL, False);
1679 }
1680
1681 void FirstSettingsProc(w, event, prms, nprms)
1682      Widget w;
1683      XEvent *event;
1684      String *prms;
1685      Cardinal *nprms;
1686 {
1687    SettingsPopUp(&first);
1688 }
1689
1690 void SecondSettingsProc(w, event, prms, nprms)
1691      Widget w;
1692      XEvent *event;
1693      String *prms;
1694      Cardinal *nprms;
1695 {
1696    SettingsPopUp(&second);
1697 }
1698
1699 //---------------------------- Chat Windows ----------------------------------------------
1700
1701 void OutputChatMessage(int partner, char *mess)
1702 {
1703     return; // dummy
1704 }
1705