src/video/cocoa/SDL_cocoamessagebox.m
author Kevin Shanahan <kmshanah@disenchant.net>
Fri, 03 May 2013 14:11:41 +0930
changeset 7140 e1896b95a8a7
parent 7088 664d8532219b
child 7457 8e6dfbc8ac6b
permissions -rw-r--r--
Fix compile on Xcode 3.1 PPC + Altivec -faltivec is given on the command line, so #include <altivec.h> is ignored in this case and #undef'ing vector causes compile to fail.
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
6617
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
7140
e1896b95a8a7 Fix compile on Xcode 3.1 PPC + Altivec
Kevin Shanahan <kmshanah@disenchant.net>
parents: 7088
diff changeset
    25
#if defined(__APPLE__) && defined(__POWERPC__) && !defined(__APPLE_ALTIVEC__)
6624
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
7088
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    35
@interface SDLMessageBoxPresenter : NSObject {
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    36
@public
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    37
    NSInteger clicked;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    38
}
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    39
@end
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    40
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    41
@implementation SDLMessageBoxPresenter
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    42
- (id)init
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    43
{
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    44
    self = [super init];
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    45
    if (self) {
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    46
        clicked = -1;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    47
    }
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    48
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    49
    return self;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    50
}
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    51
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    52
- (void)showAlert:(NSAlert*)alert
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    53
{
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    54
    clicked = [alert runModal];
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    55
}
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    56
@end
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    57
6617
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
/* Display a Cocoa message box */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
int
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
{
6639
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    63
    Cocoa_RegisterApp();
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    64
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    65
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    66
7088
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    67
    NSAlert* alert = [[[NSAlert alloc] init] autorelease];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    69
    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    70
        [alert setAlertStyle:NSCriticalAlertStyle];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    71
    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    72
        [alert setAlertStyle:NSWarningAlertStyle];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    73
    } else {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    74
        [alert setAlertStyle:NSInformationalAlertStyle];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    75
    }
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    77
    [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    78
    [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    80
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    81
    int i;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    82
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    83
        NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    84
        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    85
            [button setKeyEquivalent:@"\r"];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    86
        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    87
            [button setKeyEquivalent:@"\033"];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    88
        } else {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    89
            [button setKeyEquivalent:@""];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
        }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
    91
    }
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    92
7088
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    93
    SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] init] autorelease];
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    94
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    95
    [presenter performSelectorOnMainThread:@selector(showAlert:)
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    96
                                withObject:alert
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    97
                             waitUntilDone:YES];
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    98
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
    99
    int returnValue = 0;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   100
    NSInteger clicked = presenter->clicked;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   101
    if (clicked >= NSAlertFirstButtonReturn)
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   102
    {
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   103
        clicked -= NSAlertFirstButtonReturn;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   104
        *buttonid = buttons[clicked].buttonid;
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   105
    }
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   106
    else
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   107
    {
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   108
        returnValue = SDL_SetError("Did not get a valid `clicked button' id: %d", clicked);
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   109
    }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   110
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   111
    [pool release];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
7088
664d8532219b Make Cocoa_ShowMessageBox work in background threads.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 6885
diff changeset
   113
    return returnValue;
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
}
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
#endif /* SDL_VIDEO_DRIVER_COCOA */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
/* vi: set ts=4 sw=4 expandtab: */