src/video/x11/SDL_x11messagebox.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 18 Oct 2013 01:36:41 -0400
changeset 7827 a03ec8de0426
parent 7522 b186724247dd
child 7860 2b0bcdea3a79
permissions -rw-r--r--
Don't supply duplicate X11 symbols inside SDL.

Fixes static linking when something else also uses X11.
slouken@6602
     1
/*
slouken@6602
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@6602
     4
slouken@6602
     5
  This software is provided 'as-is', without any express or implied
slouken@6602
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6602
     7
  arising from the use of this software.
slouken@6602
     8
slouken@6602
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6602
    10
  including commercial applications, and to alter it and redistribute it
slouken@6602
    11
  freely, subject to the following restrictions:
slouken@6602
    12
slouken@6602
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6602
    14
     claim that you wrote the original software. If you use this software
slouken@6602
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6602
    16
     appreciated but is not required.
slouken@6602
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6602
    18
     misrepresented as being the original software.
slouken@6602
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6602
    20
*/
icculus@6723
    21
slouken@6602
    22
#include "SDL_config.h"
slouken@6602
    23
slouken@6602
    24
#if SDL_VIDEO_DRIVER_X11
slouken@6602
    25
slouken@6602
    26
#include "SDL.h"
slouken@6602
    27
#include "SDL_x11video.h"
slouken@6602
    28
#include "SDL_x11dyn.h"
icculus@6726
    29
#include "SDL_assert.h"
slouken@6602
    30
icculus@6723
    31
#include <locale.h>
icculus@6723
    32
icculus@6726
    33
mikesart@6835
    34
#define SDL_FORK_MESSAGEBOX 0
mikesart@6835
    35
#define SDL_SET_LOCALE      0
icculus@6726
    36
icculus@6726
    37
#if SDL_FORK_MESSAGEBOX
icculus@6726
    38
#include <sys/types.h>
icculus@6726
    39
#include <sys/wait.h>
icculus@6726
    40
#include <unistd.h>
icculus@6726
    41
#include <errno.h>
icculus@6726
    42
#endif
icculus@6726
    43
slouken@6602
    44
#define MAX_BUTTONS             8       /* Maximum number of buttons supported */
slouken@6602
    45
#define MAX_TEXT_LINES          32      /* Maximum number of text lines supported */
slouken@6602
    46
#define MIN_BUTTON_WIDTH        64      /* Minimum button width */
slouken@6602
    47
#define MIN_DIALOG_WIDTH        200     /* Minimum dialog width */
slouken@6602
    48
#define MIN_DIALOG_HEIGHT       100     /* Minimum dialog height */
slouken@6602
    49
icculus@6723
    50
static const char g_MessageBoxFontLatin1[] = "-*-*-medium-r-normal--0-120-*-*-p-0-iso8859-1";
icculus@6723
    51
static const char g_MessageBoxFont[] = "-*-*-*-*-*-*-*-*-*-*-*-*-*-*";
slouken@6602
    52
slouken@6725
    53
static const SDL_MessageBoxColor g_default_colors[ SDL_MESSAGEBOX_COLOR_MAX ] = {
icculus@6817
    54
    { 56,  54,  53  }, /* SDL_MESSAGEBOX_COLOR_BACKGROUND, */
icculus@6817
    55
    { 209, 207, 205 }, /* SDL_MESSAGEBOX_COLOR_TEXT, */
icculus@6817
    56
    { 140, 135, 129 }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BORDER, */
icculus@6817
    57
    { 105, 102, 99  }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND, */
icculus@6817
    58
    { 205, 202, 53  }, /* SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED, */
slouken@6602
    59
};
slouken@6602
    60
slouken@6602
    61
#define SDL_MAKE_RGB( _r, _g, _b )  ( ( ( Uint32 )( _r ) << 16 ) | \
slouken@6602
    62
                                      ( ( Uint32 )( _g ) << 8 ) |  \
slouken@6602
    63
                                      ( ( Uint32 )( _b ) ) )
slouken@6602
    64
slouken@6725
    65
typedef struct SDL_MessageBoxButtonDataX11 {
slouken@6602
    66
    int x, y;                           /* Text position */
slouken@6602
    67
    int length;                         /* Text length */
slouken@6602
    68
    int text_width;                     /* Text width */
slouken@6602
    69
slouken@6602
    70
    SDL_Rect rect;                      /* Rectangle for entire button */
slouken@6602
    71
slouken@6602
    72
    const SDL_MessageBoxButtonData *buttondata;   /* Button data from caller */
slouken@6602
    73
} SDL_MessageBoxButtonDataX11;
slouken@6602
    74
slouken@6725
    75
typedef struct TextLineData {
slouken@6602
    76
    int width;                          /* Width of this text line */
slouken@6602
    77
    int length;                         /* String length of this text line */
slouken@6602
    78
    const char *text;                   /* Text for this line */
slouken@6602
    79
} TextLineData;
slouken@6602
    80
slouken@7522
    81
