Replace deprecated API usage for window modal dialogs

Remove the delegate objects that are not used any longer.

Move wxGetAvailableDrives() to Objective-C code file dirdlg.mm to allow
for Cocoa implementation.
This commit is contained in:
Stefan Csomor
2020-07-09 12:01:00 +02:00
committed by Vadim Zeitlin
parent be9216b6b1
commit 220dfe17ea
8 changed files with 104 additions and 112 deletions

View File

@@ -61,8 +61,6 @@ private:
// Create and initialize NSOpenPanel that we use in both ShowModal() and // Create and initialize NSOpenPanel that we use in both ShowModal() and
// ShowWindowModal(). // ShowWindowModal().
WX_NSOpenPanel OSXCreatePanel() const; WX_NSOpenPanel OSXCreatePanel() const;
WX_NSObject m_sheetDelegate;
#endif #endif
// Common part of all ctors. // Common part of all ctors.

View File

@@ -94,8 +94,6 @@ protected:
bool m_useFileTypeFilter; bool m_useFileTypeFilter;
int m_firstFileTypeFilter; int m_firstFileTypeFilter;
wxArrayString m_currentExtensions; wxArrayString m_currentExtensions;
WX_NSObject m_delegate;
WX_NSObject m_sheetDelegate;
#endif #endif
private: private:

View File

@@ -45,9 +45,6 @@ protected:
int m_buttonId[4]; int m_buttonId[4];
int m_buttonCount; int m_buttonCount;
#if wxOSX_USE_COCOA
WX_NSObject m_sheetDelegate;
#endif
wxDECLARE_DYNAMIC_CLASS(wxMessageDialog); wxDECLARE_DYNAMIC_CLASS(wxMessageDialog);
}; };

View File

@@ -98,6 +98,10 @@ wxDEFINE_EVENT( wxEVT_DIRCTRL_FILEACTIVATED, wxTreeEvent );
// wxGetAvailableDrives, for WINDOWS, OSX, UNIX (returns "/") // wxGetAvailableDrives, for WINDOWS, OSX, UNIX (returns "/")
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// since the macOS implementation needs objective-C this is dirdlg.mm
#ifdef __WXOSX__
extern size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids);
#else
size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids) size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
{ {
#ifdef wxHAS_FILESYSTEM_VOLUMES #ifdef wxHAS_FILESYSTEM_VOLUMES
@@ -156,38 +160,6 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI
} }
#endif // __WIN32__/!__WIN32__ #endif // __WIN32__/!__WIN32__
#elif defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON
ItemCount volumeIndex = 1;
OSErr err = noErr ;
while( noErr == err )
{
HFSUniStr255 volumeName ;
FSRef fsRef ;
FSVolumeInfo volumeInfo ;
err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
if( noErr == err )
{
wxString path = wxMacFSRefToPath( &fsRef ) ;
wxString name = wxMacHFSUniStrToString( &volumeName ) ;
if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
{
icon_ids.Add(wxFileIconsTable::cdrom);
}
else
{
icon_ids.Add(wxFileIconsTable::drive);
}
// todo other removable
paths.Add(path);
names.Add(name);
volumeIndex++ ;
}
}
#elif defined(__UNIX__) #elif defined(__UNIX__)
paths.Add(wxT("/")); paths.Add(wxT("/"));
names.Add(wxT("/")); names.Add(wxT("/"));
@@ -199,6 +171,7 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI
wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives.")); wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
return paths.GetCount(); return paths.GetCount();
} }
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxIsDriveAvailable // wxIsDriveAvailable

View File

@@ -32,11 +32,11 @@ void wxDialog::DoShowWindowModal()
NSWindow* parentWindow = parent->GetWXWindow(); NSWindow* parentWindow = parent->GetWXWindow();
NSWindow* theWindow = GetWXWindow(); NSWindow* theWindow = GetWXWindow();
[NSApp beginSheet: theWindow [parentWindow beginSheet:theWindow completionHandler:
modalForWindow: parentWindow ^(NSModalResponse returnCode)
modalDelegate: theWindow {
didEndSelector: nil this->ModalFinishedCallback(theWindow, returnCode);
contextInfo: nil]; }];
} }
void wxDialog::EndWindowModal() void wxDialog::EndWindowModal()

