1/*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2021 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#include "SDL_events.h"
26#include "SDL_timer.h"
27#include "SDL_messagebox.h"
28#include "SDL_cocoavideo.h"
29
30@interface SDLMessageBoxPresenter : NSObject {
31@public
32    NSInteger clicked;
33    NSWindow *nswindow;
34}
35- (id) initWithParentWindow:(SDL_Window *)window;
36- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
37@end
38
39@implementation SDLMessageBoxPresenter
40- (id) initWithParentWindow:(SDL_Window *)window
41{
42    self = [super init];
43    if (self) {
44        clicked = -1;
45
46        /* Retain the NSWindow because we'll show the alert later on the main thread */
47        if (window) {
48            nswindow = [((SDL_WindowData *) window->driverdata)->nswindow retain];
49        } else {
50            nswindow = NULL;
51        }
52    }
53
54    return self;
55}
56
57- (void)showAlert:(NSAlert*)alert
58{
59    if (nswindow) {
60#ifdef MAC_OS_X_VERSION_10_9
61        if ([alert respondsToSelector:@selector(beginSheetModalForWindow:completionHandler:)]) {
62            [alert beginSheetModalForWindow:nswindow completionHandler:^(NSModalResponse returnCode) {
63                clicked = returnCode;
64            }];
65        } else
66#endif
67        {
68#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
69            [alert beginSheetModalForWindow:nswindow modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
70#endif
71        }
72
73        while (clicked < 0) {
74            SDL_PumpEvents();
75            SDL_Delay(100);
76        }
77
78        [nswindow release];
79    } else {
80        clicked = [alert runModal];
81    }
82}
83
84- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
85{
86    clicked = returnCode;
87}
88
89@end
90
91
92static void
93Cocoa_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid, int *returnValue)
94{
95    Cocoa_RegisterApp();
96
97    NSAlert* alert = [[[NSAlert alloc] init] autorelease];
98
99    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
100        [alert setAlertStyle:NSAlertStyleCritical];
101    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
102        [alert setAlertStyle:NSAlertStyleWarning];
103    } else {
104        [alert setAlertStyle:NSAlertStyleInformational];
105    }
106
107    [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
108    [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
109
110    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
111    int i;
112    for (i = 0; i < messageboxdata->numbuttons; ++i) {
113        const SDL_MessageBoxButtonData *sdlButton;
114        NSButton *button;
115
116        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
117            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
118        } else {
119            sdlButton = &messageboxdata->buttons[i];
120        }
121
122        button = [alert addButtonWithTitle:[NSString stringWithUTF8String:sdlButton->text]];
123        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
124            [button setKeyEquivalent:@"\r"];
125        } else if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
126            [button setKeyEquivalent:@"\033"];
127        } else {
128            [button setKeyEquivalent:@""];
129        }
130    }
131
132    SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease];
133
134    [presenter showAlert:alert];
135
136    NSInteger clicked = presenter->clicked;
137    if (clicked >= NSAlertFirstButtonReturn) {
138        clicked -= NSAlertFirstButtonReturn;
139        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
140            clicked = messageboxdata->numbuttons - 1 - clicked;
141        }
142        *buttonid = buttons[clicked].buttonid;
143        *returnValue = 0;
144    } else {
145        *returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
146    }
147}
148
149/* Display a Cocoa message box */
150int
151Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
152{ @autoreleasepool
153{
154    __block int returnValue = 0;
155
156    if ([NSThread isMainThread]) {
157        Cocoa_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue);
158    } else {
159        dispatch_sync(dispatch_get_main_queue(), ^{ Cocoa_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue); });
160    }
161    return returnValue;
162}}
163
164#endif /* SDL_VIDEO_DRIVER_COCOA */
165
166/* vi: set ts=4 sw=4 expandtab: */
167