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