1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/filedlg.h"
27 #include "wx/msgdlg.h"
30 #include "wx/stattext.h"
31 #include "wx/choice.h"
34 #include "wx/filename.h"
35 #include "wx/tokenzr.h"
36 #include "wx/evtloop.h"
38 #include "wx/osx/private.h"
39 #include "wx/sysopt.h"
40 #include "wx/modalhook.h"
42 #include <mach-o/dyld.h>
44 // ============================================================================
46 // ============================================================================
49 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
50 // - as setAllowedFileTypes is only functional for NSOpenPanel on 10.6+, on earlier systems, the file
51 // type choice will not be shown, but all possible file items will be shown, if a popup must be working
52 // then the delegate method - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename will have to
60 // Even if we require 10.6, we might be loaded by an application that
61 // was linked against 10.5. setAllowedFileTypes will still be ignored
62 // in this case. From NSSavePanel.h:
63 // NSOpenPanel: On versions less than 10.6, this property is ignored.
64 // For applications that link against 10.6 and higher, this property will
65 // determine which files should be enabled in the open panel.
66 int32_t version = NSVersionOfLinkTimeLibrary("AppKit");
69 // If we're loaded by an application that doesn't link against AppKit,
70 // use the runtime version instead. This check will not work for the
72 version = NSVersionOfRunTimeLibrary("AppKit");
75 // Notice that this still works correctly even if version is -1.
76 return version >= 0x40e2400 /* version of 10.6 AppKit */;
79 } // anonymous namespace
81 @interface wxOpenPanelDelegate : NSObject wxOSX_10_6_AND_LATER(<NSOpenSavePanelDelegate>)
83 wxFileDialog* _dialog;
86 - (wxFileDialog*) fileDialog;
87 - (void) setFileDialog:(wxFileDialog*) dialog;
89 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename;
93 @implementation wxOpenPanelDelegate
102 - (wxFileDialog*) fileDialog
107 - (void) setFileDialog:(wxFileDialog*) dialog
112 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
114 BOOL showObject = YES;
116 NSString* resolvedLink = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath:filename];
117 if ( resolvedLink != nil )
118 filename = resolvedLink;
120 NSDictionary* fileAttribs = [[NSFileManager defaultManager]
121 fileAttributesAtPath:filename traverseLink:YES];
124 // check for packages
125 if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]])
127 if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename] == NO)
128 showObject = YES; // it's a folder, OK to show
131 // it's a packaged directory, apply check
132 wxCFStringRef filecf([filename retain]);
133 showObject = _dialog->CheckFile(filecf.AsString());
138 // the code above only solves links, not aliases, do this here:
140 NSString* resolvedAlias = nil;
142 CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
143 (CFStringRef)filename,
144 kCFURLPOSIXPathStyle,
149 if (CFURLGetFSRef(url, &fsRef))
151 Boolean targetIsFolder, wasAliased;
152 OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
154 if ((err == noErr) && wasAliased)
156 CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
157 if (resolvedUrl != NULL)
159 resolvedAlias = (NSString*) CFURLCopyFileSystemPath(resolvedUrl,
160 kCFURLPOSIXPathStyle);
161 CFRelease(resolvedUrl);
168 if (resolvedAlias != nil)
171 [resolvedAlias autorelease];
172 showObject = [self panel:sender shouldShowFilename:resolvedAlias];
176 wxCFStringRef filecf([filename retain]);
177 showObject = _dialog->CheckFile(filecf.AsString());
187 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
189 void wxFileDialog::Init()
193 m_sheetDelegate = nil;
196 void wxFileDialog::Create(
197 wxWindow *parent, const wxString& message,
198 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
199 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
201 wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
203 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
204 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
207 wxFileDialog::~wxFileDialog()
209 [m_sheetDelegate release];
212 bool wxFileDialog::SupportsExtraControl() const
217 NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions )
219 NSMutableArray* types = nil;
222 wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ;
223 while ( tokenizer.HasMoreTokens() )
225 wxString extension = tokenizer.GetNextToken() ;
226 // Remove leading '*'
227 if ( extension.length() && (extension.GetChar(0) == '*') )
228 extension = extension.Mid( 1 );
230 // Remove leading '.'
231 if ( extension.length() && (extension.GetChar(0) == '.') )
232 extension = extension.Mid( 1 );
234 // Remove leading '*', this is for handling *.*
235 if ( extension.length() && (extension.GetChar(0) == '*') )
236 extension = extension.Mid( 1 );
238 if ( extension.IsEmpty() )
247 types = [[NSMutableArray alloc] init];
249 extensions.Add(extension.Lower());
250 wxCFStringRef cfext(extension);
251 [types addObject: (NSString*)cfext.AsNSString() ];
253 // add support for classic fileType / creator here
254 wxUint32 fileType, creator;
255 // extension -> mactypes
262 NSArray* GetTypesFromFilter( const wxString& filter, wxArrayString& names, wxArrayString& extensiongroups )
264 NSMutableArray* types = nil;
265 bool allowAll = false;
268 extensiongroups.Clear();
270 if ( !filter.empty() )
272 wxStringTokenizer tokenizer( filter, wxT("|") );
273 int numtokens = (int)tokenizer.CountTokens();
276 // we allow for compatibility reason to have a single filter expression (like *.*) without
277 // an explanatory text, in that case the first part is name and extension at the same time
278 wxString extension = tokenizer.GetNextToken();
279 names.Add( extension );
280 extensiongroups.Add( extension );
284 int numextensions = numtokens / 2;
285 for(int i = 0; i < numextensions; i++)
287 wxString name = tokenizer.GetNextToken();
288 wxString extension = tokenizer.GetNextToken();
290 extensiongroups.Add( extension );
294 const size_t extCount = extensiongroups.GetCount();
295 wxArrayString extensions;
296 for ( size_t i = 0 ; i < extCount; i++ )
298 NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions);
299 if ( exttypes != nil )
301 if ( allowAll == false )
304 types = [[NSMutableArray alloc] init];
306 [types addObjectsFromArray:exttypes];
321 void wxFileDialog::ShowWindowModal()
323 wxCFStringRef cf( m_message );
324 wxCFStringRef dir( m_dir );
325 wxCFStringRef file( m_fileName );
327 wxNonOwnedWindow* parentWindow = NULL;
329 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
332 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
334 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
336 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
337 if ( HasFlag(wxFD_SAVE) )
339 NSSavePanel* sPanel = [NSSavePanel savePanel];
341 SetupExtraControls(sPanel);
343 // makes things more convenient:
344 [sPanel setCanCreateDirectories:YES];
345 [sPanel setMessage:cf.AsNSString()];
346 // if we should be able to descend into pacakges we must somehow
347 // be able to pass this in
348 [sPanel setTreatsFilePackagesAsDirectories:NO];
349 [sPanel setCanSelectHiddenExtension:YES];
350 [sPanel setAllowedFileTypes:types];
351 [sPanel setAllowsOtherFileTypes:NO];
353 NSWindow* nativeParent = parentWindow->GetWXWindow();
354 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
355 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
356 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
361 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
363 SetupExtraControls(oPanel);
365 [oPanel setTreatsFilePackagesAsDirectories:NO];
366 [oPanel setCanChooseDirectories:NO];
367 [oPanel setResolvesAliases:YES];
368 [oPanel setCanChooseFiles:YES];
369 [oPanel setMessage:cf.AsNSString()];
370 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
372 NSWindow* nativeParent = parentWindow->GetWXWindow();
373 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
374 types: types modalForWindow: nativeParent
375 modalDelegate: m_sheetDelegate
376 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
381 // Create a panel with the file type drop down list
382 // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add
383 // them to the panel as well
384 // Returns the newly created wxPanel
386 wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
388 wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
389 wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
390 extrapanel->SetSizer(verticalSizer);
392 // the file type control
394 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
395 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
396 wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") );
397 horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
398 m_filterChoice = new wxChoice(extrapanel, wxID_ANY);
399 horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
400 m_filterChoice->Append(m_filterNames);
401 if( m_filterNames.GetCount() > 0)
403 if ( m_firstFileTypeFilter >= 0 )
404 m_filterChoice->SetSelection(m_firstFileTypeFilter);
406 m_filterChoice->Connect(wxEVT_CHOICE, wxCommandEventHandler(wxFileDialog::OnFilterSelected), NULL, this);
411 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
412 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
414 extracontrol->Reparent(extrapanel);
415 horizontalSizer->Add(extracontrol);
418 verticalSizer->Layout();
419 verticalSizer->SetSizeHints(extrapanel);
423 void wxFileDialog::DoOnFilterSelected(int index)
425 NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
426 NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
428 [panel validateVisibleColumns];
430 [panel setAllowedFileTypes:types];
433 // An item has been selected in the file filter wxChoice:
434 void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
436 DoOnFilterSelected( m_filterChoice->GetSelection() );
439 bool wxFileDialog::CheckFile( const wxString& filename )
441 if ( m_currentExtensions.GetCount() == 0 )
444 wxString ext = filename.AfterLast('.').Lower();
446 for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i )
448 if ( ext == m_currentExtensions[i] )
454 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
456 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
457 // for sandboxed app we cannot access the outer structures
458 // this leads to problems with extra controls, so as a temporary
459 // workaround for crashes we don't support those yet
460 if ( [panel contentView] == nil )
463 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
464 wxWindow* extracontrol = NULL;
465 if ( HasExtraControlCreator() )
467 CreateExtraControl();
468 extracontrol = GetExtraControl();
471 NSView* accView = nil;
473 if ( m_useFileTypeFilter )
475 m_filterPanel = CreateFilterPanel(extracontrol);
476 accView = m_filterPanel->GetHandle();
477 if( HasFlag(wxFD_OPEN) )
479 if ( UMAGetSystemVersion() < 0x1060 || !HasAppKit_10_6() )
481 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
482 [del setFileDialog:this];
483 [panel setDelegate:del];
490 m_filterPanel = NULL;
491 m_filterChoice = NULL;
492 if ( extracontrol != nil )
493 accView = extracontrol->GetHandle();
496 if ( accView != nil )
498 [accView removeFromSuperview];
499 [panel setAccessoryView:accView];
503 [panel setAccessoryView:nil];
507 int wxFileDialog::ShowModal()
509 WX_HOOK_MODAL_DIALOG();
511 wxCFEventLoopPauseIdleEvents pause;
513 wxMacAutoreleasePool autoreleasepool;
515 wxCFStringRef cf( m_message );
517 wxCFStringRef dir( m_dir );
518 wxCFStringRef file( m_fileName );
520 m_path = wxEmptyString;
524 wxNonOwnedWindow* parentWindow = NULL;
529 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
533 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
535 m_useFileTypeFilter = m_filterExtensions.GetCount() > 1;
537 if( HasFlag(wxFD_OPEN) )
539 if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) )
540 m_useFileTypeFilter = false;
543 m_firstFileTypeFilter = -1;
545 if ( m_useFileTypeFilter
546 && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() )
548 m_firstFileTypeFilter = m_filterIndex;
550 else if ( m_useFileTypeFilter )
553 bool useDefault = true;
554 for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i )
556 types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions);
557 if ( m_currentExtensions.GetCount() == 0 )
560 m_firstFileTypeFilter = i;
564 for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j )
566 if ( m_fileName.EndsWith(m_currentExtensions[j]) )
568 m_firstFileTypeFilter = i;
578 types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions);
579 m_firstFileTypeFilter = 0;
583 if ( HasFlag(wxFD_SAVE) )
585 NSSavePanel* sPanel = [NSSavePanel savePanel];
587 SetupExtraControls(sPanel);
589 // makes things more convenient:
590 [sPanel setCanCreateDirectories:YES];
591 [sPanel setMessage:cf.AsNSString()];
592 // if we should be able to descend into pacakges we must somehow
593 // be able to pass this in
594 [sPanel setTreatsFilePackagesAsDirectories:NO];
595 [sPanel setCanSelectHiddenExtension:YES];
596 [sPanel setAllowedFileTypes:types];
597 [sPanel setAllowsOtherFileTypes:NO];
599 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
604 Let the file dialog know what file type should be used initially.
605 If this is not done then when setting the filter index
606 programmatically to 1 the file will still have the extension
607 of the first file type instead of the second one. E.g. when file
608 types are foo and bar, a filename "myletter" with SetDialogIndex(1)
609 would result in saving as myletter.foo, while we want myletter.bar.
611 if(m_firstFileTypeFilter > 0)
613 DoOnFilterSelected(m_firstFileTypeFilter);
616 returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ];
617 ModalFinishedCallback(sPanel, returnCode);
621 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
623 SetupExtraControls(oPanel);
625 [oPanel setTreatsFilePackagesAsDirectories:NO];
626 [oPanel setCanChooseDirectories:NO];
627 [oPanel setResolvesAliases:YES];
628 [oPanel setCanChooseFiles:YES];
629 [oPanel setMessage:cf.AsNSString()];
630 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
632 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
633 if ( UMAGetSystemVersion() >= 0x1060 && HasAppKit_10_6() )
635 [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
636 if ( !m_dir.IsEmpty() )
637 [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
639 returnCode = [oPanel runModal];
644 returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString()
645 file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
648 ModalFinishedCallback(oPanel, returnCode);
651 return GetReturnCode();
654 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
656 int result = wxID_CANCEL;
657 if (HasFlag(wxFD_SAVE))
659 if (returnCode == NSOKButton )
661 NSSavePanel* sPanel = (NSSavePanel*)panel;
664 m_path = wxCFStringRef::AsStringWithNormalizationFormC([sPanel filename]);
665 m_fileName = wxFileNameFromPath(m_path);
666 m_dir = wxPathOnly( m_path );
669 m_filterIndex = m_filterChoice->GetSelection();
675 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
676 if (returnCode == NSOKButton )
680 NSArray* filenames = [oPanel filenames];
681 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
683 wxString fnstr = wxCFStringRef::AsStringWithNormalizationFormC([filenames objectAtIndex:i]);
684 m_paths.Add( fnstr );
685 m_fileNames.Add( wxFileNameFromPath(fnstr) );
689 m_fileName = wxFileNameFromPath(fnstr);
690 m_dir = wxPathOnly( fnstr );
696 [oPanel setDelegate:nil];
697 [m_delegate release];
701 SetReturnCode(result);
703 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
704 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
706 // workaround for sandboxed app, see above
707 if ( m_isNativeWindowWrapper )
709 [(NSSavePanel*) panel setAccessoryView:nil];
712 #endif // wxUSE_FILEDLG