typedef struct SDL_MessageBoxDataX11
slouken@7522
    82
{
slouken@7522
    83
    Display *display;
slouken@7522
    84
    int screen;
slouken@6602
    85
    Window window;
slouken@6603
    86
    long event_mask;
slouken@6602
    87
    Atom wm_protocols;
slouken@6602
    88
    Atom wm_delete_message;
slouken@6602
    89
slouken@6602
    90
    int dialog_width;                   /* Dialog box width. */
slouken@6602
    91
    int dialog_height;                  /* Dialog box height. */
slouken@6602
    92
slouken@7522
    93
    XFontSet font_set;                  /* for UTF-8 systems */
slouken@7522
    94
    XFontStruct *font_struct;           /* Latin1 (ASCII) fallback. */
slouken@6602
    95
    int xtext, ytext;                   /* Text position to start drawing at. */
slouken@6602
    96
    int numlines;                       /* Count of Text lines. */
slouken@6602
    97
    int text_height;                    /* Height for text lines. */
slouken@6602
    98
    TextLineData linedata[ MAX_TEXT_LINES ];
slouken@6602
    99
slouken@6602
   100
    int *pbuttonid;                     /* Pointer to user return buttonid value. */
slouken@6602
   101
slouken@6602
   102
    int button_press_index;             /* Index into buttondata/buttonpos for button which is pressed (or -1). */
slouken@6602
   103
    int mouse_over_index;               /* Index into buttondata/buttonpos for button mouse is over (or -1). */
slouken@6602
   104
slouken@6602
   105
    int numbuttons;                     /* Count of buttons. */
slouken@6602
   106
    const SDL_MessageBoxButtonData *buttondata;
slouken@6602
   107
    SDL_MessageBoxButtonDataX11 buttonpos[ MAX_BUTTONS ];
slouken@6602
   108
slouken@6603
   109
    Uint32 color[ SDL_MESSAGEBOX_COLOR_MAX ];
slouken@6602
   110
slouken@6602
   111
    const SDL_MessageBoxData *messageboxdata;
slouken@6602
   112
} SDL_MessageBoxDataX11;
slouken@6602
   113
slouken@6602
   114
/* Maximum helper for ints. */
slouken@6602
   115
static __inline__ int
slouken@6602
   116
IntMax( int a, int b )
slouken@6602
   117
{
slouken@6602
   118
    return ( a > b  ) ? a : b;
slouken@6602
   119
}
slouken@6602
   120
slouken@6602
   121
/* Return width and height for a string. */
slouken@6602
   122
static void
icculus@6723
   123
GetTextWidthHeight( SDL_MessageBoxDataX11 *data, const char *str, int nbytes, int *pwidth, int *pheight )
slouken@6602
   124
{
icculus@6723
   125
    if (SDL_X11_HAVE_UTF8) {
icculus@6723
   126
        XRectangle overall_ink, overall_logical;
icculus@7827
   127
        X11_Xutf8TextExtents(data->font_set, str, nbytes, &overall_ink, &overall_logical);
icculus@6723
   128
        *pwidth = overall_logical.width;
icculus@6723
   129
        *pheight = overall_logical.height;
icculus@6723
   130
    } else {
icculus@6723
   131
        XCharStruct text_structure;
icculus@6723
   132
        int font_direction, font_ascent, font_descent;
icculus@7827
   133
        X11_XTextExtents( data->font_struct, str, nbytes,
icculus@6723
   134
                      &font_direction, &font_ascent, &font_descent,
icculus@6723
   135
                      &text_structure );
icculus@6723
   136
        *pwidth = text_structure.width;
icculus@6723
   137
        *pheight = text_structure.ascent + text_structure.descent;
icculus@6723
   138
    }
slouken@6602
   139
}
slouken@6602
   140
slouken@6602
   141
/* Return index of button if position x,y is contained therein. */
slouken@6602
   142
static int
slouken@6602
   143
GetHitButtonIndex( SDL_MessageBoxDataX11 *data, int x, int y )
slouken@6602
   144
{
slouken@6602
   145
    int i;
slouken@6602
   146
    int numbuttons = data->numbuttons;
slouken@6602
   147
    SDL_MessageBoxButtonDataX11 *buttonpos = data->buttonpos;
slouken@6602
   148
slouken@6725
   149
    for ( i = 0; i < numbuttons; i++ ) {
slouken@6602
   150
        SDL_Rect *rect = &buttonpos[ i ].rect;
slouken@6602
   151
slouken@6602
   152
        if ( ( x >= rect->x ) &&
slouken@6725
   153
             ( x <= ( rect->x + rect->w ) ) &&
slouken@6725
   154
             ( y >= rect->y ) &&
slouken@6725
   155
             ( y <= ( rect->y + rect->h ) ) ) {
slouken@6602
   156
            return i;
slouken@6602
   157
        }
slouken@6602
   158
    }
slouken@6602
   159
slouken@6602
   160
    return -1;
slouken@6602
   161
}
slouken@6602
   162
slouken@6602
   163
/* Initialize SDL_MessageBoxData structure and Display, etc. */
slouken@6602
   164
static int
slouken@6602
   165
