]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/filedlg.mm
appeasing clang
[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: filedlg.mm 40007 2006-07-05 13:10:46Z 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"
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 }
167
168 bool wxFileDialog::SupportsExtraControl() const
169 {
170 return true;
171 }
172
173 NSArray* GetTypesFromExtension( const wxString extensiongroup, wxArrayString& extensions )
174 {
175 NSMutableArray* types = nil;
176 extensions.Clear();
177
178 wxStringTokenizer tokenizer( extensiongroup, wxT(";") ) ;
179 while ( tokenizer.HasMoreTokens() )
180 {
181 wxString extension = tokenizer.GetNextToken() ;
182 // Remove leading '*'
183 if ( extension.length() && (extension.GetChar(0) == '*') )
184 extension = extension.Mid( 1 );
185
186 // Remove leading '.'
187 if ( extension.length() && (extension.GetChar(0) == '.') )
188 extension = extension.Mid( 1 );
189
190 // Remove leading '*', this is for handling *.*
191 if ( extension.length() && (extension.GetChar(0) == '*') )
192 extension = extension.Mid( 1 );
193
194 if ( extension.IsEmpty() )
195 {
196 extensions.Clear();
197 [types release];
198 types = nil;
199 return nil;
200 }
201
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++)
242 {
243 wxString name = tokenizer.GetNextToken();
244 wxString extension = tokenizer.GetNextToken();
245 names.Add( name );
246 extensiongroups.Add( extension );
247 }
248 }
249
250 const size_t extCount = extensiongroups.GetCount();
251 wxArrayString extensions;
252 for ( size_t i = 0 ; i < extCount; i++ )
253 {
254 NSArray* exttypes = GetTypesFromExtension(extensiongroups[i], extensions);
255 if ( exttypes != nil )
256 {
257 if ( allowAll == false )
258 {
259 if ( types == nil )
260 types = [[NSMutableArray alloc] init];
261
262 [types addObjectsFromArray:exttypes];
263 }
264 }
265 else
266 {
267 allowAll = true;
268 [types release];
269 types = nil;
270 }
271 }
272 }
273 [types autorelease];
274 return types;
275 }
276
277 void wxFileDialog::ShowWindowModal()
278 {
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.");
291
292 NSArray* types = GetTypesFromFilter( m_wildCard, m_filterNames, m_filterExtensions ) ;
293 if ( HasFlag(wxFD_SAVE) )
294 {
295 NSSavePanel* sPanel = [NSSavePanel savePanel];
296
297 SetupExtraControls(sPanel);
298
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];
306 [sPanel setAllowedFileTypes:types];
307 [sPanel setAllowsOtherFileTypes:NO];
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 {
319 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
320
321 SetupExtraControls(oPanel);
322
323 [oPanel setTreatsFilePackagesAsDirectories:NO];
324 [oPanel setCanChooseDirectories:NO];
325 [oPanel setResolvesAliases:YES];
326 [oPanel setCanChooseFiles:YES];
327 [oPanel setMessage:cf.AsNSString()];
328 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
329
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 }
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;
409 }
410
411 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
412 {
413 NSSavePanel* panel = (NSSavePanel*) nativeWindow;
414
415 wxNonOwnedWindow::Create( GetParent(), nativeWindow );
416 wxWindow* extracontrol = NULL;
417 if ( HasExtraControlCreator() )
418 {
419 CreateExtraControl();
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) )
431 {
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 }
439 }
440 }
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 }
458 }
459
460 int wxFileDialog::ShowModal()
461 {
462 wxCFStringRef cf( m_message );
463
464 wxCFStringRef dir( m_dir );
465 wxCFStringRef file( m_fileName );
466
467 m_path = wxEmptyString;
468 m_fileNames.Clear();
469 m_paths.Clear();
470 // since we don't support retrieving the matching filter
471 m_filterIndex = -1;
472
473 wxNonOwnedWindow* parentWindow = NULL;
474 int returnCode = -1;
475
476 if (GetParent())
477 {
478 parentWindow = dynamic_cast<wxNonOwnedWindow*>(wxGetTopLevelParent(GetParent()));
479 }
480
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) )
528 {
529 NSSavePanel* sPanel = [NSSavePanel savePanel];
530
531 SetupExtraControls(sPanel);
532
533 // makes things more convenient:
534 [sPanel setCanCreateDirectories:YES];
535 [sPanel setMessage:cf.AsNSString()];
536 // if we should be able to descend into pacakges we must somehow
537 // be able to pass this in
538 [sPanel setTreatsFilePackagesAsDirectories:NO];
539 [sPanel setCanSelectHiddenExtension:YES];
540 [sPanel setAllowedFileTypes:types];
541 [sPanel setAllowsOtherFileTypes:NO];
542
543 if ( HasFlag(wxFD_OVERWRITE_PROMPT) )
544 {
545 }
546
547 returnCode = [sPanel runModalForDirectory:dir.AsNSString() file:file.AsNSString() ];
548 ModalFinishedCallback(sPanel, returnCode);
549 }
550 else
551 {
552 NSOpenPanel* oPanel = [NSOpenPanel openPanel];
553
554 SetupExtraControls(oPanel);
555
556 [oPanel setTreatsFilePackagesAsDirectories:NO];
557 [oPanel setCanChooseDirectories:NO];
558 [oPanel setResolvesAliases:YES];
559 [oPanel setCanChooseFiles:YES];
560 [oPanel setMessage:cf.AsNSString()];
561 [oPanel setAllowsMultipleSelection: (HasFlag(wxFD_MULTIPLE) ? YES : NO )];
562
563 if ( UMAGetSystemVersion() < 0x1060 )
564 {
565 returnCode = [oPanel runModalForDirectory:dir.AsNSString()
566 file:file.AsNSString() types:(m_delegate == nil ? types : nil)];
567 }
568 else
569 {
570 [oPanel setAllowedFileTypes: (m_delegate == nil ? types : nil)];
571 [oPanel setDirectoryURL:[NSURL fileURLWithPath:dir.AsNSString()
572 isDirectory:YES]];
573 returnCode = [oPanel runModal];
574 }
575
576 ModalFinishedCallback(oPanel, returnCode);
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 );
595 }
596 }
597 else
598 {
599 NSOpenPanel* oPanel = (NSOpenPanel*)panel;
600 if (returnCode == NSOKButton )
601 {
602 panel = oPanel;
603 result = wxID_OK;
604 NSArray* filenames = [oPanel filenames];
605 for ( size_t i = 0 ; i < [filenames count] ; ++ i )
606 {
607 wxString fnstr = wxCFStringRef::AsString([filenames objectAtIndex:i]);
608 m_paths.Add( fnstr );
609 m_fileNames.Add( wxFileNameFromPath(fnstr) );
610 if ( i == 0 )
611 {
612 m_path = fnstr;
613 m_fileName = wxFileNameFromPath(fnstr);
614 m_dir = wxPathOnly( fnstr );
615 }
616 }
617 }
618 if ( m_delegate )
619 {
620 [oPanel setDelegate:nil];
621 [m_delegate release];
622 m_delegate = nil;
623 }
624 }
625 SetReturnCode(result);
626
627 if (GetModality() == wxDIALOG_MODALITY_WINDOW_MODAL)
628 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED );
629
630 UnsubclassWin();
631 [(NSSavePanel*) panel setAccessoryView:nil];
632 }
633
634 #endif // wxUSE_FILEDLG