src/video/cocoa/SDL_cocoamessagebox.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Jun 2014 11:36:00 -0700
changeset 8884 a13802298fdd
parent 8149 681eb46b8ac4
child 8986 1c81316ac642
permissions -rw-r--r--
Fixed compiler warning
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_COCOA
    24 
    25 #if defined(__APPLE__) && defined(__POWERPC__) && !defined(__APPLE_ALTIVEC__)
    26 #include <altivec.h>
    27 #undef bool
    28 #undef vector
    29 #undef pixel
    30 #endif
    31 
    32 #include "SDL_events.h"
    33 #include "SDL_timer.h"
    34 #include "SDL_messagebox.h"
    35 #include "SDL_cocoavideo.h"
    36 
    37 @interface SDLMessageBoxPresenter : NSObject {
    38 @public
    39     NSInteger clicked;
    40     NSWindow *nswindow;
    41 }
    42 - (id) initWithParentWindow:(SDL_Window *)window;
    43 - (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
    44 @end
    45 
    46 @implementation SDLMessageBoxPresenter
    47 - (id) initWithParentWindow:(SDL_Window *)window
    48 {
    49     self = [super init];
    50     if (self) {
    51         clicked = -1;
    52 
    53         /* Retain the NSWindow because we'll show the alert later on the main thread */
    54         if (window) {
    55             nswindow = [((SDL_WindowData *) window->driverdata)->nswindow retain];
    56         } else {
    57             nswindow = NULL;
    58         }
    59     }
    60 
    61     return self;
    62 }
    63 
    64 - (void)showAlert:(NSAlert*)alert
    65 {
    66     if (nswindow) {
    67         [alert beginSheetModalForWindow:nswindow modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
    68         while (clicked < 0) {
    69             SDL_PumpEvents();
    70             SDL_Delay(100);
    71         }
    72         [nswindow release];
    73     } else {
    74         clicked = [alert runModal];
    75     }
    76 }
    77 
    78 - (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
    79 {
    80     clicked = returnCode;
    81 }
    82 
    83 @end
    84 
    85 
    86 /* Display a Cocoa message box */
    87 int
    88 Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    89 {
    90     Cocoa_RegisterApp();
    91 
    92     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    93 
    94     NSAlert* alert = [[[NSAlert alloc] init] autorelease];
    95 
    96     if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    97         [alert setAlertStyle:NSCriticalAlertStyle];
    98     } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    99         [alert setAlertStyle:NSWarningAlertStyle];
   100     } else {
   101         [alert setAlertStyle:NSInformationalAlertStyle];
   102     }
   103 
   104     [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
   105     [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
   106 
   107     const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
   108     int i;
   109     for (i = 0; i < messageboxdata->numbuttons; ++i) {
   110         NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
   111         if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
   112             [button setKeyEquivalent:@"\r"];
   113         } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
   114             [button setKeyEquivalent:@"\033"];
   115         } else {
   116             [button setKeyEquivalent:@""];
   117         }
   118     }
   119 
   120     SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease];
   121 
   122     [presenter performSelectorOnMainThread:@selector(showAlert:)
   123                                 withObject:alert
   124                              waitUntilDone:YES];
   125 
   126     int returnValue = 0;
   127     NSInteger clicked = presenter->clicked;
   128     if (clicked >= NSAlertFirstButtonReturn)
   129     {
   130         clicked -= NSAlertFirstButtonReturn;
   131         *buttonid = buttons[clicked].buttonid;
   132     }
   133     else
   134     {
   135         returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
   136     }
   137 
   138     [pool release];
   139 
   140     return returnValue;
   141 }
   142 
   143 #endif /* SDL_VIDEO_DRIVER_COCOA */
   144 
   145 /* vi: set ts=4 sw=4 expandtab: */