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