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