Skip to content

Commit 704a790

Browse files
author
Everret Afton
committed
Use circle for the icon background + implement more suggested changes + make positioning less wonky
1 parent c667a78 commit 704a790

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

src/video/x11/SDL_x11messagebox.c

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ typedef struct SDL_MessageBoxButtonDataX11
8282
int text_a;
8383
int text_d;
8484

85-
SDL_Rect text_rct;
85+
SDL_Rect text_rect;
8686
SDL_Rect rect; // Rectangle for entire button
8787

8888
const SDL_MessageBoxButtonData *buttondata; // Button data from caller
@@ -92,7 +92,7 @@ typedef struct TextLineData
9292
{
9393
int length; // String length of this text line
9494
const char *text; // Text for this line
95-
SDL_Rect rct;
95+
SDL_Rect rect;
9696
} TextLineData;
9797

9898
typedef struct SDL_MessageBoxDataX11
@@ -224,7 +224,6 @@ static bool X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBox
224224
data->buttondata = buttondata;
225225
data->numbuttons = numbuttons;
226226
data->pbuttonid = pbuttonid;
227-
228227

229228
// Convert flags to icon character
230229
switch (data->messageboxdata->flags & (SDL_MESSAGEBOX_ERROR | SDL_MESSAGEBOX_WARNING | SDL_MESSAGEBOX_INFORMATION)) {
@@ -283,7 +282,7 @@ static bool X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBox
283282
if (!data->icon_char_font) {
284283
data->icon_char_font = X11_XLoadQueryFont(data->display, g_MessageBoxFontLatin1);
285284
if (!data->icon_char_font) {
286-
return SDL_SetError("Couldn't load icon font %s", g_MessageBoxFontLatin1);
285+
data->icon_char = '\0';
287286
}
288287
}
289288
}
@@ -350,14 +349,18 @@ static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
350349
int icon_char_w;
351350
int icon_char_h;
352351
int icon_char_a;
353-
352+
int icon_char_max;
353+
354354
GetTextWidthHeightForFont(data, data->icon_char_font, &data->icon_char, 1, &icon_char_w, &icon_char_h, &icon_char_a);
355355
data->icon_box_rect.w = icon_char_w + paddingx2;
356356
data->icon_box_rect.h = icon_char_h + paddingx2;
357+
icon_char_max = IntMax(data->icon_box_rect.w, data->icon_box_rect.h);
358+
data->icon_box_rect.w = icon_char_max;
359+
data->icon_box_rect.h = icon_char_max;
357360
data->icon_box_rect.y = 0;
358361
data->icon_box_rect.x = 0;
359-
data->icon_char_y = icon_char_a + data->icon_box_rect.y + SDL_DIALOG_ELEMENT_PADDING;
360-
data->icon_char_x = data->icon_box_rect.x + SDL_DIALOG_ELEMENT_PADDING;
362+
data->icon_char_y = icon_char_a + data->icon_box_rect.y + (data->icon_box_rect.h - icon_char_h)/2 + 1;
363+
data->icon_char_x = data->icon_box_rect.x + (data->icon_box_rect.w - icon_char_w)/2 + 1;
361364
}
362365

363366
// Go over text and break linefeeds into separate lines.
@@ -383,23 +386,23 @@ static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
383386

384387
plinedata[i].text = text;
385388

386-
GetTextWidthHeight(data, text, length, &plinedata[i].rct.w, &plinedata[i].rct.h, &ascent, &descent);
389+
GetTextWidthHeight(data, text, length, &plinedata[i].rect.w, &plinedata[i].rect.h, &ascent, &descent);
387390

388391
// Text widths are the largest we've ever seen.
389-
text_width_max = IntMax(text_width_max, plinedata[i].rct.w);
392+
text_width_max = IntMax(text_width_max, plinedata[i].rect.w);
390393

391394
plinedata[i].length = length;
392395
if (lf && (lf > text) && (lf[-1] == '\r')) {
393396
plinedata[i].length--;
394397
}
395398

