Skip to content

Commit df07c09

Browse files
Everett Aftonicculus
authored andcommitted
Use visuals and colormapsin the X11 messagebox implementation
1 parent 737b9e1 commit df07c09

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/video/x11/SDL_x11messagebox.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ static const SDL_MessageBoxColor g_default_colors[SDL_MESSAGEBOX_COLOR_COUNT] =
6868
{ 205, 202, 53 }, // SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED,
6969
};
7070

71-
#define SDL_MAKE_RGB(_r, _g, _b) (((Uint32)(_r) << 16) | \
72-
((Uint32)(_g) << 8) | \
73-
((Uint32)(_b)))
74-
7571
typedef struct SDL_MessageBoxButtonDataX11
7672
{
7773
int x, y; // Text position
@@ -95,6 +91,8 @@ typedef struct SDL_MessageBoxDataX11
9591
Display *display;
9692
int screen;
9793
Window window;
94+
Visual *visual;
95+
Colormap cmap;
9896
#ifdef SDL_VIDEO_DRIVER_X11_XDBE
9997
XdbeBackBuffer buf;
10098
bool xdbe; // Whether Xdbe is present or not
@@ -122,7 +120,7 @@ typedef struct SDL_MessageBoxDataX11
122120
const SDL_MessageBoxButtonData *buttondata;
123121
SDL_MessageBoxButtonDataX11 buttonpos[MAX_BUTTONS];
124122

125-
Uint32 color[SDL_MESSAGEBOX_COLOR_COUNT];
123+
XColor xcolor[SDL_MESSAGEBOX_COLOR_COUNT];
126124

127125
const SDL_MessageBoxData *messageboxdata;
128126
} SDL_MessageBoxDataX11;
@@ -233,9 +231,12 @@ static bool X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBox
233231
colorhints = g_default_colors;
234232
}
235233

236-
// Convert our SDL_MessageBoxColor r,g,b values to packed RGB format.
234+
// Convert colors to 16 bpc XColor format
237235
for (i = 0; i < SDL_MESSAGEBOX_COLOR_COUNT; i++) {
238-
data->color[i] = SDL_MAKE_RGB(colorhints[i].r, colorhints[i].g, colorhints[i].b);
236+
data->xcolor[i].flags = DoRed|DoGreen|DoBlue;
237+
data->xcolor[i].red = colorhints[i].r*257;
238+
data->xcolor[i].green = colorhints[i].g*257;
239+
data->xcolor[i].blue = colorhints[i].b*257;
239240
}
240241

241242
return true;
@@ -418,7 +419,7 @@ static void X11_MessageBoxShutdown(SDL_MessageBoxDataX11 *data)
418419
// Create and set up our X11 dialog box indow.
419420
static bool X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
420421
{
421-
int x, y;
422+
int x, y, i;
422423
XSizeHints *sizehints;
423424
XSetWindowAttributes wnd_attr;
424425
Atom _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DIALOG;
@@ -441,17 +442,24 @@ static bool X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
441442
data->screen = DefaultScreen(display);
442443
}
443444

445+
data->visual = DefaultVisual(display, data->screen);
446+
data->cmap = DefaultColormap(display, data->screen);
447+
for (i = 0; i < SDL_MESSAGEBOX_COLOR_COUNT; i++) {
448+
X11_XAllocColor(display, data->cmap, &data->xcolor[i]);
449+
}
450+
444451
data->event_mask = ExposureMask |
445452
ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
446453
StructureNotifyMask | FocusChangeMask | PointerMotionMask;
447454
wnd_attr.event_mask = data->event_mask;
455+
wnd_attr.colormap = data->cmap;
448456

449457
data->window = X11_XCreateWindow(
450458
display, RootWindow(display, data->screen),
451459
0, 0,
452460
data->dialog_width, data->dialog_height,
453-
0, CopyFromParent, InputOutput, CopyFromParent,
454-
CWEventMask, &wnd_attr);
461+
0, DefaultDepth(display, data->screen), InputOutput, data->visual,
462+
CWEventMask | CWColormap, &wnd_attr);
455463
if (data->window == None) {
456464
return SDL_SetError("Couldn't create X window");
457465
}
@@ -569,10 +577,10 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
569577
}
570578
#endif
571579