X11_MessageBoxInit( SDL_MessageBoxDataX11 *data, const SDL_MessageBoxData * messageboxdata, int * pbuttonid )
slouken@6602
   166
{
slouken@6603
   167
    int i;
slouken@6602
   168
    int numbuttons = messageboxdata->numbuttons;
slouken@6602
   169
    const SDL_MessageBoxButtonData *buttondata = messageboxdata->buttons;
slouken@6603
   170
    const SDL_MessageBoxColor *colorhints;
slouken@6602
   171
slouken@6602
   172
    if ( numbuttons > MAX_BUTTONS ) {
icculus@7037
   173
        return SDL_SetError("Too many buttons (%d max allowed)", MAX_BUTTONS);
slouken@6602
   174
    }
slouken@6602
   175
slouken@6602
   176
    data->dialog_width = MIN_DIALOG_WIDTH;
slouken@6602
   177
    data->dialog_height = MIN_DIALOG_HEIGHT;
slouken@6602
   178
    data->messageboxdata = messageboxdata;
slouken@6602
   179
    data->buttondata = buttondata;
slouken@6602
   180
    data->numbuttons = numbuttons;
slouken@6602
   181
    data->pbuttonid = pbuttonid;
slouken@6602
   182
icculus@7827
   183
    data->display = X11_XOpenDisplay( NULL );
slouken@6602
   184
    if ( !data->display ) {
icculus@7037
   185
        return SDL_SetError("Couldn't open X11 display");
slouken@6602
   186
    }
slouken@6602
   187
icculus@6723
   188
    if (SDL_X11_HAVE_UTF8) {
icculus@6723
   189
        char **missing = NULL;
icculus@6723
   190
        int num_missing = 0;
icculus@7827
   191
        data->font_set = X11_XCreateFontSet(data->display, g_MessageBoxFont,
icculus@6723
   192
                                        &missing, &num_missing, NULL);
icculus@6723
   193
        if ( missing != NULL ) {
icculus@7827
   194
            X11_XFreeStringList(missing);
icculus@6723
   195
        }
icculus@6723
   196
        if ( data->font_set == NULL ) {
icculus@7037
   197
            return SDL_SetError("Couldn't load font %s", g_MessageBoxFont);
icculus@6723
   198
        }
icculus@6723
   199
    } else {
icculus@7827
   200
        data->font_struct = X11_XLoadQueryFont( data->display, g_MessageBoxFontLatin1 );
icculus@6723
   201
        if ( data->font_struct == NULL ) {
icculus@7037
   202
            return SDL_SetError("Couldn't load font %s", g_MessageBoxFontLatin1);
icculus@6723
   203
        }
slouken@6602
   204
    }
slouken@6602
   205
slouken@6603
   206
    if ( messageboxdata->colorScheme ) {
slouken@6603
   207
        colorhints = messageboxdata->colorScheme->colors;
slouken@6603
   208
    } else {
slouken@6603
   209
        colorhints = g_default_colors;
slouken@6603
   210
    }
slouken@6602
   211
slouken@6603
   212
    /* Convert our SDL_MessageBoxColor r,g,b values to packed RGB format. */
slouken@6603
   213
    for ( i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; i++ ) {
slouken@6603
   214
        data->color[ i ] = SDL_MAKE_RGB( colorhints[ i ].r, colorhints[ i ].g, colorhints[ i ].b );
slouken@6603
   215
    }
slouken@6602
   216
slouken@6602
   217
    return 0;
slouken@6602
   218
}
slouken@6602
   219
slouken@6602
   220
/* Calculate and initialize text and button locations. */
slouken@6602
   221
static int
slouken@6602
   222
X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
slouken@6602
   223
{
slouken@6602
   224
    int i;
slouken@6602
   225
    int ybuttons;
slouken@6602
   226
    int text_width_max = 0;
slouken@6602
   227
    int button_text_height = 0;
slouken@6602
   228
    int button_width = MIN_BUTTON_WIDTH;
slouken@6602
   229
    const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
slouken@6602
   230
slouken@6602
   231
    /* Go over text and break linefeeds into separate lines. */
slouken@6725
   232
    if ( messageboxdata->message && messageboxdata->message[ 0 ] ) {
slouken@6602
   233
        const char *text = messageboxdata->message;
slouken@6602
   234
        TextLineData *plinedata = data->linedata;
slouken@6602
   235
slouken@6725
   236
        for ( i = 0; i < MAX_TEXT_LINES; i++, plinedata++ ) {
slouken@6602
   237
            int height;
slouken@6602
   238
            char *lf = SDL_strchr( ( char * )text, '\n' );
slouken@6602
   239
slouken@6602
   240
            data->numlines++;
slouken@6602
   241
slouken@6602
   242
            /* Only grab length up to lf if it exists and isn't the last line. */
slouken@6602
   243
            plinedata->length = ( lf && ( i < MAX_TEXT_LINES - 1 ) ) ? ( lf - text ) : SDL_strlen( text );
slouken@6602
   244
            plinedata->text = text;
slouken@6602
   245
icculus@6723
   246
            GetTextWidthHeight( data, text, plinedata->length, &plinedata->width, &height );
slouken@6602
   247
slouken@6602
   248
            /* Text and widths are the largest we've ever seen. */
slouken@6602
   249
            data->text_height = IntMax( data->text_height, height );
slouken@6602
   250
            text_width_max = IntMax( text_width_max, plinedata->width );
slouken@6602
   251
icculus@6758
   252
            if (lf && (lf > text) && (lf[-1] == '\r')) {
icculus@6758
   253
                plinedata->length--;
icculus@6758
   254
            }
icculus@6758
   255
slouken@6602
   256
            text += plinedata->length + 1;
slouken@6602
   257
slouken@6602
   258
            /* Break if there are no more linefeeds. */
slouken@6602
   259
            if ( !lf )
slouken@6602
   260
                break;
slouken@6602
   261
        }
slouken@6602
   262
slouken@6602
   263
        /* Bump up the text height slightly. */
slouken@6602
   264
        data->text_height += 2;
slouken@6602
   265
    }
slouken@6602
   266
slouken@6602
   267
    /* Loop through all buttons and calculate the button widths and height. */
slouken@6725
   268
    for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   269
        int height;
slouken@6602
   270
slouken@6602
   271
        data->buttonpos[ i ].buttondata = &data->buttondata[ i ];
slouken@6602
   272
        data->buttonpos[ i ].length = SDL_strlen( data->buttondata[ i ].text );
slouken@6602
   273
icculus@6723
   274
        GetTextWidthHeight( data, data->buttondata[ i ].text, SDL_strlen( data->buttondata[ i ].text ),
slouken@6602
   275
                            &data->buttonpos[ i ].text_width, &height );
slouken@6602
   276
slouken@6602
   277
        button_width = IntMax( button_width, data->buttonpos[ i ].text_width );
slouken@6602
   278
        button_text_height = IntMax( button_text_height, height );
slouken@6602
   279
    }
