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 bool wxFileDialog::SupportsExtraControl() const
60 NSArray* CopyTypesFromFilter( const wxString filter )
62 NSMutableArray* types = nil;
63 if ( !filter.empty() )
66 wxArrayString extensions;
68 wxString filter2(filter) ;
73 for ( unsigned int i = 0; i < filter2.length() ; i++ )
75 if ( filter2.GetChar(i) == wxT('|') )
79 names.Add( current ) ;
83 extensions.Add( current ) ;
88 current = wxEmptyString ;
92 current += filter2.GetChar(i) ;
95 // we allow for compatibility reason to have a single filter expression (like *.*) without
96 // an explanatory text, in that case the first part is name and extension at the same time
98 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
99 if ( current.empty() )
100 extensions.Add( names[filterIndex] ) ;
102 extensions.Add( current ) ;
103 if ( filterIndex == 0 || isName )
104 names.Add( current ) ;
108 const size_t extCount = extensions.GetCount();
109 for ( size_t i = 0 ; i < extCount; i++ )
111 wxString extensiongroup = extensions[i];
112 wxStringTokenizer tokenizer( extensiongroup , wxT(";") ) ;
113 while ( tokenizer.HasMoreTokens() )
115 wxString extension = tokenizer.GetNextToken() ;
116 // Remove leading '*'
117 if (extension.length() && (extension.GetChar(0) == '*'))
118 extension = extension.Mid( 1 );
120 // Remove leading '.'
121 if (extension.length() && (extension.GetChar(0) == '.'))
122 extension = extension.Mid( 1 );
124 // Remove leading '*', this is for handling *.*
125 if (extension.length() && (extension.GetChar(0) == '*'))
126 extension = extension.Mid( 1 );
128 if ( extension.IsEmpty() )
136 types = [[NSMutableArray alloc] init];
138 wxCFStringRef cfext(extension);
139 [types addObject: (NSString*)cfext.AsNSString() ];
141 // add support for classic fileType / creator here
142 wxUint32 fileType, creator;
143 // extension -> mactypes
152 void wxFileDialog::ShowWindowModal()
154 wxCFStringRef cf( m_message );
155 wxCFStringRef dir( m_dir );
156 wxCFStringRef file( m_fileName );
158 wxNonOwnedWindow* parentWindow = NULL;
160 m_modality = wxDIALOG_MODALITY_WINDOW_MODAL;
163 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
165 wxASSERT_MSG(parentWindow, "Window modal display requires parent.");
167 if (HasFlag(wxFD_SAVE))
169 NSSavePanel* sPanel = [NSSavePanel savePanel];
171 SetupExtraControls(sPanel);
173 // makes things more convenient:
174 [sPanel setCanCreateDirectories:YES];
175 [sPanel setMessage:cf.AsNSString()];
176 // if we should be able to descend into pacakges we must somehow
177 // be able to pass this in
178 [sPanel setTreatsFilePackagesAsDirectories:NO];
179 [sPanel setCanSelectHiddenExtension:YES];
181 NSWindow* nativeParent = parentWindow->GetWXWindow();
182 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
183 [sheetDelegate setImplementation: this];
184 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
185 modalForWindow: nativeParent modalDelegate: sheetDelegate
186 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
191 NSArray* types = CopyTypesFromFilter( m_wildCard ) ;
192 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
194 SetupExtraControls(oPanel);
196 [oPanel setTreatsFilePackagesAsDirectories:NO];
197 [oPanel setCanChooseDirectories:NO];
198 [oPanel setResolvesAliases:YES];
199 [oPanel setCanChooseFiles:YES];
200 [oPanel setMessage:cf.AsNSString()];
202 NSWindow* nativeParent = parentWindow->GetWXWindow();
203 ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init];
204 [sheetDelegate setImplementation: this];
205 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
206 types: types modalForWindow: nativeParent
207 modalDelegate: sheetDelegate
208 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
215 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
217 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
219 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
221 if (HasExtraControlCreator())
223 CreateExtraControl();
224 wxWindow* control = GetExtraControl();
227 NSView* accView = control->GetHandle();
228 [accView removeFromSuperview];
229 [panel setAccessoryView:accView];
233 [panel setAccessoryView:nil];
238 int wxFileDialog::ShowModal()
240 wxCFStringRef cf( m_message );
242 wxCFStringRef dir( m_dir );
243 wxCFStringRef file( m_fileName );
245 m_path = wxEmptyString;
248 // since we don't support retrieving the matching filter
251 wxNonOwnedWindow* parentWindow = NULL;
256 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
259 if (HasFlag(wxFD_SAVE))
261 NSSavePanel* sPanel = [NSSavePanel savePanel];
263 SetupExtraControls(sPanel);
265 // makes things more convenient:
266 [sPanel setCanCreateDirectories:YES];
267 [sPanel setMessage:cf.AsNSString()];
268 // if we should be able to descend into pacakges we must somehow
269 // be able to pass this in
270 [sPanel setTreatsFilePackagesAsDirectories:NO];
271 [sPanel setCanSelectHiddenExtension:YES];
273 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
277 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
278 ModalFinishedCallback(sPanel, returnCode);
281 [sPanel setAccessoryView:nil];
285 NSArray* types = CopyTypesFromFilter( m_wildCard ) ;
286 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
288 SetupExtraControls(oPanel);
290 [oPanel setTreatsFilePackagesAsDirectories:NO];
291 [oPanel setCanChooseDirectories:NO];
292 [oPanel setResolvesAliases:YES];
293 [oPanel setCanChooseFiles:YES];
294 [oPanel setMessage:cf.AsNSString()];
296 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
297 file:file.AsNSString() types:types];
299 ModalFinishedCallback(oPanel, returnCode);
302 [oPanel setAccessoryView:nil];
308 return GetReturnCode();
311 void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode)
313 int result = wxID_CANCEL;
314 if (HasFlag(wxFD_SAVE))
316 if (returnCode == NSOKButton )
318 NSSavePanel* sPanel = (NSSavePanel*)panel;
321 m_path = wxCFStringRef::AsString([sPanel filename]);
322 m_fileName = wxFileNameFromPath(m_path);
323 m_dir = wxPathOnly( m_path );
328 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
329 if (returnCode == NSOKButton )
333 NSArray* filenames = [oPanel filenames];
334 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
336 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
337 m_paths.Add( fnstr );
338 m_fileNames.Add( wxFileNameFromPath(fnstr) );
342 m_fileName = wxFileNameFromPath(fnstr);
343 m_dir = wxPathOnly( fnstr );
348 SetReturnCode(result);
350 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
351 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
353 [(NSSavePanel*) panel setAccessoryView:nil];
356 #endif // wxUSE_FILEDLG