src/video/cocoa/SDL_cocoamessagebox.m
author Jørgen P. Tjernø
Mon, 22 Apr 2013 18:14:32 -0700
changeset 7088 664d8532219b
parent 6885 700f1b25f77f
child 7140 e1896b95a8a7
permissions -rw-r--r--
Make Cocoa_ShowMessageBox work in background threads.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 #if defined(__APPLE__) && defined(__POWERPC__)
    26 #include <altivec.h>
    27 #undef bool
    28 #undef vector
    29 #undef pixel
    30 #endif
    31 
    32 #include "SDL_messagebox.h"
    33 #include "SDL_cocoavideo.h"
    34 
    35 @interface SDLMessageBoxPresenter : NSObject {
    36 @public
    37     NSInteger clicked;
    38 }
    39 @end
    40 
    41 @implementation SDLMessageBoxPresenter
    42 - (id)init
    43 {
    44     self = [super init];
    45     if (self) {
    46         clicked = -1;
    47     }
    48 
    49     return self;
    50 }
    51 
    52 - (void)showAlert:(NSAlert*)alert
    53 {
    54     clicked = [alert runModal];
    55 }
    56 @end
    57 
    58 
    59 /* Display a Cocoa message box */
    60 int
    61 Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    62 {
    63     Cocoa_RegisterApp();
    64 
    65     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    66 
    67     NSAlert* alert = [[[NSAlert alloc] init] autorelease];
    68 
    69     if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    70         [alert setAlertStyle:NSCriticalAlertStyle];
    71     } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    72         [alert setAlertStyle:NSWarningAlertStyle];
    73     } else {
    74         [alert setAlertStyle:NSInformationalAlertStyle];
    75     }
    76 
    77     [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
    78     [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
    79 
    80     const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    81     int i;
    82     for (i = 0; i < messageboxdata->numbuttons; ++i) {
    83         NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
    84         if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    85             [button setKeyEquivalent:@"\r"];
    86         } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    87             [button setKeyEquivalent:@"\033"];
    88         } else {
    89             [button setKeyEquivalent:@""];
    90         }
    91     }
    92 
    93     SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] init] autorelease];
    94 
    95     [presenter performSelectorOnMainThread:@selector(showAlert:)
    96                                 withObject:alert
    97                              waitUntilDone:YES];
    98 
    99     int returnValue = 0;
   100     NSInteger clicked = presenter->clicked;
   101     if (clicked >= NSAlertFirstButtonReturn)
   102     {
   103         clicked -= NSAlertFirstButtonReturn;
   104         *buttonid = buttons[clicked].buttonid;
   105     }
   106     else
   107     {
   108         returnValue = SDL_SetError("Did not get a valid `clicked button' id: %d", clicked);
   109     }
   110 
   111     [pool release];
   112 
   113     return returnValue;
   114 }
   115 
   116 #endif /* SDL_VIDEO_DRIVER_COCOA */
   117 
   118 /* vi: set ts=4 sw=4 expandtab: */