Added Cocoa message box implementation
authorSam Lantinga <slouken@libsdl.org>
Tue, 30 Oct 2012 12:31:26 -0700
changeset 66173a92812e0c91
parent 6616 4f272256d172
child 6618 196b8f8dfc0a
Added Cocoa message box implementation
src/video/SDL_video.c
src/video/cocoa/SDL_cocoamessagebox.h
src/video/cocoa/SDL_cocoamessagebox.m
src/video/x11/SDL_x11messagebox.h
     1.1 --- a/src/video/SDL_video.c	Tue Oct 30 12:30:02 2012 -0700
     1.2 +++ b/src/video/SDL_video.c	Tue Oct 30 12:31:26 2012 -0700
     1.3 @@ -2841,8 +2841,11 @@
     1.4      return SDL_FALSE;
     1.5  }
     1.6  
     1.7 +#if SDL_VIDEO_DRIVER_COCOA
     1.8 +#include "cocoa/SDL_cocoamessagebox.h"
     1.9 +#endif
    1.10  #if SDL_VIDEO_DRIVER_X11
    1.11 -extern int X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    1.12 +#include "x11/SDL_x11messagebox.h"
    1.13  #endif
    1.14  
    1.15  int
    1.16 @@ -2860,6 +2863,11 @@
    1.17      }
    1.18  
    1.19      /* It's completely fine to call this function before video is initialized */
    1.20 +#if SDL_VIDEO_DRIVER_COCOA
    1.21 +    if (Cocoa_ShowMessageBox(messageboxdata, buttonid) == 0) {
    1.22 +        return 0;
    1.23 +    }
    1.24 +#endif
    1.25  #if SDL_VIDEO_DRIVER_X11
    1.26      if (X11_ShowMessageBox(messageboxdata, buttonid) == 0) {
    1.27          return 0;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/video/cocoa/SDL_cocoamessagebox.h	Tue Oct 30 12:31:26 2012 -0700
     2.3 @@ -0,0 +1,29 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +#include "SDL_config.h"
    2.25 +
    2.26 +#if SDL_VIDEO_DRIVER_COCOA
    2.27 +
    2.28 +extern int Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    2.29 +
    2.30 +#endif /* SDL_VIDEO_DRIVER_COCOA */
    2.31 +
    2.32 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/cocoa/SDL_cocoamessagebox.m	Tue Oct 30 12:31:26 2012 -0700
     3.3 @@ -0,0 +1,72 @@
     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_COCOA
    3.27 +
    3.28 +#include "SDL.h"
    3.29 +#include "SDL_cocoavideo.h"
    3.30 +
    3.31 +
    3.32 +/* Display a Cocoa message box */
    3.33 +int
    3.34 +Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    3.35 +{
    3.36 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    3.37 +
    3.38 +    NSAlert* alert = [[NSAlert alloc] init];
    3.39 +
    3.40 +    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    3.41 +        [alert setAlertStyle:NSCriticalAlertStyle];
    3.42 +    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    3.43 +        [alert setAlertStyle:NSWarningAlertStyle];
    3.44 +    } else {
    3.45 +        [alert setAlertStyle:NSInformationalAlertStyle];
    3.46 +    }
    3.47 +
    3.48 +    [alert setMessageText:[[NSString alloc] initWithUTF8String:messageboxdata->title]];
    3.49 +    [alert setInformativeText:[[NSString alloc] initWithUTF8String:messageboxdata->message]];
    3.50 +
    3.51 +    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    3.52 +    int i;
    3.53 +    for (i = 0; i < messageboxdata->numbuttons; ++i) {
    3.54 +        NSButton *button = [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
    3.55 +        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    3.56 +            [button setKeyEquivalent:@"\r"];
    3.57 +        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    3.58 +            [button setKeyEquivalent:@"\033"];
    3.59 +        } else {
    3.60 +            [button setKeyEquivalent:@""];
    3.61 +        }
    3.62 +    }
    3.63 +
    3.64 +    NSInteger clicked = [alert runModal];
    3.65 +    clicked -= NSAlertFirstButtonReturn;
    3.66 +    *buttonid = buttons[clicked].buttonid;
    3.67 +
    3.68 +    [pool release];
    3.69 +
    3.70 +    return 0;
    3.71 +}
    3.72 +
    3.73 +#endif /* SDL_VIDEO_DRIVER_COCOA */
    3.74 +
    3.75 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/x11/SDL_x11messagebox.h	Tue Oct 30 12:31:26 2012 -0700
     4.3 @@ -0,0 +1,28 @@
     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 +
    4.25 +#if SDL_VIDEO_DRIVER_X11
    4.26 +
    4.27 +extern int X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    4.28 +
    4.29 +#endif /* SDL_VIDEO_DRIVER_X11 */
    4.30 +
    4.31 +/* vi: set ts=4 sw=4 expandtab: */