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