]>
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 | } | |
73 | if (version == -1) | |
74 | { | |
75 | return false; | |
76 | } | |
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 | ||
190 | wxFileDialog::wxFileDialog( | |
191 | wxWindow *parent, const wxString& message, | |
192 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
193 | long style, const wxPoint& pos, const wxSize& sz, const wxString& name) | |
194 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name) | |
195 | { | |
daa31651 | 196 | m_filterIndex = -1; |
4d3e2dc9 SC |
197 | m_sheetDelegate = [[ModalDialogDelegate alloc] init]; |
198 | [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this]; | |
199 | } | |
200 | ||
201 | wxFileDialog::~wxFileDialog() | |
202 | { | |
203 | [m_sheetDelegate release]; | |
0f9b48d1 SC |
204 | } |
205 | ||
61ad44c7 SC |
206 | bool wxFileDialog::SupportsExtraControl() const |
207 | { | |
2f30930a | 208 | return true; |
61ad44c7 SC |
209 | } |
210 | ||
13390af4 | 211 | NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions ) |
0f9b48d1 SC |
212 | { |
213 | NSMutableArray* types = nil; | |
13390af4 SC |
214 | extensions.Clear(); |
215 | ||
216 | wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ; | |
217 | while ( tokenizer.HasMoreTokens() ) | |
0f9b48d1 | 218 | { |
13390af4 SC |
219 | wxString extension = tokenizer.GetNextToken() ; |
220 | // Remove leading '*' | |
221 | if ( extension.length() && (extension.GetChar(0) == '*') ) | |
222 | extension = extension.Mid( 1 ); | |
bd365871 | 223 | |
13390af4 SC |
224 | // Remove leading '.' |
225 | if ( extension.length() && (extension.GetChar(0) == '.') ) | |
226 | extension = extension.Mid( 1 ); | |
0f9b48d1 | 227 | |
13390af4 SC |
228 | // Remove leading '*', this is for handling *.* |
229 | if ( extension.length() && (extension.GetChar(0) == '*') ) | |
230 | extension = extension.Mid( 1 ); | |
231 | ||
232 | if ( extension.IsEmpty() ) | |
0f9b48d1 | 233 | { |
13390af4 SC |
234 | extensions.Clear(); |
235 | [types release]; | |
236 | types = nil; | |
237 | return nil; | |
238 | } | |
0f9b48d1 | 239 | |
13390af4 SC |
240 | if ( types == nil ) |
241 | types = [[NSMutableArray alloc] init]; | |
242 | ||
243 | extensions.Add(extension.Lower()); | |
244 | wxCFStringRef cfext(extension); | |
245 | [types addObject: (NSString*)cfext.AsNSString() ]; | |
246 | #if 0 | |
247 | // add support for classic fileType / creator here | |
248 | wxUint32 fileType, creator; | |
249 | // extension -> mactypes | |
250 | #endif | |
251 | } | |
252 | [types autorelease]; | |
253 | return types; | |
254 | } | |
255 | ||
256 | NSArray* GetTypesFromFilter( const wxString& filter, wxArrayString& names, wxArrayString& extensiongroups ) | |
257 | { | |
258 | NSMutableArray* types = nil; | |
259 | bool allowAll = false; | |
260 | ||
261 | names.Clear(); | |
262 | extensiongroups.Clear(); | |
263 | ||
264 | if ( !filter.empty() ) | |
265 | { | |
266 | wxStringTokenizer tokenizer( filter, wxT("|") ); | |
267 | int numtokens = (int)tokenizer.CountTokens(); | |
268 | if(numtokens == 1) | |
269 | { | |
270 | // we allow for compatibility reason to have a single filter expression (like *.*) without | |
271 | // an explanatory text, in that case the first part is name and extension at the same time | |
272 | wxString extension = tokenizer.GetNextToken(); | |
273 | names.Add( extension ); | |
274 | extensiongroups.Add( extension ); | |
275 | } | |
276 | else | |
277 | { | |
278 | int numextensions = numtokens / 2; | |
279 | for(int i = 0; i < numextensions; i++) | |
0f9b48d1 | 280 | { |
13390af4 SC |
281 | wxString name = tokenizer.GetNextToken(); |
282 | wxString extension = tokenizer.GetNextToken(); | |
283 | names.Add( name ); | |
284 | extensiongroups.Add( extension ); | |
0f9b48d1 SC |
285 | } |
286 | } | |
0f9b48d1 | 287 | |
13390af4 SC |
288 | const size_t extCount = extensiongroups.GetCount(); |
289 | wxArrayString extensions; | |
0f9b48d1 SC |
290 | for ( size_t i = 0 ; i < extCount; i++ ) |
291 | { | |
13390af4 SC |
292 | NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions); |
293 | if ( exttypes != nil ) | |
0f9b48d1 | 294 | { |
13390af4 | 295 | if ( allowAll == false ) |
b822bdc0 | 296 | { |
13390af4 SC |
297 | if ( types == nil ) |
298 | types = [[NSMutableArray alloc] init]; | |
bd365871 | 299 | |
13390af4 SC |
300 | [types addObjectsFromArray:exttypes]; |
301 | } | |
302 | } | |
303 | else | |
304 | { | |
305 | allowAll = true; | |
306 | [types release]; | |
307 | types = nil; | |
b822bdc0 | 308 | } |
0f9b48d1 SC |
309 | } |
310 | } | |
13390af4 | 311 | [types autorelease]; |
0f9b48d1 SC |
312 | return types; |
313 | } | |
314 | ||
bfa92264 | 315 | void wxFileDialog::ShowWindowModal() |
0f9b48d1 | 316 | { |
bfa92264 KO |
317 | wxCFStringRef cf( m_message ); |
318 | wxCFStringRef dir( m_dir ); | |
319 | wxCFStringRef file( m_fileName ); | |
320 | ||
321 | wxNonOwnedWindow* parentWindow = NULL; | |
322 | ||
323 | m_modality = wxDIALOG_MODALITY_WINDOW_MODAL; | |
324 | ||
325 | if (GetParent()) | |
326 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
327 | ||
328 | wxASSERT_MSG(parentWindow, "Window modal display requires parent."); | |
13390af4 SC |
329 | |
330 | NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; | |
331 | if ( HasFlag(wxFD_SAVE) ) | |
bfa92264 KO |
332 | { |
333 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
2f30930a SC |
334 | |
335 | SetupExtraControls(sPanel); | |
336 | ||
bfa92264 KO |
337 | // makes things more convenient: |
338 | [sPanel setCanCreateDirectories:YES]; | |
339 | [sPanel setMessage:cf.AsNSString()]; | |
340 | // if we should be able to descend into pacakges we must somehow | |
341 | // be able to pass this in | |
342 | [sPanel setTreatsFilePackagesAsDirectories:NO]; | |
343 | [sPanel setCanSelectHiddenExtension:YES]; | |
5411e35f SC |
344 | [sPanel setAllowedFileTypes:types]; |
345 | [sPanel setAllowsOtherFileTypes:NO]; | |
bfa92264 KO |
346 | |
347 | NSWindow* nativeParent = parentWindow->GetWXWindow(); | |
bfa92264 | 348 | [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() |
4d3e2dc9 | 349 | modalForWindow: nativeParent modalDelegate: m_sheetDelegate |
bfa92264 KO |
350 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) |
351 | contextInfo: nil]; | |
352 | } | |
353 | else | |
354 | { | |
bfa92264 | 355 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
2f30930a SC |
356 | |
357 | SetupExtraControls(oPanel); | |
358 | ||
bfa92264 KO |
359 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
360 | [oPanel setCanChooseDirectories:NO]; | |
361 | [oPanel setResolvesAliases:YES]; | |
362 | [oPanel setCanChooseFiles:YES]; | |
363 | [oPanel setMessage:cf.AsNSString()]; | |
075386e6 SC |
364 | [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; |
365 | ||
bfa92264 | 366 | NSWindow* nativeParent = parentWindow->GetWXWindow(); |
bfa92264 KO |
367 | [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString() |
368 | types: types modalForWindow: nativeParent | |
4d3e2dc9 | 369 | modalDelegate: m_sheetDelegate |
bfa92264 KO |
370 | didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) |
371 | contextInfo: nil]; | |
372 | } | |
13390af4 SC |
373 | } |
374 | ||
375 | // Create a panel with the file type drop down list | |
376 | // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add | |
377 | // them to the panel as well | |
378 | // Returns the newly created wxPanel | |
379 | ||
380 | wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol) | |
381 | { | |
382 | wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize); | |
383 | wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL); | |
384 | extrapanel->SetSizer(verticalSizer); | |
385 | ||
386 | // the file type control | |
387 | { | |
388 | wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); | |
389 | verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); | |
390 | wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") ); | |
391 | horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
392 | m_filterChoice = new wxChoice(extrapanel, wxID_ANY); | |
393 | horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5); | |
394 | m_filterChoice->Append(m_filterNames); | |
395 | if( m_filterNames.GetCount() > 0) | |
396 | { | |
397 | if ( m_firstFileTypeFilter >= 0 ) | |
398 | m_filterChoice->SetSelection(m_firstFileTypeFilter); | |
399 | } | |
400 | m_filterChoice->Connect(wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler(wxFileDialog::OnFilterSelected), NULL, this); | |
401 | } | |
402 | ||
403 | if(extracontrol) | |
404 | { | |
405 | wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); | |
406 | verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); | |
407 | ||
408 | extracontrol->Reparent(extrapanel); | |
409 | horizontalSizer->Add(extracontrol); | |
410 | } | |
411 | ||
412 | verticalSizer->Layout(); | |
413 | verticalSizer->SetSizeHints(extrapanel); | |
414 | return extrapanel; | |
415 | } | |
416 | ||
cbbb1f54 | 417 | void wxFileDialog::DoOnFilterSelected(int index) |
13390af4 | 418 | { |
13390af4 SC |
419 | NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions); |
420 | NSSavePanel* panel = (NSSavePanel*) GetWXWindow(); | |
421 | if ( m_delegate ) | |
422 | [panel validateVisibleColumns]; | |
423 | else | |
424 | [panel setAllowedFileTypes:types]; | |
425 | } | |
426 | ||
cbbb1f54 DS |
427 | // An item has been selected in the file filter wxChoice: |
428 | void wxFileDialog::OnFilterSelected( wxCommandEvent &WXUNUSED(event) ) | |
429 | { | |
430 | DoOnFilterSelected( m_filterChoice->GetSelection() ); | |
431 | } | |
432 | ||
13390af4 SC |
433 | bool wxFileDialog::CheckFile( const wxString& filename ) |
434 | { | |
435 | if ( m_currentExtensions.GetCount() == 0 ) | |
436 | return true; | |
437 | ||
438 | wxString ext = filename.AfterLast('.').Lower(); | |
439 | ||
440 | for ( size_t i = 0; i < m_currentExtensions.GetCount(); ++i ) | |
441 | { | |
442 | if ( ext == m_currentExtensions[i] ) | |
443 | return true; | |
444 | } | |
445 | return false; | |
bfa92264 | 446 | } |
bd365871 | 447 | |
2f30930a | 448 | void wxFileDialog::SetupExtraControls(WXWindow nativeWindow) |
bfa92264 | 449 | { |
2f30930a SC |
450 | NSSavePanel* panel = (NSSavePanel*) nativeWindow; |
451 | ||
452 | wxNonOwnedWindow::Create( GetParent(), nativeWindow ); | |
13390af4 SC |
453 | wxWindow* extracontrol = NULL; |
454 | if ( HasExtraControlCreator() ) | |
2f30930a SC |
455 | { |
456 | CreateExtraControl(); | |
13390af4 SC |
457 | extracontrol = GetExtraControl(); |
458 | } | |
459 | ||
460 | NSView* accView = nil; | |
461 | m_delegate = nil; | |
462 | ||
463 | if ( m_useFileTypeFilter ) | |
464 | { | |
465 | m_filterPanel = CreateFilterPanel(extracontrol); | |
466 | accView = m_filterPanel->GetHandle(); | |
467 | if( HasFlag(wxFD_OPEN) ) | |
2f30930a | 468 | { |
632e5093 | 469 | if ( UMAGetSystemVersion() < 0x1060 || !HasAppKit_10_6() ) |
13390af4 SC |
470 | { |
471 | wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init]; | |
472 | [del setFileDialog:this]; | |
473 | [panel setDelegate:del]; | |
474 | m_delegate = del; | |
475 | } | |
2f30930a SC |
476 | } |
477 | } | |
13390af4 SC |
478 | else |
479 | { | |
480 | m_filterPanel = NULL; | |
481 | m_filterChoice = NULL; | |
482 | if ( extracontrol != nil ) | |
483 | accView = extracontrol->GetHandle(); | |
484 | } | |
485 | ||
486 | if ( accView != nil ) | |
487 | { | |
488 | [accView removeFromSuperview]; | |
489 | [panel setAccessoryView:accView]; | |
490 | } | |
491 | else | |
492 | { | |
493 | [panel setAccessoryView:nil]; | |
494 | } | |
2f30930a | 495 | } |
bd365871 | 496 | |
2f30930a SC |
497 | int wxFileDialog::ShowModal() |
498 | { | |
7e3d8eab SC |
499 | wxMacAutoreleasePool autoreleasepool; |
500 | ||
0f9b48d1 | 501 | wxCFStringRef cf( m_message ); |
bd365871 | 502 | |
1b447793 | 503 | wxCFStringRef dir( m_dir ); |
0f9b48d1 | 504 | wxCFStringRef file( m_fileName ); |
bd365871 | 505 | |
0f9b48d1 SC |
506 | m_path = wxEmptyString; |
507 | m_fileNames.Clear(); | |
8b558f12 | 508 | m_paths.Clear(); |
03647350 | 509 | |
724999ee KO |
510 | wxNonOwnedWindow* parentWindow = NULL; |
511 | int returnCode = -1; | |
03647350 VZ |
512 | |
513 | if (GetParent()) | |
724999ee KO |
514 | { |
515 | parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent())); | |
516 | } | |
bd365871 | 517 | |
13390af4 SC |
518 | |
519 | NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ; | |
520 | ||
521 | m_useFileTypeFilter = m_filterExtensions.GetCount() > 1; | |
522 | ||
523 | if( HasFlag(wxFD_OPEN) ) | |
524 | { | |
525 | if ( !(wxSystemOptions::HasOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) && (wxSystemOptions::GetOptionInt( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES ) == 1)) ) | |
526 | m_useFileTypeFilter = false; | |
527 | } | |
528 | ||
529 | m_firstFileTypeFilter = -1; | |
daa31651 DS |
530 | |
531 | if ( m_useFileTypeFilter | |
532 | && m_filterIndex >= 0 && m_filterIndex < m_filterExtensions.GetCount() ) | |
533 | { | |
534 | m_firstFileTypeFilter = m_filterIndex; | |
535 | } | |
536 | else if ( m_useFileTypeFilter ) | |
13390af4 SC |
537 | { |
538 | types = nil; | |
539 | bool useDefault = true; | |
540 | for ( size_t i = 0; i < m_filterExtensions.GetCount(); ++i ) | |
541 | { | |
542 | types = GetTypesFromExtension(m_filterExtensions[i], m_currentExtensions); | |
543 | if ( m_currentExtensions.GetCount() == 0 ) | |
544 | { | |
545 | useDefault = false; | |
546 | m_firstFileTypeFilter = i; | |
547 | break; | |
548 | } | |
549 | ||
550 | for ( size_t j = 0; j < m_currentExtensions.GetCount(); ++j ) | |
551 | { | |
552 | if ( m_fileName.EndsWith(m_currentExtensions[j]) ) | |
553 | { | |
554 | m_firstFileTypeFilter = i; | |
555 | useDefault = false; | |
556 | break; | |
557 | } | |
558 | } | |
559 | if ( !useDefault ) | |
560 | break; | |
561 | } | |
562 | if ( useDefault ) | |
563 | { | |
564 | types = GetTypesFromExtension(m_filterExtensions[0], m_currentExtensions); | |
565 | m_firstFileTypeFilter = 0; | |
566 | } | |
567 | } | |
568 | ||
569 | if ( HasFlag(wxFD_SAVE) ) | |
0f9b48d1 SC |
570 | { |
571 | NSSavePanel* sPanel = [NSSavePanel savePanel]; | |
2f30930a SC |
572 | |
573 | SetupExtraControls(sPanel); | |
574 | ||
0f9b48d1 SC |
575 | // makes things more convenient: |
576 | [sPanel setCanCreateDirectories:YES]; | |
577 | [sPanel setMessage:cf.AsNSString()]; | |
4dd9fdf8 SC |
578 | // if we should be able to descend into pacakges we must somehow |
579 | // be able to pass this in | |
0f9b48d1 | 580 | [sPanel setTreatsFilePackagesAsDirectories:NO]; |
4dd9fdf8 | 581 | [sPanel setCanSelectHiddenExtension:YES]; |
5411e35f SC |
582 | [sPanel setAllowedFileTypes:types]; |
583 | [sPanel setAllowsOtherFileTypes:NO]; | |
bd365871 | 584 | |
0f9b48d1 SC |
585 | if ( HasFlag(wxFD_OVERWRITE_PROMPT) ) |
586 | { | |
587 | } | |
440e5cb2 | 588 | |
cbbb1f54 DS |
589 | /* |
590 | Let the file dialog know what file type should be used initially. | |
591 | If this is not done then when setting the filter index | |
592 | programmatically to 1 the file will still have the extension | |
593 | of the first file type instead of the second one. E.g. when file | |
594 | types are foo and bar, a filename "myletter" with SetDialogIndex(1) | |
595 | would result in saving as myletter.foo, while we want myletter.bar. | |
596 | */ | |
597 | if(m_firstFileTypeFilter > 0) | |
598 | { | |
599 | DoOnFilterSelected(m_firstFileTypeFilter); | |
600 | } | |
601 | ||
7e3d8eab | 602 | returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ]; |
bfa92264 | 603 | ModalFinishedCallback(sPanel, returnCode); |
0f9b48d1 SC |
604 | } |
605 | else | |
606 | { | |
0f9b48d1 | 607 | NSOpenPanel* oPanel = [NSOpenPanel openPanel]; |
2f30930a SC |
608 | |
609 | SetupExtraControls(oPanel); | |
610 | ||
0f9b48d1 SC |
611 | [oPanel setTreatsFilePackagesAsDirectories:NO]; |
612 | [oPanel setCanChooseDirectories:NO]; | |
613 | [oPanel setResolvesAliases:YES]; | |
614 | [oPanel setCanChooseFiles:YES]; | |
615 | [oPanel setMessage:cf.AsNSString()]; | |
075386e6 | 616 | [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )]; |
bd365871 | 617 | |
95cb8819 | 618 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
632e5093 | 619 | if ( UMAGetSystemVersion() >= 0x1060 && HasAppKit_10_6() ) |
075386e6 | 620 | { |
13390af4 | 621 | [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)]; |
7e3d8eab SC |
622 | if ( !m_dir.IsEmpty() ) |
623 | [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString() | |
95cb8819 | 624 | isDirectory:YES]]; |
075386e6 SC |
625 | returnCode = [oPanel runModal]; |
626 | } | |
95cb8819 SC |
627 | else |
628 | #endif | |
629 | { | |
630 | returnCode = [oPanel runModalForDirectory:m_dir.IsEmpty() ? nil : dir.AsNSString() | |
631 | file:file.AsNSString() types:(m_delegate == nil ? types : nil)]; | |
632 | } | |
633 | ||
bfa92264 | 634 | ModalFinishedCallback(oPanel, returnCode); |
bfa92264 KO |
635 | } |
636 | ||
637 | return GetReturnCode(); | |
638 | } | |
639 | ||
640 | void wxFileDialog::ModalFinishedCallback(void* panel, int returnCode) | |
641 | { | |
642 | int result = wxID_CANCEL; | |
643 | if (HasFlag(wxFD_SAVE)) | |
644 | { | |
645 | if (returnCode == NSOKButton ) | |
646 | { | |
647 | NSSavePanel* sPanel = (NSSavePanel*)panel; | |
648 | result = wxID_OK; | |
649 | ||
650 | m_path = wxCFStringRef::AsString([sPanel filename]); | |
651 | m_fileName = wxFileNameFromPath(m_path); | |
652 | m_dir = wxPathOnly( m_path ); | |
0d1cd874 DS |
653 | if (m_filterChoice) |
654 | { | |
655 | m_filterIndex = m_filterChoice->GetSelection(); | |
656 | } | |
724999ee | 657 | } |
bfa92264 KO |
658 | } |
659 | else | |
660 | { | |
661 | NSOpenPanel* oPanel = (NSOpenPanel*)panel; | |
b2c47ad3 | 662 | if (returnCode == NSOKButton ) |
0f9b48d1 SC |
663 | { |
664 | panel = oPanel; | |
665 | result = wxID_OK; | |
666 | NSArray* filenames = [oPanel filenames]; | |
667 | for ( size_t i = 0 ; i < [filenames count] ; ++ i ) | |
668 | { | |
f66ecdc4 | 669 | wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]); |
0f9b48d1 SC |
670 | m_paths.Add( fnstr ); |
671 | m_fileNames.Add( wxFileNameFromPath(fnstr) ); | |
672 | if ( i == 0 ) | |
673 | { | |
674 | m_path = fnstr; | |
675 | m_fileName = wxFileNameFromPath(fnstr); | |
676 | m_dir = wxPathOnly( fnstr ); | |
677 | } | |
678 | } | |
679 | } | |
13390af4 SC |
680 | if ( m_delegate ) |
681 | { | |
682 | [oPanel setDelegate:nil]; | |
683 | [m_delegate release]; | |
684 | m_delegate = nil; | |
685 | } | |
0f9b48d1 | 686 | } |
bfa92264 KO |
687 | SetReturnCode(result); |
688 | ||
689 | if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL) | |
690 | SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); | |
2f30930a | 691 | |
13390af4 | 692 | UnsubclassWin(); |
2f30930a | 693 | [(NSSavePanel*) panel setAccessoryView:nil]; |
0f9b48d1 SC |
694 | } |
695 | ||
0f9b48d1 | 696 | #endif // wxUSE_FILEDLG |