src/video/cocoa/SDL_cocoamessagebox.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 02 Nov 2012 16:48:47 -0400
changeset 6639 c7e81fae23c4
parent 6626 4aa5af31f2f9
child 6714 146d1d7ecdc4
permissions -rw-r--r--
Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().

Otherwise, message boxes can fail if SDL_Init(SDL_INIT_VIDEO) wasn't completed.
slouken@6617
     1
/*
slouken@6617
     2
  Simple DirectMedia Layer
slouken@6617
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@6617
     4
slouken@6617
     5
  This software is provided 'as-is', without any express or implied
slouken@6617
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6617
     7
  arising from the use of this software.
slouken@6617
     8
slouken@6617
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6617
    10
  including commercial applications, and to alter it and redistribute it
slouken@6617
    11
  freely, subject to the following restrictions:
slouken@6617
    12
slouken@6617
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6617
    14
     claim that you wrote the original software. If you use this software
slouken@6617
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6617
    16
     appreciated but is not required.
slouken@6617
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6617
    18
     misrepresented as being the original software.
slouken@6617
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6617
    20
*/
slouken@6617
    21
#include "SDL_config.h"
slouken@6617
    22
slouken@6617
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6617
    24
icculus@6624
    25
#if defined(__APPLE__) && defined(__POWERPC__)
icculus@6624
    26
#include <altivec.h>
icculus@6624
    27
#undef bool
icculus@6624
    28
#undef vector
icculus@6626
    29
#undef pixel
icculus@6624
    30
#endif
icculus@6624
    31
slouken@6623
    32
#include "SDL_messagebox.h"
slouken@6617
    33
#include "SDL_cocoavideo.h"
slouken@6617
    34
slouken@6617
    35
slouken@6617
    36
/* Display a Cocoa message box */
slouken@6617
    37
int
slouken@6617
    38
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6617
    39
{
icculus@6639
    40
    Cocoa_RegisterApp();
icculus@6639
    41
slouken@6617
    42
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6617
    43
slouken@6617
    44
    NSAlert* alert = [[NSAlert alloc] init];
slouken@6617
    45
slouken@6617
    46
    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
slouken@6617
    47
        [alert setAlertStyle:NSCriticalAlertStyle];
slouken@6617
    48
    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
slouken@6617
    49
        [alert setAlertStyle:NSWarningAlertStyle];
slouken@6617
    50
    } else {
slouken@6617
    51
        [alert setAlertStyle:NSInformationalAlertStyle];
slouken@6617
    52
    }
slouken@6617
    53
slouken@6617
    54
    [alert setMessageText:[[NSString alloc] initWithUTF8String:messageboxdata->title]];
slouken@6617
    55
    [alert setInformativeText:[[NSString alloc] initWithUTF8String:messageboxdata->message]];
slouken@6617
    56
slouken@6617
    57
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slouken@6617
    58
    int i;
slouken@6617
    59
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
slouken@6617
    60
        NSButton *button = [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
slouken@6617
    61
        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
slouken@6617
    62
            [button setKeyEquivalent:@"\r"];
slouken@6617
    63
        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
slouken@6617
    64
            [button setKeyEquivalent:@"\033"];
slouken@6617
    65
        } else {
slouken@6617
    66
            [button setKeyEquivalent:@""];
slouken@6617
    67
        }
slouken@6617
    68
    }
slouken@6617
    69
slouken@6617
    70
    NSInteger clicked = [alert runModal];
slouken@6617
    71
    clicked -= NSAlertFirstButtonReturn;
slouken@6617
    72
    *buttonid = buttons[clicked].buttonid;
slouken@6617
    73
slouken@6617
    74
    [pool release];
slouken@6617
    75
slouken@6617
    76
    return 0;
slouken@6617
    77
}
slouken@6617
    78
slouken@6617
    79
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6617
    80
slouken@6617
    81
/* vi: set ts=4 sw=4 expandtab: */