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