Added UIKit implementation of SDL messagebox
authorSam Lantinga <slouken@libsdl.org>
Tue, 30 Oct 2012 13:44:59 -0700
changeset 661942214b6959c5
parent 6618 196b8f8dfc0a
child 6620 62fe18dc6841
Added UIKit implementation of SDL messagebox
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
src/video/SDL_video.c
src/video/uikit/SDL_uikitmessagebox.h
src/video/uikit/SDL_uikitmessagebox.m
     1.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Tue Oct 30 12:37:57 2012 -0700
     1.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Tue Oct 30 13:44:59 2012 -0700
     1.3 @@ -157,6 +157,8 @@
     1.4  		AA7558CA1595D55500BBD41B /* SDL.h in Headers */ = {isa = PBXBuildFile; fileRef = AA7558971595D55500BBD41B /* SDL.h */; };
     1.5  		AA9781C91576A7FA00472542 /* libSDL2.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FD6526630DE8FCCB002AD96B /* libSDL2.a */; };
     1.6  		AA9FF9511637C6E5000DF050 /* SDL_messagebox.h in Headers */ = {isa = PBXBuildFile; fileRef = AA9FF9501637C6E5000DF050 /* SDL_messagebox.h */; };
     1.7 +		AABCC3941640643D00AB8930 /* SDL_uikitmessagebox.h in Headers */ = {isa = PBXBuildFile; fileRef = AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */; };
     1.8 +		AABCC3951640643D00AB8930 /* SDL_uikitmessagebox.m in Sources */ = {isa = PBXBuildFile; fileRef = AABCC3931640643D00AB8930 /* SDL_uikitmessagebox.m */; };
     1.9  		FD3F4A760DEA620800C5B771 /* SDL_getenv.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A700DEA620800C5B771 /* SDL_getenv.c */; };
    1.10  		FD3F4A770DEA620800C5B771 /* SDL_iconv.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A710DEA620800C5B771 /* SDL_iconv.c */; };
    1.11  		FD3F4A780DEA620800C5B771 /* SDL_malloc.c in Sources */ = {isa = PBXBuildFile; fileRef = FD3F4A720DEA620800C5B771 /* SDL_malloc.c */; };
    1.12 @@ -412,6 +414,8 @@
    1.13  		AA7558961595D55500BBD41B /* SDL_video.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_video.h; sourceTree = "<group>"; };
    1.14  		AA7558971595D55500BBD41B /* SDL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL.h; sourceTree = "<group>"; };
    1.15  		AA9FF9501637C6E5000DF050 /* SDL_messagebox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_messagebox.h; sourceTree = "<group>"; };
    1.16 +		AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitmessagebox.h; sourceTree = "<group>"; };
    1.17 +		AABCC3931640643D00AB8930 /* SDL_uikitmessagebox.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitmessagebox.m; sourceTree = "<group>"; };
    1.18  		FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitview.h; sourceTree = "<group>"; };
    1.19  		FD3F4A700DEA620800C5B771 /* SDL_getenv.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_getenv.c; sourceTree = "<group>"; };
    1.20  		FD3F4A710DEA620800C5B771 /* SDL_iconv.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_iconv.c; sourceTree = "<group>"; };
    1.21 @@ -854,6 +858,8 @@
    1.22  				FD689FCC0E26E9D400F90B21 /* SDL_uikitappdelegate.m */,
    1.23  				FD689F0C0E26E5D900F90B21 /* SDL_uikitevents.h */,
    1.24  				FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */,
    1.25 +				AABCC3921640643D00AB8930 /* SDL_uikitmessagebox.h */,
    1.26 +				AABCC3931640643D00AB8930 /* SDL_uikitmessagebox.m */,
    1.27  				AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */,
    1.28  				AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */,
    1.29  				FD689F0E0E26E5D900F90B21 /* SDL_uikitopengles.h */,
    1.30 @@ -1252,6 +1258,7 @@
    1.31  				AA126AD41617C5E7005ABC8F /* SDL_uikitmodes.h in Headers */,
    1.32  				AA704DD6162AA90A0076D1C1 /* SDL_dropevents_c.h in Headers */,
    1.33  				AA9FF9511637C6E5000DF050 /* SDL_messagebox.h in Headers */,
    1.34 +				AABCC3941640643D00AB8930 /* SDL_uikitmessagebox.h in Headers */,
    1.35  			);
    1.36  			runOnlyForDeploymentPostprocessing = 0;
    1.37  		};
    1.38 @@ -1470,6 +1477,7 @@
    1.39  				AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */,
    1.40  				AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */,
    1.41  				AA704DD7162AA90A0076D1C1 /* SDL_dropevents.c in Sources */,
    1.42 +				AABCC3951640643D00AB8930 /* SDL_uikitmessagebox.m in Sources */,
    1.43  			);
    1.44  			runOnlyForDeploymentPostprocessing = 0;
    1.45  		};
     2.1 --- a/src/video/SDL_video.c	Tue Oct 30 12:37:57 2012 -0700
     2.2 +++ b/src/video/SDL_video.c	Tue Oct 30 13:44:59 2012 -0700
     2.3 @@ -2844,6 +2844,9 @@
     2.4  #if SDL_VIDEO_DRIVER_COCOA
     2.5  #include "cocoa/SDL_cocoamessagebox.h"
     2.6  #endif
     2.7 +#if SDL_VIDEO_DRIVER_UIKIT
     2.8 +#include "uikit/SDL_uikitmessagebox.h"
     2.9 +#endif
    2.10  #if SDL_VIDEO_DRIVER_X11
    2.11  #include "x11/SDL_x11messagebox.h"
    2.12  #endif
    2.13 @@ -2868,6 +2871,11 @@
    2.14          return 0;
    2.15      }
    2.16  #endif
    2.17 +#if SDL_VIDEO_DRIVER_UIKIT
    2.18 +    if (UIKit_ShowMessageBox(messageboxdata, buttonid) == 0) {
    2.19 +        return 0;
    2.20 +    }
    2.21 +#endif
    2.22  #if SDL_VIDEO_DRIVER_X11
    2.23      if (X11_ShowMessageBox(messageboxdata, buttonid) == 0) {
    2.24          return 0;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/uikit/SDL_uikitmessagebox.h	Tue Oct 30 13:44:59 2012 -0700
     3.3 @@ -0,0 +1,29 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +#include "SDL_config.h"
    3.25 +
    3.26 +#if SDL_VIDEO_DRIVER_UIKIT
    3.27 +
    3.28 +extern int UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    3.29 +
    3.30 +#endif /* SDL_VIDEO_DRIVER_UIKIT */
    3.31 +
    3.32 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/uikit/SDL_uikitmessagebox.m	Tue Oct 30 13:44:59 2012 -0700
     4.3 @@ -0,0 +1,101 @@
     4.4 +/*
     4.5 +  Simple DirectMedia Layer
     4.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4.7 +
     4.8 +  This software is provided 'as-is', without any express or implied
     4.9 +  warranty.  In no event will the authors be held liable for any damages
    4.10 +  arising from the use of this software.
    4.11 +
    4.12 +  Permission is granted to anyone to use this software for any purpose,
    4.13 +  including commercial applications, and to alter it and redistribute it
    4.14 +  freely, subject to the following restrictions:
    4.15 +
    4.16 +  1. The origin of this software must not be misrepresented; you must not
    4.17 +     claim that you wrote the original software. If you use this software
    4.18 +     in a product, an acknowledgment in the product documentation would be
    4.19 +     appreciated but is not required.
    4.20 +  2. Altered source versions must be plainly marked as such, and must not be
    4.21 +     misrepresented as being the original software.
    4.22 +  3. This notice may not be removed or altered from any source distribution.
    4.23 +*/
    4.24 +#include "SDL_config.h"
    4.25 +
    4.26 +#if SDL_VIDEO_DRIVER_UIKIT
    4.27 +
    4.28 +#include "SDL.h"
    4.29 +#include "SDL_uikitvideo.h"
    4.30 +
    4.31 +
    4.32 +/* Display a UIKit message box */
    4.33 +
    4.34 +
    4.35 +@interface UIKit_UIAlertViewDelegate : NSObject <UIAlertViewDelegate> {
    4.36 +@private
    4.37 +    int *clickedButtonIndex;
    4.38 +}
    4.39 +
    4.40 +- (id)initWithButtonIndex:(int *)_buttonIndex;
    4.41 +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
    4.42 +
    4.43 +@end
    4.44 +
    4.45 +@implementation UIKit_UIAlertViewDelegate
    4.46 +
    4.47 +- (id)initWithButtonIndex:(int *)buttonIndex
    4.48 +{
    4.49 +    self = [self init];
    4.50 +    if (self == nil) {
    4.51 +        return nil;
    4.52 +    }
    4.53 +    self->clickedButtonIndex = buttonIndex;
    4.54 +    
    4.55 +    return self;
    4.56 +}
    4.57 +
    4.58 +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
    4.59 +{
    4.60 +    *clickedButtonIndex = buttonIndex;
    4.61 +}
    4.62 +
    4.63 +@end // UIKit_UIAlertViewDelegate
    4.64 +
    4.65 +
    4.66 +int
    4.67 +UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    4.68 +{
    4.69 +    int clicked;
    4.70 +    
    4.71 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    4.72 +
    4.73 +    UIAlertView* alert = [[UIAlertView alloc] init];
    4.74 +
    4.75 +    alert.title = [[NSString alloc] initWithUTF8String:messageboxdata->title];
    4.76 +    alert.message = [[NSString alloc] initWithUTF8String:messageboxdata->message];
    4.77 +    alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
    4.78 +
    4.79 +    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    4.80 +    int i;
    4.81 +    for (i = 0; i < messageboxdata->numbuttons; ++i) {
    4.82 +        [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
    4.83 +    }
    4.84 +
    4.85 +    // Set up for showing the alert
    4.86 +    clicked = messageboxdata->numbuttons;
    4.87 +
    4.88 +    [alert show];
    4.89 +    
    4.90 +    // Run the main event loop until the alert has finished
    4.91 +    // Note that this needs to be done on the main thread
    4.92 +    while (clicked == messageboxdata->numbuttons) {
    4.93 +        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    4.94 +    }
    4.95 +    *buttonid = messageboxdata->buttons[clicked].buttonid;
    4.96 +
    4.97 +    [pool release];
    4.98 +
    4.99 +    return 0;
   4.100 +}
   4.101 +
   4.102 +#endif /* SDL_VIDEO_DRIVER_UIKIT */
   4.103 +
   4.104 +/* vi: set ts=4 sw=4 expandtab: */