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"
33 #include "wx/tokenzr.h"
35 #include "wx/osx/private.h"
37 // ============================================================================
39 // ============================================================================
42 // - support for old style MacOS creator / type combos
43 // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
45 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
47 wxFileDialog::wxFileDialog(
48 wxWindow *parent, const wxString& message,
49 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
50 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
51 : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name)
55 NSArray* GetTypesFromFilter( const wxString filter )
57 NSMutableArray* types = nil;
58 if ( !filter.empty() )
61 wxArrayString extensions;
63 wxString filter2(filter) ;
68 for ( unsigned int i = 0; i < filter2.length() ; i++ )
70 if ( filter2.GetChar(i) == wxT('|') )
74 names.Add( current ) ;
78 extensions.Add( current ) ;
83 current = wxEmptyString ;
87 current += filter2.GetChar(i) ;
90 // we allow for compatibility reason to have a single filter expression (like *.*) without
91 // an explanatory text, in that case the first part is name and extension at the same time
93 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
94 if ( current.empty() )
95 extensions.Add( names[filterIndex] ) ;
97 extensions.Add( current ) ;
98 if ( filterIndex == 0 || isName )
99 names.Add( current ) ;
103 const size_t extCount = extensions.GetCount();
104 for ( size_t i = 0 ; i < extCount; i++ )
106 wxString extensiongroup = extensions[i];
107 wxStringTokenizer tokenizer( extensiongroup , wxT(";") ) ;
108 while ( tokenizer.HasMoreTokens() )
110 wxString extension = tokenizer.GetNextToken() ;
111 // Remove leading '*'
112 if (extension.length() && (extension.GetChar(0) == '*'))
113 extension = extension.Mid( 1 );
115 // Remove leading '.'
116 if (extension.length() && (extension.GetChar(0) == '.'))
117 extension = extension.Mid( 1 );
119 if ( extension.IsEmpty() )
127 types = [[NSMutableArray alloc] init];
129 wxCFStringRef cfext(extension);
130 [types addObject: (NSString*)cfext.AsNSString() ];
132 // add support for classic fileType / creator here
133 wxUint32 fileType, creator;
134 // extension -> mactypes
143 int wxFileDialog::ShowModal()
145 int result = wxID_CANCEL;
147 NSSavePanel *panel = nil;
149 wxCFStringRef cf( m_message );
151 wxCFStringRef dir( m_dir );
152 wxCFStringRef file( m_fileName );
154 m_path = wxEmptyString;
158 wxNonOwnedWindow* parentWindow = NULL;
163 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
166 if (HasFlag(wxFD_SAVE))
168 NSSavePanel* sPanel = [NSSavePanel savePanel];
169 // makes things more convenient:
170 [sPanel setCanCreateDirectories:YES];
171 [sPanel setMessage:cf.AsNSString()];
172 // if we should be able to descend into pacakges we must somehow
173 // be able to pass this in
174 [sPanel setTreatsFilePackagesAsDirectories:NO];
175 [sPanel setCanSelectHiddenExtension:YES];
177 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
184 NSWindow* nativeParent = parentWindow->GetWXWindow();
185 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
186 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
187 modalForWindow: nativeParent modalDelegate: sheetDelegate
188 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
190 [sheetDelegate waitForSheetToFinish];
191 returnCode = [sheetDelegate code];
192 [sheetDelegate release];
197 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
200 if (returnCode == NSOKButton )
205 m_path = wxCFStringRef::AsString([sPanel filename]);
206 m_fileName = wxFileNameFromPath(m_path);
207 m_dir = wxPathOnly( m_path );
212 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
213 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
214 [oPanel setTreatsFilePackagesAsDirectories:NO];
215 [oPanel setCanChooseDirectories:NO];
216 [oPanel setResolvesAliases:YES];
217 [oPanel setCanChooseFiles:YES];
218 [oPanel setMessage:cf.AsNSString()];
223 NSWindow* nativeParent = parentWindow->GetWXWindow();
224 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
225 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
226 types: types modalForWindow: nativeParent
227 modalDelegate: sheetDelegate
228 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
230 [sheetDelegate waitForSheetToFinish];
231 returnCode = [sheetDelegate code];
232 [sheetDelegate release];
237 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
238 file:file.AsNSString() types:types];
240 if (returnCode == NSOKButton )
244 NSArray* filenames = [oPanel filenames];
245 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
247 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
248 m_paths.Add( fnstr );
249 m_fileNames.Add( wxFileNameFromPath(fnstr) );
253 m_fileName = wxFileNameFromPath(fnstr);
254 m_dir = wxPathOnly( fnstr );
265 #endif // wxUSE_FILEDLG