src/video/cocoa/SDL_cocoamessagebox.m
author Kevin Shanahan
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.
slouken@6617
     1
/*
slouken@6617
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
kmshanah@7140
    25
#if defined(__APPLE__) && defined(__POWERPC__) && !defined(__APPLE_ALTIVEC__)
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
jorgen@7088
    35
@interface SDLMessageBoxPresenter : NSObject {
jorgen@7088
    36
@public
jorgen@7088
    37
    NSInteger clicked;
jorgen@7088
    38
}
jorgen@7088
    39
@end
jorgen@7088
    40
jorgen@7088
    41
@implementation SDLMessageBoxPresenter
jorgen@7088
    42
- (id)init
jorgen@7088
    43
{
jorgen@7088
    44
    self = [super init];
jorgen@7088
    45
    if (self) {
jorgen@7088
    46
        clicked = -1;
jorgen@7088
    47
    }
jorgen@7088
    48
jorgen@7088
    49
    return self;
jorgen@7088
    50
}
jorgen@7088
    51
jorgen@7088
    52
- (void)showAlert:(NSAlert*)alert
jorgen@7088
    53
{
jorgen@7088
    54
    clicked = [alert runModal];
jorgen@7088
    55
}
jorgen@7088
    56
@end
jorgen@7088
    57
slouken@6617
    58
slouken@6617
    59
/* Display a Cocoa message box */
slouken@6617
    60
int
slouken@6617
    61
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6617
    62
{
icculus@6639
    63
    Cocoa_RegisterApp();
icculus@6639
    64
slouken@6848
    65
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    66
jorgen@7088
    67
    NSAlert* alert = [[[NSAlert alloc] init] autorelease];
slouken@6617
    68
slouken@6848
    69
    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
slouken@6848
    70
        [alert setAlertStyle:NSCriticalAlertStyle];
slouken@6848
    71
    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
slouken@6848
    72
        [alert setAlertStyle:NSWarningAlertStyle];
slouken@6848
    73
    } else {
slouken@6848
    74
        [alert setAlertStyle:NSInformationalAlertStyle];
slouken@6848
    75
    }
slouken@6617
    76
slouken@6848
    77
    [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
slouken@6848
    78
    [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
slouken@6617
    79
slouken@6848
    80
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slouken@6848
    81
    int i;
slouken@6848
    82
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
slouken@6848
    83
        NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
slouken@6848
    84
        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
slouken@6848
    85
            [button setKeyEquivalent:@"\r"];
slouken@6848
    86
        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
slouken@6848
    87
            [button setKeyEquivalent:@"\033"];
slouken@6848
    88
        } else {
slouken@6848
    89
            [button setKeyEquivalent:@""];
slouken@6617
    90
        }
slouken@6848
    91
    }
alexey@6832
    92
jorgen@7088
    93
    SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] init] autorelease];
jorgen@7088
    94
jorgen@7088
    95
    [presenter performSelectorOnMainThread:@selector(showAlert:)
jorgen@7088
    96
                                withObject:alert
jorgen@7088
    97
                             waitUntilDone:YES];
jorgen@7088
    98
jorgen@7088
    99
    int returnValue = 0;
jorgen@7088
   100
    NSInteger clicked = presenter->clicked;
jorgen@7088
   101
    if (clicked >= NSAlertFirstButtonReturn)
jorgen@7088
   102
    {
jorgen@7088
   103
        clicked -= NSAlertFirstButtonReturn;
jorgen@7088
   104
        *buttonid = buttons[clicked].buttonid;
jorgen@7088
   105
    }
jorgen@7088
   106
    else
jorgen@7088
   107
    {
jorgen@7088
   108
        returnValue = SDL_SetError("Did not get a valid `clicked button' id: %d", clicked);
jorgen@7088
   109
    }
slouken@6848
   110
slouken@6848
   111
    [pool release];
slouken@6617
   112
jorgen@7088
   113
    return returnValue;
slouken@6617
   114
}
slouken@6617
   115
slouken@6617
   116
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6617
   117
slouken@6617
   118
/* vi: set ts=4 sw=4 expandtab: */