src/video/cocoa/SDL_cocoamessagebox.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Feb 2013 00:54:08 -0800
changeset 6836 b0ca1571caf3
parent 6832 156e608ec4ef
child 6848 478ecc8a58b3
permissions -rw-r--r--
Fixed bug 1689 - Leaks galore in OS X Cocoa code. Edward Rudd This is to document the various leaks I'm finding in the Cocoa code in SDL 2.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
  arising from the use of this software.
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
     appreciated but is not required.
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#if SDL_VIDEO_DRIVER_COCOA
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
6624
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    25
#if defined(__APPLE__) && defined(__POWERPC__)
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    26
#include <altivec.h>
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    27
#undef bool
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    28
#undef vector
6626
4aa5af31f2f9 Works better when you put it in the right file. :)
Ryan C. Gordon <icculus@icculus.org>
parents: 6624
diff changeset
    29
#undef pixel
6624
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    30
#endif
ad4892cb5be9 Fix Mac OS X PowerPC builds, I think.
Ryan C. Gordon <icculus@icculus.org>
parents: 6623
diff changeset
    31
6623
f4ae87f3ec15 Maybe fix PPC build error?
Sam Lantinga <slouken@libsdl.org>
parents: 6617
diff changeset
    32
#include "SDL_messagebox.h"
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
#include "SDL_cocoavideo.h"
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
/* Display a Cocoa message box */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
int
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
{
6639
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    40
    Cocoa_RegisterApp();
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6626
diff changeset
    41
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    42
    @autoreleasepool {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    43
        NSAlert* alert = [[NSAlert alloc] init];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    45
        if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    46
            [alert setAlertStyle:NSCriticalAlertStyle];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    47
        } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    48
            [alert setAlertStyle:NSWarningAlertStyle];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    49
        } else {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    50
            [alert setAlertStyle:NSInformationalAlertStyle];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    51
        }
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    53
        [alert setMessageText:[NSString stringWithUTF8String:messageboxdata->title]];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    54
        [alert setInformativeText:[NSString stringWithUTF8String:messageboxdata->message]];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    56
        const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    57
        int i;
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    58
        for (i = 0; i < messageboxdata->numbuttons; ++i) {
6836
b0ca1571caf3 Fixed bug 1689 - Leaks galore in OS X Cocoa code.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    59
            NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    60
            if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    61
                [button setKeyEquivalent:@"\r"];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    62
            } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    63
                [button setKeyEquivalent:@"\033"];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    64
            } else {
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    65
                [button setKeyEquivalent:@""];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    66
            }
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
        }
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    68
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    69
        NSInteger clicked = [alert runModal];
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    70
        clicked -= NSAlertFirstButtonReturn;
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    71
        *buttonid = buttons[clicked].buttonid;
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6714
diff changeset
    72
        [alert release];
6617
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
    }
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
    return 0;
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
}
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
#endif /* SDL_VIDEO_DRIVER_COCOA */
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
3a92812e0c91 Added Cocoa message box implementation
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
/* vi: set ts=4 sw=4 expandtab: */