Module: SDL2::MessageBox

Defined in:
messagebox.c

Constant Summary

ERROR =

This flag means that the message box shows an error message

INT2NUM(SDL_MESSAGEBOX_ERROR)
WARNING =

This flag means that the message box shows a warning message

INT2NUM(SDL_MESSAGEBOX_WARNING)
INFORMATION =

This flag means that the message box shows an informational message

INT2NUM(SDL_MESSAGEBOX_INFORMATION)
BUTTON_RETURNKEY_DEFAULT =

This flag represents the button is selected when return key is pressed

INT2NUM(SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT)
BUTTON_ESCAPEKEY_DEFAULT =

This flag represents the button is selected when escape key is pressed

INT2NUM(SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT)

Class Method Summary (collapse)

Class Method Details

+ (Integer) show(flag:, window:nil, title:, message:, buttons:, color_scheme:nil)

Create a model message box.

You specify one of the following constants as flag

One button in the dialog represents a hash with folloing elements.

{ flags: 0, SDL2::MessageBox::BUTTON_ESCAPEKEY_DEFAULT, or
         SDL2::MessageBox::BUTTON_RETURNKEY_DEFAULT,
         you can ignore it for 0,
  text: text of a button,
  id: index of the button
}

and buttons is an array of above button hashes.

You can specify the color of message box by color_scheme. color_scheme is an hash whose keys are :bg, :text, :button_border, :button_bg, and :button_selected and values are array of three integers representing color. You can also use default color scheme by giving nil.

This function pauses all ruby's threads until the modal dialog is closed.

You can create a message box before calling SDL2.init.

Examples:

show a dialog with 3 buttons

button = SDL2::MessageBox.show(flags: SDL2::MessageBox::WARNING,
                               window: nil,
                               title: "Warning window",
                               message: "Here is the warning message",
                               buttons: [ { # flags is ignored
                                           id: 0,
                                           text: "No",
                                          },
                                          {flags: SDL2::MessageBox::BUTTON_RETURNKEY_DEFAULT,
                                           id: 1,
                                           text: "Yes",
                                          },
                                          {flags: SDL2::MessageBox::BUTTON_ESCAPEKEY_DEFAULT,
                                           id: 2,
                                           text: "Cancel",
                                          },
                                        ],
                               color_scheme: {
                                              bg: [255, 0, 0],
                                              text: [0, 255, 0],
                                              button_border: [255, 0, 0],
                                              button_bg: [0, 0, 255],
                                              button_selected: [255, 0, 0]
                                             }
                              )

Parameters:

  • flags (Integer)

    message box type flag

  • window (SDL2::Window, nil)

    the parent window, or nil for no parent

  • title (String)

    the title text

  • message (String)

    the message text

  • buttons (Array<Hash<Symbol => Object>>)

    array of buttons

  • color_scheme (Hash<Symbol=>[Integer,Integer,Integer]> nil)

    color scheme, or nil for the default color scheme

Returns:

  • (Integer)

    pressed button id

See Also:

  • show_simple_message_box


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'messagebox.c', line 137

static VALUE MessageBox_s_show(VALUE self, VALUE params)
{
    SDL_MessageBoxData mb_data;
    VALUE title, message, texts, buttons, color_scheme;
    long num_buttons;
    int i;
    SDL_MessageBoxButtonData* button_data;
    SDL_MessageBoxColorScheme scheme;
    int buttonid;

    Check_Type(params, T_HASH);
    mb_data.flags = NUM2INT(rb_hash_aref(params, sym_flags));
    mb_data.window = Get_SDL_Window_or_NULL(rb_hash_aref(params, sym_window));
    title = rb_str_export_to_utf8(rb_hash_aref(params, sym_title));
    mb_data.title = StringValueCStr(title);
    message = rb_str_export_to_utf8(rb_hash_aref(params, sym_message));
    mb_data.message = StringValueCStr(message);
    
    buttons = rb_hash_aref(params, sym_buttons);
    Check_Type(buttons, T_ARRAY);
    mb_data.numbuttons = num_buttons = RARRAY_LEN(buttons);
    button_data = ALLOCA_N(SDL_MessageBoxButtonData, num_buttons);
    mb_data.buttons = button_data;
    texts = rb_ary_new2(num_buttons);
    
    for (i=0; i<num_buttons; ++i) {
        VALUE button = rb_ary_entry(buttons, i);
        VALUE flags = rb_hash_aref(button, sym_flags);
        VALUE text;
        if (flags == Qnil)
            button_data[i].flags = 0;
        else
            button_data[i].flags = NUM2INT(flags);
        text = rb_str_export_to_utf8(rb_hash_aref(button, sym_text));
        rb_ary_push(texts, text);
        button_data[i].buttonid = NUM2INT(rb_hash_aref(button, sym_id));
        button_data[i].text = StringValueCStr(text);
    }

    color_scheme = rb_hash_aref(params, sym_color_scheme);
    if (color_scheme == Qnil) {
        mb_data.colorScheme = NULL;
    } else {
        Check_Type(color_scheme, T_HASH);
        mb_data.colorScheme = &scheme;
        set_color_scheme(color_scheme, sym_bg, &scheme.colors[0]);
        set_color_scheme(color_scheme, sym_text, &scheme.colors[1]);
        set_color_scheme(color_scheme, sym_button_border, &scheme.colors[2]);
        set_color_scheme(color_scheme, sym_button_bg, &scheme.colors[3]);
        set_color_scheme(color_scheme, sym_button_selected, &scheme.colors[4]);
    }
    
    HANDLE_ERROR(SDL_ShowMessageBox(&mb_data, &buttonid));
    
    RB_GC_GUARD(title); RB_GC_GUARD(message); RB_GC_GUARD(texts);
    return INT2NUM(buttonid);
}

+ (nil) show_simple_box(flag, title, message, parent)

Create a simple modal message box.

This function pauses all ruby's threads and the threads are resumed when modal dialog is closed.

You can create a message box before calling SDL2.init.

You specify one of the following constants as flag

Examples:

show warning dialog


SDL2.show_simple_message_box(SDL2::MessageBox::WARNING, "warning!",
                             "Somewhat special warning message!!", nil)

Parameters:

  • flag (Integer)

    one of the above flags

  • title (String)

    title text

  • message (String)

    message text

  • parent (SDL2::Window, nil)

    the parent window, or nil for no parent

Returns:

  • (nil)

See Also:

  • show_message_box


45
46
47
48
49
50
51
52
53
54
55
# File 'messagebox.c', line 45

static VALUE MessageBox_s_show_simple_box(VALUE self, VALUE flag, VALUE title,
                                          VALUE message, VALUE parent)
{
    title = rb_str_export_to_utf8(title);
    message = rb_str_export_to_utf8(message);
    HANDLE_ERROR(SDL_ShowSimpleMessageBox(NUM2UINT(flag),
                                          StringValueCStr(title),
                                          StringValueCStr(message),
                                          Get_SDL_Window_or_NULL(parent)));
    return Qnil;
}