slouken@6602
   280
slouken@6725
   281
    if ( data->numlines ) {
slouken@6602
   282
        /* x,y for this line of text. */
slouken@6602
   283
        data->xtext = data->text_height;
slouken@6602
   284
        data->ytext = data->text_height + data->text_height;
slouken@6602
   285
slouken@6602
   286
        /* Bump button y down to bottom of text. */
slouken@6602
   287
        ybuttons = 3 * data->ytext / 2 + ( data->numlines - 1 ) * data->text_height;
slouken@6602
   288
slouken@6602
   289
        /* Bump the dialog box width and height up if needed. */
slouken@6602
   290
        data->dialog_width = IntMax( data->dialog_width, 2 * data->xtext + text_width_max );
slouken@6602
   291
        data->dialog_height = IntMax( data->dialog_height, ybuttons );
slouken@6725
   292
    } else {
slouken@6602
   293
        /* Button y starts at height of button text. */
slouken@6602
   294
        ybuttons = button_text_height;
slouken@6602
   295
    }
slouken@6602
   296
slouken@6725
   297
    if ( data->numbuttons ) {
slouken@6602
   298
        int x, y;
slouken@6602
   299
        int width_of_buttons;
slouken@6602
   300
        int button_spacing = button_text_height;
slouken@6602
   301
        int button_height = 2 * button_text_height;
slouken@6602
   302
slouken@6602
   303
        /* Bump button width up a bit. */
slouken@6602
   304
        button_width += button_text_height;
slouken@6602
   305
slouken@6602
   306
        /* Get width of all buttons lined up. */
slouken@6602
   307
        width_of_buttons = data->numbuttons * button_width + ( data->numbuttons - 1 ) * button_spacing;
slouken@6602
   308
slouken@6602
   309
        /* Bump up dialog width and height if buttons are wider than text. */
slouken@6602
   310
        data->dialog_width = IntMax( data->dialog_width, width_of_buttons + 2 * button_spacing );
slouken@6602
   311
        data->dialog_height = IntMax( data->dialog_height, ybuttons + 2 * button_height );
slouken@6602
   312
slouken@6602
   313
        /* Location for first button. */
slouken@6602
   314
        x = ( data->dialog_width - width_of_buttons ) / 2;
slouken@6602
   315
        y = ybuttons + ( data->dialog_height - ybuttons - button_height ) / 2;
slouken@6602
   316
slouken@6725
   317
        for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   318
            /* Button coordinates. */
slouken@6602
   319
            data->buttonpos[ i ].rect.x = x;
slouken@6602
   320
            data->buttonpos[ i ].rect.y = y;
slouken@6602
   321
            data->buttonpos[ i ].rect.w = button_width;
slouken@6602
   322
            data->buttonpos[ i ].rect.h = button_height;
slouken@6602
   323
slouken@6602
   324
            /* Button text coordinates. */
slouken@6602
   325
            data->buttonpos[ i ].x = x + ( button_width - data->buttonpos[ i ].text_width ) / 2;
slouken@6602
   326
            data->buttonpos[ i ].y = y + ( button_height - button_text_height - 1 ) / 2 + button_text_height;
slouken@6602
   327
slouken@6602
   328
            /* Scoot over for next button. */
slouken@6615
   329
            x += button_width + button_spacing;
slouken@6602
   330
        }
slouken@6602
   331
    }
slouken@6602
   332
slouken@6602
   333
    return 0;
slouken@6602
   334
}
slouken@6602
   335
slouken@6602
   336
/* Free SDL_MessageBoxData data. */
slouken@6602
   337
static void
slouken@6602
   338
X11_MessageBoxShutdown( SDL_MessageBoxDataX11 *data )
slouken@6602
   339
{
slouken@6725
   340
    if ( data->font_set != NULL ) {
icculus@7827
   341
        X11_XFreeFontSet( data->display, data->font_set );
icculus@6723
   342
        data->font_set = NULL;
icculus@6723
   343
    }
icculus@6723
   344
slouken@6725
   345
    if ( data->font_struct != NULL ) {
icculus@7827
   346
        X11_XFreeFont( data->display, data->font_struct );
icculus@6723
   347
        data->font_struct = NULL;
slouken@6602
   348
    }
slouken@6602
   349
slouken@6725
   350
    if ( data->display ) {
slouken@6725
   351
        if ( data->window != None ) {
icculus@7827
   352
            X11_XWithdrawWindow( data->display, data->window, data->screen );
icculus@7827
   353
            X11_XDestroyWindow( data->display, data->window );
slouken@6602
   354
            data->window = None;
slouken@6602
   355
        }
slouken@6602
   356
icculus@7827
   357
        X11_XCloseDisplay( data->display );
slouken@6602
   358
        data->display = NULL;
slouken@6602
   359
    }
slouken@6602
   360
}
slouken@6602
   361
slouken@6602
   362
/* Create and set up our X11 dialog box indow. */
slouken@6602
   363
static int
slouken@6602
   364
