src/video/uikit/SDL_uikitmessagebox.m
author Alex Szpakowski <slime73@gmail.com>
Wed, 23 Jul 2014 03:05:31 -0300
branchiOS-improvements
changeset 9501 574db299498f
parent 9499 6fe9b44b2d84
child 9505 6fc615dfc93f
permissions -rw-r--r--
More cleanup of the iOS Objective-C code.
slouken@6619
     1
/*
slouken@6619
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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"
slouken@6619
    27
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@9501
    33
@interface UIKit_UIAlertViewDelegate : NSObject <UIAlertViewDelegate>
slouken@6619
    34
slouken@6619
    35
- (id)initWithButtonIndex:(int *)_buttonIndex;
slouken@6619
    36
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
slouken@6619
    37
slouken@6619
    38
@end
slouken@6619
    39
slime73@9501
    40
@implementation UIKit_UIAlertViewDelegate {
slime73@9501
    41
    int *clickedButtonIndex;
slime73@9501
    42
}
slouken@6619
    43
slouken@6619
    44
- (id)initWithButtonIndex:(int *)buttonIndex
slouken@6619
    45
{
slouken@6619
    46
    self = [self init];
slouken@6619
    47
    if (self == nil) {
slouken@6619
    48
        return nil;
slouken@6619
    49
    }
slime73@9501
    50
slime73@9501
    51
    clickedButtonIndex = buttonIndex;
slouken@7191
    52
slouken@6619
    53
    return self;
slouken@6619
    54
}
slouken@6619
    55
slime73@9499
    56
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
slouken@6619
    57
{
slouken@8983
    58
    *clickedButtonIndex = (int)buttonIndex;
slouken@6619
    59
}
slouken@6619
    60
slouken@7191
    61
@end /* UIKit_UIAlertViewDelegate */
slouken@6619
    62
slouken@6619
    63
slouken@6686
    64
SDL_bool
slouken@6686
    65
UIKit_ShowingMessageBox()
slouken@6686
    66
{
slouken@6686
    67
    return s_showingMessageBox;
slouken@6686
    68
}
slouken@6686
    69
slouken@6619
    70
int
slouken@6619
    71
UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6619
    72
{
slouken@6619
    73
    int clicked;
slime73@9487
    74
    int i;
slime73@9487
    75
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slouken@7191
    76
slime73@9487
    77
    @autoreleasepool {
slime73@9487
    78
        UIAlertView* alert = [[UIAlertView alloc] init];
slouken@6619
    79
slime73@9499
    80
        alert.title = @(messageboxdata->title);
slime73@9499
    81
        alert.message = @(messageboxdata->message);
slime73@9487
    82
        alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
slouken@6619
    83
slime73@9487
    84
        for (i = 0; i < messageboxdata->numbuttons; ++i) {
slime73@9487
    85
            [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
slime73@9487
    86
        }
slouken@6619
    87
slime73@9487
    88
        /* Set up for showing the alert */
slime73@9487
    89
        clicked = messageboxdata->numbuttons;
slime73@9487
    90
slime73@9487
    91
        [alert show];
slime73@9487
    92
slime73@9487
    93
        /* Run the main event loop until the alert has finished */
slime73@9487
    94
        /* Note that this needs to be done on the main thread */
slime73@9487
    95
        s_showingMessageBox = SDL_TRUE;
slime73@9487
    96
        while (clicked == messageboxdata->numbuttons) {
slime73@9487
    97
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
slime73@9487
    98
        }
slime73@9487
    99
        s_showingMessageBox = SDL_FALSE;
slime73@9487
   100
slime73@9487
   101
        *buttonid = messageboxdata->buttons[clicked].buttonid;
slime73@9487
   102
slime73@9487
   103
        [alert.delegate release];
slime73@9487
   104
        [alert release];
slouken@6619
   105
    }
slouken@6619
   106
slouken@6619
   107
    return 0;
slouken@6619
   108
}
slouken@6619
   109
slouken@6619
   110
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6619
   111
slouken@6619
   112
/* vi: set ts=4 sw=4 expandtab: */