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"
38 #include "wx/osx/private.h"
39 #include "wx/sysopt.h"
41 #include <mach-o/dyld.h>
43 // ============================================================================
45 // ============================================================================
48 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
49 // - as setAllowedFileTypes is only functional for NSOpenPanel on 10.6+, on earlier systems, the file
50 // type choice will not be shown, but all possible file items will be shown, if a popup must be working
51 // then the delegate method - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename will have to
59 // Even if we require 10.6, we might be loaded by an application that
60 // was linked against 10.5. setAllowedFileTypes will still be ignored
61 // in this case. From NSSavePanel.h:
62 // NSOpenPanel: On versions less than 10.6, this property is ignored.
63 // For applications that link against 10.6 and higher, this property will
64 // determine which files should be enabled in the open panel.
65 int32_t version = NSVersionOfLinkTimeLibrary("AppKit");
68 // If we're loaded by an application that doesn't link against AppKit,
69 // use the runtime version instead. This check will not work for the
71 version = NSVersionOfRunTimeLibrary("AppKit");
74 // Notice that this still works correctly even if version is -1.
75 return version >= 0x40e2400 /* version of 10.6 AppKit */;
78 } // anonymous namespace
80 @interface wxOpenPanelDelegate : NSObject wxOSX_10_6_AND_LATER(<NSOpenSavePanelDelegate>)
82 wxFileDialog* _dialog;
85 - (wxFileDialog*) fileDialog;
86 - (void) setFileDialog:(wxFileDialog*) dialog;
88 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename;
92 @implementation wxOpenPanelDelegate
101 - (wxFileDialog*) fileDialog
106 - (void) setFileDialog:(wxFileDialog*) dialog
111 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
113 BOOL showObject = YES;
115 NSString* resolvedLink = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath:filename];
116 if ( resolvedLink != nil )
117 filename = resolvedLink;
119 NSDictionary* fileAttribs = [[NSFileManager defaultManager]
120 fileAttributesAtPath:filename traverseLink:YES];
123 // check for packages
124 if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]])
126 if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename] == NO)
127 showObject = YES; // it's a folder, OK to show
130 // it's a packaged directory, apply check
131 wxCFStringRef filecf([filename retain]);
132 showObject = _dialog->CheckFile(filecf.AsString());
137 // the code above only solves links, not aliases, do this here:
139 NSString* resolvedAlias = nil;
141 CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault,
142 (CFStringRef)filename,
143 kCFURLPOSIXPathStyle,
148 if (CFURLGetFSRef(url, &fsRef))
150 Boolean targetIsFolder, wasAliased;
151 OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
153 if ((err == noErr) && wasAliased)
155 CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
156 if (resolvedUrl != NULL)
158 resolvedAlias = (NSString*) CFURLCopyFileSystemPath(resolvedUrl,
159 kCFURLPOSIXPathStyle);
160 CFRelease(resolvedUrl);
167 if (resolvedAlias != nil)
170 [resolvedAlias autorelease];
171 showObject = [self panel:sender shouldShowFilename:resolvedAlias];
175 wxCFStringRef filecf([filename retain]);
176 showObject = _dialog->CheckFile(filecf.AsString());
186 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
188 wxFileDialog::wxFileDialog(
189 wxWindow *parent, const wxString& message,
190 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
191 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
192 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
195 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
196 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
199 wxFileDialog::~wxFileDialog()
201 [m_sheetDelegate release];
204 bool wxFileDialog::SupportsExtraControl() const
209 NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions )
211 NSMutableArray* types = nil;
214 wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ;
215 while ( tokenizer.HasMoreTokens() )
217 wxString extension = tokenizer.GetNextToken() ;
218 // Remove leading '*'
219 if ( extension.length() && (extension.GetChar(0) == '*') )
220 extension = extension.Mid( 1 );
222 // Remove leading '.'
223 if ( extension.length() && (extension.GetChar(0) == '.') )
224 extension = extension.Mid( 1 );
226 // Remove leading '*', this is for handling *.*
227 if ( extension.length() && (extension.GetChar(0) == '*') )
228 extension = extension.Mid( 1 );
230 if ( extension.IsEmpty() )
239 types = [[NSMutableArray alloc] init];
241 extensions.Add(extension.Lower());
242 wxCFStringRef cfext(extension);
243 [types addObject: (NSString*)cfext.AsNSString() ];
245 // add support for classic fileType / creator here
246 wxUint32 fileType, creator;
247 // extension -> mactypes
254 NSArray* GetTypesFromFilter( const wxString& filter, wxArrayString& names, wxArrayString& extensiongroups )
256 NSMutableArray* types = nil;
257 bool allowAll = false;
260 extensiongroups.Clear();
262 if ( !filter.empty() )
264 wxStringTokenizer tokenizer( filter, wxT("|") );
265 int numtokens = (int)tokenizer.CountTokens();
268 // we allow for compatibility reason to have a single filter expression (like *.*) without
269 // an explanatory text, in that case the first part is name and extension at the same time
270 wxString extension = tokenizer.GetNextToken();
271 names.Add( extension );
272 extensiongroups.Add( extension );
276 int numextensions = numtokens / 2;
277 for(int i = 0; i < numextensions; i++)
279 wxString name = tokenizer.GetNextToken();
280 wxString extension = tokenizer.GetNextToken();
282 extensiongroups.Add( extension );
286 const size_t extCount = extensiongroups.GetCount();
287 wxArrayString extensions;
288 for ( size_t i = 0 ; i < extCount; i++ )
290 NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions);
291 if ( exttypes != nil )
293 if ( allowAll == false )
296 types = [[NSMutableArray alloc] init];
298 [types addObjectsFromArray:exttypes];
313 void wxFileDialog::ShowWindowModal()
315 wxCFStringRef cf( m_message );
316 wxCFStringRef dir( m_dir );
317 wxCFStringRef file( m_fileName );
319 wxNonOwnedWindow* parentWindow = NULL;
321 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
324 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
326 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
328 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
329 if ( HasFlag(wxFD_SAVE) )
331 NSSavePanel* sPanel = [NSSavePanel savePanel];
333 SetupExtraControls(sPanel);
335 // makes things more convenient:
336 [sPanel setCanCreateDirectories:YES];
337 [sPanel setMessage:cf.AsNSString()];
338 // if we should be able to descend into pacakges we must somehow
339 // be able to pass this in
340 [sPanel setTreatsFilePackagesAsDirectories:NO];
341 [sPanel setCanSelectHiddenExtension:YES];
342 [sPanel setAllowedFileTypes:types];
343 [sPanel setAllowsOtherFileTypes:NO];
345 NSWindow* nativeParent = parentWindow->GetWXWindow();
346 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
347 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
348 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
353 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
355 SetupExtraControls(oPanel);
357 [oPanel setTreatsFilePackagesAsDirectories:NO];
358 [oPanel setCanChooseDirectories:NO];
359 [oPanel setResolvesAliases:YES];
360 [oPanel setCanChooseFiles:YES];
361 [oPanel setMessage:cf.AsNSString()];
362 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
364 NSWindow* nativeParent = parentWindow->GetWXWindow();
365 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
366 types: types modalForWindow: nativeParent
367 modalDelegate: m_sheetDelegate
368 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
373 // Create a panel with the file type drop down list
374 // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add
375 // them to the panel as well
376 // Returns the newly created wxPanel
378 wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
380 wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
381 wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
382 extrapanel->SetSizer(verticalSizer);
384 // the file type control
386 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
387 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
388 wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") );
389 horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
390 m_filterChoice = new wxChoice(extrapanel, wxID_ANY);
391 horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
392 m_filterChoice->Append(m_filterNames);
393 if( m_filterNames.GetCount() > 0)
395 if ( m_firstFileTypeFilter >= 0 )
396 m_filterChoice->SetSelection(m_firstFileTypeFilter);
398 m_filterChoice->Connect(wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler(wxFileDialog::OnFilterSelected), NULL, this);
403 wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
404 verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
406 extracontrol->Reparent(extrapanel);
407 horizontalSizer->Add(extracontrol);
410 verticalSizer->Layout();
411 verticalSizer->SetSizeHints(extrapanel);
415 void wxFileDialog::DoOnFilterSelected(int index)
417 NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
418 NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
420 [panel validateVisibleColumns];
422 [panel setAllowedFileTypes:types];
425 // An item has been selected in the file filter wxChoice:
426 void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) )
428 DoOnFilterSelected( m_filterChoice->GetSelection() );
431 bool wxFileDialog::CheckFile( const wxString& filename )
433 if ( m_currentExtensions.GetCount() == 0 )
436 wxString ext = filename.AfterLast('.').Lower();
438 for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i )
440 if ( ext == m_currentExtensions[i] )
446 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
448 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
450 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
451 wxWindow* extracontrol = NULL;
452 if ( HasExtraControlCreator() )
454 CreateExtraControl();
455 extracontrol = GetExtraControl();
458 NSView* accView = nil;
461 if ( m_useFileTypeFilter )
463 m_filterPanel = CreateFilterPanel(extracontrol);
464 accView = m_filterPanel->GetHandle();
465 if( HasFlag(wxFD_OPEN) )
467 if ( UMAGetSystemVersion() < 0x1060 || !HasAppKit_10_6() )
469 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
470 [del setFileDialog:this];
471 [panel setDelegate:del];
478 m_filterPanel = NULL;
479 m_filterChoice = NULL;
480 if ( extracontrol != nil )
481 accView = extracontrol->GetHandle();
484 if ( accView != nil )
486 [accView removeFromSuperview];
487 [panel setAccessoryView:accView];
491 [panel setAccessoryView:nil];
495 int wxFileDialog::ShowModal()
497 wxMacAutoreleasePool autoreleasepool;
499 wxCFStringRef cf( m_message );
501 wxCFStringRef dir( m_dir );
502 wxCFStringRef file( m_fileName );
504 m_path = wxEmptyString;
508 wxNonOwnedWindow* parentWindow = NULL;
513 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
517 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
519 m_useFileTypeFilter = m_filterExtensions.GetCount() > 1;
521 if( HasFlag(wxFD_OPEN) )
523 if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) )
524 m_useFileTypeFilter = false;
527 m_firstFileTypeFilter = -1;
529 if ( m_useFileTypeFilter
530 && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() )
532 m_firstFileTypeFilter = m_filterIndex;
534 else if ( m_useFileTypeFilter )
537 bool useDefault = true;
538 for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i )
540 types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions);
541 if ( m_currentExtensions.GetCount() == 0 )
544 m_firstFileTypeFilter = i;
548 for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j )
550 if ( m_fileName.EndsWith(m_currentExtensions[j]) )
552 m_firstFileTypeFilter = i;
562 types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions);
563 m_firstFileTypeFilter = 0;
567 if ( HasFlag(wxFD_SAVE) )
569 NSSavePanel* sPanel = [NSSavePanel savePanel];
571 SetupExtraControls(sPanel);
573 // makes things more convenient:
574 [sPanel setCanCreateDirectories:YES];
575 [sPanel setMessage:cf.AsNSString()];
576 // if we should be able to descend into pacakges we must somehow
577 // be able to pass this in
578 [sPanel setTreatsFilePackagesAsDirectories:NO];
579 [sPanel setCanSelectHiddenExtension:YES];
580 [sPanel setAllowedFileTypes:types];
581 [sPanel setAllowsOtherFileTypes:NO];
583 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
588 Let the file dialog know what file type should be used initially.
589 If this is not done then when setting the filter index
590 programmatically to 1 the file will still have the extension
591 of the first file type instead of the second one. E.g. when file
592 types are foo and bar, a filename "myletter" with SetDialogIndex(1)
593 would result in saving as myletter.foo, while we want myletter.bar.
595 if(m_firstFileTypeFilter > 0)
597 DoOnFilterSelected(m_firstFileTypeFilter);
600 returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ];
601 ModalFinishedCallback(sPanel, returnCode);
605 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
607 SetupExtraControls(oPanel);
609 [oPanel setTreatsFilePackagesAsDirectories:NO];
610 [oPanel setCanChooseDirectories:NO];
611 [oPanel setResolvesAliases:YES];
612 [oPanel setCanChooseFiles:YES];
613 [oPanel setMessage:cf.AsNSString()];
614 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
616 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
617 if ( UMAGetSystemVersion() >= 0x1060 && HasAppKit_10_6() )
619 [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
620 if ( !m_dir.IsEmpty() )
621 [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
623 returnCode = [oPanel runModal];
628 returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString()
629 file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
632 ModalFinishedCallback(oPanel, returnCode);
635 return GetReturnCode();
638 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
640 int result = wxID_CANCEL;
641 if (HasFlag(wxFD_SAVE))
643 if (returnCode == NSOKButton )
645 NSSavePanel* sPanel = (NSSavePanel*)panel;
648 m_path = wxCFStringRef::AsString([sPanel filename]);
649 m_fileName = wxFileNameFromPath(m_path);
650 m_dir = wxPathOnly( m_path );
653 m_filterIndex = m_filterChoice->GetSelection();
659 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
660 if (returnCode == NSOKButton )
664 NSArray* filenames = [oPanel filenames];
665 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
667 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
668 m_paths.Add( fnstr );
669 m_fileNames.Add( wxFileNameFromPath(fnstr) );
673 m_fileName = wxFileNameFromPath(fnstr);
674 m_dir = wxPathOnly( fnstr );
680 [oPanel setDelegate:nil];
681 [m_delegate release];
685 SetReturnCode(result);
687 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
688 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
691 [(NSSavePanel*) panel setAccessoryView:nil];
694 #endif // wxUSE_FILEDLG