2 * xoptions.c -- Move list window, part of X front end for XBoard
4 * Copyright 2000, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 <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 #include <X11/Xaw/Scrollbar.h>
71 #include <cairo/cairo.h>
72 #include <cairo/cairo-xlib.h>
82 # define _(s) gettext (s)
83 # define N_(s) gettext_noop (s)
89 // [HGM] the following code for makng menu popups was cloned from the FileNamePopUp routines
91 static Widget previous = NULL;
92 static Option *currentOption;
100 XtSetArg(args[0], XtNdisplayCaret, False);
101 XtSetValues(previous, args, 1);
107 SetFocus (Widget w, XtPointer data, XEvent *event, Boolean *b)
114 XtSetArg(args[0], XtNstring, &s);
115 XtGetValues(w, args, 1);
117 XtSetArg(args[0], XtNdisplayCaret, True);
118 if(!strchr(s, '\n') && strlen(s) < 80) XtSetArg(args[1], XtNinsertPosition, strlen(s)), j++;
119 XtSetValues(w, args, j);
120 XtSetKeyboardFocus((Widget) data, w);
127 XtSetKeyboardFocus(shellWidget, formWidget);
130 //--------------------------- Engine-specific options menu ----------------------------------
133 Option *dialogOptions[NrOfDialogs];
135 static Arg layoutArgs[] = {
136 { XtNborderWidth, 0 },
137 { XtNdefaultDistance, 0 },
140 static Arg formArgs[] = {
141 { XtNborderWidth, 0 },
142 { XtNresizable, (XtArgVal) True },
146 CursorAtEnd (Option *opt)
151 GetWidgetText (Option *opt, char **buf)
154 XtSetArg(arg, XtNstring, buf);
155 XtGetValues(opt->handle, &arg, 1);
159 SetWidgetText (Option *opt, char *buf, int n)
162 XtSetArg(arg, XtNstring, buf);
163 XtSetValues(opt->handle, &arg, 1);
164 if(n >= 0) SetFocus(opt->handle, shells[n], NULL, False);
168 GetWidgetState (Option *opt, int *state)
171 XtSetArg(arg, XtNstate, state);
172 XtGetValues(opt->handle, &arg, 1);
176 SetWidgetState (Option *opt, int state)
179 XtSetArg(arg, XtNstate, state);
180 XtSetValues(opt->handle, &arg, 1);
184 WidgetEcho (Option *opt, int state)
189 SetWidgetLabel (Option *opt, char *buf)
192 XtSetArg(arg, XtNlabel, (XtArgVal) buf);
193 XtSetValues(opt->handle, &arg, 1);
197 SetComboChoice (Option *opt, int n)
199 SetWidgetText(opt, opt->choice[n], MasterDlg);
203 SetDialogTitle (DialogClass dlg, char *title)
206 XtSetArg(args[0], XtNtitle, title);
207 XtSetValues(shells[dlg], args, 1);
211 LoadListBox (Option *opt, char *emptyText, int n1, int n2)
213 static char *dummyList[2];
214 dummyList[0] = emptyText; // empty listboxes tend to crash X, so display user-supplied warning string instead
215 XawListChange(opt->handle, *(char**)opt->target ? opt->target : dummyList, 0, 0, True);
216 //printf("listbox data = %x\n", opt->target);
220 ReadScroll (Option *opt, float *top, float *bottom)
221 { // retreives fractions of top and bottom of thumb
223 Widget w = XtParent(opt->handle); // viewport
224 Widget v = XtNameToWidget(w, "vertical");
227 if(!v) return FALSE; // no scroll bar
228 XtSetArg(args[j], XtNshown, &h); j++;
229 XtSetArg(args[j], XtNtopOfThumb, top); j++;
230 XtGetValues(v, args, j);
236 SetScroll (Option *opt, float f)
237 { // sets top of thumb to given fraction
238 static char *params[3] = { "", "Continuous", "Proportional" };
240 Widget w = XtParent(opt->handle); // viewport
241 Widget v = XtNameToWidget(w, "vertical");
242 if(!v) return; // no scroll bar
243 XtCallActionProc(v, "StartScroll", &event, params+1, 1);
244 XawScrollbarSetThumb(v, f, -1.0);
245 XtCallActionProc(v, "NotifyThumb", &event, params, 0);
246 // XtCallActionProc(v, "NotifyScroll", &event, params+2, 1);
247 XtCallActionProc(v, "EndScroll", &event, params, 0);
251 HighlightListBoxItem (Option *opt, int nr)
253 XawListHighlight(opt->handle, nr);
257 HighlightWithScroll (Option *opt, int sel, int max)
259 float top, bottom, f, g;
260 HighlightListBoxItem(opt, sel);
261 if(!ReadScroll(opt, &top, &bottom)) return; // no scroll bar
262 bottom = bottom*max - 1.f;
265 if(sel > (top + 3*bottom)/4) f = (sel - 0.75f*(bottom-top))/max; else
266 if(sel < (3*top + bottom)/4) f = (sel - 0.25f*(bottom-top))/max;
267 if(f < 0.f) f = 0.; if(f + 1.f/max > 1.f) f = 1. - 1./max;
268 if(f != g) SetScroll(opt, f);
272 SelectedListBoxItem (Option *opt)
274 XawListReturnStruct *rs;
275 rs = XawListShowCurrent(opt->handle);
276 return rs->list_index;
280 SetTextColor (char **cnames, int fg, int bg, int attr)
281 { // this is not possible in Xaw
285 AppendColorized (Option *opt, char *message, int count)
287 if(!opt->handle) return;
288 AppendText(opt, message);
292 Show (Option *opt, int hide)
298 return; // FIXME: it would be nice if the Chat window did have an ICS pane we could hide behind
299 //printf("Show(%d) %x\n", hide, opt->handle);
300 if(!opt->handle) return;
301 if(hide) { // make sure original size is saved
302 XtSetArg(args[j], XtNheight, &v); j++;
303 XtGetValues(opt->handle, args, j);
306 printf("h = %d\n",h);
308 XtSetArg(args[j], XtNheight, hide ? 1 : h); j++;
309 XtSetValues(opt->handle, args, j);
313 HighlightText (Option *opt, int start, int end, Boolean on)
316 XawTextSetSelection( opt->handle, start, end ); // for lack of a better method, use selection for highighting
318 XawTextSetSelection( opt->handle, 0, 0 );
322 FocusOnWidget (Option *opt, DialogClass dlg)
325 XtSetKeyboardFocus(shells[dlg], opt->handle);
329 SetIconName (DialogClass dlg, char *name)
333 XtSetArg(args[j], XtNiconName, (XtArgVal) name); j++;
334 // XtSetArg(args[j], XtNtitle, (XtArgVal) name); j++;
335 XtSetValues(shells[dlg], args, j);
339 LabelCallback (Widget ww, XtPointer client_data, XEvent *event, Boolean *b)
340 { // called on ButtonPress in label widgets with attached user handler (clocks!)
341 int s, data = (intptr_t) client_data;
342 Option *opt = dialogOptions[data >> 8] + (s = data & 255);
344 if(((XButtonEvent*)event)->button != Button1) s = -s;
345 ((ButtonCallback*) opt->target) (s);
349 CheckCallback (Widget ww, XtPointer client_data, XEvent *event, Boolean *b)
351 int s, data = (intptr_t) client_data;
352 Option *opt = dialogOptions[data >> 8] + (data & 255);
354 GetWidgetState(opt, &s);
355 SetWidgetState(opt, !s);
359 SpinCallback (Widget w, XtPointer client_data, XtPointer call_data)
363 char buf[MSG_SIZ], *p;
364 int j = 0; // Initialisation is necessary because the text value may be non-numeric causing the scanf conversion to fail
365 int data = (intptr_t) client_data;
366 Option *opt = dialogOptions[data >> 8] + (data & 255);
368 XtSetArg(args[0], XtNlabel, &name);
369 XtGetValues(w, args, 1);
371 GetWidgetText(opt, &val);
372 sscanf(val, "%d", &j);
373 if (strcmp(name, _("browse")) == 0) {
375 for(r = ""; *q; q++) if(*q == '.') r = q; else if(*q == '/') r = ""; // last dot after last slash
376 if(!strcmp(r, "") && !currentCps && opt->type == FileName && opt->textValue)
378 Browse(data>>8, opt->name, NULL, r, opt->type == PathName, "", &p, (FILE**) opt);
381 if (strcmp(name, "+") == 0) {
382 if(++j > opt->max) return;
384 if (strcmp(name, "-") == 0) {
385 if(--j < opt->min) return;
387 snprintf(buf, MSG_SIZ, "%d", j);
388 SetWidgetText(opt, buf, shellUp[TransientDlg] ? TransientDlg : MasterDlg);
392 ComboSelect (Widget w, caddr_t addr, caddr_t index) // callback for all combo items
395 Option *opt = dialogOptions[((intptr_t)addr)>>24]; // applicable option list
396 int i = ((intptr_t)addr)>>16 & 255; // option number
397 int j = 0xFFFF & (intptr_t) addr;
399 values[i] = j; // store selected value in Option struct, for retrieval at OK
401 if(opt[i].type == Graph || opt[i].min & COMBO_CALLBACK && (!currentCps || shellUp[BrowserDlg])) {
402 ((ButtonCallback*) opt[i].target)(i);
406 if(opt[i].min & NO_GETTEXT)
407 XtSetArg(args[0], XtNlabel, ((char**)opt[i].choice)[j]);
409 XtSetArg(args[0], XtNlabel, _(((char**)opt[i].choice)[j]));
411 XtSetValues(opt[i].handle, args, 1);
415 CreateMenuItem (Widget menu, char *msg, XtCallbackProc CB, int n)
420 XtSetArg(args[j], XtNleftMargin, 20); j++;
421 XtSetArg(args[j], XtNrightMargin, 20); j++;
422 if(!strcmp(msg, "----")) { XtCreateManagedWidget(msg, smeLineObjectClass, menu, args, j); return NULL; }
423 XtSetArg(args[j], XtNlabel, msg);
424 entry = XtCreateManagedWidget("item", smeBSBObjectClass, menu, args, j+1);
425 XtAddCallback(entry, XtNcallback, CB, (caddr_t)(intptr_t) n);
430 format_accel (char *input)
437 if( strstr(input, "<Ctrl>") )
439 output = realloc(output, strlen(output) + strlen(_("Ctrl"))+2);
440 strncat(output, _("Ctrl"), strlen(_("Ctrl")) +1);
441 strncat(output, "+", 1);
443 if( strstr(input, "<Alt>") )
445 output = realloc(output, strlen(output) + strlen(_("Alt"))+2);
446 strncat(output, _("Alt"), strlen(_("Alt")) +1);
447 strncat(output, "+", 1);
449 if( strstr(input, "<Shift>") )
451 output = realloc(output, strlen(output) + strlen(_("Shift"))+2);
452 strncat(output, _("Shift"), strlen(_("Shift")) +1);
453 strncat(output, "+", 1);
456 test = strrchr(input, '>');
460 key = strdup(++test); // remove ">"
461 if(strlen(key) == 1) key[0] = ToUpper(key[0]);
463 output = realloc(output, strlen(output) + strlen(_(key))+2);
464 strncat(output, _(key), strlen(_(key)) +1);
476 XTextExtents(messageFontStruct, s, strlen(s), &dummy, &dummy, &dummy, &overall);
477 return overall.width;
480 while(*s) switch(*s++) {
481 case '.': tot += 0.45; break;
482 case ' ': tot += 0.55; break;
483 case 'i': tot += 0.45; break;
484 case 'l': tot += 0.45; break;
485 case 'j': tot += 0.45; break;
486 case 'f': tot += 0.45; break;
487 case 'I': tot += 0.45; break;
488 case 't': tot += 0.45; break;
489 case 'k': tot += 0.83; break;
490 case 's': tot += 0.83; break;
491 case 'x': tot += 0.83; break;
492 case 'z': tot += 0.83; break;
493 case 'r': tot += 0.55; break;
494 case 'w': tot += 1.3; break;
495 case 'm': tot += 1.3; break;
496 case 'A': tot += 1.3; break;
497 case 'C': tot += 1.3; break;
498 case 'D': tot += 1.3; break;
499 case 'G': tot += 1.3; break;
500 case 'H': tot += 1.3; break;
501 case 'N': tot += 1.3; break;
502 case 'V': tot += 1.3; break;
503 case 'X': tot += 1.3; break;
504 case 'Y': tot += 1.3; break;
505 case 'Z': tot += 1.3; break;
506 case 'M': tot += 1.6; break;
507 case 'W': tot += 1.6; break;
508 case 'B': tot += 1.1; break;
509 case 'E': tot += 1.1; break;
510 case 'F': tot += 1.1; break;
511 case 'K': tot += 1.1; break;
512 case 'P': tot += 1.1; break;
513 case 'R': tot += 1.1; break;
514 case 'S': tot += 1.1; break;
515 case 'O': tot += 1.4; break;
516 case 'Q': tot += 1.4; break;
524 CreateComboPopup (Widget parent, Option *opt, int n, int fromList, int def)
525 { // fromList determines if the item texts are taken from a list of strings, or from a menu table
529 MenuItem *mb = (MenuItem *) opt->choice;
530 char **list = (char **) opt->choice;
531 int maxlength=0, menuLen[1000];
534 if(list[0] == NULL) return NULL; // avoid empty menus, as they cause crash
535 menu = XtCreatePopupShell(opt->name, simpleMenuWidgetClass, parent, NULL, 0);
538 for (i=0; mb[i].string; i++) if(mb[i].accel) {
539 int len = pixlen(_(mb[i].string));
541 if (maxlength < len )
547 char *msg = fromList ? list[i] : mb[i].string;
552 if(!fromList && mb[i].accel)
554 char *menuname = opt->min & NO_GETTEXT ? msg : _(msg);
555 char *accel = format_accel(mb[i].accel);
557 // int fill = maxlength - strlen(menuname) +2+strlen(accel);
558 int fill = (maxlength - menuLen[i] + 3)*1.8;
560 len = strlen(menuname)+fill+strlen(accel)+1;
563 snprintf(label,len,"%s%*s%s",menuname,fill," ",accel);
567 label = strdup(opt->min & NO_GETTEXT ? msg : _(msg));
569 entry = CreateMenuItem(menu, label, (XtCallbackProc) ComboSelect, (n<<16)+i);
570 if(!fromList) mb[i].handle = (void*) entry; // save item ID, for enabling / checkmarking
572 XtSetArg(arg, XtNpopupOnEntry, entry);
573 XtSetValues(menu, &arg, 1);
580 char moveTypeInTranslations[] =
581 "<Key>Return: TypeInProc(1) \n"
582 "<Key>Escape: TypeInProc(0) \n";
583 extern char filterTranslations[];
584 extern char gameListTranslations[];
585 extern char memoTranslations[];
588 char *translationTable[] = { // beware: order is essential!
589 historyTranslations, commentTranslations, moveTypeInTranslations, ICSInputTranslations,
590 filterTranslations, gameListTranslations, memoTranslations
594 AddHandler (Option *opt, DialogClass dlg, int nr)
596 XtOverrideTranslations(opt->handle, XtParseTranslationTable(translationTable[nr]));
599 //----------------------------Generic dialog --------------------------------------------
601 // cloned from Engine Settings dialog (and later merged with it)
603 Widget shells[NrOfDialogs];
604 DialogClass parents[NrOfDialogs];
605 WindowPlacement *wp[NrOfDialogs] = { // Beware! Order must correspond to DialogClass enum
606 NULL, &wpComment, &wpTags, NULL, NULL, NULL, NULL, &wpMoveHistory, &wpGameList, &wpEngineOutput, &wpEvalGraph,
607 NULL, NULL, NULL, NULL, /*&wpMain*/ NULL
611 DialogExists (DialogClass n)
612 { // accessor for use in back-end
613 return shells[n] != NULL;
617 RaiseWindow (DialogClass dlg)
620 Window root = RootWindow(xDisplay, DefaultScreen(xDisplay));
621 Atom atom = XInternAtom (xDisplay, "_NET_ACTIVE_WINDOW", False);
623 xev.xclient.type = ClientMessage;
624 xev.xclient.serial = 0;
625 xev.xclient.send_event = True;
626 xev.xclient.display = xDisplay;
627 xev.xclient.window = XtWindow(shells[dlg]);
628 xev.xclient.message_type = atom;
629 xev.xclient.format = 32;
630 xev.xclient.data.l[0] = 1;
631 xev.xclient.data.l[1] = CurrentTime;
633 XSendEvent (xDisplay,
635 SubstructureRedirectMask | SubstructureNotifyMask,
639 XSync(xDisplay, False);
643 PopDown (DialogClass n)
644 { // pops down any dialog created by GenericPopUp (or returns False if it wasn't up), unmarks any associated marked menu
647 Dimension windowH, windowW; Position windowX, windowY;
648 if (!shellUp[n] || !shells[n]) return 0;
649 if(n && wp[n]) { // remember position
651 XtSetArg(args[j], XtNx, &windowX); j++;
652 XtSetArg(args[j], XtNy, &windowY); j++;
653 XtSetArg(args[j], XtNheight, &windowH); j++;
654 XtSetArg(args[j], XtNwidth, &windowW); j++;
655 XtGetValues(shells[n], args, j);
658 wp[n]->width = windowW;
659 wp[n]->height = windowH;
662 XtPopdown(shells[n]);
663 shellUp[n]--; // count rather than clear
664 if(n == 0 || n >= PromoDlg) XtDestroyWidget(shells[n]), shells[n] = NULL;
666 MarkMenuItem(marked[n], False);
669 if(!n && n != BrowserDlg) currentCps = NULL; // if an Engine Settings dialog was up, we must be popping it down now
670 currentOption = dialogOptions[TransientDlg]; // just in case a transient dialog was up (to allow its check and combo callbacks to work)
671 RaiseWindow(parents[n]);
672 if(parents[n] == BoardWindow) XtSetKeyboardFocus(shellWidget, formWidget);
677 GenericPopDown (Widget w, XEvent *event, String *prms, Cardinal *nprms)
678 { // to cause popdown through a translation (Delete Window button!)
679 int dlg = atoi(prms[0]);
680 Widget sh = shells[dlg];
681 if(shellUp[BrowserDlg] && dlg != BrowserDlg || dialogError || dlg == MasterDlg && shellUp[TransientDlg])
682 return; // prevent closing dialog when it has an open file-browse or transient daughter
685 shells[dlg] = sh; // restore
689 AppendText (Option *opt, char *s)
694 GetWidgetText(opt, &v);
696 t.ptr = s; t.firstPos = 0; t.length = strlen(s); t.format = XawFmt8Bit;
697 XawTextReplace(opt->handle, len, len, &t);
702 SetColor (char *colorName, Option *box)
703 { // sets the color of a widget
707 if (!appData.monoMode) {
708 vFrom.addr = (caddr_t) colorName;
709 vFrom.size = strlen(colorName);
710 XtConvert(shellWidget, XtRString, &vFrom, XtRPixel, &vTo);
711 if (vTo.addr == NULL) {
712 buttonColor = (Pixel) -1;
714 buttonColor = *(Pixel *) vTo.addr;
716 } else buttonColor = timerBackgroundPixel;
717 XtSetArg(args[0], XtNbackground, buttonColor);;
718 XtSetValues(box->handle, args, 1);
722 ColorChanged (Widget w, XtPointer data, XEvent *event, Boolean *b)
723 { // for detecting a typed change in color
725 if ( (XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) && *buf == '\r' )
726 RefreshColor((int)(intptr_t) data, 0);
730 GraphEventProc(Widget widget, caddr_t client_data, XEvent *event)
731 { // handle expose and mouse events on Graph widget
734 int j, button=10, f=1, sizing=0;
735 Option *opt, *graph = (Option *) client_data;
736 PointerCallback *userHandler = graph->target;
738 if (!XtIsRealized(widget)) return;
740 switch(event->type) {
741 case Expose: // make handling of expose events generic, just copying from memory buffer (->choice) to display (->textValue)
742 /* Get window size */
744 XtSetArg(args[j], XtNwidth, &w); j++;
745 XtSetArg(args[j], XtNheight, &h); j++;
746 XtGetValues(widget, args, j);
748 if(w < graph->max || w > graph->max + 1 || h != graph->value) { // use width fudge of 1 pixel
749 if(((XExposeEvent*)event)->count >= 0) { // suppress sizing on expose for ordered redraw in response to sizing.
751 graph->max = w; graph->value = h; // note: old values are kept if we we don't exceed width fudge
753 } else w = graph->max;
755 if(sizing && ((XExposeEvent*)event)->count > 0) { graph->max = 0; return; } // don't bother if further exposure is pending during resize
756 if(!graph->textValue || sizing) { // create surfaces of new size for display widget
757 if(graph->textValue) cairo_surface_destroy((cairo_surface_t *)graph->textValue);
758 graph->textValue = (char*) cairo_xlib_surface_create(xDisplay, XtWindow(widget), DefaultVisual(xDisplay, 0), w, h);
760 if(sizing) { // the memory buffer was already created in GenericPopup(),
761 // to give drawing routines opportunity to use it before first expose event
762 // (which are only processed when main gets to the event loop, so after all init!)
763 // so only change when size is no longer good
765 if(graph->choice) cairo_surface_destroy((cairo_surface_t *) graph->choice);
766 graph->choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
767 // paint white, to prevent weirdness when people maximize window and drag pieces over space next to board
768 cr = cairo_create ((cairo_surface_t *) graph->choice);
769 cairo_rectangle (cr, 0, 0, w, h);
770 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
775 w = ((XExposeEvent*)event)->width;
776 if(((XExposeEvent*)event)->x + w > graph->max) w--; // cut off fudge pixel
777 if(w) ExposeRedraw(graph, ((XExposeEvent*)event)->x, ((XExposeEvent*)event)->y, w, ((XExposeEvent*)event)->height);
781 w = ((XButtonEvent*)event)->x; h = ((XButtonEvent*)event)->y;
784 f = -1; // release indicated by negative button numbers
786 w = ((XButtonEvent*)event)->x; h = ((XButtonEvent*)event)->y;
787 switch(((XButtonEvent*)event)->button) {
788 case Button1: button = 1; break;
789 case Button2: button = 2; break;
790 case Button3: button = 3; break;
791 case Button4: button = 4; break;
792 case Button5: button = 5; break;
796 opt = userHandler(button, w, h);
797 if(opt) { // user callback specifies a context menu; pop it up
798 XUngrabPointer(xDisplay, CurrentTime);
799 XtCallActionProc(widget, "XawPositionSimpleMenu", event, &(opt->name), 1);
800 XtPopupSpringLoaded(opt->handle);
802 XSync(xDisplay, False);
806 GraphExpose (Option *opt, int x, int y, int w, int h)
809 if(!opt->handle) return;
810 e.x = x; e.y = y; e.width = w; e.height = h; e.count = -1; e.type = Expose; // count = -1: kludge to suppress sizing
811 GraphEventProc(opt->handle, (caddr_t) opt, (XEvent *) &e); // fake expose event
815 GenericCallback (Widget w, XtPointer client_data, XtPointer call_data)
816 { // all Buttons in a dialog (including OK, cancel) invoke this
820 int data = (intptr_t) client_data;
822 Widget sh = XtParent(XtParent(XtParent(w))), oldSh;
824 currentOption = dialogOptions[dlg=data>>16]; data &= 0xFFFF;
825 oldSh = shells[dlg]; shells[dlg] = sh; // bow to reality
826 if (data == 30000) { // cancel
829 if (data == 30001) { // save buttons imply OK
830 if(GenericReadout(currentOption, -1)) PopDown(dlg); // calls OK-proc after full readout, but no popdown if it returns false
833 if(currentCps && dlg != BrowserDlg) {
834 XtSetArg(args[0], XtNlabel, &name);
835 XtGetValues(w, args, 1);
836 if(currentOption[data].type == SaveButton) GenericReadout(currentOption, -1);
837 snprintf(buf, MSG_SIZ, "option %s\n", name);
838 SendToProgram(buf, currentCps);
839 } else ((ButtonCallback*) currentOption[data].target)(data);
841 shells[dlg] = oldSh; // in case of multiple instances, restore previous (as this one could be popped down now)
845 TabProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
846 { // for transfering focus to the next text-edit
848 for(opt = currentOption; opt->type != EndMark; opt++) {
849 if(opt->handle == w) {
851 if(opt->type == EndMark) opt = currentOption; // wrap
852 if(opt->handle == w) return; // full circle
853 if(opt->type == TextBox || opt->type == Spin || opt->type == Fractional || opt->type == FileName || opt->type == PathName) {
854 SetFocus(opt->handle, XtParent(XtParent(XtParent(w))), NULL, 0);
863 WheelProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
864 { // for scrolling a widget seen through a viewport with the mouse wheel (ListBox!)
865 int j=0, n = atoi(prms[0]);
866 static char *params[3] = { "", "Continuous", "Proportional" };
870 if(!n) { // transient dialogs also use this for list-selection callback
872 Option *opt=dialogOptions[prms[2][0]-'A'] + n;
873 if(opt->textValue) ((ListBoxCallback*) opt->textValue)(n, SelectedListBoxItem(opt));
876 v = XtNameToWidget(XtParent(w), "vertical");
878 XtSetArg(args[j], XtNshown, &h); j++;
879 XtSetArg(args[j], XtNtopOfThumb, &top); j++;
880 XtGetValues(v, args, j);
881 top += 0.1f*h*n; if(top < 0.f) top = 0.;
882 XtCallActionProc(v, "StartScroll", event, params+1, 1);
883 XawScrollbarSetThumb(v, top, -1.0);
884 XtCallActionProc(v, "NotifyThumb", event, params, 0);
885 // XtCallActionProc(w, "NotifyScroll", event, params+2, 1);
886 XtCallActionProc(v, "EndScroll", event, params, 0);
889 static char *oneLiner =
890 "<Key>Return: redraw-display() \n \
891 <Key>Tab: TabProc() \n ";
892 static char scrollTranslations[] =
893 "<Btn1Up>(2): WheelProc(0 0 A) \n \
894 <Btn4Down>: WheelProc(-1) \n \
895 <Btn5Down>: WheelProc(1) \n ";
898 SqueezeIntoBox (Option *opt, int nr, int width)
899 { // size buttons in bar to fit, clipping button names where necessary
901 Dimension widths[20], oldWidths[20];
903 for(i=1; i<nr; i++) {
904 XtSetArg(arg, XtNwidth, &widths[i]);
905 XtGetValues(opt[i].handle, &arg, 1);
906 wtot += oldWidths[i] = widths[i];
909 if(width <= 0) return;
910 while(wtot > width) {
912 for(i=1; i<nr; i++) if(widths[i] > wmax) wmax = widths[imax=i];
916 for(i=1; i<nr; i++) if(widths[i] != oldWidths[i]) {
917 XtSetArg(arg, XtNwidth, widths[i]);
918 XtSetValues(opt[i].handle, &arg, 1);
924 SetPositionAndSize (Arg *args, Widget leftNeigbor, Widget topNeigbor, int b, int w, int h, int chaining)
925 { // sizing and positioning most widgets have in common
927 // first position the widget w.r.t. earlier ones
928 if(chaining & 1) { // same row: position w.r.t. last (on current row) and lastrow
929 XtSetArg(args[j], XtNfromVert, topNeigbor); j++;
930 XtSetArg(args[j], XtNfromHoriz, leftNeigbor); j++;
931 } else // otherwise it goes at left margin (which is default), below the previous element
932 XtSetArg(args[j], XtNfromVert, leftNeigbor), j++;
933 // arrange chaining ('2'-bit indicates top and bottom chain the same)
934 if((chaining & 14) == 6) XtSetArg(args[j], XtNtop, XtChainBottom), j++;
935 if((chaining & 14) == 10) XtSetArg(args[j], XtNbottom, XtChainTop ), j++;
936 if(chaining & 4) XtSetArg(args[j], XtNbottom, XtChainBottom ), j++;
937 if(chaining & 8) XtSetArg(args[j], XtNtop, XtChainTop), j++;
938 if(chaining & 0x10) XtSetArg(args[j], XtNright, XtChainRight), j++;
939 if(chaining & 0x20) XtSetArg(args[j], XtNleft, XtChainRight), j++;
940 if(chaining & 0x40) XtSetArg(args[j], XtNright, XtChainLeft ), j++;
941 if(chaining & 0x80) XtSetArg(args[j], XtNleft, XtChainLeft ), j++;
942 // set size (if given)
943 if(w) XtSetArg(args[j], XtNwidth, w), j++;
944 if(h) XtSetArg(args[j], XtNheight, h), j++;
946 if(!appData.monoMode) {
947 if(!b && appData.dialogColor[0]) XtSetArg(args[j], XtNbackground, dialogColor), j++;
948 if(b == 3 && appData.buttonColor[0]) XtSetArg(args[j], XtNbackground, buttonColor), j++;
952 XtSetArg(args[j], XtNborderWidth, b); j++;
957 GenericPopUp (Option *option, char *title, DialogClass dlgNr, DialogClass parent, int modal, int top)
960 Widget popup, layout, dialog=NULL, edit=NULL, form, last, b_ok, b_cancel, previousPane = NULL, textField = NULL, oldForm, oldLastRow, oldForeLast;
962 int x, y, i, j, height=999, width=1, h, c, w, shrink=FALSE, stack = 0, box, chain;
963 int win_x, win_y, maxWidth, maxTextWidth;
965 char def[MSG_SIZ], *msg, engineDlg = (currentCps != NULL && dlgNr != BrowserDlg);
966 static char pane[6] = "paneX";
967 Widget texts[100], forelast = NULL, anchor, widest, lastrow = NULL, browse = NULL;
968 Dimension bWidth = 50;
970 if(dlgNr < PromoDlg && shellUp[dlgNr]) return 0; // already up
971 if(dlgNr && dlgNr < PromoDlg && shells[dlgNr]) { // reusable, and used before (but popped down)
972 XtPopup(shells[dlgNr], XtGrabNone);
973 shellUp[dlgNr] = True;
976 if(dlgNr == TransientDlg && parent == BoardWindow && shellUp[MasterDlg]) parent = MasterDlg; // MasterDlg can always take role of main window
978 dialogOptions[dlgNr] = option; // make available to callback
979 // post currentOption globally, so Spin and Combo callbacks can already use it
980 // WARNING: this kludge does not work for persistent dialogs, so that these cannot have spin or combo controls!
981 currentOption = option;
983 if(engineDlg) { // Settings popup for engine: format through heuristic
984 int n = currentCps->nrOptions;
985 if(n > 50) width = 4; else if(n>24) width = 2; else width = 1;
986 height = n / width + 1;
987 if(n && (currentOption[n-1].type == Button || currentOption[n-1].type == SaveButton)) currentOption[n].min = SAME_ROW; // OK on same line
988 currentOption[n].type = EndMark; currentOption[n].target = NULL; // delimit list by callback-less end mark
991 XtSetArg(args[i], XtNresizable, True); i++;
992 shells[BoardWindow] = shellWidget; parents[dlgNr] = parent;
994 if(dlgNr == BoardWindow) popup = shellWidget; else
995 popup = shells[dlgNr] =
996 XtCreatePopupShell(title, !top || !appData.topLevel ? transientShellWidgetClass : topLevelShellWidgetClass,
997 shells[parent], args, i);
1000 XtCreateManagedWidget(layoutName, formWidgetClass, popup,
1001 layoutArgs, XtNumber(layoutArgs));
1002 if(!appData.monoMode && appData.dialogColor[0]) XtSetArg(args[0], XtNbackground, dialogColor);
1003 XtSetValues(layout, args, 1);
1005 for(c=0; c<width; c++) {
1008 XtCreateManagedWidget(pane, formWidgetClass, layout,
1009 formArgs, XtNumber(formArgs));
1011 XtSetArg(args[j], stack ? XtNfromVert : XtNfromHoriz, previousPane); j++;
1012 if(!appData.monoMode && appData.dialogColor[0]) XtSetArg(args[j], XtNbackground, dialogColor), j++;
1013 XtSetValues(form, args, j);
1014 lastrow = forelast = NULL;
1015 previousPane = form;
1017 last = widest = NULL; anchor = lastrow;
1018 for(h=0; h<height || c == width-1; h++) {
1020 if(option[i].type == EndMark) break;
1021 if(option[i].type == Skip) continue;
1024 switch(option[i].type) {
1026 snprintf(def, MSG_SIZ, "%.2f", *(float*)option[i].target);
1027 option[i].value = *(float*)option[i].target;
1030 if(!engineDlg) option[i].value = *(int*)option[i].target;
1031 snprintf(def, MSG_SIZ, "%d", option[i].value);
1036 if(option[i].name[0]) { // prefixed by label with option name
1037 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1038 0 /* w */, textHeight /* h */, 0xC0 /* chain to left edge */);
1039 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1040 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1041 texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1042 } else texts[h] = dialog = NULL; // kludge to position from left margin
1043 w = option[i].type == Spin || option[i].type == Fractional ? 70 : option[i].max ? option[i].max : 205;
1044 if(option[i].type == FileName || option[i].type == PathName) w -= 55;
1045 if(squareSize > 33) w += (squareSize - 33)/2;
1046 j = SetPositionAndSize(args, dialog, last, 1 /* border */,
1047 w /* w */, option[i].type == TextBox ? option[i].value : 0 /* h */, 0x91 /* chain full width */);
1048 if(option[i].type == TextBox) { // decorations for multi-line text-edits
1049 if(option[i].min & T_VSCRL) { XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways); j++; }
1050 if(option[i].min & T_HSCRL) { XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollAlways); j++; }
1051 if(option[i].min & T_FILL) { XtSetArg(args[j], XtNautoFill, True); j++; }
1052 if(option[i].min & T_WRAP) { XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++; }
1053 if(option[i].min & T_TOP) { XtSetArg(args[j], XtNtop, XtChainTop); j++;
1054 if(!option[i].value) { XtSetArg(args[j], XtNbottom, XtChainTop); j++;
1055 XtSetValues(dialog, args+j-2, 2);
1058 } else shrink = TRUE;
1059 XtSetArg(args[j], XtNeditType, XawtextEdit); j++;
1060 XtSetArg(args[j], XtNuseStringInPlace, False); j++;
1061 XtSetArg(args[j], XtNdisplayCaret, False); j++;
1062 XtSetArg(args[j], XtNresizable, True); j++;
1063 XtSetArg(args[j], XtNinsertPosition, 9999); j++;
1064 XtSetArg(args[j], XtNstring, option[i].type==Spin || option[i].type==Fractional ? def :
1065 engineDlg ? option[i].textValue : *(char**)option[i].target); j++;
1067 option[i].handle = (void*)
1068 (textField = last = XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j));
1069 XtAddEventHandler(last, ButtonPressMask, False, SetFocus, (XtPointer) popup); // gets focus on mouse click
1070 if(option[i].min == 0 || option[i].type != TextBox)
1071 XtOverrideTranslations(last, XtParseTranslationTable(oneLiner)); // standard handler for <Enter> and <Tab>
1073 if(option[i].type == TextBox || option[i].type == Fractional) break;
1075 // add increment and decrement controls for spin
1076 if(option[i].type == FileName || option[i].type == PathName) {
1077 msg = _("browse"); w = 0; // automatically scale to width of text
1078 j = textHeight ? textHeight : 0;
1080 w = 20; msg = "+"; j = textHeight/2; // spin button
1082 j = SetPositionAndSize(args, last, edit, 3 /* border */,
1083 w /* w */, j /* h */, 0x31 /* chain to right edge */);
1084 edit = XtCreateManagedWidget(msg, commandWidgetClass, form, args, j);
1085 XtAddCallback(edit, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1086 if(w == 0) browse = edit;
1088 if(option[i].type != Spin) break;
1090 j = SetPositionAndSize(args, last, edit, 3 /* border */,
1091 20 /* w */, textHeight/2 /* h */, 0x31 /* chain to right edge */);
1092 XtSetArg(args[j], XtNvertDistance, -1); j++;
1093 last = XtCreateManagedWidget("-", commandWidgetClass, form, args, j);
1094 XtAddCallback(last, XtNcallback, SpinCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1097 if(!engineDlg) option[i].value = *(Boolean*)option[i].target; // where checkbox callback uses it
1098 j = SetPositionAndSize(args, last, lastrow, 1 /* border */,
1099 textHeight/2 /* w */, textHeight/2 /* h */, 0xC0 /* chain both to left edge */);
1100 XtSetArg(args[j], XtNvertDistance, (textHeight+2)/4 + 3); j++;
1101 XtSetArg(args[j], XtNstate, option[i].value); j++;
1103 option[i].handle = (void*)
1104 (last = XtCreateManagedWidget(" ", toggleWidgetClass, form, args, j));
1105 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1106 option[i].max /* w */, textHeight /* h */, 0xC1 /* chain */);
1107 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1108 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1109 last = XtCreateManagedWidget("label", commandWidgetClass, form, args, j);
1110 // make clicking the text toggle checkbox
1111 XtAddEventHandler(last, ButtonPressMask, False, CheckCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1112 shrink = TRUE; // following buttons must get text height
1116 msg = option[i].name;
1118 chain = option[i].min;
1119 if(chain & SAME_ROW) forelast = lastrow; else shrink = FALSE;
1120 j = SetPositionAndSize(args, last, lastrow, (chain & 2) != 0 /* border */,
1121 option[i].max /* w */, shrink ? textHeight : 0 /* h */, chain | 2 /* chain */);
1123 if(option[i].choice) XtSetArg(args[j], XtNfontSet, *(XFontSet*)option[i].choice), j++;
1125 if(option[i].choice) XtSetArg(args[j], XtNfont, (XFontStruct*)option[i].choice), j++;
1127 XtSetArg(args[j], XtNresizable, False); j++;
1128 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1129 XtSetArg(args[j], XtNlabel, _(msg)); j++;
1130 option[i].handle = (void*) (last = XtCreateManagedWidget("label", labelWidgetClass, form, args, j));
1131 if(option[i].target) // allow user to specify event handler for button presses
1132 XtAddEventHandler(last, ButtonPressMask, False, LabelCallback, (XtPointer)(intptr_t) i + 256*dlgNr);
1136 if(option[i].min & SAME_ROW) {
1137 chain = 0x31; // 0011.0001 = both left and right side to right edge
1139 } else chain = 0, shrink = FALSE;
1140 j = SetPositionAndSize(args, last, lastrow, 3 /* border */,
1141 option[i].max /* w */, shrink ? textHeight : 0 /* h */, option[i].min & 0xE | chain /* chain */);
1142 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1143 if(option[i].textValue && *option[i].textValue == '#') { // special for buttons of New Variant dialog
1144 char *p = NULL, *v, n = option[i].value;
1145 if(n >= 0) v = VariantName(n), p = strstr(first.variants, v);
1146 XtSetArg(args[j], XtNsensitive, option[i].value >= 0 && (appData.noChessProgram
1147 || p && (!*v || strlen(p) == strlen(v) || p[strlen(v)] == ','))); j++;
1148 XtSetArg(args[j], XtNborderWidth, (gameInfo.variant == option[i].value)+1); j++;
1150 option[i].handle = (void*)
1151 (dialog = last = XtCreateManagedWidget(option[i].name, commandWidgetClass, form, args, j));
1152 if(option[i].choice && ((char*)option[i].choice)[0] == '#' && !engineDlg) { // for the color picker default-reset
1153 SetColor( *(char**) option[i-1].target, &option[i]);
1154 XtAddEventHandler(option[i-1].handle, KeyReleaseMask, False, ColorChanged, (XtPointer)(intptr_t) i-1);
1156 XtAddCallback(last, XtNcallback, GenericCallback, (XtPointer)(intptr_t) i + (dlgNr<<16)); // invokes user callback
1157 if(option[i].textValue && *option[i].textValue == '#') SetColor( option[i].textValue, &option[i]); // for new-variant buttons
1160 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1161 0 /* w */, textHeight /* h */, 0xC0 /* chain both sides to left edge */);
1162 XtSetArg(args[j], XtNjustify, XtJustifyLeft); j++;
1163 XtSetArg(args[j], XtNlabel, _(option[i].name)); j++;
1164 texts[h] = dialog = XtCreateManagedWidget(option[i].name, labelWidgetClass, form, args, j);
1166 if(option[i].min & COMBO_CALLBACK) msg = _(option[i].name); else {
1167 if(!engineDlg) SetCurrentComboSelection(option+i);
1168 msg=_(((char**)option[i].choice)[option[i].value]);
1171 j = SetPositionAndSize(args, dialog, last, (option[i].min & 2) == 0 /* border */,
1172 option[i].max && !engineDlg ? option[i].max : 100 /* w */,
1173 textHeight /* h */, 0x91 /* chain */); // same row as its label!
1174 XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name)); j++;
1175 XtSetArg(args[j], XtNlabel, msg); j++;
1177 option[i].handle = (void*)
1178 (last = XtCreateManagedWidget(" ", menuButtonWidgetClass, form, args, j));
1179 CreateComboPopup(last, option + i, i + 256*dlgNr, TRUE, -1);
1180 values[i] = option[i].value;
1183 // Listbox goes in viewport, as needed for game list
1184 if(option[i].min & SAME_ROW) forelast = lastrow;
1185 j = SetPositionAndSize(args, last, lastrow, 1 /* border */,
1186 option[i].max /* w */, option[i].value /* h */, option[i].min /* chain */);
1187 XtSetArg(args[j], XtNresizable, False); j++;
1188 XtSetArg(args[j], XtNallowVert, True); j++; // scoll direction
1190 XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
1191 j = 0; // now list itself
1192 XtSetArg(args[j], XtNdefaultColumns, 1); j++;
1193 XtSetArg(args[j], XtNforceColumns, True); j++;
1194 XtSetArg(args[j], XtNverticalList, True); j++;
1195 option[i].handle = (void*)
1196 (edit = XtCreateManagedWidget("list", listWidgetClass, last, args, j));
1197 XawListChange(option[i].handle, option[i].target, 0, 0, True);
1198 XawListHighlight(option[i].handle, 0);
1199 scrollTranslations[25] = '0' + i;
1200 scrollTranslations[27] = 'A' + dlgNr;
1201 XtOverrideTranslations(edit, XtParseTranslationTable(scrollTranslations)); // for mouse-wheel
1204 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1205 option[i].max /* w */, option[i].value /* h */, option[i].min /* chain */);
1206 option[i].handle = (void*)
1207 (last = XtCreateManagedWidget("graph", widgetClass, form, args, j));
1208 XtAddEventHandler(last, ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, False,
1209 (XtEventHandler) GraphEventProc, &option[i]); // mandatory user-supplied expose handler
1210 if(option[i].min & SAME_ROW) last = forelast, forelast = lastrow;
1211 option[i].choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, option[i].max, option[i].value); // image buffer
1213 case PopUp: // note: used only after Graph, so 'last' refers to the Graph widget
1214 option[i].handle = (void*) CreateComboPopup(last, option + i, i + 256*dlgNr, TRUE, option[i].value);
1218 if(option[i].min & SAME_ROW) forelast = lastrow;
1219 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1220 0 /* w */, 0 /* h */, option[i].min /* chain */);
1221 XtSetArg(args[j], XtNorientation, XtorientHorizontal); j++;
1222 XtSetArg(args[j], XtNvSpace, 0); j++;
1223 option[box=i].handle = (void*)
1224 (last = XtCreateWidget("box", boxWidgetClass, form, args, j));
1225 oldForm = form; form = last; oldLastRow = lastrow; oldForeLast = forelast;
1226 lastrow = NULL; last = NULL;
1229 j = SetPositionAndSize(args, last, lastrow, 0 /* border */,
1230 0 /* w */, 0 /* h */, 1 /* chain (always on same row) */);
1232 msg = _(option[i].name); // write name on the menu button
1233 if(msg) { if(*msg == '_') msg++; else if(msg[1] == '_') { // kludge to remove GTK shortkut-key indicators
1234 static char buf[MSG_SIZ];
1235 strncpy(buf, msg, MSG_SIZ); msg = buf + 1;
1238 XtSetArg(args[j], XtNmenuName, XtNewString(option[i].name)); j++;
1239 XtSetArg(args[j], XtNlabel, msg); j++;
1240 option[i].handle = (void*)
1241 (last = XtCreateManagedWidget(option[i].name, menuButtonWidgetClass, form, args, j));
1242 option[i].textValue = (char*) CreateComboPopup(last, option + i, i + 256*dlgNr, FALSE, -1);
1246 XtManageChildren(&form, 1);
1247 SqueezeIntoBox(&option[box], i-box, option[box].max);
1248 if(option[i].target) ((ButtonCallback*)option[i].target)(box); // callback that can make sizing decisions
1249 last = form; lastrow = oldLastRow; form = oldForm; forelast = oldForeLast;
1255 stack = !(option[i].min & SAME_ROW);
1258 printf("GenericPopUp: unexpected case in switch.\n");
1263 // make an attempt to align all spins and textbox controls
1264 maxWidth = maxTextWidth = 0;
1265 if(browse != NULL) {
1267 XtSetArg(args[j], XtNwidth, &bWidth); j++;
1268 XtGetValues(browse, args, j);
1270 for(h=0; h<height || c == width-1; h++) {
1272 if(option[i].type == EndMark) break;
1273 if(option[i].type == Spin || option[i].type == TextBox || option[i].type == ComboBox
1274 || option[i].type == PathName || option[i].type == FileName) {
1276 if(!texts[h]) continue;
1278 XtSetArg(args[j], XtNwidth, &w); j++;
1279 XtGetValues(texts[h], args, j);
1280 if(option[i].type == Spin) {
1281 if(w > maxWidth) maxWidth = w;
1284 if(w > maxTextWidth) maxTextWidth = w;
1285 if(!widest) widest = texts[h];
1289 if(maxTextWidth + 110 < maxWidth)
1290 maxTextWidth = maxWidth - 110;
1291 else maxWidth = maxTextWidth + 110;
1292 for(h=0; h<height || c == width-1; h++) {
1294 if(option[i].type == EndMark) break;
1295 if(!texts[h]) continue; // Note: texts[h] can be undefined (giving errors in valgrind), but then both if's below will be false.
1297 if(option[i].type == Spin) {
1298 XtSetArg(args[j], XtNwidth, maxWidth); j++;
1299 XtSetValues(texts[h], args, j);
1301 if(option[i].type == TextBox || option[i].type == ComboBox || option[i].type == PathName || option[i].type == FileName) {
1302 XtSetArg(args[j], XtNwidth, maxTextWidth); j++;
1303 XtSetValues(texts[h], args, j);
1304 if(bWidth != 50 && (option[i].type == FileName || option[i].type == PathName)) {
1305 int tWidth = (option[i].max ? option[i].max : 205) - 5 - bWidth;
1307 XtSetArg(args[j], XtNwidth, tWidth); j++;
1308 XtSetValues(option[i].handle, args, j);
1314 if(option[i].min & SAME_ROW) { // even when OK suppressed this EndMark bit can request chaining of last row to bottom
1315 for(j=i-1; option[j+1].min & SAME_ROW; j--) {
1316 XtSetArg(args[0], XtNtop, XtChainBottom);
1317 XtSetArg(args[1], XtNbottom, XtChainBottom);
1318 XtSetValues(option[j].handle, args, 2);
1320 if((option[j].type == TextBox || option[j].type == ListBox) && option[j].name[0] == NULLCHAR) {
1321 Widget w = option[j].handle;
1322 if(option[j].type == ListBox) w = XtParent(w); // for listbox we must chain viewport
1323 XtSetArg(args[0], XtNbottom, XtChainBottom);
1324 XtSetValues(w, args, 1);
1327 } else shrink = FALSE, lastrow = last, last = widest ? widest : dialog;
1328 j = SetPositionAndSize(args, last, anchor ? anchor : lastrow, 3 /* border */,
1329 0 /* w */, shrink ? textHeight : 0 /* h */, 0x37 /* chain: right, bottom and use both neighbors */);
1331 if(!(option[i].min & NO_OK)) {
1332 option[i].handle = b_ok = XtCreateManagedWidget(_("OK"), commandWidgetClass, form, args, j);
1333 XtAddCallback(b_ok, XtNcallback, GenericCallback, (XtPointer)(intptr_t) (30001 + (dlgNr<<16)));
1334 if(!(option[i].min & NO_CANCEL)) {
1335 XtSetArg(args[1], XtNfromHoriz, b_ok); // overwrites!
1336 b_cancel = XtCreateManagedWidget(_("Cancel"), commandWidgetClass, form, args, j);
1337 XtAddCallback(b_cancel, XtNcallback, GenericCallback, (XtPointer)(intptr_t) (30000 + (dlgNr<<16)));
1341 XtRealizeWidget(popup);
1342 if(dlgNr != BoardWindow) { // assign close button, and position w.r.t. pointer, if not main window
1343 XSetWMProtocols(xDisplay, XtWindow(popup), &wm_delete_window, 1);
1344 snprintf(def, MSG_SIZ, "<Message>WM_PROTOCOLS: GenericPopDown(\"%d\") \n", dlgNr);
1345 XtAugmentTranslations(popup, XtParseTranslationTable(def));
1346 XQueryPointer(xDisplay, xBoardWindow, &root, &child,
1347 &x, &y, &win_x, &win_y, &mask);
1349 XtSetArg(args[0], XtNx, x - 10);
1350 XtSetArg(args[1], XtNy, y - 30);
1351 XtSetValues(popup, args, 2);
1353 XtPopup(popup, modal ? XtGrabExclusive : XtGrabNone);
1354 shellUp[dlgNr]++; // count rather than flag
1356 if(textField) SetFocus(textField, popup, (XEvent*) NULL, False);
1357 if(dlgNr && wp[dlgNr]) { // if persistent window-info available, reposition
1359 if(wp[dlgNr]->width > 0 && wp[dlgNr]->height > 0) {
1360 XtSetArg(args[j], XtNheight, (Dimension) (wp[dlgNr]->height)); j++;
1361 XtSetArg(args[j], XtNwidth, (Dimension) (wp[dlgNr]->width)); j++;
1363 if(wp[dlgNr]->x > 0 && wp[dlgNr]->y > 0) {
1364 XtSetArg(args[j], XtNx, (Position) (wp[dlgNr]->x)); j++;
1365 XtSetArg(args[j], XtNy, (Position) (wp[dlgNr]->y)); j++;
1367 if(j) XtSetValues(popup, args, j);
1370 return 1; // tells caller he must do initialization (e.g. add specific event handlers)
1374 /* function called when the data to Paste is ready */
1376 SendTextCB (Widget w, XtPointer client_data, Atom *selection,
1377 Atom *type, XtPointer value, unsigned long *len, int *format)
1379 char buf[MSG_SIZ], *p = (char*) textOptions[(int)(intptr_t) client_data].choice, *name = (char*) value, *q;
1380 if (value==NULL || *len==0) return; /* nothing selected, abort */
1382 strncpy(buf, p, MSG_SIZ);
1383 q = strstr(p, "$name");
1384 snprintf(buf + (q-p), MSG_SIZ -(q-p), "%s%s", name, q+5);
1392 char *p = (char*) textOptions[n].choice;
1393 if(strstr(p, "$name")) {
1394 XtGetSelectionValue(menuBarWidget,
1395 XA_PRIMARY, XA_STRING,
1396 /* (XtSelectionCallbackProc) */ SendTextCB,
1397 (XtPointer) (intptr_t) n, /* client_data passed to PastePositionCB */
1400 } else SendString(p);
1404 SetInsertPos (Option *opt, int pos)
1407 if(pos == 999999) { // this kludge to indicate end in GTK is fatal in Xaw
1409 GetWidgetText(opt, &s);
1410 pos = strlen(s) - 1;
1412 XtSetArg(args[0], XtNinsertPosition, pos);
1413 XtSetValues(opt->handle, args, 1);
1414 // SetFocus(opt->handle, shells[InputBoxDlg], NULL, False); // No idea why this does not work, and the following is needed:
1415 // XSetInputFocus(xDisplay, XtWindow(opt->handle), RevertToPointerRoot, CurrentTime);
1419 TypeInProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
1420 { // can be used as handler for any text edit in any dialog (from GenericPopUp, that is)
1421 int n = prms[0][0] - '0';
1422 Widget sh = XtParent(XtParent(XtParent(w))); // popup shell
1426 if(n<2) { // Enter or Esc typed from primed text widget: treat as if dialog OK or cancel button hit.
1427 int dlgNr; // figure out what the dialog number is by comparing shells (because we must pass it :( )
1428 for(dlgNr=0; dlgNr<NrOfDialogs; dlgNr++) if(shellUp[dlgNr] && shells[dlgNr] == sh)
1429 GenericCallback (w, (XtPointer)(intptr_t) (30000 + n + (dlgNr<<16)), NULL);
1434 HardSetFocus (Option *opt, DialogClass dlg)
1436 XSetInputFocus(xDisplay, XtWindow(opt->handle), RevertToPointerRoot, CurrentTime);
1440 FileNamePopUpWrapper (char *label, char *def, char *filter, FileProc proc, Boolean pathFlag, char *openMode, char **openName, FILE **openFP)
1442 Browse(BoardWindow, label, (def[0] ? def : NULL), filter, False, openMode, openName, openFP);