]>
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 | ||
cbbb1f54 | 384 | void wxFileDialog::DoOnFilterSelected(int index) |
13390af4 | 385 | { |
13390af4 SC |
386 | NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions); |
387 | NSSavePanel* panel = (NSSavePanel*) GetWXWindow(); | |
388 | if ( m_delegate ) | |
389 | [panel validateVisibleColumns]; | |
390 | else | |
391 | [panel setAllowedFileTypes:types]; | |
392 | } | |
393 | ||
cbbb1f54 DS |
394 | // An item has been selected in the file filter wxChoice: |
395 | void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) ) | |
396 | { | |
397 | DoOnFilterSelected( m_filterChoice->GetSelection() ); | |
398 | } | |
399 | ||
13390af4 SC |
400 | bool wxFileDialog::CheckFile( const wxString& filename ) |
401 | { | |
402 | if ( m_currentExtensions.GetCount() == 0 ) | |
403 | return true; | |
404 | ||
405 | wxString ext = filename.AfterLast('.').Lower(); | |
406 | ||
407 | for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i ) | |
408 | { | |
409 | if ( ext == m_currentExtensions[i] ) | |
410 | return true; | |
411 | } | |
412 | return false; | |
bfa92264 | 413 | } |
bd365871 | 414 | |
2f30930a | 415 | void wxFileDialog::SetupExtraControls(WXWindow nativeWindow) |
bfa92264 | 416 | { |
2f30930a SC |
417 | NSSavePanel* panel = (NSSavePanel*) nativeWindow; |
418 | ||
419 | wxNonOwnedWindow::Create( GetParent(), nativeWindow ); | |
13390af4 SC |
420 | wxWindow* extracontrol = NULL; |
421 | if ( HasExtraControlCreator() ) | |
2f30930a SC |
422 | { |
423 | CreateExtraControl(); | |
13390af4 SC |
424 | extracontrol = GetExtraControl(); |
425 | } | |
426 | ||
427 | NSView* accView = nil; | |
428 | m_delegate = nil; | |
429 | ||
430 | if ( m_useFileTypeFilter ) | |
431 | { | |
432 | m_filterPanel = CreateFilterPanel(extracontrol); | |
433 | accView = m_filterPanel->GetHandle(); | |
434 | if( HasFlag(wxFD_OPEN) ) | |
2f30930a | 435 | { |
7e3d8eab | 436 | if ( UMAGetSystemVersion() < 0x1060 ) |
13390af4 SC |
437 | { |
438 | wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init]; | |
439 | [del setFileDialog:this]; | |
440 | [panel setDelegate:del]; | |
441 | m_delegate = del; | |
442 | } | |
2f30930a SC |
443 | } |
444 | } | |
13390af4 SC |
445 | else |
446 | { | |
447 | m_filterPanel = NULL; | |
448 | m_filterChoice = NULL; | |
449 | if ( extracontrol != nil ) | |
450 | accView = extracontrol->GetHandle(); | |
451 | } | |
452 | ||
453 | if ( accView != nil ) | |
454 | { | |
455 | [accView removeFromSuperview]; | |
456 | [panel setAccessoryView:accView]; | |
457 | } | |
458 | else | |
459 | { | |
460 | [panel setAccessoryView:nil]; | |
461 | } | |
2f30930a | 462 | } |
bd365871 | 463 | |
2f30930a SC |
464 | int wxFileDialog::ShowModal() |
465 | { | |
7e3d8eab SC |
466 | wxMacAutoreleasePool autoreleasepool; |
467 | ||
0f9b48d1 | 468 | wxCFStringRef cf( m_message ); |
bd365871 | 469 | |
1b447793 | 470 | wxCFStringRef dir( m_dir ); |
0f9b48d1 | 471 | wxCFStringRef file( m_fileName ); |
bd365871 | 472 | |
0f9b48d1 SC |
473 | m_path = wxEmptyString; |
474 | m_fileNames.Clear(); | |
8b558f12 | 475 | m_paths.Clear(); |
03647350 | 476 | |
724999ee KO |
477 | wxNonOwnedWindow* parentWindow = NULL; |
478 | int returnCode = -1; | |
03647350 VZ |
479 | |
480 | if (GetParent()) | |
724999ee KO |
481 | { |
482 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
483 | } | |
bd365871 | 484 | |
13390af4 SC |
485 | |
486 | NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; | |
487 | ||
488 | m_useFileTypeFilter = m_filterExtensions.GetCount() > 1; | |
489 | ||
490 | if( HasFlag(wxFD_OPEN) ) | |
491 | { | |
492 | if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) ) | |
493 | m_useFileTypeFilter = false; | |
494 | } | |
495 | ||
496 | m_firstFileTypeFilter = -1; | |
daa31651 DS |
497 | |
498 | if ( m_useFileTypeFilter | |
499 | && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() ) | |
500 | { | |
501 | m_firstFileTypeFilter = m_filterIndex; | |
502 | } | |
503 | else if ( m_useFileTypeFilter ) | |
13390af4 SC |
504 | { |
505 | types = nil; | |
506 | bool useDefault = true; | |
507 | for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i ) | |
508 | { | |
509 | types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions); | |
510 | if ( m_currentExtensions.GetCount() == 0 ) | |
511 | { | |
512 | useDefault = false; | |
513 | m_firstFileTypeFilter = i; | |
514 | break; | |
515 | } | |
516 | ||
517 | for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j ) | |
518 | { | |
519 | if ( m_fileName.EndsWith(m_currentExtensions[j]) ) | |
520 | { | |
521 | m_firstFileTypeFilter = i; | |
522 | useDefault = false; | |
523 | break; | |
524 | } | |
525 | } | |
526 | if ( !useDefault ) | |
527 | break; | |
528 | } | |
529 | if ( useDefault ) | |
530 | { | |
531 | types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions); | |
532 | m_firstFileTypeFilter = 0; | |
533 | } | |
534 | } | |
535 | ||
536 | if ( HasFlag(wxFD_SAVE) ) | |
0f9b48d1 SC |
537 | { |
538 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
2f30930a SC |
539 | |
540 | SetupExtraControls(sPanel); | |
541 | ||
0f9b48d1 SC |
542 | // makes things more convenient: |
543 | [sPanel setCanCreateDirectories:YES]; | |
544 | [sPanel setMessage:cf.AsNSString()]; | |
4dd9fdf8 SC |
545 | // if we should be able to descend into pacakges we must somehow |
546 | // be able to pass this in | |
0f9b48d1 | 547 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
4dd9fdf8 | 548 | [sPanel setCanSelectHiddenExtension:YES]; |
5411e35f SC |
549 | [sPanel setAllowedFileTypes:types]; |
550 | [sPanel setAllowsOtherFileTypes:NO]; | |
bd365871 | 551 | |
0f9b48d1 SC |
552 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
553 | { | |
554 | } | |
440e5cb2 | 555 | |
cbbb1f54 DS |
556 | /* |
557 | Let the file dialog know what file type should be used initially. | |
558 | If this is not done then when setting the filter index | |
559 | programmatically to 1 the file will still have the extension | |
560 | of the first file type instead of the second one. E.g. when file | |
561 | types are foo and bar, a filename "myletter" with SetDialogIndex(1) | |
562 | would result in saving as myletter.foo, while we want myletter.bar. | |
563 | */ | |
564 | if(m_firstFileTypeFilter > 0) | |
565 | { | |
566 | DoOnFilterSelected(m_firstFileTypeFilter); | |
567 | } | |
568 | ||
7e3d8eab | 569 | returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ]; |
bfa92264 | 570 | ModalFinishedCallback(sPanel, returnCode); |
0f9b48d1 SC |
571 | } |
572 | else | |
573 | { | |
0f9b48d1 | 574 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
2f30930a SC |
575 | |
576 | SetupExtraControls(oPanel); | |
577 | ||
0f9b48d1 SC |
578 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
579 | [oPanel setCanChooseDirectories:NO]; | |
580 | [oPanel setResolvesAliases:YES]; | |
581 | [oPanel setCanChooseFiles:YES]; | |
582 | [oPanel setMessage:cf.AsNSString()]; | |
075386e6 | 583 | [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; |
bd365871 | 584 | |
075386e6 SC |
585 | if ( UMAGetSystemVersion() < 0x1060 ) |
586 | { | |
7e3d8eab | 587 | returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString() |
13390af4 | 588 | file:file.AsNSString() types:(m_delegate == nil ? types : nil)]; |
075386e6 SC |
589 | } |
590 | else | |
591 | { | |
13390af4 | 592 | [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)]; |
7e3d8eab SC |
593 | if ( !m_dir.IsEmpty() ) |
594 | [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() | |
075386e6 SC |
595 | isDirectory:YES]]; |
596 | returnCode = [oPanel runModal]; | |
597 | } | |
bfa92264 KO |
598 | |
599 | ModalFinishedCallback(oPanel, returnCode); | |
bfa92264 KO |
600 | } |
601 | ||
602 | return GetReturnCode(); | |
603 | } | |
604 | ||
605 | void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode) | |
606 | { | |
607 | int result = wxID_CANCEL; | |
608 | if (HasFlag(wxFD_SAVE)) | |
609 | { | |
610 | if (returnCode == NSOKButton ) | |
611 | { | |
612 | NSSavePanel* sPanel = (NSSavePanel*)panel; | |
613 | result = wxID_OK; | |
614 | ||
615 | m_path = wxCFStringRef::AsString([sPanel filename]); | |
616 | m_fileName = wxFileNameFromPath(m_path); | |
617 | m_dir = wxPathOnly( m_path ); | |
0d1cd874 DS |
618 | if (m_filterChoice) |
619 | { | |
620 | m_filterIndex = m_filterChoice->GetSelection(); | |
621 | } | |
724999ee | 622 | } |
bfa92264 KO |
623 | } |
624 | else | |
625 | { | |
626 | NSOpenPanel* oPanel = (NSOpenPanel*)panel; | |
b2c47ad3 | 627 | if (returnCode == NSOKButton ) |
0f9b48d1 SC |
628 | { |
629 | panel = oPanel; | |
630 | result = wxID_OK; | |
631 | NSArray* filenames = [oPanel filenames]; | |
632 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) | |
633 | { | |
f66ecdc4 | 634 | wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]); |
0f9b48d1 SC |
635 | m_paths.Add( fnstr ); |
636 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); | |
637 | if ( i == 0 ) | |
638 | { | |
639 | m_path = fnstr; | |
640 | m_fileName = wxFileNameFromPath(fnstr); | |
641 | m_dir = wxPathOnly( fnstr ); | |
642 | } | |
643 | } | |
644 | } | |
13390af4 SC |
645 | if ( m_delegate ) |
646 | { | |
647 | [oPanel setDelegate:nil]; | |
648 | [m_delegate release]; | |
649 | m_delegate = nil; | |
650 | } | |
0f9b48d1 | 651 | } |
bfa92264 KO |
652 | SetReturnCode(result); |
653 | ||
654 | if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL) | |
655 | SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); | |
2f30930a | 656 | |
13390af4 | 657 | UnsubclassWin(); |
2f30930a | 658 | [(NSSavePanel*) panel setAccessoryView:nil]; |
0f9b48d1 SC |
659 | } |
660 | ||
0f9b48d1 | 661 | #endif // wxUSE_FILEDLG |