From 45ecbb84d6caaadfd94f3c261835d6ef49d46f0b Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Tue, 23 Oct 2018 20:30:44 +0300 Subject: [PATCH] X11: Move color and font structs ... so that other window types can also use them. --- include/winX.h | 13 +++-- win/X11/winX.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++ win/X11/winmenu.c | 122 +----------------------------------------- 3 files changed, 141 insertions(+), 125 deletions(-) diff --git a/include/winX.h b/include/winX.h index 9a9b8ce75..b5fcfd34e 100644 --- a/include/winX.h +++ b/include/winX.h @@ -153,12 +153,7 @@ struct menu_info_t { struct menu curr_menu; /* Menu being displayed. */ struct menu new_menu; /* New menu being built. */ - boolean nh_colors_inited; - XColor nh_colors[CLR_MAX]; - XFontStruct *fs; /* Font for the window. */ - XFontStruct *boldfs; /* Bold font */ - Display *boldfs_dpy; long menu_count; /* number entered by user */ Dimension line_height; /* Total height of a line of text. */ Dimension internal_height; /* Internal height between widget & border */ @@ -203,6 +198,11 @@ struct xwindow { int prevx, cursx; /* Cursor position, only used by */ int prevy, cursy; /* map and "plain" status windows.*/ + boolean nh_colors_inited; + XColor nh_colors[CLR_MAX]; + XFontStruct *boldfs; /* Bold font */ + Display *boldfs_dpy; /* Bold font display */ + union { struct map_info_t *Map_info; /* map window info */ struct mesg_info_t *Mesg_info; /* message window info */ @@ -298,6 +298,9 @@ E void FDECL(positionpopup, (Widget, BOOLEAN_P)); /* ### winX.c ### */ E struct xwindow *FDECL(find_widget, (Widget)); +E XColor FDECL(get_nhcolor, (struct xwindow *, int)); +E void FDECL(init_menu_nhcolors, (struct xwindow *)); +E void FDECL(load_boldfont, (struct xwindow *, Widget)); E Boolean FDECL(nhApproxColor, (Screen *, Colormap, char *, XColor *)); E void FDECL(get_widget_window_geometry, (Widget, int *, int *, int *, int *)); E char *FDECL(fontname_boldify, (const char *)); diff --git a/win/X11/winX.c b/win/X11/winX.c index 18a8fb9f5..f914521f0 100644 --- a/win/X11/winX.c +++ b/win/X11/winX.c @@ -49,6 +49,7 @@ #include "hack.h" #include "winX.h" #include "dlb.h" +#include "xwindow.h" #ifndef NO_SIGNAL #include @@ -263,6 +264,102 @@ find_free_window() return (winid) windex; } + +XColor +get_nhcolor(wp, clr) +struct xwindow *wp; +int clr; +{ + init_menu_nhcolors(wp); + /* FIXME: init_menu_nhcolors may fail */ + + if (clr >= 0 && clr < CLR_MAX) + return wp->nh_colors[clr]; + + return wp->nh_colors[0]; +} + +void +init_menu_nhcolors(wp) +struct xwindow *wp; +{ + static const char *mapCLR_to_res[CLR_MAX] = { + XtNblack, + XtNred, + XtNgreen, + XtNbrown, + XtNblue, + XtNmagenta, + XtNcyan, + XtNgray, + XtNforeground, + XtNorange, + XtNbright_green, + XtNyellow, + XtNbright_blue, + XtNbright_magenta, + XtNbright_cyan, + XtNwhite, + }; + Display *dpy; + Colormap screen_colormap; + XrmDatabase rDB; + XrmValue value; + Status rc; + int color; + char *ret_type[32]; + char clr_name[BUFSZ]; + char clrclass[BUFSZ]; + const char *wintypenames[NHW_TEXT] = { + "message", "status", "map", "menu", "text" + }; + const char *wtn; + char wtn_up[BUFSZ]; + + if (wp->nh_colors_inited || !wp->type) + return; + + wtn = wintypenames[wp->type - 1]; + Strcpy(wtn_up, wtn); + (void) upstart(wtn_up); + + dpy = XtDisplay(wp->w); + screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy)); + rDB = XrmGetDatabase(dpy); + + for (color = 0; color < CLR_MAX; color++) { + Sprintf(clr_name, "nethack.%s.%s", wtn, mapCLR_to_res[color]); + Sprintf(clrclass, "NetHack.%s.%s", wtn_up, mapCLR_to_res[color]); + + if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) { + Sprintf(clr_name, "nethack.map.%s", mapCLR_to_res[color]); + Sprintf(clrclass, "NetHack.Map.%s", mapCLR_to_res[color]); + } + + if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) { + impossible("XrmGetResource error (%s)", clr_name); + } else if (!strcmp(ret_type[0], "String")) { + char tmpbuf[256]; + + if (value.size >= sizeof tmpbuf) + value.size = sizeof tmpbuf - 1; + (void) strncpy(tmpbuf, (char *) value.addr, (int) value.size); + tmpbuf[value.size] = '\0'; + /* tmpbuf now contains the color name from the named resource */ + + rc = XAllocNamedColor(dpy, screen_colormap, tmpbuf, + &wp->nh_colors[color], + &wp->nh_colors[color]); + if (rc == 0) { + impossible("XAllocNamedColor failed for color %i (%s)", + color, clr_name); + } + } + } + + wp->nh_colors_inited = TRUE; +} + /* * Color conversion. The default X11 color converters don't try very * hard to find matching colors in PseudoColor visuals. If they can't @@ -552,6 +649,31 @@ const char *fontname; return buf; } +void +load_boldfont(wp, w) +struct xwindow *wp; +Widget w; +{ + Arg args[1]; + XFontStruct *fs; + unsigned long ret; + char *fontname; + Display *dpy; + + if (wp->boldfs) + return; + + XtSetArg(args[0], nhStr(XtNfont), &fs); + XtGetValues(w, args, 1); + + if (!XGetFontProperty(fs, XA_FONT, &ret)) + return; + + wp->boldfs_dpy = dpy = XtDisplay(w); + fontname = fontname_boldify(XGetAtomName(dpy, (Atom)ret)); + wp->boldfs = XLoadQueryFont(dpy, fontname); +} + #ifdef TEXTCOLOR /* ARGSUSED */ static void @@ -952,6 +1074,9 @@ int type; wp->prevx = wp->prevy = wp->cursx = wp->cursy = wp->pixel_width = wp->pixel_height = 0; wp->keep_window = FALSE; + wp->nh_colors_inited = FALSE; + wp->boldfs = (XFontStruct *) 0; + wp->boldfs_dpy = (Display *) 0; switch (type) { case NHW_MAP: @@ -1096,6 +1221,12 @@ winid window; WIN_INVEN = WIN_ERR; } + if (wp->boldfs) { + XFreeFont(wp->boldfs_dpy, wp->boldfs); + wp->boldfs = (XFontStruct *) 0; + wp->boldfs_dpy = (Display *) 0; + } + switch (wp->type) { case NHW_MAP: destroy_map_window(wp); diff --git a/win/X11/winmenu.c b/win/X11/winmenu.c index 49cd79162..e10adcab4 100644 --- a/win/X11/winmenu.c +++ b/win/X11/winmenu.c @@ -32,12 +32,9 @@ #undef PRESERVE_NO_SYSV #endif -#include "xwindow.h" #include "hack.h" #include "winX.h" -XColor FDECL(get_nhcolor, (struct xwindow *, int)); -static void FDECL(init_menu_nhcolors, (struct xwindow *)); static void FDECL(menu_size_change_handler, (Widget, XtPointer, XEvent *, Boolean *)); static void FDECL(menu_select, (Widget, XtPointer, XtPointer)); @@ -55,7 +52,6 @@ static void FDECL(invert_all, (struct xwindow *)); static void FDECL(invert_match, (struct xwindow *, char *)); static void FDECL(menu_popdown, (struct xwindow *)); static Widget FDECL(menu_create_buttons, (struct xwindow *, Widget, Widget)); -static void FDECL(load_boldfont, (struct xwindow *, Widget)); static void FDECL(menu_create_entries, (struct xwindow *, struct menu *)); static void FDECL(destroy_menu_entry_widgets, (struct xwindow *)); static void NDECL(create_menu_translation_tables); @@ -1121,31 +1117,6 @@ Widget form,under; return all; } -static void -load_boldfont(wp, w) -struct xwindow *wp; -Widget w; -{ - Arg args[1]; - XFontStruct *fs; - unsigned long ret; - char *fontname; - Display *dpy; - - if (wp->menu_information->boldfs) - return; - - XtSetArg(args[0], nhStr(XtNfont), &fs); - XtGetValues(w, args, 1); - - if (!XGetFontProperty(fs, XA_FONT, &ret)) - return; - - wp->menu_information->boldfs_dpy = dpy = XtDisplay(w); - fontname = fontname_boldify(XGetAtomName(dpy, (Atom)ret)); - wp->menu_information->boldfs = XLoadQueryFont(dpy, fontname); -} - static void menu_create_entries(wp, curr_menu) struct xwindow *wp; @@ -1185,7 +1156,7 @@ struct menu *curr_menu; get_nhcolor(wp, color).pixel); num_args++; } - /* TODO: ATR_BOLD, ATR_DIM, ATR_ULINE, ATR_BLINK */ + /* TODO: ATR_DIM, ATR_ULINE, ATR_BLINK */ if (attr == ATR_INVERSE) { XtSetArg(args[num_args], nhStr(XtNforeground), @@ -1215,7 +1186,7 @@ struct menu *curr_menu; load_boldfont(wp, curr->w); num_args = 0; XtSetArg(args[num_args], nhStr(XtNfont), - wp->menu_information->boldfs); num_args++; + wp->boldfs); num_args++; XtSetValues(curr->w, args, num_args); } @@ -1315,91 +1286,6 @@ struct xwindow *wp; } } -XColor -get_nhcolor(wp, clr) -struct xwindow *wp; -int clr; -{ - init_menu_nhcolors(wp); - - if (clr >= 0 && clr < CLR_MAX) - return wp->menu_information->nh_colors[clr]; - - return wp->menu_information->nh_colors[0]; -} - -static void -init_menu_nhcolors(wp) -struct xwindow *wp; -{ - static const char *mapCLR_to_res[CLR_MAX] = { - XtNblack, - XtNred, - XtNgreen, - XtNbrown, - XtNblue, - XtNmagenta, - XtNcyan, - XtNgray, - XtNforeground, - XtNorange, - XtNbright_green, - XtNyellow, - XtNbright_blue, - XtNbright_magenta, - XtNbright_cyan, - XtNwhite, - }; - Display *dpy; - Colormap screen_colormap; - XrmDatabase rDB; - XrmValue value; - Status rc; - int color; - char *ret_type[32]; - char clr_name[BUFSZ]; - char clrclass[BUFSZ]; - - if (wp->menu_information->nh_colors_inited) - return; - - dpy = XtDisplay(wp->w); - screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy)); - rDB = XrmGetDatabase(dpy); - - for (color = 0; color < CLR_MAX; color++) { - Sprintf(clr_name, "nethack.menu.%s", mapCLR_to_res[color]); - Sprintf(clrclass, "NetHack.Menu.%s", mapCLR_to_res[color]); - - if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) { - Sprintf(clr_name, "nethack.map.%s", mapCLR_to_res[color]); - Sprintf(clrclass, "NetHack.Map.%s", mapCLR_to_res[color]); - } - - if (!XrmGetResource(rDB, clr_name, clrclass, ret_type, &value)) { - impossible("XrmGetResource error (%s)", clr_name); - } else if (!strcmp(ret_type[0], "String")) { - char tmpbuf[256]; - - if (value.size >= sizeof tmpbuf) - value.size = sizeof tmpbuf - 1; - (void) strncpy(tmpbuf, (char *) value.addr, (int) value.size); - tmpbuf[value.size] = '\0'; - /* tmpbuf now contains the color name from the named resource */ - - rc = XAllocNamedColor(dpy, screen_colormap, tmpbuf, - &wp->menu_information->nh_colors[color], - &wp->menu_information->nh_colors[color]); - if (rc == 0) { - impossible("XAllocNamedColor failed for color %i (%s)", - color, clr_name); - } - } - } - - wp->menu_information->nh_colors_inited = TRUE; -} - void create_menu_window(wp) struct xwindow *wp; @@ -1413,7 +1299,6 @@ struct xwindow *wp; reset_menu_to_default(&wp->menu_information->new_menu); reset_menu_count(wp->menu_information); wp->w = wp->popup = (Widget) 0; - wp->menu_information->nh_colors_inited = FALSE; wp->menu_information->permi_x = -1; wp->menu_information->permi_y = -1; wp->menu_information->permi_w = -1; @@ -1425,9 +1310,6 @@ destroy_menu_window(wp) struct xwindow *wp; { clear_old_menu(wp); /* this will also destroy the widgets */ - if (wp->menu_information->boldfs) - XFreeFont(wp->menu_information->boldfs_dpy, - wp->menu_information->boldfs); free((genericptr_t) wp->menu_information); wp->menu_information = (struct menu_info_t *) 0; wp->type = NHW_NONE; /* allow re-use */ -- 2.49.0