author | Sam Lantinga |
Sat, 02 Jan 2016 10:10:34 -0800 | |
changeset 9998 | f67cf37e9cd4 |
parent 9619 | b94b6d0bff0f |
child 10737 | 3406a0f8b041 |
permissions | -rw-r--r-- |
slouken@6617 | 1 |
/* |
slouken@6617 | 2 |
Simple DirectMedia Layer |
slouken@9998 | 3 |
Copyright (C) 1997-2016 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 |
*/ |
icculus@8093 | 21 |
#include "../../SDL_internal.h" |
slouken@6617 | 22 |
|
slouken@6617 | 23 |
#if SDL_VIDEO_DRIVER_COCOA |
slouken@6617 | 24 |
|
slouken@7457 | 25 |
#include "SDL_events.h" |
slouken@7457 | 26 |
#include "SDL_timer.h" |
slouken@6623 | 27 |
#include "SDL_messagebox.h" |
slouken@6617 | 28 |
#include "SDL_cocoavideo.h" |
slouken@6617 | 29 |
|
jorgen@7088 | 30 |
@interface SDLMessageBoxPresenter : NSObject { |
jorgen@7088 | 31 |
@public |
jorgen@7088 | 32 |
NSInteger clicked; |
slouken@7457 | 33 |
NSWindow *nswindow; |
jorgen@7088 | 34 |
} |
slouken@7457 | 35 |
- (id) initWithParentWindow:(SDL_Window *)window; |
slouken@7457 | 36 |
- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; |
jorgen@7088 | 37 |
@end |
jorgen@7088 | 38 |
|
jorgen@7088 | 39 |
@implementation SDLMessageBoxPresenter |
slouken@7457 | 40 |
- (id) initWithParentWindow:(SDL_Window *)window |
jorgen@7088 | 41 |
{ |
jorgen@7088 | 42 |
self = [super init]; |
jorgen@7088 | 43 |
if (self) { |
jorgen@7088 | 44 |
clicked = -1; |
slouken@7457 | 45 |
|
slouken@7457 | 46 |
/* Retain the NSWindow because we'll show the alert later on the main thread */ |
slouken@7457 | 47 |
if (window) { |
slouken@7457 | 48 |
nswindow = [((SDL_WindowData *) window->driverdata)->nswindow retain]; |
slouken@7457 | 49 |
} else { |
slouken@7457 | 50 |
nswindow = NULL; |
slouken@7457 | 51 |
} |
jorgen@7088 | 52 |
} |
jorgen@7088 | 53 |
|
jorgen@7088 | 54 |
return self; |
jorgen@7088 | 55 |
} |
jorgen@7088 | 56 |
|
jorgen@7088 | 57 |
- (void)showAlert:(NSAlert*)alert |
jorgen@7088 | 58 |
{ |
slouken@7457 | 59 |
if (nswindow) { |
slouken@7457 | 60 |
[alert beginSheetModalForWindow:nswindow modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil]; |
slouken@7457 | 61 |
while (clicked < 0) { |
slouken@7457 | 62 |
SDL_PumpEvents(); |
slouken@7457 | 63 |
SDL_Delay(100); |
slouken@7457 | 64 |
} |
slouken@7457 | 65 |
[nswindow release]; |
slouken@7457 | 66 |
} else { |
slouken@7457 | 67 |
clicked = [alert runModal]; |
slouken@7457 | 68 |
} |
jorgen@7088 | 69 |
} |
slouken@7457 | 70 |
|
slouken@7457 | 71 |
- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo |
slouken@7457 | 72 |
{ |
slouken@7457 | 73 |
clicked = returnCode; |
slouken@7457 | 74 |
} |
slouken@7457 | 75 |
|
jorgen@7088 | 76 |
@end |
jorgen@7088 | 77 |
|
slouken@6617 | 78 |
|
slouken@6617 | 79 |
/* Display a Cocoa message box */ |
slouken@6617 | 80 |
int |
slouken@6617 | 81 |
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid) |
slouken@9087 | 82 |
{ @autoreleasepool |
slouken@6617 | 83 |
{ |
icculus@6639 | 84 |
Cocoa_RegisterApp(); |
icculus@6639 | 85 |
|
jorgen@7088 | 86 |
NSAlert* alert = [[[NSAlert alloc] init] autorelease]; |
slouken@6617 | 87 |
|
slouken@6848 | 88 |
if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) { |
slouken@6848 | 89 |
[alert setAlertStyle:NSCriticalAlertStyle]; |
slouken@6848 | 90 |
} else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) { |
slouken@6848 | 91 |
[alert setAlertStyle:NSWarningAlertStyle]; |
slouken@6848 | 92 |
} else { |
slouken@6848 | 93 |
[alert setAlertStyle:NSInformationalAlertStyle]; |
slouken@6848 | 94 |
} |
slouken@6617 | 95 |
|
slouken@6848 | 96 |
[alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]]; |
slouken@6848 | 97 |
[alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]]; |
slouken@6617 | 98 |
|
slouken@6848 | 99 |
const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons; |
slouken@6848 | 100 |
int i; |
slouken@6848 | 101 |
for (i = 0; i < messageboxdata->numbuttons; ++i) { |
slouken@6848 | 102 |
NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]]; |
slouken@6848 | 103 |
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) { |
slouken@6848 | 104 |
[button setKeyEquivalent:@"\r"]; |
slouken@6848 | 105 |
} else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) { |
slouken@6848 | 106 |
[button setKeyEquivalent:@"\033"]; |
slouken@6848 | 107 |
} else { |
slouken@6848 | 108 |
[button setKeyEquivalent:@""]; |
slouken@6617 | 109 |
} |
slouken@6848 | 110 |
} |
alexey@6832 | 111 |
|
slouken@7457 | 112 |
SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease]; |
jorgen@7088 | 113 |
|
jorgen@7088 | 114 |
[presenter performSelectorOnMainThread:@selector(showAlert:) |
jorgen@7088 | 115 |
withObject:alert |
jorgen@7088 | 116 |
waitUntilDone:YES]; |
jorgen@7088 | 117 |
|
jorgen@7088 | 118 |
int returnValue = 0; |
jorgen@7088 | 119 |
NSInteger clicked = presenter->clicked; |
slouken@8986 | 120 |
if (clicked >= NSAlertFirstButtonReturn) { |
jorgen@7088 | 121 |
clicked -= NSAlertFirstButtonReturn; |
jorgen@7088 | 122 |
*buttonid = buttons[clicked].buttonid; |
slouken@8986 | 123 |
} else { |
slouken@8884 | 124 |
returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked); |
jorgen@7088 | 125 |
} |
slouken@6848 | 126 |
|
jorgen@7088 | 127 |
return returnValue; |
slouken@9087 | 128 |
}} |
slouken@6617 | 129 |
|
slouken@6617 | 130 |
#endif /* SDL_VIDEO_DRIVER_COCOA */ |
slouken@6617 | 131 |
|
slouken@6617 | 132 |
/* vi: set ts=4 sw=4 expandtab: */ |