396399
if (i > 0) {
397-
plinedata[i].rct.y = ascent + descent + plinedata[i-1].rct.y;
400+
plinedata[i].rect.y = ascent + descent + plinedata[i-1].rect.y;
398401
} else {
399-
plinedata[i].rct.y = data->icon_box_rect.y + SDL_DIALOG_ELEMENT_PADDING + ascent;
402+
plinedata[i].rect.y = data->icon_box_rect.y + SDL_DIALOG_ELEMENT_PADDING + ascent;
400403
iascent = ascent;
401404
}
402-
plinedata[i].rct.x = text_ix = data->icon_box_rect.x + data->icon_box_rect.w + SDL_DIALOG_ELEMENT_PADDING_2;
405+
plinedata[i].rect.x = text_ix = data->icon_box_rect.x + data->icon_box_rect.w + SDL_DIALOG_ELEMENT_PADDING_2;
403406
text += length + 1;
404407

405408
// Break if there are no more linefeeds.
@@ -408,18 +411,18 @@ static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
408411
}
409412
}
410413

411-
text_height_total = plinedata[linecount-1].rct.y + plinedata[linecount-1].rct.h - iascent - data->icon_box_rect.y - SDL_DIALOG_ELEMENT_PADDING;
414+
text_height_total = plinedata[linecount-1].rect.y + plinedata[linecount-1].rect.h - iascent - data->icon_box_rect.y - SDL_DIALOG_ELEMENT_PADDING;
412415
}
413416

414417
// Loop through all buttons and calculate the button widths and height.
415418
for (i = 0; i < data->numbuttons; i++) {
416419
data->buttonpos[i].buttondata = &data->buttondata[i];
417420
data->buttonpos[i].length = SDL_strlen(data->buttondata[i].text);
418421

419-
GetTextWidthHeight(data, data->buttondata[i].text, SDL_strlen(data->buttondata[i].text), &data->buttonpos[i].text_rct.w, &data->buttonpos[i].text_rct.h, &data->buttonpos[i].text_a, &data->buttonpos[i].text_d);
422+
GetTextWidthHeight(data, data->buttondata[i].text, SDL_strlen(data->buttondata[i].text), &data->buttonpos[i].text_rect.w, &data->buttonpos[i].text_rect.h, &data->buttonpos[i].text_a, &data->buttonpos[i].text_d);
420423

421-
button_height_max = IntMax(button_height_max, (data->buttonpos[i].text_rct.h + SDL_DIALOG_ELEMENT_PADDING_3 * 2));
422-
button_width_max = IntMax(button_width_max, (data->buttonpos[i].text_rct.w + padding2x2));
424+
button_height_max = IntMax(button_height_max, (data->buttonpos[i].text_rect.h + SDL_DIALOG_ELEMENT_PADDING_3 * 2));
425+
button_width_max = IntMax(button_width_max, (data->buttonpos[i].text_rect.w + padding2x2));
423426

424427
}
425428
button_width_total = button_width_max * data->numbuttons + SDL_DIALOG_ELEMENT_PADDING * (data->numbuttons - 1);
@@ -450,55 +453,55 @@ static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
450453
}
451454
}
452455
for (i = 0; i < data->numlines; i++) {
453-
data->linedata[i].rct.x += t;
456+
data->linedata[i].rect.x += t;
454457
}
455458