View File

@@ -32,6 +32,7 @@
#include "wx/filename.h" #include "wx/filename.h"
#include "wx/evtloop.h" #include "wx/evtloop.h"
#include "wx/modalhook.h" #include "wx/modalhook.h"
#include "wx/generic/dirctrlg.h" // for wxFileIconsTable
#include "wx/osx/private.h" #include "wx/osx/private.h"
@@ -39,7 +40,6 @@ wxIMPLEMENT_CLASS(wxDirDialog, wxDialog);
void wxDirDialog::Init() void wxDirDialog::Init()
{ {
m_sheetDelegate = nil;
} }
void wxDirDialog::Create(wxWindow *parent, const wxString& message, void wxDirDialog::Create(wxWindow *parent, const wxString& message,
@@ -54,13 +54,10 @@ void wxDirDialog::Create(wxWindow *parent, const wxString& message,
SetMessage( message ); SetMessage( message );
SetWindowStyle(style); SetWindowStyle(style);
SetPath(defaultPath); SetPath(defaultPath);
m_sheetDelegate = [[ModalDialogDelegate alloc] init];
[(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
} }
wxDirDialog::~wxDirDialog() wxDirDialog::~wxDirDialog()
{ {
[m_sheetDelegate release];
} }
WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const WX_NSOpenPanel wxDirDialog::OSXCreatePanel() const
@@ -116,7 +113,7 @@ void wxDirDialog::ShowWindowModal()
// Create the window and have it call the ModalFinishedCallback on completion // Create the window and have it call the ModalFinishedCallback on completion
[oPanel beginSheetModalForWindow: nativeParent completionHandler: ^(NSModalResponse returnCode){ [oPanel beginSheetModalForWindow: nativeParent completionHandler: ^(NSModalResponse returnCode){
[(ModalDialogDelegate*)m_sheetDelegate sheetDidEnd: oPanel returnCode: returnCode contextInfo: nil]; this->ModalFinishedCallback(oPanel, returnCode);
}]; }];
} }
@@ -128,12 +125,10 @@ int wxDirDialog::ShowModal()
NSOpenPanel *oPanel = OSXCreatePanel(); NSOpenPanel *oPanel = OSXCreatePanel();
int returnCode = -1;
OSXBeginModalDialog(); OSXBeginModalDialog();
// Display the panel and process the result on completion // Display the panel and process the result on completion
returnCode = (NSInteger)[oPanel runModal]; int returnCode = (NSInteger)[oPanel runModal];
ModalFinishedCallback(oPanel, returnCode); ModalFinishedCallback(oPanel, returnCode);
OSXEndModalDialog(); OSXEndModalDialog();
@@ -146,7 +141,7 @@ void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
{ {
int result = wxID_CANCEL; int result = wxID_CANCEL;
if (returnCode == NSOKButton ) if (returnCode == NSModalResponseOK )
{ {
NSOpenPanel* oPanel = (NSOpenPanel*)panel; NSOpenPanel* oPanel = (NSOpenPanel*)panel;
@@ -154,7 +149,9 @@ void wxDirDialog::ModalFinishedCallback(void* panel, int returnCode)
for ( NSURL* url in selectedURL ) for ( NSURL* url in selectedURL )
{ {
m_paths.Add([url fileSystemRepresentation]); NSString* unsafePath = [NSString stringWithUTF8String:[url fileSystemRepresentation]];
wxCFStringRef safePath([[unsafePath precomposedStringWithCanonicalMapping] retain]);
m_paths.Add(safePath.AsString());
} }
if ( !HasFlag(wxDD_MULTIPLE) ) if ( !HasFlag(wxDD_MULTIPLE) )
@@ -176,4 +173,35 @@ void wxDirDialog::SetTitle(const wxString &title)
wxDialog::SetTitle(title); wxDialog::SetTitle(title);
} }
size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
{
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSArray *volumes = [workspace mountedLocalVolumePaths];
NSFileManager *filemanager = [NSFileManager defaultManager];
for (NSString *path in volumes)
{
NSString *description, *type, *name;
BOOL removable, writable, unmountable;
if ( [workspace getFileSystemInfoForPath:path isRemovable:&removable isWritable:&writable
isUnmountable:&unmountable description:&description type:&type] )
{
if ( writable )
icon_ids.Add(wxFileIconsTable::drive);
else
icon_ids.Add(wxFileIconsTable::cdrom);
name = [filemanager displayNameAtPath:path];
paths.Add(wxCFStringRef(path).AsString());
names.Add(wxCFStringRef(name).AsString());
}
}
wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
return paths.GetCount();
}
#endif // wxUSE_DIRDLG #endif // wxUSE_DIRDLG

View File

@@ -50,10 +50,10 @@ wxIMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase);
void wxFileDialog::Init() void wxFileDialog::Init()
{ {
m_filterIndex = -1; m_filterIndex = -1;
m_delegate = nil;
m_sheetDelegate = nil;
m_filterPanel = NULL; m_filterPanel = NULL;
m_filterChoice = NULL; m_filterChoice = NULL;
m_useFileTypeFilter = false;
m_firstFileTypeFilter = 0;
} }
void wxFileDialog::Create( void wxFileDialog::Create(
@@ -63,13 +63,10 @@ void wxFileDialog::Create(
{ {
wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name); wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
m_sheetDelegate = [[ModalDialogDelegate alloc] init];
[(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
} }
wxFileDialog::~wxFileDialog() wxFileDialog::~wxFileDialog()
{ {
[m_sheetDelegate release];
} }
bool wxFileDialog::SupportsExtraControl() const bool wxFileDialog::SupportsExtraControl() const
@@ -196,8 +193,6 @@ void wxFileDialog::ShowWindowModal()
wxCHECK_RET(parentWindow, "Window modal display requires parent."); wxCHECK_RET(parentWindow, "Window modal display requires parent.");
wxGCC_WARNING_SUPPRESS(deprecated-declarations)
NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
if ( HasFlag(wxFD_SAVE) ) if ( HasFlag(wxFD_SAVE) )
{ {
@@ -214,13 +209,20 @@ void wxFileDialog::ShowWindowModal()
[sPanel setCanSelectHiddenExtension:YES]; [sPanel setCanSelectHiddenExtension:YES];
[sPanel setAllowedFileTypes:types]; [sPanel setAllowedFileTypes:types];
[sPanel setAllowsOtherFileTypes:NO]; [sPanel setAllowsOtherFileTypes:NO];
[sPanel setShowsHiddenFiles: HasFlag(wxFD_SHOW_HIDDEN) ? YES : NO];
NSWindow* nativeParent = parentWindow->GetWXWindow(); NSWindow* nativeParent = parentWindow->GetWXWindow();
[sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() if ( !m_dir.IsEmpty() )
modalForWindow: nativeParent modalDelegate: m_sheetDelegate [sPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) isDirectory:YES]];
contextInfo: nil]; if ( !m_fileName.IsEmpty() )
[sPanel setShowsHiddenFiles: HasFlag(wxFD_SHOW_HIDDEN) ? YES : NO]; [sPanel setNameFieldStringValue: file.AsNSString()];
[sPanel beginSheetModalForWindow:nativeParent completionHandler:
^(NSModalResponse returnCode)
{
this->ModalFinishedCallback(sPanel, returnCode);
}];
} }
else else
{ {
@@ -234,17 +236,19 @@ void wxFileDialog::ShowWindowModal()
[oPanel setCanChooseFiles:YES]; [oPanel setCanChooseFiles:YES];
[oPanel setMessage:cf.AsNSString()]; [oPanel setMessage:cf.AsNSString()];
[oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
[oPanel setShowsHiddenFiles: HasFlag(wxFD_SHOW_HIDDEN) ? YES : NO];
NSWindow* nativeParent = parentWindow->GetWXWindow(); NSWindow* nativeParent = parentWindow->GetWXWindow();
[oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() if ( !m_dir.IsEmpty() )
types: types modalForWindow: nativeParent [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
modalDelegate: m_sheetDelegate isDirectory:YES]];
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) [oPanel beginSheetModalForWindow:nativeParent completionHandler:
contextInfo: nil]; ^(NSModalResponse returnCode)
[oPanel setShowsHiddenFiles: HasFlag(wxFD_SHOW_HIDDEN) ? YES : NO]; {
this->ModalFinishedCallback(oPanel, returnCode);
}];
} }
wxGCC_WARNING_RESTORE(deprecated-declarations)
} }
// Create a panel with the file type drop down list // Create a panel with the file type drop down list
@@ -293,10 +297,7 @@ void wxFileDialog::DoOnFilterSelected(int index)
{ {
NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions); NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
NSSavePanel* panel = (NSSavePanel*) GetWXWindow(); NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
if ( m_delegate ) [panel setAllowedFileTypes:types];
[panel validateVisibleColumns];
else
[panel setAllowedFileTypes:types];
} }
// An item has been selected in the file filter wxChoice: // An item has been selected in the file filter wxChoice:
@@ -424,8 +425,6 @@ int wxFileDialog::ShowModal()
} }
} }
wxGCC_WARNING_SUPPRESS(deprecated-declarations)
OSXBeginModalDialog(); OSXBeginModalDialog();
if ( HasFlag(wxFD_SAVE) ) if ( HasFlag(wxFD_SAVE) )
@@ -462,7 +461,14 @@ int wxFileDialog::ShowModal()
DoOnFilterSelected(m_firstFileTypeFilter); DoOnFilterSelected(m_firstFileTypeFilter);
} }
returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ]; if ( !m_dir.IsEmpty() )
[sPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
isDirectory:YES]];
if ( !m_fileName.IsEmpty() )
[sPanel setNameFieldStringValue: file.AsNSString()];
returnCode = [sPanel runModal];
ModalFinishedCallback(sPanel, returnCode); ModalFinishedCallback(sPanel, returnCode);
} }
else else
@@ -489,7 +495,7 @@ int wxFileDialog::ShowModal()
} }
else else
{ {
[oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)]; [oPanel setAllowedFileTypes: types];
} }
if ( !m_dir.IsEmpty() ) if ( !m_dir.IsEmpty() )
[oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
@@ -502,24 +508,21 @@ int wxFileDialog::ShowModal()
OSXEndModalDialog(); OSXEndModalDialog();
wxGCC_WARNING_RESTORE(deprecated-declarations)
return GetReturnCode(); return GetReturnCode();
} }
void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode) void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
{ {
wxGCC_WARNING_SUPPRESS(deprecated-declarations)
int result = wxID_CANCEL; int result = wxID_CANCEL;
if (HasFlag(wxFD_SAVE)) if (HasFlag(wxFD_SAVE))
{ {
if (returnCode == NSOKButton ) if (returnCode == NSModalResponseOK )
{ {
NSSavePanel* sPanel = (NSSavePanel*)panel; NSSavePanel* sPanel = (NSSavePanel*)panel;
result = wxID_OK; result = wxID_OK;
m_path = wxCFStringRef::AsStringWithNormalizationFormC([sPanel filename]); NSString* unsafePath = [NSString stringWithUTF8String:[[sPanel URL] fileSystemRepresentation]];
m_path = wxCFStringRef([[unsafePath precomposedStringWithCanonicalMapping] retain]).AsString();
m_fileName = wxFileNameFromPath(m_path); m_fileName = wxFileNameFromPath(m_path);
m_dir = wxPathOnly( m_path ); m_dir = wxPathOnly( m_path );
if (m_filterChoice) if (m_filterChoice)
@@ -531,7 +534,7 @@ void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
else else
{ {
NSOpenPanel* oPanel = (NSOpenPanel*)panel; NSOpenPanel* oPanel = (NSOpenPanel*)panel;
if (returnCode == NSOKButton ) if (returnCode == NSModalResponseOK )
{ {
panel = oPanel; panel = oPanel;
result = wxID_OK; result = wxID_OK;
@@ -541,38 +544,34 @@ void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
m_filterIndex = m_filterChoice->GetSelection(); m_filterIndex = m_filterChoice->GetSelection();
} }
NSArray* filenames = [oPanel filenames]; bool isFirst = true;
for ( size_t i = 0 ; i < [filenames count] ; ++ i ) for (NSURL* filename in [oPanel URLs])
{ {
wxString fnstr = wxCFStringRef::AsStringWithNormalizationFormC([filenames objectAtIndex:i]); NSString* unsafePath = [NSString stringWithUTF8String:[filename fileSystemRepresentation]];
wxString fnstr = wxCFStringRef([[unsafePath precomposedStringWithCanonicalMapping] retain]).AsString();
m_paths.Add( fnstr ); m_paths.Add( fnstr );
m_fileNames.Add( wxFileNameFromPath(fnstr) ); m_fileNames.Add( wxFileNameFromPath(fnstr) );
if ( i == 0 ) if ( isFirst )
{ {
m_path = fnstr; m_path = fnstr;
m_fileName = wxFileNameFromPath(fnstr); m_fileName = wxFileNameFromPath(fnstr);
m_dir = wxPathOnly( fnstr ); m_dir = wxPathOnly( fnstr );
isFirst = false;
} }
} }
} }
if ( m_delegate )
{
[oPanel setDelegate:nil];
[m_delegate release];
m_delegate = nil;
}
} }
SetReturnCode(result); SetReturnCode(result);
// workaround for sandboxed app, see above, must be executed before window modal handler
// because there this instance will be deleted
if ( m_isNativeWindowWrapper )
UnsubclassWin();
if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL) if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
// workaround for sandboxed app, see above
if ( m_isNativeWindowWrapper )
UnsubclassWin();
[(NSSavePanel*) panel setAccessoryView:nil]; [(NSSavePanel*) panel setAccessoryView:nil];
wxGCC_WARNING_RESTORE(deprecated-declarations)
} }
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG

View File

@@ -54,13 +54,10 @@ wxMessageDialog::wxMessageDialog(wxWindow *parent,
const wxPoint& WXUNUSED(pos)) const wxPoint& WXUNUSED(pos))
: wxMessageDialogBase(parent, message, caption, style) : wxMessageDialogBase(parent, message, caption, style)
{ {
m_sheetDelegate = [[ModalDialogDelegate alloc] init];
[(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
} }
wxMessageDialog::~wxMessageDialog() wxMessageDialog::~wxMessageDialog()
{ {
[m_sheetDelegate release];
} }
int wxMessageDialog::ShowModal() int wxMessageDialog::ShowModal()
@@ -196,9 +193,11 @@ void wxMessageDialog::ShowWindowModal()
NSAlert* alert = (NSAlert*)ConstructNSAlert(); NSAlert* alert = (NSAlert*)ConstructNSAlert();
NSWindow* nativeParent = parentWindow->GetWXWindow(); NSWindow* nativeParent = parentWindow->GetWXWindow();
[alert beginSheetModalForWindow: nativeParent modalDelegate: m_sheetDelegate [alert beginSheetModalForWindow:nativeParent completionHandler:
didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) ^(NSModalResponse returnCode)
contextInfo: nil]; {
this->ModalFinishedCallback(alert, returnCode);
}];
} }
} }