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