src/video/uikit/SDL_uikitmessagebox.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 18 May 2018 13:09:30 -0700
changeset 11983 3a50eb90e4b2
parent 11841 fe303e78ea49
child 12503 806492103856
permissions -rw-r--r--
Merged latest changes from Steam Link app
slouken@6619
     1
/*
slouken@6619
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@6619
     4
slouken@6619
     5
  This software is provided 'as-is', without any express or implied
slouken@6619
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6619
     7
  arising from the use of this software.
slouken@6619
     8
slouken@6619
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6619
    10
  including commercial applications, and to alter it and redistribute it
slouken@6619
    11
  freely, subject to the following restrictions:
slouken@6619
    12
slouken@6619
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6619
    14
     claim that you wrote the original software. If you use this software
slouken@6619
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6619
    16
     appreciated but is not required.
slouken@6619
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6619
    18
     misrepresented as being the original software.
slouken@6619
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6619
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@6619
    22
slouken@6619
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@6619
    24
slouken@6619
    25
#include "SDL.h"
slouken@6619
    26
#include "SDL_uikitvideo.h"
slime73@9879
    27
#include "SDL_uikitwindow.h"
slouken@6619
    28
slouken@6619
    29
/* Display a UIKit message box */
slouken@6619
    30
slouken@6686
    31
static SDL_bool s_showingMessageBox = SDL_FALSE;
slouken@6619
    32
slime73@9879
    33
SDL_bool
slouken@10721
    34
UIKit_ShowingMessageBox(void)
slime73@9879
    35
{
slime73@9879
    36
    return s_showingMessageBox;
slime73@9879
    37
}
slime73@9879
    38
slime73@9879
    39
static void
slime73@9879
    40
UIKit_WaitUntilMessageBoxClosed(const SDL_MessageBoxData *messageboxdata, int *clickedindex)
slime73@9879
    41
{
slime73@9879
    42
    *clickedindex = messageboxdata->numbuttons;
slime73@9879
    43
slime73@9879
    44
    @autoreleasepool {
slime73@9879
    45
        /* Run the main event loop until the alert has finished */
slime73@9879
    46
        /* Note that this needs to be done on the main thread */
slime73@9879
    47
        s_showingMessageBox = SDL_TRUE;
slime73@9879
    48
        while ((*clickedindex) == messageboxdata->numbuttons) {
slime73@9879
    49
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
slime73@9879
    50
        }
slime73@9879
    51
        s_showingMessageBox = SDL_FALSE;
slime73@9879
    52
    }
slime73@9879
    53
}
slime73@9879
    54
slime73@9879
    55
static BOOL
slime73@9879
    56
UIKit_ShowMessageBoxAlertController(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slime73@9879
    57
{
slouken@11506
    58
#ifdef __IPHONE_8_0
slime73@9879
    59
    int i;
slime73@9879
    60
    int __block clickedindex = messageboxdata->numbuttons;
slime73@9879
    61
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slime73@9879
    62
    UIWindow *window = nil;
slime73@9879
    63
    UIWindow *alertwindow = nil;
slime73@9879
    64
slime73@9879
    65
    if (![UIAlertController class]) {
slime73@9879
    66
        return NO;
slime73@9879
    67
    }
slime73@9879
    68
slime73@9879
    69
    UIAlertController *alert;
slime73@9879
    70
    alert = [UIAlertController alertControllerWithTitle:@(messageboxdata->title)
slime73@9879
    71
                                                message:@(messageboxdata->message)
slime73@9879
    72
                                         preferredStyle:UIAlertControllerStyleAlert];
slime73@9879
    73
slime73@9879
    74
    for (i = 0; i < messageboxdata->numbuttons; i++) {
slime73@9879
    75
        UIAlertAction *action;
slime73@9879
    76
        UIAlertActionStyle style = UIAlertActionStyleDefault;
slime73@9879
    77
slime73@9879
    78
        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
slime73@9879
    79
            style = UIAlertActionStyleCancel;
slime73@9879
    80
        }
slime73@9879
    81
slime73@9879
    82
        action = [UIAlertAction actionWithTitle:@(buttons[i].text)
slime73@9879
    83
                                          style:style
slime73@9879
    84
                                        handler:^(UIAlertAction *action) {
slime73@9879
    85
                                            clickedindex = i;
slime73@9879
    86
                                        }];
slime73@9879
    87
        [alert addAction:action];
slime73@9879
    88
    }
slime73@9879
    89
slime73@9879
    90
    if (messageboxdata->window) {
slime73@9879
    91
        SDL_WindowData *data = (__bridge SDL_WindowData *) messageboxdata->window->driverdata;
slime73@9879
    92
        window = data.uiwindow;
slime73@9879
    93
    }
slime73@9879
    94
slime73@9879
    95
    if (window == nil || window.rootViewController == nil) {
slime73@9879
    96
        alertwindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
slime73@9879
    97
        alertwindow.rootViewController = [UIViewController new];
slime73@9879
    98
        alertwindow.windowLevel = UIWindowLevelAlert;
slime73@9879
    99
slime73@9879
   100
        window = alertwindow;
slime73@9879
   101
slime73@9879
   102
        [alertwindow makeKeyAndVisible];
slime73@9879
   103
    }
slime73@9879
   104
slime73@9879
   105
    [window.rootViewController presentViewController:alert animated:YES completion:nil];
slime73@9879
   106
    UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex);
