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 // Remove leading '*', this is for handling *.*
120 if (extension.length() && (extension.GetChar(0) == '*'))
121 extension = extension.Mid( 1 );
123 if ( extension.IsEmpty() )
131 types = [[NSMutableArray alloc] init];
133 wxCFStringRef cfext(extension);
134 [types addObject: (NSString*)cfext.AsNSString() ];
136 // add support for classic fileType / creator here
137 wxUint32 fileType, creator;
138 // extension -> mactypes
147 void wxFileDialog::ShowWindowModal()
149 wxCFStringRef cf( m_message );
150 wxCFStringRef dir( m_dir );
151 wxCFStringRef file( m_fileName );
153 wxNonOwnedWindow* parentWindow = NULL;
155 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
158 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
160 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
162 if (HasFlag(wxFD_SAVE))
164 NSSavePanel* sPanel = [NSSavePanel savePanel];
165 // makes things more convenient:
166 [sPanel setCanCreateDirectories:YES];
167 [sPanel setMessage:cf.AsNSString()];
168 // if we should be able to descend into pacakges we must somehow
169 // be able to pass this in
170 [sPanel setTreatsFilePackagesAsDirectories:NO];
171 [sPanel setCanSelectHiddenExtension:YES];
173 NSWindow* nativeParent = parentWindow->GetWXWindow();
174 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
175 [sheetDelegate setImplementation: this];
176 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
177 modalForWindow: nativeParent modalDelegate: sheetDelegate
178 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
183 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
184 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
185 [oPanel setTreatsFilePackagesAsDirectories:NO];
186 [oPanel setCanChooseDirectories:NO];
187 [oPanel setResolvesAliases:YES];
188 [oPanel setCanChooseFiles:YES];
189 [oPanel setMessage:cf.AsNSString()];
191 NSWindow* nativeParent = parentWindow->GetWXWindow();
192 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
193 [sheetDelegate setImplementation: this];
194 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
195 types: types modalForWindow: nativeParent
196 modalDelegate: sheetDelegate
197 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
202 int wxFileDialog::ShowModal()
204 NSSavePanel *panel = nil;
206 wxCFStringRef cf( m_message );
208 wxCFStringRef dir( m_dir );
209 wxCFStringRef file( m_fileName );
211 m_path = wxEmptyString;
214 // since we don't support retrieving the matching filter
217 wxNonOwnedWindow* parentWindow = NULL;
222 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
225 if (HasFlag(wxFD_SAVE))
227 NSSavePanel* sPanel = [NSSavePanel savePanel];
228 // makes things more convenient:
229 [sPanel setCanCreateDirectories:YES];
230 [sPanel setMessage:cf.AsNSString()];
231 // if we should be able to descend into pacakges we must somehow
232 // be able to pass this in
233 [sPanel setTreatsFilePackagesAsDirectories:NO];
234 [sPanel setCanSelectHiddenExtension:YES];
236 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
240 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
241 ModalFinishedCallback(sPanel, returnCode);
245 NSArray* types = GetTypesFromFilter( m_wildCard ) ;
246 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
247 [oPanel setTreatsFilePackagesAsDirectories:NO];
248 [oPanel setCanChooseDirectories:NO];
249 [oPanel setResolvesAliases:YES];
250 [oPanel setCanChooseFiles:YES];
251 [oPanel setMessage:cf.AsNSString()];
253 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
254 file:file.AsNSString() types:types];
256 ModalFinishedCallback(oPanel, returnCode);
262 return GetReturnCode();
265 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
267 int result = wxID_CANCEL;
268 if (HasFlag(wxFD_SAVE))
270 if (returnCode == NSOKButton )
272 NSSavePanel* sPanel = (NSSavePanel*)panel;
275 m_path = wxCFStringRef::AsString([sPanel filename]);
276 m_fileName = wxFileNameFromPath(m_path);
277 m_dir = wxPathOnly( m_path );
282 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
283 if (returnCode == NSOKButton )
287 NSArray* filenames = [oPanel filenames];
288 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
290 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
291 m_paths.Add( fnstr );
292 m_fileNames.Add( wxFileNameFromPath(fnstr) );
296 m_fileName = wxFileNameFromPath(fnstr);
297 m_dir = wxPathOnly( fnstr );
302 SetReturnCode(result);
304 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
305 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
308 #endif // wxUSE_FILEDLG