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