src/video/uikit/SDL_uikitmessagebox.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8983 21f5923cfd15
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
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
slouken@6619
    33
@interface UIKit_UIAlertViewDelegate : NSObject <UIAlertViewDelegate> {
slouken@6619
    34
@private
slouken@6619
    35
    int *clickedButtonIndex;
slouken@6619
    36
}
slouken@6619
    37
slouken@6619
    38
- (id)initWithButtonIndex:(int *)_buttonIndex;
slouken@6619
    39
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
slouken@6619
    40
slouken@6619
    41
@end
slouken@6619
    42
slouken@6619
    43
@implementation UIKit_UIAlertViewDelegate
slouken@6619
    44
slouken@6619
    45
- (id)initWithButtonIndex:(int *)buttonIndex
slouken@6619
    46
{
slouken@6619
    47
    self = [self init];
slouken@6619
    48
    if (self == nil) {
slouken@6619
    49
        return nil;
slouken@6619
    50
    }
slouken@6619
    51
    self->clickedButtonIndex = buttonIndex;
slouken@7191
    52
slouken@6619
    53
    return self;
slouken@6619
    54
}
slouken@6619
    55
slouken@6619
    56
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
slouken@6619
    57
{
slouken@6619
    58
    *clickedButtonIndex = 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;
slouken@7191
    74
slouken@6619
    75
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6619
    76
slouken@6619
    77
    UIAlertView* alert = [[UIAlertView alloc] init];
slouken@6619
    78
slouken@6714
    79
    alert.title = [NSString stringWithUTF8String:messageboxdata->title];
slouken@6714
    80
    alert.message = [NSString stringWithUTF8String:messageboxdata->message];
slouken@6619
    81
    alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
slouken@6619
    82
slouken@6619
    83
    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
slouken@6619
    84
    int i;
slouken@6619
    85
    for (i = 0; i < messageboxdata->numbuttons; ++i) {
slouken@6619
    86
        [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
slouken@6619
    87
    }
slouken@6619
    88
slouken@7191
    89
    /* Set up for showing the alert */
slouken@6619
    90
    clicked = messageboxdata->numbuttons;
slouken@6619
    91
slouken@6619
    92
    [alert show];
slouken@7191
    93
slouken@7191
    94
    /* Run the main event loop until the alert has finished */
slouken@7191
    95
    /* Note that this needs to be done on the main thread */
slouken@6686
    96
    s_showingMessageBox = SDL_TRUE;
slouken@6619
    97
    while (clicked == messageboxdata->numbuttons) {
slouken@6619
    98
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
slouken@6619
    99
    }
slouken@6686
   100
    s_showingMessageBox = SDL_FALSE;
slouken@6686
   101
slouken@6619
   102
    *buttonid = messageboxdata->buttons[clicked].buttonid;
slouken@7191
   103
slouken@6714
   104
    [alert.delegate release];
slouken@6714
   105
    [alert release];
slouken@6619
   106
slouken@6619
   107
    [pool release];
slouken@6619
   108
slouken@6619
   109
    return 0;
slouken@6619
   110
}
slouken@6619
   111
slouken@6619
   112
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6619
   113
slouken@6619
   114
/* vi: set ts=4 sw=4 expandtab: */