src/video/uikit/SDL_uikitmessagebox.m
author Sam Lantinga
Fri, 27 Jan 2017 06:05:50 -0800
changeset 10856 486aa38c6a88
parent 10737 3406a0f8b041
child 11505 4f3f29b6b666
permissions -rw-r--r--
Added Thrustmaster Wheel FFB entry to the list of wheels
slouken@6619
     1
/*
slouken@6619
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
slouken@6686
    33
SDL_bool
slouken@10721
    34
UIKit_ShowingMessageBox(void)
slouken@6686
    35
{
slouken@6686
    36
    return s_showingMessageBox;
slouken@6686
    37
}
slouken@6686
    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
{
slime73@9879
    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
slime73@9879
   112
    *buttonid = messageboxdata->buttons[clickedindex].buttonid;
slime73@9879
   113
    return YES;
slime73@9879
   114
#else
slime73@9879
   115
    return NO;
slime73@9879
   116
#endif /* __IPHONE_8_0 */
slime73@9879
   117
}
slime73@9879
   118
slime73@9879
   119
/* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
slime73@9879
   120
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
slime73@9879
   121
@interface SDLAlertViewDelegate : NSObject <UIAlertViewDelegate>
slime73@9879
   122
slime73@9879
   123
@property (nonatomic, assign) int *clickedIndex;
slime73@9879
   124
slime73@9879
   125
@end
slime73@9879
   126
slime73@9879
   127
@implementation SDLAlertViewDelegate
slime73@9879
   128
slime73@9879
   129
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
slime73@9879
   130
{
slime73@9879
   131
    if (_clickedIndex != NULL) {
slime73@9879
   132
        *_clickedIndex = (int) buttonIndex;
slime73@9879
   133
    }
slime73@9879
   134
}
slime73@9879
   135
slime73@9879
   136
@end
slime73@9879
   137
#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
slime73@9879
   138
slime73@9879
   139
static BOOL
slime73@9879
   140
UIKit_ShowMessageBoxAlertView(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slime73@9879
   141
{
slime73@9879
   142
    /* UIAlertView is deprecated in iOS 8+ in favor of UIAlertController. */
slime73@9879
   143
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
slime73@9879
   144
    int i;
slime73@9879
   145
    int clickedindex = messageboxdata->numbuttons;
slime73@9879
   146
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slime73@9879
   147
    UIAlertView *alert = [[UIAlertView alloc] init];
slime73@9879
   148
    SDLAlertViewDelegate *delegate = [[SDLAlertViewDelegate alloc] init];
slime73@9879
   149
slime73@9879
   150
    alert.delegate = delegate;
slime73@9879
   151
    alert.title = @(messageboxdata->title);
slime73@9879
   152
    alert.message = @(messageboxdata->message);
slime73@9879
   153
slime73@9879
   154
    for (i = 0; i < messageboxdata->numbuttons; i++) {
slime73@9879
   155
        [alert addButtonWithTitle:@(buttons[i].text)];
slime73@9879
   156
    }
slime73@9879
   157
slime73@9879
   158
    delegate.clickedIndex = &clickedindex;
slime73@9879
   159
slime73@9879
   160
    [alert show];
slime73@9879
   161
slime73@9879
   162
    UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex);
slime73@9879
   163
slime73@9879
   164
    alert.delegate = nil;
slime73@9879
   165
slime73@9879
   166
    *buttonid = messageboxdata->buttons[clickedindex].buttonid;
slime73@9879
   167
    return YES;
slime73@9879
   168
#else
slime73@9879
   169
    return NO;
slime73@9879
   170
#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
slime73@9879
   171
}
slime73@9879
   172
slouken@6619
   173
int
slouken@6619
   174
UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6619
   175
{
slime73@9879
   176
    BOOL success = NO;
slouken@7191
   177
slime73@9487
   178
    @autoreleasepool {
slime73@9879
   179
        success = UIKit_ShowMessageBoxAlertController(messageboxdata, buttonid);
slime73@9879
   180
        if (!success) {
slime73@9879
   181
            success = UIKit_ShowMessageBoxAlertView(messageboxdata, buttonid);
slime73@9487
   182
        }
slime73@9879
   183
    }
slouken@6619
   184
slime73@9879
   185
    if (!success) {
slime73@9879
   186
        return SDL_SetError("Could not show message box.");
slouken@6619
   187
    }
slouken@6619
   188
slouken@6619
   189
    return 0;
slouken@6619
   190
}
slouken@6619
   191
slouken@6619
   192
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6619
   193
slouken@6619
   194
/* vi: set ts=4 sw=4 expandtab: */