b8b1ee8e71fef9dce662cea958a140abe9acb47e
[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 *filter, 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 static Boolean browserUp;
628
629 void CheckCallback(Widget ww, XtPointer data, XEvent *event, Boolean *b)
630 {
631     Widget w = currentOption[(int)(intptr_t)data].handle;
632     Boolean s;
633     Arg args[16];
634
635     XtSetArg(args[0], XtNstate, &s);
636     XtGetValues(w, args, 1);
637     XtSetArg(args[0], XtNstate, !s);
638     XtSetValues(w, args, 1);
639 }
640
641 void SpinCallback(w, client_data, call_data)
642      Widget w;
643      XtPointer client_data, call_data;
644 {
645     String name, val;
646     Arg args[16];
647     char buf[MSG_SIZ], *p;
648     int j;
649     int data = (intptr_t) client_data;
650
651     XtSetArg(args[0], XtNlabel, &name);
652     XtGetValues(w, args, 1);
653
654     j = 0;
655     XtSetArg(args[0], XtNstring, &val);
656     XtGetValues(currentOption[data].handle, args, 1);
657     sscanf(val, "%d", &j);
658     if (strcmp(name, "browse") == 0) {
659         char *q, *r;
660         XtSetArg(args[0], XtNstring, &q);
661         XtGetValues(currentOption[data].handle, args, 1);
662         for(r = ""; *q; q++) if(*q == '.') r = q; else if(*q == '/') r = ""; // last dot after last slash
663         browserUp = True;
664         if(XsraSelFile(shells[0], currentOption[data].name, NULL, NULL, "", "", r,
665                                   currentOption[data].type == PathName ? "p" : "f", NULL, &p)) {
666                 int len = strlen(p);
667                 if(len && p[len-1] == '/') p[len-1] = NULLCHAR;
668                 XtSetArg(args[0], XtNstring, p);
669                 XtSetValues(currentOption[data].handle, args, 1);
670         }
671         browserUp = False;
672         SetFocus(currentOption[data].handle, shells[0], (XEvent*) NULL, False);
673         return;
674     } else
675     if (strcmp(name, "+") == 0) {
676         if(++j > currentOption[data].max) return;
677     } else
678     if (strcmp(name, "-") == 0) {
679         if(--j < currentOption[data].min) return;
680     } else return;
681     snprintf(buf, MSG_SIZ,  "%d", j);
682     XtSetArg(args[0], XtNstring, buf);
683     XtSetValues(currentOption[data].handle, args, 1);
684     SetFocus(currentOption[data].handle, shells[0], NULL, False);
685 }
686
687 void ComboSelect(w, addr, index) // callback for all combo items
688      Widget w;
689      caddr_t addr;
690      caddr_t index;
691 {
692     Arg args[16];
693     int i = ((intptr_t)addr)>>8;
694     int j = 255 & (intptr_t) addr;
695
696     values[i] = j; // store in temporary, for transfer at OK
697     XtSetArg(args[0], XtNlabel, _(((char**)currentOption[i].textValue)[j]));
698     XtSetValues(currentOption[i].handle, args, 1);
699 }
700
701 void CreateComboPopup(parent, name, n, mb)
702      Widget parent;
703      String name;
704      int n;
705      char *mb[];
706 {
707     int i=0, j;
708     Widget menu, entry;
709     Arg args[16];
710
711     menu = XtCreatePopupShell(name, simpleMenuWidgetClass,
712                               parent, NULL, 0);
713     j = 0;
714     XtSetArg(args[j], XtNwidth, 100);  j++;
715     XtSetArg(args[j], XtNlabel, _(mb[i]));  j++;
716 //    XtSetArg(args[j], XtNright, XtChainRight);  j++;
717     while (mb[i] != NULL) {
718             entry = XtCreateManagedWidget(mb[i], smeBSBObjectClass,
719                                           menu, args, j);
720             XtAddCallback(entry, XtNcallback,
721                           (XtCallbackProc) ComboSelect,
722                           (caddr_t)(intptr_t) (256*n+i));
723         i++;
724     }
725 }
726
727
728 //----------------------------Generic dialog --------------------------------------------
729
730 // cloned from Engine Settings dialog (and later merged with it)
731
732 typedef void ButtonCallback(int n);
733
734 extern WindowPlacement wpComment, wpTags;
735 char *trialSound;
736 static int oldCores, oldPonder;
737 int MakeColors P((void));
738 void CreateGCs P((int redo));
739 void CreateAnyPieces P((void));
740 void GenericReadout P((int selected));
741 Widget shells[10];
742 Widget marked[10];
743 Boolean shellUp[10];
744 WindowPlacement *wp[10] = { NULL, &wpComment, &wpTags };
745 Option *dialogOptions[10];
746
747 void MarkMenu(char *item, int dlgNr)
748 {
749     Arg args[2];
750     XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
751     XtSetValues(marked[dlgNr] = XtNameToWidget(menuBarWidget, item), args, 1);
752 }
753
754 int PopDown(int n)
755 {
756     int j;
757     Arg args[10];
758     Dimension windowH, windowW; Position windowX, windowY;
759     if (!shellUp[n]) return 0;
760     if(n && wp[n]) { // remember position
761         j = 0;
762         XtSetArg(args[j], XtNx, &windowX); j++;
763         XtSetArg(args[j], XtNy, &windowY); j++;
764         XtSetArg(args[j], XtNheight, &windowH); j++;
765         XtSetArg(args[j], XtNwidth, &windowW); j++;
766         XtGetValues(shells[n], args, j);
767         wp[n]->x = windowX;
768         wp[n]->x = windowY;
769         wp[n]->width  = windowW;
770         wp[n]->height = windowH;
771     }
772     previous = NULL;
773     XtPopdown(shells[n]);
774     if(n == 0) XtDestroyWidget(shells[n]);
775     shellUp[n] = False;
776     if(marked[n]) {
777         XtSetArg(args[0], XtNleftBitmap, None);
778         XtSetValues(marked[n], args, 1);
779     }
780     if(!n) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
781     return 1;
782 }
783
784 void GenericPopDown(w, event, prms, nprms)
785      Widget w;
786      XEvent *event;
787      String *prms;
788      Cardinal *nprms;
789 {
790     int n;
791     if(browserUp) return; // prevent closing dialog when it has an open file-browse daughter
792     PopDown(prms[0][0] - '0');
793 }
794
795 Option matchOptions[] = {
796 { 0,  2, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, N_("Default Number of Games in Match:") },
797 { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Match Games (msec):") },
798 { 0,  0,          0, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, N_("Game File with Opening Lines:") },
799 { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, N_("Game Number (-1 or -2 = Auto-Increment):") },
800 { 0,  0,          0, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, N_("File with Start Positions:") },
801 { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, N_("Position Number (-1 or -2 = Auto-Increment):") },
802 { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind Index after this many Games (0 = never):") },
803 { 0, 0, 0, NULL, NULL, "", NULL, EndMark , "" }
804 };
805
806 void GeneralOptionsOK(int n)
807 {
808         int newPonder = appData.ponderNextMove;
809         appData.ponderNextMove = oldPonder;
810         PonderNextMoveEvent(newPonder);
811 }
812
813 Option generalOptions[] = {
814 { 0,  0, 0, NULL, (void*) &appData.sweepSelect, "", NULL, CheckBox, N_("Almost Always Queen (Detour Under-Promote)") },
815 { 0,  0, 0, NULL, (void*) &appData.animateDragging, "", NULL, CheckBox, N_("Animate Dragging") },
816 { 0,  0, 0, NULL, (void*) &appData.animate, "", NULL, CheckBox, N_("Animate Moving") },
817 { 0,  0, 0, NULL, (void*) &appData.autoCallFlag, "", NULL, CheckBox, N_("Auto Flag") },
818 { 0,  0, 0, NULL, (void*) &appData.autoFlipView, "", NULL, CheckBox, N_("Auto Flip View") },
819 { 0,  0, 0, NULL, (void*) &appData.blindfold, "", NULL, CheckBox, N_("Blindfold") },
820 { 0,  0, 0, NULL, (void*) &appData.dropMenu, "", NULL, CheckBox, N_("Drop Menu") },
821 { 0,  0, 0, NULL, (void*) &appData.highlightDragging, "", NULL, CheckBox, N_("Highlight Dragging (Show Move Targets)") },
822 { 0,  0, 0, NULL, (void*) &appData.highlightLastMove, "", NULL, CheckBox, N_("Highlight Last Move") },
823 { 0,  0, 0, NULL, (void*) &appData.highlightMoveWithArrow, "", NULL, CheckBox, N_("Highlight with Arrow") },
824 { 0,  0, 0, NULL, (void*) &appData.ringBellAfterMoves, "", NULL, CheckBox, N_("Move Sound") },
825 { 0,  0, 0, NULL, (void*) &appData.oneClick, "", NULL, CheckBox, N_("One-Click Moving") },
826 { 0,  0, 0, NULL, (void*) &appData.periodicUpdates, "", NULL, CheckBox, N_("Periodic Updates (in Analysis Mode)") },
827 { 0,  0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, N_("Ponder Next Move") },
828 { 0,  0, 0, NULL, (void*) &appData.popupExitMessage, "", NULL, CheckBox, N_("Popup Exit Messages") },
829 { 0,  0, 0, NULL, (void*) &appData.popupMoveErrors, "", NULL, CheckBox, N_("Popup Move Errors") },
830 { 0,  0, 0, NULL, (void*) &appData.showCoords, "", NULL, CheckBox, N_("Show Coordinates") },
831 { 0,  0, 0, NULL, (void*) &appData.markers, "", NULL, CheckBox, N_("Show Target Squares") },
832 { 0,  0, 0, NULL, (void*) &appData.hideThinkingFromHuman, "", NULL, CheckBox, N_("Hide Thinking from Human") },
833 { 0,  0, 0, NULL, (void*) &appData.testLegality, "", NULL, CheckBox, N_("Test Legality") },
834 { 0, 0, 10, NULL, (void*) &appData.flashCount, "", NULL, Spin, N_("Flash Moves (0 = no flashing):") },
835 { 0, 1, 10, NULL, (void*) &appData.flashRate, "", NULL, Spin, N_("Flash Rate (high = fast):") },
836 { 0, 5, 100,NULL, (void*) &appData.animSpeed, "", NULL, Spin, N_("Animation Speed (high = slow):") },
837 { 0,  0, 0, NULL, (void*) &GeneralOptionsOK, "", NULL, EndMark , "" }
838 };
839
840 void Pick(int n)
841 {
842         VariantClass v = currentOption[n].value;
843         if(!appData.noChessProgram) {
844             char *name = VariantName(v), buf[MSG_SIZ];
845             if (first.protocolVersion > 1 && StrStr(first.variants, name) == NULL) {
846                 /* [HGM] in protocol 2 we check if variant is suported by engine */
847               snprintf(buf, MSG_SIZ,  _("Variant %s not supported by %s"), name, first.tidy);
848                 DisplayError(buf, 0);
849                 return; /* ignore OK if first engine does not support it */
850             } else
851             if (second.initDone && second.protocolVersion > 1 && StrStr(second.variants, name) == NULL) {
852               snprintf(buf, MSG_SIZ,  _("Warning: second engine (%s) does not support this!"), second.tidy);
853                 DisplayError(buf, 0);   /* use of second engine is optional; only warn user */
854             }
855         }
856
857         GenericReadout(-1); // make sure ranks and file settings are read
858
859         gameInfo.variant = v;
860         appData.variant = VariantName(v);
861
862         shuffleOpenings = FALSE; /* [HGM] shuffle: possible shuffle reset when we switch */
863         startedFromPositionFile = FALSE; /* [HGM] loadPos: no longer valid in new variant */
864         appData.pieceToCharTable = NULL;
865         appData.pieceNickNames = "";
866         appData.colorNickNames = "";
867         Reset(True, True);
868         PopDown(0);
869         return;
870 }
871
872 Option variantDescriptors[] = {
873 { VariantNormal, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("normal")},
874 { VariantFairy, 1, 135, NULL, (void*) &Pick, "#BFBFBF", NULL, Button, N_("fairy")},
875 { VariantFischeRandom, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("FRC")},
876 { VariantSChess, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Seirawan")},
877 { VariantWildCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("wild castle")},
878 { VariantSuper, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("Superchess")},
879 { VariantNoCastle, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("no castle")},
880 { VariantCrazyhouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("crazyhouse")},
881 { VariantKnightmate, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("knightmate")},
882 { VariantBughouse, 1, 135, NULL, (void*) &Pick, "#FFBFBF", NULL, Button, N_("bughouse")},
883 { VariantBerolina, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("berolina")},
884 { VariantShogi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("shogi (9x9)")},
885 { VariantCylinder, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("cylinder")},
886 { VariantXiangqi, 1, 135, NULL, (void*) &Pick, "#BFFFFF", NULL, Button, N_("xiangqi (9x10)")},
887 { VariantShatranj, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("shatranj")},
888 { VariantCourier, 1, 135, NULL, (void*) &Pick, "#BFFFBF", NULL, Button, N_("courier (12x8)")},
889 { VariantMakruk, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("makruk")},
890 { VariantGreat, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Great Shatranj (10x8)")},
891 { VariantAtomic, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("atomic")},
892 { VariantCapablanca, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Capablanca (10x8)")},
893 { VariantTwoKings, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("two kings")},
894 { VariantGothic, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("Gothic (10x8)")},
895 { Variant3Check, 0, 135, NULL, (void*) &Pick, "#FFFFFF", NULL, Button, N_("3-checks")},
896 { VariantJanus, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("janus (10x8)")},
897 { VariantSuicide, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("suicide")},
898 { VariantCapaRandom, 1, 135, NULL, (void*) &Pick, "#BFBFFF", NULL, Button, N_("CRC (10x8)")},
899 { VariantGiveaway, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("give-away")},
900 { VariantSpartan, 1, 135, NULL, (void*) &Pick, "#FF0000", NULL, Button, N_("Spartan")},
901 { VariantLosers, 0, 135, NULL, (void*) &Pick, "#FFFFBF", NULL, Button, N_("losers")},
902 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, N_("Board size ( -1 = default for selected variant):")},
903 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.NrRanks, "", NULL, Spin, N_("Number of Board Ranks:") },
904 { 0, -1, BOARD_FILES, NULL, (void*) &appData.NrFiles, "", NULL, Spin, N_("Number of Board Files:") },
905 { 0, -1, BOARD_RANKS-1, NULL, (void*) &appData.holdingsSize, "", NULL, Spin, N_("Holdings Size:") },
906 { 0, 0, 0, NULL, NULL, NULL, NULL, Label,
907                                 N_("WARNING: variants with un-orthodox\n"
908                                   "pieces only have built-in bitmaps\n"
909                                   "for -boardSize middling, bulky and\n"
910                                   "petite, and substitute king or amazon\n"
911                                   "for missing bitmaps. (See manual.)")},
912 { 0, 2, 0, NULL, NULL, "", NULL, EndMark , "" }
913 };
914
915 void CommonOptionsOK(int n)
916 {
917         int newPonder = appData.ponderNextMove;
918         // make sure changes are sent to first engine by re-initializing it
919         // if it was already started pre-emptively at end of previous game
920         if(gameMode == BeginningOfGame) Reset(True, True); else {
921             // Some changed setting need immediate sending always.
922             if(oldCores != appData.smpCores)
923                 NewSettingEvent(False, &(first.maxCores), "cores", appData.smpCores);
924             appData.ponderNextMove = oldPonder;
925             PonderNextMoveEvent(newPonder);
926         }
927 }
928
929 Option commonEngineOptions[] = {
930 { 0,     0, 0, NULL, (void*) &appData.ponderNextMove, "", NULL, CheckBox, N_("Ponder Next Move") },
931 { 0,  0, 1000, NULL, (void*) &appData.smpCores, "", NULL, Spin, N_("Maximum Number of CPUs per Engine:") },
932 { 0,     0, 0, NULL, (void*) &appData.polyglotDir, "", NULL, PathName, N_("Polygot Directory:") },
933 { 0, 0, 16000, NULL, (void*) &appData.defaultHashSize, "", NULL, Spin, N_("Hash-Table Size (MB):") },
934 { 0,     0, 0, NULL, (void*) &appData.defaultPathEGTB, "", NULL, PathName, N_("Nalimov EGTB Path:") },
935 { 0,  0, 1000, NULL, (void*) &appData.defaultCacheSizeEGTB, "", NULL, Spin, N_("EGTB Cache Size (MB):") },
936 { 0,     0, 0, NULL, (void*) &appData.usePolyglotBook, "", NULL, CheckBox, N_("Use GUI Book") },
937 { 0,     0, 0, NULL, (void*) &appData.polyglotBook, "", NULL, FileName, N_("Opening-Book Filename:") },
938 { 0,   0, 100, NULL, (void*) &appData.bookDepth, "", NULL, Spin, N_("Book Depth (moves):") },
939 { 0,   0, 100, NULL, (void*) &appData.bookStrength, "", NULL, Spin, N_("Book Variety (0) vs. Strength (100):") },
940 { 0,     0, 0, NULL, (void*) &appData.firstHasOwnBookUCI, "", NULL, CheckBox, N_("Engine #1 Has Own Book") },
941 { 0,     0, 0, NULL, (void*) &appData.secondHasOwnBookUCI, "", NULL, CheckBox, N_("Engine #2 Has Own Book          ") },
942 { 0,     1, 0, NULL, (void*) &CommonOptionsOK, "", NULL, EndMark , "" }
943 };
944
945 Option adjudicationOptions[] = {
946 { 0, 0,    0, NULL, (void*) &appData.checkMates, "", NULL, CheckBox, N_("Detect all Mates") },
947 { 0, 0,    0, NULL, (void*) &appData.testClaims, "", NULL, CheckBox, N_("Verify Engine Result Claims") },
948 { 0, 0,    0, NULL, (void*) &appData.materialDraws, "", NULL, CheckBox, N_("Draw if Insufficient Mating Material") },
949 { 0, 0,    0, NULL, (void*) &appData.trivialDraws, "", NULL, CheckBox, N_("Adjudicate Trivial Draws (3-Move Delay)") },
950 { 0, 0,  100, NULL, (void*) &appData.ruleMoves, "", NULL, Spin, N_("N-Move Rule:") },
951 { 0, 0,    6, NULL, (void*) &appData.drawRepeats, "", NULL, Spin, N_("N-fold Repeats:") },
952 { 0, 0, 1000, NULL, (void*) &appData.adjudicateDrawMoves, "", NULL, Spin, N_("Draw after N Moves Total:") },
953 { 0,-5000, 0, NULL, (void*) &appData.adjudicateLossThreshold, "", NULL, Spin, N_("Win / Loss Threshold:") },
954 { 0, 0,    0, NULL, (void*) &first.scoreIsAbsolute, "", NULL, CheckBox, N_("Negate Score of Engine #1") },
955 { 0, 0,    0, NULL, (void*) &second.scoreIsAbsolute, "", NULL, CheckBox, N_("Negate Score of Engine #2") },
956 { 0, 1,    0, NULL, NULL, "", NULL, EndMark , "" }
957 };
958
959 void IcsOptionsOK(int n)
960 {
961     ParseIcsTextColors();
962 }
963
964 Option icsOptions[] = {
965 { 0, 0, 0, NULL, (void*) &appData.autoKibitz, "",  NULL, CheckBox, N_("Auto-Kibitz") },
966 { 0, 0, 0, NULL, (void*) &appData.autoComment, "", NULL, CheckBox, N_("Auto-Comment") },
967 { 0, 0, 0, NULL, (void*) &appData.autoObserve, "", NULL, CheckBox, N_("Auto-Observe") },
968 { 0, 0, 0, NULL, (void*) &appData.autoRaiseBoard, "", NULL, CheckBox, N_("Auto-Raise Board") },
969 { 0, 0, 0, NULL, (void*) &appData.bgObserve, "",   NULL, CheckBox, N_("Background Observe while Playing") },
970 { 0, 0, 0, NULL, (void*) &appData.dualBoard, "",   NULL, CheckBox, N_("Dual Board for Background-Observed Game") },
971 { 0, 0, 0, NULL, (void*) &appData.getMoveList, "", NULL, CheckBox, N_("Get Move List") },
972 { 0, 0, 0, NULL, (void*) &appData.quietPlay, "",   NULL, CheckBox, N_("Quiet Play") },
973 { 0, 0, 0, NULL, (void*) &appData.seekGraph, "",   NULL, CheckBox, N_("Seek Graph") },
974 { 0, 0, 0, NULL, (void*) &appData.autoRefresh, "", NULL, CheckBox, N_("Auto-Refresh Seek Graph") },
975 { 0, 0, 0, NULL, (void*) &appData.premove, "",     NULL, CheckBox, N_("Premove") },
976 { 0, 0, 0, NULL, (void*) &appData.premoveWhite, "", NULL, CheckBox, N_("Premove for White") },
977 { 0, 0, 0, NULL, (void*) &appData.premoveWhiteText, "", NULL, TextBox, N_("First White Move:") },
978 { 0, 0, 0, NULL, (void*) &appData.premoveBlack, "", NULL, CheckBox, N_("Premove for Black") },
979 { 0, 0, 0, NULL, (void*) &appData.premoveBlackText, "", NULL, TextBox, N_("First Black Move:") },
980 { 0, 0, 0, NULL, NULL, NULL, NULL, Break, "" },
981 { 0, 0, 0, NULL, (void*) &appData.icsAlarm, "", NULL, CheckBox, N_("Alarm") },
982 { 0, 0, 100000000, NULL, (void*) &appData.icsAlarmTime, "", NULL, Spin, N_("Alarm Time (msec):") },
983 //{ 0, 0, 0, NULL, (void*) &appData.chatBoxes, "", NULL, TextBox, N_("Startup Chat Boxes:") },
984 { 0, 0, 0, NULL, (void*) &appData.colorize, "", NULL, CheckBox, N_("Colorize Messages") },
985 { 0, 0, 0, NULL, (void*) &appData.colorShout, "", NULL, TextBox, N_("Shout Text Colors:") },
986 { 0, 0, 0, NULL, (void*) &appData.colorSShout, "", NULL, TextBox, N_("S-Shout Text Colors:") },
987 { 0, 0, 0, NULL, (void*) &appData.colorChannel1, "", NULL, TextBox, N_("Channel #1 Text Colors:") },
988 { 0, 0, 0, NULL, (void*) &appData.colorChannel, "", NULL, TextBox, N_("Other Channel Text Colors:") },
989 { 0, 0, 0, NULL, (void*) &appData.colorKibitz, "", NULL, TextBox, N_("Kibitz Text Colors:") },
990 { 0, 0, 0, NULL, (void*) &appData.colorTell, "", NULL, TextBox, N_("Tell Text Colors:") },
991 { 0, 0, 0, NULL, (void*) &appData.colorChallenge, "", NULL, TextBox, N_("Challenge Text Colors:") },
992 { 0, 0, 0, NULL, (void*) &appData.colorRequest, "", NULL, TextBox, N_("Request Text Colors:") },
993 { 0, 0, 0, NULL, (void*) &appData.colorSeek, "", NULL, TextBox, N_("Seek Text Colors:") },
994 { 0, 0, 0, NULL, (void*) &IcsOptionsOK, "", NULL, EndMark , "" }
995 };
996
997 Option loadOptions[] = {
998 { 0, 0, 0, NULL, (void*) &appData.autoDisplayTags, "", NULL, CheckBox, N_("Auto-Display Tags") },
999 { 0, 0, 0, NULL, (void*) &appData.autoDisplayComment, "", NULL, CheckBox, N_("Auto-Display Comment") },
1000 { 0, 0, 0, NULL, NULL, NULL, NULL, Label, N_("Auto-Play speed of loaded games\n(0 = instant, -1 = off):") },
1001 { 0, -1, 10000000, NULL, (void*) &appData.timeDelay, "", NULL, Fractional, N_("Seconds per Move:") },
1002 { 0,  0, 0, NULL, NULL, "", NULL, EndMark , "" }
1003 };
1004
1005 Option saveOptions[] = {
1006 { 0, 0, 0, NULL, (void*) &appData.autoSaveGames, "", NULL, CheckBox, N_("Auto-Save Games") },
1007 { 0, 0, 0, NULL, (void*) &appData.saveGameFile, "", NULL, FileName,  N_("Save Games on File:") },
1008 { 0, 0, 0, NULL, (void*) &appData.savePositionFile, "", NULL, FileName,  N_("Save Final Positions on File:") },
1009 { 0, 0, 0, NULL, (void*) &appData.pgnEventHeader, "", NULL, TextBox,  N_("PGN Event Header:") },
1010 { 0, 0, 0, NULL, (void*) &appData.oldSaveStyle, "", NULL, CheckBox, N_("Old Save Style (as opposed to PGN)") },
1011 { 0, 0, 0, NULL, (void*) &appData.saveExtendedInfoInPGN, "", NULL, CheckBox, N_("Save Score/Depth Info in PGN") },
1012 { 0, 0, 0, NULL, (void*) &appData.saveOutOfBookInfo, "", NULL, CheckBox, N_("Save Out-of-Book Info in PGN           ") },
1013 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1014 };
1015
1016 char *soundNames[] = {
1017         N_("No Sound"),
1018         N_("Default Beep"),
1019         N_("Above WAV File"),
1020         N_("Car Horn"),
1021         N_("Cymbal"),
1022         N_("Ding"),
1023         N_("Gong"),
1024         N_("Laser"),
1025         N_("Penalty"),
1026         N_("Phone"),
1027         N_("Pop"),
1028         N_("Slap"),
1029         N_("Wood Thunk"),
1030         NULL,
1031         N_("User File")
1032 };
1033
1034 char *soundFiles[] = { // sound files corresponding to above names
1035         "",
1036         "$",
1037         "*", // kludge alert: as first thing in the dialog readout this is replaced with the user-given .WAV filename
1038         "honkhonk.wav",
1039         "cymbal.wav",
1040         "ding1.wav",
1041         "gong.wav",
1042         "laser.wav",
1043         "penalty.wav",
1044         "phone.wav",
1045         "pop2.wav",
1046         "slap.wav",
1047         "woodthunk.wav",
1048         NULL,
1049         NULL
1050 };
1051
1052 void Test(int n)
1053 {
1054     GenericReadout(2);
1055     if(soundFiles[values[3]]) PlaySound(soundFiles[values[3]]);
1056 }
1057
1058 Option soundOptions[] = {
1059 { 0, 0, 0, NULL, (void*) &appData.soundProgram, "", NULL, TextBox, N_("Sound Program:") },
1060 { 0, 0, 0, NULL, (void*) &appData.soundDirectory, "", NULL, PathName, N_("Sounds Directory:") },
1061 { 0, 0, 0, NULL, (void*) (soundFiles+2) /* kludge! */, "", NULL, FileName, N_("User WAV File:") },
1062 { 0, 0, 0, NULL, (void*) &trialSound, (char*) soundNames, soundFiles, ComboBox, N_("Try-Out Sound:") },
1063 { 0, 1, 0, NULL, (void*) &Test, NULL, NULL, Button, N_("Play") },
1064 { 0, 0, 0, NULL, (void*) &appData.soundMove, (char*) soundNames, soundFiles, ComboBox, N_("Move:") },
1065 { 0, 0, 0, NULL, (void*) &appData.soundIcsWin, (char*) soundNames, soundFiles, ComboBox, N_("Win:") },
1066 { 0, 0, 0, NULL, (void*) &appData.soundIcsLoss, (char*) soundNames, soundFiles, ComboBox, N_("Lose:") },
1067 { 0, 0, 0, NULL, (void*) &appData.soundIcsDraw, (char*) soundNames, soundFiles, ComboBox, N_("Draw:") },
1068 { 0, 0, 0, NULL, (void*) &appData.soundIcsUnfinished, (char*) soundNames, soundFiles, ComboBox, N_("Unfinished:") },
1069 { 0, 0, 0, NULL, (void*) &appData.soundIcsAlarm, (char*) soundNames, soundFiles, ComboBox, N_("Alarm:") },
1070 { 0, 0, 0, NULL, (void*) &appData.soundShout, (char*) soundNames, soundFiles, ComboBox, N_("Shout:") },
1071 { 0, 0, 0, NULL, (void*) &appData.soundSShout, (char*) soundNames, soundFiles, ComboBox, N_("S-Shout:") },
1072 { 0, 0, 0, NULL, (void*) &appData.soundChannel, (char*) soundNames, soundFiles, ComboBox, N_("Channel:") },
1073 { 0, 0, 0, NULL, (void*) &appData.soundChannel1, (char*) soundNames, soundFiles, ComboBox, N_("Channel 1:") },
1074 { 0, 0, 0, NULL, (void*) &appData.soundTell, (char*) soundNames, soundFiles, ComboBox, N_("Tell:") },
1075 { 0, 0, 0, NULL, (void*) &appData.soundKibitz, (char*) soundNames, soundFiles, ComboBox, N_("Kibitz:") },
1076 { 0, 0, 0, NULL, (void*) &appData.soundChallenge, (char*) soundNames, soundFiles, ComboBox, N_("Challenge:") },
1077 { 0, 0, 0, NULL, (void*) &appData.soundRequest, (char*) soundNames, soundFiles, ComboBox, N_("Request:") },
1078 { 0, 0, 0, NULL, (void*) &appData.soundSeek, (char*) soundNames, soundFiles, ComboBox, N_("Seek:") },
1079 { 0, 1, 0, NULL, NULL, "", NULL, EndMark , "" }
1080 };
1081
1082 void SetColor(char *colorName, Widget box)
1083 {
1084         Arg args[5];
1085         Pixel buttonColor;
1086         XrmValue vFrom, vTo;
1087         if (!appData.monoMode) {
1088             vFrom.addr = (caddr_t) colorName;
1089             vFrom.size = strlen(colorName);
1090             XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
1091             if (vTo.addr == NULL) {
1092                 buttonColor = (Pixel) -1;
1093             } else {
1094                 buttonColor = *(Pixel *) vTo.addr;
1095             }
1096         }
1097         XtSetArg(args[0], XtNbackground, buttonColor);;
1098         XtSetValues(box, args, 1);
1099 }
1100
1101 void SetColorText(int n, char *buf)
1102 {
1103     Arg args[5];
1104     XtSetArg(args[0], XtNstring, buf);
1105     XtSetValues(currentOption[n-1].handle, args, 1);
1106     SetFocus(currentOption[n-1].handle, shells[0], NULL, False);
1107     SetColor(buf, currentOption[n].handle);
1108 }
1109
1110 void DefColor(int n)
1111 {
1112     SetColorText(n, (char*) currentOption[n].choice);
1113 }
1114
1115 void RefreshColor(int source, int n)
1116 {
1117     int col, j, r, g, b, step = 10;
1118     char *s, buf[MSG_SIZ]; // color string
1119     Arg args[5];
1120     XtSetArg(args[0], XtNstring, &s);
1121     XtGetValues(currentOption[source].handle, args, 1);
1122     if(sscanf(s, "#%x", &col) != 1) return;   // malformed
1123     b = col & 0xFF; g = col & 0xFF00; r = col & 0xFF0000;
1124     switch(n) {
1125         case 1: r += 0x10000*step;break;
1126         case 2: g += 0x100*step;  break;
1127         case 3: b += step;        break;
1128         case 4: r -= 0x10000*step; g -= 0x100*step; b -= step; break;
1129     }
1130     if(r < 0) r = 0; if(g < 0) g = 0; if(b < 0) b = 0;
1131     if(r > 0xFF0000) r = 0xFF0000; if(g > 0xFF00) g = 0xFF00; if(b > 0xFF) b = 0xFF;
1132     col = r | g | b;
1133     snprintf(buf, MSG_SIZ, "#%06x", col);
1134     for(j=1; j<7; j++) if(buf[j] >= 'a') buf[j] -= 32; // capitalize
1135     SetColorText(source+1, buf);
1136 }
1137
1138 void ColorChanged(Widget w, XtPointer data, XEvent *event, Boolean *b)
1139 {
1140     char buf[10];
1141     if ( (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) && *buf == '\r' )
1142         RefreshColor((int)(intptr_t) data, 0);
1143 }
1144
1145 void AdjustColor(int i)
1146 {
1147     int n = currentOption[i].value;
1148     RefreshColor(i-n-1, n);
1149 }
1150
1151 void BoardOptionsOK(int n)
1152 {
1153     extern int defaultLineGap, useImages, useImageSqs;
1154     if(appData.overrideLineGap >= 0) lineGap = appData.overrideLineGap; else lineGap = defaultLineGap;
1155     useImages = useImageSqs = 0;
1156     MakeColors(); CreateGCs(True);
1157     CreateAnyPieces();
1158     InitDrawingSizes(-1, 0);
1159     DrawPosition(True, NULL);
1160 }
1161
1162 Option boardOptions[] = {
1163 { 0,   0, 70, NULL, (void*) &appData.whitePieceColor, "", NULL, TextBox, N_("White Piece Color:") },
1164 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFFCC", Button, "      " },
1165 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1166 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1167 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1168 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1169 { 0,   0, 70, NULL, (void*) &appData.blackPieceColor, "", NULL, TextBox, N_("Black Piece Color:") },
1170 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#202020", Button, "      " },
1171 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1172 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1173 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1174 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1175 { 0,   0, 70, NULL, (void*) &appData.lightSquareColor, "", NULL, TextBox, N_("Light Square Color:") },
1176 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#C8C365", Button, "      " },
1177 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1178 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1179 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1180 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1181 { 0,   0, 70, NULL, (void*) &appData.darkSquareColor, "", NULL, TextBox, N_("Dark Square Color:") },
1182 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#77A26D", Button, "      " },
1183 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1184 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1185 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1186 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1187 { 0,   0, 70, NULL, (void*) &appData.highlightSquareColor, "", NULL, TextBox, N_("Highlight Color:") },
1188 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FFFF00", Button, "      " },
1189 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1190 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1191 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1192 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1193 { 0,   0, 70, NULL, (void*) &appData.premoveHighlightColor, "", NULL, TextBox, N_("Premove Highlight Color:") },
1194 { 1000, 1, 0, NULL, (void*) &DefColor, NULL, (char**) "#FF0000", Button, "      " },
1195 {    1, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "R" },
1196 {    2, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "G" },
1197 {    3, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "B" },
1198 {    4, 1, 0, NULL, (void*) &AdjustColor, NULL, NULL, Button, "D" },
1199 { 0, 0, 0, NULL, (void*) &appData.upsideDown, "", NULL, CheckBox, N_("Flip Pieces Shogi Style        (Colored buttons restore default)") },
1200 //{ 0, 0, 0, NULL, (void*) &appData.allWhite, "", NULL, CheckBox, N_("Use Outline Pieces for Black") },
1201 { 0, 0, 0, NULL, (void*) &appData.monoMode, "", NULL, CheckBox, N_("Mono Mode") },
1202 { 0,-1, 5, NULL, (void*) &appData.overrideLineGap, "", NULL, Spin, N_("Line Gap ( -1 = default for board size):") },
1203 { 0, 0, 0, NULL, (void*) &appData.liteBackTextureFile, "", NULL, FileName, N_("Light-Squares Texture File:") },
1204 { 0, 0, 0, NULL, (void*) &appData.darkBackTextureFile, "", NULL, FileName, N_("Dark-Squares Texture File:") },
1205 { 0, 0, 0, NULL, (void*) &appData.bitmapDirectory, "", NULL, PathName, N_("Directory with Bitmap Pieces:") },
1206 { 0, 0, 0, NULL, (void*) &appData.pixmapDirectory, "", NULL, PathName, N_("Directory with Pixmap Pieces:") },
1207 { 0, 0, 0, NULL, (void*) &BoardOptionsOK, "", NULL, EndMark , "" }
1208 };
1209
1210 void GenericReadout(int selected)
1211 {
1212     int i, j;
1213     String name, val;
1214     Arg args[16];
1215     char buf[MSG_SIZ], **dest;
1216     float x;
1217         for(i=0; ; i++) { // send all options that had to be OK-ed to engine
1218             if(selected >= 0) { if(i < selected) continue; else if(i > selected) break; }
1219             switch(currentOption[i].type) {
1220                 case TextBox:
1221                 case FileName:
1222                 case PathName:
1223                     XtSetArg(args[0], XtNstring, &val);
1224                     XtGetValues(currentOption[i].handle, args, 1);
1225                     dest = currentCps ? &(currentOption[i].textValue) : (char**) currentOption[i].target;
1226                     if(*dest == NULL || strcmp(*dest, val)) {
1227                         if(currentCps) {
1228                             snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name, val);
1229                             SendToProgram(buf, currentCps);
1230                         } else *dest = currentOption[i].name + 100; // option gets to point to private storage;
1231                         safeStrCpy(*dest, val, MSG_SIZ - (*dest - currentOption[i].name)); // copy text there
1232                     }
1233                     break;
1234                 case Spin:
1235                 case Fractional:
1236                     XtSetArg(args[0], XtNstring, &val);
1237                     XtGetValues(currentOption[i].handle, args, 1);
1238                     sscanf(val, "%f", &x);
1239                     if(x > currentOption[i].max) x = currentOption[i].max;
1240                     if(x < currentOption[i].min) x = currentOption[i].min;
1241                     if(currentOption[i].type == Fractional)
1242                         *(float*) currentOption[i].target = x; // engines never have float options!
1243                     else if(currentOption[i].value != x) {
1244                         currentOption[i].value = x;
1245                         if(currentCps) {
1246                             snprintf(buf, MSG_SIZ,  "option %s=%.0f\n", currentOption[i].name, x);
1247                             SendToProgram(buf, currentCps);
1248                         } else *(int*) currentOption[i].target = x;
1249                     }
1250                     break;
1251                 case CheckBox:
1252                     j = 0;
1253                     XtSetArg(args[0], XtNstate, &j);
1254                     XtGetValues(currentOption[i].handle, args, 1);
1255                     if(currentOption[i].value != j) {
1256                         currentOption[i].value = j;
1257                         if(currentCps) {
1258                             snprintf(buf, MSG_SIZ,  "option %s=%d\n", currentOption[i].name, j);
1259                             SendToProgram(buf, currentCps);
1260                         } else *(Boolean*) currentOption[i].target = j;
1261                     }
1262                     break;
1263                 case ComboBox:
1264                     val = ((char**)currentOption[i].choice)[values[i]];
1265                     if(currentCps) {
1266                         if(currentOption[i].value == values[i]) break; // not changed
1267                         currentOption[i].value = values[i];
1268                         snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentOption[i].name,
1269                                 ((char**)currentOption[i].textValue)[values[i]]);
1270                         SendToProgram(buf, currentCps);
1271                     } else if(val && (*(char**) currentOption[i].target == NULL || strcmp(*(char**) currentOption[i].target, val))) {
1272                       if(*(char**) currentOption[i].target) free(*(char**) currentOption[i].target);
1273                       *(char**) currentOption[i].target = strdup(val);
1274                     }
1275                     break;
1276                 case EndMark:
1277                     if(currentOption[i].target) // callback for implementing necessary actions on OK (like redraw)
1278                         ((ButtonCallback*) currentOption[i].target)(i);
1279                     break;
1280             default:
1281                 printf("GenericReadout: unexpected case in switch.\n");
1282                 case Button:
1283                 case SaveButton:
1284                 case Label:
1285                 case Break:
1286               break;
1287             }
1288             if(currentOption[i].type == EndMark) break;
1289         }
1290 }
1291
1292 void GenericCallback(w, client_data, call_data)
1293      Widget w;
1294      XtPointer client_data, call_data;
1295 {
1296     String name, val;
1297     Arg args[16];
1298     char buf[MSG_SIZ];
1299     int i, j;
1300     int data = (intptr_t) client_data;
1301
1302     currentOption = dialogOptions[data>>16]; data &= 0xFFFF;
1303
1304     XtSetArg(args[0], XtNlabel, &name);
1305     XtGetValues(w, args, 1);
1306
1307     if (strcmp(name, _("cancel")) == 0) {
1308         PopDown(data);
1309         return;
1310     }
1311     if (strcmp(name, _("OK")) == 0) { // save buttons imply OK
1312         GenericReadout(-1);
1313         PopDown(data);
1314         return;
1315     }
1316     if(currentCps) {
1317         if(currentOption[data].type == SaveButton) GenericReadout(-1);
1318         snprintf(buf, MSG_SIZ,  "option %s\n", name);
1319         SendToProgram(buf, currentCps);
1320     } else ((ButtonCallback*) currentOption[data].target)(data);
1321 }
1322
1323 static char *oneLiner  = "<Key>Return:  redraw-display()\n";
1324
1325 int
1326 GenericPopUp(Option *option, char *title, int dlgNr)
1327 {
1328     Arg args[16];
1329     Widget popup, layout, dialog, edit=NULL, form,  last, b_ok, b_cancel, leftMargin = NULL, textField = NULL;
1330     Window root, child;
1331     int x, y, i, j, height=999, width=1, h, c, w;
1332     int win_x, win_y, maxWidth, maxTextWidth;
1333     unsigned int mask;
1334     char def[MSG_SIZ], *msg;
1335     static char pane[6] = "paneX";
1336     Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL;
1337
1338     if(shellUp[dlgNr]) return 0; // already up          
1339     if(dlgNr && shells[dlgNr]) {
1340         XtPopup(shells[dlgNr], XtGrabNone);
1341         shellUp[dlgNr] = True;
1342         return 0;
1343     }
1344
1345     dialogOptions[dlgNr] = option; // make available to callback
1346     // post currentOption globally, so Spin and Combo callbacks can already use it
1347     // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1348     currentOption = option;
1349
1350     if(currentCps) { // Settings popup for engine: format through heuristic
1351         int n = currentCps->nrOptions;
1352         if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1353         height = n / width + 1;
1354         if(currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton) currentOption[n].min = 1; // OK on same line
1355         currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1356     }
1357      i = 0;
1358     XtSetArg(args[i], XtNresizable, True); i++;
1359     popup = shells[dlgNr] =
1360       XtCreatePopupShell(title, transientShellWidgetClass,
1361                          shellWidget, args, i);
1362
1363     layout =
1364       XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1365                             layoutArgs, XtNumber(layoutArgs));
1366   for(c=0; c<width; c++) {
1367     pane[4] = 'A'+c;
1368     form =
1369       XtCreateManagedWidget(pane, formWidgetClass, layout,
1370                             formArgs, XtNumber(formArgs));
1371     j=0;
1372     XtSetArg(args[j], XtNfromHoriz, leftMargin);  j++;
1373     XtSetValues(form, args, j);
1374     leftMargin = form;
1375
1376     last = widest = NULL; anchor = lastrow;
1377     for(h=0; h<height; h++) {
1378         i = h + c*height;
1379         if(option[i].type == EndMark) break;
1380         lastrow = forelast;
1381         forelast = last;
1382         switch(option[i].type) {
1383           case Fractional:
1384             snprintf(def, MSG_SIZ,  "%.2f", *(float*)option[i].target);
1385             option[i].value = *(float*)option[i].target;
1386             goto tBox;
1387           case Spin:
1388             if(!currentCps) option[i].value = *(int*)option[i].target;
1389             snprintf(def, MSG_SIZ,  "%d", option[i].value);
1390           case TextBox:
1391           case FileName:
1392           case PathName:
1393           tBox:
1394             if(option[i].name[0]) {
1395             j=0;
1396             XtSetArg(args[j], XtNfromVert, last);  j++;
1397             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1398             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1399             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1400             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1401             XtSetArg(args[j], XtNlabel, _(option[i].name));  j++;
1402             texts[h] =
1403             dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1404             } else texts[h] = dialog = NULL;
1405             w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1406             if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1407             j=0;
1408             XtSetArg(args[j], XtNfromVert, last);  j++;
1409             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1410             XtSetArg(args[j], XtNborderWidth, 1); j++;
1411             XtSetArg(args[j], XtNwidth, w); j++;
1412             if(option[i].type == TextBox && option[i].min) {
1413                 XtSetArg(args[j], XtNheight, option[i].min); j++;
1414                 if(option[i].value & 1) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++; }
1415                 if(option[i].value & 2) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways);  j++; }
1416                 if(option[i].value & 4) { XtSetArg(args[j], XtNautoFill, True);  j++; }
1417                 if(option[i].value & 8) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1418             }
1419             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1420             XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
1421             XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
1422             XtSetArg(args[j], XtNdisplayCaret, False);  j++;
1423             XtSetArg(args[j], XtNright, XtChainRight);  j++;
1424             XtSetArg(args[j], XtNresizable, True);  j++;
1425             XtSetArg(args[j], XtNinsertPosition, 9999);  j++;
1426             XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def : 
1427                                 currentCps ? option[i].textValue : *(char**)option[i].target);  j++;
1428             edit = last;
1429             option[i].handle = (void*)
1430                 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1431             XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup);
1432             if(option[i].min == 0 || option[i].type != TextBox)
1433                 XtOverrideTranslations(last, XtParseTranslationTable(oneLiner));
1434
1435             if(option[i].type == TextBox || option[i].type == Fractional) break;
1436
1437             // add increment and decrement controls for spin
1438             j=0;
1439             XtSetArg(args[j], XtNfromVert, edit);  j++;
1440             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1441             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1442             XtSetArg(args[j], XtNright, XtChainRight); j++;
1443             if(option[i].type == FileName || option[i].type == PathName) {
1444                 w = 50; msg = _("browse");
1445             } else {
1446                 XtSetArg(args[j], XtNheight, 10);  j++;
1447                 w = 20; msg = "+";
1448             }
1449             XtSetArg(args[j], XtNwidth, w);  j++;
1450             edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1451             XtAddCallback(edit, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1452
1453             if(option[i].type != Spin) break;
1454
1455             j=0;
1456             XtSetArg(args[j], XtNfromVert, edit);  j++;
1457             XtSetArg(args[j], XtNfromHoriz, last);  j++;
1458             XtSetArg(args[j], XtNheight, 10);  j++;
1459             XtSetArg(args[j], XtNwidth, 20);  j++;
1460             XtSetArg(args[j], XtNleft, XtChainRight); j++;
1461             XtSetArg(args[j], XtNright, XtChainRight); j++;
1462             last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1463             XtAddCallback(last, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i);
1464             break;
1465           case CheckBox:
1466             if(!currentCps) option[i].value = *(Boolean*)option[i].target;
1467             j=0;
1468             XtSetArg(args[j], XtNfromVert, last);  j++;
1469             XtSetArg(args[j], XtNwidth, 10);  j++;
1470             XtSetArg(args[j], XtNheight, 10);  j++;
1471             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1472             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1473             XtSetArg(args[j], XtNstate, option[i].value);  j++;
1474             option[i].handle = (void*)
1475                 (dialog = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1476           case Label:
1477             msg = option[i].name;
1478             if(*msg == NULLCHAR) msg = option[i].textValue;
1479             if(!msg) break;
1480             j=0;
1481             XtSetArg(args[j], XtNfromVert, last);  j++;
1482             XtSetArg(args[j], XtNfromHoriz, option[i].type != Label ? dialog : NULL);  j++;
1483             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1484             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1485             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1486             XtSetArg(args[j], XtNlabel, _(msg));  j++;
1487             last = XtCreateManagedWidget(msg, labelWidgetClass, form, args, j);
1488             if(option[i].type == CheckBox)
1489                 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i);
1490             break;
1491           case SaveButton:
1492           case Button:
1493             j=0;
1494             XtSetArg(args[j], XtNfromVert, option[i].min & 1 ? lastrow : last);  j++;
1495             XtSetArg(args[j], XtNlabel, _(option[i].name));  j++;
1496             if(option[i].min & 1) { XtSetArg(args[j], XtNfromHoriz, last);  j++; }
1497             else  { XtSetArg(args[j], XtNfromHoriz, NULL);  j++; lastrow = forelast; }
1498             if(option[i].max) XtSetArg(args[j], XtNwidth, option[i].max);  j++;
1499             if(option[i].textValue) { // special for buttons of New Variant dialog
1500                 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
1501                                          || strstr(first.variants, VariantName(option[i].value))); j++;
1502                 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1503             }
1504             option[i].handle = (void*)
1505                 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1506             if(option[i].choice && ((char*)option[i].choice)[0] == '#' && !currentCps) {
1507                 SetColor( *(char**) option[i-1].target, last);
1508                 XtAddEventHandler(option[i-1].handle, KeyReleaseMask, False, ColorChanged, (XtPointer)(intptr_t) i-1);
1509             }
1510             XtAddCallback(last, XtNcallback, GenericCallback,
1511                           (XtPointer)(intptr_t) i + (dlgNr<<16));
1512             if(option[i].textValue) SetColor( option[i].textValue, last);
1513             forelast = lastrow; // next button can go on same row
1514             break;
1515           case ComboBox:
1516             j=0;
1517             XtSetArg(args[j], XtNfromVert, last);  j++;
1518             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1519             XtSetArg(args[j], XtNright, XtChainLeft); j++;
1520             XtSetArg(args[j], XtNborderWidth, 0);  j++;
1521             XtSetArg(args[j], XtNjustify, XtJustifyLeft);  j++;
1522             XtSetArg(args[j], XtNlabel, _(option[i].name));  j++;
1523             texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1524
1525             if(currentCps) option[i].choice = (char**) option[i].textValue; else {
1526               for(j=0; option[i].choice[j]; j++)
1527                 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
1528               option[i].value = j + (option[i].choice[j] == NULL);
1529             }
1530
1531             j=0;
1532             XtSetArg(args[j], XtNfromVert, last);  j++;
1533             XtSetArg(args[j], XtNfromHoriz, dialog);  j++;
1534             XtSetArg(args[j], XtNwidth, option[i].max && !currentCps ? option[i].max : 100);  j++;
1535             XtSetArg(args[j], XtNleft, XtChainLeft); j++;
1536             XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name));  j++;
1537             XtSetArg(args[j], XtNlabel, _(((char**)option[i].textValue)[option[i].value]));  j++;
1538             option[i].handle = (void*)
1539                 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1540             CreateComboPopup(last, option[i].name, i, (char **) option[i].textValue);
1541             values[i] = option[i].value;
1542             break;
1543           case Break:
1544             width++;
1545             height = i+1;
1546             break;
1547         default:
1548             printf("GenericPopUp: unexpected case in switch.\n");
1549             break;
1550         }
1551     }
1552
1553     // make an attempt to align all spins and textbox controls
1554     maxWidth = maxTextWidth = 0;
1555     for(h=0; h<height; h++) {
1556         i = h + c*height;
1557         if(option[i].type == EndMark) break;
1558         if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1559                                   || option[i].type == PathName || option[i].type == FileName) {
1560             Dimension w;
1561             if(!texts[h]) continue;
1562             j=0;
1563             XtSetArg(args[j], XtNwidth, &w);  j++;
1564             XtGetValues(texts[h], args, j);
1565             if(option[i].type == Spin) {
1566                 if(w > maxWidth) maxWidth = w;
1567                 widest = texts[h];
1568             } else {
1569                 if(w > maxTextWidth) maxTextWidth = w;
1570                 if(!widest) widest = texts[h];
1571             }
1572         }
1573     }
1574     if(maxTextWidth + 110 < maxWidth)
1575          maxTextWidth = maxWidth - 110;
1576     else maxWidth = maxTextWidth + 110;
1577     for(h=0; h<height; h++) {
1578         i = h + c*height;
1579         if(option[i].type == EndMark) break;
1580         if(!texts[h]) continue;
1581         j=0;
1582         if(option[i].type == Spin) {
1583             XtSetArg(args[j], XtNwidth, maxWidth);  j++;
1584             XtSetValues(texts[h], args, j);
1585         } else
1586         if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1587             XtSetArg(args[j], XtNwidth, maxTextWidth);  j++;
1588             XtSetValues(texts[h], args, j);
1589         }
1590     }
1591   }
1592
1593   if(!(option[i].min & 2)) {
1594     j=0;
1595     if(option[i].min & 1) {
1596         for(j=i-1; option[j+1].min&1 && option[j].type == Button; j--) {
1597             XtSetArg(args[0], XtNtop, XtChainBottom);
1598             XtSetArg(args[1], XtNbottom, XtChainBottom);
1599             XtSetValues(option[j].handle, args, 2);
1600         }
1601         if(option[j].type == TextBox && option[j].name[0] == NULLCHAR) {
1602             XtSetArg(args[0], XtNbottom, XtChainBottom);
1603             XtSetValues(option[j].handle, args, 1);
1604         }
1605         j = 0;
1606         XtSetArg(args[j], XtNfromHoriz, last); last = forelast;
1607     } else
1608     XtSetArg(args[j], XtNfromHoriz, widest ? widest : dialog);  j++;
1609     XtSetArg(args[j], XtNfromVert, anchor ? anchor : last);  j++;
1610     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
1611     XtSetArg(args[j], XtNtop, XtChainBottom);  j++;
1612     XtSetArg(args[j], XtNleft, XtChainRight);  j++;
1613     XtSetArg(args[j], XtNright, XtChainRight);  j++;
1614     b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1615     XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr + (dlgNr<<16));
1616
1617     XtSetArg(args[0], XtNfromHoriz, b_ok);
1618     b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1619     XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer)(intptr_t) dlgNr);
1620   }
1621
1622     XtRealizeWidget(popup);
1623     XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1624     snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1625     XtAugmentTranslations(popup, XtParseTranslationTable(def));
1626     XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1627                   &x, &y, &win_x, &win_y, &mask);
1628
1629     XtSetArg(args[0], XtNx, x - 10);
1630     XtSetArg(args[1], XtNy, y - 30);
1631     XtSetValues(popup, args, 2);
1632
1633     XtPopup(popup, dlgNr ? XtGrabNone : XtGrabExclusive);
1634     shellUp[dlgNr] = True;
1635     previous = NULL;
1636     if(textField)SetFocus(textField, popup, (XEvent*) NULL, False);
1637     if(dlgNr && wp[dlgNr] && wp[dlgNr]->width > 0) { // if persistent window-info available, reposition
1638         j = 0;
1639         XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height));  j++;
1640         XtSetArg(args[j], XtNwidth,  (Dimension) (wp[dlgNr]->width));  j++;
1641         XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x));  j++;
1642         XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y));  j++;
1643         XtSetValues(popup, args, j);
1644     }
1645     return 1;
1646 }
1647
1648
1649 void IcsOptionsProc(w, event, prms, nprms)
1650      Widget w;
1651      XEvent *event;
1652      String *prms;
1653      Cardinal *nprms;
1654 {
1655    GenericPopUp(icsOptions, _("ICS Options"), 0);
1656 }
1657
1658 void LoadOptionsProc(w, event, prms, nprms)
1659      Widget w;
1660      XEvent *event;
1661      String *prms;
1662      Cardinal *nprms;
1663 {
1664    GenericPopUp(loadOptions, _("Load Game Options"), 0);
1665 }
1666
1667 void SaveOptionsProc(w, event, prms, nprms)
1668      Widget w;
1669      XEvent *event;
1670      String *prms;
1671      Cardinal *nprms;
1672 {
1673    GenericPopUp(saveOptions, _("Save Game Options"), 0);
1674 }
1675
1676 void SoundOptionsProc(w, event, prms, nprms)
1677      Widget w;
1678      XEvent *event;
1679      String *prms;
1680      Cardinal *nprms;
1681 {
1682    soundFiles[2] = "*";
1683    GenericPopUp(soundOptions, _("Sound Options"), 0);
1684 }
1685
1686 void BoardOptionsProc(w, event, prms, nprms)
1687      Widget w;
1688      XEvent *event;
1689      String *prms;
1690      Cardinal *nprms;
1691 {
1692    GenericPopUp(boardOptions, _("Board Options"), 0);
1693 }
1694
1695 void EngineMenuProc(w, event, prms, nprms)
1696      Widget w;
1697      XEvent *event;
1698      String *prms;
1699      Cardinal *nprms;
1700 {
1701    GenericPopUp(adjudicationOptions, "Adjudicate non-ICS Games", 0);
1702 }
1703
1704 void UciMenuProc(w, event, prms, nprms)
1705      Widget w;
1706      XEvent *event;
1707      String *prms;
1708      Cardinal *nprms;
1709 {
1710    oldCores = appData.smpCores;
1711    oldPonder = appData.ponderNextMove;
1712    GenericPopUp(commonEngineOptions, _("Common Engine Settings"), 0);
1713 }
1714
1715 void NewVariantProc(w, event, prms, nprms)
1716      Widget w;
1717      XEvent *event;
1718      String *prms;
1719      Cardinal *nprms;
1720 {
1721    GenericPopUp(variantDescriptors, _("New Variant"), 0);
1722 }
1723
1724 void OptionsProc(w, event, prms, nprms)
1725      Widget w;
1726      XEvent *event;
1727      String *prms;
1728      Cardinal *nprms;
1729 {
1730    oldPonder = appData.ponderNextMove;
1731    GenericPopUp(generalOptions, _("General Options"), 0);
1732 }
1733
1734 void MatchOptionsProc(w, event, prms, nprms)
1735      Widget w;
1736      XEvent *event;
1737      String *prms;
1738      Cardinal *nprms;
1739 {
1740    GenericPopUp(matchOptions, _("Match Options"), 0);
1741 }
1742
1743 Option textOptions[100];
1744 extern char *icsTextMenuString;
1745 void PutText P((char *text, int pos));
1746
1747 SendString(char *p)
1748 {
1749     char buf[MSG_SIZ], *q;
1750     if(q = strstr(p, "$input")) {
1751         if(!shellUp[4]) return;
1752         strncpy(buf, p, MSG_SIZ);
1753         strncpy(buf + (q-p), q+6, MSG_SIZ-(q-p));
1754         PutText(buf, q-p);
1755         return;
1756     }
1757     snprintf(buf, MSG_SIZ, "%s\n", p);
1758     SendToICS(buf);
1759 }
1760
1761 /* function called when the data to Paste is ready */
1762 static void
1763 SendTextCB(Widget w, XtPointer client_data, Atom *selection,
1764            Atom *type, XtPointer value, unsigned long *len, int *format)
1765 {
1766   char buf[MSG_SIZ], *p = (char*) textOptions[(int)(intptr_t) client_data].choice, *name = (char*) value, *q;
1767   if (value==NULL || *len==0) return; /* nothing selected, abort */
1768   name[*len]='\0';
1769   strncpy(buf, p, MSG_SIZ);
1770   q = strstr(p, "$name");
1771   snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1772   SendString(buf);
1773   XtFree(value);
1774 }
1775
1776 void SendText(int n)
1777 {
1778     char *p = (char*) textOptions[n].choice;
1779     if(strstr(p, "$name")) {
1780         XtGetSelectionValue(menuBarWidget,
1781           XA_PRIMARY, XA_STRING,
1782           /* (XtSelectionCallbackProc) */ SendTextCB,
1783           (XtPointer) (intptr_t) n, /* client_data passed to PastePositionCB */
1784           CurrentTime
1785         );
1786     } else SendString(p);
1787 }
1788
1789 void IcsTextProc(w, event, prms, nprms)
1790      Widget w;
1791      XEvent *event;
1792      String *prms;
1793      Cardinal *nprms;
1794 {
1795    int i=0, j;
1796    char *p, *q, *r;
1797    if((p = icsTextMenuString) == NULL) return;
1798    do {
1799         q = r = p; while(*p && *p != ';') p++;
1800         for(j=0; j<p-q; j++) textOptions[i].name[j] = *r++;
1801         textOptions[i].name[j++] = 0;
1802         if(!*p) break;
1803         if(*++p == '\n') p++; // optional linefeed after button-text terminating semicolon
1804         q = p;
1805         textOptions[i].choice = (char**) (r = textOptions[i].name + j);
1806         while(*p && (*p != ';' || p[1] != '\n')) textOptions[i].name[j++] = *p++;
1807         textOptions[i].name[j++] = 0;
1808         if(*p) p += 2;
1809         textOptions[i].max = 135;
1810         textOptions[i].min = i&1;
1811         textOptions[i].handle = NULL;
1812         textOptions[i].target = &SendText;
1813         textOptions[i].textValue = strstr(r, "$input") ? "#80FF80" : strstr(r, "$name") ? "#FF8080" : "#FFFFFF";
1814         textOptions[i].type = Button;
1815    } while(++i < 99 && *p);
1816    if(i == 0) return;
1817    textOptions[i].type = EndMark;
1818    textOptions[i].target = NULL;
1819    textOptions[i].min = 2;
1820    MarkMenu("menuView.ICStex", 3);
1821    GenericPopUp(textOptions, _("ICS text menu"), 3);
1822 }
1823
1824 static char *commentText;
1825 static int commentIndex;
1826 void ClearComment P((int n));
1827 extern char commentTranslations[];
1828
1829 void NewComCallback(int n)
1830 {
1831     ReplaceComment(commentIndex, commentText);
1832 }
1833
1834 void SaveChanges(int n)
1835 {
1836     Arg args[16];
1837     XtSetArg(args[0], XtNstring, &commentText);
1838     XtGetValues(currentOption[0].handle, args, 1);
1839     ReplaceComment(commentIndex, commentText);
1840 }
1841
1842 Option commentOptions[] = {
1843 { 0xD, 200, 300, NULL, (void*) &commentText, "", NULL, TextBox, "" },
1844 {   0,  0,    0, NULL, (void*) &ClearComment, NULL, NULL, Button, "clear" },
1845 {   0,  1,    0, NULL, (void*) &SaveChanges, NULL, NULL, Button, "save changes" },
1846 {   0,  1,    0, NULL, (void*) &NewComCallback, "", NULL, EndMark , "" }
1847 };
1848
1849 void ClearComment(int n)
1850 {
1851     XtCallActionProc(commentOptions[0].handle, "select-all", NULL, NULL, 0);
1852     XtCallActionProc(commentOptions[0].handle, "kill-selection", NULL, NULL, 0);
1853 }
1854
1855 void NewCommentPopup(char *title, char *text, int index)
1856 {
1857     Widget edit;
1858     Arg args[16];
1859
1860     if(shells[1]) { // if already exists, alter title and content
1861         XtSetArg(args[0], XtNtitle, title);
1862         XtSetValues(shells[1], args, 1);
1863         XtSetArg(args[0], XtNstring, text);
1864         XtSetValues(commentOptions[0].handle, args, 1);
1865     }
1866     commentText = text;
1867     commentIndex = index;
1868     MarkMenu("menuView.Show Comments", 1);
1869     if(GenericPopUp(commentOptions, title, 1))
1870         XtOverrideTranslations(commentOptions[0].handle, XtParseTranslationTable(commentTranslations));
1871 }
1872
1873 static char *tagsText, *msgText;
1874
1875 void NewTagsCallback(int n)
1876 {
1877     ReplaceTags(tagsText, &gameInfo);
1878 }
1879
1880 void changeTags(int n)
1881 {
1882     Arg args[16];
1883     XtSetArg(args[0], XtNstring, &tagsText);
1884     XtGetValues(currentOption[1].handle, args, 1);
1885     ReplaceTags(tagsText, &gameInfo);
1886 }
1887
1888 Option tagsOptions[] = {
1889 {   0,  0,    0, NULL, NULL, NULL, NULL, Label,  "" },
1890 { 0xD, 200, 250, NULL, (void*) &tagsText, "", NULL, TextBox, "" },
1891 {   0,  0,    0, NULL, (void*) &changeTags, NULL, NULL, Button, "save changes" },
1892 {   0,  1,    0, NULL, (void*) &NewTagsCallback, "", NULL, EndMark , "" }
1893 };
1894
1895 void NewTagsPopup(char *text, char *msg)
1896 {
1897     Widget edit;
1898     Arg args[16];
1899
1900     if(shells[2]) { // if already exists, alter title and content
1901         XtSetArg(args[0], XtNstring, text);
1902         XtSetValues(tagsOptions[1].handle, args, 1);
1903     }
1904     tagsText = text;
1905     tagsOptions[0].textValue = msg;
1906     MarkMenu("menuView.Show Tags", 2);
1907     GenericPopUp(tagsOptions, _("Tags"), 2);
1908 }
1909
1910 extern char ICSInputTranslations[];
1911 char *icsText;
1912
1913 Option boxOptions[] = {
1914 {   0, 30,  400, NULL, (void*) &icsText, "", NULL, TextBox, "" },
1915 {   0,  3,    0, NULL, NULL, "", NULL, EndMark , "" }
1916 };
1917
1918 void PutText(char *text, int pos)
1919 {
1920     Widget edit;
1921     Arg args[16];
1922     char buf[MSG_SIZ], *p;
1923
1924     if(strstr(text, "$add ") == text) {
1925         XtSetArg(args[0], XtNstring, &p);
1926         XtGetValues(boxOptions[0].handle, args, 1);
1927         snprintf(buf, MSG_SIZ, "%s%s", p, text+5); text = buf;
1928         pos += strlen(p) - 5;
1929     }
1930     XtSetArg(args[0], XtNstring, text);
1931     XtSetValues(boxOptions[0].handle, args, 1);
1932     XtSetArg(args[0], XtNinsertPosition, pos);
1933     XtSetValues(boxOptions[0].handle, args, 1);
1934 //    SetFocus(boxOptions[0].handle, shells[4], NULL, False); // No idea why this does not work, and the following is needed:
1935     XSetInputFocus(xDisplay, XtWindow(boxOptions[0].handle), RevertToPointerRoot, CurrentTime);
1936 }
1937
1938 void InputBoxPopup()
1939 {
1940     MarkMenu("menuView.ICS Input Box", 4);
1941     if(GenericPopUp(boxOptions, _("ICS input box"), 4))
1942         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(ICSInputTranslations));
1943 }
1944
1945 void TypeInProc(w, event, prms, nprms)
1946      Widget w;
1947      XEvent *event;
1948      String *prms;
1949      Cardinal *nprms;
1950 {
1951     Arg args[2];
1952     String val;
1953
1954     if(prms[0][0] == '1') {
1955         XtSetArg(args[0], XtNstring, &val);
1956         XtGetValues(boxOptions[0].handle, args, 1);
1957         TypeInDoneEvent((char*)val);
1958     }
1959     PopDown(0);\r
1960 }
1961
1962 char moveTypeInTranslations[] =
1963     "<Key>Return: TypeInProc(1) \n"
1964     "<Key>Escape: TypeInProc(0) \n";
1965
1966 void PopUpMoveDialog(char firstchar)
1967 {
1968     static char buf[2];
1969     buf[0] = firstchar; icsText = buf;
1970     if(GenericPopUp(boxOptions, _("Type a move"), 0))
1971         XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(moveTypeInTranslations));
1972 }
1973
1974 void MoveTypeInProc(Widget widget, caddr_t unused, XEvent *event)
1975 {
1976     char buf[10], keys[32];
1977     KeySym sym;
1978     KeyCode metaL, metaR;
1979     int n = XLookupString(&(event->xkey), buf, 10, &sym, NULL);
1980     XQueryKeymap(xDisplay,keys);
1981     metaL = XKeysymToKeycode(xDisplay, XK_Meta_L);
1982     metaR = XKeysymToKeycode(xDisplay, XK_Meta_R);
1983     if ( n == 1 && *buf > 32 && !(keys[metaL>>3]&1<<(metaL&7)) && !(keys[metaR>>3]&1<<(metaR&7))) // printable, no alt
1984         PopUpMoveDialog(*buf);
1985
1986 }
1987
1988 void
1989 SettingsPopUp(ChessProgramState *cps)
1990 {
1991    currentCps = cps;
1992    GenericPopUp(cps->option, _("Engine Settings"), 0);
1993 }
1994
1995 void FirstSettingsProc(w, event, prms, nprms)
1996      Widget w;
1997      XEvent *event;
1998      String *prms;
1999      Cardinal *nprms;
2000 {
2001     SettingsPopUp(&first);
2002 }
2003
2004 void SecondSettingsProc(w, event, prms, nprms)
2005      Widget w;
2006      XEvent *event;
2007      String *prms;
2008      Cardinal *nprms;
2009 {
2010    if(WaitForEngine(&second, SettingsMenuIfReady)) return;
2011    SettingsPopUp(&second);
2012 }
2013
2014 char *engineName, *engineDir, *engineChoice, *engineLine;
2015 Boolean isUCI, hasBook, storeVariant, v1, addToList;
2016 extern Option installOptions[];
2017 extern char *firstChessProgramNames;
2018 char *engineNr[] = { N_("First Engine"), N_("Second Engine"), NULL };
2019 char *engineList[100] = {" "}, *engineMnemonic[100] = {""};
2020
2021 void NamesToList(char *names)
2022 {
2023     char buf[MSG_SIZ], *p, *q;
2024     int i=1;
2025     while(*names) {
2026         p = names; q = buf;
2027         while(*p && *p != '\n') *q++ = *p++;
2028         *q = 0;
2029         if(engineList[i]) free(engineList[i]);
2030         engineList[i] = strdup(buf);
2031         if(*p == '\n') p++;
2032         TidyProgramName(engineList[i], "localhost", buf);
2033         if(engineMnemonic[i]) free(engineMnemonic[i]);
2034         if(q = strstr(engineList[i], " -variant ")) {
2035             strcat(buf, "(");
2036             sscanf(q + 10, "%s", buf + strlen(buf));
2037             strcat(buf, ")");
2038         }
2039         engineMnemonic[i] = strdup(buf);
2040         names = p; i++;
2041     }
2042     engineList[i] = NULL;
2043 }
2044
2045 // following implemented as macro to avoid type limitations
2046 #define SWAP(item, temp) temp = appData.item[0]; appData.item[0] = appData.item[n]; appData.item[n] = temp;
2047
2048 void SwapEngines(int n)
2049 {   // swap settings for first engine and other engine (so far only some selected options)
2050     int h;
2051     char *p;
2052     if(n == 0) return;
2053     SWAP(directory, p)
2054     SWAP(chessProgram, p)
2055     SWAP(isUCI, h)
2056     SWAP(hasOwnBookUCI, h)
2057     SWAP(protocolVersion, h)
2058     SWAP(reuse, h)
2059     SWAP(scoreIsAbsolute, h)
2060     SWAP(timeOdds, h)
2061 }
2062
2063 void Load(ChessProgramState *cps, int i)
2064 {
2065     char *p, *q, buf[MSG_SIZ];
2066     if(engineLine[0]) { // an engine was selected from the combo box
2067         snprintf(buf, MSG_SIZ, "-fcp %s", engineLine);
2068         SwapEngines(i); // kludge to parse -f* / -first* like it is -s* / -second*
2069         ParseArgsFromString(buf);
2070         SwapEngines(i);
2071         ReplaceEngine(cps, i);
2072         return;
2073     }
2074     p = engineName;
2075     while(q = strchr(p, '/')) p = q+1;
2076     if(*p== NULLCHAR) return;
2077     appData.chessProgram[i] = strdup(p);
2078     if(engineDir[0] != NULLCHAR)
2079         appData.directory[i] = engineDir;
2080     else if(p != engineName) { // derive directory from engine path, when not given
2081         p[-1] = 0;
2082         appData.directory[i] = strdup(engineName);
2083         p[-1] = '/';
2084     } else appData.directory[i] = ".";
2085     appData.isUCI[i] = isUCI;
2086     appData.protocolVersion[i] = v1 ? 1 : PROTOVER;
2087     appData.hasOwnBookUCI[i] = hasBook;
2088     if(addToList) {
2089         int len;
2090         q = firstChessProgramNames;
2091         snprintf(buf, MSG_SIZ, "\"%s\" -fd \"%s\"%s%s%s%s%s\n", p, appData.directory[i], 
2092                         v1 ? " -firstProtocolVersion 1" : "",
2093                         hasBook ? "" : " -fNoOwnBookUCI",
2094                         isUCI ? " -fUCI" : "",
2095                         storeVariant ? " -variant " : "",
2096                         storeVariant ? VariantName(gameInfo.variant) : "");
2097 printf("new line: %s", buf);
2098         firstChessProgramNames = malloc(len = strlen(q) + strlen(buf) + 1);
2099         snprintf(firstChessProgramNames, len, "%s%s", q, buf);
2100         if(q)   free(q);
2101     }
2102     ReplaceEngine(cps, i);
2103 }
2104
2105 void InstallOK(int n)
2106 {
2107     PopDown(0); // early popdown, to allow FreezeUI to instate grab
2108     if(engineChoice[0] == engineNr[0][0])  Load(&first, 0); else Load(&second, 1);
2109 }
2110
2111 Option installOptions[] = {
2112 {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },
2113 {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },
2114 {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("Engine Directory:") },
2115 {   0,  0,    0, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine Command:") },
2116 {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when empty)") },
2117 {   0,  0,    0, NULL, (void*) &isUCI, NULL, NULL, CheckBox, N_("UCI") },
2118 {   0,  0,    0, NULL, (void*) &v1, NULL, NULL, CheckBox, N_("WB protocol v1 (do not wait for engine features)") },
2119 {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },
2120 {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },
2121 {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },
2122 {   0,  0,    0, NULL, (void*) &engineChoice, (char*) engineNr, engineNr, ComboBox, N_("Load mentioned engine as") },
2123 {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }
2124 };
2125
2126 void LoadEngineProc(w, event, prms, nprms)
2127      Widget w;
2128      XEvent *event;
2129      String *prms;
2130      Cardinal *nprms;
2131 {
2132    isUCI = addToList = storeVariant = v1 = False; hasBook = True; // defaults
2133    engineDir = ""; 
2134    if(engineChoice) free(engineChoice); engineChoice = strdup(engineNr[0]);
2135    if(engineLine)   free(engineLine);   engineLine = strdup("");
2136    NamesToList(firstChessProgramNames);
2137    GenericPopUp(installOptions, _("Load engine"), 0);
2138 }
2139
2140 //---------------------------- Chat Windows ----------------------------------------------
2141
2142 void OutputChatMessage(int partner, char *mess)
2143 {
2144     return; // dummy
2145 }
2146