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