1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/filedlg.h"
28 #include "wx/msgdlg.h"
31 #include "wx/stattext.h"
32 #include "wx/choice.h"
35 #include "wx/filename.h"
36 #include "wx/tokenzr.h"
37 #include "wx/evtloop.h"
39 #include "wx/osx/private.h"
40 #include "wx/sysopt.h"
41 #include "wx/modalhook.h"
43 #include <mach-o/dyld.h>
45 // ============================================================================
47 // ============================================================================
50 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
51 // - as setAllowedFileTypes is only functional for NSOpenPanel on 10.6+, on earlier systems, the file
52 // type choice will not be shown, but all possible file items will be shown, if a popup must be working
53 // then the delegate method - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename will have to
61 // Even if we require 10.6, we might be loaded by an application that
62 // was linked against 10.5. setAllowedFileTypes will still be ignored
63 // in this case. From NSSavePanel.h:
64 // NSOpenPanel: On versions less than 10.6, this property is ignored.
65 // For applications that link against 10.6 and higher, this property will
66 // determine which files should be enabled in the open panel.
67 int32_t version = NSVersionOfLinkTimeLibrary("AppKit");
70 // If we're loaded by an application that doesn't link against AppKit,
71 // use the runtime version instead. This check will not work for the
73 version = NSVersionOfRunTimeLibrary("AppKit");
76 // Notice that this still works correctly even if version is -1.
77 return version >= 0x40e2400 /* version of 10.6 AppKit */;
80 } // anonymous namespace
82 @interface wxOpenPanelDelegate : NSObject wxOSX_10_6_AND_LATER(<NSOpenSavePanelDelegate>)
84 wxFileDialog* _dialog;
87 - (wxFileDialog*) fileDialog;
88 - (void) setFileDialog:(wxFileDialog*) dialog;
90 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename;
94 @implementation wxOpenPanelDelegate
103 - (wxFileDialog*) fileDialog
108 - (void) setFileDialog:(wxFileDialog*) dialog
113 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
115 BOOL showObject = YES;
117 NSString* resolvedLink = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath:filename];
118 if ( resolvedLink != nil )
119 filename = resolvedLink;
121 NSDictionary* fileAttribs = [[NSFileManager defaultManager]
122 fileAttributesAtPath:filename traverseLink:YES];
125 // check for packages
126 if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]])
128 if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename] == NO)
129 showObject = YES; // it's a folder, OK to show
132 // it's a packaged directory, apply check
133 wxCFStringRef filecf([filename retain]);
134 showObject = _dialog->CheckFile(filecf.AsString());
139 // the code above only solves links, not aliases, do this here:
141 NSString* resolvedAlias = nil;
143 CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
144 (CFStringRef)filename,
145 kCFURLPOSIXPathStyle,
150 if (CFURLGetFSRef(url, &fsRef))
152 Boolean targetIsFolder, wasAliased;
153 OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
155 if ((err == noErr) && wasAliased)
157 CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
158 if (resolvedUrl != NULL)
160 resolvedAlias = (NSString*) CFURLCopyFileSystemPath(resolvedUrl,
161 kCFURLPOSIXPathStyle);
162 CFRelease(resolvedUrl);
169 if (resolvedAlias != nil)
172 [resolvedAlias autorelease];
173 showObject = [self panel:sender shouldShowFilename:resolvedAlias];
177 wxCFStringRef filecf([filename retain]);
178 showObject = _dialog->CheckFile(filecf.AsString());
188 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
190 void wxFileDialog::Init()
194 m_sheetDelegate = nil;
197 void wxFileDialog::Create(
198 wxWindow *parent, const wxString& message,
199 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
200 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
202 wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
204 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
205 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
208 wxFileDialog::~wxFileDialog()
210 [m_sheetDelegate release];
213 bool wxFileDialog::SupportsExtraControl() const
218 NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions )
220 NSMutableArray* types = nil;
223 wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ;
224 while ( tokenizer.HasMoreTokens() )
226 wxString extension = tokenizer.GetNextToken() ;
227 // Remove leading '*'
228 if ( extension.length() && (extension.GetChar(0) == '*') )
229 extension = extension.Mid( 1 );
231 // Remove leading '.'
232 if ( extension.length() && (extension.GetChar(0) == '.') )
233 extension = extension.Mid( 1 );
235 // Remove leading '*', this is for handling *.*
236 if ( extension.length() && (extension.GetChar(0) == '*') )
237 extension = extension.Mid( 1 );
239 if ( extension.IsEmpty() )
248 types = [[NSMutableArray alloc] init];
250 extensions.Add(extension.Lower());
251 wxCFStringRef cfext(extension);
252 [types addObject: (NSString*)cfext.AsNSString() ];
254 // add support for classic fileType / creator here
255 wxUint32 fileType, creator;
256 // extension -> mactypes
263 NSArray* GetTypesFromFilter( const wxString& filter, wxArrayString& names, wxArrayString& extensiongroups )
265 NSMutableArray* types = nil;
266 bool allowAll = false;
269 extensiongroups.Clear();
271 if ( !filter.empty() )
273 wxStringTokenizer tokenizer( filter, wxT("|") );
274 int numtokens = (int)tokenizer.CountTokens();
277 // we allow for compatibility reason to have a single filter expression (like *.*) without
278 // an explanatory text, in that case the first part is name and extension at the same time
279 wxString extension = tokenizer.GetNextToken();
280 names.Add( extension );
281 extensiongroups.Add( extension );
285 int numextensions = numtokens / 2;
286 for(int i = 0; i < numextensions; i++)
288 wxString name = tokenizer.GetNextToken();
289 wxString extension = tokenizer.GetNextToken();
291 extensiongroups.Add( extension );
295 const size_t extCount = extensiongroups.GetCount();
296 wxArrayString extensions;
297 for ( size_t i = 0 ; i < extCount; i++ )
299 NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions);
300 if ( exttypes != nil )
302 if ( allowAll == false )
305 types = [[NSMutableArray alloc] init];
307 [types addObjectsFromArray:exttypes];
322 void wxFileDialog::ShowWindowModal()
324 wxCFStringRef cf( m_message );
325 wxCFStringRef dir( m_dir );
326 wxCFStringRef file( m_fileName );
328 wxNonOwnedWindow* parentWindow = NULL;
330 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
333 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
335 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
337 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
338 if ( HasFlag(wxFD_SAVE) )
340 NSSavePanel* sPanel = [NSSavePanel savePanel];
342 SetupExtraControls(sPanel);
344 // makes things more convenient:
345 [sPanel setCanCreateDirectories:YES];
346 [sPanel setMessage:cf.AsNSString()];
347 // if we should be able to descend into pacakges we must somehow
348 // be able to pass this in
349 [sPanel setTreatsFilePackagesAsDirectories:NO];
350 [sPanel setCanSelectHiddenExtension:YES];
351 [sPanel setAllowedFileTypes:types];
352 [sPanel setAllowsOtherFileTypes:NO];
354 NSWindow* nativeParent = parentWindow->GetWXWindow();
355 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
356 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
357 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
362 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
364 SetupExtraControls(oPanel);
366 [oPanel setTreatsFilePackagesAsDirectories:NO];
367 [oPanel setCanChooseDirectories:NO];
368 [oPanel setResolvesAliases:YES];
369 [oPanel setCanChooseFiles:YES];
370 [oPanel setMessage:cf.AsNSString()];
371 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
373 NSWindow* nativeParent = parentWindow->GetWXWindow();
374 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
375 types: types modalForWindow: nativeParent
376 modalDelegate: m_sheetDelegate
377 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
382 // Create a panel with the file type drop down list
383 // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add
384 // them to the panel as well
385 // Returns the newly created wxPanel
387 wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
389 wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
390 wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
391 extrapanel->SetSizer(verticalSizer);
393 // the file type control
395 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
396 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
397 wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") );
398 horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
399 m_filterChoice = new wxChoice(extrapanel, wxID_ANY);
400 horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
401 m_filterChoice->Append(m_filterNames);
402 if( m_filterNames.GetCount() > 0)
404 if ( m_firstFileTypeFilter >= 0 )
405 m_filterChoice->SetSelection(m_firstFileTypeFilter);
407 m_filterChoice->Connect(wxEVT_CHOICE, wxCommandEventHandler(wxFileDialog::OnFilterSelected), NULL, this);
412 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
413 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
415 extracontrol->Reparent(extrapanel);
416 horizontalSizer->Add(extracontrol);
419 verticalSizer->Layout();
420 verticalSizer->SetSizeHints(extrapanel);
424 void wxFileDialog::DoOnFilterSelected(int index)
426 NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
427 NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
429 [panel validateVisibleColumns];
431 [panel setAllowedFileTypes:types];
434 // An item has been selected in the file filter wxChoice:
435 void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
437 DoOnFilterSelected( m_filterChoice->GetSelection() );
440 bool wxFileDialog::CheckFile( const wxString& filename )
442 if ( m_currentExtensions.GetCount() == 0 )
445 wxString ext = filename.AfterLast('.').Lower();
447 for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i )
449 if ( ext == m_currentExtensions[i] )
455 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
457 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
458 // for sandboxed app we cannot access the outer structures
459 // this leads to problems with extra controls, so as a temporary
460 // workaround for crashes we don't support those yet
461 if ( [panel contentView] == nil )
464 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
465 wxWindow* extracontrol = NULL;
466 if ( HasExtraControlCreator() )
468 CreateExtraControl();
469 extracontrol = GetExtraControl();
472 NSView* accView = nil;
474 if ( m_useFileTypeFilter )
476 m_filterPanel = CreateFilterPanel(extracontrol);
477 accView = m_filterPanel->GetHandle();
478 if( HasFlag(wxFD_OPEN) )
480 if ( UMAGetSystemVersion() < 0x1060 || !HasAppKit_10_6() )
482 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
483 [del setFileDialog:this];
484 [panel setDelegate:del];
491 m_filterPanel = NULL;
492 m_filterChoice = NULL;
493 if ( extracontrol != nil )
494 accView = extracontrol->GetHandle();
497 if ( accView != nil )
499 [accView removeFromSuperview];
500 [panel setAccessoryView:accView];
504 [panel setAccessoryView:nil];
508 int wxFileDialog::ShowModal()
510 WX_HOOK_MODAL_DIALOG();
512 wxCFEventLoopPauseIdleEvents pause;
514 wxMacAutoreleasePool autoreleasepool;
516 wxCFStringRef cf( m_message );
518 wxCFStringRef dir( m_dir );
519 wxCFStringRef file( m_fileName );
521 m_path = wxEmptyString;
525 wxNonOwnedWindow* parentWindow = NULL;
530 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
534 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
536 m_useFileTypeFilter = m_filterExtensions.GetCount() > 1;
538 if( HasFlag(wxFD_OPEN) )
540 if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) )
541 m_useFileTypeFilter = false;
544 m_firstFileTypeFilter = -1;
546 if ( m_useFileTypeFilter
547 && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() )
549 m_firstFileTypeFilter = m_filterIndex;
551 else if ( m_useFileTypeFilter )
554 bool useDefault = true;
555 for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i )
557 types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions);
558 if ( m_currentExtensions.GetCount() == 0 )
561 m_firstFileTypeFilter = i;
565 for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j )
567 if ( m_fileName.EndsWith(m_currentExtensions[j]) )
569 m_firstFileTypeFilter = i;
579 types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions);
580 m_firstFileTypeFilter = 0;
584 if ( HasFlag(wxFD_SAVE) )
586 NSSavePanel* sPanel = [NSSavePanel savePanel];
588 SetupExtraControls(sPanel);
590 // makes things more convenient:
591 [sPanel setCanCreateDirectories:YES];
592 [sPanel setMessage:cf.AsNSString()];
593 // if we should be able to descend into pacakges we must somehow
594 // be able to pass this in
595 [sPanel setTreatsFilePackagesAsDirectories:NO];
596 [sPanel setCanSelectHiddenExtension:YES];
597 [sPanel setAllowedFileTypes:types];
598 [sPanel setAllowsOtherFileTypes:NO];
600 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
605 Let the file dialog know what file type should be used initially.
606 If this is not done then when setting the filter index
607 programmatically to 1 the file will still have the extension
608 of the first file type instead of the second one. E.g. when file
609 types are foo and bar, a filename "myletter" with SetDialogIndex(1)
610 would result in saving as myletter.foo, while we want myletter.bar.
612 if(m_firstFileTypeFilter > 0)
614 DoOnFilterSelected(m_firstFileTypeFilter);
617 returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ];
618 ModalFinishedCallback(sPanel, returnCode);
622 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
624 SetupExtraControls(oPanel);
626 [oPanel setTreatsFilePackagesAsDirectories:NO];
627 [oPanel setCanChooseDirectories:NO];
628 [oPanel setResolvesAliases:YES];
629 [oPanel setCanChooseFiles:YES];
630 [oPanel setMessage:cf.AsNSString()];
631 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
633 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
634 if ( UMAGetSystemVersion() >= 0x1060 && HasAppKit_10_6() )
636 [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
637 if ( !m_dir.IsEmpty() )
638 [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
640 returnCode = [oPanel runModal];
645 returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString()
646 file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
649 ModalFinishedCallback(oPanel, returnCode);
652 return GetReturnCode();
655 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
657 int result = wxID_CANCEL;
658 if (HasFlag(wxFD_SAVE))
660 if (returnCode == NSOKButton )
662 NSSavePanel* sPanel = (NSSavePanel*)panel;
665 m_path = wxCFStringRef::AsStringWithNormalizationFormC([sPanel filename]);
666 m_fileName = wxFileNameFromPath(m_path);
667 m_dir = wxPathOnly( m_path );
670 m_filterIndex = m_filterChoice->GetSelection();
676 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
677 if (returnCode == NSOKButton )
681 NSArray* filenames = [oPanel filenames];
682 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
684 wxString fnstr = wxCFStringRef::AsStringWithNormalizationFormC([filenames objectAtIndex:i]);
685 m_paths.Add( fnstr );
686 m_fileNames.Add( wxFileNameFromPath(fnstr) );
690 m_fileName = wxFileNameFromPath(fnstr);
691 m_dir = wxPathOnly( fnstr );
697 [oPanel setDelegate:nil];
698 [m_delegate release];
702 SetReturnCode(result);
704 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
705 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
707 // workaround for sandboxed app, see above
708 if ( m_isNativeWindowWrapper )
710 [(NSSavePanel*) panel setAccessoryView:nil];
713 #endif // wxUSE_FILEDLG