Fix linegap option in board dialog
[xboard.git] / xoptions.c
1 /*
2  * xoptions.c -- Move list window, part of X front end for XBoard
3  *
4  * Copyright 2000, 2009, 2010, 2011 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/Xatom.h>
54 #include <X11/Xaw/Dialog.h>
55 #include <X11/Xaw/Form.h>
56 #include <X11/Xaw/List.h>
57 #include <X11/Xaw/Label.h>
58 #include <X11/Xaw/SimpleMenu.h>
59 #include <X11/Xaw/SmeBSB.h>
60 #include <X11/Xaw/SmeLine.h>
61 #include <X11/Xaw/Box.h>
62 #include <X11/Xaw/Paned.h>
63 #include <X11/Xaw/MenuButton.h>
64 #include <X11/cursorfont.h>
65 #include <X11/Xaw/Text.h>
66 #include <X11/Xaw/AsciiText.h>
67 #include <X11/Xaw/Viewport.h>
68 #include <X11/Xaw/Toggle.h>
69
70 #include "common.h"
71 #include "backend.h"
72 #include "xboard.h"
73 #include "gettext.h"
74
75 #ifdef ENABLE_NLS
76 # define  _(s) gettext (s)
77 # define N_(s) gettext_noop (s)
78 #else
79 # define  _(s) (s)
80 # define N_(s)  s
81 #endif
82
83 extern void SendToProgram P((char *message, ChessProgramState *cps));
84 FILE * XsraSelFile P((Widget w, char *prompt, char *ok, char *cancel, char *failed,
85                 char *init_path, char *mode, int (*show_entry)(), char **name_return));
86
87 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
88 extern Display *xDisplay;
89 extern int squareSize;
90 extern Pixmap xMarkPixmap;
91 extern char *layoutName;
92 extern Window xBoardWindow;
93 extern Arg layoutArgs[2], formArgs[2];
94 Pixel timerForegroundPixel, timerBackgroundPixel;
95 extern int searchTime;
96 extern Atom wm_delete_window;
97 extern int lineGap;
98
99 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
100
101 static Widget previous = NULL;
102
103 void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
104 {
105     Arg args[2];
106     char *s;
107
108     if(previous) {
109         XtSetArg(args[0], XtNdisplayCaret, False);
110         XtSetValues(previous, args, 1);
111     }
112     XtSetArg(args[0], XtNstring, &s);
113     XtGetValues(w, args, 1);
114     XtSetArg(args[0], XtNdisplayCaret, True);
115     XtSetArg(args[1], XtNinsertPosition, strlen(s));
116     XtSetValues(w, args, 2);
117     XtSetKeyboardFocus((Widget) data, w);
118     previous = w;
119 }
120
121 //--------------------------- New Shuffle Game --------------------------------------------
122 extern int shuffleOpenings;
123 extern int startedFromPositionFile;
124 int shuffleUp;
125 Widget shuffleShell;
126
127 void ShufflePopDown()
128 {
129     if (!shuffleUp) return;
130     XtPopdown(shuffleShell);
131     XtDestroyWidget(shuffleShell);
132     shuffleUp = False;
133     ModeHighlight();
134 }
135
136 void ShuffleCallback(w, client_data, call_data)
137      Widget w;
138      XtPointer client_data, call_data;
139 {
140     String name;
141     Widget w2;
142     Arg args[16];
143     char buf[MSG_SIZ];
144
145     XtSetArg(args[0], XtNlabel, &name);
146     XtGetValues(w, args, 1);
147
148     if (strcmp(name, _("cancel")) == 0) {
149         ShufflePopDown();
150         return;
151     }
152     if (strcmp(name, _("off")) == 0) {
153         ShufflePopDown();
154         shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
155         ResetGameEvent();
156         return;
157     }
158     if (strcmp(name, _("random")) == 0) {
159       snprintf(buf, MSG_SIZ,  "%d", rand());
160         XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
161         XtSetValues(XtParent(w), args, 1);
162         return;
163     }
164     if (strcmp(name, _("ok")) == 0) {
165         int nr; String name;
166         name = XawDialogGetValueString(w2 = XtParent(w));
167         if(sscanf(name ,"%d",&nr) != 1) {
168           snprintf(buf, MSG_SIZ,  "%d", appData.defaultFrcPosition);
169             XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
170             XtSetValues(w2, args, 1);
171             return;
172         }
173         appData.defaultFrcPosition = nr;
174         shuffleOpenings = True;
175         ShufflePopDown();
176         ResetGameEvent();
177         return;
178     }
179 }
180
181 void ShufflePopUp()
182 {
183     Arg args[16];
184     Widget popup, layout, dialog, edit;
185     Window root, child;
186     int x, y, i;
187     int win_x, win_y;
188     unsigned int mask;
189     char def[MSG_SIZ];
190
191     i = 0;
192     XtSetArg(args[i], XtNresizable, True); i++;
193     XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
194     shuffleShell = popup =
195       XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
196                          shellWidget, args, i);
197
198     layout =
199       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
200                             layoutArgs, XtNumber(layoutArgs));
201
202     snprintf(def, MSG_SIZ,  "%d\n", appData.defaultFrcPosition);
203     i = 0;
204     XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
205     XtSetArg(args[i], XtNvalue, def); i++;
206     XtSetArg(args[i], XtNborderWidth, 0); i++;
207     dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
208                                    layout, args, i);
209
210 //    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
211 //    XtSetArg(args[1], XtNuseStringInPlace, False);
212 //    XtSetValues(dialog, args, 2);
213
214     XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
215     XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
216     XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
217     XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
218
219     XtRealizeWidget(popup);
220     CatchDeleteWindow(popup, "ShufflePopDown");
221
222     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
223                   &x, &y, &win_x, &win_y, &mask);
224
225     XtSetArg(args[0], XtNx, x - 10);
226     XtSetArg(args[1], XtNy, y - 30);
227     XtSetValues(popup, args, 2);
228
229     XtPopup(popup, XtGrabExclusive);
230     shuffleUp = True;
231
232     edit = XtNameToWidget(dialog, "*value");
233
234     XtSetKeyboardFocus(popup, edit);
235 }
236
237 void ShuffleMenuProc(w, event, prms, nprms)
238      Widget w;
239      XEvent *event;
240      String *prms;
241      Cardinal *nprms;
242 {
243 //    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
244 //      Reset(FALSE, TRUE);
245 //    }
246     ShufflePopUp();
247 }
248
249 //--------------------------- Time-Control Menu Popup ----------------------------------
250 int TimeControlUp;
251 Widget TimeControlShell;
252 int tcInc;
253 Widget tcMess1, tcMess2, tcData, tcTime, tcOdds1, tcOdds2;
254 int tcIncrement, tcMoves;
255
256 void TimeControlPopDown()
257 {
258     if (!TimeControlUp) return;
259     previous = NULL;
260     XtPopdown(TimeControlShell);
261     XtDestroyWidget(TimeControlShell);
262     TimeControlUp = False;
263     ModeHighlight();
264 }
265
266 void TimeControlCallback(w, client_data, call_data)
267      Widget w;
268      XtPointer client_data, call_data;
269 {
270     String name, txt;
271     Arg args[16];
272     char buf[MSG_SIZ];
273     int j;
274
275     XtSetArg(args[0], XtNlabel, &name);
276     XtGetValues(w, args, 1);
277
278     if (strcmp(name, _("classical")) == 0) {
279         if(tcInc == 0) return;
280         j=0;
281         XtSetArg(args[j], XtNlabel, _("minutes for each")); j++;
282         XtSetValues(tcMess1, args, j);
283         j=0;
284         XtSetArg(args[j], XtNlabel, _("moves")); j++;
285         XtSetValues(tcMess2, args, j);
286         if(tcInc == 1) {
287             j=0;
288             XtSetArg(args[j], XtNstring, &name); j++;
289             XtGetValues(tcData, args, j);
290             tcIncrement = 0; sscanf(name, "%d", &tcIncrement);
291         }
292         snprintf(buf, MSG_SIZ,  "%d", tcMoves);
293         j=0;
294         XtSetArg(args[j], XtNstring, buf); j++;
295         XtSetValues(tcData, args, j);
296         tcInc = 0;
297         return;
298     }
299     if (strcmp(name, _("incremental")) == 0) {
300         if(tcInc == 1) return;
301         j=0;
302         XtSetArg(args[j], XtNlabel, _("minutes, plus")); j++;
303         XtSetValues(tcMess1, args, j);
304         j=0;
305         XtSetArg(args[j], XtNlabel, _("sec/move")); j++;
306         XtSetValues(tcMess2, args, j);
307         if(tcInc == 0) {
308             j=0;
309             XtSetArg(args[j], XtNstring, &name); j++;
310             XtGetValues(tcData, args, j);
311             tcMoves = appData.movesPerSession; sscanf(name, "%d", &tcMoves);
312         }
313         snprintf(buf, MSG_SIZ,  "%d", tcIncrement);
314         j=0;
315         XtSetArg(args[j], XtNstring, buf); j++;
316         XtSetValues(tcData, args, j);
317         tcInc = 1;
318         return;
319     }
320     if (strcmp(name, _("fixed time")) == 0) {
321         if(tcInc == 2) return;
322         j=0;
323         XtSetArg(args[j], XtNlabel, _("sec/move (max)")); j++;
324         XtSetValues(tcMess1, args, j);
325         j=0;
326         XtSetArg(args[j], XtNlabel, _("")); j++;
327         XtSetValues(tcMess2, args, j);
328         j=0;
329         XtSetArg(args[j], XtNstring, ""); j++;
330         XtSetValues(tcData, args, j);
331         tcInc = 2;
332         return;
333     }
334     if (strcmp(name, _(" OK ")) == 0) {
335         int inc, mps, ok;
336         XtSetArg(args[0], XtNstring, &txt);
337         XtGetValues(tcData, args, 1);
338         switch(tcInc) {
339           case 1:
340             ok = sscanf(txt, "%d", &inc); mps = 0;
341             if(!ok && txt[0] == 0) { inc = 0; ok = 1; } // accept empty string as zero
342             ok &= (inc >= 0);
343             break;
344           case 0:
345             ok = sscanf(txt, "%d", &mps); inc = -1;
346             ok &= (mps > 0);
347             break;
348           case 2:
349             ok = 1; inc = -1; mps = 40;
350         }
351         if(ok != 1) {
352             XtSetArg(args[0], XtNstring, ""); // erase any offending input
353             XtSetValues(tcData, args, 1);
354             return;
355         }
356         XtSetArg(args[0], XtNstring, &txt);
357         XtGetValues(tcTime, args, 1);
358         if(tcInc == 2) {
359             if(sscanf(txt, "%d", &inc) != 1) {
360                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
361                 XtSetValues(tcTime, args, 1);
362                 DisplayError(_("Bad Time-Control String"), 0);
363                 return;
364             }
365             searchTime = inc;
366         } else {
367             if(!ParseTimeControl(txt, inc, mps)) {
368                 XtSetArg(args[0], XtNstring, ""); // erase any offending input
369                 XtSetValues(tcTime, args, 1);
370                 DisplayError(_("Bad Time-Control String"), 0);
371                 return;
372             }
373             searchTime = 0;
374             appData.movesPerSession = mps;
375             appData.timeIncrement = inc;
376             appData.timeControl = strdup(txt);
377         }
378         XtSetArg(args[0], XtNstring, &txt);
379         XtGetValues(tcOdds1, args, 1);
380         appData.firstTimeOdds = first.timeOdds
381                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
382         XtGetValues(tcOdds2, args, 1);
383         appData.secondTimeOdds = second.timeOdds
384                 = (sscanf(txt, "%d", &j) == 1 && j > 0) ? j : 1;
385
386         Reset(True, True);
387         TimeControlPopDown();
388         return;
389     }
390 }
391
392 void TimeControlPopUp()
393 {
394     Arg args[16];
395     Widget popup, layout, form,  b_ok, b_cancel, b_clas, b_inc, mess;
396     Window root, child;
397     int x, y, i, j;
398     int win_x, win_y;
399     unsigned int mask;
400     char def[MSG_SIZ];
401
402     tcInc = searchTime > 0 ? 2 : (appData.timeIncrement >= 0);
403     tcMoves = appData.movesPerSession; tcIncrement = appData.timeIncrement;
404     if(!tcInc) tcIncrement = 0;
405     snprintf(def, MSG_SIZ,  "%d", tcInc ? tcIncrement : tcMoves);
406
407     i = 0;
408     XtSetArg(args[i], XtNresizable, True); i++;
409 //    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
410     TimeControlShell = popup =
411       XtCreatePopupShell(_("TimeControl Menu"), transientShellWidgetClass,
412                          shellWidget, args, i);
413
414     layout =
415       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
416                             layoutArgs, XtNumber(layoutArgs));
417
418     form =
419       XtCreateManagedWidget(layoutName, formWidgetClass, layout,
420                             formArgs, XtNumber(formArgs));
421
422     j = 0;
423 //    XtSetArg(args[j], XtNwidth,     (XtArgVal) 300); j++;
424 //    XtSetArg(args[j], XtNheight,    (XtArgVal) 85); j++;
425     XtSetValues(popup, args, j);
426
427     j= 0;
428     XtSetArg(args[j], XtNborderWidth, 1); j++;
429     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
430     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
431     XtSetArg(args[j], XtNstring, appData.timeControl);  j++;
432     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
433     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
434     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
435     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
436     XtSetArg(args[j], XtNright, XtChainRight);  j++;
437     XtSetArg(args[j], XtNresizable, True);  j++;
438     XtSetArg(args[j], XtNwidth,  85);  j++;
439     XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
440     tcTime = XtCreateManagedWidget("TC", asciiTextWidgetClass, form, args, j);
441     XtAddEventHandler(tcTime, ButtonPressMask, False, SetFocus, (XtPointer) popup);
442
443     j= 0;
444     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("sec/move (max)   ") : _("   minutes, plus   ") : _("minutes for each")); j++;
445     XtSetArg(args[j], XtNborderWidth, 0); j++;
446     XtSetArg(args[j], XtNfromHoriz, tcTime); j++;
447     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
448     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
449     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
450     XtSetArg(args[j], XtNright, XtChainRight);  j++;
451   //  XtSetArg(args[j], XtNwidth,  100);  j++;
452   //  XtSetArg(args[j], XtNheight, 20);  j++;
453     tcMess1 = XtCreateManagedWidget("TCtext", labelWidgetClass, form, args, j);
454
455     j= 0;
456     XtSetArg(args[j], XtNborderWidth, 1); j++;
457     XtSetArg(args[j], XtNfromHoriz, tcMess1); j++;
458     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
459     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
460     XtSetArg(args[j], XtNstring, def);  j++;
461     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
462     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
463     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
464     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
465     XtSetArg(args[j], XtNright, XtChainRight);  j++;
466     XtSetArg(args[j], XtNresizable, True);  j++;
467     XtSetArg(args[j], XtNwidth,  40);  j++;
468 //    XtSetArg(args[j], XtNheight, 20);  j++;
469     tcData = XtCreateManagedWidget("MPS", asciiTextWidgetClass, form, args, j);
470     XtAddEventHandler(tcData, ButtonPressMask, False, SetFocus, (XtPointer) popup);
471
472     j= 0;
473     XtSetArg(args[j], XtNlabel, tcInc ? tcInc == 2 ? _("             ") : _("sec/move") : _("moves     ")); j++;
474     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
475     XtSetArg(args[j], XtNborderWidth, 0); j++;
476     XtSetArg(args[j], XtNfromHoriz, tcData); j++;
477     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
478     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
479     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
480     XtSetArg(args[j], XtNright, XtChainRight);  j++;
481 //    XtSetArg(args[j], XtNwidth,  80);  j++;
482 //    XtSetArg(args[j], XtNheight, 20);  j++;
483     tcMess2 = XtCreateManagedWidget("MPStext", labelWidgetClass,
484                                    form, args, j);
485
486     j= 0;
487     XtSetArg(args[j], XtNborderWidth, 1); j++;
488     XtSetArg(args[j], XtNfromVert, tcTime); j++;
489     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
490     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
491     XtSetArg(args[j], XtNstring, "1");  j++;
492     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
493     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
494     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
495     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
496     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
497     XtSetArg(args[j], XtNresizable, True);  j++;
498     XtSetArg(args[j], XtNwidth,  40);  j++;
499 //    XtSetArg(args[j], XtNheight, 20);  j++;
500     tcOdds1 = XtCreateManagedWidget("Odds1", asciiTextWidgetClass, form, args, j);
501     XtAddEventHandler(tcOdds1, ButtonPressMask, False, SetFocus, (XtPointer) popup);
502
503     j= 0;
504     XtSetArg(args[j], XtNborderWidth, 1); j++;
505     XtSetArg(args[j], XtNfromVert, tcTime); j++;
506     XtSetArg(args[j], XtNfromHoriz, tcOdds1); j++;
507     XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
508     XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
509     XtSetArg(args[j], XtNstring, "1");  j++;
510     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
511     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
512     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
513     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
514     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
515     XtSetArg(args[j], XtNresizable, True);  j++;
516     XtSetArg(args[j], XtNwidth,  40);  j++;
517 //    XtSetArg(args[j], XtNheight, 20);  j++;
518     tcOdds2 = XtCreateManagedWidget("Odds2", asciiTextWidgetClass, form, args, j);
519     XtAddEventHandler(tcOdds2, ButtonPressMask, False, SetFocus, (XtPointer) popup);
520
521     j= 0;
522     XtSetArg(args[j], XtNlabel, _("Engine #1 and #2 Time-Odds Factors")); j++;
523     XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
524     XtSetArg(args[j], XtNborderWidth, 0); j++;
525     XtSetArg(args[j], XtNfromVert, tcTime); j++;
526     XtSetArg(args[j], XtNfromHoriz, tcOdds2); j++;
527     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
528     XtSetArg(args[j], XtNbottom, XtChainTop);  j++;
529     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
530     XtSetArg(args[j], XtNright, XtChainRight);  j++;
531 //    XtSetArg(args[j], XtNwidth,  200);  j++;
532 //    XtSetArg(args[j], XtNheight, 20);  j++;
533     mess = XtCreateManagedWidget("Oddstext", labelWidgetClass,
534                                    form, args, j);
535     j=0;
536     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
537     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
538     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
539     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
540     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
541     XtSetArg(args[j], XtNstate, tcInc==0); j++;
542     b_clas= XtCreateManagedWidget(_("classical"), toggleWidgetClass,
543                                    form, args, j);
544     XtAddCallback(b_clas, XtNcallback, TimeControlCallback, (XtPointer) 0);
545
546     j=0;
547     XtSetArg(args[j], XtNradioGroup, b_clas); j++;
548     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
549     XtSetArg(args[j], XtNfromHoriz, b_clas);  j++;
550     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
551     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
552     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
553     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
554     XtSetArg(args[j], XtNstate, tcInc==1); j++;
555     b_inc = XtCreateManagedWidget(_("incremental"), toggleWidgetClass,
556                                    form, args, j);
557     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
558
559     j=0;
560     XtSetArg(args[j], XtNradioGroup, b_inc); j++;
561     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
562     XtSetArg(args[j], XtNfromHoriz, b_inc);  j++;
563     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
564     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
565     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
566     XtSetArg(args[j], XtNright, XtChainLeft);  j++;
567     XtSetArg(args[j], XtNstate, tcInc==2); j++;
568     b_inc = XtCreateManagedWidget(_("fixed time"), toggleWidgetClass,
569                                    form, args, j);
570     XtAddCallback(b_inc, XtNcallback, TimeControlCallback, (XtPointer) 0);
571
572     j=0;
573     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
574     XtSetArg(args[j], XtNfromHoriz, tcData);  j++;
575     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
576     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
577     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
578     XtSetArg(args[j], XtNright, XtChainRight);  j++;
579     b_ok= XtCreateManagedWidget(_(" OK "), commandWidgetClass,
580                                    form, args, j);
581     XtAddCallback(b_ok, XtNcallback, TimeControlCallback, (XtPointer) 0);
582
583     j=0;
584     XtSetArg(args[j], XtNfromVert, tcOdds1);  j++;
585     XtSetArg(args[j], XtNfromHoriz, b_ok);  j++;
586     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
587     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
588     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
589     XtSetArg(args[j], XtNright, XtChainRight);  j++;
590     b_cancel= XtCreateManagedWidget(_("cancel"), commandWidgetClass,
591                                    form, args, j);
592     XtAddCallback(b_cancel, XtNcallback, TimeControlPopDown, (XtPointer) 0);
593
594     XtRealizeWidget(popup);
595     CatchDeleteWindow(popup, "TimeControlPopDown");
596
597     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
598                   &x, &y, &win_x, &win_y, &mask);
599
600     XtSetArg(args[0], XtNx, x - 10);
601     XtSetArg(args[1], XtNy, y - 30);
602     XtSetValues(popup, args, 2);
603
604     XtPopup(popup, XtGrabExclusive);
605     TimeControlUp = True;
606
607     previous = NULL;
608     SetFocus(tcTime, popup, (XEvent*) NULL, False);
609 //    XtSetKeyboardFocus(popup, tcTime);
610 }
611
612 void TimeControlProc(w, event, prms, nprms)
613      Widget w;
614      XEvent *event;
615      String *prms;
616      Cardinal *nprms;
617 {
618    TimeControlPopUp();
619 }
620
621 //--------------------------- Engine-specific options menu ----------------------------------
622
623 int values[MAX_OPTIONS];
624 ChessProgramState *currentCps;
625 static Option *currentOption;
626 extern Widget shells[];
627
628 void SpinCallback(w, client_data, call_data)
629      Widget w;
630      XtPointer client_data, call_data;
631 {
632     String name, val;
633     Arg args[16];
634     char buf[MSG_SIZ], *p;
635     int j;
636     int data = (intptr_t) client_data;
637
638     XtSetArg(args[0], XtNlabel, &name);
639     XtGetValues(w, args, 1);
640
641     j = 0;
642     XtSetArg(args[0], XtNstring, &val);
643     XtGetValues(currentOption[data].handle, args, 1);
644     sscanf(val, "%d", &j);
645     if (strcmp(name, "browse") == 0) {
646         if(XsraSelFile(shells[0], currentOption[data].name, NULL, NULL, "", "", 
647                                   currentOption[data].type == PathName ? "p" : "f", NULL, &p)) {
648                 int len = strlen(p);
649                 if(len && p[len-1] == '/') p[len-1] = NULLCHAR;
650                 XtSetArg(args[0], XtNstring, p);
651                 XtSetValues(currentOption[data].handle, args, 1);
652         }
653         SetFocus(currentOption[data].handle, shells[0], (XEvent*) NULL, False);
654         return;
655     } else
656     if (strcmp(name, "+") == 0) {
657         if(++j > currentOption[data].max) return;
658     } else
659     if (strcmp(name, "-") == 0) {
660         if(--j < currentOption[data].min) return;
661     } else return;
662     snprintf(buf, MSG_SIZ,  "%d", j);
663     XtSetArg(args[0], XtNstring, buf);
664     XtSetValues(currentOption[data].handle, args, 1);
665 }
666
667 void ComboSelect(w, addr, index) // callback for all combo items
668      Widget w;
669      caddr_t addr;
670      caddr_t index;
671 {
672     Arg args[16];
673     int i = ((intptr_t)addr)>>8;
674     int j = 255 & (intptr_t) addr;
675
676     values[i] = j; // store in temporary, for transfer at OK
677     XtSetArg(args[0], XtNlabel, ((char**)currentOption[i].textValue)[j]);
678     XtSetValues(currentOption[i].handle, args, 1);
679 }
680
681 void CreateComboPopup(parent, name, n, mb)
682      Widget parent;
683      String name;
684      int n;
685      char *mb[];
686 {
687     int i=0, j;
688     Widget menu, entry;
689     Arg args[16];
690
691     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
692                               parent, NULL, 0);
693     j = 0;
694     XtSetArg(args[j], XtNwidth, 100);  j++;
695 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
696     while (mb[i] != NULL) {
697             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
698                                           menu, args, j);
699             XtAddCallback(entry, XtNcallback,
700                           (XtCallbackProc) ComboSelect,
701                           (caddr_t)(intptr_t) (256*n+i));
702         i++;
703     }
704 }
705
706
707 //----------------------------Generic dialog --------------------------------------------
708
709 // cloned from Engine Settings dialog (and later merged with it)
710
711 typedef void ButtonCallback(int n);
712
713 char *trialSound;
714 static int oldCores, oldPonder;
715 int MakeColors P((void));
716 void CreateGCs P((int redo));
717 void CreateXPMBoard P((char *s, int kind));
718 void CreateXPMPieces P((void));
719 void GenericReadout();
720 Widget shells[10];
721 Widget marked[10];
722 Boolean shellUp[10];
723 WindowPlacement *wp[10];
724 Option *dialogOptions[10];
725
726 void MarkMenu(char *item, int dlgNr)
727 {
728     Arg args[2];
729     XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
730     XtSetValues(marked[dlgNr] = XtNameToWidget(menuBarWidget, item), args, 1);
731 }
732
733 int PopDown(int n)
734 {
735     int j;
736     Arg args[10];
737     Dimension windowH, windowW; Position windowX, windowY;
738     if (!shellUp[n]) return 0;
739     if(n && wp[n]) { // remember position
740         j = 0;
741         XtSetArg(args[j], XtNx, &windowX); j++;
742         XtSetArg(args[j], XtNy, &windowY); j++;
743         XtSetArg(args[j], XtNheight, &windowH); j++;
744         XtSetArg(args[j], XtNwidth, &windowW); j++;
745         XtGetValues(shells[n], args, j);
746         wp[n]->x = windowX;
747         wp[n]->x = windowY;
748         wp[n]->width  = windowW;
749         wp[n]->height = windowH;
750     }
751     previous = NULL;
752     XtPopdown(shells[n]);
753     if(n == 0) XtDestroyWidget(shells[n]);
754     shellUp[n] = False;
755     if(marked[n]) {
756         XtSetArg(args[0], XtNleftBitmap, None);
757         XtSetValues(marked[n], args, 1);
758     }
759     if(!n) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
760     return 1;
761 }
762
763 void GenericPopDown(w, event, prms, nprms)
764      Widget w;
765      XEvent *event;
766      String *prms;
767      Cardinal *nprms;
768 {
769     int n;
770     PopDown(prms[0][0] - '0');
771 }
772
773 Option matchOptions[] = {
774 { 0,  2, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, _("Default Number of Games in Match:") },
775 { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, _("Pause between Match Games (msec):") },
776 { 0,  0,          0, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, _("Game File with Opening Lines:") },
777 { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, _("Game Number (-1 or -2 = Auto-Increment):") },
778 { 0,  0,          0, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, _("File with Start Positions:") },
779 { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, _("Position Number (-1 or -2 = Auto-Increment):") },
780 { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, _("Rewind Index after this many Games (0 = never):") },
781 { 0, 0, 0, NULL, NULL, "", NULL, EndMark , "" }
782 };
783
784 void GeneralOptionsOK(int n)
785 {
786         int newPonder = appData.ponderNextMove;
787         appData.ponderNextMove = oldPonder;
788         PonderNextMoveEvent(newPonder);
789 }
790
791 Option generalOptions[] = {
792 { 0,  0, 0, NULL, (void*) &appData.alwaysPromoteToQueen, "", NULL, CheckBox, _("Always Queen") },
793 { 0,  0, 0, NULL, (void*) &appData.animateDragging, "", NULL, CheckBox, _("Animate Dragging") },
794 { 0,  0, 0, NULL, (void*) &appData.animate, "", NULL, CheckBox, _("Animate Moving") },
795 { 0,  0, 0, NULL, (void*) &appData.autoCallFlag, "", NULL, CheckBox, _("Auto Flag") },
796 { 0,  0, 0, NULL, (void*) &appData.autoFlipView, "", NULL, CheckBox, _("Auto Flip View") },
797 { 0,  0, 0, NULL, (void*) &appData.blindfold, "", NULL, CheckBox, _("Blindfold") },
798 { 0,  0, 0, NULL, (void*) &appData.dropMenu, "", NULL, CheckBox, _("Drop Menu") },
799 { 0,  0, 0, NULL, (void*) &appData.highlightDragging, "", NULL, CheckBox, _("Highlight Dragging (Show Move Targets)") },
800 { 0,  0, 0, NULL, (void*) &appData.highlightLastMove, "", NULL, CheckBox, _("Highlight Last Move") },
801 { 0,  0, 0, NULL, (void*) &appData.highlightMoveWithArrow, "", NULL, CheckBox, _("Highlight with Arrow") },
802 { 0,  0, 0, NULL, (void*) &appData.ringBellAfterMoves, "", NULL, CheckBox, _("Move Sound") },
803 { 0,  0, 0, NULL, (void*) &appData.oneClick, "", NULL, CheckBox, _("One-Click Moving") },
804 { 0,  0, 0, NULL, (void*) &appData.periodicUpdates, "", NULL, CheckBox, _("Periodic Updates (in Analysis Mode)") },
805 { 0,  0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
806 { 0,  0, 0, NULL, (void*) &appData.popupExitMessage, "", NULL, CheckBox, _("Popup Exit Messages") },
807 { 0,  0, 0, NULL, (void*) &appData.popupMoveErrors, "", NULL, CheckBox, _("Popup Move Errors") },
808 { 0,  0, 0, NULL, (void*) &appData.showCoords, "", NULL, CheckBox, _("Show Coordinates") },
809 { 0,  0, 0, NULL, (void*) &appData.markers, "", NULL, CheckBox, _("Show Target Squares") },
810 { 0,  0, 0, NULL, (void*) &appData.hideThinkingFromHuman, "", NULL, CheckBox, _("Hide Thinking from Human") },
811 { 0,  0, 0, NULL, (void*) &appData.testLegality, "", NULL, CheckBox, _("Test Legality") },
812 { 0, 0, 10, NULL, (void*) &appData.flashCount, "", NULL, Spin, _("Flash Moves (0 = no flashing):") },
813 { 0, 1, 10, NULL, (void*) &appData.flashRate, "", NULL, Spin, _("Flash Rate (high = fast):") },
814 { 0, 5, 100,NULL, (void*) &appData.animSpeed, "", NULL, Spin, _("Animation Speed (high = slow):") },
815 { 0,  0, 0, NULL, (void*) &GeneralOptionsOK, "", NULL, EndMark , "" }
816 };
817
818 void Pick(int n)
819 {
820         VariantClass v = currentOption[n].value;
821         if(!appData.noChessProgram) {
822             char *name = VariantName(v), buf[MSG_SIZ];
823             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
824                 /* [HGM] in protocol 2 we check if variant is suported by engine */
825               snprintf(buf, MSG_SIZ,  _("Variant %s not supported by %s"), name, first.tidy);
826                 DisplayError(buf, 0);
827                 return; /* ignore OK if first engine does not support it */
828             } else
829             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
830               snprintf(buf, MSG_SIZ,  _("Warning: second engine (%s) does not support this!"), second.tidy);
831                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
832             }
833         }
834
835         GenericReadout(); // make sure ranks and file settings are read
836
837         gameInfo.variant = v;
838         appData.variant = VariantName(v);
839
840         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
841         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
842         appData.pieceToCharTable = NULL;
843         appData.pieceNickNames = "";
844         appData.colorNickNames = "";
845         Reset(True, True);
846         PopDown(0);
847         return;
848 }
849
850 Option variantDescriptors[] = {
851 { VariantNormal, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("normal")},
852 { VariantFairy, 1, 135, NULL, (void*) &Pick, "#BFBFBF", NULL, Button, N_("fairy")},
853 { VariantFischeRandom, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("FRC")},
854 { VariantSChess, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Seirawan")},
855 { VariantWildCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("wild castle")},
856 { VariantSuper, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Superchess")},
857 { VariantNoCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("no castle")},
858 { VariantCrazyhouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("crazyhouse")},
859 { VariantKnightmate, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("knightmate")},
860 { VariantBughouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("bughouse")},
861 { VariantBerolina, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("berolina")},
862 { VariantShogi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("shogi (9x9)")},
863 { VariantCylinder, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("cylinder")},
864 { VariantXiangqi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("xiangqi (9x10)")},
865 { VariantShatranj, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("shatranj")},
866 { VariantCourier, 1, 135, NULL, (void*) &Pick, "#BFFFBF", NULL, Button, N_("courier (12x8)")},
867 { VariantMakruk, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("makruk")},
868 { VariantGreat, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Great Shatranj (10x8)")},
869 { VariantAtomic, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("atomic")},
870 { VariantCapablanca, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Capablanca (10x8)")},
871 { VariantTwoKings, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("two kings")},
872 { VariantGothic, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Gothic (10x8)")},
873 { Variant3Check, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("3-checks")},
874 { VariantJanus, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("janus (10x8)")},
875 { VariantSuicide, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("suicide")},
876 { VariantCapaRandom, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("CRC (10x8)")},
877 { VariantGiveaway, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("give-away")},
878 { VariantSpartan, 1, 135, NULL, (void*) &Pick, "#FF0000", NULL, Button, N_("Spartan")},
879 { VariantLosers, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("losers")},
880 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Board size ( -1 = default for selected variant):")},
881 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.NrRanks, "", NULL, Spin, N_("Number of Board Ranks:") },
882 { 0, -1, BOARD_FILES, NULL, (void*) &appData.NrFiles, "", NULL, Spin, N_("Number of Board Files:") },
883 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.holdingsSize, "", NULL, Spin, N_("Holdings Size:") },
884 { 0, 0, 0, NULL, NULL, NULL, NULL, Label,
885                                 _("WARNING: variants with un-orthodox\n"
886                                   "pieces only have built-in bitmaps\n"
887                                   "for -boardSize middling, bulky and\n"
888                                   "petite, and substitute king or amazon\n"
889                                   "for missing bitmaps. (See manual.)")},
890 { 0, 2, 0, NULL, NULL, "", NULL, EndMark , "" }
891 };
892
893 void CommonOptionsOK(int n)
894 {
895         int newPonder = appData.ponderNextMove;
896         // make sure changes are sent to first engine by re-initializing it
897         // if it was already started pre-emptively at end of previous game
898         if(gameMode == BeginningOfGame) Reset(True, True); else {
899             // Some changed setting need immediate sending always.
900             if(oldCores != appData.smpCores)
901                 NewSettingEvent(False, &(first.maxCores), "cores", appData.smpCores);
902             appData.ponderNextMove = oldPonder;
903             PonderNextMoveEvent(newPonder);
904         }
905 }
906
907 Option commonEngineOptions[] = {
908 { 0,     0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, _("Ponder Next Move") },
909 { 0,  0, 1000, NULL, (void*) &appData.smpCores, "", NULL, Spin, _("Maximum Number of CPUs per Engine:") },
910 { 0,     0, 0, NULL, (void*) &appData.polyglotDir, "", NULL, PathName, _("Polygot Directory:") },
911 { 0, 0, 16000, NULL, (void*) &appData.defaultHashSize, "", NULL, Spin, _("Hash-Table Size (MB):") },
912 { 0,     0, 0, NULL, (void*) &appData.defaultPathEGTB, "", NULL, PathName, _("Nalimov EGTB Path:") },
913 { 0,  0, 1000, NULL, (void*) &appData.defaultCacheSizeEGTB, "", NULL, Spin, _("EGTB Cache Size (MB):") },
914 { 0,     0, 0, NULL, (void*) &appData.usePolyglotBook, "", NULL, CheckBox, _("Use GUI Book") },
915 { 0,     0, 0, NULL, (void*) &appData.polyglotBook, "", NULL, FileName, _("Opening-Book Filename:") },
916 { 0,   0, 100, NULL, (void*) &appData.bookDepth, "", NULL, Spin, _("Book Depth (moves):") },
917 { 0,   0, 100, NULL, (void*) &appData.bookStrength, "", NULL, Spin, _("Book Variety (0) vs. Strength (100):") },
918 { 0,     0, 0, NULL, (void*) &appData.firstHasOwnBookUCI, "", NULL, CheckBox, _("Engine #1 Has Own Book") },
919 { 0,     0, 0, NULL, (void*) &appData.secondHasOwnBookUCI, "", NULL, CheckBox, _("Engine #2 Has Own Book          ") },
920 { 0,     1, 0, NULL, (void*) &CommonOptionsOK, "", NULL, EndMark , "" }
921 };
922
923 Option adjudicationOptions[] = {
924 { 0, 0,    0, NULL, (void*) &appData.checkMates, "", NULL, CheckBox, _("Detect all Mates") },
925 { 0, 0,    0, NULL, (void*) &appData.testClaims, "", NULL, CheckBox, _("Verify Engine Result Claims") },
926 { 0, 0,    0, NULL, (void*) &appData.materialDraws, "", NULL, CheckBox, _("Draw if Insufficient Mating Material") },
927 { 0, 0,    0, NULL, (void*) &appData.trivialDraws, "", NULL, CheckBox, _("Adjudicate Trivial Draws (3-Move Delay)") },
928 { 0, 0,  100, NULL, (void*) &appData.ruleMoves, "", NULL, Spin, _("N-Move Rule:") },
929 { 0, 0,    6, NULL, (void*) &appData.drawRepeats, "", NULL, Spin, _("N-fold Repeats:") },
930 { 0, 0, 1000, NULL, (void*) &appData.adjudicateDrawMoves, "", NULL, Spin, _("Draw after N Moves Total:") },
931 { 0,-5000, 0, NULL, (void*) &appData.adjudicateLossThreshold, "", NULL, Spin, _("Win / Loss Threshold:") },
932 { 0, 0,    0, NULL, (void*) &first.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #1") },
933 { 0, 0,    0, NULL, (void*) &second.scoreIsAbsolute, "", NULL, CheckBox, _("Negate Score of Engine #2") },
934 { 0, 1,    0, NULL, NULL, "", NULL, EndMark , "" }
935 };
936
937 void IcsOptionsOK(int n)
938 {
939     ParseIcsTextColors();
940 }
941
942 Option icsOptions[] = {
943 { 0, 0, 0, NULL, (void*) &appData.autoKibitz, "",  NULL, CheckBox, _("Auto-Kibitz") },
944 { 0, 0, 0, NULL, (void*) &appData.autoComment, "", NULL, CheckBox, _("Auto-Comment") },
945 { 0, 0, 0, NULL, (void*) &appData.autoObserve, "", NULL, CheckBox, _("Auto-Observe") },
946 { 0, 0, 0, NULL, (void*) &appData.autoRaiseBoard, "", NULL, CheckBox, _("Auto-Raise Board") },
947 { 0, 0, 0, NULL, (void*) &appData.bgObserve, "",   NULL, CheckBox, _("Background Observe while Playing") },
948 { 0, 0, 0, NULL, (void*) &appData.dualBoard, "",   NULL, CheckBox, _("Dual Board for Background-Observed Game") },
949 { 0, 0, 0, NULL, (void*) &appData.getMoveList, "", NULL, CheckBox, _("Get Move List") },
950 { 0, 0, 0, NULL, (void*) &appData.quietPlay, "",   NULL, CheckBox, _("Quiet Play") },
951 { 0, 0, 0, NULL, (void*) &appData.seekGraph, "",   NULL, CheckBox, _("Seek Graph") },
952 { 0, 0, 0, NULL, (void*) &appData.autoRefresh, "", NULL, CheckBox, _("Auto-Refresh Seek Graph") },
953 { 0, 0, 0, NULL, (void*) &appData.premove, "",     NULL, CheckBox, _("Premove") },
954 { 0, 0, 0, NULL, (void*) &appData.premoveWhite, "", NULL, CheckBox, _("Premove for White") },
955 { 0, 0, 0, NULL, (void*) &appData.premoveWhiteText, "", NULL, TextBox, _("First White Move:") },
956 { 0, 0, 0, NULL, (void*) &appData.premoveBlack, "", NULL, CheckBox, _("Premove for Black") },
957 { 0, 0, 0, NULL, (void*) &appData.premoveBlackText, "", NULL, TextBox, _("First Black Move:") },
958 { 0, 0, 0, NULL, NULL, NULL, NULL, Break, "" },
959 { 0, 0, 0, NULL, (void*) &appData.icsAlarm, "", NULL, CheckBox, _("Alarm") },
960 { 0, 0, 100000000, NULL, (void*) &appData.icsAlarmTime, "", NULL, Spin, _("Alarm Time (msec):") },
961 //{ 0, 0, 0, NULL, (void*) &appData.chatBoxes, "", NULL, TextBox, _("Startup Chat Boxes:") },
962 { 0, 0, 0, NULL, (void*) &appData.colorize, "", NULL, CheckBox, _("Colorize Messages") },
963 { 0, 0, 0, NULL, (void*) &appData.colorShout, "", NULL, TextBox, _("Shout Text Colors:") },
964 { 0, 0, 0, NULL, (void*) &appData.colorSShout, "", NULL, TextBox, _("S-Shout Text Colors:") },
965 { 0, 0, 0, NULL, (void*) &appData.colorChannel1, "", NULL, TextBox, _("Channel #1 Text Colors:") },
966 { 0, 0, 0, NULL, (void*) &appData.colorChannel, "", NULL, TextBox, _("Other Channel Text Colors:") },
967 { 0, 0, 0, NULL, (void*) &appData.colorKibitz, "", NULL, TextBox, _("Kibitz Text Colors:") },
968 { 0, 0, 0, NULL, (void*) &appData.colorTell, "", NULL, TextBox, _("Tell Text Colors:") },
969 { 0, 0, 0, NULL, (void*) &appData.colorChallenge, "", NULL, TextBox, _("Challenge Text Colors:") },
970 { 0, 0, 0, NULL, (void*) &appData.colorRequest, "", NULL, TextBox, _("Request Text Colors:") },
971 { 0, 0, 0, NULL, (void*) &appData.colorSeek, "", NULL, TextBox, _("Seek Text Colors:") },
972 { 0, 0, 0, NULL, (void*) &IcsOptionsOK, "", NULL, EndMark , "" }
973 };
974
975 Option loadOptions[] = {
976 { 0, 0, 0, NULL, (void*) &appData.autoDisplayTags, "", NULL, CheckBox, _("Auto-Display Tags") },
977 { 0, 0, 0, NULL, (void*) &appData.autoDisplayComment, "", NULL, CheckBox, _("Auto-Display Comment") },
978 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, _("Auto-Play speed of loaded games\n(0 = instant, -1 = off):") },
979 { 0, -1, 10000000, NULL, (void*) &appData.timeDelay, "", NULL, Fractional, _("Seconds per Move:") },
980 { 0,  0, 0, NULL, NULL, "", NULL, EndMark , "" }
981 };
982
983 Option saveOptions[] = {
984 { 0, 0, 0, NULL, (void*) &appData.autoSaveGames, "", NULL, CheckBox, _("Auto-Save Games") },
985 { 0, 0, 0, NULL, (void*) &appData.saveGameFile, "", NULL, FileName,  _("Save Games on File:") },
986 { 0, 0, 0, NULL, (void*) &appData.savePositionFile, "", NULL, FileName,  _("Save Final Positions on File:") },
987 { 0, 0, 0, NULL, (void*) &appData.pgnEventHeader, "", NULL, TextBox,  _("PGN Event Header:") },
988 { 0, 0, 0, NULL, (void*) &appData.oldSaveStyle, "", NULL, CheckBox, _("Old Save Style (as opposed to PGN)") },
989 { 0, 0, 0, NULL, (void*) &appData.saveExtendedInfoInPGN, "", NULL, CheckBox, _("Save Score/Depth Info in PGN") },
990 { 0, 0, 0, NULL, (void*) &appData.saveOutOfBookInfo, "", NULL, CheckBox, _("Save Out-of-Book Info in PGN           ") },
991 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
992 };
993
994 char *soundNames[] = {
995         N_("No Sound"),
996         N_("Default Beep"),
997         N_("Above WAV File"),
998         N_("Ching"),
999         N_("Click"),
1000         N_("Ding"),
1001         N_("Gong"),
1002         N_("Laser"),
1003         N_("Penalty"),
1004         N_("Phone"),
1005         N_("Thud"),
1006         N_("Challenge"),
1007         N_("Tell"),
1008         NULL,
1009         N_("User File")
1010 };
1011
1012 char *soundFiles[] = { // sound files corresponding to above names
1013         "",
1014         "$",
1015         "*", // kludge alert: as first thing in the dialog readout this is replaced with the user-given .WAV filename
1016         "ching.wav",
1017         "click.wav",
1018         "ding1.wav",
1019         "gong.wav",
1020         "laser.wav",
1021         "penalty.wav",
1022         "phone.wav",
1023         "thud.wav",
1024         "challenge.wav",
1025         "tell.wav",
1026         NULL,
1027         NULL
1028 };
1029
1030 void Test(int n)
1031 {
1032     if(soundFiles[values[3]]) PlaySound(soundFiles[values[3]]);
1033 }
1034
1035 Option soundOptions[] = {
1036 { 0, 0, 0, NULL, (void*) &appData.soundProgram, "", NULL, TextBox, _("Sound Program:") },
1037 { 0, 0, 0, NULL, (void*) &appData.soundDirectory, "", NULL, PathName, _("Sounds Directory:") },
1038 { 0, 0, 0, NULL, (void*) (soundFiles+2) /* kludge! */, "", NULL, FileName, _("User WAV File:") },
1039 { 0, 0, 0, NULL, (void*) &trialSound, (char*) soundNames, soundFiles, ComboBox, _("Try-Out Sound:") },
1040 { 0, 1, 0, NULL, (void*) &Test, NULL, NULL, Button, _("Play") },
1041 { 0, 0, 0, NULL, (void*) &appData.soundMove, (char*) soundNames, soundFiles, ComboBox, _("Move:") },
1042 { 0, 0, 0, NULL, (void*) &appData.soundIcsWin, (char*) soundNames, soundFiles, ComboBox, _("Win:") },
1043 { 0, 0, 0, NULL, (void*) &appData.soundIcsLoss, (char*) soundNames, soundFiles, ComboBox, _("Lose:") },
1044 { 0, 0, 0, NULL, (void*) &appData.soundIcsDraw, (char*) soundNames, soundFiles, ComboBox, _("Draw:") },
1045 { 0, 0, 0, NULL, (void*) &appData.soundIcsUnfinished, (char*) soundNames, soundFiles, ComboBox, _("Unfinished:") },
1046 { 0, 0, 0, NULL, (void*) &appData.soundIcsAlarm, (char*) soundNames, soundFiles, ComboBox, _("Alarm:") },
1047 { 0, 0, 0, NULL, (void*) &appData.soundShout, (char*) soundNames, soundFiles, ComboBox, _("Shout:") },
1048 { 0, 0, 0, NULL, (void*) &appData.soundSShout, (char*) soundNames, soundFiles, ComboBox, _("S-Shout:") },
1049 { 0, 0, 0, NULL, (void*) &appData.soundChannel, (char*) soundNames, soundFiles, ComboBox, _("Channel:") },
1050 { 0, 0, 0, NULL, (void*) &appData.soundChannel1, (char*) soundNames, soundFiles, ComboBox, _("Channel 1:") },
1051 { 0, 0, 0, NULL, (void*) &appData.soundTell, (char*) soundNames, soundFiles, ComboBox, _("Tell:") },
1052 { 0, 0, 0, NULL, (void*) &appData.soundKibitz, (char*) soundNames, soundFiles, ComboBox, _("Kibitz:") },
1053 { 0, 0, 0, NULL, (void*) &appData.soundChallenge, (char*) soundNames, soundFiles, ComboBox, _("Challenge:") },
1054 { 0, 0, 0, NULL, (void*) &appData.soundRequest, (char*) soundNames, soundFiles, ComboBox, _("Request:") },
1055 { 0, 0, 0, NULL, (void*) &appData.soundSeek, (char*) soundNames, soundFiles, ComboBox, _("Seek:") },
1056 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1057 };
1058
1059 void SetColor(char *colorName, Widget box)
1060 {
1061         Arg args[5];
1062         Pixel buttonColor;
1063         XrmValue vFrom, vTo;
1064         if (!appData.monoMode) {
1065             vFrom.addr = (caddr_t) colorName;
1066             vFrom.size = strlen(colorName);
1067             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1068             if (vTo.addr == NULL) {
1069                 buttonColor = (Pixel) -1;
1070             } else {
1071                 buttonColor = *(Pixel *) vTo.addr;
1072             }
1073         }
1074         XtSetArg(args[0], XtNbackground, buttonColor);;
1075         XtSetValues(box, args, 1);
1076 }
1077
1078 void AdjustColor(int i)
1079 {
1080     int n = currentOption[i].value, col, j, r, g, b, step = 10;
1081     char *s, buf[MSG_SIZ]; // color string
1082     Arg args[5];
1083     XtSetArg(args[0], XtNstring, &s);
1084     XtGetValues(currentOption[i-n-1].handle, args, 1);
1085     if(sscanf(s, "#%x", &col) != 1) return;   // malformed
1086     b = col & 0xFF; g = col & 0xFF00; r = col & 0xFF0000;
1087     switch(n) {
1088         case 1: g -= 0x100*step; b -= step; break;
1089         case 2: r -= 0x10000*step; b -= step; break;
1090         case 3: g -= 0x100*step; r -= 0x10000*step; break;
1091         case 4: r += 0x10000*step; g += 0x100*step; b += step; break;
1092     }
1093     if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0;
1094     if(r > 0xFF0000) r = 0xFF0000; if(g > 0xFF00) g = 0xFF00; if(b > 0xFF) b = 0xFF;
1095     col = r | g | b;
1096     snprintf(buf, MSG_SIZ, "#%06x", col);
1097     for(j=1; j<7; j++) if(buf[j] >= 'a') buf[j] -= 32; // capitalize
1098     SetColor(buf, currentOption[i-n].handle);
1099     XtSetArg(args[0], XtNstring, buf);
1100     XtSetValues(currentOption[i-n-1].handle, args, 1);
1101 }
1102
1103 void BoardOptionsOK(int n)
1104 {
1105     extern int defaultLineGap;
1106     if(appData.overrideLineGap >= 0) lineGap = appData.overrideLineGap; else lineGap = defaultLineGap;
1107     MakeColors(); CreateGCs(True);
1108     CreateXPMPieces();
1109     CreateXPMBoard(appData.liteBackTextureFile, 1);
1110     CreateXPMBoard(appData.darkBackTextureFile, 0);
1111     InitDrawingSizes(-1, 0);
1112     DrawPosition(True, NULL);
1113 }
1114
1115 Option boardOptions[] = {
1116 { 0,   0, 70, NULL, (void*) &appData.whitePieceColor, "", NULL, TextBox, _("White Piece Color:") },
1117 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1118 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1119 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1120 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1121 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1122 { 0,   0, 70, NULL, (void*) &appData.blackPieceColor, "", NULL, TextBox, _("Black Piece Color:") },
1123 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1124 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1125 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1126 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1127 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1128 { 0,   0, 70, NULL, (void*) &appData.lightSquareColor, "", NULL, TextBox, _("Light Square Color:") },
1129 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1130 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1131 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1132 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1133 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1134 { 0,   0, 70, NULL, (void*) &appData.darkSquareColor, "", NULL, TextBox, _("Dark Square Color:") },
1135 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1136 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1137 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1138 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1139 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1140 { 0,   0, 70, NULL, (void*) &appData.highlightSquareColor, "", NULL, TextBox, _("Highlight Color:") },
1141 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1142 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1143 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1144 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1145 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1146 { 0,   0, 70, NULL, (void*) &appData.premoveHighlightColor, "", NULL, TextBox, _("Premove Highlight Color:") },
1147 { 1000, 1, 0, NULL, NULL, NULL, NULL, Button, "      " },
1148 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1149 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1150 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1151 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "W" },
1152 { 0, 0, 0, NULL, (void*) &appData.upsideDown, "", NULL, CheckBox, _("Flip Pieces Shogi Style") },
1153 { 0, 0, 0, NULL, (void*) &appData.allWhite, "", NULL, CheckBox, _("Use Outline Pieces for Black") },
1154 { 0, 0, 0, NULL, (void*) &appData.monoMode, "", NULL, CheckBox, _("Mono Mode") },
1155 { 0,-1, 5, NULL, (void*) &appData.overrideLineGap, "", NULL, Spin, _("Line Gap ( -1 = default for board size):") },
1156 { 0, 0, 0, NULL, (void*) &appData.liteBackTextureFile, "", NULL, FileName, _("Light-Squares Texture File:") },
1157 { 0, 0, 0, NULL, (void*) &appData.darkBackTextureFile, "", NULL, FileName, _("Dark-Squares Texture File:") },
1158 { 0, 0, 0, NULL, (void*) &appData.bitmapDirectory, "", NULL, PathName, _("Directory with Bitmap Pieces:") },
1159 { 0, 0, 0, NULL, (void*) &appData.pixmapDirectory, "", NULL, PathName, _("Directory with Pixmap Pieces:") },
1160 { 0, 0, 0, NULL, (void*) &BoardOptionsOK, "", NULL, EndMark , "" }
1161 };
1162
1163 void GenericReadout()
1164 {
1165     int i, j;
1166     String name, val;
1167     Arg args[16];
1168     char buf[MSG_SIZ], **dest;
1169     float x;
1170         for(i=0; ; i++) { // send all options that had to be OK-ed to engine
1171             switch(currentOption[i].type) {
1172                 case TextBox:
1173                 case FileName:
1174                 case PathName:
1175                     XtSetArg(args[0], XtNstring, &val);
1176                     XtGetValues(currentOption[i].handle, args, 1);
1177                     dest = currentCps ? &(currentOption[i].textValue) : (char**) currentOption[i].target;
1178                     if(*dest == NULL || strcmp(*dest, val)) {
1179                         if(currentCps) {
1180                             snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name, val);
1181                             SendToProgram(buf, currentCps);
1182                         } else *dest = currentOption[i].name + 100; // option gets to point to private storage;
1183                         safeStrCpy(*dest, val, MSG_SIZ - (*dest - currentOption[i].name)); // copy text there
1184                     }
1185                     break;
1186                 case Spin:
1187                 case Fractional:
1188                     XtSetArg(args[0], XtNstring, &val);
1189                     XtGetValues(currentOption[i].handle, args, 1);
1190                     sscanf(val, "%f", &x);
1191                     if(x > currentOption[i].max) x = currentOption[i].max;
1192                     if(x < currentOption[i].min) x = currentOption[i].min;
1193                     if(currentOption[i].value != x) {
1194                         currentOption[i].value = x;
1195                         if(currentCps) { // engines never have float options, so no decimals!
1196                             snprintf(buf, MSG_SIZ,  "option %s=%.0f\n", currentOption[i].name, x);
1197                             SendToProgram(buf, currentCps);
1198                         } else if(currentOption[i].type == Spin) *(int*) currentOption[i].target = x;
1199                         else *(float*) currentOption[i].target = x;
1200                     }
1201                     break;
1202                 case CheckBox:
1203                     j = 0;
1204                     XtSetArg(args[0], XtNstate, &j);
1205                     XtGetValues(currentOption[i].handle, args, 1);
1206                     if(currentOption[i].value != j) {
1207                         currentOption[i].value = j;
1208                         if(currentCps) {
1209                             snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentOption[i].name, j);
1210                             SendToProgram(buf, currentCps);
1211                         } else *(Boolean*) currentOption[i].target = j;
1212                     }
1213                     break;
1214                 case ComboBox:
1215                     val = ((char**)currentOption[i].choice)[values[i]];
1216                     if(currentCps) {
1217                         if(currentOption[i].value == values[i]) break; // not changed
1218                         currentOption[i].value = values[i];
1219                         snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name,
1220                                 ((char**)currentOption[i].textValue)[values[i]]);
1221                         SendToProgram(buf, currentCps);
1222                     } else if(val && (*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val))) {
1223                       if(*(char**) currentOption[i].target) free(*(char**) currentOption[i].target);
1224                       *(char**) currentOption[i].target = strdup(val);
1225                     }
1226                     break;
1227                 case EndMark:
1228                     if(currentOption[i].target) // callback for implementing necessary actions on OK (like redraw)
1229                         ((ButtonCallback*) currentOption[i].target)(i);
1230                     break;
1231             default:
1232                 printf("GenericReadout: unexpected case in switch.\n");
1233                 case Button:
1234                 case SaveButton:
1235                 case Label:
1236               break;
1237             }
1238             if(currentOption[i].type == EndMark) break;
1239         }
1240 }
1241
1242 void GenericCallback(w, client_data, call_data)
1243      Widget w;
1244      XtPointer client_data, call_data;
1245 {
1246     String name, val;
1247     Arg args[16];
1248     char buf[MSG_SIZ];
1249     int i, j;
1250     int data = (intptr_t) client_data;
1251
1252     currentOption = dialogOptions[data>>16]; data &= 0xFFFF;
1253
1254     XtSetArg(args[0], XtNlabel, &name);
1255     XtGetValues(w, args, 1);
1256
1257     if (strcmp(name, _("cancel")) == 0) {
1258         PopDown(data);
1259         return;
1260     }
1261     if (strcmp(name, _("OK")) == 0) { // save buttons imply OK
1262         GenericReadout();
1263         PopDown(data);
1264         return;
1265     }
1266     if(currentCps) {
1267         if(currentOption[data].type == SaveButton) GenericReadout();
1268         snprintf(buf, MSG_SIZ,  "option %s\n", name);
1269         SendToProgram(buf, currentCps);
1270     } else ((ButtonCallback*) currentOption[data].target)(data);
1271 }
1272
1273 int
1274 GenericPopUp(Option *option, char *title, int dlgNr)
1275 {
1276     Arg args[16];
1277     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
1278     Window root, child;
1279     int x, y, i, j, height=999, width=1, h, c, w;
1280     int win_x, win_y, maxWidth, maxTextWidth;
1281     unsigned int mask;
1282     char def[MSG_SIZ], *msg;
1283     static char pane[6] = "paneX";
1284     Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL;
1285
1286     if(shellUp[dlgNr]) return 0; // already up
1287     if(dlgNr && shells[dlgNr]) {
1288         XtPopup(shells[dlgNr], XtGrabNone);
1289         shellUp[dlgNr] = True;
1290         return 0;
1291     }
1292
1293     dialogOptions[dlgNr] = option; // make available to callback
1294     // post currentOption globally, so Spin and Combo callbacks can already use it
1295     // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1296     currentOption = option;
1297
1298     if(currentCps) { // Settings popup for engine: format through heuristic
1299         int n = currentCps->nrOptions;
1300         if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1301         height = n / width + 1;
1302         if(currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton) currentOption[n].min = 1; // OK on same line
1303         currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1304     }
1305      i = 0;
1306     XtSetArg(args[i], XtNresizable, True); i++;
1307     popup = shells[dlgNr] =
1308       XtCreatePopupShell(title, transientShellWidgetClass,
1309                          shellWidget, args, i);
1310
1311     layout =
1312       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1313                             layoutArgs, XtNumber(layoutArgs));
1314   for(c=0; c<width; c++) {
1315     pane[4] = 'A'+c;
1316     form =
1317       XtCreateManagedWidget(pane, formWidgetClass, layout,
1318                             formArgs, XtNumber(formArgs));
1319     j=0;
1320     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1321     XtSetValues(form, args, j);
1322     leftMargin = form;
1323
1324     last = widest = NULL; anchor = lastrow;
1325     for(h=0; h<height; h++) {
1326         i = h + c*height;
1327         if(option[i].type == EndMark) break;
1328         lastrow = forelast;
1329         forelast = last;
1330         switch(option[i].type) {
1331           case Fractional:
1332             snprintf(def, MSG_SIZ,  "%.2f", *(float*)option[i].target);
1333             option[i].value = *(float*)option[i].target;
1334             goto tBox;
1335           case Spin:
1336             if(!currentCps) option[i].value = *(int*)option[i].target;
1337             snprintf(def, MSG_SIZ,  "%d", option[i].value);
1338           case TextBox:
1339           case FileName:
1340           case PathName:
1341           tBox:
1342             if(option[i].name[0]) {
1343             j=0;
1344             XtSetArg(args[j], XtNfromVert, last);  j++;
1345             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1346             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1347             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1348             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1349             texts[h] =
1350             dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1351             } else texts[h] = dialog = NULL;
1352             w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1353             if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1354             j=0;
1355             XtSetArg(args[j], XtNfromVert, last);  j++;
1356             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1357             XtSetArg(args[j], XtNborderWidth, 1); j++;
1358             XtSetArg(args[j], XtNwidth, w); j++;
1359             if(option[i].type == TextBox && option[i].min) {
1360                 XtSetArg(args[j], XtNheight, option[i].min); j++;
1361                 if(option[i].value & 1) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++; }
1362                 if(option[i].value & 2) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways);  j++; }
1363                 if(option[i].value & 4) { XtSetArg(args[j], XtNautoFill, True);  j++; }
1364                 if(option[i].value & 8) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1365             }
1366             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1367             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1368             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1369             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1370             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1371             XtSetArg(args[j], XtNresizable, True);  j++;
1372             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1373             XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def : 
1374                                 currentCps ? option[i].textValue : *(char**)option[i].target);  j++;
1375             edit = last;
1376             option[i].handle = (void*)
1377                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1378             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1379
1380             if(option[i].type == TextBox || option[i].type == Fractional) break;
1381
1382             // add increment and decrement controls for spin
1383             j=0;
1384             XtSetArg(args[j], XtNfromVert, edit);  j++;
1385             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1386             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1387             XtSetArg(args[j], XtNright, XtChainRight); j++;
1388             if(option[i].type == FileName || option[i].type == PathName) {
1389                 w = 50; msg = "browse";
1390             } else {
1391                 XtSetArg(args[j], XtNheight, 10);  j++;
1392                 w = 20; msg = "+";
1393             }
1394             XtSetArg(args[j], XtNwidth, w);  j++;
1395             edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1396             XtAddCallback(edit, XtNcallback, SpinCallback,
1397                           (XtPointer)(intptr_t) i);
1398
1399             if(option[i].type != Spin) break;
1400
1401             j=0;
1402             XtSetArg(args[j], XtNfromVert, edit);  j++;
1403             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1404             XtSetArg(args[j], XtNheight, 10);  j++;
1405             XtSetArg(args[j], XtNwidth, 20);  j++;
1406             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1407             XtSetArg(args[j], XtNright, XtChainRight); j++;
1408             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1409             XtAddCallback(last, XtNcallback, SpinCallback,
1410                           (XtPointer)(intptr_t) i);
1411             break;
1412           case CheckBox:
1413             if(!currentCps) option[i].value = *(Boolean*)option[i].target;
1414             j=0;
1415             XtSetArg(args[j], XtNfromVert, last);  j++;
1416             XtSetArg(args[j], XtNwidth, 10);  j++;
1417             XtSetArg(args[j], XtNheight, 10);  j++;
1418             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1419             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1420             XtSetArg(args[j], XtNstate, option[i].value);  j++;
1421             option[i].handle = (void*)
1422                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1423           case Label:
1424             msg = option[i].name;
1425             if(*msg == NULLCHAR) msg = option[i].textValue;
1426             if(!msg) break;
1427             j=0;
1428             XtSetArg(args[j], XtNfromVert, last);  j++;
1429             XtSetArg(args[j], XtNfromHoriz, option[i].type != Label ? dialog : NULL);  j++;
1430             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1431             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1432             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1433             last = XtCreateManagedWidget(msg, labelWidgetClass, form, args, j);
1434             break;
1435           case SaveButton:
1436           case Button:
1437             j=0;
1438             XtSetArg(args[j], XtNfromVert, option[i].min & 1 ? lastrow : last);  j++;
1439             if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last);  j++; }
1440             else  { XtSetArg(args[j], XtNfromHoriz, NULL);  j++; lastrow = forelast; }
1441             if(option[i].max) XtSetArg(args[j], XtNwidth, option[i].max);  j++;
1442             if(option[i].textValue) { // special for buttons of New Variant dialog
1443                 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
1444                                          || strstr(first.variants, VariantName(option[i].value))); j++;
1445                 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1446             }
1447             option[i].handle = (void*)
1448                 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1449             if(option[i].target == NULL && !currentCps) SetColor( *(char**) option[i-1].target, last); else
1450             XtAddCallback(last, XtNcallback, GenericCallback,
1451                           (XtPointer)(intptr_t) i + (dlgNr<<16));
1452             if(option[i].textValue) SetColor( option[i].textValue, last);
1453             forelast = lastrow; // next button can go on same row
1454             break;
1455           case ComboBox:
1456             j=0;
1457             XtSetArg(args[j], XtNfromVert, last);  j++;
1458             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1459             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1460             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1461             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1462             texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1463
1464             if(currentCps) option[i].choice = (char**) option[i].textValue; else {
1465               for(j=0; option[i].choice[j]; j++)
1466                 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
1467               option[i].value = j + (option[i].choice[j] == NULL);
1468             }
1469
1470             j=0;
1471             XtSetArg(args[j], XtNfromVert, last);  j++;
1472             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1473             XtSetArg(args[j], XtNwidth, option[i].max && !currentCps ? option[i].max : 100);  j++;
1474             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1475             XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name));  j++;
1476             XtSetArg(args[j], XtNlabel, ((char**)option[i].textValue)[option[i].value]);  j++;
1477             option[i].handle = (void*)
1478                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1479             CreateComboPopup(last, option[i].name, i, (char **) option[i].textValue);
1480             values[i] = option[i].value;
1481             break;
1482           case Break:
1483             width++;
1484             height = i+1;
1485             break;
1486         default:
1487             printf("GenericPopUp: unexpected case in switch.\n");
1488             break;
1489         }
1490     }
1491
1492     // make an attempt to align all spins and textbox controls
1493     maxWidth = maxTextWidth = 0;
1494     for(h=0; h<height; h++) {
1495         i = h + c*height;
1496         if(option[i].type == EndMark) break;
1497         if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1498                                   || option[i].type == PathName || option[i].type == FileName) {
1499             Dimension w;
1500             if(!texts[h]) continue;
1501             j=0;
1502             XtSetArg(args[j], XtNwidth, &w);  j++;
1503             XtGetValues(texts[h], args, j);
1504             if(option[i].type == Spin) {
1505                 if(w > maxWidth) maxWidth = w;
1506                 widest = texts[h];
1507             } else {
1508                 if(w > maxTextWidth) maxTextWidth = w;
1509                 if(!widest) widest = texts[h];
1510             }
1511         }
1512     }
1513     if(maxTextWidth + 110 < maxWidth)
1514          maxTextWidth = maxWidth - 110;
1515     else maxWidth = maxTextWidth + 110;
1516     for(h=0; h<height; h++) {
1517         i = h + c*height;
1518         if(option[i].type == EndMark) break;
1519         if(!texts[h]) continue;
1520         j=0;
1521         if(option[i].type == Spin) {
1522             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1523             XtSetValues(texts[h], args, j);
1524         } else
1525         if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1526             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1527             XtSetValues(texts[h], args, j);
1528         }
1529     }
1530   }
1531
1532   if(!(option[i].min & 2)) {
1533     j=0;
1534     if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last); last = forelast; } else
1535     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1536     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1537     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1538     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1539     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1540     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1541     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1542     XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer) dlgNr);
1543
1544     XtSetArg(args[0], XtNfromHoriz, b_ok);
1545     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1546     XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer) dlgNr);
1547   }
1548
1549     XtRealizeWidget(popup);
1550     XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1551     snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1552     XtAugmentTranslations(popup, XtParseTranslationTable(def));
1553     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1554                   &x, &y, &win_x, &win_y, &mask);
1555
1556     XtSetArg(args[0], XtNx, x - 10);
1557     XtSetArg(args[1], XtNy, y - 30);
1558     XtSetValues(popup, args, 2);
1559
1560     XtPopup(popup, dlgNr ? XtGrabNone : XtGrabExclusive);
1561     shellUp[dlgNr] = True;
1562     previous = NULL;
1563     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1564     if(dlgNr && wp[dlgNr] && wp[dlgNr]->width > 0) { // if persistent window-info available, reposition
1565         j = 0;
1566         XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height));  j++;
1567         XtSetArg(args[j], XtNwidth,  (Dimension) (wp[dlgNr]->width));  j++;
1568         XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x));  j++;
1569         XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y));  j++;
1570         XtSetValues(popup, args, j);
1571     }
1572     return 1;
1573 }
1574
1575
1576 void IcsOptionsProc(w, event, prms, nprms)
1577      Widget w;
1578      XEvent *event;
1579      String *prms;
1580      Cardinal *nprms;
1581 {
1582    GenericPopUp(icsOptions, _("ICS Options"), 0);
1583 }
1584
1585 void LoadOptionsProc(w, event, prms, nprms)
1586      Widget w;
1587      XEvent *event;
1588      String *prms;
1589      Cardinal *nprms;
1590 {
1591    GenericPopUp(loadOptions, _("Load Game Options"), 0);
1592 }
1593
1594 void SaveOptionsProc(w, event, prms, nprms)
1595      Widget w;
1596      XEvent *event;
1597      String *prms;
1598      Cardinal *nprms;
1599 {
1600    GenericPopUp(saveOptions, _("Save Game Options"), 0);
1601 }
1602
1603 void SoundOptionsProc(w, event, prms, nprms)
1604      Widget w;
1605      XEvent *event;
1606      String *prms;
1607      Cardinal *nprms;
1608 {
1609    soundFiles[2] = "*";
1610    GenericPopUp(soundOptions, _("Sound Options"), 0);
1611 }
1612
1613 void BoardOptionsProc(w, event, prms, nprms)
1614      Widget w;
1615      XEvent *event;
1616      String *prms;
1617      Cardinal *nprms;
1618 {
1619    GenericPopUp(boardOptions, _("Board Options"), 0);
1620 }
1621
1622 void EngineMenuProc(w, event, prms, nprms)
1623      Widget w;
1624      XEvent *event;
1625      String *prms;
1626      Cardinal *nprms;
1627 {
1628    GenericPopUp(adjudicationOptions, "Adjudicate non-ICS Games", 0);
1629 }
1630
1631 void UciMenuProc(w, event, prms, nprms)
1632      Widget w;
1633      XEvent *event;
1634      String *prms;
1635      Cardinal *nprms;
1636 {
1637    oldCores = appData.smpCores;
1638    oldPonder = appData.ponderNextMove;
1639    GenericPopUp(commonEngineOptions, _("Common Engine Settings"), 0);
1640 }
1641
1642 void NewVariantProc(w, event, prms, nprms)
1643      Widget w;
1644      XEvent *event;
1645      String *prms;
1646      Cardinal *nprms;
1647 {
1648    GenericPopUp(variantDescriptors, _("New Variant"), 0);
1649 }
1650
1651 void OptionsProc(w, event, prms, nprms)
1652      Widget w;
1653      XEvent *event;
1654      String *prms;
1655      Cardinal *nprms;
1656 {
1657    oldPonder = appData.ponderNextMove;
1658    GenericPopUp(generalOptions, _("General Options"), 0);
1659 }
1660
1661 void MatchOptionsProc(w, event, prms, nprms)
1662      Widget w;
1663      XEvent *event;
1664      String *prms;
1665      Cardinal *nprms;
1666 {
1667    GenericPopUp(matchOptions, _("Match Options"), 0);
1668 }
1669
1670 Option textOptions[100];
1671 extern char *icsTextMenuString;
1672 void PutText P((char *text, int pos));
1673
1674 SendString(char *p)
1675 {
1676     char buf[MSG_SIZ], *q;
1677     if(q = strstr(p, "$input")) {
1678         if(!shellUp[4]) return;
1679         strncpy(buf, p, MSG_SIZ);
1680         strncpy(buf + (q-p), q+6, MSG_SIZ-(q-p));
1681         PutText(buf, q-p);
1682         return;
1683     }
1684     snprintf(buf, MSG_SIZ, "%s\n", p);
1685     SendToICS(buf);
1686 }
1687
1688 /* function called when the data to Paste is ready */
1689 static void
1690 SendTextCB(Widget w, XtPointer client_data, Atom *selection,
1691            Atom *type, XtPointer value, unsigned long *len, int *format)
1692 {
1693   char buf[MSG_SIZ], *p = (char*) textOptions[(int) client_data].choice, *name = (char*) value, *q;
1694   if (value==NULL || *len==0) return; /* nothing selected, abort */
1695   name[*len]='\0';
1696   strncpy(buf, p, MSG_SIZ);
1697   q = strstr(p, "$name");
1698   snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1699   SendString(buf);
1700   XtFree(value);
1701 }
1702
1703 void SendText(int n)
1704 {
1705     char *p = (char*) textOptions[n].choice;
1706     if(strstr(p, "$name")) {
1707         XtGetSelectionValue(menuBarWidget,
1708           XA_PRIMARY, XA_STRING,
1709           /* (XtSelectionCallbackProc) */ SendTextCB,
1710           (XtPointer) n, /* client_data passed to PastePositionCB */
1711           CurrentTime
1712         );
1713     } else SendString(p);
1714 }
1715
1716 void IcsTextProc(w, event, prms, nprms)
1717      Widget w;
1718      XEvent *event;
1719      String *prms;
1720      Cardinal *nprms;
1721 {
1722    int i=0, j;
1723    char *p, *q, *r;
1724    if((p = icsTextMenuString) == NULL) return;
1725    do {
1726         q = r = p; while(*p && *p != ';') p++;
1727         for(j=0; j<p-q; j++) textOptions[i].name[j] = *r++;
1728         textOptions[i].name[j++] = 0;
1729         if(!*p) break;
1730         if(*++p == '\n') p++; // optional linefeed after button-text terminating semicolon
1731         q = p;
1732         textOptions[i].choice = (char**) (r = textOptions[i].name + j);
1733         while(*p && (*p != ';' || p[1] != '\n')) textOptions[i].name[j++] = *p++;
1734         textOptions[i].name[j++] = 0;
1735         if(*p) p += 2;
1736         textOptions[i].max = 135;
1737         textOptions[i].min = i&1;
1738         textOptions[i].handle = NULL;
1739         textOptions[i].target = &SendText;
1740         textOptions[i].textValue = strstr(r, "$input") ? "#80FF80" : strstr(r, "$name") ? "#FF8080" : "#FFFFFF";
1741         textOptions[i].type = Button;
1742    } while(++i < 99 && *p);
1743    if(i == 0) return;
1744    textOptions[i].type = EndMark;
1745    textOptions[i].target = NULL;
1746    textOptions[i].min = 2;
1747    MarkMenu("menuView.ICStex", 3);
1748    GenericPopUp(textOptions, _("ICS text menu"), 3);
1749 }
1750
1751 extern char ICSInputTranslations[];
1752 char *icsText;
1753
1754 Option boxOptions[] = {
1755 {   0, 20,  400, NULL, (void*) &icsText, "", NULL, TextBox, "" },
1756 {   0,  3,    0, NULL, NULL, "", NULL, EndMark , "" }
1757 };
1758
1759 void PutText(char *text, int pos)
1760 {
1761     Widget edit;
1762     Arg args[16];
1763     char buf[MSG_SIZ], *p;
1764
1765     if(strstr(text, "$add ") == text) {
1766         XtSetArg(args[0], XtNstring, &p);
1767         XtGetValues(boxOptions[0].handle, args, 1);
1768         snprintf(buf, MSG_SIZ, "%s%s", p, text+5); text = buf;
1769         pos += strlen(p) - 5;
1770     }
1771     XtSetArg(args[0], XtNstring, text);
1772     XtSetValues(boxOptions[0].handle, args, 1);
1773     XtSetArg(args[0], XtNinsertPosition, pos);
1774     XtSetValues(boxOptions[0].handle, args, 1);
1775 //    SetFocus(boxOptions[0].handle, shells[4], NULL, False); // No idea why this does not work, and the following is needed:
1776     XSetInputFocus(xDisplay, XtWindow(boxOptions[0].handle), RevertToPointerRoot, CurrentTime);
1777 }
1778
1779 void InputBoxPopup()
1780 {
1781     MarkMenu("menuView.ICS Input Box", 4);
1782     if(GenericPopUp(boxOptions, _("ICS input box"), 4))
1783         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(ICSInputTranslations));
1784 }
1785
1786 void
1787 SettingsPopUp(ChessProgramState *cps)
1788 {
1789    currentCps = cps;
1790    GenericPopUp(cps->option, _("Engine Settings"), 0);
1791 }
1792
1793 void FirstSettingsProc(w, event, prms, nprms)
1794      Widget w;
1795      XEvent *event;
1796      String *prms;
1797      Cardinal *nprms;
1798 {
1799    SettingsPopUp(&first);
1800 }
1801
1802 void SecondSettingsProc(w, event, prms, nprms)
1803      Widget w;
1804      XEvent *event;
1805      String *prms;
1806      Cardinal *nprms;
1807 {
1808    if(WaitForSecond(SettingsMenuIfReady)) return;
1809    SettingsPopUp(&second);
1810 }
1811
1812 //---------------------------- Chat Windows ----------------------------------------------
1813
1814 void OutputChatMessage(int partner, char *mess)
1815 {
1816     return; // dummy
1817 }
1818