]>
Commit | Line | Data |
---|---|---|
0f9b48d1 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/cocoa/filedlg.mm | |
3 | // Purpose: wxFileDialog for wxCocoa | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 2004-10-02 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
0f9b48d1 SC |
8 | // Copyright: (c) Ryan Norton |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #if wxUSE_FILEDLG | |
24 | ||
25 | #include "wx/filedlg.h" | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/msgdlg.h" | |
29 | #include "wx/app.h" | |
fbede18c SC |
30 | #include "wx/sizer.h" |
31 | #include "wx/stattext.h" | |
32 | #include "wx/choice.h" | |
0f9b48d1 SC |
33 | #endif |
34 | ||
35 | #include "wx/filename.h" | |
b822bdc0 | 36 | #include "wx/tokenzr.h" |
0f9b48d1 SC |
37 | |
38 | #include "wx/osx/private.h" | |
13390af4 | 39 | #include "wx/sysopt.h" |
0f9b48d1 SC |
40 | |
41 | // ============================================================================ | |
42 | // implementation | |
43 | // ============================================================================ | |
44 | ||
4dd9fdf8 | 45 | // Open Items: |
4dd9fdf8 | 46 | // - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories) |
13390af4 SC |
47 | // - as setAllowedFileTypes is only functional for NSOpenPanel on 10.6+, on earlier systems, the file |
48 | // type choice will not be shown, but all possible file items will be shown, if a popup must be working | |
49 | // then the delegate method - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename will have to | |
50 | // be implemented | |
51 | ||
52 | @interface wxOpenPanelDelegate : NSObject wxOSX_10_6_AND_LATER(<NSOpenSavePanelDelegate>) | |
53 | { | |
54 | wxFileDialog* _dialog; | |
55 | } | |
56 | ||
57 | - (wxFileDialog*) fileDialog; | |
58 | - (void) setFileDialog:(wxFileDialog*) dialog; | |
59 | ||
60 | - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename; | |
61 | ||
62 | @end | |
63 | ||
64 | @implementation wxOpenPanelDelegate | |
65 | ||
66 | - (id) init | |
67 | { | |
68 | [super init]; | |
69 | _dialog = NULL; | |
70 | return self; | |
71 | } | |
72 | ||
73 | - (wxFileDialog*) fileDialog | |
74 | { | |
75 | return _dialog; | |
76 | } | |
77 | ||
78 | - (void) setFileDialog:(wxFileDialog*) dialog | |
79 | { | |
80 | _dialog = dialog; | |
81 | } | |
82 | ||
83 | - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename | |
84 | { | |
85 | BOOL showObject = YES; | |
86 | ||
87 | NSString* resolvedLink = [[NSFileManager defaultManager] pathContentOfSymbolicLinkAtPath:filename]; | |
88 | if ( resolvedLink != nil ) | |
89 | filename = resolvedLink; | |
90 | ||
91 | NSDictionary* fileAttribs = [[NSFileManager defaultManager] | |
92 | fileAttributesAtPath:filename traverseLink:YES]; | |
93 | if (fileAttribs) | |
94 | { | |
95 | // check for packages | |
96 | if ([NSFileTypeDirectory isEqualTo:[fileAttribs objectForKey:NSFileType]]) | |
97 | { | |
98 | if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename] == NO) | |
99 | showObject = YES; // it's a folder, OK to show | |
100 | else | |
101 | { | |
102 | // it's a packaged directory, apply check | |
103 | wxCFStringRef filecf([filename retain]); | |
104 | showObject = _dialog->CheckFile(filecf.AsString()); | |
105 | } | |
106 | } | |
107 | else | |
108 | { | |
109 | // the code above only solves links, not aliases, do this here: | |
110 | ||
111 | NSString* resolvedAlias = nil; | |
112 | ||
113 | CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, | |
114 | (CFStringRef)filename, | |
115 | kCFURLPOSIXPathStyle, | |
116 | NO); | |
117 | if (url != NULL) | |
118 | { | |
119 | FSRef fsRef; | |
120 | if (CFURLGetFSRef(url, &fsRef)) | |
121 | { | |
122 | Boolean targetIsFolder, wasAliased; | |
123 | OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased); | |
124 | ||
125 | if ((err == noErr) && wasAliased) | |
126 | { | |
127 | CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef); | |
128 | if (resolvedUrl != NULL) | |
129 | { | |
130 | resolvedAlias = (NSString*) CFURLCopyFileSystemPath(resolvedUrl, | |
131 | kCFURLPOSIXPathStyle); | |
132 | CFRelease(resolvedUrl); | |
133 | } | |
134 | } | |
135 | } | |
136 | CFRelease(url); | |
137 | } | |
138 | ||
139 | if (resolvedAlias != nil) | |
140 | { | |
141 | // recursive call | |
142 | [resolvedAlias autorelease]; | |
143 | showObject = [self panel:sender shouldShowFilename:resolvedAlias]; | |
144 | } | |
145 | else | |
146 | { | |
147 | wxCFStringRef filecf([filename retain]); | |
148 | showObject = _dialog->CheckFile(filecf.AsString()); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | return showObject; | |
154 | } | |
155 | ||
156 | @end | |
4dd9fdf8 | 157 | |
0f9b48d1 SC |
158 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) |
159 | ||
160 | wxFileDialog::wxFileDialog( | |
161 | wxWindow *parent, const wxString& message, | |
162 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
163 | long style, const wxPoint& pos, const wxSize& sz, const wxString& name) | |
164 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name) | |
165 | { | |
daa31651 | 166 | m_filterIndex = -1; |
0f9b48d1 SC |
167 | } |
168 | ||
61ad44c7 SC |
169 | bool wxFileDialog::SupportsExtraControl() const |
170 | { | |
2f30930a | 171 | return true; |
61ad44c7 SC |
172 | } |
173 | ||
13390af4 | 174 | NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions ) |
0f9b48d1 SC |
175 | { |
176 | NSMutableArray* types = nil; | |
13390af4 SC |
177 | extensions.Clear(); |
178 | ||
179 | wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ; | |
180 | while ( tokenizer.HasMoreTokens() ) | |
0f9b48d1 | 181 | { |
13390af4 SC |
182 | wxString extension = tokenizer.GetNextToken() ; |
183 | // Remove leading '*' | |
184 | if ( extension.length() && (extension.GetChar(0) == '*') ) | |
185 | extension = extension.Mid( 1 ); | |
bd365871 | 186 | |
13390af4 SC |
187 | // Remove leading '.' |
188 | if ( extension.length() && (extension.GetChar(0) == '.') ) | |
189 | extension = extension.Mid( 1 ); | |
0f9b48d1 | 190 | |
13390af4 SC |
191 | // Remove leading '*', this is for handling *.* |
192 | if ( extension.length() && (extension.GetChar(0) == '*') ) | |
193 | extension = extension.Mid( 1 ); | |
194 | ||
195 | if ( extension.IsEmpty() ) | |
0f9b48d1 | 196 | { |
13390af4 SC |
197 | extensions.Clear(); |
198 | [types release]; | |
199 | types = nil; | |
200 | return nil; | |
201 | } | |
0f9b48d1 | 202 | |
13390af4 SC |
203 | if ( types == nil ) |
204 | types = [[NSMutableArray alloc] init]; | |
205 | ||
206 | extensions.Add(extension.Lower()); | |
207 | wxCFStringRef cfext(extension); | |
208 | [types addObject: (NSString*)cfext.AsNSString() ]; | |
209 | #if 0 | |
210 | // add support for classic fileType / creator here | |
211 | wxUint32 fileType, creator; | |
212 | // extension -> mactypes | |
213 | #endif | |
214 | } | |
215 | [types autorelease]; | |
216 | return types; | |
217 | } | |
218 | ||
219 | NSArray* GetTypesFromFilter( const wxString& filter, wxArrayString& names, wxArrayString& extensiongroups ) | |
220 | { | |
221 | NSMutableArray* types = nil; | |
222 | bool allowAll = false; | |
223 | ||
224 | names.Clear(); | |
225 | extensiongroups.Clear(); | |
226 | ||
227 | if ( !filter.empty() ) | |
228 | { | |
229 | wxStringTokenizer tokenizer( filter, wxT("|") ); | |
230 | int numtokens = (int)tokenizer.CountTokens(); | |
231 | if(numtokens == 1) | |
232 | { | |
233 | // we allow for compatibility reason to have a single filter expression (like *.*) without | |
234 | // an explanatory text, in that case the first part is name and extension at the same time | |
235 | wxString extension = tokenizer.GetNextToken(); | |
236 | names.Add( extension ); | |
237 | extensiongroups.Add( extension ); | |
238 | } | |
239 | else | |
240 | { | |
241 | int numextensions = numtokens / 2; | |
242 | for(int i = 0; i < numextensions; i++) | |
0f9b48d1 | 243 | { |
13390af4 SC |
244 | wxString name = tokenizer.GetNextToken(); |
245 | wxString extension = tokenizer.GetNextToken(); | |
246 | names.Add( name ); | |
247 | extensiongroups.Add( extension ); | |
0f9b48d1 SC |
248 | } |
249 | } | |
0f9b48d1 | 250 | |
13390af4 SC |
251 | const size_t extCount = extensiongroups.GetCount(); |
252 | wxArrayString extensions; | |
0f9b48d1 SC |
253 | for ( size_t i = 0 ; i < extCount; i++ ) |
254 | { | |
13390af4 SC |
255 | NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions); |
256 | if ( exttypes != nil ) | |
0f9b48d1 | 257 | { |
13390af4 | 258 | if ( allowAll == false ) |
b822bdc0 | 259 | { |
13390af4 SC |
260 | if ( types == nil ) |
261 | types = [[NSMutableArray alloc] init]; | |
bd365871 | 262 | |
13390af4 SC |
263 | [types addObjectsFromArray:exttypes]; |
264 | } | |
265 | } | |
266 | else | |
267 | { | |
268 | allowAll = true; | |
269 | [types release]; | |
270 | types = nil; | |
b822bdc0 | 271 | } |
0f9b48d1 SC |
272 | } |
273 | } | |
13390af4 | 274 | [types autorelease]; |
0f9b48d1 SC |
275 | return types; |
276 | } | |
277 | ||
bfa92264 | 278 | void wxFileDialog::ShowWindowModal() |
0f9b48d1 | 279 | { |
bfa92264 KO |
280 | wxCFStringRef cf( m_message ); |
281 | wxCFStringRef dir( m_dir ); | |
282 | wxCFStringRef file( m_fileName ); | |
283 | ||
284 | wxNonOwnedWindow* parentWindow = NULL; | |
285 | ||
286 | m_modality = wxDIALOG_MODALITY_WINDOW_MODAL; | |
287 | ||
288 | if (GetParent()) | |
289 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
290 | ||
291 | wxASSERT_MSG(parentWindow, "Window modal display requires parent."); | |
13390af4 SC |
292 | |
293 | NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; | |
294 | if ( HasFlag(wxFD_SAVE) ) | |
bfa92264 KO |
295 | { |
296 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
2f30930a SC |
297 | |
298 | SetupExtraControls(sPanel); | |
299 | ||
bfa92264 KO |
300 | // makes things more convenient: |
301 | [sPanel setCanCreateDirectories:YES]; | |
302 | [sPanel setMessage:cf.AsNSString()]; | |
303 | // if we should be able to descend into pacakges we must somehow | |
304 | // be able to pass this in | |
305 | [sPanel setTreatsFilePackagesAsDirectories:NO]; | |
306 | [sPanel setCanSelectHiddenExtension:YES]; | |
5411e35f SC |
307 | [sPanel setAllowedFileTypes:types]; |
308 | [sPanel setAllowsOtherFileTypes:NO]; | |
bfa92264 KO |
309 | |
310 | NSWindow* nativeParent = parentWindow->GetWXWindow(); | |
311 | ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init]; | |
312 | [sheetDelegate setImplementation: this]; | |
313 | [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() | |
314 | modalForWindow: nativeParent modalDelegate: sheetDelegate | |
315 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) | |
316 | contextInfo: nil]; | |
317 | } | |
318 | else | |
319 | { | |
bfa92264 | 320 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
2f30930a SC |
321 | |
322 | SetupExtraControls(oPanel); | |
323 | ||
bfa92264 KO |
324 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
325 | [oPanel setCanChooseDirectories:NO]; | |
326 | [oPanel setResolvesAliases:YES]; | |
327 | [oPanel setCanChooseFiles:YES]; | |
328 | [oPanel setMessage:cf.AsNSString()]; | |
075386e6 SC |
329 | [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; |
330 | ||
bfa92264 KO |
331 | NSWindow* nativeParent = parentWindow->GetWXWindow(); |
332 | ModalDialogDelegate* sheetDelegate = [[ModalDialogDelegate alloc] init]; | |
333 | [sheetDelegate setImplementation: this]; | |
334 | [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() | |
335 | types: types modalForWindow: nativeParent | |
336 | modalDelegate: sheetDelegate | |
337 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) | |
338 | contextInfo: nil]; | |
339 | } | |
13390af4 SC |
340 | } |
341 | ||
342 | // Create a panel with the file type drop down list | |
343 | // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add | |
344 | // them to the panel as well | |
345 | // Returns the newly created wxPanel | |
346 | ||
347 | wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol) | |
348 | { | |
349 | wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize); | |
350 | wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL); | |
351 | extrapanel->SetSizer(verticalSizer); | |
352 | ||
353 | // the file type control | |
354 | { | |
355 | wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); | |
356 | verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); | |
357 | wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") ); | |
358 | horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
359 | m_filterChoice = new wxChoice(extrapanel, wxID_ANY); | |
360 | horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
361 | m_filterChoice->Append(m_filterNames); | |
362 | if( m_filterNames.GetCount() > 0) | |
363 | { | |
364 | if ( m_firstFileTypeFilter >= 0 ) | |
365 | m_filterChoice->SetSelection(m_firstFileTypeFilter); | |
366 | } | |
367 | m_filterChoice->Connect(wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler(wxFileDialog::OnFilterSelected), NULL, this); | |
368 | } | |
369 | ||
370 | if(extracontrol) | |
371 | { | |
372 | wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); | |
373 | verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); | |
374 | ||
375 | extracontrol->Reparent(extrapanel); | |
376 | horizontalSizer->Add(extracontrol); | |
377 | } | |
378 | ||
379 | verticalSizer->Layout(); | |
380 | verticalSizer->SetSizeHints(extrapanel); | |
381 | return extrapanel; | |
382 | } | |
383 | ||
384 | // An item has been selected in the file filter wxChoice: | |
385 | void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) ) | |
386 | { | |
387 | int index = m_filterChoice->GetSelection(); | |
388 | ||
389 | NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions); | |
390 | NSSavePanel* panel = (NSSavePanel*) GetWXWindow(); | |
391 | if ( m_delegate ) | |
392 | [panel validateVisibleColumns]; | |
393 | else | |
394 | [panel setAllowedFileTypes:types]; | |
395 | } | |
396 | ||
397 | bool wxFileDialog::CheckFile( const wxString& filename ) | |
398 | { | |
399 | if ( m_currentExtensions.GetCount() == 0 ) | |
400 | return true; | |
401 | ||
402 | wxString ext = filename.AfterLast('.').Lower(); | |
403 | ||
404 | for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i ) | |
405 | { | |
406 | if ( ext == m_currentExtensions[i] ) | |
407 | return true; | |
408 | } | |
409 | return false; | |
bfa92264 | 410 | } |
bd365871 | 411 | |
2f30930a | 412 | void wxFileDialog::SetupExtraControls(WXWindow nativeWindow) |
bfa92264 | 413 | { |
2f30930a SC |
414 | NSSavePanel* panel = (NSSavePanel*) nativeWindow; |
415 | ||
416 | wxNonOwnedWindow::Create( GetParent(), nativeWindow ); | |
13390af4 SC |
417 | wxWindow* extracontrol = NULL; |
418 | if ( HasExtraControlCreator() ) | |
2f30930a SC |
419 | { |
420 | CreateExtraControl(); | |
13390af4 SC |
421 | extracontrol = GetExtraControl(); |
422 | } | |
423 | ||
424 | NSView* accView = nil; | |
425 | m_delegate = nil; | |
426 | ||
427 | if ( m_useFileTypeFilter ) | |
428 | { | |
429 | m_filterPanel = CreateFilterPanel(extracontrol); | |
430 | accView = m_filterPanel->GetHandle(); | |
431 | if( HasFlag(wxFD_OPEN) ) | |
2f30930a | 432 | { |
13390af4 SC |
433 | if ( 1 /* UMAGetSystemVersion() < 0x1060 */ ) |
434 | { | |
435 | wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init]; | |
436 | [del setFileDialog:this]; | |
437 | [panel setDelegate:del]; | |
438 | m_delegate = del; | |
439 | } | |
2f30930a SC |
440 | } |
441 | } | |
13390af4 SC |
442 | else |
443 | { | |
444 | m_filterPanel = NULL; | |
445 | m_filterChoice = NULL; | |
446 | if ( extracontrol != nil ) | |
447 | accView = extracontrol->GetHandle(); | |
448 | } | |
449 | ||
450 | if ( accView != nil ) | |
451 | { | |
452 | [accView removeFromSuperview]; | |
453 | [panel setAccessoryView:accView]; | |
454 | } | |
455 | else | |
456 | { | |
457 | [panel setAccessoryView:nil]; | |
458 | } | |
2f30930a | 459 | } |
bd365871 | 460 | |
2f30930a SC |
461 | int wxFileDialog::ShowModal() |
462 | { | |
0f9b48d1 | 463 | wxCFStringRef cf( m_message ); |
bd365871 | 464 | |
1b447793 | 465 | wxCFStringRef dir( m_dir ); |
0f9b48d1 | 466 | wxCFStringRef file( m_fileName ); |
bd365871 | 467 | |
0f9b48d1 SC |
468 | m_path = wxEmptyString; |
469 | m_fileNames.Clear(); | |
8b558f12 | 470 | m_paths.Clear(); |
03647350 | 471 | |
724999ee KO |
472 | wxNonOwnedWindow* parentWindow = NULL; |
473 | int returnCode = -1; | |
03647350 VZ |
474 | |
475 | if (GetParent()) | |
724999ee KO |
476 | { |
477 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
478 | } | |
bd365871 | 479 | |
13390af4 SC |
480 | |
481 | NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; | |
482 | ||
483 | m_useFileTypeFilter = m_filterExtensions.GetCount() > 1; | |
484 | ||
485 | if( HasFlag(wxFD_OPEN) ) | |
486 | { | |
487 | if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) ) | |
488 | m_useFileTypeFilter = false; | |
489 | } | |
490 | ||
491 | m_firstFileTypeFilter = -1; | |
daa31651 DS |
492 | |
493 | if ( m_useFileTypeFilter | |
494 | && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() ) | |
495 | { | |
496 | m_firstFileTypeFilter = m_filterIndex; | |
497 | } | |
498 | else if ( m_useFileTypeFilter ) | |
13390af4 SC |
499 | { |
500 | types = nil; | |
501 | bool useDefault = true; | |
502 | for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i ) | |
503 | { | |
504 | types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions); | |
505 | if ( m_currentExtensions.GetCount() == 0 ) | |
506 | { | |
507 | useDefault = false; | |
508 | m_firstFileTypeFilter = i; | |
509 | break; | |
510 | } | |
511 | ||
512 | for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j ) | |
513 | { | |
514 | if ( m_fileName.EndsWith(m_currentExtensions[j]) ) | |
515 | { | |
516 | m_firstFileTypeFilter = i; | |
517 | useDefault = false; | |
518 | break; | |
519 | } | |
520 | } | |
521 | if ( !useDefault ) | |
522 | break; | |
523 | } | |
524 | if ( useDefault ) | |
525 | { | |
526 | types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions); | |
527 | m_firstFileTypeFilter = 0; | |
528 | } | |
529 | } | |
530 | ||
531 | if ( HasFlag(wxFD_SAVE) ) | |
0f9b48d1 SC |
532 | { |
533 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
2f30930a SC |
534 | |
535 | SetupExtraControls(sPanel); | |
536 | ||
0f9b48d1 SC |
537 | // makes things more convenient: |
538 | [sPanel setCanCreateDirectories:YES]; | |
539 | [sPanel setMessage:cf.AsNSString()]; | |
4dd9fdf8 SC |
540 | // if we should be able to descend into pacakges we must somehow |
541 | // be able to pass this in | |
0f9b48d1 | 542 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
4dd9fdf8 | 543 | [sPanel setCanSelectHiddenExtension:YES]; |
5411e35f SC |
544 | [sPanel setAllowedFileTypes:types]; |
545 | [sPanel setAllowsOtherFileTypes:NO]; | |
bd365871 | 546 | |
0f9b48d1 SC |
547 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
548 | { | |
549 | } | |
440e5cb2 | 550 | |
bfa92264 KO |
551 | returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ]; |
552 | ModalFinishedCallback(sPanel, returnCode); | |
0f9b48d1 SC |
553 | } |
554 | else | |
555 | { | |
0f9b48d1 | 556 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
2f30930a SC |
557 | |
558 | SetupExtraControls(oPanel); | |
559 | ||
0f9b48d1 SC |
560 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
561 | [oPanel setCanChooseDirectories:NO]; | |
562 | [oPanel setResolvesAliases:YES]; | |
563 | [oPanel setCanChooseFiles:YES]; | |
564 | [oPanel setMessage:cf.AsNSString()]; | |
075386e6 | 565 | [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; |
bd365871 | 566 | |
075386e6 SC |
567 | if ( UMAGetSystemVersion() < 0x1060 ) |
568 | { | |
569 | returnCode = [oPanel runModalForDirectory:dir.AsNSString() | |
13390af4 | 570 | file:file.AsNSString() types:(m_delegate == nil ? types : nil)]; |
075386e6 SC |
571 | } |
572 | else | |
573 | { | |
13390af4 | 574 | [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)]; |
075386e6 SC |
575 | [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() |
576 | isDirectory:YES]]; | |
577 | returnCode = [oPanel runModal]; | |
578 | } | |
bfa92264 KO |
579 | |
580 | ModalFinishedCallback(oPanel, returnCode); | |
bfa92264 KO |
581 | } |
582 | ||
583 | return GetReturnCode(); | |
584 | } | |
585 | ||
586 | void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode) | |
587 | { | |
588 | int result = wxID_CANCEL; | |
589 | if (HasFlag(wxFD_SAVE)) | |
590 | { | |
591 | if (returnCode == NSOKButton ) | |
592 | { | |
593 | NSSavePanel* sPanel = (NSSavePanel*)panel; | |
594 | result = wxID_OK; | |
595 | ||
596 | m_path = wxCFStringRef::AsString([sPanel filename]); | |
597 | m_fileName = wxFileNameFromPath(m_path); | |
598 | m_dir = wxPathOnly( m_path ); | |
0d1cd874 DS |
599 | if (m_filterChoice) |
600 | { | |
601 | m_filterIndex = m_filterChoice->GetSelection(); | |
602 | } | |
724999ee | 603 | } |
bfa92264 KO |
604 | } |
605 | else | |
606 | { | |
607 | NSOpenPanel* oPanel = (NSOpenPanel*)panel; | |
b2c47ad3 | 608 | if (returnCode == NSOKButton ) |
0f9b48d1 SC |
609 | { |
610 | panel = oPanel; | |
611 | result = wxID_OK; | |
612 | NSArray* filenames = [oPanel filenames]; | |
613 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) | |
614 | { | |
f66ecdc4 | 615 | wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]); |
0f9b48d1 SC |
616 | m_paths.Add( fnstr ); |
617 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); | |
618 | if ( i == 0 ) | |
619 | { | |
620 | m_path = fnstr; | |
621 | m_fileName = wxFileNameFromPath(fnstr); | |
622 | m_dir = wxPathOnly( fnstr ); | |
623 | } | |
624 | } | |
625 | } | |
13390af4 SC |
626 | if ( m_delegate ) |
627 | { | |
628 | [oPanel setDelegate:nil]; | |
629 | [m_delegate release]; | |
630 | m_delegate = nil; | |
631 | } | |
0f9b48d1 | 632 | } |
bfa92264 KO |
633 | SetReturnCode(result); |
634 | ||
635 | if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL) | |
636 | SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); | |
2f30930a | 637 | |
13390af4 | 638 | UnsubclassWin(); |
2f30930a | 639 | [(NSSavePanel*) panel setAccessoryView:nil]; |
0f9b48d1 SC |
640 | } |
641 | ||
0f9b48d1 | 642 | #endif // wxUSE_FILEDLG |