src/video/cocoa/SDL_cocoamessagebox.m
changeset 7088 664d8532219b
parent 6885 700f1b25f77f
child 7140 e1896b95a8a7
equal deleted inserted replaced
7087:5639ac726076 7088:664d8532219b
    30 #endif
    30 #endif
    31 
    31 
    32 #include "SDL_messagebox.h"
    32 #include "SDL_messagebox.h"
    33 #include "SDL_cocoavideo.h"
    33 #include "SDL_cocoavideo.h"
    34 
    34 
       
    35 @interface SDLMessageBoxPresenter : NSObject {
       
    36 @public
       
    37     NSInteger clicked;
       
    38 }
       
    39 @end
       
    40 
       
    41 @implementation SDLMessageBoxPresenter
       
    42 - (id)init
       
    43 {
       
    44     self = [super init];
       
    45     if (self) {
       
    46         clicked = -1;
       
    47     }
       
    48 
       
    49     return self;
       
    50 }
       
    51 
       
    52 - (void)showAlert:(NSAlert*)alert
       
    53 {
       
    54     clicked = [alert runModal];
       
    55 }
       
    56 @end
       
    57 
    35 
    58 
    36 /* Display a Cocoa message box */
    59 /* Display a Cocoa message box */
    37 int
    60 int
    38 Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    61 Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    39 {
    62 {
    40     Cocoa_RegisterApp();
    63     Cocoa_RegisterApp();
    41 
    64 
    42     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    65     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    43 
    66 
    44     NSAlert* alert = [[NSAlert alloc] init];
    67     NSAlert* alert = [[[NSAlert alloc] init] autorelease];
    45 
    68 
    46     if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    69     if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    47         [alert setAlertStyle:NSCriticalAlertStyle];
    70         [alert setAlertStyle:NSCriticalAlertStyle];
    48     } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    71     } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    49         [alert setAlertStyle:NSWarningAlertStyle];
    72         [alert setAlertStyle:NSWarningAlertStyle];
    65         } else {
    88         } else {
    66             [button setKeyEquivalent:@""];
    89             [button setKeyEquivalent:@""];
    67         }
    90         }
    68     }
    91     }
    69 
    92 
    70     NSInteger clicked = [alert runModal];
    93     SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] init] autorelease];
    71     clicked -= NSAlertFirstButtonReturn;
    94 
    72     *buttonid = buttons[clicked].buttonid;
    95     [presenter performSelectorOnMainThread:@selector(showAlert:)
    73     [alert release];
    96                                 withObject:alert
       
    97                              waitUntilDone:YES];
       
    98 
       
    99     int returnValue = 0;
       
   100     NSInteger clicked = presenter->clicked;
       
   101     if (clicked >= NSAlertFirstButtonReturn)
       
   102     {
       
   103         clicked -= NSAlertFirstButtonReturn;
       
   104         *buttonid = buttons[clicked].buttonid;
       
   105     }
       
   106     else
       
   107     {
       
   108         returnValue = SDL_SetError("Did not get a valid `clicked button' id: %d", clicked);
       
   109     }
    74 
   110 
    75     [pool release];
   111     [pool release];
    76 
   112 
    77     return 0;
   113     return returnValue;
    78 }
   114 }
    79 
   115 
    80 #endif /* SDL_VIDEO_DRIVER_COCOA */
   116 #endif /* SDL_VIDEO_DRIVER_COCOA */
    81 
   117 
    82 /* vi: set ts=4 sw=4 expandtab: */
   118 /* vi: set ts=4 sw=4 expandtab: */