src/video/uikit/SDL_uikitmessagebox.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 May 2015 06:27:46 -0700
changeset 9619 b94b6d0bff0f
parent 9532 318042c16b76
child 9879 8465a79c9f85
permissions -rw-r--r--
Updated the copyright year to 2015
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 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 SDLAlertViewDelegate : NSObject <UIAlertViewDelegate>
    34 
    35 @property (nonatomic, assign) int clickedIndex;
    36 
    37 @end
    38 
    39 @implementation SDLAlertViewDelegate
    40 
    41 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    42 {
    43     _clickedIndex = (int)buttonIndex;
    44 }
    45 
    46 @end
    47 
    48 
    49 SDL_bool
    50 UIKit_ShowingMessageBox()
    51 {
    52     return s_showingMessageBox;
    53 }
    54 
    55 int
    56 UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    57 {
    58     int i;
    59     const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    60 
    61     @autoreleasepool {
    62         UIAlertView *alert = [[UIAlertView alloc] init];
    63         SDLAlertViewDelegate *delegate = [[SDLAlertViewDelegate alloc] init];
    64 
    65         alert.delegate = delegate;
    66         alert.title = @(messageboxdata->title);
    67         alert.message = @(messageboxdata->message);
    68 
    69         for (i = 0; i < messageboxdata->numbuttons; ++i) {
    70             [alert addButtonWithTitle:@(buttons[i].text)];
    71         }
    72 
    73         /* Set up for showing the alert */
    74         delegate.clickedIndex = messageboxdata->numbuttons;
    75 
    76         [alert show];
    77 
    78         /* Run the main event loop until the alert has finished */
    79         /* Note that this needs to be done on the main thread */
    80         s_showingMessageBox = SDL_TRUE;
    81         while (delegate.clickedIndex == messageboxdata->numbuttons) {
    82             [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    83         }
    84         s_showingMessageBox = SDL_FALSE;
    85 
    86         *buttonid = messageboxdata->buttons[delegate.clickedIndex].buttonid;
    87 
    88         alert.delegate = nil;
    89     }
    90 
    91     return 0;
    92 }
    93 
    94 #endif /* SDL_VIDEO_DRIVER_UIKIT */
    95 
    96 /* vi: set ts=4 sw=4 expandtab: */