1 /* NetHack 3.6 winX.c $NHDT-Date: 1552441031 2019/03/13 01:37:11 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.73 $ */
2 /* Copyright (c) Dean Luick, 1992 */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* JNetHack Copyright */
6 /* (c) Issei Numata 1994-1999 */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2020 */
8 /* JNetHack may be freely redistributed. See license for details. */
11 * "Main" file for the X window-port. This contains most of the interface
12 * routines. Please see doc/window.doc for an description of the window
17 #define PRESERVE_NO_SYSV /* X11 include files may define SYSV */
20 #ifdef MSDOS /* from compiler */
21 #define SHORT_FILENAMES
24 #include <X11/Intrinsic.h>
25 #include <X11/StringDefs.h>
26 #include <X11/Shell.h>
27 #include <X11/Xaw/AsciiText.h>
28 #include <X11/Xaw/Label.h>
29 #include <X11/Xaw/Form.h>
30 #include <X11/Xaw/Scrollbar.h>
31 #include <X11/Xaw/Paned.h>
32 #include <X11/Xaw/Cardinals.h>
33 #include <X11/Xatom.h>
36 /* for color support */
37 #ifdef SHORT_FILENAMES
38 #include <X11/IntrinsP.h>
40 #include <X11/IntrinsicP.h>
43 #ifdef PRESERVE_NO_SYSV
47 #undef PRESERVE_NO_SYSV
50 #ifdef SHORT_FILENAMES
51 #undef SHORT_FILENAMES /* hack.h will reset via global.h if necessary */
63 /* Should be defined in <X11/Intrinsic.h> but you never know */
64 #ifndef XtSpecificationRelease
65 #define XtSpecificationRelease 0
71 #include "../win/X11/nh72icon"
72 #include "../win/X11/nh56icon"
73 #include "../win/X11/nh32icon"
75 static struct icon_info {
78 unsigned width, height;
79 } icon_data[] = { { "nh72", nh72icon_bits, nh72icon_width, nh72icon_height },
80 { "nh56", nh56icon_bits, nh56icon_width, nh56icon_height },
81 { "nh32", nh32icon_bits, nh32icon_width, nh32icon_height },
82 { (const char *) 0, (unsigned char *) 0, 0, 0 } };
85 * Private global variables (shared among the window port files).
87 struct xwindow window_list[MAX_WINDOWS];
88 AppResources appResources;
89 void FDECL((*input_func), (Widget, XEvent *, String *, Cardinal *));
90 int click_x, click_y, click_button; /* Click position on a map window */
91 /* (filled by set_button_values()). */
92 int updated_inventory;
94 static int (*old_error_handler) (Display *, XErrorEvent *);
96 #if !defined(NO_SIGNAL) && defined(SAFERHANGUP)
97 #if XtSpecificationRelease >= 6
98 #define X11_HANGUP_SIGNAL
99 static XtSignalId X11_sig_id;
103 /* Interface definition, for windows.c */
104 struct window_procs X11_procs = {
106 (WC_COLOR | WC_HILITE_PET | WC_ASCII_MAP | WC_TILED_MAP
107 | WC_PLAYER_SELECTION | WC_PERM_INVENT | WC_MOUSE_SUPPORT),
108 #if defined(STATUS_HILITES)
109 WC2_FLUSH_STATUS | WC2_RESET_STATUS | WC2_HILITE_STATUS |
112 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* color availability */
114 X11_player_selection, X11_askname, X11_get_nh_event, X11_exit_nhwindows,
115 X11_suspend_nhwindows, X11_resume_nhwindows, X11_create_nhwindow,
116 X11_clear_nhwindow, X11_display_nhwindow, X11_destroy_nhwindow, X11_curs,
117 X11_putstr, genl_putmixed, X11_display_file, X11_start_menu, X11_add_menu,
118 X11_end_menu, X11_select_menu,
119 genl_message_menu, /* no need for X-specific handling */
120 X11_update_inventory, X11_mark_synch, X11_wait_synch,
127 X11_print_glyph, X11_raw_print, X11_raw_print_bold, X11_nhgetch,
128 X11_nh_poskey, X11_nhbell, X11_doprev_message, X11_yn_function,
129 X11_getlin, X11_get_ext_cmd, X11_number_pad, X11_delay_output,
130 #ifdef CHANGE_COLOR /* only a Mac option currently */
133 /* other defs that really should go away (they're tty specific) */
134 X11_start_screen, X11_end_screen,
135 #ifdef GRAPHIC_TOMBSTONE
140 X11_preference_update, X11_getmsghistory, X11_putmsghistory,
141 X11_status_init, X11_status_finish, X11_status_enablefield,
143 genl_can_suspend_no, /* XXX may not always be correct */
149 static winid NDECL(find_free_window);
151 static void FDECL(nhFreePixel, (XtAppContext, XrmValuePtr, XtPointer,
152 XrmValuePtr, Cardinal *));
154 static boolean FDECL(new_resource_macro, (String, unsigned));
155 static void NDECL(load_default_resources);
156 static void NDECL(release_default_resources);
157 static int FDECL(panic_on_error, (Display *, XErrorEvent *));
158 #ifdef X11_HANGUP_SIGNAL
159 static void FDECL(X11_sig, (int));
160 static void FDECL(X11_sig_cb, (XtPointer, XtSignalId *));
162 static void FDECL(d_timeout, (XtPointer, XtIntervalId *));
163 static void FDECL(X11_hangup, (Widget, XEvent *, String *, Cardinal *));
164 static void FDECL(askname_delete, (Widget, XEvent *, String *, Cardinal *));
165 static void FDECL(askname_done, (Widget, XtPointer, XtPointer));
166 static void FDECL(done_button, (Widget, XtPointer, XtPointer));
167 static void FDECL(getline_delete, (Widget, XEvent *, String *, Cardinal *));
168 static void FDECL(abort_button, (Widget, XtPointer, XtPointer));
169 static void NDECL(release_getline_widgets);
170 static void FDECL(yn_delete, (Widget, XEvent *, String *, Cardinal *));
171 static void FDECL(yn_key, (Widget, XEvent *, String *, Cardinal *));
172 static void NDECL(release_yn_widgets);
173 static int FDECL(input_event, (int));
174 static void FDECL(win_visible, (Widget, XtPointer, XEvent *, Boolean *));
175 static void NDECL(init_standard_windows);
177 #ifdef INSTALLCOLORMAP
184 static boolean x_inited = FALSE; /* TRUE if window system is set up. */
185 static winid message_win = WIN_ERR, /* These are the winids of the message, */
186 map_win = WIN_ERR, /* map, and status windows, when they */
187 status_win = WIN_ERR; /* are created in init_windows(). */
188 static Pixmap icon_pixmap = None; /* Pixmap for icon. */
191 X11_putmsghistory(msg, is_restoring)
193 boolean is_restoring;
195 if (WIN_MESSAGE != WIN_ERR) {
196 struct xwindow *wp = &window_list[WIN_MESSAGE];
197 debugpline2("X11_putmsghistory('%s',%i)", msg, is_restoring);
199 append_message(wp, msg);
204 X11_getmsghistory(init)
207 if (WIN_MESSAGE != WIN_ERR) {
208 static struct line_element *curr = (struct line_element *) 0;
209 static int numlines = 0;
210 struct xwindow *wp = &window_list[WIN_MESSAGE];
213 curr = (struct line_element *) 0;
216 curr = wp->mesg_information->head;
220 if (numlines < wp->mesg_information->num_lines) {
223 debugpline2("X11_getmsghistory(%i)='%s'", init, curr->line);
231 * Find the window structure that corresponds to the given widget. Note
232 * that this is not the popup widget, nor the viewport, but the child.
242 * Search to find the corresponding window. Look at the main widget,
243 * popup, the parent of the main widget, then parent of the widget.
245 for (windex = 0, wp = window_list; windex < MAX_WINDOWS; windex++, wp++)
246 if (wp->type != NHW_NONE && (wp->w == w || wp->popup == w
247 || (wp->w && (XtParent(wp->w)) == w)
248 || (wp->popup == XtParent(w))))
251 if (windex == MAX_WINDOWS)
252 panic("find_widget: can't match widget");
257 * Find a free window slot for use.
265 for (windex = 0, wp = &window_list[0]; windex < MAX_WINDOWS;
267 if (wp->type == NHW_NONE)
270 if (windex == MAX_WINDOWS)
271 panic("find_free_window: no free windows!");
272 return (winid) windex;
281 init_menu_nhcolors(wp);
282 /* FIXME: init_menu_nhcolors may fail */
284 if (clr >= 0 && clr < CLR_MAX)
285 return wp->nh_colors[clr];
287 return wp->nh_colors[0];
291 init_menu_nhcolors(wp)
294 static const char *mapCLR_to_res[CLR_MAX] = {
313 Colormap screen_colormap;
319 char clr_name[BUFSZ];
320 char clrclass[BUFSZ];
321 const char *wintypenames[NHW_TEXT] = {
322 "message", "status", "map", "menu", "text"
327 if (wp->nh_colors_inited || !wp->type)
330 wtn = wintypenames[wp->type - 1];
332 (void) upstart(wtn_up);
334 dpy = XtDisplay(wp->w);
335 screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy));
336 rDB = XrmGetDatabase(dpy);
338 for (color = 0; color < CLR_MAX; color++) {
339 Sprintf(clr_name, "nethack.%s.%s", wtn, mapCLR_to_res[color]);
340 Sprintf(clrclass, "NetHack.%s.%s", wtn_up, mapCLR_to_res[color]);
342 if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) {
343 Sprintf(clr_name, "nethack.map.%s", mapCLR_to_res[color]);
344 Sprintf(clrclass, "NetHack.Map.%s", mapCLR_to_res[color]);
347 if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) {
348 impossible("XrmGetResource error (%s)", clr_name);
349 } else if (!strcmp(ret_type[0], "String")) {
352 if (value.size >= sizeof tmpbuf)
353 value.size = sizeof tmpbuf - 1;
354 (void) strncpy(tmpbuf, (char *) value.addr, (int) value.size);
355 tmpbuf[value.size] = '\0';
356 /* tmpbuf now contains the color name from the named resource */
358 rc = XAllocNamedColor(dpy, screen_colormap, tmpbuf,
359 &wp->nh_colors[color],
360 &wp->nh_colors[color]);
362 impossible("XAllocNamedColor failed for color %i (%s)",
368 wp->nh_colors_inited = TRUE;
372 * Color conversion. The default X11 color converters don't try very
373 * hard to find matching colors in PseudoColor visuals. If they can't
374 * allocate the exact color, they puke and give you something stupid.
375 * This is an attempt to find some close readonly cell and use it.
377 XtConvertArgRec const nhcolorConvertArgs[] = {
378 { XtWidgetBaseOffset,
379 (XtPointer) (ptrdiff_t) XtOffset(Widget, core.screen),
381 { XtWidgetBaseOffset,
382 (XtPointer) (ptrdiff_t) XtOffset(Widget, core.colormap),
386 #define done(type, value) \
388 if (toVal->addr != 0) { \
389 if (toVal->size < sizeof(type)) { \
390 toVal->size = sizeof(type); \
393 *(type *)(toVal->addr) = (value); \
395 static type static_val; \
396 static_val = (value); \
397 toVal->addr = (genericptr_t) &static_val; \
399 toVal->size = sizeof(type); \
404 * Find a color that approximates the color named in "str".
405 * The "str" color may be a color name ("red") or number ("#7f0000").
406 * If str is Null, then "color" is assumed to contain the RGB color wanted.
407 * The approximate color found is returned in color as well.
408 * Return True if something close was found.
411 nhApproxColor(screen, colormap, str, color)
412 Screen *screen; /* screen to use */
413 Colormap colormap; /* the colormap to use */
414 char *str; /* color name */
415 XColor *color; /* the X color structure; changed only if successful */
418 long cdiff = 16777216; /* 2^24; hopefully our map is smaller */
420 static XColor *table = 0;
424 /* if the screen doesn't have a big colormap, don't waste our time
425 or if it's huge, and _some_ match should have been possible */
426 if ((ncells = CellsOfScreen(screen)) < 256 || ncells > 4096)
429 if (str != (char *) 0) {
430 if (!XParseColor(DisplayOfScreen(screen), colormap, str, &tmp))
434 tmp.flags = 7; /* force to use all 3 of RGB */
438 table = (XColor *) XtCalloc(ncells, sizeof(XColor));
439 for (i = 0; i < ncells; i++)
441 XQueryColors(DisplayOfScreen(screen), colormap, table, ncells);
444 /* go thru cells and look for the one with smallest diff */
445 /* diff is calculated abs(reddiff)+abs(greendiff)+abs(bluediff) */
446 /* a more knowledgeable color person might improve this -dlc */
448 for (i = 0; i < ncells; i++) {
449 if (table[i].flags == tmp.flags) {
450 j = (int) table[i].red - (int) tmp.red;
454 j = (int) table[i].green - (int) tmp.green;
458 j = (int) table[i].blue - (int) tmp.blue;
464 tmp.pixel = i; /* table[i].pixel == i */
469 if (cdiff == 16777216)
470 return False; /* nothing found?! */
473 * Found something. Return it and mark this color as used to avoid
474 * reuse. Reuse causes major contrast problems :-)
476 *color = table[tmp.pixel];
477 table[tmp.pixel].flags = 0;
478 /* try to alloc the color, so no one else can change it */
479 if (!XAllocColor(DisplayOfScreen(screen), colormap, color)) {
486 #ifdef INSTALLCOLORMAP
488 nhCvtStringToColormap(dpy, args, num_args, fromVal, toVal, data)
497 int screen = DefaultScreen(dpy);
498 Colormap dcmap = DefaultColormap(dpy, screen);
500 if(strncmp(fromVal->addr, "install", 7)){
504 cmap = XCreateColormap(dpy,
505 RootWindow(dpy, screen),
506 DefaultVisual(dpy, screen),
516 white.green = 0xffff;
519 XAllocColor(dpy, dcmap, &black);
520 XAllocColor(dpy, dcmap, &white);
522 XAllocColor(dpy, cmap, &black);
523 XAllocColor(dpy, cmap, &white);
526 done(Colormap, cmap);
528 #endif /*INSTALLCOLORMAP*/
531 nhCvtStringToPixel(dpy, args, num_args, fromVal, toVal, closure_ret)
537 XtPointer *closure_ret;
539 String str = (String) fromVal->addr;
543 XtAppContext app = XtDisplayToApplicationContext(dpy);
547 Cardinal num_params = 1;
549 if (*num_args != 2) {
550 XtAppWarningMsg(app, "wrongParameters",
551 "cvtStringToPixel", "XtToolkitError",
552 "String to pixel conversion needs screen and colormap arguments",
553 (String *) 0, (Cardinal *) 0);
557 screen = *((Screen **) args[0].addr);
558 colormap = *((Colormap *) args[1].addr);
560 /* If Xt colors, use the Xt routine and hope for the best */
561 #if (XtSpecificationRelease >= 5)
562 if ((strcmpi(str, XtDefaultBackground) == 0)
563 || (strcmpi(str, XtDefaultForeground) == 0)) {
564 return XtCvtStringToPixel(dpy, args, num_args, fromVal, toVal,
568 if (strcmpi(str, XtDefaultBackground) == 0) {
569 *closure_ret = (char *) False;
570 done(Pixel, WhitePixelOfScreen(screen));
572 if (strcmpi(str, XtDefaultForeground) == 0) {
573 *closure_ret = (char *) False;
574 done(Pixel, BlackPixelOfScreen(screen));
578 status = XAllocNamedColor(DisplayOfScreen(screen), colormap, (char *) str,
579 &screenColor, &exactColor);
583 /* some versions of XAllocNamedColor don't allow #xxyyzz names */
584 if (str[0] == '#' && XParseColor(DisplayOfScreen(screen), colormap,
586 && XAllocColor(DisplayOfScreen(screen), colormap, &exactColor)) {
587 *closure_ret = (char *) True;
588 done(Pixel, exactColor.pixel);
592 /* Server returns a specific error code but Xlib discards it. Ugh */
593 if (XLookupColor(DisplayOfScreen(screen), colormap, (char *) str,
594 &exactColor, &screenColor)) {
595 /* try to find another color that will do */
596 if (nhApproxColor(screen, colormap, (char *) str, &screenColor)) {
597 *closure_ret = (char *) True;
598 done(Pixel, screenColor.pixel);
600 type = nhStr("noColormap");
601 msg = nhStr("Cannot allocate colormap entry for \"%s\"");
603 /* some versions of XLookupColor also don't allow #xxyyzz names */
605 && (nhApproxColor(screen, colormap, (char *) str,
607 *closure_ret = (char *) True;
608 done(Pixel, screenColor.pixel);
610 type = nhStr("badValue");
611 msg = nhStr("Color name \"%s\" is not defined");
614 XtAppWarningMsg(app, type, "cvtStringToPixel", "XtToolkitError", msg,
615 params, &num_params);
616 *closure_ret = False;
619 *closure_ret = (char *) True;
620 done(Pixel, screenColor.pixel);
624 /* Ask the WM for window frame size */
626 get_window_frame_extents(w, top, bottom, left, right)
628 long *top, *bottom, *left, *right;
631 Display *dpy = XtDisplay(w);
632 Window win = XtWindow(w);
635 unsigned long nitems;
636 unsigned long nbytes;
637 unsigned char *data = 0;
640 prop = XInternAtom(dpy, "_NET_FRAME_EXTENTS", True);
642 while (XGetWindowProperty(dpy, win, prop,
643 0, 4, False, AnyPropertyType,
645 &nitems, &nbytes, &data) != Success
646 || nitems != 4 || nbytes != 0)
648 XNextEvent(dpy, &event);
651 extents = (long *) data;
656 *bottom = extents[3];
660 get_widget_window_geometry(w, x,y, width, height)
662 int *x, *y, *width, *height;
664 long top, bottom, left, right;
666 XtSetArg(args[0], nhStr(XtNx), x);
667 XtSetArg(args[1], nhStr(XtNy), y);
668 XtSetArg(args[2], nhStr(XtNwidth), width);
669 XtSetArg(args[3], nhStr(XtNheight), height);
670 XtGetValues(w, args, 4);
671 get_window_frame_extents(w, &top, &bottom, &left, &right);
676 /* Change the full font name string so the weight is "bold" */
678 fontname_boldify(fontname)
679 const char *fontname;
681 static char buf[BUFSZ];
686 if (*fontname == '-')
694 } while (*fontname && *fontname != '-');
718 XtSetArg(args[0], nhStr(XtNfont), &fs);
719 XtGetValues(w, args, 1);
721 if (!XGetFontProperty(fs, XA_FONT, &ret))
724 wp->boldfs_dpy = dpy = XtDisplay(w);
725 fontname = fontname_boldify(XGetAtomName(dpy, (Atom)ret));
726 wp->boldfs = XLoadQueryFont(dpy, fontname);
732 nhFreePixel(app, toVal, closure, args, num_args)
742 if (*num_args != 2) {
743 XtAppWarningMsg(app, "wrongParameters", "freePixel", "XtToolkitError",
744 "Freeing a pixel requires screen and colormap arguments",
745 (String *) 0, (Cardinal *) 0);
749 screen = *((Screen **) args[0].addr);
750 colormap = *((Colormap *) args[1].addr);
753 XFreeColors(DisplayOfScreen(screen), colormap,
754 (unsigned long *) toVal->addr, 1, (unsigned long) 0);
759 /* [ALI] Utility function to ask Xaw for font height, since the previous
760 * assumption of ascent + descent is not always valid.
766 #ifdef _XawTextSink_h
768 XawTextPosition pos = 0;
769 int resWidth, resHeight;
772 XtSetArg(args[0], XtNtextSink, &sink);
773 XtGetValues(w, args, 1);
775 XawTextSinkFindPosition(sink, pos, 0, 0, 0, &pos, &resWidth, &resHeight);
781 XtSetArg(args[0], XtNfont, &fs);
782 XtGetValues(w, args, 1);
784 /* Assume font height is ascent + descent. */
785 return = fs->ascent + fs->descent;
789 static String *default_resource_data = 0, /* NULL-terminated arrays */
790 *def_rsrc_macr = 0, /* macro names */
791 *def_rsrc_valu = 0; /* macro values */
793 /* caller found "#define"; parse into macro name and its expansion value */
795 new_resource_macro(inbuf, numdefs)
796 String inbuf; /* points past '#define' rather than to start of buffer */
797 unsigned numdefs; /* array slot to fill */
801 /* we expect inbuf to be terminated by newline; get rid of it */
803 if (q > inbuf && q[-1] == '\n')
806 /* figure out macro's name */
807 for (p = inbuf; *p == ' ' || *p == '\t'; ++p)
808 continue; /* skip whitespace */
809 for (q = p; *q && *q != ' ' && *q != '\t'; ++q)
810 continue; /* token consists of non-whitespace */
811 Strcat(q, " "); /* guarantee something beyond '#define FOO' */
812 *q++ = '\0'; /* p..(q-1) contains macro name */
813 if (!*p) /* invalid definition: '#define' followed by nothing */
815 def_rsrc_macr[numdefs] = dupstr(p);
817 /* figure out macro's value; empty value is supported but not expected */
818 while (*q == ' ' || *q == '\t')
819 ++q; /* skip whitespace between name and value */
820 for (p = eos(q); --p > q && (*p == ' ' || *p == '\t'); )
821 continue; /* discard trailing whitespace */
822 *++p = '\0'; /* q..p containes macro value */
823 def_rsrc_valu[numdefs] = dupstr(q);
827 /* read the template NetHack.ad into default_resource_data[] to supply
828 fallback resources to XtAppInitialize() */
830 load_default_resources()
834 unsigned insiz, linelen, longlen, numlines, numdefs, midx;
835 boolean comment, isdef;
838 * Running nethack via the shell script adds $HACKDIR to the path used
839 * by X to find resources, but running it directly doesn't. So, if we
840 * can find the template file for NetHack.ad in the current directory,
841 * load its contents into memory so that the application startup call
842 * in X11_init_nhwindows() can use them as fallback resources.
844 * No attempt to support the 'include' directive has been made, nor
845 * backslash+newline continuation lines. Macro expansion (at most
846 * one substitution per line) is supported. '#define' to introduce
847 * a macro must be at start of line (no whitespace before or after
848 * the '#' character).
850 fp = fopen("./NetHack.ad", "r");
854 /* measure the file without retaining its contents */
855 insiz = BUFSIZ; /* stdio BUFSIZ, not nethack BUFSZ */
856 inbuf = (String) alloc(insiz);
857 linelen = longlen = 0;
858 numlines = numdefs = 0;
859 comment = isdef = FALSE; /* lint suppression */
860 while (fgets(inbuf, insiz, fp)) {
862 /* !linelen: inbuf has start of record; treat empty as comment */
863 comment = (*inbuf == '!' || *inbuf == '\n');
864 isdef = !strncmp(inbuf, "#define", 7);
867 linelen += strlen(inbuf);
868 if (!index(inbuf, '\n'))
870 if (linelen > longlen)
873 if (!comment && !isdef)
877 if (numdefs) { /* don't alloc if 0; no need for any terminator */
878 def_rsrc_macr = (String *) alloc(numdefs * sizeof (String));
879 def_rsrc_valu = (String *) alloc(numdefs * sizeof (String));
880 insiz += BUFSIZ; /* include room for macro expansion within buffer */
882 if (insiz > BUFSIZ) {
883 free((genericptr_t) inbuf);
884 inbuf = (String) alloc(insiz);
886 ++numlines; /* room for terminator */
887 default_resource_data = (String *) alloc(numlines * sizeof (String));
889 /* now re-read the file, storing its contents into the allocated array
890 after performing macro substitutions */
892 numlines = numdefs = 0;
893 while (fgets(inbuf, insiz, fp)) {
894 if (!strncmp(inbuf, "#define", 7)) {
895 if (new_resource_macro(&inbuf[7], numdefs))
897 } else if (*inbuf != '!' && *inbuf != '\n') {
900 * Macro expansion: we assume at most one substitution
901 * per line. That's all that our sample NetHack.ad uses.
903 * If we ever need more, this will have to become a lot
904 * more sophisticated. It will need to find the first
905 * instance within inbuf[] rather than first macro which
906 * appears, and to avoid finding names within substituted
909 * Any substitution which would exceed the buffer size is
910 * skipped. A sophisticated implementation would need to
911 * be prepared to allocate a bigger buffer when needed.
913 linelen = strlen(inbuf);
914 for (midx = 0; midx < numdefs; ++midx) {
915 if ((linelen + strlen(def_rsrc_valu[midx])
916 < insiz - strlen(def_rsrc_macr[midx]))
917 && strNsubst(inbuf, def_rsrc_macr[midx],
918 def_rsrc_valu[midx], 1))
922 default_resource_data[numlines++] = dupstr(inbuf);
925 default_resource_data[numlines] = (String) 0;
927 free((genericptr_t) inbuf);
928 if (def_rsrc_macr) { /* implies def_rsrc_valu is non-Null too */
929 for (midx = 0; midx < numdefs; ++midx) {
930 free((genericptr_t) def_rsrc_macr[midx]);
931 free((genericptr_t) def_rsrc_valu[midx]);
933 free((genericptr_t) def_rsrc_macr), def_rsrc_macr = 0;
934 free((genericptr_t) def_rsrc_valu), def_rsrc_valu = 0;
939 release_default_resources()
941 if (default_resource_data) {
944 for (idx = 0; default_resource_data[idx]; idx++)
945 free((genericptr_t) default_resource_data[idx]);
946 free((genericptr_t) default_resource_data), default_resource_data = 0;
948 /* def_rsrc_macr[] and def_rsrc_valu[] have already been released */
951 /* Global Functions ======================================================= */
963 X11_raw_print_bold(str)
973 X11_curs(window, x, y)
979 if (x < 0 || x >= COLNO) {
980 impossible("curs: bad x value [%d]", x);
983 if (y < 0 || y >= ROWNO) {
984 impossible("curs: bad y value [%d]", y);
988 window_list[window].cursx = x;
989 window_list[window].cursy = y;
993 X11_putstr(window, attr, str)
1001 check_winid(window);
1002 wp = &window_list[window];
1006 (void) strncpy(toplines, str, TBUFSZ); /* for Norep(). */
1007 toplines[TBUFSZ - 1] = 0;
1008 append_message(wp, str);
1010 #ifndef STATUS_HILITES
1012 adjust_status(wp, str);
1016 impossible("putstr: called on map window \"%s\"", str);
1019 if (wp->menu_information->is_menu) {
1020 impossible("putstr: called on a menu window, \"%s\" discarded",
1025 * Change this menu window into a text window by creating a
1026 * new text window, then copying it to this winid.
1028 new_win = X11_create_nhwindow(NHW_TEXT);
1029 X11_destroy_nhwindow(window);
1030 *wp = window_list[new_win];
1031 window_list[new_win].type = NHW_NONE; /* allow re-use */
1032 /* fall though to add text */
1034 add_to_text_window(wp, attr, str);
1037 impossible("putstr: unknown window type [%d] \"%s\"", wp->type, str);
1041 /* We do event processing as a callback, so this is a null routine. */
1051 return input_event(EXIT_ON_KEY_PRESS);
1055 X11_nh_poskey(x, y, mod)
1058 int val = input_event(EXIT_ON_KEY_OR_BUTTON_PRESS);
1060 if (val == 0) { /* user clicked on a map window */
1063 *mod = click_button;
1069 X11_create_nhwindow(type)
1076 panic("create_nhwindow: windows not initialized");
1078 #ifdef X11_HANGUP_SIGNAL
1079 /* set up our own signal handlers on the first call. Can't do this in
1080 * X11_init_nhwindows because unixmain sets its handler after calling
1081 * all the init routines. */
1082 if (X11_sig_id == 0) {
1083 X11_sig_id = XtAppAddSignal(app_context, X11_sig_cb, (XtPointer) 0);
1086 struct sigaction sact;
1088 (void) memset((char *) &sact, 0, sizeof(struct sigaction));
1089 sact.sa_handler = (SIG_RET_TYPE) X11_sig;
1090 (void) sigaction(SIGHUP, &sact, (struct sigaction *) 0);
1092 (void) sigaction(SIGXCPU, &sact, (struct sigaction *) 0);
1096 (void) signal(SIGHUP, (SIG_RET_TYPE) X11_sig);
1098 (void) signal(SIGXCPU, (SIG_RET_TYPE) X11_sig);
1105 * We have already created the standard message, map, and status
1106 * windows in the window init routine. The first window of that
1107 * type to be created becomes the standard.
1109 * A better way to do this would be to say that init_nhwindows()
1110 * has already defined these three windows.
1112 if (type == NHW_MAP && map_win != WIN_ERR) {
1117 if (type == NHW_MESSAGE && message_win != WIN_ERR) {
1118 window = message_win;
1119 message_win = WIN_ERR;
1122 if (type == NHW_STATUS && status_win != WIN_ERR) {
1123 window = status_win;
1124 status_win = WIN_ERR;
1128 window = find_free_window();
1129 wp = &window_list[window];
1131 /* The create routines will set type, popup, w, and Win_info. */
1132 wp->prevx = wp->prevy = wp->cursx = wp->cursy = wp->pixel_width =
1133 wp->pixel_height = 0;
1134 wp->keep_window = FALSE;
1135 wp->nh_colors_inited = FALSE;
1136 wp->boldfs = (XFontStruct *) 0;
1137 wp->boldfs_dpy = (Display *) 0;
1138 wp->title = (char *) 0;
1142 create_map_window(wp, TRUE, (Widget) 0);
1145 create_message_window(wp, TRUE, (Widget) 0);
1148 create_status_window(wp, TRUE, (Widget) 0);
1151 create_menu_window(wp);
1154 create_text_window(wp);
1157 panic("create_nhwindow: unknown type [%d]", type);
1164 X11_clear_nhwindow(window)
1169 check_winid(window);
1170 wp = &window_list[window];
1174 clear_map_window(wp);
1177 clear_text_window(wp);
1182 /* do nothing for these window types */
1185 panic("clear_nhwindow: unknown window type [%d]", wp->type);
1191 X11_display_nhwindow(window, blocking)
1197 check_winid(window);
1198 wp = &window_list[window];
1203 nh_XtPopup(wp->popup, (int) XtGrabNone, wp->w);
1204 display_map_window(wp); /* flush map */
1207 * We need to flush the message window here due to the way the tty
1208 * port is set up. To flush a window, you need to call this
1209 * routine. However, the tty port _pauses_ with a --more-- if we
1210 * do a display_nhwindow(WIN_MESSAGE, FALSE). Thus, we can't call
1211 * display_nhwindow(WIN_MESSAGE,FALSE) in parse() because then we
1212 * get a --more-- after every line.
1214 * Perhaps the window document should mention that when the map
1215 * is flushed, everything on the three main windows should be
1216 * flushed. Note: we don't need to flush the status window
1217 * because we don't buffer changes.
1219 if (WIN_MESSAGE != WIN_ERR)
1220 display_message_window(&window_list[WIN_MESSAGE]);
1222 (void) x_event(EXIT_ON_KEY_OR_BUTTON_PRESS);
1226 nh_XtPopup(wp->popup, (int) XtGrabNone, wp->w);
1227 display_message_window(wp); /* flush messages */
1231 nh_XtPopup(wp->popup, (int) XtGrabNone, wp->w);
1232 break; /* no flushing necessary */
1235 menu_item *selected;
1238 n = X11_select_menu(window, PICK_NONE, &selected);
1240 impossible("perminvent: %d selected??", n);
1241 free((genericptr_t) selected);
1246 display_text_window(wp, blocking); /* pop up text window */
1249 panic("display_nhwindow: unknown window type [%d]", wp->type);
1255 X11_destroy_nhwindow(window)
1260 check_winid(window);
1261 wp = &window_list[window];
1263 * "Zap" known windows, but don't destroy them. We need to keep the
1264 * toplevel widget popped up so that later windows (e.g. tombstone)
1265 * are visible on DECWindow systems. This is due to the virtual
1266 * roots that the DECWindow wm creates.
1268 if (window == WIN_MESSAGE) {
1269 wp->keep_window = TRUE;
1270 WIN_MESSAGE = WIN_ERR;
1271 iflags.window_inited = 0;
1272 } else if (window == WIN_MAP) {
1273 wp->keep_window = TRUE;
1275 } else if (window == WIN_STATUS) {
1276 wp->keep_window = TRUE;
1277 WIN_STATUS = WIN_ERR;
1278 } else if (window == WIN_INVEN) {
1279 /* don't need to keep this one */
1280 WIN_INVEN = WIN_ERR;
1284 XFreeFont(wp->boldfs_dpy, wp->boldfs);
1285 wp->boldfs = (XFontStruct *) 0;
1286 wp->boldfs_dpy = (Display *) 0;
1291 wp->title = (char *) 0;
1296 destroy_map_window(wp);
1299 destroy_menu_window(wp);
1302 destroy_text_window(wp);
1305 destroy_status_window(wp);
1308 destroy_message_window(wp);
1311 panic("destroy_nhwindow: unknown window type [%d]", wp->type);
1317 X11_update_inventory()
1319 if (x_inited && window_list[WIN_INVEN].menu_information->is_up) {
1320 updated_inventory = 1; /* hack to avoid mapping&raising window */
1321 (void) display_inventory((char *) 0, FALSE);
1322 updated_inventory = 0;
1326 /* The current implementation has all of the saved lines on the screen. */
1328 X11_doprev_message()
1336 /* We can't use XBell until toplevel has been initialized. */
1338 XBell(XtDisplay(toplevel), 0);
1339 /* else print ^G ?? */
1347 * The window document is unclear about the status of text
1348 * that has been pline()d but not displayed w/display_nhwindow().
1349 * Both the main and tty code assume that a pline() followed
1350 * by mark_synch() results in the text being seen, even if
1351 * display_nhwindow() wasn't called. Duplicate this behavior.
1353 if (WIN_MESSAGE != WIN_ERR)
1354 display_message_window(&window_list[WIN_MESSAGE]);
1355 XSync(XtDisplay(toplevel), False);
1363 XFlush(XtDisplay(toplevel));
1366 /* Both resume_ and suspend_ are called from ioctl.c and unixunix.c. */
1368 X11_resume_nhwindows()
1374 X11_suspend_nhwindows(str)
1382 /* Under X, we don't need to initialize the number pad. */
1385 X11_number_pad(state) /* called from options.c */
1393 /* called from setftty() in unixtty.c */
1400 /* called from settty() in unixtty.c */
1407 #ifdef GRAPHIC_TOMBSTONE
1409 X11_outrip(window, how, when)
1417 check_winid(window);
1418 wp = &window_list[window];
1420 /* make sure the graphical tombstone is available; it's not easy to
1421 revert to the ASCII-art text tombstone once we're past this point */
1422 if (appResources.tombstone && *appResources.tombstone)
1423 rip_fp = fopen(appResources.tombstone, "r"); /* "rip.xpm" */
1425 genl_outrip(window, how, when);
1428 (void) fclose(rip_fp);
1430 if (wp->type == NHW_TEXT) {
1431 wp->text_information->is_rip = TRUE;
1433 panic("ripout on non-text window (window type [%d])", wp->type);
1436 calculate_rip_text(how, when);
1440 /* init and exit nhwindows ------------------------------------------------ */
1442 XtAppContext app_context; /* context of application */
1443 Widget toplevel = (Widget) 0; /* toplevel widget */
1444 Atom wm_delete_window; /* pop down windows */
1446 static XtActionsRec actions[] = {
1447 { nhStr("dismiss_text"), dismiss_text }, /* text widget button action */
1448 { nhStr("delete_text"), delete_text }, /* text widget delete action */
1449 { nhStr("key_dismiss_text"), key_dismiss_text }, /* text key action */
1450 #ifdef GRAPHIC_TOMBSTONE
1451 { nhStr("rip_dismiss_text"), rip_dismiss_text }, /* rip in text widget */
1453 { nhStr("menu_key"), menu_key }, /* menu accelerators */
1454 { nhStr("yn_key"), yn_key }, /* yn accelerators */
1455 { nhStr("yn_delete"), yn_delete }, /* yn delete-window */
1456 { nhStr("askname_delete"), askname_delete }, /* askname delete-window */
1457 { nhStr("getline_delete"), getline_delete }, /* getline delete-window */
1458 { nhStr("menu_delete"), menu_delete }, /* menu delete-window */
1459 { nhStr("ec_key"), ec_key }, /* extended commands */
1460 { nhStr("ec_delete"), ec_delete }, /* ext-com menu delete */
1461 { nhStr("ps_key"), ps_key }, /* player selection */
1462 { nhStr("plsel_quit"), plsel_quit }, /* player selection dialog */
1463 { nhStr("plsel_play"), plsel_play }, /* player selection dialog */
1464 { nhStr("plsel_rnd"), plsel_randomize }, /* player selection dialog */
1465 { nhStr("race_key"), race_key }, /* race selection */
1466 { nhStr("gend_key"), gend_key }, /* gender selection */
1467 { nhStr("algn_key"), algn_key }, /* alignment selection */
1468 { nhStr("X11_hangup"), X11_hangup }, /* delete of top-level */
1469 { nhStr("input"), map_input }, /* key input */
1470 { nhStr("scroll"), nh_keyscroll }, /* scrolling by keys */
1473 static XtResource resources[] = {
1474 { nhStr("slow"), nhStr("Slow"), XtRBoolean, sizeof(Boolean),
1475 XtOffset(AppResources *, slow), XtRString, nhStr("True") },
1476 { nhStr("fancy_status"), nhStr("Fancy_status"), XtRBoolean, sizeof(Boolean),
1477 XtOffset(AppResources *, fancy_status), XtRString, nhStr("True") },
1478 { nhStr("autofocus"), nhStr("AutoFocus"), XtRBoolean, sizeof(Boolean),
1479 XtOffset(AppResources *, autofocus), XtRString, nhStr("False") },
1480 { nhStr("message_line"), nhStr("Message_line"), XtRBoolean,
1481 sizeof(Boolean), XtOffset(AppResources *, message_line), XtRString,
1483 { nhStr("highlight_prompt"), nhStr("Highlight_prompt"), XtRBoolean,
1484 sizeof(Boolean), XtOffset(AppResources *, highlight_prompt), XtRString,
1486 { nhStr("double_tile_size"), nhStr("Double_tile_size"), XtRBoolean,
1487 sizeof(Boolean), XtOffset(AppResources *, double_tile_size), XtRString,
1489 { nhStr("tile_file"), nhStr("Tile_file"), XtRString, sizeof(String),
1490 XtOffset(AppResources *, tile_file), XtRString, nhStr("x11tiles") },
1492 { nhStr("tile_width"), nhStr("Tile_width"), XtRInt, sizeof(int),
1493 XtOffset(AppResources *, tile_width), XtRString, nhStr("32") },
1494 { nhStr("tile_height"), nhStr("Tile_height"), XtRInt, sizeof(int),
1495 XtOffset(AppResources *, tile_height), XtRString, nhStr("32") },
1497 { nhStr("icon"), nhStr("Icon"), XtRString, sizeof(String),
1498 XtOffset(AppResources *, icon), XtRString, nhStr("nh72") },
1499 { nhStr("message_lines"), nhStr("Message_lines"), XtRInt, sizeof(int),
1500 XtOffset(AppResources *, message_lines), XtRString, nhStr("12") },
1501 { nhStr("extcmd_height_delta"), nhStr("Extcmd_height_delta"),
1502 XtRInt, sizeof (int),
1503 XtOffset(AppResources *, extcmd_height_delta), XtRString, nhStr("0") },
1504 { nhStr("pet_mark_bitmap"), nhStr("Pet_mark_bitmap"), XtRString,
1505 sizeof(String), XtOffset(AppResources *, pet_mark_bitmap), XtRString,
1506 nhStr("pet_mark.xbm") },
1507 { nhStr("pet_mark_color"), nhStr("Pet_mark_color"), XtRPixel,
1508 sizeof(XtRPixel), XtOffset(AppResources *, pet_mark_color), XtRString,
1510 { nhStr("pilemark_bitmap"), nhStr("Pilemark_bitmap"), XtRString,
1511 sizeof(String), XtOffset(AppResources *, pilemark_bitmap), XtRString,
1512 nhStr("pilemark.xbm") },
1513 { nhStr("pilemark_color"), nhStr("Pilemark_color"), XtRPixel,
1514 sizeof(XtRPixel), XtOffset(AppResources *, pilemark_color), XtRString,
1516 #ifdef GRAPHIC_TOMBSTONE
1517 { nhStr("tombstone"), nhStr("Tombstone"), XtRString, sizeof(String),
1518 XtOffset(AppResources *, tombstone), XtRString, nhStr("rip.xpm") },
1519 { nhStr("tombtext_x"), nhStr("Tombtext_x"), XtRInt, sizeof(int),
1520 XtOffset(AppResources *, tombtext_x), XtRString, nhStr("155") },
1521 { nhStr("tombtext_y"), nhStr("Tombtext_y"), XtRInt, sizeof(int),
1522 XtOffset(AppResources *, tombtext_y), XtRString, nhStr("78") },
1523 { nhStr("tombtext_dx"), nhStr("Tombtext_dx"), XtRInt, sizeof(int),
1524 XtOffset(AppResources *, tombtext_dx), XtRString, nhStr("0") },
1525 { nhStr("tombtext_dy"), nhStr("Tombtext_dy"), XtRInt, sizeof(int),
1526 XtOffset(AppResources *, tombtext_dy), XtRString, nhStr("13") },
1531 panic_on_error(display, error)
1536 XGetErrorText(display, error->error_code, buf, BUFSZ);
1537 fprintf(stderr, "X Error: code %i (%s), request %i, minor %i, serial %lu\n",
1538 error->error_code, buf,
1539 error->request_code, error->minor_code,
1546 X11_init_nhwindows(argcp, argv)
1555 /* Init windows to nothing. */
1556 for (i = 0; i < MAX_WINDOWS; i++)
1557 window_list[i].type = NHW_NONE;
1559 /* add another option that can be set */
1560 set_wc_option_mod_status(WC_TILED_MAP, SET_IN_GAME);
1561 set_option_mod_status("mouse_support", SET_IN_GAME);
1563 load_default_resources(); /* create default_resource_data[] */
1566 * setuid hack: make sure that if nethack is setuid, to use real uid
1567 * when opening X11 connections, in case the user is using xauth, since
1568 * the "games" or whatever user probably doesn't have permission to open
1569 * a window on the user's display. This code is harmless if the binary
1570 * is not installed setuid. See include/system.h on compilation failures.
1573 (void) seteuid(getuid());
1575 XSetIOErrorHandler((XIOErrorHandler) hangup);
1578 XtSetLanguageProc(NULL, NULL, NULL);
1581 XSetIOErrorHandler((XIOErrorHandler) hangup);
1583 #ifdef INSTALLCOLORMAP
1584 XtSetTypeConverter(XtRString, XtRColormap, nhCvtStringToColormap,
1585 0, 0, XtCacheByDisplay, 0);
1588 XtSetArg(args[num_args], XtNallowShellResize, True); num_args++;
1589 XtSetArg(args[num_args], XtNtitle, "NetHack"); num_args++;
1592 toplevel = XtAppInitialize(&app_context, "NetHack", /* application */
1594 toplevel = XtAppInitialize(&app_context, "JNetHack", /* application */
1596 (XrmOptionDescList) 0, 0, /* options list */
1597 argcp, (String *) argv, /* command line */
1598 default_resource_data, /* fallback resources */
1599 (ArgList) args, num_args);
1600 XtOverrideTranslations(toplevel,
1601 XtParseTranslationTable("<Message>WM_PROTOCOLS: X11_hangup()"));
1603 /* We don't need to realize the top level widget. */
1605 old_error_handler = XSetErrorHandler(panic_on_error);
1608 /* add new color converter to deal with overused colormaps */
1609 XtSetTypeConverter(XtRString, XtRPixel, nhCvtStringToPixel,
1610 (XtConvertArgList) nhcolorConvertArgs,
1611 XtNumber(nhcolorConvertArgs), XtCacheByDisplay,
1613 #endif /* TEXTCOLOR */
1615 /* Register the actions mentioned in "actions". */
1616 XtAppAddActions(app_context, actions, XtNumber(actions));
1618 /* Get application-wide resources */
1619 XtGetApplicationResources(toplevel, (XtPointer) &appResources, resources,
1620 XtNumber(resources), (ArgList) 0, ZERO);
1622 /* Initialize other things. */
1623 init_standard_windows();
1625 /* Give the window manager an icon to use; toplevel must be realized. */
1626 if (appResources.icon && *appResources.icon) {
1627 struct icon_info *ip;
1629 for (ip = icon_data; ip->name; ip++)
1630 if (!strcmp(appResources.icon, ip->name)) {
1631 icon_pixmap = XCreateBitmapFromData(
1632 XtDisplay(toplevel), XtWindow(toplevel),
1633 (genericptr_t) ip->bits, ip->width, ip->height);
1634 if (icon_pixmap != None) {
1637 (void) memset((genericptr_t) &hints, 0, sizeof(XWMHints));
1638 hints.flags = IconPixmapHint;
1639 hints.icon_pixmap = icon_pixmap;
1640 XSetWMHints(XtDisplay(toplevel), XtWindow(toplevel),
1647 /* end of setuid hack: reset uid back to the "games" uid */
1648 (void) seteuid(savuid);
1650 x_inited = TRUE; /* X is now initialized */
1651 plsel_ask_name = FALSE;
1653 release_default_resources();
1655 /* Display the startup banner in the message window. */
1656 for (i = 1; i <= 4 + 2; ++i) /* (values beyond 4 yield blank lines) */
1657 X11_putstr(WIN_MESSAGE, 0, copyright_banner_line(i));
1665 X11_exit_nhwindows(dummy)
1668 extern Pixmap tile_pixmap; /* from winmap.c */
1672 /* explicitly free the icon and tile pixmaps */
1673 if (icon_pixmap != None) {
1674 XFreePixmap(XtDisplay(toplevel), icon_pixmap);
1677 if (tile_pixmap != None) {
1678 XFreePixmap(XtDisplay(toplevel), tile_pixmap);
1681 if (WIN_INVEN != WIN_ERR)
1682 X11_destroy_nhwindow(WIN_INVEN);
1683 if (WIN_STATUS != WIN_ERR)
1684 X11_destroy_nhwindow(WIN_STATUS);
1685 if (WIN_MAP != WIN_ERR)
1686 X11_destroy_nhwindow(WIN_MAP);
1687 if (WIN_MESSAGE != WIN_ERR)
1688 X11_destroy_nhwindow(WIN_MESSAGE);
1690 release_getline_widgets();
1691 release_yn_widgets();
1694 #ifdef X11_HANGUP_SIGNAL
1696 X11_sig(sig) /* Unix signal handler */
1699 XtNoticeSignal(X11_sig_id);
1704 X11_sig_cb(not_used, id)
1709 XClientMessageEvent *mesg;
1714 /* Set up a fake message to the event handler. */
1715 mesg = (XClientMessageEvent *) &event;
1716 mesg->type = ClientMessage;
1717 mesg->message_type = XA_STRING;
1720 XSendEvent(XtDisplay(window_list[WIN_MAP].w),
1721 XtWindow(window_list[WIN_MAP].w), False, NoEventMask,
1726 /* delay_output ----------------------------------------------------------- */
1729 * Timeout callback for delay_output(). Send a fake message to the map
1734 d_timeout(client_data, id)
1735 XtPointer client_data;
1739 XClientMessageEvent *mesg;
1744 /* Set up a fake message to the event handler. */
1745 mesg = (XClientMessageEvent *) &event;
1746 mesg->type = ClientMessage;
1747 mesg->message_type = XA_STRING;
1749 XSendEvent(XtDisplay(window_list[WIN_MAP].w),
1750 XtWindow(window_list[WIN_MAP].w), False, NoEventMask,
1755 * Delay for 50ms. This is not implemented asynch. Maybe later.
1756 * Start the timeout, then wait in the event loop. The timeout
1757 * function will send an event to the map window which will be waiting
1766 (void) XtAppAddTimeOut(app_context, 30L, d_timeout, (XtPointer) 0);
1768 /* The timeout function will enable the event loop exit. */
1769 (void) x_event(EXIT_ON_SENT_EVENT);
1772 /* X11_hangup ------------------------------------------------------------- */
1775 X11_hangup(w, event, params, num_params)
1779 Cardinal *num_params;
1786 hangup(1); /* 1 is commonly SIGHUP, but ignored anyway */
1787 exit_x_event = TRUE;
1790 /* askname ---------------------------------------------------------------- */
1793 askname_delete(w, event, params, num_params)
1797 Cardinal *num_params;
1804 (void) strcpy(plname, "Mumbles"); /* give them a name... ;-) */
1805 exit_x_event = TRUE;
1808 /* Callback for askname dialog widget. */
1811 askname_done(w, client_data, call_data)
1813 XtPointer client_data;
1814 XtPointer call_data;
1818 Widget dialog = (Widget) client_data;
1823 s = (char *) GetDialogResponse(dialog);
1831 /* Truncate name if necessary */
1832 if (len >= sizeof plname - 1)
1833 len = sizeof plname - 1;
1835 (void) strncpy(plname, s, len);
1839 nh_XtPopdown(XtParent(dialog));
1840 exit_x_event = TRUE;
1843 /* ask player for character's name to replace generic name "player" (or other
1844 values; see config.h) after 'nethack -u player' or OPTIONS=name:player */
1848 Widget popup, dialog;
1851 if (iflags.wc_player_selection == VIA_DIALOG) {
1852 /* X11_player_selection_dialog() handles name query */
1853 plsel_ask_name = TRUE;
1854 iflags.defer_plname = TRUE;
1856 } /* else iflags.wc_player_selection == VIA_PROMPTS */
1858 XtSetArg(args[0], XtNallowShellResize, True);
1860 popup = XtCreatePopupShell("askname", transientShellWidgetClass, toplevel,
1862 XtOverrideTranslations(popup,
1863 XtParseTranslationTable("<Message>WM_PROTOCOLS: askname_delete()"));
1865 dialog = CreateDialog(popup, nhStr("dialog"), askname_done,
1866 (XtCallbackProc) 0);
1868 SetDialogPrompt(dialog, nhStr("What is your name?")); /* set prompt */
1869 SetDialogResponse(dialog, plname, PL_NSIZ); /* set default answer */
1871 XtRealizeWidget(popup);
1872 positionpopup(popup, TRUE); /* center,bottom */
1874 nh_XtPopup(popup, (int) XtGrabExclusive, dialog);
1876 /* The callback will enable the event loop exit. */
1877 (void) x_event(EXIT_ON_EXIT);
1879 XtDestroyWidget(dialog);
1880 XtDestroyWidget(popup);
1883 /* getline ---------------------------------------------------------------- */
1884 /* This uses Tim Theisen's dialog widget set (from GhostView). */
1886 static Widget getline_popup, getline_dialog;
1888 #define CANCEL_STR "\033"
1889 static char *getline_input;
1891 /* Callback for getline dialog widget. */
1894 done_button(w, client_data, call_data)
1896 XtPointer client_data;
1897 XtPointer call_data;
1901 Widget dialog = (Widget) client_data;
1906 s = (char *) GetDialogResponse(dialog);
1909 /* Truncate input if necessary */
1913 (void) strncpy(getline_input, s, len);
1914 getline_input[len] = '\0';
1917 nh_XtPopdown(XtParent(dialog));
1918 exit_x_event = TRUE;
1923 getline_delete(w, event, params, num_params)
1927 Cardinal *num_params;
1933 Strcpy(getline_input, CANCEL_STR);
1935 exit_x_event = TRUE;
1938 /* Callback for getline dialog widget. */
1941 abort_button(w, client_data, call_data)
1943 XtPointer client_data;
1944 XtPointer call_data;
1946 Widget dialog = (Widget) client_data;
1951 Strcpy(getline_input, CANCEL_STR);
1952 nh_XtPopdown(XtParent(dialog));
1953 exit_x_event = TRUE;
1957 release_getline_widgets()
1960 XtDestroyWidget(getline_dialog), getline_dialog = (Widget) 0;
1962 XtDestroyWidget(getline_popup), getline_popup = (Widget) 0;
1966 X11_getlin(question, input)
1967 const char *question;
1970 getline_input = input;
1973 if (!getline_popup) {
1976 XtSetArg(args[0], XtNallowShellResize, True);
1978 getline_popup = XtCreatePopupShell("getline",
1979 transientShellWidgetClass,
1980 toplevel, args, ONE);
1981 XtOverrideTranslations(getline_popup,
1982 XtParseTranslationTable(
1983 "<Message>WM_PROTOCOLS: getline_delete()"));
1985 getline_dialog = CreateDialog(getline_popup, nhStr("dialog"),
1986 done_button, abort_button);
1988 XtRealizeWidget(getline_popup);
1989 XSetWMProtocols(XtDisplay(getline_popup), XtWindow(getline_popup),
1990 &wm_delete_window, 1);
1992 SetDialogPrompt(getline_dialog, (String) question); /* set prompt */
1993 /* 60: make the answer widget be wide enough to hold 60 characters,
1994 or the length of the prompt string, whichever is bigger. User's
1995 response can be longer--when limit is reached, value-so-far will
1996 slide left hiding some chars at the beginning of the response but
1997 making room to type more. [Prior to 3.6.1, width wasn't specifiable
1998 and answer box always got sized to match the width of the prompt.] */
2000 SetDialogResponse(getline_dialog, input, 60); /* set default answer */
2002 SetDialogResponse(getline_dialog, nhStr(""), 60); /* set default answer */
2004 positionpopup(getline_popup, TRUE); /* center,bottom */
2006 nh_XtPopup(getline_popup, (int) XtGrabExclusive, getline_dialog);
2008 /* The callback will enable the event loop exit. */
2009 (void) x_event(EXIT_ON_EXIT);
2012 /* Display file ----------------------------------------------------------- */
2014 /* uses a menu (with no selectors specified) rather than a text window
2015 to allow previous_page and first_menu actions to move backwards */
2017 X11_display_file(str, complain)
2025 menu_item *menu_list;
2030 XFontSetExtents *extent;
2033 /* Use the port-independent file opener to see if the file exists. */
2034 fp = dlb_fopen(str, RDTMODE);
2037 pline("Cannot open %s. Sorry.", str);
2038 return; /* it doesn't exist, ignore */
2041 newwin = X11_create_nhwindow(NHW_MENU);
2042 wp = &window_list[newwin];
2043 X11_start_menu(newwin);
2046 while (dlb_fgets(line, LLEN, fp)) {
2047 X11_add_menu(newwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2048 line, MENU_UNSELECTED);
2050 (void) dlb_fclose(fp);
2052 /* show file name as the window title */
2054 wp->title = dupstr(str);
2056 wp->menu_information->permi = FALSE;
2057 wp->menu_information->disable_mcolors = TRUE;
2058 (void) X11_select_menu(newwin, PICK_NONE, &menu_list);
2059 X11_destroy_nhwindow(newwin);
2062 /* yn_function ------------------------------------------------------------ */
2063 /* (not threaded) */
2065 static const char *yn_quitchars = " \n\r";
2066 static const char *yn_choices; /* string of acceptable input */
2068 static char yn_return; /* return value */
2069 static char yn_esc_map; /* ESC maps to this char. */
2070 static Widget yn_popup; /* popup for the yn fuction (created once) */
2071 static Widget yn_label; /* label for yn function (created once) */
2072 static boolean yn_getting_num; /* TRUE if accepting digits */
2073 static boolean yn_preserve_case; /* default is to force yn to lower case */
2074 static int yn_ndigits; /* digit count */
2075 static long yn_val; /* accumulated value */
2077 static const char yn_translations[] = "#override\n\
2081 * Convert the given key event into a character. If the key maps to
2082 * more than one character only the first is returned. If there is
2083 * no conversion (i.e. just the CTRL key hit) a NUL is returned.
2086 key_event_to_char(key)
2089 char keystring[MAX_KEY_STRING];
2091 boolean meta = !!(key->state & Mod1Mask);
2093 nbytes = XLookupString(key, keystring, MAX_KEY_STRING, (KeySym *) 0,
2094 (XComposeStatus *) 0);
2096 /* Modifier keys return a zero lengh string when pressed. */
2100 return (char) (((int) keystring[0]) + (meta ? 0x80 : 0));
2104 * Called when we get a WM_DELETE_WINDOW event on a yn window.
2108 yn_delete(w, event, params, num_params)
2112 Cardinal *num_params;
2119 yn_getting_num = FALSE;
2120 /* Only use yn_esc_map if we have choices. Otherwise, return ESC. */
2121 yn_return = yn_choices ? yn_esc_map : '\033';
2122 exit_x_event = TRUE; /* exit our event handler */
2126 * Called when we get a key press event on a yn window.
2130 yn_key(w, event, params, num_params)
2134 Cardinal *num_params;
2138 if (appResources.slow && !input_func)
2139 map_input(w, event, params, num_params);
2141 ch = key_event_to_char((XKeyEvent *) event);
2143 if (ch == '\0') { /* don't accept nul char or modifier event */
2148 if (!yn_choices) { /* accept any input */
2151 if (!yn_preserve_case)
2152 ch = lowc(ch); /* move to lower case */
2155 yn_getting_num = FALSE;
2156 yn_return = yn_esc_map;
2157 } else if (index(yn_quitchars, ch)) {
2159 } else if (index(yn_choices, ch)) {
2161 if (yn_getting_num) { /* don't select again */
2165 yn_getting_num = TRUE;
2168 return; /* wait for more input */
2172 yn_getting_num = FALSE;
2174 if (yn_getting_num) {
2177 yn_val = (yn_val * 10) + (long) (ch - '0');
2178 return; /* wait for more input */
2180 if (yn_ndigits && (ch == '\b' || ch == 127 /*DEL*/)) {
2182 yn_val = yn_val / 10;
2183 return; /* wait for more input */
2186 X11_nhbell(); /* no match */
2190 if (yn_getting_num) {
2194 yn_number = yn_val; /* assign global */
2197 exit_x_event = TRUE; /* exit our event handler */
2200 /* called at exit time */
2202 release_yn_widgets()
2205 XtDestroyWidget(yn_label), yn_label = (Widget) 0;
2207 XtDestroyWidget(yn_popup), yn_popup = (Widget) 0;
2210 /* X11-specific edition of yn_function(), the routine called by the core
2211 to show a prompt and get a single keystroke answer, often 'y' vs 'n' */
2213 X11_yn_function(ques, choices, def)
2215 const char *choices; /* string of possible response chars; any char if Null */
2216 char def; /* default response if user hits <space> or <return> */
2222 yn_choices = choices; /* set up globals for callback to use */
2224 yn_preserve_case = !choices; /* preserve case when an arbitrary
2225 response is allowed */
2228 * This is sort of a kludge. There are quite a few places in the main
2229 * nethack code where a pline containing information is followed by a
2230 * call to yn_function(). There is no flush of the message window
2231 * (it is implicit in the tty window port), so the line never shows
2232 * up for us! Solution: do our own flush.
2234 if (WIN_MESSAGE != WIN_ERR)
2235 display_message_window(&window_list[WIN_MESSAGE]);
2238 char *cb, choicebuf[QBUFSZ];
2240 Strcpy(choicebuf, choices); /* anything beyond <esc> is hidden */
2241 /* default when choices are present is to force yn answer to
2242 lowercase unless one or more choices are explicitly uppercase;
2243 check this before stripping the hidden choices */
2244 for (cb = choicebuf; *cb; ++cb)
2245 if ('A' <= *cb && *cb <= 'Z') {
2246 yn_preserve_case = TRUE;
2249 if ((cb = index(choicebuf, '\033')) != 0)
2251 /* ques [choices] (def) */
2252 if ((int) (1 + strlen(ques) + 2 + strlen(choicebuf) + 4) >= BUFSZ)
2253 panic("X11_yn_function: question too long");
2254 (void) strncpy(buf, ques, QBUFSZ - 1);
2255 buf[QBUFSZ - 1] = '\0';
2256 Sprintf(eos(buf), " [%s]", choicebuf);
2258 Sprintf(eos(buf), " (%c)", def);
2261 /* escape maps to 'q' or 'n' or default, in that order */
2262 yn_esc_map = (index(choices, 'q') ? 'q'
2263 : index(choices, 'n') ? 'n'
2266 if ((int) (1 + strlen(ques) + 1) >= BUFSZ)
2267 panic("X11_yn_function: question too long");
2273 * The 'slow' resource is misleadingly named. When it is True, we
2274 * re-use the same one-line widget above the map for all yn prompt
2275 * responses instead of re-using a popup widget for each one. It's
2276 * crucial for client/server configs where the server is slow putting
2277 * up a popup or requires click-to-focus to respond to the popup, but
2278 * is also useful for players who just want to be able to look at the
2279 * same location to read questions they're being asked to answer.
2282 if (appResources.slow) {
2284 * 'slow': the yn_label widget was created when the map and
2285 * status widgets were, and is positioned between them. It
2286 * will persist until end of game. All we need to do for
2287 * yn_function is direct keystroke input to the yn response
2288 * handler and reset its label to be the prompt text (below).
2290 input_func = yn_key;
2291 highlight_yn(FALSE); /* expose yn_label as separate from map */
2292 } else if (!yn_label) {
2294 * Not 'slow'; create a persistent widget that will be popped up
2295 * as needed, then down again, and last until end of game. The
2296 * associated yn_label widget is used to track whether it exists.
2298 XtSetArg(args[0], XtNallowShellResize, True);
2299 yn_popup = XtCreatePopupShell("query", transientShellWidgetClass,
2300 toplevel, args, ONE);
2301 XtOverrideTranslations(yn_popup,
2302 XtParseTranslationTable("<Message>WM_PROTOCOLS: yn_delete()"));
2305 XtSetArg(args[num_args], XtNtranslations,
2306 XtParseTranslationTable(yn_translations)); num_args++;
2307 #if defined(X11R6) && defined(XI18N)
2308 XtSetArg(args[num_args], XtNinternational, True); num_args++;
2310 yn_label = XtCreateManagedWidget("yn_label", labelWidgetClass,
2311 yn_popup, args, num_args);
2313 XtRealizeWidget(yn_popup);
2314 XSetWMProtocols(XtDisplay(yn_popup), XtWindow(yn_popup),
2315 &wm_delete_window, 1);
2318 /* set the label of the yn widget to be the prompt text */
2320 XtSetArg(args[num_args], XtNlabel, buf); num_args++;
2321 XtSetValues(yn_label, args, num_args);
2323 if (!appResources.slow) {
2325 * Due to some kind of weird bug in the X11R4 and X11R5 shell, we
2326 * need to set the label twice to get the size to change.
2329 XtSetArg(args[num_args], XtNlabel, buf); num_args++;
2330 XtSetValues(yn_label, args, num_args);
2332 positionpopup(yn_popup, TRUE);
2333 nh_XtPopup(yn_popup, (int) XtGrabExclusive, yn_label);
2336 yn_getting_num = FALSE;
2337 (void) x_event(EXIT_ON_EXIT); /* get keystroke(s) */
2339 if (appResources.slow) {
2340 /* keystrokes now belong to the map */
2342 /* erase the prompt */
2344 XtSetArg(args[num_args], XtNlabel, " "); num_args++;
2345 XtSetValues(yn_label, args, num_args);
2346 highlight_yn(FALSE); /* disguise yn_label as part of map */
2348 nh_XtPopdown(yn_popup); /* this removes the event grab */
2351 pline("%s%c", buf, (yn_return != '\033') ? yn_return : '\0');
2356 /* used when processing window-capability-specific run-time options;
2357 we support toggling tiles on and off via iflags.wc_tiled_map */
2359 X11_preference_update(pref)
2362 if (!strcmp(pref, "tiled_map")) {
2363 if (WIN_MAP != WIN_ERR)
2364 display_map_window(&window_list[WIN_MAP]);
2368 /* End global functions =================================================== */
2371 * Before we wait for input via nhgetch() and nh_poskey(), we need to
2372 * do some pre-processing.
2375 input_event(exit_condition)
2378 if (appResources.fancy_status && WIN_STATUS != WIN_ERR) /* hilighting on the fancy status window */
2379 check_turn_events();
2380 if (WIN_MAP != WIN_ERR) /* make sure cursor is not clipped */
2381 check_cursor_visibility(&window_list[WIN_MAP]);
2382 if (WIN_MESSAGE != WIN_ERR) /* reset pause line */
2383 set_last_pause(&window_list[WIN_MESSAGE]);
2385 return x_event(exit_condition);
2390 msgkey(w, data, event)
2400 map_input(window_list[WIN_MAP].w, event, (String *) 0, &num);
2403 /* only called for autofocus */
2406 win_visible(w, data, event, flag)
2408 XtPointer data; /* client_data not used */
2410 Boolean *flag; /* continue_to_dispatch flag not used */
2412 XVisibilityEvent *vis_event = (XVisibilityEvent *) event;
2417 if (vis_event->state != VisibilityFullyObscured) {
2418 /* one-time operation; cancel ourself */
2419 XtRemoveEventHandler(toplevel, VisibilityChangeMask, False,
2420 win_visible, (XtPointer) 0);
2421 /* grab initial input focus */
2422 XSetInputFocus(XtDisplay(w), XtWindow(w), RevertToNone, CurrentTime);
2426 /* if 'slow' and 'highlight_prompt', set the yn_label widget to look like
2427 part of the map when idle or to invert background and foreground when
2428 a prompt is active */
2433 struct xwindow *xmap;
2435 if (!appResources.slow || !appResources.highlight_prompt)
2438 /* first time through, WIN_MAP isn't fully initiialized yet */
2439 xmap = ((map_win != WIN_ERR) ? &window_list[map_win]
2440 : (WIN_MAP != WIN_ERR) ? &window_list[WIN_MAP] : 0);
2445 unsigned long fg_bg = (GCForeground | GCBackground);
2446 GC gc = (xmap->map_information->is_tile
2447 ? xmap->map_information->tile_map.white_gc
2448 : xmap->map_information->text_map.copy_gc);
2450 (void) memset((genericptr_t) &vals, 0, sizeof vals);
2451 if (XGetGCValues(XtDisplay(xmap->w), gc, fg_bg, &vals)) {
2452 XtSetArg(args[0], XtNforeground, vals.foreground);
2453 XtSetArg(args[1], XtNbackground, vals.background);
2454 XtSetValues(yn_label, args, TWO);
2457 swap_fg_bg(yn_label);
2461 * Set up the playing console. This has three major parts: the
2462 * message window, the map, and the status window.
2464 * For configs specifying the 'slow' resource, the yn_label widget
2465 * is placed above the map and below the message window. Prompts
2466 * requiring a single character response are displayed there rather
2467 * than using a popup.
2470 init_standard_windows()
2472 Widget form, message_viewport, map_viewport, status;
2475 Dimension message_vp_width, map_vp_width, status_width, max_width;
2476 int map_vp_hd, status_hd;
2480 XtSetArg(args[num_args], XtNallowShellResize, True); num_args++;
2482 form = XtCreateManagedWidget("nethack", panedWidgetClass, toplevel, args,
2485 form = XtCreateManagedWidget("jnethack", panedWidgetClass, toplevel, args,
2489 XtAddEventHandler(form, KeyPressMask, False, (XtEventHandler) msgkey,
2492 if (appResources.autofocus)
2493 XtAddEventHandler(toplevel, VisibilityChangeMask, False, win_visible,
2497 * Create message window.
2499 WIN_MESSAGE = message_win = find_free_window();
2500 wp = &window_list[message_win];
2501 wp->cursx = wp->cursy = wp->pixel_width = wp->pixel_height = 0;
2502 wp->popup = (Widget) 0;
2503 create_message_window(wp, FALSE, form);
2504 message_viewport = XtParent(wp->w);
2506 /* Tell the form that contains it that resizes are OK. */
2508 XtSetArg(args[num_args], nhStr(XtNresizable), True); num_args++;
2509 XtSetArg(args[num_args], nhStr(XtNleft), XtChainLeft); num_args++;
2510 XtSetArg(args[num_args], nhStr(XtNtop), XtChainTop); num_args++;
2511 XtSetValues(message_viewport, args, num_args);
2513 if (appResources.slow) {
2515 XtSetArg(args[num_args], XtNtranslations,
2516 XtParseTranslationTable(yn_translations)); num_args++;
2517 #if defined(X11R6) && defined(XI18N)
2518 XtSetArg(args[num_args], XtNinternational, True);
2521 yn_label = XtCreateManagedWidget("yn_label", labelWidgetClass, form,
2524 XtSetArg(args[num_args], nhStr(XtNfromVert), message_viewport);
2526 XtSetArg(args[num_args], nhStr(XtNjustify), XtJustifyLeft);
2528 XtSetArg(args[num_args], nhStr(XtNresizable), True); num_args++;
2529 XtSetArg(args[num_args], nhStr(XtNlabel), " "); num_args++;
2530 XtSetValues(yn_label, args, num_args);
2534 * Create the map window & viewport and chain the viewport beneath the
2537 map_win = find_free_window();
2538 wp = &window_list[map_win];
2539 wp->cursx = wp->cursy = wp->pixel_width = wp->pixel_height = 0;
2540 wp->popup = (Widget) 0;
2541 create_map_window(wp, FALSE, form);
2542 map_viewport = XtParent(wp->w);
2544 /* Chain beneath message_viewport or yn window. */
2546 if (appResources.slow) {
2547 XtSetArg(args[num_args], nhStr(XtNfromVert), yn_label); num_args++;
2549 XtSetArg(args[num_args], nhStr(XtNfromVert), message_viewport);
2552 XtSetArg(args[num_args], nhStr(XtNbottom), XtChainBottom); num_args++;
2553 XtSetValues(map_viewport, args, num_args);
2555 /* Create the status window, with the form as it's parent. */
2556 status_win = find_free_window();
2557 wp = &window_list[status_win];
2558 wp->cursx = wp->cursy = wp->pixel_width = wp->pixel_height = 0;
2559 wp->popup = (Widget) 0;
2560 create_status_window(wp, FALSE, form);
2564 * Chain the status window beneath the viewport. Mark the left and right
2565 * edges so that they stay a fixed distance from the left edge of the
2566 * parent, as well as the top and bottom edges so that they stay a fixed
2567 * distance from the bottom of the parent. We do this so that the status
2568 * will never expand or contract.
2571 XtSetArg(args[num_args], nhStr(XtNfromVert), map_viewport); num_args++;
2572 XtSetArg(args[num_args], nhStr(XtNleft), XtChainLeft); num_args++;
2573 XtSetArg(args[num_args], nhStr(XtNright), XtChainLeft); num_args++;
2574 XtSetArg(args[num_args], nhStr(XtNtop), XtChainBottom); num_args++;
2575 XtSetArg(args[num_args], nhStr(XtNbottom), XtChainBottom); num_args++;
2576 XtSetValues(status, args, num_args);
2579 * Realize the popup so that the status widget knows it's size.
2581 * If we unset MappedWhenManaged then the DECwindow driver doesn't
2582 * attach the nethack toplevel to the highest virtual root window.
2585 /* XtSetMappedWhenManaged(toplevel, False); */
2586 XtRealizeWidget(toplevel);
2587 wm_delete_window = XInternAtom(XtDisplay(toplevel),
2588 "WM_DELETE_WINDOW", False);
2589 XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
2590 &wm_delete_window, 1);
2593 * Resize to at most full-screen.
2596 #define TITLEBAR_SPACE 18 /* Leave SOME screen for window decorations */
2598 int screen_width = WidthOfScreen(XtScreen(wp->w));
2599 int screen_height = HeightOfScreen(XtScreen(wp->w)) - TITLEBAR_SPACE;
2600 Dimension form_width, form_height;
2602 XtSetArg(args[0], XtNwidth, &form_width);
2603 XtSetArg(args[1], XtNheight, &form_height);
2604 XtGetValues(toplevel, args, TWO);
2606 if (form_width > screen_width || form_height > screen_height) {
2607 XtSetArg(args[0], XtNwidth, min(form_width, screen_width));
2608 XtSetArg(args[1], XtNheight, min(form_height, screen_height));
2609 XtSetValues(toplevel, args, TWO);
2610 XMoveWindow(XtDisplay(toplevel), XtWindow(toplevel), 0,
2613 #undef TITLEBAR_SPACE
2616 post_process_tiles(); /* after toplevel is realized */
2619 * Now get the default widths of the windows.
2621 XtSetArg(args[0], nhStr(XtNwidth), &message_vp_width);
2622 XtGetValues(message_viewport, args, ONE);
2623 XtSetArg(args[0], nhStr(XtNwidth), &map_vp_width);
2624 XtSetArg(args[1], nhStr(XtNhorizDistance), &map_vp_hd);
2625 XtGetValues(map_viewport, args, TWO);
2626 XtSetArg(args[0], nhStr(XtNwidth), &status_width);
2627 XtSetArg(args[1], nhStr(XtNhorizDistance), &status_hd);
2628 XtGetValues(status, args, TWO);
2631 * Adjust positions and sizes. The message viewport widens out to the
2632 * widest width. Both the map and status are centered by adjusting
2633 * their horizDistance.
2635 if (map_vp_width < status_width || map_vp_width < message_vp_width) {
2636 if (status_width > message_vp_width) {
2637 XtSetArg(args[0], nhStr(XtNwidth), status_width);
2638 XtSetValues(message_viewport, args, ONE);
2639 max_width = status_width;
2641 max_width = message_vp_width;
2643 XtSetArg(args[0], nhStr(XtNhorizDistance),
2644 map_vp_hd + ((int) (max_width - map_vp_width) / 2));
2645 XtSetValues(map_viewport, args, ONE);
2647 } else { /* map is widest */
2648 XtSetArg(args[0], nhStr(XtNwidth), map_vp_width);
2649 XtSetValues(message_viewport, args, ONE);
2652 * Clear all data values on the fancy status widget so that the values
2653 * used for spacing don't appear. This needs to be called some time
2654 * after the fancy status widget is realized (above, with the game popup),
2655 * but before it is popped up.
2657 if (appResources.fancy_status)
2660 * Set the map size to its standard size. As with the message window
2661 * above, the map window needs to be set to its constrained size until
2662 * its parent (the viewport widget) was realized.
2664 * Move the message window's slider to the bottom.
2666 set_map_size(&window_list[map_win], COLNO, ROWNO);
2667 set_message_slider(&window_list[message_win]);
2669 /* attempt to catch fatal X11 errors before the program quits */
2670 (void) XtAppSetErrorHandler(app_context, (XtErrorHandler) hangup);
2672 highlight_yn(TRUE); /* switch foreground and background */
2674 /* We can now print to the message window. */
2675 iflags.window_inited = 1;
2679 nh_XtPopup(w, g, childwid)
2680 Widget w; /* widget */
2681 int g; /* type of grab */
2682 Widget childwid; /* child to receive focus (can be None) */
2684 XtPopup(w, (XtGrabKind) g);
2685 XSetWMProtocols(XtDisplay(w), XtWindow(w), &wm_delete_window, 1);
2686 if (appResources.autofocus)
2687 XtSetKeyboardFocus(toplevel, childwid);
2695 if (appResources.autofocus)
2696 XtSetKeyboardFocus(toplevel, None);
2707 /* With the OpenWindows 3.0 libraries and the SunOS 4.1.2 ld, these
2708 * two routines will not be found when linking. An apparently correct
2709 * executable is produced, along with nasty messages and a failure code
2710 * returned to make. The routines are in the static libXmu.a and
2711 * libXmu.sa.4.0, but not in libXmu.so.4.0. Rather than fiddle with
2712 * static linking, we do this.
2714 if (rn2(2) > 2) { /* i.e., FALSE that an optimizer probably can't find */
2715 get_wmShellWidgetClass();
2716 get_applicationShellWidgetClass();
2723 find_scrollbars(w, horiz, vert)
2725 Widget *horiz, *vert;
2729 *horiz = XtNameToWidget(w, "*horizontal");
2730 *vert = XtNameToWidget(w, "*vertical");
2732 } while (!*horiz && !*vert && w);
2737 * Scroll a viewport, using standard NH 1,2,3,4,6,7,8,9 directions.
2741 nh_keyscroll(viewport, event, params, num_params)
2745 Cardinal *num_params;
2748 Widget horiz_sb = (Widget) 0, vert_sb = (Widget) 0;
2752 Cardinal in_nparams = (num_params ? *num_params : 0);
2756 if (in_nparams != 1)
2757 return; /* bad translation */
2759 direction = atoi(params[0]);
2761 find_scrollbars(viewport, &horiz_sb, &vert_sb);
2763 #define H_DELTA 0.25 /* distance of horiz shift */
2764 /* vert shift is half of curr distance */
2765 /* The V_DELTA is 1/2 the value of shown. */
2768 XtSetArg(arg[0], nhStr(XtNshown), &shown);
2769 XtSetArg(arg[1], nhStr(XtNtopOfThumb), &top);
2770 XtGetValues(horiz_sb, arg, TWO);
2774 switch (direction) {
2786 if (top + shown > 1.0)
2794 XtCallCallbacks(horiz_sb, XtNjumpProc, &top);
2799 XtSetArg(arg[0], nhStr(XtNshown), &shown);
2800 XtSetArg(arg[1], nhStr(XtNtopOfThumb), &top);
2801 XtGetValues(vert_sb, arg, TWO);
2805 switch (direction) {
2817 if (top + shown > 1.0)
2825 XtCallCallbacks(vert_sb, XtNjumpProc, &top);