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