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