X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
slouken@6602
   365
{
slouken@6602
   366
    int x, y;
slouken@6602
   367
    XSizeHints *sizehints;
slouken@6602
   368
    XSetWindowAttributes wnd_attr;
slouken@6602
   369
    Display *display = data->display;
slouken@6602
   370
    SDL_WindowData *windowdata = NULL;
slouken@6602
   371
    const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
slouken@6602
   372
slouken@6602
   373
    if ( messageboxdata->window ) {
slouken@7522
   374
        SDL_DisplayData *displaydata =
slouken@7522
   375
            (SDL_DisplayData *) SDL_GetDisplayForWindow(messageboxdata->window)->driverdata;
slouken@6602
   376
        windowdata = (SDL_WindowData *)messageboxdata->window->driverdata;
slouken@7522
   377
        data->screen = displaydata->screen;
slouken@7522
   378
    } else {
slouken@7522
   379
        data->screen = DefaultScreen( display );
slouken@6602
   380
    }
slouken@6602
   381
slouken@6602
   382
    data->event_mask = ExposureMask |
slouken@6725
   383
                       ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
slouken@6725
   384
                       StructureNotifyMask | FocusChangeMask | PointerMotionMask;
slouken@6603
   385
    wnd_attr.event_mask = data->event_mask;
slouken@6602
   386
icculus@7827
   387
    data->window = X11_XCreateWindow(
slouken@7522
   388
                       display, RootWindow(display, data->screen),
slouken@6725
   389
                       0, 0,
slouken@6725
   390
                       data->dialog_width, data->dialog_height,
slouken@6725
   391
                       0, CopyFromParent, InputOutput, CopyFromParent,
slouken@6725
   392
                       CWEventMask, &wnd_attr );
slouken@6602
   393
    if ( data->window == None ) {
icculus@7037
   394
        return SDL_SetError("Couldn't create X window");
slouken@6602
   395
    }
slouken@6602
   396
slouken@6602
   397
    if ( windowdata ) {
slouken@6602
   398
        /* http://tronche.com/gui/x/icccm/sec-4.html#WM_TRANSIENT_FOR */
icculus@7827
   399
        X11_XSetTransientForHint( display, data->window, windowdata->xwindow );
slouken@6602
   400
    }
slouken@6602
   401
icculus@7827
   402
    X11_XStoreName( display, data->window, messageboxdata->title );
slouken@6602
   403
slouken@6602
   404
    /* Allow the window to be deleted by the window manager */
icculus@7827
   405
    data->wm_protocols = X11_XInternAtom( display, "WM_PROTOCOLS", False );
icculus@7827
   406
    data->wm_delete_message = X11_XInternAtom( display, "WM_DELETE_WINDOW", False );
icculus@7827
   407
    X11_XSetWMProtocols( display, data->window, &data->wm_delete_message, 1 );
slouken@6602
   408
slouken@6602
   409
    if ( windowdata ) {
slouken@6602
   410
        XWindowAttributes attrib;
slouken@6602
   411
        Window dummy;
slouken@6602
   412
icculus@7827
   413
        X11_XGetWindowAttributes(display, windowdata->xwindow, &attrib);
slouken@6602
   414
        x = attrib.x + ( attrib.width - data->dialog_width ) / 2;
slouken@6602
   415
        y = attrib.y + ( attrib.height - data->dialog_height ) / 3 ;
icculus@7827
   416
        X11_XTranslateCoordinates(display, windowdata->xwindow, RootWindow(display, data->screen), x, y, &x, &y, &dummy);
slouken@6602
   417
    } else {
slouken@7522
   418
        x = ( DisplayWidth( display, data->screen ) - data->dialog_width ) / 2;
slouken@7522
   419
        y = ( DisplayHeight( display, data->screen ) - data->dialog_height ) / 3 ;
slouken@6602
   420
    }
icculus@7827
   421
    X11_XMoveWindow( display, data->window, x, y );
slouken@6602
   422
icculus@7827
   423
    sizehints = X11_XAllocSizeHints();
slouken@6602
   424
    if ( sizehints ) {
slouken@6602
   425
        sizehints->flags = USPosition | USSize | PMaxSize | PMinSize;
slouken@6602
   426
        sizehints->x = x;
slouken@6602
   427
        sizehints->y = y;
slouken@6602
   428
        sizehints->width = data->dialog_width;
slouken@6602
   429
        sizehints->height = data->dialog_height;
slouken@6602
   430
slouken@6602
   431
        sizehints->min_width = sizehints->max_width = data->dialog_width;
slouken@6602
   432
        sizehints->min_height = sizehints->max_height = data->dialog_height;
slouken@6602
   433
icculus@7827
   434
        X11_XSetWMNormalHints( display, data->window, sizehints );
slouken@6602
   435
icculus@7827
   436
        X11_XFree( sizehints );
slouken@6602
   437
    }
slouken@6602
   438
icculus@7827
   439
    X11_XMapRaised( display, data->window );
slouken@6602
   440
    return 0;
slouken@6602
   441
}
slouken@6602
   442
slouken@6602
   443
/* Draw our message box. */
slouken@6602
   444
static void
slouken@6602
   445
X11_MessageBoxDraw( SDL_MessageBoxDataX11 *data, GC ctx )
slouken@6602
   446
{
slouken@6602
   447
    int i;
slouken@6602
   448
    Window window = data->window;
slouken@6602
   449
    Display *display = data->display;
slouken@6602
   450
icculus@7827
   451
    X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ] );
icculus@7827
   452
    X11_XFillRectangle( display, window, ctx, 0, 0, data->dialog_width, data->dialog_height );
slouken@6602
   453
icculus@7827
   454
    X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_TEXT ] );
