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