456459
/* Button poistioning */
457460
for (i = 0; i < data->numbuttons; i++) {
458461
data->buttonpos[i].rect.w = button_width_max;
459-
data->buttonpos[i].text_rct.x = (data->buttonpos[i].rect.w - data->buttonpos[i].text_rct.w)/2;
462+
data->buttonpos[i].text_rect.x = (data->buttonpos[i].rect.w - data->buttonpos[i].text_rect.w)/2;
460463
data->buttonpos[i].rect.h = button_height_max;
461-
data->buttonpos[i].text_rct.y = data->buttonpos[i].text_a + (data->buttonpos[i].rect.h - data->buttonpos[i].text_rct.h)/2;
464+
data->buttonpos[i].text_rect.y = data->buttonpos[i].text_a + (data->buttonpos[i].rect.h - data->buttonpos[i].text_rect.h)/2;
462465
if (i > 0) {
463466
data->buttonpos[i].rect.x += data->buttonpos[i-1].rect.x + data->buttonpos[i-1].rect.w + SDL_DIALOG_ELEMENT_PADDING_3;
464-
data->buttonpos[i].text_rct.x += data->buttonpos[i].rect.x;
467+
data->buttonpos[i].text_rect.x += data->buttonpos[i].rect.x;
465468
}
466469
}
467470
button_width_total = data->buttonpos[data->numbuttons-1].rect.x + data->buttonpos[data->numbuttons-1].rect.w;
468471
data->dialog_width = IntMax(data->dialog_width, (button_width_total + padding2x2));
469472
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
470473
for (i = 0; i < data->numbuttons; i++) {
471474
data->buttonpos[i].rect.x += (data->dialog_width - button_width_total)/2;
472-
data->buttonpos[i].text_rct.x += (data->dialog_width - button_width_total)/2;
475+
data->buttonpos[i].text_rect.x += (data->dialog_width - button_width_total)/2;
473476
if (data->icon_box_rect.h > text_height_total) {
474-
data->buttonpos[i].text_rct.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2;
477+
data->buttonpos[i].text_rect.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2 - 2;
475478
data->buttonpos[i].rect.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2;
476479
} else {
477480
int a;
478481

479482
a = 0;
480483
if (data->numlines) {
481-
a = data->linedata[data->numlines - 1].rct.y + data->linedata[data->numlines - 1].rct.h;
484+
a = data->linedata[data->numlines - 1].rect.y + data->linedata[data->numlines - 1].rect.h;
482485
}
483-
data->buttonpos[i].text_rct.y += a + SDL_DIALOG_ELEMENT_PADDING_2;
486+
data->buttonpos[i].text_rect.y += a + SDL_DIALOG_ELEMENT_PADDING_2 - 2;
484487
data->buttonpos[i].rect.y += a + SDL_DIALOG_ELEMENT_PADDING_2;
485488
}
486489
}
487490
} else {
488491
for (i = data->numbuttons; i != -1; i--) {
489492
data->buttonpos[i].rect.x += (data->dialog_width - button_width_total)/2;
490-
data->buttonpos[i].text_rct.x += (data->dialog_width - button_width_total)/2;
493+
data->buttonpos[i].text_rect.x += (data->dialog_width - button_width_total)/2;
491494
if (data->icon_box_rect.h > text_height_total) {
492-
data->buttonpos[i].text_rct.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2;
495+
data->buttonpos[i].text_rect.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2 - 2;
493496
data->buttonpos[i].rect.y += data->icon_box_rect.h + SDL_DIALOG_ELEMENT_PADDING_2;
494497
} else {
495498
int a;
496499

497500
a = 0;
498501
if (data->numlines) {
499-
a = data->linedata[data->numlines - 1].rct.y + data->linedata[data->numlines - 1].rct.h;
502+
a = data->linedata[data->numlines - 1].rect.y + data->linedata[data->numlines - 1].rect.h;
500503
}
501-
data->buttonpos[i].text_rct.y += a + SDL_DIALOG_ELEMENT_PADDING_2;
504+
data->buttonpos[i].text_rect.y += a + SDL_DIALOG_ELEMENT_PADDING_2 - 2;
502505
data->buttonpos[i].rect.y += a + SDL_DIALOG_ELEMENT_PADDING_2;
503506
}
504507
}
@@ -513,11 +516,11 @@ static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
513516
data->icon_char_y += t;
514517
}
515518
for (i = 0; i < data->numbuttons; i++) {
516-
data->buttonpos[i].text_rct.y += t;
519+
data->buttonpos[i].text_rect.y += t;
517520
data->buttonpos[i].rect.y += t;
518521
}
519522
for (i = 0; i < data->numlines; i++) {
520-
data->linedata[i].rct.y += t;
523+
data->linedata[i].rect.y += t;
521524
}
522525
return true;
523526
}
@@ -738,7 +741,7 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx, bool utf8)
738741