slouken@6725
   455
    for ( i = 0; i < data->numlines; i++ ) {
slouken@6602
   456
        TextLineData *plinedata = &data->linedata[ i ];
slouken@6602
   457
icculus@6723
   458
        if (SDL_X11_HAVE_UTF8) {
icculus@7827
   459
            X11_Xutf8DrawString( display, window, data->font_set, ctx,
icculus@6723
   460
                             data->xtext, data->ytext + i * data->text_height,
icculus@6723
   461
                             plinedata->text, plinedata->length );
icculus@6723
   462
        } else {
icculus@7827
   463
            X11_XDrawString( display, window, ctx,
icculus@6723
   464
                         data->xtext, data->ytext + i * data->text_height,
icculus@6723
   465
                         plinedata->text, plinedata->length );
icculus@6723
   466
        }
slouken@6602
   467
    }
slouken@6602
   468
slouken@6725
   469
    for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   470
        SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ i ];
slouken@6602
   471
        const SDL_MessageBoxButtonData *buttondata = buttondatax11->buttondata;
slouken@6602
   472
        int border = ( buttondata->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT ) ? 2 : 0;
slouken@6602
   473
        int offset = ( ( data->mouse_over_index == i ) && ( data->button_press_index == data->mouse_over_index ) ) ? 1 : 0;
slouken@6602
   474
icculus@7827
   475
        X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND ] );
icculus@7827
   476
        X11_XFillRectangle( display, window, ctx,
slouken@6602
   477
                        buttondatax11->rect.x - border, buttondatax11->rect.y - border,
slouken@6602
   478
                        buttondatax11->rect.w + 2 * border, buttondatax11->rect.h + 2 * border );
slouken@6602
   479
icculus@7827
   480
        X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_BORDER ] );
icculus@7827
   481
        X11_XDrawRectangle( display, window, ctx,
slouken@6602
   482
                        buttondatax11->rect.x, buttondatax11->rect.y,
slouken@6602
   483
                        buttondatax11->rect.w, buttondatax11->rect.h );
slouken@6602
   484
icculus@7827
   485
        X11_XSetForeground( display, ctx, ( data->mouse_over_index == i ) ?
slouken@6602
   486
                        data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED ] :
slouken@6603
   487
                        data->color[ SDL_MESSAGEBOX_COLOR_TEXT ] );
icculus@6723
   488
icculus@6723
   489
        if (SDL_X11_HAVE_UTF8) {
icculus@7827
   490
            X11_Xutf8DrawString( display, window, data->font_set, ctx,
icculus@6723
   491
                             buttondatax11->x + offset,
icculus@6723
   492
                             buttondatax11->y + offset,
icculus@6723
   493
                             buttondata->text, buttondatax11->length );
icculus@6723
   494
        } else {
icculus@7827
   495
            X11_XDrawString( display, window, ctx,
icculus@6723
   496
                         buttondatax11->x + offset, buttondatax11->y + offset,
icculus@6723
   497
                         buttondata->text, buttondatax11->length );
icculus@6723
   498
        }
slouken@6602
   499
    }
slouken@6602
   500
}
slouken@6602
   501
slouken@6602
   502
/* Loop and handle message box event messages until something kills it. */
slouken@6602
   503
static int
slouken@6602
   504
X11_MessageBoxLoop( SDL_MessageBoxDataX11 *data )
slouken@6602
   505
{
slouken@6602
   506
    GC ctx;
slouken@6602
   507
    XGCValues ctx_vals;
slouken@6602
   508
    SDL_bool close_dialog = SDL_FALSE;
slouken@6602
   509
    SDL_bool has_focus = SDL_TRUE;
slouken@6602
   510
    KeySym last_key_pressed = XK_VoidSymbol;
icculus@6723
   511
    unsigned long gcflags = GCForeground | GCBackground;
slouken@6602
   512
icculus@6723
   513
    SDL_zero(ctx_vals);
slouken@6602
   514
    ctx_vals.foreground = data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ];
slouken@6602
   515
    ctx_vals.background = data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ];
slouken@6602
   516
icculus@6723
   517
    if (!SDL_X11_HAVE_UTF8) {
icculus@6723
   518
        gcflags |= GCFont;
icculus@6723
   519
        ctx_vals.font = data->font_struct->fid;
icculus@6723
   520
    }
icculus@6723
   521
icculus@7827
   522
    ctx = X11_XCreateGC( data->display, data->window, gcflags, &ctx_vals );
slouken@6602
   523
    if ( ctx == None ) {
icculus@7037
   524
        return SDL_SetError("Couldn't create graphics context");
slouken@6602
   525
    }
slouken@6602
   526
slouken@6602
   527
    data->button_press_index = -1;  /* Reset what button is currently depressed. */
slouken@6602
   528
    data->mouse_over_index = -1;    /* Reset what button the mouse is over. */
slouken@6602
   529
