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