739742
if(data->icon_char != '\0') {
740743
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_TEXT].pixel);
741-
X11_XFillRectangle(display, window, ctx, data->icon_box_rect.x, data->icon_box_rect.y, data->icon_box_rect.w, data->icon_box_rect.h);
744+
X11_XFillArc(display, window, ctx, data->icon_box_rect.x, data->icon_box_rect.y, data->icon_box_rect.w, data->icon_box_rect.h, 0, 360 * 64);
742745

743746
X11_XSetFont(display, ctx, data->icon_char_font->fid);
744747
X11_XSetForeground(display, ctx, data->xcolor[SDL_MESSAGEBOX_COLOR_BACKGROUND].pixel);
@@ -755,13 +758,13 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx, bool utf8)
755758
#ifdef X_HAVE_UTF8_STRING
756759
if (SDL_X11_HAVE_UTF8) {
757760
X11_Xutf8DrawString(display, window, data->font_set, ctx,
758-
plinedata->rct.x, plinedata->rct.y,
761+
plinedata->rect.x, plinedata->rect.y,
759762
plinedata->text, plinedata->length);
760763
} else
761764
#endif
762765
{
763766
X11_XDrawString(display, window, ctx,
764-
plinedata->rct.x, plinedata->rct.y,
767+
plinedata->rect.x, plinedata->rect.y,
765768
plinedata->text, plinedata->length);
766769
}
767770
}
@@ -788,14 +791,14 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx, bool utf8)
788791
#ifdef X_HAVE_UTF8_STRING
789792
if (SDL_X11_HAVE_UTF8) {
790793
X11_Xutf8DrawString(display, window, data->font_set, ctx,
791-
buttondatax11->text_rct.x,
792-
buttondatax11->text_rct.y,
794+
buttondatax11->text_rect.x,
795+
buttondatax11->text_rect.y,
793796
buttondata->text, buttondatax11->length);
794797
} else
795798
#endif
796799
{
797800
X11_XDrawString(display, window, ctx,
798-
buttondatax11->text_rct.x, buttondatax11->text_rct.y,
801+
buttondatax11->text_rect.x, buttondatax11->text_rect.y,
799802
buttondata->text, buttondatax11->length);
800803
}
801804
}
@@ -1007,20 +1010,20 @@ static bool X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int
10071010

10081011
// Init and display the message box.
10091012
if (!X11_MessageBoxInit(&data, messageboxdata, buttonID)) {
1010-
goto INIT_BAIL;
1013+
goto done;
10111014
}
10121015

10131016
if (!X11_MessageBoxInitPositions(&data)) {
1014-
goto INIT_BAIL;
1017+
goto done;
10151018
}
10161019

10171020
if (!X11_MessageBoxCreateWindow(&data)) {
1018-
goto INIT_BAIL;
1021+
goto done;
10191022
}
10201023

10211024
result = X11_MessageBoxLoop(&data);
10221025

1023-
INIT_BAIL:
1026+
done:
10241027
X11_MessageBoxShutdown(&data);
10251028
#if SDL_SET_LOCALE
10261029
if (origlocale) {

src/video/x11/SDL_x11sym.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ SDL_X11_SYM(int,XDeleteProperty,(Display* a,Window b,Atom c))
5454
SDL_X11_SYM(int,XDestroyWindow,(Display* a,Window b))
5555
SDL_X11_SYM(int,XDisplayKeycodes,(Display* a,int* b,int* c))
5656
SDL_X11_SYM(int,XDrawRectangle,(Display* a,Drawable b,GC c,int d,int e,unsigned int f,unsigned int g))
57+
SDL_X11_SYM(int,XFillArc,(Display* a,Drawable b,GC c,int d,int e,unsigned int f,unsigned int g, int h, int i))
5758
SDL_X11_SYM(char*,XDisplayName,(_Xconst char* a))
5859
SDL_X11_SYM(int,XDrawString,(Display* a,Drawable b,GC c,int d,int e,_Xconst char* f,int g))
5960
SDL_X11_SYM(int,XEventsQueued,(Display* a,int b))

0 commit comments

Comments
 (0)