1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/filedlg.mm
3 // Purpose: wxFileDialog for wxCocoa
7 // RCS-ID: $Id: filedlg.mm 40007 2006-07-05 13:10:46Z SC $
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"
32 #include "wx/filename.h"
34 #include "wx/osx/private.h"
36 // ============================================================================
38 // ============================================================================
41 // - support for old style MacOS creator / type combos
42 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
44 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
46 wxFileDialog::wxFileDialog(
47 wxWindow *parent, const wxString& message,
48 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
49 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
50 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
54 NSArray* GetTypesFromFilter( const wxString filter )
56 NSMutableArray* types = nil;
57 if ( !filter.empty() )
60 wxArrayString extensions;
62 wxString filter2(filter) ;
67 for ( unsigned int i = 0; i < filter2.length() ; i++ )
69 if ( filter2.GetChar(i) == wxT('|') )
73 names.Add( current ) ;
77 extensions.Add( current ) ;
82 current = wxEmptyString ;
86 current += filter2.GetChar(i) ;
89 // we allow for compatibility reason to have a single filter expression (like *.*) without
90 // an explanatory text, in that case the first part is name and extension at the same time
92 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
93 if ( current.empty() )
94 extensions.Add( names[filterIndex] ) ;
96 extensions.Add( current ) ;
97 if ( filterIndex == 0 || isName )
98 names.Add( current ) ;
102 const size_t extCount = extensions.GetCount();
103 for ( size_t i = 0 ; i < extCount; i++ )
105 wxString extension = extensions[i];
107 // Remove leading '*'
108 if (extension.length() && (extension.GetChar(0) == '*'))
109 extension = extension.Mid( 1 );
111 // Remove leading '.'
112 if (extension.length() && (extension.GetChar(0) == '.'))
113 extension = extension.Mid( 1 );
115 if ( extension.IsEmpty() )
124 types = [[NSMutableArray alloc] init];
126 wxCFStringRef cfext(extension);
127 [types addObject: (NSString*)cfext.AsNSString() ];
129 // add support for classic fileType / creator here
130 wxUint32 fileType, creator;
131 // extension -> mactypes
138 int wxFileDialog::ShowModal()
140 int result = wxID_CANCEL;
142 NSSavePanel *panel = nil;
144 wxCFStringRef cf( m_message );
146 wxCFStringRef dir( m_dir );
147 wxCFStringRef file( m_fileName );
149 m_path = wxEmptyString;
152 wxNonOwnedWindow* parentWindow = NULL;
157 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
160 if (HasFlag(wxFD_SAVE))
162 NSSavePanel* sPanel = [NSSavePanel savePanel];
163 // makes things more convenient:
164 [sPanel setCanCreateDirectories:YES];
165 [sPanel setMessage:cf.AsNSString()];
166 // if we should be able to descend into pacakges we must somehow
167 // be able to pass this in
168 [sPanel setTreatsFilePackagesAsDirectories:NO];
169 [sPanel setCanSelectHiddenExtension:YES];
171 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
177 NSWindow* nativeParent = parentWindow->GetWXWindow();
178 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
179 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
180 modalForWindow: nativeParent modalDelegate: sheetDelegate
181 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
183 [sheetDelegate waitForSheetToFinish];
184 returnCode = [sheetDelegate code];
185 [sheetDelegate release];
189 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
192 if (returnCode == NSOKButton )
197 wxCFStringRef filename( [[sPanel filename] retain] );
199 m_path = filename.AsString();
200 m_fileName = wxFileNameFromPath(m_path);
201 m_dir = wxPathOnly( m_path );
206 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
207 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
208 [oPanel setTreatsFilePackagesAsDirectories:NO];
209 [oPanel setCanChooseDirectories:NO];
210 [oPanel setResolvesAliases:YES];
211 [oPanel setCanChooseFiles:YES];
212 [oPanel setMessage:cf.AsNSString()];
216 NSWindow* nativeParent = parentWindow->GetWXWindow();
217 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
218 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
219 types: types modalForWindow: nativeParent
220 modalDelegate: sheetDelegate
221 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
223 [sheetDelegate waitForSheetToFinish];
224 returnCode = [sheetDelegate code];
225 [sheetDelegate release];
229 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
230 file:file.AsNSString() types:types];
232 if (returnCode == NSOKButton )
236 NSArray* filenames = [oPanel filenames];
237 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
239 wxCFStringRef filename( [(NSString*) [filenames objectAtIndex:i] retain] );
240 wxString fnstr = filename.AsString();
241 m_paths.Add( fnstr );
242 m_fileNames.Add( wxFileNameFromPath(fnstr) );
246 m_fileName = wxFileNameFromPath(fnstr);
247 m_dir = wxPathOnly( fnstr );
258 #endif // wxUSE_FILEDLG