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