Skip to content

Commit

Permalink
Use sheets for native file dialogs if the dialog has its parent set.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59346 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
  • Loading branch information
kollivier committed Mar 5, 2009
1 parent cecbc29 commit 724999e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
18 changes: 18 additions & 0 deletions include/wx/osx/cocoa/private.h
Expand Up @@ -277,6 +277,24 @@ protected :

void WXDLLIMPEXP_CORE wxOSXCocoaClassAddWXMethods(Class c);

/*
We need this for ShowModal, as the sheet just disables the parent window and
returns control to the app, whereas we don't want to return from ShowModal
until the sheet has been dismissed.
*/
@interface ModalDialogDelegate : NSObject
{
BOOL sheetFinished;
int resultCode;
}

- (BOOL)finished;
- (int)code;
- (void)waitForSheetToFinish;
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
@end


#endif // __OBJC__

// NSCursor
Expand Down
50 changes: 46 additions & 4 deletions src/osx/cocoa/filedlg.mm
Expand Up @@ -51,7 +51,6 @@
{
}


NSArray* GetTypesFromFilter( const wxString filter )
{
NSMutableArray* types = nil;
Expand Down Expand Up @@ -149,6 +148,14 @@

m_path = wxEmptyString;
m_fileNames.Clear();

wxNonOwnedWindow* parentWindow = NULL;
int returnCode = -1;

if (GetParent())
{
parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
}

if (HasFlag(wxFD_SAVE))
{
Expand All @@ -164,8 +171,25 @@
if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
{
}

if ( [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ] == NSOKButton )

if (parentWindow)
{
NSWindow* nativeParent = parentWindow->GetWXWindow();
ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
[sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
modalForWindow: nativeParent modalDelegate: sheetDelegate
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo: nil];
[sheetDelegate waitForSheetToFinish];
result = [sheetDelegate code];
[sheetDelegate release];
}
else
{
result = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
}

if (result == NSOKButton )
{
panel = sPanel;
result = wxID_OK;
Expand All @@ -187,7 +211,25 @@
[oPanel setCanChooseFiles:YES];
[oPanel setMessage:cf.AsNSString()];

if ( [oPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() types:types] == NSOKButton )
if (parentWindow)
{
NSWindow* nativeParent = parentWindow->GetWXWindow();
ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
[oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
types: types modalForWindow: nativeParent
modalDelegate: sheetDelegate
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo: nil];
[sheetDelegate waitForSheetToFinish];
result = [sheetDelegate code];
[sheetDelegate release];
}
else
{
result = [oPanel runModalForDirectory:dir.AsNSString()
file:file.AsNSString() types:types];
}
if (result == NSOKButton )
{
panel = oPanel;
result = wxID_OK;
Expand Down
39 changes: 39 additions & 0 deletions src/osx/cocoa/utils.mm
Expand Up @@ -138,6 +138,45 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
}
@end

/*
allows ShowModal to work when using sheets.
see include/wx/osx/cocoa/private.h for more info
*/
@implementation ModalDialogDelegate
- (id)init
{
[super init];
sheetFinished = NO;
resultCode = -1;
return self;
}

- (BOOL)finished
{
return sheetFinished;
}

- (int)code
{
return resultCode;
}

- (void)waitForSheetToFinish
{
while (!sheetFinished)
{
wxSafeYield();
}
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
resultCode = returnCode;
sheetFinished = YES;
[sheet orderOut: self];
}
@end

bool wxApp::DoInitGui()
{
[NSApplication sharedApplication];
Expand Down

0 comments on commit 724999e

Please sign in to comment.