572-
X11_XSetForeground(display, ctx, data->color[SDL_MESSAGEBOX_COLOR_BACKGROUND]);
580+
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_BACKGROUND].pixel);
573581
X11_XFillRectangle(display, window, ctx, 0, 0, data->dialog_width, data->dialog_height);
574582

575-
X11_XSetForeground(display, ctx, data->color[SDL_MESSAGEBOX_COLOR_TEXT]);
583+
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_TEXT].pixel);
576584
for (i = 0; i < data->numlines; i++) {
577585
TextLineData *plinedata = &data->linedata[i];
578586

@@ -596,17 +604,17 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
596604
int border = (buttondata->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) ? 2 : 0;
597605
int offset = ((data->mouse_over_index == i) && (data->button_press_index == data->mouse_over_index)) ? 1 : 0;
598606

599-
X11_XSetForeground(display, ctx, data->color[SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND]);
607+
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND].pixel);
600608
X11_XFillRectangle(display, window, ctx,
601609
buttondatax11->rect.x - border, buttondatax11->rect.y - border,
602610
buttondatax11->rect.w + 2 * border, buttondatax11->rect.h + 2 * border);
603611

604-
X11_XSetForeground(display, ctx, data->color[SDL_MESSAGEBOX_COLOR_BUTTON_BORDER]);
612+
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_BUTTON_BORDER].pixel);
605613
X11_XDrawRectangle(display, window, ctx,
606614
buttondatax11->rect.x, buttondatax11->rect.y,
607615
buttondatax11->rect.w, buttondatax11->rect.h);
608616

609-
X11_XSetForeground(display, ctx, (data->mouse_over_index == i) ? data->color[SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] : data->color[SDL_MESSAGEBOX_COLOR_TEXT]);
617+
X11_XSetForeground(display, ctx, (data->mouse_over_index == i) ? data->xcolor[SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED].pixel : data->xcolor[SDL_MESSAGEBOX_COLOR_TEXT].pixel);
610618

611619
#ifdef X_HAVE_UTF8_STRING
612620
if (SDL_X11_HAVE_UTF8) {
@@ -657,8 +665,8 @@ static bool X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
657665
#endif
658666

659667
SDL_zero(ctx_vals);
660-
ctx_vals.foreground = data->color[SDL_MESSAGEBOX_COLOR_BACKGROUND];
661-
ctx_vals.background = data->color[SDL_MESSAGEBOX_COLOR_BACKGROUND];
668+
ctx_vals.foreground = data->xcolor[SDL_MESSAGEBOX_COLOR_BACKGROUND].pixel;
669+
ctx_vals.background = data->xcolor[SDL_MESSAGEBOX_COLOR_BACKGROUND].pixel;
662670

663671
if (!have_utf8) {
664672
gcflags |= GCFont;

src/video/x11/SDL_x11sym.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ SDL_X11_SYM(Cursor,XCreatePixmapCursor,(Display* a,Pixmap b,Pixmap c,XColor* d,X
4545
SDL_X11_SYM(Cursor,XCreateFontCursor,(Display* a,unsigned int b))
4646
SDL_X11_SYM(XFontSet,XCreateFontSet,(Display* a, _Xconst char* b, char*** c, int* d, char** e))
4747
SDL_X11_SYM(GC,XCreateGC,(Display* a,Drawable b,unsigned long c,XGCValues* d))
48+
SDL_X11_SYM(Status,XAllocColor,(Display *a, Colormap b, XColor *c))
4849
SDL_X11_SYM(XImage*,XCreateImage,(Display* a,Visual* b,unsigned int c,int d,int e,char* f,unsigned int g,unsigned int h,int i,int j))
4950
SDL_X11_SYM(Window,XCreateWindow,(Display* a,Window b,int c,int d,unsigned int e,unsigned int f,unsigned int g,int h,unsigned int i,Visual* j,unsigned long k,XSetWindowAttributes* l))
5051
SDL_X11_SYM(int,XDefineCursor,(Display* a,Window b,Cursor c))

0 commit comments

Comments
 (0)