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.
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
6624
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    25
#if defined(__APPLE__) && defined(__POWERPC__)
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    26
#include <altivec.h>
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    27
#undef bool
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    28
#undef vector
6626
4aa5af31f2f9 Works better when you put it in the right file. :)
Ryan C. Gordon <icculus@icculus.org>
parents: 6624
diff changeset
    29
#undef pixel
6624
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    30
#endif
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    31
6623
f4ae87f3ec15 Maybe fix PPC build error?
Sam Lantinga <slouken@libsdl.org>
parents: 6617
diff changeset
    32
#include "SDL_messagebox.h"
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#include "SDL_cocoavideo.h"
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
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
/* Display a Cocoa message box */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
int
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
{
6639
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    40
    Cocoa_RegisterApp();
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    41
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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
    NSAlert* alert = [[NSAlert alloc] init];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
        [alert setAlertStyle:NSCriticalAlertStyle];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
        [alert setAlertStyle:NSWarningAlertStyle];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    } else {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
        [alert setAlertStyle:NSInformationalAlertStyle];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    }
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    [alert setMessageText:[[NSString alloc] initWithUTF8String:messageboxdata->title]];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    [alert setInformativeText:[[NSString alloc] initWithUTF8String:messageboxdata->message]];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
    int i;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
        NSButton *button = [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
            [button setKeyEquivalent:@"\r"];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
            [button setKeyEquivalent:@"\033"];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
        } else {
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
            [button setKeyEquivalent:@""];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
        }
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
    NSInteger clicked = [alert runModal];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    clicked -= NSAlertFirstButtonReturn;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    *buttonid = buttons[clicked].buttonid;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
    [pool release];
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    return 0;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
}
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
#endif /* SDL_VIDEO_DRIVER_COCOA */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
/* vi: set ts=4 sw=4 expandtab: */