Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1970 - Cocoa message boxes ignore parent window requests
Browse files Browse the repository at this point in the history
Ryan C. Gordon

Cocoa_ShowMessageBox() ignores the "window" field of SDL_MessageBoxData, which means you can't assign a parent window to a message box. This is particularly egregious on Mac OS X, because it'll actually make the NSAlert visually part of the parent window instead of just concerning itself with window focus.
  • Loading branch information
slouken committed Jul 14, 2013
1 parent 6ed6a36 commit fa8296d
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/video/cocoa/SDL_cocoamessagebox.m
Expand Up @@ -29,30 +29,57 @@
#undef pixel
#endif

#include "SDL_events.h"
#include "SDL_timer.h"
#include "SDL_messagebox.h"
#include "SDL_cocoavideo.h"

@interface SDLMessageBoxPresenter : NSObject {
@public
NSInteger clicked;
NSWindow *nswindow;
}
- (id) initWithParentWindow:(SDL_Window *)window;
- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
@end

@implementation SDLMessageBoxPresenter
- (id)init
- (id) initWithParentWindow:(SDL_Window *)window
{
self = [super init];
if (self) {
clicked = -1;

/* Retain the NSWindow because we'll show the alert later on the main thread */
if (window) {
nswindow = [((SDL_WindowData *) window->driverdata)->nswindow retain];
} else {
nswindow = NULL;
}
}

return self;
}

- (void)showAlert:(NSAlert*)alert
{
clicked = [alert runModal];
if (nswindow) {
[alert beginSheetModalForWindow:nswindow modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
while (clicked < 0) {
SDL_PumpEvents();
SDL_Delay(100);
}
[nswindow release];
} else {
clicked = [alert runModal];
}
}

- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
clicked = returnCode;
}

@end


Expand Down Expand Up @@ -90,7 +117,7 @@ - (void)showAlert:(NSAlert*)alert
}
}

SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] init] autorelease];
SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease];

[presenter performSelectorOnMainThread:@selector(showAlert:)
withObject:alert
Expand Down

0 comments on commit fa8296d

Please sign in to comment.