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.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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 
    28 
    29 /* Display a UIKit message box */
    30 
    31 static SDL_bool s_showingMessageBox = SDL_FALSE;
    32 
    33 @interface UIKit_UIAlertViewDelegate : NSObject <UIAlertViewDelegate>
    34 
    35 - (id)initWithButtonIndex:(int *)_buttonIndex;
    36 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
    37 
    38 @end
    39 
    40 @implementation UIKit_UIAlertViewDelegate {
    41     int *clickedButtonIndex;
    42 }
    43 
    44 - (id)initWithButtonIndex:(int *)buttonIndex
    45 {
    46     self = [self init];
    47     if (self == nil) {
    48         return nil;
    49     }
    50 
    51     clickedButtonIndex = buttonIndex;
    52 
    53     return self;
    54 }
    55 
    56 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    57 {
    58     *clickedButtonIndex = (int)buttonIndex;
    59 }
    60 
    61 @end /* UIKit_UIAlertViewDelegate */
    62 
    63 
    64 SDL_bool
    65 UIKit_ShowingMessageBox()
    66 {
    67     return s_showingMessageBox;
    68 }
    69 
    70 int
    71 UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    72 {
    73     int clicked;
    74     int i;
    75     const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    76 
    77     @autoreleasepool {
    78         UIAlertView* alert = [[UIAlertView alloc] init];
    79 
    80         alert.title = @(messageboxdata->title);
    81         alert.message = @(messageboxdata->message);
    82         alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
    83 
    84         for (i = 0; i < messageboxdata->numbuttons; ++i) {
    85             [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
    86         }
    87 
    88         /* Set up for showing the alert */
    89         clicked = messageboxdata->numbuttons;
    90 
    91         [alert show];
    92 
    93         /* Run the main event loop until the alert has finished */
    94         /* Note that this needs to be done on the main thread */
    95         s_showingMessageBox = SDL_TRUE;
    96         while (clicked == messageboxdata->numbuttons) {
    97             [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    98         }
    99         s_showingMessageBox = SDL_FALSE;
   100 
   101         *buttonid = messageboxdata->buttons[clicked].buttonid;
   102 
   103         [alert.delegate release];
   104         [alert release];
   105     }
   106 
   107     return 0;
   108 }
   109 
   110 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   111 
   112 /* vi: set ts=4 sw=4 expandtab: */