]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/filedlg.mm
Add wxCALL_FOR_EACH() macro.
[wxWidgets.git] / src / osx / cocoa / filedlg.mm
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/filedlg.mm
3// Purpose: wxFileDialog for wxCocoa
4// Author: Ryan Norton
5// Modified by:
6// Created: 2004-10-02
7// RCS-ID: $Id$
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"
30 #include "wx/sizer.h"
31 #include "wx/stattext.h"
32 #include "wx/choice.h"
33#endif
34
35#include "wx/filename.h"
36#include "wx/tokenzr.h"
37#include "wx/evtloop.h"
38
39#include "wx/osx/private.h"
40#include "wx/sysopt.h"
41
42#include <mach-o/dyld.h>
43
44// ============================================================================
45// implementation
46// ============================================================================
47
48// Open Items:
49// - parameter support for descending into packages as directories (setTreatsFilePackagesAsDirectories)
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
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 }
74
75 // Notice that this still works correctly even if version is -1.
76 return version >= 0x40e2400 /* version of 10.6 AppKit */;
77}
78
79} // anonymous namespace
80
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{
97 self = [super init];
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
186
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{
195 m_filterIndex = -1;
196 m_sheetDelegate = [[ModalDialogDelegate alloc] init];
197 [(ModalDialogDelegate*)m_sheetDelegate setImplementation: this];
198}
199
200wxFileDialog::~wxFileDialog()
201{
202 [m_sheetDelegate release];
203}
204
205bool wxFileDialog::SupportsExtraControl() const
206{
207 return true;
208}
209
210NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions )
211{
212 NSMutableArray* types = nil;
213 extensions.Clear();
214
215 wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ;
216 while ( tokenizer.HasMoreTokens() )
217 {
218 wxString extension = tokenizer.GetNextToken() ;
219 // Remove leading '*'
220 if ( extension.length() && (extension.GetChar(0) == '*') )
221 extension = extension.Mid( 1 );
222
223 // Remove leading '.'
224 if ( extension.length() && (extension.GetChar(0) == '.') )
225 extension = extension.Mid( 1 );
226
227 // Remove leading '*', this is for handling *.*
228 if ( extension.length() && (extension.GetChar(0) == '*') )
229 extension = extension.Mid( 1 );
230
231 if ( extension.IsEmpty() )
232 {
233 extensions.Clear();
234 [types release];
235 types = nil;
236 return nil;
237 }
238
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++)
279 {
280 wxString name = tokenizer.GetNextToken();
281 wxString extension = tokenizer.GetNextToken();
282 names.Add( name );
283 extensiongroups.Add( extension );
284 }
285 }
286
287 const size_t extCount = extensiongroups.GetCount();
288 wxArrayString extensions;
289 for ( size_t i = 0 ; i < extCount; i++ )
290 {
291 NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions);
292 if ( exttypes != nil )
293 {
294 if ( allowAll == false )
295 {
296 if ( types == nil )
297 types = [[NSMutableArray alloc] init];
298
299 [types addObjectsFromArray:exttypes];
300 }
301 }
302 else
303 {
304 allowAll = true;
305 [types release];
306 types = nil;
307 }
308 }
309 }
310 [types autorelease];
311 return types;
312}
313
314void wxFileDialog::ShowWindowModal()
315{
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.");
328
329 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
330 if ( HasFlag(wxFD_SAVE) )
331 {
332 NSSavePanel* sPanel = [NSSavePanel savePanel];
333
334 SetupExtraControls(sPanel);
335
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];
343 [sPanel setAllowedFileTypes:types];
344 [sPanel setAllowsOtherFileTypes:NO];
345
346 NSWindow* nativeParent = parentWindow->GetWXWindow();
347 [sPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
348 modalForWindow: nativeParent modalDelegate: m_sheetDelegate
349 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
350 contextInfo: nil];
351 }
352 else
353 {
354 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
355
356 SetupExtraControls(oPanel);
357
358 [oPanel setTreatsFilePackagesAsDirectories:NO];
359 [oPanel setCanChooseDirectories:NO];
360 [oPanel setResolvesAliases:YES];
361 [oPanel setCanChooseFiles:YES];
362 [oPanel setMessage:cf.AsNSString()];
363 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
364
365 NSWindow* nativeParent = parentWindow->GetWXWindow();
366 [oPanel beginSheetForDirectory:dir.AsNSString() file:file.AsNSString()
367 types: types modalForWindow: nativeParent
368 modalDelegate: m_sheetDelegate
369 didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
370 contextInfo: nil];
371 }
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
416void wxFileDialog::DoOnFilterSelected(int index)
417{
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
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
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;
445}
446
447void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
448{
449 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
450
451 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
452 wxWindow* extracontrol = NULL;
453 if ( HasExtraControlCreator() )
454 {
455 CreateExtraControl();
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) )
467 {
468 if ( UMAGetSystemVersion() < 0x1060 || !HasAppKit_10_6() )
469 {
470 wxOpenPanelDelegate* del = [[wxOpenPanelDelegate alloc]init];
471 [del setFileDialog:this];
472 [panel setDelegate:del];
473 m_delegate = del;
474 }
475 }
476 }
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 }
494}
495
496int wxFileDialog::ShowModal()
497{
498 wxCFEventLoopPauseIdleEvents pause;
499
500 wxMacAutoreleasePool autoreleasepool;
501
502 wxCFStringRef cf( m_message );
503
504 wxCFStringRef dir( m_dir );
505 wxCFStringRef file( m_fileName );
506
507 m_path = wxEmptyString;
508 m_fileNames.Clear();
509 m_paths.Clear();
510
511 wxNonOwnedWindow* parentWindow = NULL;
512 int returnCode = -1;
513
514 if (GetParent())
515 {
516 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
517 }
518
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;
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 )
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) )
571 {
572 NSSavePanel* sPanel = [NSSavePanel savePanel];
573
574 SetupExtraControls(sPanel);
575
576 // makes things more convenient:
577 [sPanel setCanCreateDirectories:YES];
578 [sPanel setMessage:cf.AsNSString()];
579 // if we should be able to descend into pacakges we must somehow
580 // be able to pass this in
581 [sPanel setTreatsFilePackagesAsDirectories:NO];
582 [sPanel setCanSelectHiddenExtension:YES];
583 [sPanel setAllowedFileTypes:types];
584 [sPanel setAllowsOtherFileTypes:NO];
585
586 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
587 {
588 }
589
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
603 returnCode = [sPanel runModalForDirectory: m_dir.IsEmpty() ? nil : dir.AsNSString() file:file.AsNSString() ];
604 ModalFinishedCallback(sPanel, returnCode);
605 }
606 else
607 {
608 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
609
610 SetupExtraControls(oPanel);
611
612 [oPanel setTreatsFilePackagesAsDirectories:NO];
613 [oPanel setCanChooseDirectories:NO];
614 [oPanel setResolvesAliases:YES];
615 [oPanel setCanChooseFiles:YES];
616 [oPanel setMessage:cf.AsNSString()];
617 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
618
619#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
620 if ( UMAGetSystemVersion() >= 0x1060 && HasAppKit_10_6() )
621 {
622 [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
623 if ( !m_dir.IsEmpty() )
624 [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
625 isDirectory:YES]];
626 returnCode = [oPanel runModal];
627 }
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
635 ModalFinishedCallback(oPanel, returnCode);
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 );
654 if (m_filterChoice)
655 {
656 m_filterIndex = m_filterChoice->GetSelection();
657 }
658 }
659 }
660 else
661 {
662 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
663 if (returnCode == NSOKButton )
664 {
665 panel = oPanel;
666 result = wxID_OK;
667 NSArray* filenames = [oPanel filenames];
668 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
669 {
670 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
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 }
681 if ( m_delegate )
682 {
683 [oPanel setDelegate:nil];
684 [m_delegate release];
685 m_delegate = nil;
686 }
687 }
688 SetReturnCode(result);
689
690 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
691 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
692
693 UnsubclassWin();
694 [(NSSavePanel*) panel setAccessoryView:nil];
695}
696
697#endif // wxUSE_FILEDLG