1 /* NetHack 3.6 wintext.c $NHDT-Date: 1450453309 2015/12/18 15:41:49 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.15 $ */
2 /* Copyright (c) Dean Luick, 1992 */
3 /* NetHack may be freely redistributed. See license for details. */
6 * File for dealing with text windows.
8 * + No global functions.
12 ** Japanese version Copyright (C) Issei Numata, 1994-1999
13 ** changing point is marked `JP' (94/6/7) or XI18N (96/7/19)
14 ** For 3.4, Copyright (c) Kentaro Shirakata, 2002-2003
15 ** JNetHack may be freely redistributed. See license for details.
19 #define PRESERVE_NO_SYSV /* X11 include files may define SYSV */
22 #include <X11/Intrinsic.h>
23 #include <X11/StringDefs.h>
24 #include <X11/Shell.h>
26 #include <X11/Xaw/Form.h>
27 #include <X11/Xaw/AsciiText.h>
28 #include <X11/Xaw/Cardinals.h>
29 #include <X11/Xatom.h>
31 #ifdef PRESERVE_NO_SYSV
35 #undef PRESERVE_NO_SYSV
42 #ifdef GRAPHIC_TOMBSTONE
46 #ifdef INSTALLCOLORMAP
50 #define TRANSIENT_TEXT /* text window is a transient window (no positioning) \
53 static const char text_translations[] = "#override\n\
54 <BtnDown>: dismiss_text()\n\
55 <Key>: key_dismiss_text()";
57 #ifdef GRAPHIC_TOMBSTONE
58 static const char rip_translations[] = "#override\n\
59 <BtnDown>: rip_dismiss_text()\n\
60 <Key>: rip_dismiss_text()";
62 static Widget FDECL(create_ripout_widget, (Widget));
67 delete_text(w, event, params, num_params)
74 struct text_info_t *text_info;
81 text_info = wp->text_information;
83 nh_XtPopdown(wp->popup);
85 if (text_info->blocked) {
87 } else if (text_info->destroy_on_ack) {
88 destroy_text_window(wp);
93 * Callback used for all text windows. The window is poped down on any key
94 * or button down event. It is destroyed if the main nethack code is done
99 dismiss_text(w, event, params, num_params)
103 Cardinal *num_params;
106 struct text_info_t *text_info;
113 text_info = wp->text_information;
115 nh_XtPopdown(wp->popup);
117 if (text_info->blocked) {
119 } else if (text_info->destroy_on_ack) {
120 destroy_text_window(wp);
124 /* Dismiss when a non-modifier key pressed. */
126 key_dismiss_text(w, event, params, num_params)
130 Cardinal *num_params;
132 char ch = key_event_to_char((XKeyEvent *) event);
134 dismiss_text(w, event, params, num_params);
137 #ifdef GRAPHIC_TOMBSTONE
138 /* Dismiss from clicking on rip image. */
140 rip_dismiss_text(w, event, params, num_params)
144 Cardinal *num_params;
146 dismiss_text(XtParent(w), event, params, num_params);
152 add_to_text_window(wp, attr, str)
154 int attr; /* currently unused */
157 struct text_info_t *text_info = wp->text_information;
162 append_text_buffer(&text_info->text, str, FALSE);
164 /* Calculate text width and save longest line */
166 width = XTextWidth(text_info->fs, str, (int) strlen(str));
168 width = XmbTextEscapement(text_info->fontset, str, (int) strlen(str));
170 if (width > text_info->max_width)
171 text_info->max_width = width;
175 display_text_window(wp, blocking)
179 struct text_info_t *text_info;
182 Dimension width, height, font_height;
186 XFontSetExtents *extent;
189 text_info = wp->text_information;
190 width = text_info->max_width + text_info->extra_width;
191 text_info->blocked = blocking;
192 text_info->destroy_on_ack = FALSE;
193 font_height = nhFontHeight(wp->w);
197 extent = XExtentsOfFontSet(text_info->fontset);
201 * Calculate the number of lines to use. First, find the number of
202 * lines that would fit on the screen. Next, remove four of these
203 * lines to give room for a possible window manager titlebar (some
204 * wm's put a titlebar on transient windows). Make sure we have
205 * _some_ lines. Finally, use the number of lines in the text if
206 * there are fewer than the max.
211 (XtScreen(wp->w)->height - text_info->extra_height) / font_height;
214 (XtScreen(wp->w)->height - text_info->extra_height) / extent->max_logical_extent.height;
218 if (nlines > text_info->text.num_lines)
219 nlines = text_info->text.num_lines;
225 height = nlines * font_height + text_info->extra_height;
227 height = (nlines * extent->max_logical_extent.height) + text_info->extra_height;
232 if (nlines < text_info->text.num_lines) {
233 /* add on width of scrollbar. Really should look this up,
234 * but can't until the window is realized. Chicken-and-egg problem.
239 #ifdef GRAPHIC_TOMBSTONE
240 if (text_info->is_rip) {
241 Widget rip = create_ripout_widget(XtParent(wp->w));
242 XtSetArg(args[num_args], XtNfromVert, rip);
248 > (Dimension) XtScreen(wp->w)->width) { /* too wide for screen */
249 /* Back off some amount - we really need to back off the scrollbar */
250 /* width plus some extra. */
251 width = XtScreen(wp->w)->width - 20;
253 XtSetArg(args[num_args], XtNstring, text_info->text.text);
255 XtSetArg(args[num_args], XtNwidth, width);
257 XtSetArg(args[num_args], XtNheight, height);
259 XtSetValues(wp->w, args, num_args);
261 #ifdef TRANSIENT_TEXT
262 XtRealizeWidget(wp->popup);
263 XSetWMProtocols(XtDisplay(wp->popup), XtWindow(wp->popup),
264 &wm_delete_window, 1);
265 positionpopup(wp->popup, FALSE);
268 nh_XtPopup(wp->popup, (int) XtGrabNone, wp->w);
270 /* Kludge alert. Scrollbars are not sized correctly by the Text widget */
271 /* if added before the window is displayed, so do it afterward. */
273 if (nlines < text_info->text.num_lines) { /* add vert scrollbar */
274 XtSetArg(args[num_args], nhStr(XtNscrollVertical),
275 XawtextScrollAlways);
278 if (width >= (Dimension)(XtScreen(wp->w)->width - 20)) { /* too wide */
279 XtSetArg(args[num_args], nhStr(XtNscrollHorizontal),
280 XawtextScrollAlways);
284 XtSetValues(wp->w, args, num_args);
286 /* We want the user to acknowlege. */
288 (void) x_event(EXIT_ON_EXIT);
289 nh_XtPopdown(wp->popup);
294 create_text_window(wp)
297 struct text_info_t *text_info;
300 Position top_margin, bottom_margin, left_margin, right_margin;
305 wp->text_information = text_info =
306 (struct text_info_t *) alloc(sizeof(struct text_info_t));
308 init_text_buffer(&text_info->text);
309 text_info->max_width = 0;
310 text_info->extra_width = 0;
311 text_info->extra_height = 0;
312 text_info->blocked = FALSE;
313 text_info->destroy_on_ack = TRUE; /* Ok to destroy before display */
314 #ifdef GRAPHIC_TOMBSTONE
315 text_info->is_rip = FALSE;
319 XtSetArg(args[num_args], XtNallowShellResize, True);
321 XtSetArg(args[num_args], XtNtranslations,
322 XtParseTranslationTable(text_translations));
325 #ifdef TRANSIENT_TEXT
326 wp->popup = XtCreatePopupShell("text", transientShellWidgetClass,
327 toplevel, args, num_args);
329 wp->popup = XtCreatePopupShell("text", topLevelShellWidgetClass, toplevel,
332 XtOverrideTranslations(
334 XtParseTranslationTable("<Message>WM_PROTOCOLS: delete_text()"));
337 XtSetArg(args[num_args], XtNallowShellResize, True);
339 form = XtCreateManagedWidget("form", formWidgetClass, wp->popup, args,
343 XtSetArg(args[num_args], nhStr(XtNdisplayCaret), False);
345 XtSetArg(args[num_args], XtNresize, XawtextResizeBoth);
347 XtSetArg(args[num_args], XtNtranslations,
348 XtParseTranslationTable(text_translations));
351 #if defined(X11R6) && defined(XI18N)
352 XtSetArg(args[num_args], XtNinternational, True); num_args++;
355 wp->w = XtCreateManagedWidget(killer.name[0] && WIN_MAP == WIN_ERR
357 : "text_text", /* name */
358 asciiTextWidgetClass,
359 form, /* parent widget */
360 args, /* set some values */
361 num_args); /* number of values to set */
363 /* Get the font and margin information. */
366 XtSetArg(args[num_args], XtNfont, &text_info->fs);
368 XtSetArg(args[num_args], XtNfontSet, &text_info->fontset);
371 XtSetArg(args[num_args], nhStr(XtNtopMargin), &top_margin);
373 XtSetArg(args[num_args], nhStr(XtNbottomMargin), &bottom_margin);
375 XtSetArg(args[num_args], nhStr(XtNleftMargin), &left_margin);
377 XtSetArg(args[num_args], nhStr(XtNrightMargin), &right_margin);
379 XtGetValues(wp->w, args, num_args);
381 text_info->extra_width = left_margin + right_margin;
382 text_info->extra_height = top_margin + bottom_margin;
385 /* Send events to "text" which is where the translations are. */
386 XtSetKeyboardFocus (form, wp->w);
391 destroy_text_window(wp)
394 /* Don't need to pop down, this only called from dismiss_text(). */
396 struct text_info_t *text_info = wp->text_information;
399 * If the text window was blocked, then the user has already ACK'ed
400 * it and we are free to really destroy the window. Otherwise, don't
401 * destroy until the user dismisses the window via a key or button
404 if (text_info->blocked || text_info->destroy_on_ack) {
405 XtDestroyWidget(wp->popup);
406 free_text_buffer(&text_info->text);
407 free((genericptr_t) text_info), wp->text_information = 0;
408 wp->type = NHW_NONE; /* allow reuse */
410 text_info->destroy_on_ack = TRUE; /* destroy on next ACK */
415 clear_text_window(wp)
418 clear_text_buffer(&wp->text_information->text);
421 /* text buffer routines ----------------------------------------------------
424 /* Append a line to the text buffer. */
426 append_text_buffer(tb, str, concat)
427 struct text_buffer *tb;
435 panic("append_text_buffer: null text buffer");
438 length = strlen(str);
443 if (length + tb->text_last + 1 >= tb->text_size) {
444 /* we need to go to a bigger buffer! */
447 "append_text_buffer: text buffer growing from %d to %d bytes\n",
448 tb->text_size, 2 * tb->text_size);
450 copy = (char *) alloc((unsigned) tb->text_size * 2);
451 (void) memcpy(copy, tb->text, tb->text_last);
457 if (tb->num_lines) { /* not first --- append a newline */
460 if (concat && !index("!.?'\")", tb->text[tb->text_last - 1])) {
462 tb->num_lines--; /* offset increment at end of function */
465 *(tb->text + tb->text_last) = appchar;
470 (void) memcpy((tb->text + tb->text_last), str, length + 1);
472 /* Remove all newlines. Otherwise we have a confused line count.
474 copy = (tb->text + tb->text_last);
475 while ((copy = index(copy, '\n')) != (char *) 0)
479 tb->text_last += length;
481 tb->text[tb->text_last] = '\0';
485 /* Initialize text buffer. */
488 struct text_buffer *tb;
490 tb->text = (char *) alloc(START_SIZE);
492 tb->text_size = START_SIZE;
497 /* Empty the text buffer */
499 clear_text_buffer(tb)
500 struct text_buffer *tb;
507 /* Free up allocated memory. */
510 struct text_buffer *tb;
513 tb->text = (char *) 0;
519 #ifdef GRAPHIC_TOMBSTONE
521 static void FDECL(rip_exposed, (Widget, XtPointer, XtPointer));
523 static XImage *rip_image = 0;
525 #define STONE_LINE_LEN 16 /* # chars that fit on one line */
526 #define NAME_LINE 0 /* line # for player name */
527 #define GOLD_LINE 1 /* line # for amount of gold */
528 #define DEATH_LINE 2 /* line # for death description */
529 #define YEAR_LINE 6 /* line # for year */
531 static char rip_line[YEAR_LINE + 1][STONE_LINE_LEN + 1];
534 calculate_rip_text(int how, time_t when)
536 /* Follows same algorithm as genl_outrip() */
543 /* Put name on stone */
544 Sprintf(rip_line[NAME_LINE], "%s", plname);
547 Sprintf(rip_line[GOLD_LINE], "%ld Au", done_money);
548 /* Put together death description */
549 formatkiller(buf, sizeof buf, how, FALSE);
551 /* Put death type on stone */
552 for (line = DEATH_LINE, dpx = buf; line < YEAR_LINE; line++) {
559 if ((i0 = strlen(dpx)) <= STONE_LINE_LEN)
560 jstone_line = STONE_LINE_LEN;
561 else if (i0 / 2 <= STONE_LINE_LEN )
562 jstone_line = ((i0 + 3) / 4) * 2;
563 else if (i0 / 3 <= STONE_LINE_LEN )
564 jstone_line = ((i0 + 5) / 6) * 2;
566 jstone_line = ((i0+7)/8)*2;
570 if ((i0 = strlen(dpx)) > STONE_LINE_LEN) {
571 for (i = STONE_LINE_LEN; ((i0 > STONE_LINE_LEN) && i); i--)
573 if (i0 > jstone_line) {
574 for(i = jstone_line; ((i0 > jstone_line) && i); i--)
584 while(i0 < jstone_line){
585 uc = (unsigned char *)(dpx + i0);
596 strcpy(rip_line[line], dpx);
597 if (tmpchar != ' ') {
604 /* Put year on stone */
605 year = yyyymmdd(when) / 10000L;
606 Sprintf(rip_line[YEAR_LINE], "%4ld", year);
610 * RIP image expose callback.
614 rip_exposed(w, client_data, widget_data)
616 XtPointer client_data; /* unused */
617 XtPointer widget_data; /* expose event from Window widget */
619 XExposeEvent *event = (XExposeEvent *) widget_data;
620 Display *dpy = XtDisplay(w);
625 static Pixmap rip_pixmap = None;
628 if (!XtIsRealized(w) || event->count > 0)
631 if (rip_pixmap == None && rip_image) {
632 rip_pixmap = XCreatePixmap(dpy, XtWindow(w), rip_image->width,
634 DefaultDepth(dpy, DefaultScreen(dpy)));
635 XPutImage(dpy, rip_pixmap, DefaultGC(dpy, DefaultScreen(dpy)),
636 rip_image, 0, 0, 0, 0, /* src, dest top left */
637 rip_image->width, rip_image->height);
638 XDestroyImage(rip_image); /* data bytes free'd also */
641 mask = GCFunction | GCForeground | GCGraphicsExposures | GCFont;
642 values.graphics_exposures = False;
643 XtSetArg(args[0], XtNforeground, &values.foreground);
644 XtGetValues(w, args, 1);
645 values.function = GXcopy;
646 values.font = WindowFont(w);
647 gc = XtGetGC(w, mask, &values);
649 if (rip_pixmap != None) {
650 XCopyArea(dpy, rip_pixmap, XtWindow(w), gc, event->x, event->y,
651 event->width, event->height, event->x, event->y);
654 x = appResources.tombtext_x;
655 y = appResources.tombtext_y;
656 for (i = 0; i <= YEAR_LINE; i++) {
657 int len = strlen(rip_line[i]);
659 XFontStruct *font = WindowFontStruct(w);
660 int width = XTextWidth(font, rip_line[i], len);
661 XDrawString(dpy, XtWindow(w), gc, x - width / 2, y, rip_line[i], len);
663 XFontSet fontset = WindowFontSet(w);
664 int width = XmbTextEscapement(fontset, rip_line[i], len);
665 XmbDrawString(dpy, XtWindow(w), fontset, gc, x - width / 2, y, rip_line[i], len);
667 x += appResources.tombtext_dx;
668 y += appResources.tombtext_dy;
675 * The ripout window creation routine.
678 create_ripout_widget(Widget parent)
684 static int rip_width, rip_height;
687 XpmAttributes attributes;
691 attributes.valuemask = XpmCloseness;
693 attributes.valuemask = XpmCloseness | XpmColormap;
695 #ifdef INSTALLCOLORMAP
696 attributes.colormap = cmap;
698 attributes.closeness = 65535; /* Try anything */
700 XpmReadFileToImage(XtDisplay(parent), appResources.tombstone,
701 &rip_image, 0, &attributes);
702 if (errorcode != XpmSuccess) {
704 Sprintf(buf, "Failed to load %s: %s", appResources.tombstone,
705 XpmGetErrorString(errorcode));
708 rip_width = rip_image->width;
709 rip_height = rip_image->height;
713 XtSetArg(args[num_args], XtNwidth, rip_width);
715 XtSetArg(args[num_args], XtNheight, rip_height);
717 XtSetArg(args[num_args], XtNtranslations,
718 XtParseTranslationTable(rip_translations));
721 imageport = XtCreateManagedWidget("rip", windowWidgetClass, parent, args,
724 XtAddCallback(imageport, XtNexposeCallback, rip_exposed, (XtPointer) 0);
729 #endif /* GRAPHIC_TOMBSTONE */