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