Use non-deprecated NSOpenPanel methods in wxDirDialog on OSX

This commit is contained in:
Ian McInerney
2020-01-17 00:14:53 +00:00
committed by Vadim Zeitlin
parent 79d73d4eb3
commit b98660b996

View File

@@ -73,15 +73,17 @@ WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
if ( !HasFlag(wxDD_DIR_MUST_EXIST) ) if ( !HasFlag(wxDD_DIR_MUST_EXIST) )
[oPanel setCanCreateDirectories:YES]; [oPanel setCanCreateDirectories:YES];
// Set the directory to use
if ( !m_path.IsEmpty() )
{
wxCFStringRef dir(m_path);
NSURL* dirUrl = [NSURL fileURLWithPath: dir.AsNSString() isDirectory: YES];
[oPanel setDirectoryURL: dirUrl];
}
return oPanel; return oPanel;
} }
// We use several deprecated methods of NSOpenPanel in the code below, we
// should replace them with newer equivalents now that we don't support OS X
// versions which didn't have them (pre 10.6), but until then, get rid of
// the warning.
wxGCC_WARNING_SUPPRESS(deprecated-declarations)
void wxDirDialog::ShowWindowModal() void wxDirDialog::ShowWindowModal()
{ {
wxNonOwnedWindow* parentWindow = NULL; wxNonOwnedWindow* parentWindow = NULL;
@@ -96,11 +98,11 @@ void wxDirDialog::ShowWindowModal()
NSOpenPanel *oPanel = OSXCreatePanel(); NSOpenPanel *oPanel = OSXCreatePanel();
NSWindow* nativeParent = parentWindow->GetWXWindow(); NSWindow* nativeParent = parentWindow->GetWXWindow();
wxCFStringRef dir( m_path );
[oPanel beginSheetForDirectory:dir.AsNSString() file:nil types: nil // Create the window and have it call the ModalFinishedCallback on completion
modalForWindow: nativeParent modalDelegate: m_sheetDelegate [oPanel beginSheetModalForWindow: nativeParent completionHandler: ^(NSModalResponse returnCode){
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) [(ModalDialogDelegate*)m_sheetDelegate sheetDidEnd: oPanel returnCode: returnCode contextInfo: nil];
contextInfo: nil]; }];
} }
int wxDirDialog::ShowModal() int wxDirDialog::ShowModal()
@@ -111,15 +113,12 @@ int wxDirDialog::ShowModal()
NSOpenPanel *oPanel = OSXCreatePanel(); NSOpenPanel *oPanel = OSXCreatePanel();
wxCFStringRef dir( m_path );
m_path.clear();
int returnCode = -1; int returnCode = -1;
OSXBeginModalDialog(); OSXBeginModalDialog();
returnCode = (NSInteger)[oPanel runModalForDirectory:dir.AsNSString() file:nil types:nil]; // Display the panel and process the result on completion
returnCode = (NSInteger)[oPanel runModal];
ModalFinishedCallback(oPanel, returnCode); ModalFinishedCallback(oPanel, returnCode);
OSXEndModalDialog(); OSXEndModalDialog();
@@ -135,7 +134,15 @@ void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
if (returnCode == NSOKButton ) if (returnCode == NSOKButton )
{ {
NSOpenPanel* oPanel = (NSOpenPanel*)panel; NSOpenPanel* oPanel = (NSOpenPanel*)panel;
SetPath( wxCFStringRef::AsStringWithNormalizationFormC([[oPanel filenames] objectAtIndex:0]));
NSArray<NSURL*>* selectedURL = [oPanel URLs];
for ( NSURL* url in selectedURL )
{
SetPath([url fileSystemRepresentation]);
break;
}
result = wxID_OK; result = wxID_OK;
} }
SetReturnCode(result); SetReturnCode(result);
@@ -144,6 +151,5 @@ void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
} }
wxGCC_WARNING_RESTORE(deprecated-declarations)
#endif // wxUSE_DIRDLG #endif // wxUSE_DIRDLG