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