slouken@6602
   530
    while( !close_dialog ) {
slouken@6602
   531
        XEvent e;
slouken@6602
   532
        SDL_bool draw = SDL_TRUE;
slouken@6602
   533
icculus@7827
   534
        X11_XWindowEvent( data->display, data->window, data->event_mask, &e );
slouken@6602
   535
icculus@7827
   536
        /* If X11_XFilterEvent returns True, then some input method has filtered the
slouken@6602
   537
           event, and the client should discard the event. */
icculus@7827
   538
        if ( ( e.type != Expose ) && X11_XFilterEvent( &e, None ) )
slouken@6602
   539
            continue;
slouken@6602
   540
slouken@6602
   541
        switch( e.type ) {
slouken@6602
   542
        case Expose:
slouken@6602
   543
            if ( e.xexpose.count > 0 ) {
slouken@6602
   544
                draw = SDL_FALSE;
slouken@6602
   545
            }
slouken@6602
   546
            break;
slouken@6602
   547
slouken@6602
   548
        case FocusIn:
slouken@6602
   549
            /* Got focus. */
slouken@6602
   550
            has_focus = SDL_TRUE;
slouken@6602
   551
            break;
slouken@6602
   552
slouken@6602
   553
        case FocusOut:
slouken@6602
   554
            /* lost focus. Reset button and mouse info. */
slouken@6602
   555
            has_focus = SDL_FALSE;
slouken@6602
   556
            data->button_press_index = -1;
slouken@6602
   557
            data->mouse_over_index = -1;
slouken@6602
   558
            break;
slouken@6602
   559
slouken@6602
   560
        case MotionNotify:
slouken@6602
   561
            if ( has_focus ) {
slouken@6602
   562
                /* Mouse moved... */
slouken@6602
   563
                data->mouse_over_index = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
slouken@6602
   564
            }
slouken@6602
   565
            break;
slouken@6602
   566
slouken@6602
   567
        case ClientMessage:
slouken@6602
   568
            if ( e.xclient.message_type == data->wm_protocols &&
slouken@6602
   569
                 e.xclient.format == 32 &&
slouken@6602
   570
                 e.xclient.data.l[ 0 ] == data->wm_delete_message ) {
slouken@6602
   571
                close_dialog = SDL_TRUE;
slouken@6602
   572
            }
slouken@6602
   573
            break;
slouken@6602
   574
slouken@6602
   575
        case KeyPress:
slouken@6602
   576
            /* Store key press - we make sure in key release that we got both. */
icculus@7827
   577
            last_key_pressed = X11_XLookupKeysym( &e.xkey, 0 );
slouken@6602
   578
            break;
slouken@6602
   579
slouken@6725
   580
        case KeyRelease: {
slouken@6602
   581
            Uint32 mask = 0;
icculus@7827
   582
            KeySym key = X11_XLookupKeysym( &e.xkey, 0 );
slouken@6602
   583
slouken@6602
   584
            /* If this is a key release for something we didn't get the key down for, then bail. */
slouken@6602
   585
            if ( key != last_key_pressed )
slouken@6602
   586
                break;
slouken@6602
   587
slouken@6602
   588
            if ( key == XK_Escape )
slouken@6602
   589
                mask = SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
slouken@6602
   590
            else if ( ( key == XK_Return ) || ( key == XK_KP_Enter ) )
slouken@6602
   591
                mask = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
slouken@6602
   592
slouken@6602
   593
            if ( mask ) {
slouken@6602
   594
                int i;
slouken@6602
   595
slouken@6602
   596
                /* Look for first button with this mask set, and return it if found. */
slouken@6602
   597
                for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   598
                    SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ i ];
slouken@6602
   599
slouken@6602
   600
                    if ( buttondatax11->buttondata->flags & mask ) {
slouken@6602
   601
                        *data->pbuttonid = buttondatax11->buttondata->buttonid;
slouken@6602
   602
                        close_dialog = SDL_TRUE;
slouken@6602
   603
                        break;
slouken@6602
   604
                    }
slouken@6602
   605
                }
slouken@6602
   606
            }
slouken@6602
   607
            break;
slouken@6602
   608
        }
slouken@6602
   609
slouken@6602
   610
        case ButtonPress:
slouken@6602
   611
            data->button_press_index = -1;
slouken@6602
   612
            if ( e.xbutton.button == Button1 ) {
slouken@6602
   613
                /* Find index of button they clicked on. */
slouken@6602
   614
                data->button_press_index = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
slouken@6602
   615
            }
slouken@6602
   616
            break;
slouken@6602
   617
slouken@6602
   618
        case ButtonRelease:
slouken@6602
   619
            /* If button is released over the same button that was clicked down on, then return it. */
slouken@6602
   620
            if ( ( e.xbutton.button == Button1 ) && ( data->button_press_index >= 0 ) ) {
slouken@6602
   621
                int button = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
slouken@6602
   622
slouken@6602
   623
                if ( data->button_press_index == button ) {
slouken@6602
   624
                    SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ button ];
slouken@6602
   625
slouken@6602
   626
                    *data->pbuttonid = buttondatax11->buttondata->buttonid;
slouken@6602
   627
                    close_dialog = SDL_TRUE;
slouken@6602
   628
                }
slouken@6602
   629
            }
slouken@6602
   630
            data->button_press_index = -1;
slouken@6602
   631
            break;
slouken@6602
   632
        }
slouken@6602
   633
slouken@6602
   634
        if ( draw ) {
slouken@6602
   635
            /* Draw our dialog box. */
slouken@6602
   636
            X11_MessageBoxDraw( data, ctx );
slouken@6602
   637
        }
slouken@6602
   638
    }
slouken@6602
   639
icculus@7827
   640
    X11_XFreeGC( data->display, ctx );
slouken@6602
   641
    return 0;
slouken@6602
   642
}
slouken@6602
   643
icculus@6726
   644
static int
icculus@6726
   645
