2 * xoptions.c -- Move list window, part of X front end for XBoard
4 * Copyright 2000, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 * ------------------------------------------------------------------------
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.
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.
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/. *
20 *------------------------------------------------------------------------
21 ** See the file ChangeLog for a revision history. */
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.
31 #include <sys/types.h>
36 #else /* not STDC_HEADERS */
37 extern char *getenv();
40 # else /* not HAVE_STRING_H */
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
50 #include <cairo/cairo.h>
51 #include <cairo/cairo-xlib.h>
63 # define _(s) gettext (s)
64 # define N_(s) gettext_noop (s)
70 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
73 static Widget previous = NULL;
75 static Option *currentOption;
76 static Boolean browserUp;
85 XtSetArg(args[0], XtNdisplayCaret, False);
86 XtSetValues(previous, args, 1);
94 SetFocus (Widget w, XtPointer data, XEvent *event, Boolean *b)
101 XtSetArg(args[0], XtNstring, &s);
102 XtGetValues(w, args, 1);
104 XtSetArg(args[0], XtNdisplayCaret, True);
105 if(!strchr(s, '\n') && strlen(s) < 80) XtSetArg(args[1], XtNinsertPosition, strlen(s)), j++;
106 XtSetValues(w, args, j);
107 XtSetKeyboardFocus((Widget) data, w);
116 XtSetKeyboardFocus(shellWidget, formWidget);
120 //--------------------------- Engine-specific options menu ----------------------------------
123 Option *dialogOptions[NrOfDialogs];
126 static Arg layoutArgs[] = {
127 { XtNborderWidth, 0 },
128 { XtNdefaultDistance, 0 },
131 static Arg formArgs[] = {
132 { XtNborderWidth, 0 },
133 { XtNresizable, (XtArgVal) True },
138 MarkMenuItem (char *menuRef, int state)
140 MenuItem *item = MenuNameToItem(menuRef);
145 XtSetArg(args[0], XtNleftBitmap, state ? xMarkPixmap : None);
146 XtSetValues(item->handle, args, 1);
151 void GetWidgetTextGTK(GtkWidget *w, char **buf)
156 if (GTK_IS_TEXT_BUFFER(w)) {
157 gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(w), &start);
158 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(w), &end);
159 *buf = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(w), &start, &end, FALSE);
162 printf("error in GetWidgetText, invalid widget\n");
168 GetWidgetText (Option *opt, char **buf)
176 case TextBox: GetWidgetTextGTK((GtkWidget *) opt->handle, buf); break;
178 x = gtk_spin_button_get_value (GTK_SPIN_BUTTON(opt->handle));
179 snprintf(val, 12, "%d", x); *buf = val;
182 printf("unexpected case (%d) in GetWidgetText\n", opt->type);
187 void SetSpinValue(Option *opt, int val, int n)
189 if (opt->type == Spin)
192 gtk_widget_set_sensitive(opt->handle, FALSE);
195 gtk_widget_set_sensitive(opt->handle, TRUE);
196 gtk_spin_button_set_value(opt->handle, val);
200 printf("error in SetSpinValue, unknown type %d\n", opt->type);
203 void SetWidgetTextGTK(GtkWidget *w, char *text)
205 if (!GTK_IS_TEXT_BUFFER(w)) {
206 printf("error: SetWidgetTextGTK arg is not a GtkTextBuffer\n");
209 gtk_text_buffer_set_text(GTK_TEXT_BUFFER(w), text, -1);
213 SetWidgetText (Option *opt, char *buf, int n)
219 case TextBox: SetWidgetTextGTK((GtkWidget *) opt->handle, buf); break;
220 case Spin: SetSpinValue(opt, atoi(buf), n); break;
222 printf("unexpected case (%d) in GetWidgetText\n", opt->type);
225 // focus is automatic in GTK?
226 if(n >= 0) SetFocus(opt->handle, shells[n], NULL, False);
231 GetWidgetState (Option *opt, int *state)
235 XtSetArg(arg, XtNstate, state);
236 XtGetValues(opt->handle, &arg, 1);
241 SetWidgetState (Option *opt, int state)
245 XtSetArg(arg, XtNstate, state);
246 XtSetValues(opt->handle, &arg, 1);
251 SetWidgetLabel (Option *opt, char *buf)
255 XtSetArg(arg, XtNlabel, (XtArgVal) buf);
256 XtSetValues(opt->handle, &arg, 1);
261 SetDialogTitle (DialogClass dlg, char *title)
265 XtSetArg(args[0], XtNtitle, title);
266 XtSetValues(shells[dlg], args, 1);
271 LoadListBox (Option *opt, char *emptyText, int n1, int n2)
274 static char *dummyList[2];
275 dummyList[0] = emptyText; // empty listboxes tend to crash X, so display user-supplied warning string instead
276 XawListChange(opt->handle, *(char*)opt->target ? opt->target : dummyList, 0, 0, True);
281 ReadScroll (Option *opt, float *top, float *bottom)
282 { // retreives fractions of top and bottom of thumb
285 Widget w = XtParent(opt->handle); // viewport
286 Widget v = XtNameToWidget(w, "vertical");
289 if(!v) return FALSE; // no scroll bar
290 XtSetArg(args[j], XtNshown, &h); j++;
291 XtSetArg(args[j], XtNtopOfThumb, top); j++;
292 XtGetValues(v, args, j);
299 SetScroll (Option *opt, float f)
300 { // sets top of thumb to given fraction
302 static char *params[3] = { "", "Continuous", "Proportional" };
304 Widget w = XtParent(opt->handle); // viewport
305 Widget v = XtNameToWidget(w, "vertical");
306 if(!v) return; // no scroll bar
307 XtCallActionProc(v, "StartScroll", &event, params+1, 1);
308 XawScrollbarSetThumb(v, f, -1.0);
309 XtCallActionProc(v, "NotifyThumb", &event, params, 0);
310 // XtCallActionProc(v, "NotifyScroll", &event, params+2, 1);
311 XtCallActionProc(v, "EndScroll", &event, params, 0);
316 HighlightListBoxItem (Option *opt, int nr)
319 XawListHighlight(opt->handle, nr);
324 HighlightWithScroll (Option *opt, int sel, int max)
327 float top, bottom, f, g;
328 HighlightListBoxItem(opt, sel);
329 if(!ReadScroll(opt, &top, &bottom)) return; // no scroll bar
330 bottom = bottom*max - 1.f;
333 if(sel > (top + 3*bottom)/4) f = (sel - 0.75f*(bottom-top))/max; else
334 if(sel < (3*top + bottom)/4) f = (sel - 0.25f*(bottom-top))/max;
335 if(f < 0.f) f = 0.; if(f + 1.f/max > 1.f) f = 1. - 1./max;
336 if(f != g) SetScroll(opt, f);
341 SelectedListBoxItem (Option *opt)
344 XawListReturnStruct *rs;
345 rs = XawListShowCurrent(opt->handle);
346 return rs->list_index;
353 FocusOnWidget (Option *opt, DialogClass dlg)
357 XtSetKeyboardFocus(shells[dlg], opt->handle);
362 SetIconName (DialogClass dlg, char *name)
367 XtSetArg(args[j], XtNiconName, (XtArgVal) name); j++;
368 // XtSetArg(args[j], XtNtitle, (XtArgVal) name); j++;
369 XtSetValues(shells[dlg], args, j);
375 CheckCallback (Widget ww, XtPointer client_data, XEvent *event, Boolean *b)
377 int s, data = (intptr_t) client_data;
378 Option *opt = dialogOptions[data >> 8] + (data & 255);
380 if(opt->type == Label) { ((ButtonCallback*) opt->target)(data&255); return; }
382 GetWidgetState(opt, &s);
383 SetWidgetState(opt, !s);
389 SpinCallback (Widget w, XtPointer client_data, XtPointer call_data)
393 char buf[MSG_SIZ], *p;
394 int j = 0; // Initialisation is necessary because the text value may be non-numeric causing the scanf conversion to fail
395 int data = (intptr_t) client_data;
396 Option *opt = dialogOptions[data >> 8] + (data & 255);
398 XtSetArg(args[0], XtNlabel, &name);
399 XtGetValues(w, args, 1);
401 GetWidgetText(opt, &val);
402 sscanf(val, "%d", &j);
403 if (strcmp(name, _("browse")) == 0) {
405 for(r = ""; *q; q++) if(*q == '.') r = q; else if(*q == '/') r = ""; // last dot after last slash
406 if(!strcmp(r, "") && !currentCps && opt->type == FileName && opt->textValue)
408 Browse(data>>8, opt->name, NULL, r, opt->type == PathName, "", &p, (FILE**) opt);
411 if (strcmp(name, "+") == 0) {
412 if(++j > opt->max) return;
414 if (strcmp(name, "-") == 0) {
415 if(--j < opt->min) return;
417 snprintf(buf, MSG_SIZ, "%d", j);
418 SetWidgetText(opt, buf, TransientDlg);
422 void ComboSelect(GtkWidget *widget, gpointer addr)
424 Option *opt = dialogOptions[((intptr_t)addr)>>8]; // applicable option list
425 gint i = ((intptr_t)addr) & 255; // option number
428 g = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
429 values[i] = g; // store in temporary, for transfer at OK
432 // Note: setting text on button is probably automatic
433 // Is this still needed? Could be all comboboxes that needed a callbak are now listboxes!
435 if(opt[i].type == Graph || opt[i].min & COMBO_CALLBACK && (!currentCps || shellUp[BrowserDlg])) {
436 ((ButtonCallback*) opt[i].target)(i);
443 CreateMenuItem (Widget menu, char *msg, XtCallbackProc CB, int n)
448 XtSetArg(args[j], XtNleftMargin, 20); j++;
449 XtSetArg(args[j], XtNrightMargin, 20); j++;
450 if(!strcmp(msg, "----")) { XtCreateManagedWidget(msg, smeLineObjectClass, menu, args, j); return NULL; }
451 XtSetArg(args[j], XtNlabel, msg);
452 entry = XtCreateManagedWidget("item", smeBSBObjectClass, menu, args, j+1);
453 XtAddCallback(entry, XtNcallback, CB, (caddr_t)(intptr_t) n);
460 CreateComboPopup (Widget parent, Option *opt, int n, int fromList, int def)
461 { // fromList determines if the item texts are taken from a list of strings, or from a menu table
465 MenuItem *mb = (MenuItem *) opt->choice;
466 char **list = (char **) opt->choice;
468 if(list[0] == NULL) return NULL; // avoid empty menus, as they cause crash
469 menu = XtCreatePopupShell(opt->name, simpleMenuWidgetClass, parent, NULL, 0);
473 char *msg = fromList ? list[i] : mb[i].string;
475 entry = CreateMenuItem(menu, opt->min & NO_GETTEXT ? msg : _(msg), (XtCallbackProc) ComboSelect, (n<<16)+i);
476 if(!fromList) mb[i].handle = (void*) entry; // save item ID, for enabling / checkmarking
478 XtSetArg(arg, XtNpopupOnEntry, entry);
479 XtSetValues(menu, &arg, 1);
486 MenuSelect (gpointer addr) // callback for all combo items
488 Option *opt = dialogOptions[((intptr_t)addr)>>24]; // applicable option list
489 int i = ((intptr_t)addr)>>16 & 255; // option number
490 int j = 0xFFFF & (intptr_t) addr;
492 values[i] = j; // store selected value in Option struct, for retrieval at OK
493 ((ButtonCallback*) opt[i].target)(i);
497 CreateMenuPopup (Option *opt, int n, int def)
498 { // fromList determines if the item texts are taken from a list of strings, or from a menu table
500 GtkWidget *menu, *entry;
501 MenuItem *mb = (MenuItem *) opt->choice;
503 menu = gtk_menu_new();
504 // menu = XtCreatePopupShell(opt->name, simpleMenuWidgetClass, parent, NULL, 0);
507 char *msg = mb[i].string;
509 if(strcmp(msg, "----")) { //
510 if(!(opt->min & NO_GETTEXT)) msg = _(msg);
512 entry = gtk_check_menu_item_new_with_label(msg); // should be used for items that can be checkmarked
513 if(mb[i].handle == RADIO) gtk_check_menu_item_set_draw_as_radio(entry, True);
515 entry = gtk_menu_item_new_with_label(msg);
516 gtk_signal_connect_object (GTK_OBJECT (entry), "activate", GTK_SIGNAL_FUNC(MenuSelect), (gpointer) (n<<16)+i);
517 gtk_widget_show(entry);
518 } else entry = gtk_separator_menu_item_new();
519 gtk_menu_append(GTK_MENU (menu), entry);
520 //CreateMenuItem(menu, opt->min & NO_GETTEXT ? msg : _(msg), (XtCallbackProc) ComboSelect, (n<<16)+i);
521 mb[i].handle = (void*) entry; // save item ID, for enabling / checkmarking
523 // XtSetArg(arg, XtNpopupOnEntry, entry);
524 // XtSetValues(menu, &arg, 1);
531 char moveTypeInTranslations[] =
532 "<Key>Return: TypeInProc(1) \n"
533 "<Key>Escape: TypeInProc(0) \n";
534 extern char filterTranslations[];
535 extern char gameListTranslations[];
536 extern char memoTranslations[];
539 char *translationTable[] = { // beware: order is essential!
540 historyTranslations, commentTranslations, moveTypeInTranslations, ICSInputTranslations,
541 filterTranslations, gameListTranslations, memoTranslations
545 AddHandler (Option *opt, int nr)
551 XtOverrideTranslations(opt->handle, XtParseTranslationTable(translationTable[nr]));
555 //----------------------------Generic dialog --------------------------------------------
557 // cloned from Engine Settings dialog (and later merged with it)
559 GtkWidget *shells[NrOfDialogs];
560 DialogClass parents[NrOfDialogs];
561 WindowPlacement *wp[NrOfDialogs] = { // Beware! Order must correspond to DialogClass enum
562 NULL, &wpComment, &wpTags, NULL, NULL, NULL, NULL, &wpMoveHistory, &wpGameList, &wpEngineOutput, &wpEvalGraph,
563 NULL, NULL, NULL, NULL, /*&wpMain*/ NULL
567 DialogExists (DialogClass n)
568 { // accessor for use in back-end
569 return shells[n] != NULL;
573 RaiseWindow (DialogClass dlg)
577 Window root = RootWindow(xDisplay, DefaultScreen(xDisplay));
578 Atom atom = XInternAtom (xDisplay, "_NET_ACTIVE_WINDOW", False);
580 xev.xclient.type = ClientMessage;
581 xev.xclient.serial = 0;
582 xev.xclient.send_event = True;
583 xev.xclient.display = xDisplay;
584 xev.xclient.window = XtWindow(shells[dlg]);
585 xev.xclient.message_type = atom;
586 xev.xclient.format = 32;
587 xev.xclient.data.l[0] = 1;
588 xev.xclient.data.l[1] = CurrentTime;
590 XSendEvent (xDisplay,
592 SubstructureRedirectMask | SubstructureNotifyMask,
596 XSync(xDisplay, False);
600 /* Sets a check box on (True) or off (False) in the menu */
601 void SetCheckMenuItemActive(gchar *name, int menuDlgNr, gboolean active)
604 // This doesn't work anymore, as we don't use builder
605 gchar menuItemName[50];
608 strcpy(menuItemName, name);
610 GetMenuItemName(menuDlgNr, menuItemName);
612 if (strcmp(menuItemName, "") == 0) return;
614 GtkCheckMenuItem *chk;
615 chk = GTK_CHECK_MENU_ITEM(gtk_builder_get_object(GTK_BUILDER(builder), menuItemName));
617 printf("Error: failed to get check menu item %s from builder\n", menuItemName);
620 chk->active = active; // set the check box without emitting any signals
625 PopDown (DialogClass n)
629 if (!shellUp[n] || !shells[n]) return 0;
631 // Not sure this is still used
632 if(n && wp[n]) { // remember position
634 XtSetArg(args[j], XtNx, &windowX); j++;
635 XtSetArg(args[j], XtNy, &windowY); j++;
636 XtSetArg(args[j], XtNheight, &windowH); j++;
637 XtSetArg(args[j], XtNwidth, &windowW); j++;
638 XtGetValues(shells[n], args, j);
641 wp[n]->width = windowW;
642 wp[n]->height = windowH;
646 gtk_widget_hide(shells[n]);
647 shellUp[n]--; // count rather than clear
648 if(n == 0 || n >= PromoDlg) {
649 gtk_widget_destroy(shells[n]);
654 MarkMenuItem(marked[n], False);
658 // when popping down uncheck the check box of the menu item
659 SetCheckMenuItemActive(NULL, n, False);
661 if(!n) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
662 currentOption = dialogOptions[TransientDlg]; // just in case a transient dialog was up (to allow its check and combo callbacks to work)
664 RaiseWindow(parents[n]); // automatic in GTK?
665 if(parents[n] == BoardWindow) XtSetKeyboardFocus(shellWidget, formWidget); // also automatic???
670 gboolean GenericPopDown(w, event, gdata)
675 int dlg = (intptr_t) gdata; /* dialog number dlgnr */
678 // I guess BrowserDlg will be abandoned, as GTK has a better browser of its own
679 if(shellUp[BrowserDlg] && dlg != BrowserDlg || dialogError) return; // prevent closing dialog when it has an open file-browse daughter
681 if(browserUp || dialogError) return True; // prevent closing dialog when it has an open file-browse daughter
683 GtkWidget *sh = shells[dlg];
684 printf("popdown %d\n", dlg);
685 shells[dlg] = w; // make sure we pop down the right one in case of multiple instances
687 shells[dlg] = sh; // restore
688 if(dlg == BoardWindow) ExitEvent(0);
689 return True; /* don't propagate to default handler */
692 int AppendText(Option *opt, char *s)
698 GetWidgetTextGTK(opt->handle, &v);
701 gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(opt->handle), &end);
702 gtk_text_buffer_insert(opt->handle, &end, s, -1);
708 SetColor (char *colorName, Option *box)
709 { // sets the color of a widget
714 if (!appData.monoMode) {
715 vFrom.addr = (caddr_t) colorName;
716 vFrom.size = strlen(colorName);
717 XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
718 if (vTo.addr == NULL) {
719 buttonColor = (Pixel) -1;
721 buttonColor = *(Pixel *) vTo.addr;
723 } else buttonColor = timerBackgroundPixel;
724 XtSetArg(args[0], XtNbackground, buttonColor);;
725 XtSetValues(box->handle, args, 1);
731 ColorChanged (Widget w, XtPointer data, XEvent *event, Boolean *b)
732 { // for detecting a typed change in color
734 if ( (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) && *buf == '\r' )
735 RefreshColor((int)(intptr_t) data, 0);
740 GraphEventProc(GtkWidget *widget, GdkEventExpose *event, gpointer gdata)
741 { // handle expose and mouse events on Graph widget
743 int j, button=10, f=1, sizing=0;
744 Option *opt, *graph = (Option *) gdata;
745 PointerCallback *userHandler = graph->target;
746 GdkEventExpose *eevent = (GdkEventExpose *) event;
747 GdkEventButton *bevent = (GdkEventButton *) event;
748 GdkEventMotion *mevent = (GdkEventButton *) event;
751 // if (!XtIsRealized(widget)) return;
753 switch(event->type) {
754 case GDK_EXPOSE: // make handling of expose events generic, just copying from memory buffer (->choice) to display (->textValue)
755 /* Get window size */
758 XtSetArg(args[j], XtNwidth, &w); j++;
759 XtSetArg(args[j], XtNheight, &h); j++;
760 XtGetValues(widget, args, j);
762 if(w < graph->max || w > graph->max + 1 || h != graph->value) { // use width fudge of 1 pixel
763 if(((XExposeEvent*)event)->count >= 0) { // suppress sizing on expose for ordered redraw in response to sizing.
765 graph->max = w; graph->value = h; // note: old values are kept if we we don't exceed width fudge
767 } else w = graph->max;
769 if(sizing && ((XExposeEvent*)event)->count > 0) { graph->max = 0; return; } // don't bother if further exposure is pending during resize
770 if(!graph->textValue || sizing) { // create surfaces of new size for display widget
771 if(graph->textValue) cairo_surface_destroy((cairo_surface_t *)graph->textValue);
772 graph->textValue = (char*) cairo_xlib_surface_create(xDisplay, XtWindow(widget), DefaultVisual(xDisplay, 0), w, h);
774 if(sizing) { // the memory buffer was already created in GenericPopup(),
775 // to give drawing routines opportunity to use it before first expose event
776 // (which are only processed when main gets to the event loop, so after all init!)
777 // so only change when size is no longer good
778 if(graph->choice) cairo_surface_destroy((cairo_surface_t *) graph->choice);
779 graph->choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
783 w = eevent->area.width;
784 if(eevent->area.x + w > graph->max) w--; // cut off fudge pixel
785 cr = gdk_cairo_create(((GtkWidget *) (graph->handle))->window);
786 cairo_set_source_surface(cr, (cairo_surface_t *) graph->choice, 0, 0);
787 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
788 cairo_rectangle(cr, eevent->area.x, eevent->area.y, w, eevent->area.height);
793 case GDK_MOTION_NOTIFY:
795 w = mevent->x; h = mevent->y;
797 case GDK_BUTTON_RELEASE:
798 f = -1; // release indicated by negative button numbers
799 case GDK_BUTTON_PRESS:
800 w = bevent->x; h = bevent->y;
801 button = bevent->button;
805 opt = userHandler(button, w, h);
807 if(opt) { // user callback specifies a context menu; pop it up
808 XUngrabPointer(xDisplay, CurrentTime);
809 XtCallActionProc(widget, "XawPositionSimpleMenu", event, &(opt->name), 1);
810 XtPopupSpringLoaded(opt->handle);
812 XSync(xDisplay, False);
817 GraphExpose (Option *opt, int x, int y, int w, int h)
820 if(!opt->handle) return;
821 e.area.x = x; e.area.y = y; e.area.width = w; e.area.height = h; e.count = -1; e.type = GDK_EXPOSE; // count = -1: kludge to suppress sizing
822 GraphEventProc(opt->handle, (GdkEvent *) &e, (gpointer) opt); // fake expose event
825 /* GTK callback used when OK/cancel clicked in genericpopup for non-modal dialog */
826 void GenericPopUpCallback(w, resptype, gdata)
828 GtkResponseType resptype;
831 int data = (intptr_t) gdata; /* dialog number dlgnr */
834 currentOption = dialogOptions[dlg=data>>16]; data &= 0xFFFF;
837 if (resptype == GTK_RESPONSE_ACCEPT) {
838 if (GenericReadout(currentOption, -1)) PopDown(data);
846 void GenericCallback(GtkWidget *widget, gpointer gdata)
850 int data = (intptr_t) gdata;
853 GtkWidget *sh = XtParent(XtParent(XtParent(w))), *oldSh;
855 GtkWidget *sh, *oldSh;
858 currentOption = dialogOptions[dlg=data>>16]; data &= 0xFFFF;
860 sh = shells[dlg]; // make following line a no-op, as we haven't found out what the real shell is yet (breaks multiple popups of same type!)
862 oldSh = shells[dlg]; shells[dlg] = sh; // bow to reality
865 if (data == 30000) { // cancel
868 if (data == 30001) { // save buttons imply OK
869 if(GenericReadout(currentOption, -1)) PopDown(dlg); // calls OK-proc after full readout, but no popdown if it returns false
874 name = gtk_button_get_label (GTK_BUTTON(widget));
875 if(currentOption[data].type == SaveButton) GenericReadout(currentOption, -1);
876 snprintf(buf, MSG_SIZ, "option %s\n", name);
877 SendToProgram(buf, currentCps);
878 } else ((ButtonCallback*) currentOption[data].target)(data);
880 shells[dlg] = oldSh; // in case of multiple instances, restore previous (as this one could be popped down now)
885 TabProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
886 { // for transfering focus to the next text-edit
888 for(opt = currentOption; opt->type != EndMark; opt++) {
889 if(opt->handle == w) {
891 if(opt->type == EndMark) opt = currentOption; // wrap
892 if(opt->handle == w) return; // full circle
893 if(opt->type == TextBox || opt->type == Spin || opt->type == Fractional || opt->type == FileName || opt->type == PathName) {
894 SetFocus(opt->handle, XtParent(XtParent(XtParent(w))), NULL, 0);
905 WheelProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
906 { // for scrolling a widget seen through a viewport with the mouse wheel (ListBox!)
907 int j=0, n = atoi(prms[0]);
908 static char *params[3] = { "", "Continuous", "Proportional" };
912 if(!n) { // transient dialogs also use this for list-selection callback
914 Option *opt=dialogOptions[prms[2][0]-'A'] + n;
915 if(opt->textValue) ((ListBoxCallback*) opt->textValue)(n, SelectedListBoxItem(opt));
918 v = XtNameToWidget(XtParent(w), "vertical");
920 XtSetArg(args[j], XtNshown, &h); j++;
921 XtSetArg(args[j], XtNtopOfThumb, &top); j++;
922 XtGetValues(v, args, j);
923 top += 0.1f*h*n; if(top < 0.f) top = 0.;
924 XtCallActionProc(v, "StartScroll", event, params+1, 1);
925 XawScrollbarSetThumb(v, top, -1.0);
926 XtCallActionProc(v, "NotifyThumb", event, params, 0);
927 // XtCallActionProc(w, "NotifyScroll", event, params+2, 1);
928 XtCallActionProc(v, "EndScroll", event, params, 0);
932 static char *oneLiner =
933 "<Key>Return: redraw-display() \n \
934 <Key>Tab: TabProc() \n ";
935 static char scrollTranslations[] =
936 "<Btn1Up>(2): WheelProc(0 0 A) \n \
937 <Btn4Down>: WheelProc(-1) \n \
938 <Btn5Down>: WheelProc(1) \n ";
941 SqueezeIntoBox (Option *opt, int nr, int width)
942 { // size buttons in bar to fit, clipping button names where necessary
945 Dimension widths[20], oldWidths[20];
947 for(i=1; i<nr; i++) {
948 XtSetArg(arg, XtNwidth, &widths[i]);
949 XtGetValues(opt[i].handle, &arg, 1);
950 wtot += oldWidths[i] = widths[i];
953 if(width <= 0) return;
954 while(wtot > width) {
956 for(i=1; i<nr; i++) if(widths[i] > wmax) wmax = widths[imax=i];
960 for(i=1; i<nr; i++) if(widths[i] != oldWidths[i]) {
961 XtSetArg(arg, XtNwidth, widths[i]);
962 XtSetValues(opt[i].handle, &arg, 1);
970 SetPositionAndSize (Arg *args, Widget leftNeigbor, Widget topNeigbor, int b, int w, int h, int chaining)
971 { // sizing and positioning most widgets have in common
973 // first position the widget w.r.t. earlier ones
974 if(chaining & 1) { // same row: position w.r.t. last (on current row) and lastrow
975 XtSetArg(args[j], XtNfromVert, topNeigbor); j++;
976 XtSetArg(args[j], XtNfromHoriz, leftNeigbor); j++;
977 } else // otherwise it goes at left margin (which is default), below the previous element
978 XtSetArg(args[j], XtNfromVert, leftNeigbor), j++;
979 // arrange chaining ('2'-bit indicates top and bottom chain the same)
980 if((chaining & 14) == 6) XtSetArg(args[j], XtNtop, XtChainBottom), j++;
981 if((chaining & 14) == 10) XtSetArg(args[j], XtNbottom, XtChainTop ), j++;
982 if(chaining & 4) XtSetArg(args[j], XtNbottom, XtChainBottom ), j++;
983 if(chaining & 8) XtSetArg(args[j], XtNtop, XtChainTop), j++;
984 if(chaining & 0x10) XtSetArg(args[j], XtNright, XtChainRight), j++;
985 if(chaining & 0x20) XtSetArg(args[j], XtNleft, XtChainRight), j++;
986 if(chaining & 0x40) XtSetArg(args[j], XtNright, XtChainLeft ), j++;
987 if(chaining & 0x80) XtSetArg(args[j], XtNleft, XtChainLeft ), j++;
988 // set size (if given)
989 if(w) XtSetArg(args[j], XtNwidth, w), j++;
990 if(h) XtSetArg(args[j], XtNheight, h), j++;
992 if(!appData.monoMode) {
993 if(!b && appData.dialogColor[0]) XtSetArg(args[j], XtNbackground, dialogColor), j++;
994 if(b == 3 && appData.buttonColor[0]) XtSetArg(args[j], XtNbackground, buttonColor), j++;
998 XtSetArg(args[j], XtNborderWidth, b); j++;
1004 GenericPopUp (Option *option, char *title, DialogClass dlgNr, DialogClass parent, int modal, int topLevel)
1006 GtkWidget *dialog = NULL;
1010 GtkWidget *checkbutton;
1016 GtkAdjustment *spinner_adj;
1017 GtkWidget *combobox;
1018 GtkWidget *textview;
1019 GtkTextBuffer *textbuffer;
1021 GtkWidget *actionarea;
1025 GtkWidget *menuButton;
1029 int i, j, arraysize, left, top, height=999, width=1, boxStart;
1030 char def[MSG_SIZ], *msg, engineDlg = (currentCps != NULL && dlgNr != BrowserDlg);
1032 if(dlgNr < PromoDlg && shellUp[dlgNr]) return 0; // already up
1034 if(dlgNr && dlgNr < PromoDlg && shells[dlgNr]) { // reusable, and used before (but popped down)
1035 gtk_widget_show(shells[dlgNr]);
1036 shellUp[dlgNr] = True;
1040 dialogOptions[dlgNr] = option; // make available to callback
1041 // post currentOption globally, so Spin and Combo callbacks can already use it
1042 // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1043 currentOption = option;
1045 if(engineDlg) { // Settings popup for engine: format through heuristic
1046 int n = currentCps->nrOptions;
1047 if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1048 height = n / width + 1;
1049 // if(n && (currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton)) currentOption[n].min = SAME_ROW; // OK on same line
1050 currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1055 XtSetArg(args[i], XtNresizable, True); i++;
1056 shells[BoardWindow] = shellWidget; parents[dlgNr] = parent;
1058 if(dlgNr == BoardWindow) dialog = shellWidget; else
1060 XtCreatePopupShell(title, !top || !appData.topLevel ? transientShellWidgetClass : topLevelShellWidgetClass,
1061 shells[parent], args, i);
1063 dialog = gtk_dialog_new_with_buttons( title,
1065 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR |
1066 (modal ? GTK_DIALOG_MODAL : 0),
1067 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1068 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1071 shells[dlgNr] = dialog;
1072 box = gtk_dialog_get_content_area( GTK_DIALOG( dialog ) );
1073 gtk_box_set_spacing(GTK_BOX(box), 5);
1076 for (i=0;option[i].type != EndMark;i++) {
1080 table = gtk_table_new(arraysize, 3, FALSE);
1081 gtk_table_set_col_spacings(GTK_TABLE(table), 20);
1085 for (i=0;option[i].type != EndMark;i++) {
1086 if(option[i].type == -1) continue;
1088 if (top >= height) {
1091 gtk_table_resize(GTK_TABLE(table), height, left + 3);
1093 switch(option[i].type) {
1095 snprintf(def, MSG_SIZ, "%.2f", *(float*)option[i].target);
1096 option[i].value = *(float*)option[i].target;
1099 if(!currentCps) option[i].value = *(int*)option[i].target;
1100 snprintf(def, MSG_SIZ, "%d", option[i].value);
1105 label = gtk_label_new(option[i].name);
1107 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1110 w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1111 if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1113 if (option[i].type==TextBox && option[i].min > 80){
1114 textview = gtk_text_view_new();
1115 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
1116 /* add textview to scrolled window so we have vertical scroll bar */
1117 sw = gtk_scrolled_window_new(NULL, NULL);
1118 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1119 gtk_container_add(GTK_CONTAINER(sw), textview);
1120 gtk_widget_set_size_request(GTK_WIDGET(sw), w, -1);
1122 textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
1123 gtk_widget_set_size_request(textview, -1, option[i].min);
1124 /* check if label is empty */
1125 if (strcmp(option[i].name,"") != 0) {
1126 gtk_table_attach_defaults(GTK_TABLE(table), label, left, left+1, top, top+1);
1127 gtk_table_attach_defaults(GTK_TABLE(table), sw, left+1, left+3, top, top+1);
1130 /* no label so let textview occupy all columns */
1131 gtk_table_attach_defaults(GTK_TABLE(table), sw, left, left+3, top, top+1);
1133 if ( *(char**)option[i].target != NULL )
1134 gtk_text_buffer_set_text (textbuffer, *(char**)option[i].target, -1);
1136 gtk_text_buffer_set_text (textbuffer, "", -1);
1137 option[i].handle = (void*)textbuffer;
1141 entry = gtk_entry_new();
1143 if (option[i].type==Spin || option[i].type==Fractional)
1144 gtk_entry_set_text (GTK_ENTRY (entry), def);
1145 else if (currentCps)
1146 gtk_entry_set_text (GTK_ENTRY (entry), option[i].textValue);
1147 else if ( *(char**)option[i].target != NULL )
1148 gtk_entry_set_text (GTK_ENTRY (entry), *(char**)option[i].target);
1150 //gtk_entry_set_width_chars (GTK_ENTRY (entry), 18);
1151 gtk_entry_set_max_length (GTK_ENTRY (entry), w);
1153 // left, right, top, bottom
1154 if (strcmp(option[i].name, "") != 0) gtk_table_attach_defaults(GTK_TABLE(table), label, left, left+1, top, top+1);
1155 //gtk_table_attach_defaults(GTK_TABLE(table), entry, 1, 2, i, i+1);
1157 if (option[i].type == Spin) {
1158 spinner_adj = (GtkAdjustment *) gtk_adjustment_new (option[i].value, option[i].min, option[i].max, 1.0, 0.0, 0.0);
1159 spinner = gtk_spin_button_new (spinner_adj, 1.0, 0);
1160 gtk_table_attach_defaults(GTK_TABLE(table), spinner, left+1, left+3, top, top+1);
1161 option[i].handle = (void*)spinner;
1163 else if (option[i].type == FileName || option[i].type == PathName) {
1164 gtk_table_attach_defaults(GTK_TABLE(table), entry, left+1, left+2, top, top+1);
1165 button = gtk_button_new_with_label ("Browse");
1166 gtk_table_attach_defaults(GTK_TABLE(table), button, left+2, left+3, top, top+1);
1167 g_signal_connect (button, "clicked", G_CALLBACK (Browse), (gpointer)(intptr_t) i);
1168 option[i].handle = (void*)entry;
1171 hbox = gtk_hbox_new (FALSE, 0);
1172 if (strcmp(option[i].name, "") == 0)
1173 gtk_table_attach_defaults(GTK_TABLE(table), hbox, left, left+3, top, top+1);
1175 gtk_table_attach_defaults(GTK_TABLE(table), hbox, left+1, left+3, top, top+1);
1176 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
1177 //gtk_table_attach_defaults(GTK_TABLE(table), entry, left+1, left+3, top, top+1);
1178 option[i].handle = (void*)entry;
1182 checkbutton = gtk_check_button_new_with_label(option[i].name);
1183 if(!currentCps) option[i].value = *(Boolean*)option[i].target;
1184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), option[i].value);
1185 gtk_table_attach_defaults(GTK_TABLE(table), checkbutton, left, left+3, top, top+1);
1186 option[i].handle = (void *)checkbutton;
1189 label = gtk_label_new(option[i].name);
1191 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1192 gtk_table_attach_defaults(GTK_TABLE(table), label, left, left+3, top, top+1);
1196 button = gtk_button_new_with_label (option[i].name);
1198 /* set button color on view board dialog */
1199 if(option[i].choice && ((char*)option[i].choice)[0] == '#' && !currentCps) {
1200 gdk_color_parse( *(char**) option[i-1].target, &color );
1201 gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_NORMAL, &color );
1204 /* set button color on new variant dialog */
1205 if(option[i].textValue) {
1206 gdk_color_parse( option[i].textValue, &color );
1207 gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_NORMAL, &color );
1208 gtk_widget_set_sensitive(button, appData.noChessProgram || option[i].value < 0
1209 || strstr(first.variants, VariantName(option[i].value)));
1212 if (!(option[i].min & 1)) {
1213 if(option[i].textValue) // for new variant dialog give buttons equal space so they line up nicely
1214 hbox = gtk_hbox_new (TRUE, 0);
1216 hbox = gtk_hbox_new (FALSE, 0);
1217 // if only 1 button then put it in 1st column of table only
1218 if ( (arraysize >= (i+1)) && option[i+1].type != Button )
1219 gtk_table_attach_defaults(GTK_TABLE(table), hbox, left, left+1, top, top+1);
1221 gtk_table_attach_defaults(GTK_TABLE(table), hbox, left, left+3, top, top+1);
1223 gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
1224 g_signal_connect (button, "clicked", G_CALLBACK (GenericCallback), (gpointer)(intptr_t) i + (dlgNr<<16));
1225 option[i].handle = (void*)button;
1228 label = gtk_label_new(option[i].name);
1230 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1231 gtk_table_attach_defaults(GTK_TABLE(table), label, left, left+1, top, top+1);
1233 combobox = gtk_combo_box_new_text();
1236 if ( ((char **) option[i].textValue)[j] == NULL) break;
1237 gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), ((char **) option[i].textValue)[j]);
1241 option[i].choice = (char**) option[i].textValue;
1243 for(j=0; option[i].choice[j]; j++) {
1244 if(*(char**)option[i].target && !strcmp(*(char**)option[i].target, option[i].choice[j])) break;
1246 /* If choice is NULL set to first */
1247 if (option[i].choice[j] == NULL)
1248 option[i].value = 0;
1250 option[i].value = j;
1253 //option[i].value = j + (option[i].choice[j] == NULL);
1254 gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), option[i].value);
1257 hbox = gtk_hbox_new (FALSE, 0);
1258 gtk_table_attach_defaults(GTK_TABLE(table), hbox, left+1, left+3, top, top+1);
1259 gtk_box_pack_start (GTK_BOX (hbox), combobox, TRUE, TRUE, 0);
1260 //gtk_table_attach_defaults(GTK_TABLE(table), combobox, 1, 2, i, i+1);
1262 g_signal_connect(G_OBJECT(combobox), "changed", G_CALLBACK(ComboSelect), (gpointer) (intptr_t) (i + 256*dlgNr));
1264 option[i].handle = (void*)combobox;
1265 values[i] = option[i].value;
1269 GtkCellRenderer *renderer;
1270 GtkTreeViewColumn *column;
1271 GtkListStore *store;
1273 option[i].handle = (void *) (list = gtk_tree_view_new());
1274 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
1275 renderer = gtk_cell_renderer_text_new();
1276 column = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", 0, NULL);
1277 gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
1278 store = gtk_list_store_new(1, G_TYPE_STRING); // 1 column of text
1279 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
1280 g_object_unref(store);
1281 LoadListBox(&option[i], "?", -1, -1);
1282 HighlightListBoxItem(&option[i], 0);
1284 /* add listbox to scrolled window so we have vertical scroll bar */
1285 sw = gtk_scrolled_window_new(NULL, NULL);
1286 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1287 gtk_container_add(GTK_CONTAINER(sw), list);
1288 gtk_widget_set_size_request(GTK_WIDGET(sw), w, 300);
1290 /* never has label, so let listbox occupy all columns */
1291 gtk_table_attach_defaults(GTK_TABLE(table), sw, left, left+3, top, top+1);
1295 option[i].handle = (void*) (graph = gtk_drawing_area_new());
1296 gtk_widget_set_size_request(graph, option[i].max, option[i].value);
1297 // gtk_drawing_area_size(graph, option[i].max, option[i].value);
1298 gtk_table_attach_defaults(GTK_TABLE(table), graph, left, left+3, top, top+1);
1299 g_signal_connect (graph, "expose-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
1300 gtk_widget_add_events(GTK_WIDGET(graph), GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
1301 g_signal_connect (graph, "button-press-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
1302 g_signal_connect (graph, "button-release-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
1303 g_signal_connect (graph, "motion-notify-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
1304 // g_signal_connect (graph, "motion-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
1307 XtAddEventHandler(last, ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, False,
1308 (XtEventHandler) GraphEventProc, &option[i]); // mandatory user-supplied expose handler
1309 if(option[i].min & SAME_ROW) last = forelast, forelast = lastrow;
1311 option[i].choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, option[i].max, option[i].value); // image buffer
1315 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1316 option[i].max /* w */, option[i].value /* h */, option[i].min /* chain */);
1317 option[i].handle = (void*)
1318 (last = XtCreateManagedWidget("graph", widgetClass, form, args, j));
1319 XtAddEventHandler(last, ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, False,
1320 (XtEventHandler) GraphEventProc, &option[i]); // mandatory user-supplied expose handler
1321 if(option[i].min & SAME_ROW) last = forelast, forelast = lastrow;
1322 option[i].choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, option[i].max, option[i].value); // image buffer
1324 case PopUp: // note: used only after Graph, so 'last' refers to the Graph widget
1325 option[i].handle = (void*) CreateComboPopup(last, option + i, i + 256*dlgNr, TRUE, option[i].value);
1328 if(option[i].min & SAME_ROW) forelast = lastrow;
1329 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1330 0 /* w */, 0 /* h */, option[i].min /* chain */);
1331 XtSetArg(args[j], XtNorientation, XtorientHorizontal); j++;
1332 XtSetArg(args[j], XtNvSpace, 0); j++;
1333 option[box=i].handle = (void*)
1334 (last = XtCreateWidget("box", boxWidgetClass, form, args, j));
1335 oldForm = form; form = last; oldLastRow = lastrow; oldForeLast = forelast;
1336 lastrow = NULL; last = NULL;
1340 msg = _(option[i].name); // write name on the menu button
1341 // XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name)); j++;
1342 // XtSetArg(args[j], XtNlabel, msg); j++;
1343 option[i].handle = (void*)
1344 (menuButton = gtk_menu_item_new_with_label(msg));
1345 gtk_widget_show(menuButton);
1346 option[i].textValue = (char*) (menu = CreateMenuPopup(option + i, i + 256*dlgNr, -1));
1347 gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuButton), menu);
1348 gtk_menu_bar_append (GTK_MENU_BAR (menuBar), menuButton);
1352 menuBar = gtk_menu_bar_new ();
1353 gtk_widget_show (menuBar);
1358 gtk_table_attach_defaults(GTK_TABLE(table), menuBar, left, left+1, top, top+1);
1360 // XtManageChildren(&form, 1);
1361 // SqueezeIntoBox(&option[boxStart], i-boxStart, option[boxStart].max);
1362 if(option[i].target) ((ButtonCallback*)option[i].target)(boxStart); // callback that can make sizing decisions
1365 top = height; // force next option to start in a new column
1368 printf("GenericPopUp: unexpected case in switch. i=%d type=%d name=%s.\n", i, option[i].type, option[i].name);
1373 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
1374 table, TRUE, TRUE, 0);
1377 gtk_widget_show_all( dialog );
1379 /* hide OK/cancel buttons */
1380 if((option[i].min & 2)) {
1381 actionarea = gtk_dialog_get_action_area(GTK_DIALOG(dialog));
1382 gtk_widget_hide(actionarea);
1385 g_signal_connect (dialog, "response",
1386 G_CALLBACK (GenericPopUpCallback),
1387 (gpointer)(intptr_t) (dlgNr<<16 | i));
1388 g_signal_connect (dialog, "delete-event",
1389 G_CALLBACK (GenericPopDown),
1390 (gpointer)(intptr_t) dlgNr);
1395 Widget popup, layout, dialog=NULL, edit=NULL, form, last, b_ok, b_cancel, previousPane = NULL, textField = NULL, oldForm, oldLastRow, oldForeLast;
1397 int x, y, i, j, height=999, width=1, h, c, w, shrink=FALSE, stack = 0, box, chain;
1398 int win_x, win_y, maxWidth, maxTextWidth;
1400 char def[MSG_SIZ], *msg, engineDlg = (currentCps != NULL && dlgNr != BrowserDlg);
1401 static char pane[6] = "paneX";
1402 Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL, browse = NULL;
1403 Dimension bWidth = 50;
1405 if(dlgNr < PromoDlg && shellUp[dlgNr]) return 0; // already up
1406 if(dlgNr && dlgNr < PromoDlg && shells[dlgNr]) { // reusable, and used before (but popped down)
1407 XtPopup(shells[dlgNr], XtGrabNone);
1408 shellUp[dlgNr] = True;
1412 dialogOptions[dlgNr] = option; // make available to callback
1413 // post currentOption globally, so Spin and Combo callbacks can already use it
1414 // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
1415 currentOption = option;
1417 if(engineDlg) { // Settings popup for engine: format through heuristic
1418 int n = currentCps->nrOptions;
1419 if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
1420 height = n / width + 1;
1421 if(n && (currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton)) currentOption[n].min = SAME_ROW; // OK on same line
1422 currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
1425 XtSetArg(args[i], XtNresizable, True); i++;
1426 shells[BoardWindow] = shellWidget; parents[dlgNr] = parent;
1428 if(dlgNr == BoardWindow) popup = shellWidget; else
1429 popup = shells[dlgNr] =
1430 XtCreatePopupShell(title, !top || !appData.topLevel ? transientShellWidgetClass : topLevelShellWidgetClass,
1431 shells[parent], args, i);
1434 XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1435 layoutArgs, XtNumber(layoutArgs));
1436 if(!appData.monoMode && appData.dialogColor[0]) XtSetArg(args[0], XtNbackground, dialogColor);
1437 XtSetValues(layout, args, 1);
1439 for(c=0; c<width; c++) {
1442 XtCreateManagedWidget(pane, formWidgetClass, layout,
1443 formArgs, XtNumber(formArgs));
1445 XtSetArg(args[j], stack ? XtNfromVert : XtNfromHoriz, previousPane); j++;
1446 if(!appData.monoMode && appData.dialogColor[0]) XtSetArg(args[j], XtNbackground, dialogColor), j++;
1447 XtSetValues(form, args, j);
1448 lastrow = forelast = NULL;
1449 previousPane = form;
1451 last = widest = NULL; anchor = lastrow;
1452 for(h=0; h<height || c == width-1; h++) {
1454 if(option[i].type == EndMark) break;
1455 if(option[i].type == -1) continue;
1458 switch(option[i].type) {
1460 snprintf(def, MSG_SIZ, "%.2f", *(float*)option[i].target);
1461 option[i].value = *(float*)option[i].target;
1464 if(!engineDlg) option[i].value = *(int*)option[i].target;
1465 snprintf(def, MSG_SIZ, "%d", option[i].value);
1470 if(option[i].name[0]) { // prefixed by label with option name
1471 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1472 0 /* w */, textHeight /* h */, 0xC0 /* chain to left edge */);
1473 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1474 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1475 texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1476 } else texts[h] = dialog = NULL; // kludge to position from left margin
1477 w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1478 if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1479 j = SetPositionAndSize(args, dialog, last, 1 /* border */,
1480 w /* w */, option[i].type == TextBox ? option[i].value : 0 /* h */, 0x91 /* chain full width */);
1481 if(option[i].type == TextBox) { // decorations for multi-line text-edits
1482 if(option[i].min & T_VSCRL) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways); j++; }
1483 if(option[i].min & T_HSCRL) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways); j++; }
1484 if(option[i].min & T_FILL) { XtSetArg(args[j], XtNautoFill, True); j++; }
1485 if(option[i].min & T_WRAP) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1486 if(option[i].min & T_TOP) { XtSetArg(args[j], XtNtop, XtChainTop); j++;
1487 if(!option[i].value) { XtSetArg(args[j], XtNbottom, XtChainTop); j++;
1488 XtSetValues(dialog, args+j-2, 2);
1491 } else shrink = TRUE;
1492 XtSetArg(args[j], XtNeditType, XawtextEdit); j++;
1493 XtSetArg(args[j], XtNuseStringInPlace, False); j++;
1494 XtSetArg(args[j], XtNdisplayCaret, False); j++;
1495 XtSetArg(args[j], XtNresizable, True); j++;
1496 XtSetArg(args[j], XtNinsertPosition, 9999); j++;
1497 XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def :
1498 engineDlg ? option[i].textValue : *(char**)option[i].target); j++;
1500 option[i].handle = (void*)
1501 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1502 XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup); // gets focus on mouse click
1503 if(option[i].min == 0 || option[i].type != TextBox)
1504 XtOverrideTranslations(last, XtParseTranslationTable(oneLiner)); // standard handler for <Enter> and <Tab>
1506 if(option[i].type == TextBox || option[i].type == Fractional) break;
1508 // add increment and decrement controls for spin
1509 if(option[i].type == FileName || option[i].type == PathName) {
1510 msg = _("browse"); w = 0; // automatically scale to width of text
1511 j = textHeight ? textHeight : 0;
1513 w = 20; msg = "+"; j = textHeight/2; // spin button
1515 j = SetPositionAndSize(args, last, edit, 3 /* border */,
1516 w /* w */, j /* h */, 0x31 /* chain to right edge */);
1517 edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1518 XtAddCallback(edit, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1519 if(w == 0) browse = edit;
1521 if(option[i].type != Spin) break;
1523 j = SetPositionAndSize(args, last, edit, 3 /* border */,
1524 20 /* w */, textHeight/2 /* h */, 0x31 /* chain to right edge */);
1525 XtSetArg(args[j], XtNvertDistance, -1); j++;
1526 last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1527 XtAddCallback(last, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1530 if(!engineDlg) option[i].value = *(Boolean*)option[i].target; // where checkbox callback uses it
1531 j = SetPositionAndSize(args, last, lastrow, 1 /* border */,
1532 textHeight/2 /* w */, textHeight/2 /* h */, 0xC0 /* chain both to left edge */);
1533 XtSetArg(args[j], XtNvertDistance, (textHeight+2)/4 + 3); j++;
1534 XtSetArg(args[j], XtNstate, option[i].value); j++;
1536 option[i].handle = (void*)
1537 (last = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1538 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1539 option[i].max /* w */, textHeight /* h */, 0xC1 /* chain */);
1540 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1541 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1542 last = XtCreateManagedWidget("label", commandWidgetClass, form, args, j);
1543 // make clicking the text toggle checkbox
1544 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1545 shrink = TRUE; // following buttons must get text height
1548 msg = option[i].name;
1550 chain = option[i].min;
1551 if(chain & SAME_ROW) forelast = lastrow; else shrink = FALSE;
1552 j = SetPositionAndSize(args, last, lastrow, (chain & 2) != 0 /* border */,
1553 option[i].max /* w */, shrink ? textHeight : 0 /* h */, chain | 2 /* chain */);
1555 if(option[i].choice) XtSetArg(args[j], XtNfontSet, *(XFontSet*)option[i].choice), j++;
1557 if(option[i].choice) XtSetArg(args[j], XtNfont, (XFontStruct*)option[i].choice), j++;
1559 XtSetArg(args[j], XtNresizable, False); j++;
1560 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1561 XtSetArg(args[j], XtNlabel, _(msg)); j++;
1562 option[i].handle = (void*) (last = XtCreateManagedWidget("label", labelWidgetClass, form, args, j));
1563 if(option[i].target) // allow user to specify event handler for button presses
1564 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1568 if(option[i].min & SAME_ROW) {
1569 chain = 0x31; // 0011.0001 = both left and right side to right edge
1571 } else chain = 0, shrink = FALSE;
1572 j = SetPositionAndSize(args, last, lastrow, 3 /* border */,
1573 option[i].max /* w */, shrink ? textHeight : 0 /* h */, option[i].min & 0xE | chain /* chain */);
1574 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1575 if(option[i].textValue) { // special for buttons of New Variant dialog
1576 XtSetArg(args[j], XtNsensitive, appData.noChessProgram || option[i].value < 0
1577 || strstr(first.variants, VariantName(option[i].value))); j++;
1578 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1580 option[i].handle = (void*)
1581 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1582 if(option[i].choice && ((char*)option[i].choice)[0] == '#' && !engineDlg) { // for the color picker default-reset
1583 SetColor( *(char**) option[i-1].target, &option[i]);
1584 XtAddEventHandler(option[i-1].handle, KeyReleaseMask, False, ColorChanged, (XtPointer)(intptr_t) i-1);
1586 XtAddCallback(last, XtNcallback, GenericCallback, (XtPointer)(intptr_t) i + (dlgNr<<16)); // invokes user callback
1587 if(option[i].textValue) SetColor( option[i].textValue, &option[i]); // for new-variant buttons
1590 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1591 0 /* w */, textHeight /* h */, 0xC0 /* chain both sides to left edge */);
1592 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1593 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1594 texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1596 if(option[i].min & COMBO_CALLBACK) msg = _(option[i].name); else {
1597 if(!engineDlg) SetCurrentComboSelection(option+i);
1598 msg=_(((char**)option[i].choice)[option[i].value]);
1601 j = SetPositionAndSize(args, dialog, last, (option[i].min & 2) == 0 /* border */,
1602 option[i].max && !engineDlg ? option[i].max : 100 /* w */,
1603 textHeight /* h */, 0x91 /* chain */); // same row as its label!
1604 XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name)); j++;
1605 XtSetArg(args[j], XtNlabel, msg); j++;
1607 option[i].handle = (void*)
1608 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1609 CreateComboPopup(last, option + i, i + 256*dlgNr, TRUE, -1);
1610 values[i] = option[i].value;
1613 // Listbox goes in viewport, as needed for game list
1614 if(option[i].min & SAME_ROW) forelast = lastrow;
1615 j = SetPositionAndSize(args, last, lastrow, 1 /* border */,
1616 option[i].max /* w */, option[i].value /* h */, option[i].min /* chain */);
1617 XtSetArg(args[j], XtNresizable, False); j++;
1618 XtSetArg(args[j], XtNallowVert, True); j++; // scoll direction
1620 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
1621 j = 0; // now list itself
1622 XtSetArg(args[j], XtNdefaultColumns, 1); j++;
1623 XtSetArg(args[j], XtNforceColumns, True); j++;
1624 XtSetArg(args[j], XtNverticalList, True); j++;
1625 option[i].handle = (void*)
1626 (edit = XtCreateManagedWidget("list", listWidgetClass, last, args, j));
1627 XawListChange(option[i].handle, option[i].target, 0, 0, True);
1628 XawListHighlight(option[i].handle, 0);
1629 scrollTranslations[25] = '0' + i;
1630 scrollTranslations[27] = 'A' + dlgNr;
1631 XtOverrideTranslations(edit, XtParseTranslationTable(scrollTranslations)); // for mouse-wheel
1634 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1635 option[i].max /* w */, option[i].value /* h */, option[i].min /* chain */);
1636 option[i].handle = (void*)
1637 (last = XtCreateManagedWidget("graph", widgetClass, form, args, j));
1638 XtAddEventHandler(last, ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, False,
1639 (XtEventHandler) GraphEventProc, &option[i]); // mandatory user-supplied expose handler
1640 if(option[i].min & SAME_ROW) last = forelast, forelast = lastrow;
1641 option[i].choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, option[i].max, option[i].value); // image buffer
1643 case PopUp: // note: used only after Graph, so 'last' refers to the Graph widget
1644 option[i].handle = (void*) CreateComboPopup(last, option + i, i + 256*dlgNr, TRUE, option[i].value);
1647 if(option[i].min & SAME_ROW) forelast = lastrow;
1648 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1649 0 /* w */, 0 /* h */, option[i].min /* chain */);
1650 XtSetArg(args[j], XtNorientation, XtorientHorizontal); j++;
1651 XtSetArg(args[j], XtNvSpace, 0); j++;
1652 option[box=i].handle = (void*)
1653 (last = XtCreateWidget("box", boxWidgetClass, form, args, j));
1654 oldForm = form; form = last; oldLastRow = lastrow; oldForeLast = forelast;
1655 lastrow = NULL; last = NULL;
1658 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1659 0 /* w */, 0 /* h */, 1 /* chain (always on same row) */);
1661 msg = _(option[i].name); // write name on the menu button
1662 XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name)); j++;
1663 XtSetArg(args[j], XtNlabel, msg); j++;
1664 option[i].handle = (void*)
1665 (last = XtCreateManagedWidget(option[i].name, menuButtonWidgetClass, form, args, j));
1666 option[i].textValue = (char*) CreateComboPopup(last, option + i, i + 256*dlgNr, FALSE, -1);
1669 XtManageChildren(&form, 1);
1670 SqueezeIntoBox(&option[box], i-box, option[box].max);
1671 if(option[i].target) ((ButtonCallback*)option[i].target)(box); // callback that can make sizing decisions
1672 last = form; lastrow = oldLastRow; form = oldForm; forelast = oldForeLast;
1677 stack = !(option[i].min & SAME_ROW);
1680 printf("GenericPopUp: unexpected case in switch.\n");
1685 // make an attempt to align all spins and textbox controls
1686 maxWidth = maxTextWidth = 0;
1687 if(browse != NULL) {
1689 XtSetArg(args[j], XtNwidth, &bWidth); j++;
1690 XtGetValues(browse, args, j);
1692 for(h=0; h<height || c == width-1; h++) {
1694 if(option[i].type == EndMark) break;
1695 if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1696 || option[i].type == PathName || option[i].type == FileName) {
1698 if(!texts[h]) continue;
1700 XtSetArg(args[j], XtNwidth, &w); j++;
1701 XtGetValues(texts[h], args, j);
1702 if(option[i].type == Spin) {
1703 if(w > maxWidth) maxWidth = w;
1706 if(w > maxTextWidth) maxTextWidth = w;
1707 if(!widest) widest = texts[h];
1711 if(maxTextWidth + 110 < maxWidth)
1712 maxTextWidth = maxWidth - 110;
1713 else maxWidth = maxTextWidth + 110;
1714 for(h=0; h<height || c == width-1; h++) {
1716 if(option[i].type == EndMark) break;
1717 if(!texts[h]) continue; // Note: texts[h] can be undefined (giving errors in valgrind), but then both if's below will be false.
1719 if(option[i].type == Spin) {
1720 XtSetArg(args[j], XtNwidth, maxWidth); j++;
1721 XtSetValues(texts[h], args, j);
1723 if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1724 XtSetArg(args[j], XtNwidth, maxTextWidth); j++;
1725 XtSetValues(texts[h], args, j);
1726 if(bWidth != 50 && (option[i].type == FileName || option[i].type == PathName)) {
1727 int tWidth = (option[i].max ? option[i].max : 205) - 5 - bWidth;
1729 XtSetArg(args[j], XtNwidth, tWidth); j++;
1730 XtSetValues(option[i].handle, args, j);
1736 if(option[i].min & SAME_ROW) { // even when OK suppressed this EndMark bit can request chaining of last row to bottom
1737 for(j=i-1; option[j+1].min & SAME_ROW; j--) {
1738 XtSetArg(args[0], XtNtop, XtChainBottom);
1739 XtSetArg(args[1], XtNbottom, XtChainBottom);
1740 XtSetValues(option[j].handle, args, 2);
1742 if((option[j].type == TextBox || option[j].type == ListBox) && option[j].name[0] == NULLCHAR) {
1743 Widget w = option[j].handle;
1744 if(option[j].type == ListBox) w = XtParent(w); // for listbox we must chain viewport
1745 XtSetArg(args[0], XtNbottom, XtChainBottom);
1746 XtSetValues(w, args, 1);
1749 } else shrink = FALSE, lastrow = last, last = widest ? widest : dialog;
1750 j = SetPositionAndSize(args, last, anchor ? anchor : lastrow, 3 /* border */,
1751 0 /* w */, shrink ? textHeight : 0 /* h */, 0x37 /* chain: right, bottom and use both neighbors */);
1753 if(!(option[i].min & NO_OK)) {
1754 option[i].handle = b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1755 XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer)(intptr_t) (30001 + (dlgNr<<16)));
1756 if(!(option[i].min & NO_CANCEL)) {
1757 XtSetArg(args[1], XtNfromHoriz, b_ok); // overwrites!
1758 b_cancel = XtCreateManagedWidget(_("cancel"), commandWidgetClass, form, args, j);
1759 XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer)(intptr_t) (30000 + (dlgNr<<16)));
1763 XtRealizeWidget(popup);
1764 if(dlgNr != BoardWindow) { // assign close button, and position w.r.t. pointer, if not main window
1765 XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1766 snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1767 XtAugmentTranslations(popup, XtParseTranslationTable(def));
1768 XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1769 &x, &y, &win_x, &win_y, &mask);
1771 XtSetArg(args[0], XtNx, x - 10);
1772 XtSetArg(args[1], XtNy, y - 30);
1773 XtSetValues(popup, args, 2);
1775 XtPopup(popup, modal ? XtGrabExclusive : XtGrabNone);
1776 shellUp[dlgNr]++; // count rather than flag
1778 if(textField) SetFocus(textField, popup, (XEvent*) NULL, False);
1779 if(dlgNr && wp[dlgNr] && wp[dlgNr]->width > 0) { // if persistent window-info available, reposition
1781 XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height)); j++;
1782 XtSetArg(args[j], XtNwidth, (Dimension) (wp[dlgNr]->width)); j++;
1783 XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x)); j++;
1784 XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y)); j++;
1785 XtSetValues(popup, args, j);
1789 return 1; // tells caller he must do initialization (e.g. add specific event handlers)
1792 /* function called when the data to Paste is ready */
1795 SendTextCB (Widget w, XtPointer client_data, Atom *selection,
1796 Atom *type, XtPointer value, unsigned long *len, int *format)
1798 char buf[MSG_SIZ], *p = (char*) textOptions[(int)(intptr_t) client_data].choice, *name = (char*) value, *q;
1799 if (value==NULL || *len==0) return; /* nothing selected, abort */
1801 strncpy(buf, p, MSG_SIZ);
1802 q = strstr(p, "$name");
1803 snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1813 char *p = (char*) textOptions[n].choice;
1814 if(strstr(p, "$name")) {
1815 XtGetSelectionValue(menuBarWidget,
1816 XA_PRIMARY, XA_STRING,
1817 /* (XtSelectionCallbackProc) */ SendTextCB,
1818 (XtPointer) (intptr_t) n, /* client_data passed to PastePositionCB */
1821 } else SendString(p);
1826 SetInsertPos (Option *opt, int pos)
1830 XtSetArg(args[0], XtNinsertPosition, pos);
1831 XtSetValues(opt->handle, args, 1);
1832 // SetFocus(opt->handle, shells[InputBoxDlg], NULL, False); // No idea why this does not work, and the following is needed:
1833 // XSetInputFocus(xDisplay, XtWindow(opt->handle), RevertToPointerRoot, CurrentTime);
1839 TypeInProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
1840 { // can be used as handler for any text edit in any dialog (from GenericPopUp, that is)
1841 int n = prms[0][0] - '0';
1842 Widget sh = XtParent(XtParent(XtParent(w))); // popup shell
1844 if(n<2) { // Enter or Esc typed from primed text widget: treat as if dialog OK or cancel button hit.
1845 int dlgNr; // figure out what the dialog number is by comparing shells (because we must pass it :( )
1846 for(dlgNr=0; dlgNr<NrOfDialogs; dlgNr++) if(shellUp[dlgNr] && shells[dlgNr] == sh)
1847 GenericCallback (w, (XtPointer)(intptr_t) (30000 + n + (dlgNr<<16)), NULL);
1853 HardSetFocus (Option *opt)
1856 XSetInputFocus(xDisplay, XtWindow(opt->handle), RevertToPointerRoot, CurrentTime);