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