X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6602
   646
{
slouken@6602
   647
    int ret;
slouken@6602
   648
    SDL_MessageBoxDataX11 data;
mikesart@6835
   649
#if SDL_SET_LOCALE
icculus@6724
   650
    char *origlocale;
mikesart@6835
   651
#endif
slouken@6602
   652
icculus@6723
   653
    SDL_zero(data);
icculus@6723
   654
slouken@6602
   655
    if ( !SDL_X11_LoadSymbols() )
slouken@6602
   656
        return -1;
slouken@6602
   657
mikesart@6835
   658
#if SDL_SET_LOCALE
icculus@6724
   659
    origlocale = setlocale(LC_ALL, NULL);
icculus@6724
   660
    if (origlocale != NULL) {
icculus@6724
   661
        origlocale = SDL_strdup(origlocale);
icculus@6724
   662
        if (origlocale == NULL) {
icculus@7037
   663
            return SDL_OutOfMemory();
icculus@6724
   664
        }
icculus@6724
   665
        setlocale(LC_ALL, "");
icculus@6724
   666
    }
mikesart@6835
   667
#endif
icculus@6724
   668
slouken@6602
   669
    /* This code could get called from multiple threads maybe? */
icculus@7827
   670
    X11_XInitThreads();
slouken@6602
   671
slouken@6602
   672
    /* Initialize the return buttonid value to -1 (for error or dialogbox closed). */
slouken@6602
   673
    *buttonid = -1;
slouken@6602
   674
slouken@6602
   675
    /* Init and display the message box. */
slouken@6602
   676
    ret = X11_MessageBoxInit( &data, messageboxdata, buttonid );
slouken@6602
   677
    if ( ret != -1 ) {
slouken@6602
   678
        ret = X11_MessageBoxInitPositions( &data );
slouken@6602
   679
        if ( ret != -1 ) {
slouken@6602
   680
            ret = X11_MessageBoxCreateWindow( &data );
slouken@6602
   681
            if ( ret != -1 ) {
slouken@6602
   682
                ret = X11_MessageBoxLoop( &data );
slouken@6602
   683
            }
slouken@6602
   684
        }
slouken@6602
   685
    }
slouken@6602
   686
slouken@6602
   687
    X11_MessageBoxShutdown( &data );
icculus@6724
   688
mikesart@6835
   689
#if SDL_SET_LOCALE
icculus@6724
   690
    if (origlocale) {
icculus@6724
   691
        setlocale(LC_ALL, origlocale);
icculus@6724
   692
        SDL_free(origlocale);
icculus@6724
   693
    }
mikesart@6835
   694
#endif
icculus@6724
   695
slouken@6602
   696
    return ret;
slouken@6602
   697
}
slouken@6602
   698
icculus@6726
   699
/* Display an x11 message box. */
icculus@6726
   700
int
icculus@6726
   701
X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
icculus@6726
   702
{
icculus@6726
   703
#if SDL_FORK_MESSAGEBOX
icculus@6726
   704
    /* Use a child process to protect against setlocale(). Annoying. */
icculus@6726
   705
    pid_t pid;
icculus@6726
   706
    int fds[2];
icculus@6726
   707
    int status = 0;
icculus@6726
   708
slouken@7191
   709
    /* Need to flush here in case someone has turned grab off and it hasn't gone through yet, etc. */
icculus@7827
   710
    X11_XFlush(data->display);
mikesart@6835
   711
icculus@6726
   712
    if (pipe(fds) == -1) {
icculus@6726
   713
        return X11_ShowMessageBoxImpl(messageboxdata, buttonid); /* oh well. */
icculus@6726
   714
    }
icculus@6726
   715
icculus@6726
   716
    pid = fork();
icculus@6726
   717
    if (pid == -1) {  /* failed */
icculus@6726
   718
        close(fds[0]);
icculus@6726
   719
        close(fds[1]);
icculus@6726
   720
        return X11_ShowMessageBoxImpl(messageboxdata, buttonid); /* oh well. */
icculus@6726
   721
    } else if (pid == 0) {  /* we're the child */
icculus@6726
   722
        int exitcode = 0;
icculus@6726
   723
        close(fds[0]);
icculus@6726
   724
        status = X11_ShowMessageBoxImpl(messageboxdata, buttonid);
icculus@6726
   725
        if (write(fds[1], &status, sizeof (int)) != sizeof (int))
icculus@6726
   726
            exitcode = 1;
icculus@6726
   727
        else if (write(fds[1], buttonid, sizeof (int)) != sizeof (int))
icculus@6726
   728
            exitcode = 1;
icculus@6726
   729
        close(fds[1]);
icculus@6726
   730
        _exit(exitcode);  /* don't run atexit() stuff, static destructors, etc. */
icculus@6726
   731
    } else {  /* we're the parent */
icculus@6726
   732
        pid_t rc;
icculus@6726
   733
        close(fds[1]);
icculus@6726
   734
        do {
icculus@6726
   735
            rc = waitpid(pid, &status, 0);
icculus@6726
   736
        } while ((rc == -1) && (errno == EINTR));
icculus@6726
   737
icculus@6726
   738
        SDL_assert(rc == pid);  /* not sure what to do if this fails. */
icculus@6726
   739
icculus@6726
   740
        if ((rc == -1) || (!WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) {
icculus@7037
   741
            return SDL_SetError("msgbox child process failed");
icculus@6726
   742
        }
icculus@6726
   743
icculus@6726
   744
        if (read(fds[0], &status, sizeof (int)) != sizeof (int))
icculus@6726
   745
            status = -1;
icculus@6726
   746
        else if (read(fds[0], buttonid, sizeof (int)) != sizeof (int))
icculus@6726
   747
            status = -1;
icculus@6726
   748
        close(fds[0]);
icculus@6726
   749
icculus@6726
   750
        return status;
icculus@6726
   751
    }
icculus@6726
   752
#else
icculus@6726
   753
    return X11_ShowMessageBoxImpl(messageboxdata, buttonid);
icculus@6726
   754
#endif
icculus@6726
   755
}
slouken@6602
   756
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@6602
   757
slouken@6602
   758
/* vi: set ts=4 sw=4 expandtab: */