slime73@9879
   107
slime73@9879
   108
    if (alertwindow) {
slime73@9879
   109
        alertwindow.hidden = YES;
slime73@9879
   110
    }
slime73@9879
   111
slouken@11983
   112
#if !TARGET_OS_TV
slouken@11841
   113
    /* Force the main SDL window to re-evaluate home indicator state */
slouken@11841
   114
    SDL_Window *focus = SDL_GetFocusWindow();
slouken@11841
   115
    if (focus) {
slouken@11841
   116
        SDL_WindowData *data = (__bridge SDL_WindowData *) focus->driverdata;
slouken@11841
   117
        if (data != nil) {
slouken@11841
   118
            if (@available(iOS 11.0, *)) {
slouken@11841
   119
                [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfHomeIndicatorAutoHidden) withObject:nil waitUntilDone:NO];
slouken@11841
   120
                [data.viewcontroller performSelectorOnMainThread:@selector(setNeedsUpdateOfScreenEdgesDeferringSystemGestures) withObject:nil waitUntilDone:NO];
slouken@11841
   121
            }
slouken@11841
   122
        }
slouken@11841
   123
    }
slouken@11983
   124
#endif /* !TARGET_OS_TV */
slouken@11841
   125
slime73@9879
   126
    *buttonid = messageboxdata->buttons[clickedindex].buttonid;
slime73@9879
   127
    return YES;
slouken@11506
   128
#else
slouken@11506
   129
    return NO;
slouken@11506
   130
#endif /* __IPHONE_8_0 */
slime73@9879
   131
}
slime73@9879
   132
slime73@9879
   133
/* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
slime73@9879
   134
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
slime73@9532
   135
@interface SDLAlertViewDelegate : NSObject <UIAlertViewDelegate>
slouken@6619
   136
slime73@9879
   137
@property (nonatomic, assign) int *clickedIndex;
slouken@6619
   138
slouken@6619
   139
@end
slouken@6619
   140
slime73@9532
   141
@implementation SDLAlertViewDelegate
slouken@6619
   142
slime73@9499
   143
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
slouken@6619
   144
{
slime73@9879
   145
    if (_clickedIndex != NULL) {
slime73@9879
   146
        *_clickedIndex = (int) buttonIndex;
slime73@9879
   147
    }
slouken@6619
   148
}
slouken@6619
   149
slime73@9532
   150
@end
slime73@9879
   151
#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
slouken@6619
   152
slime73@9879
   153
static BOOL
slime73@9879
   154
UIKit_ShowMessageBoxAlertView(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slime73@9879
   155
{
slime73@9879
   156
    /* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
slime73@9879
   157
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
slime73@9879
   158
    int i;
slime73@9879
   159
    int clickedindex = messageboxdata->numbuttons;
slime73@9879
   160
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slime73@9879
   161
    UIAlertView *alert = [[UIAlertView alloc] init];
slime73@9879
   162
    SDLAlertViewDelegate *delegate = [[SDLAlertViewDelegate alloc] init];
slouken@6619
   163
slime73@9879
   164
    alert.delegate = delegate;
slime73@9879
   165
    alert.title = @(messageboxdata->title);
slime73@9879
   166
    alert.message = @(messageboxdata->message);
slime73@9879
   167
slime73@9879
   168
    for (i = 0; i < messageboxdata->numbuttons; i++) {
slime73@9879
   169
        [alert addButtonWithTitle:@(buttons[i].text)];
slime73@9879
   170
    }
slime73@9879
   171
slime73@9879
   172
    delegate.clickedIndex = &clickedindex;
slime73@9879
   173
slime73@9879
   174
    [alert show];
slime73@9879
   175
slime73@9879
   176
    UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex);
slime73@9879
   177
slime73@9879
   178
    alert.delegate = nil;
slime73@9879
   179
slime73@9879
   180
    *buttonid = messageboxdata->buttons[clickedindex].buttonid;
slime73@9879
   181
    return YES;
slime73@9879
   182
#else
slime73@9879
   183
    return NO;
slime73@9879
   184
#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
slouken@6686
   185
}
slouken@6686
   186
slouken@6619
   187
int
slouken@6619
   188
UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6619
   189
{
slime73@9879
   190
    BOOL success = NO;
slouken@7191
   191
slime73@9487
   192
    @autoreleasepool {
slime73@9879
   193
        success = UIKit_ShowMessageBoxAlertController(messageboxdata, buttonid);
slime73@9879
   194
        if (!success) {
slime73@9879
   195
            success = UIKit_ShowMessageBoxAlertView(messageboxdata, buttonid);
slime73@9879
   196
        }
slime73@9879
   197
    }
slouken@6619
   198
slime73@9879
   199
    if (!success) {
slime73@9879
   200
        return SDL_SetError("Could not show message box.");
slouken@6619
   201
    }
slouken@6619
   202
slouken@6619
   203
    return 0;
slouken@6619
   204
}
slouken@6619
   205
slouken@6619
   206
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6619
   207
slouken@6619
   208
/* vi: set ts=4 sw=4 expandtab: */