SDL  2.0
SDL_uikitmessagebox.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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_UIKIT
24 
25 #include "SDL.h"
26 #include "SDL_uikitvideo.h"
27 #include "SDL_uikitwindow.h"
28 
29 /* Display a UIKit message box */
30 
31 static SDL_bool s_showingMessageBox = SDL_FALSE;
32 
34 UIKit_ShowingMessageBox(void)
35 {
36  return s_showingMessageBox;
37 }
38 
39 static void
40 UIKit_WaitUntilMessageBoxClosed(const SDL_MessageBoxData *messageboxdata, int *clickedindex)
41 {
42  *clickedindex = messageboxdata->numbuttons;
43 
44  @autoreleasepool {
45  /* Run the main event loop until the alert has finished */
46  /* Note that this needs to be done on the main thread */
47  s_showingMessageBox = SDL_TRUE;
48  while ((*clickedindex) == messageboxdata->numbuttons) {
49  [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
50  }
51  s_showingMessageBox = SDL_FALSE;
52  }
53 }
54 
55 static BOOL
56 UIKit_ShowMessageBoxAlertController(const SDL_MessageBoxData *messageboxdata, int *buttonid)
57 {
58 #ifdef __IPHONE_8_0
59  int i;
60  int __block clickedindex = messageboxdata->numbuttons;
61  const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
62  UIWindow *window = nil;
63  UIWindow *alertwindow = nil;
64 
65  if (![UIAlertController class]) {
66  return NO;
67  }
68 
69  UIAlertController *alert;
70  alert = [UIAlertController alertControllerWithTitle:@(messageboxdata->title)
71  message:@(messageboxdata->message)
72  preferredStyle:UIAlertControllerStyleAlert];
73 
74  for (i = 0; i < messageboxdata->numbuttons; i++) {
75  UIAlertAction *action;
76  UIAlertActionStyle style = UIAlertActionStyleDefault;
77 
79  style = UIAlertActionStyleCancel;
80  }
81 
82  action = [UIAlertAction actionWithTitle:@(buttons[i].text)
83  style:style
84  handler:^(UIAlertAction *action) {
85  clickedindex = i;
86  }];
87  [alert addAction:action];
88  }
89 
90  if (messageboxdata->window) {
91  SDL_WindowData *data = (__bridge SDL_WindowData *) messageboxdata->window->driverdata;
92  window = data.uiwindow;
93  }
94 
95  if (window == nil || window.rootViewController == nil) {
96  alertwindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
97  alertwindow.rootViewController = [UIViewController new];
98  alertwindow.windowLevel = UIWindowLevelAlert;
99 
100  window = alertwindow;
101 
102  [alertwindow makeKeyAndVisible];
103  }
104 
105  [window.rootViewController presentViewController:alert animated:YES completion:nil];
106  UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex);
107 
108  if (alertwindow) {
109  alertwindow.hidden = YES;
110  }
111 
112  /* Force the main SDL window to re-evaluate home indicator state */
113  SDL_Window *focus = SDL_GetFocusWindow();
114  if (focus) {
115  SDL_WindowData *data = (__bridge SDL_WindowData *) focus->driverdata;
116  if (data != nil) {
117  if (@available(iOS 11.0, *)) {
118  [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden) withObject:nil waitUntilDone:NO];
119  [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfScreenEdgesDeferringSystemGestures) withObject:nil waitUntilDone:NO];
120  }
121  }
122  }
123 
124  *buttonid = messageboxdata->buttons[clickedindex].buttonid;
125  return YES;
126 #else
127  return NO;
128 #endif /* __IPHONE_8_0 */
129 }
130 
131 /* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
132 #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
133 @interface SDLAlertViewDelegate : NSObject <UIAlertViewDelegate>
134 
135 @property (nonatomic, assign) int *clickedIndex;
136 
137 @end
138 
139 @implementation SDLAlertViewDelegate
140 
141 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
142 {
143  if (_clickedIndex != NULL) {
144  *_clickedIndex = (int) buttonIndex;
145  }
146 }
147 
148 @end
149 #endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
150 
151 static BOOL
152 UIKit_ShowMessageBoxAlertView(const SDL_MessageBoxData *messageboxdata, int *buttonid)
153 {
154  /* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
155 #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
156  int i;
157  int clickedindex = messageboxdata->numbuttons;
158  const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
159  UIAlertView *alert = [[UIAlertView alloc] init];
160  SDLAlertViewDelegate *delegate = [[SDLAlertViewDelegate alloc] init];
161 
162  alert.delegate = delegate;
163  alert.title = @(messageboxdata->title);
164  alert.message = @(messageboxdata->message);
165 
166  for (i = 0; i < messageboxdata->numbuttons; i++) {
167  [alert addButtonWithTitle:@(buttons[i].text)];
168  }
169 
170  delegate.clickedIndex = &clickedindex;
171 
172  [alert show];
173 
174  UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex);
175 
176  alert.delegate = nil;
177 
178  *buttonid = messageboxdata->buttons[clickedindex].buttonid;
179  return YES;
180 #else
181  return NO;
182 #endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
183 }
184 
185 int
186 UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
187 {
188  BOOL success = NO;
189 
190  @autoreleasepool {
191  success = UIKit_ShowMessageBoxAlertController(messageboxdata, buttonid);
192  if (!success) {
193  success = UIKit_ShowMessageBoxAlertView(messageboxdata, buttonid);
194  }
195  }
196 
197  if (!success) {
198  return SDL_SetError("Could not show message box.");
199  }
200 
201  return 0;
202 }
203 
204 #endif /* SDL_VIDEO_DRIVER_UIKIT */
205 
206 /* vi: set ts=4 sw=4 expandtab: */
const char * message
const char * title
SDL_Window * window
static int available()
Definition: video.c:356
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
Individual button data.
SDL_Window * SDL_GetFocusWindow(void)
Definition: SDL_video.c:2594
const SDL_MessageBoxButtonData * buttons
MessageBox structure containing title, text, window, etc.
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_SetError
GLbitfield flags
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
The type used to identify a window.
Definition: SDL_sysvideo.h:73
void * driverdata
Definition: SDL_sysvideo.h:111