]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/filedlg.cpp
6e0b83dc2551cc2029e3e9030046c3c8d060c215
[wxWidgets.git] / src / osx / carbon / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_FILEDLG
15
16 #include "wx/filedlg.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dialog.h"
23 #endif
24
25 #include "wx/tokenzr.h"
26 #include "wx/filename.h"
27
28 #include "wx/osx/private.h"
29 #include "wx/modalhook.h"
30
31 #ifndef __DARWIN__
32 #include <Navigation.h>
33 #include "PLStringFuncs.h"
34 #endif
35
36 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
37
38 // the data we need to pass to our standard file hook routine
39 // includes a pointer to the dialog, a pointer to the standard
40 // file reply record (so we can inspect the current selection)
41 // and a copy of the "previous" file spec of the reply record
42 // so we can see if the selection has changed
43
44 class OpenUserDataRec
45 {
46 public:
47 OpenUserDataRec( wxFileDialog* dialog );
48
49 bool FilterCallback( AEDesc *theItem, void *info, NavFilterModes filterMode );
50 void EventProc( NavEventCallbackMessage inSelector, NavCBRecPtr ioParams );
51
52 int GetCurrentFilter() const {return m_currentfilter;}
53 CFArrayRef GetMenuItems() const { return m_menuitems;}
54
55
56 private:
57 void EventProcCBEvent( NavCBRecPtr ioParams );
58 void EventProcCBEventMouseDown( NavCBRecPtr ioParams);
59 void EventProcCBStart( NavCBRecPtr ioParams );
60 void EventProcCBPopupMenuSelect( NavCBRecPtr ioParams );
61 void EventProcCBCustomize( NavCBRecPtr ioParams );
62 void EventProcCBAdjustRect( NavCBRecPtr ioParams );
63 bool CheckFile( const wxString &filename , OSType type);
64 void MakeUserDataRec( const wxString& filter);
65
66 wxFileDialog* m_dialog;
67 int m_currentfilter;
68 wxString m_defaultLocation;
69 wxArrayString m_extensions;
70 wxArrayLong m_filtermactypes;
71 CFMutableArrayRef m_menuitems;
72 wxArrayString m_name;
73 bool m_saveMode;
74 SInt16 m_lastRight;
75 SInt16 m_lastBottom;
76 bool m_controlAdded;
77 };
78
79 OpenUserDataRec::OpenUserDataRec( wxFileDialog* d)
80 {
81 m_dialog = d;
82 m_controlAdded = false;
83 m_saveMode = m_dialog->HasFdFlag(wxFD_SAVE);
84
85 m_defaultLocation = m_dialog->GetDirectory();
86 MakeUserDataRec(m_dialog->GetWildcard());
87 m_currentfilter = m_dialog->GetFilterIndex();
88
89 m_menuitems = NULL;
90
91 size_t numFilters = m_extensions.GetCount();
92 if (numFilters)
93 {
94 m_menuitems = CFArrayCreateMutable( kCFAllocatorDefault ,
95 numFilters , &kCFTypeArrayCallBacks ) ;
96 for ( size_t i = 0 ; i < numFilters ; ++i )
97 {
98 CFArrayAppendValue( m_menuitems , (CFStringRef) wxCFStringRef( m_name[i] ) ) ;
99 }
100 }
101 m_lastRight = m_lastBottom = 0;
102 }
103
104 void OpenUserDataRec::EventProc(NavEventCallbackMessage inSelector,NavCBRecPtr ioParams)
105 {
106 switch (inSelector)
107 {
108 case kNavCBEvent:
109 EventProcCBEvent(ioParams);
110 break;
111 case kNavCBStart:
112 EventProcCBStart(ioParams);
113 break;
114 case kNavCBPopupMenuSelect:
115 EventProcCBPopupMenuSelect(ioParams);
116 break;
117 case kNavCBCustomize:
118 EventProcCBCustomize(ioParams);
119 break;
120 case kNavCBAdjustRect:
121 EventProcCBAdjustRect(ioParams);
122 break;
123 default:
124 break;
125 }
126 }
127
128 void OpenUserDataRec::EventProcCBEvent(NavCBRecPtr callBackParms)
129 {
130 switch (callBackParms->eventData.eventDataParms.event->what)
131 {
132 case mouseDown:
133 {
134 EventProcCBEventMouseDown(callBackParms);
135 break;
136 }
137 }
138 }
139
140 void OpenUserDataRec::EventProcCBEventMouseDown(NavCBRecPtr callBackParms)
141 {
142 EventRecord *evt = callBackParms->eventData.eventDataParms.event;
143 Point where = evt->where;
144 QDGlobalToLocalPoint(GetWindowPort(callBackParms->window), &where);
145
146 ControlRef whichControl = FindControlUnderMouse(where, callBackParms->window, NULL);
147 if (whichControl != NULL)
148 {
149 ControlKind theKind;
150 GetControlKind(whichControl, &theKind);
151
152 // Moving the focus if we clicked in an focusable control
153 if ((theKind.kind == kControlKindEditUnicodeText) ||
154 (theKind.kind == kControlKindEditText) ||
155 (theKind.kind == kControlKindDataBrowser) ||
156 (theKind.kind == kControlKindListBox))
157 {
158 ControlRef currentlyFocusedControl;
159 GetKeyboardFocus(callBackParms->window, &currentlyFocusedControl);
160 if (currentlyFocusedControl != whichControl)
161 SetKeyboardFocus(callBackParms->window, whichControl, kControlFocusNextPart);
162 }
163 HandleControlClick(whichControl, where, evt->modifiers, NULL);
164 }
165 }
166
167 void OpenUserDataRec::EventProcCBStart(NavCBRecPtr ioParams)
168 {
169 if (!m_defaultLocation.empty())
170 {
171 // Set default location for the modern Navigation APIs
172 // Apple Technical Q&A 1151
173 FSRef theFile;
174 wxMacPathToFSRef(m_defaultLocation, &theFile);
175 AEDesc theLocation = { typeNull, NULL };
176 if (noErr == ::AECreateDesc(typeFSRef, &theFile, sizeof(FSRef), &theLocation))
177 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
178 }
179
180 if( m_extensions.GetCount() > 0 )
181 {
182 NavMenuItemSpec menuItem;
183 memset( &menuItem, 0, sizeof(menuItem) );
184 menuItem.version = kNavMenuItemSpecVersion;
185 menuItem.menuType = m_currentfilter;
186 ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
187 }
188
189 if (m_dialog->GetExtraControl())
190 {
191 m_controlAdded = true;
192 ControlRef ref = m_dialog->GetExtraControl()->GetPeer()->GetControlRef();
193 NavCustomControl(ioParams->context, kNavCtlAddControl, ref);
194 }
195
196 }
197
198 void OpenUserDataRec::EventProcCBPopupMenuSelect(NavCBRecPtr ioParams)
199 {
200 NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
201 const size_t numFilters = m_extensions.GetCount();
202
203 if ( menu->menuType < numFilters )
204 {
205 m_currentfilter = menu->menuType ;
206 if ( m_saveMode )
207 {
208 int i = menu->menuType ;
209
210 // isolate the first extension string
211 wxString firstExtension = m_extensions[i].BeforeFirst('|').BeforeFirst(';');
212
213 wxString extension = firstExtension.AfterLast('.') ;
214 wxString sfilename ;
215
216 wxCFStringRef cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams->context ) ) );
217 sfilename = cfString.AsString() ;
218
219 int pos = sfilename.Find('.', true) ;
220 if ( pos != wxNOT_FOUND && extension != wxT("*") )
221 {
222 sfilename = sfilename.Left(pos+1)+extension ;
223 cfString = wxCFStringRef( sfilename , wxFONTENCODING_DEFAULT ) ;
224 NavDialogSetSaveFileName( ioParams->context , cfString ) ;
225 }
226 }
227 }
228 }
229
230 void OpenUserDataRec::EventProcCBCustomize(NavCBRecPtr ioParams)
231 {
232 wxWindow* control = m_dialog->GetExtraControl();
233
234 if ( control )
235 {
236 SInt16 neededRight, neededBottom;
237
238 wxSize size = m_dialog->GetExtraControl()->GetSize();
239 neededRight = ioParams->customRect.left + size.x;
240 neededBottom = ioParams->customRect.top + size.y;
241
242 if (ioParams->customRect.right == 0 && ioParams->customRect.bottom == 0)
243 {
244 ioParams->customRect.right = neededRight;
245 ioParams->customRect.bottom = neededBottom;
246 }
247 else
248 {
249 if ( ioParams->customRect.right != m_lastRight )
250 {
251 if ( ioParams->customRect.right < neededRight )
252 ioParams->customRect.right = neededRight;
253 }
254 if ( ioParams->customRect.bottom != m_lastBottom )
255 {
256 if ( ioParams->customRect.bottom < neededBottom )
257 ioParams->customRect.bottom = neededBottom;
258 }
259 }
260 m_lastRight = ioParams->customRect.right;
261 m_lastBottom = ioParams->customRect.bottom;
262 }
263 }
264
265 void OpenUserDataRec::EventProcCBAdjustRect(NavCBRecPtr ioParams)
266 {
267 wxWindow* control = m_dialog->GetExtraControl();
268
269 if ( control && m_controlAdded)
270 {
271 control->SetSize(ioParams->customRect.left , ioParams->customRect.top,
272 ioParams->customRect.right - ioParams->customRect.left,
273 ioParams->customRect.bottom - ioParams->customRect.top);
274 }
275 }
276
277 void OpenUserDataRec::MakeUserDataRec( const wxString& filter )
278 {
279 if ( !filter.empty() )
280 {
281 wxString filter2(filter) ;
282 int filterIndex = 0;
283 bool isName = true ;
284 wxString current ;
285
286 for ( unsigned int i = 0; i < filter2.length() ; i++ )
287 {
288 if ( filter2.GetChar(i) == wxT('|') )
289 {
290 if ( isName )
291 {
292 m_name.Add( current ) ;
293 }
294 else
295 {
296 m_extensions.Add( current ) ;
297 ++filterIndex ;
298 }
299
300 isName = !isName ;
301 current = wxEmptyString ;
302 }
303 else
304 {
305 current += filter2.GetChar(i) ;
306 }
307 }
308 // we allow for compatibility reason to have a single filter expression (like *.*) without
309 // an explanatory text, in that case the first part is name and extension at the same time
310
311 wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
312 if ( current.empty() )
313 m_extensions.Add( m_name[filterIndex] ) ;
314 else
315 m_extensions.Add( current ) ;
316 if ( filterIndex == 0 || isName )
317 m_name.Add( current ) ;
318
319 ++filterIndex ;
320
321 const size_t extCount = m_extensions.GetCount();
322 for ( size_t i = 0 ; i < extCount; i++ )
323 {
324 wxUint32 fileType, creator;
325 wxString extension = m_extensions[i];
326
327 // Remove leading '*'
328 if ( !extension.empty() && (extension.GetChar(0) == '*') )
329 extension = extension.Mid( 1 );
330
331 // Remove leading '.'
332 if ( !extension.empty() && (extension.GetChar(0) == '.') )
333 extension = extension.Mid( 1 );
334
335 if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
336 m_filtermactypes.Add( (OSType)fileType );
337 else
338 m_filtermactypes.Add( '****' ); // We'll fail safe if it's not recognized
339 }
340 }
341 }
342
343 bool OpenUserDataRec::CheckFile( const wxString &filename , OSType type)
344 {
345 wxString file(filename) ;
346 file.MakeUpper() ;
347
348 if ( m_extensions.GetCount() > 0 )
349 {
350 //for ( int i = 0 ; i < data->numfilters ; ++i )
351 int i = m_currentfilter ;
352 if ( m_extensions[i].Right(2) == wxT(".*") )
353 return true ;
354
355 {
356 if ( type == (OSType)m_filtermactypes[i] )
357 return true ;
358
359 wxStringTokenizer tokenizer( m_extensions[i] , wxT(";") ) ;
360 while ( tokenizer.HasMoreTokens() )
361 {
362 wxString extension = tokenizer.GetNextToken() ;
363 if ( extension.GetChar(0) == '*' )
364 extension = extension.Mid(1) ;
365 extension.MakeUpper();
366
367 if ( file.length() >= extension.length() && extension == file.Right(extension.length() ) )
368 return true ;
369 }
370 }
371
372 return false ;
373 }
374
375 return true ;
376 }
377
378 bool OpenUserDataRec::FilterCallback(
379 AEDesc *theItem,
380 void *info,
381 NavFilterModes filterMode )
382 {
383 if (filterMode == kNavFilteringBrowserList)
384 {
385 // We allow navigation to all folders. For files, we check against the current
386 // filter string.
387 // However, packages should be dealt with like files and not like folders. So
388 // check if a folder is a package before deciding what to do.
389 NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*) info ;
390 FSRef fsref;
391
392 if ( theInfo->isFolder )
393 {
394 // check bundle bit (using Finder Services - used by OS9 on some bundles)
395 FSCatalogInfo catalogInfo;
396 if (FSGetCatalogInfo (&fsref, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL) != noErr)
397 return true;
398
399 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
400 LSItemInfoRecord lsInfo;
401 if (LSCopyItemInfoForRef(&fsref, kLSRequestBasicFlagsOnly, &lsInfo ) != noErr)
402 return true;
403
404 // If it's not a bundle, then it's a normal folder and it passes our filter
405 FileInfo *fileInfo = (FileInfo *) catalogInfo.finderInfo;
406 if ( !(fileInfo->finderFlags & kHasBundle) &&
407 !(lsInfo.flags & (kLSItemInfoIsApplication | kLSItemInfoIsPackage)) )
408 return true;
409 }
410 else
411 {
412 AECoerceDesc (theItem, typeFSRef, theItem);
413 if ( AEGetDescData (theItem, &fsref, sizeof (FSRef)) == noErr)
414 {
415 wxString file = wxMacFSRefToPath( &fsref ) ;
416 return CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType ) ;
417 }
418 }
419 }
420
421 return true;
422 }
423
424 // end wxmac
425
426 pascal Boolean CrossPlatformFilterCallback(
427 AEDesc *theItem,
428 void *info,
429 void *callBackUD,
430 NavFilterModes filterMode );
431
432 pascal Boolean CrossPlatformFilterCallback(
433 AEDesc *theItem,
434 void *info,
435 void *callBackUD,
436 NavFilterModes filterMode )
437 {
438 OpenUserDataRec* data = (OpenUserDataRec*) callBackUD ;
439 return data->FilterCallback(theItem,info,filterMode);
440 }
441
442 static pascal void NavEventProc(
443 NavEventCallbackMessage inSelector,
444 NavCBRecPtr ioParams,
445 NavCallBackUserData ioUserData );
446
447 static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
448
449 static pascal void NavEventProc(
450 NavEventCallbackMessage inSelector,
451 NavCBRecPtr ioParams,
452 NavCallBackUserData ioUserData )
453 {
454 OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
455 data->EventProc(inSelector, ioParams);
456 }
457
458
459 void wxFileDialog::Init()
460 {
461 }
462
463 void wxFileDialog::Create(
464 wxWindow *parent, const wxString& message,
465 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
466 long style, const wxPoint& pos, const wxSize& sz, const wxString& name)
467 {
468 wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
469
470 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
471 }
472
473 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow)
474 {
475 wxTopLevelWindow::Create( GetParent(), nativeWindow );
476
477 if (HasExtraControlCreator())
478 {
479 CreateExtraControl();
480 }
481 }
482
483 int wxFileDialog::ShowModal()
484 {
485 WX_HOOK_MODAL_DIALOG();
486
487 m_paths.Empty();
488 m_fileNames.Empty();
489
490 OSErr err;
491 NavDialogCreationOptions dialogCreateOptions;
492
493 // set default options
494 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions);
495
496 // this was always unset in the old code
497 dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
498
499 wxCFStringRef message(m_message, GetFont().GetEncoding());
500 dialogCreateOptions.windowTitle = message;
501
502 wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding());
503 dialogCreateOptions.saveFileName = defaultFileName;
504
505 NavDialogRef dialog;
506 NavObjectFilterUPP navFilterUPP = NULL;
507 OpenUserDataRec myData( this );
508
509 dialogCreateOptions.popupExtension = myData.GetMenuItems();
510
511 if (HasFdFlag(wxFD_SAVE))
512 {
513 dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
514 dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
515 if (dialogCreateOptions.popupExtension == NULL)
516 dialogCreateOptions.optionFlags |= kNavNoTypePopup;
517
518 // The extension is important
519 if ( dialogCreateOptions.popupExtension == NULL || CFArrayGetCount(dialogCreateOptions.popupExtension)<2)
520 dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
521
522 if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT))
523 dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement;
524
525 err = ::NavCreatePutFileDialog(
526 &dialogCreateOptions,
527 kNavGenericSignature, // Suppresses the 'Default' (top) menu item
528 kNavGenericSignature,
529 sStandardNavEventFilter,
530 &myData, // for defaultLocation
531 &dialog );
532 }
533 else
534 {
535 // let the user select bundles/programs in dialogs
536 dialogCreateOptions.optionFlags |= kNavSupportPackages;
537
538 navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
539 err = ::NavCreateGetFileDialog(
540 &dialogCreateOptions,
541 NULL, // NavTypeListHandle
542 sStandardNavEventFilter,
543 NULL, // NavPreviewUPP
544 navFilterUPP,
545 (void *) &myData, // inClientData
546 &dialog );
547 }
548
549 SetupExtraControls(NavDialogGetWindow(dialog));
550
551 if (err == noErr)
552 {
553 wxDialog::OSXBeginModalDialog();
554 err = ::NavDialogRun(dialog);
555 wxDialog::OSXEndModalDialog();
556 }
557
558 // clean up filter related data, etc.
559 if (navFilterUPP)
560 ::DisposeNavObjectFilterUPP(navFilterUPP);
561
562 if (err != noErr)
563 {
564 ::NavDialogDispose(dialog);
565 return wxID_CANCEL;
566 }
567
568 NavReplyRecord navReply;
569 err = ::NavDialogGetReply(dialog, &navReply);
570 if (err == noErr && navReply.validRecord)
571 {
572 AEKeyword theKeyword;
573 DescType actualType;
574 Size actualSize;
575 FSRef theFSRef;
576 wxString thePath ;
577 long count;
578
579 m_filterIndex = myData.GetCurrentFilter();
580 ::AECountItems( &navReply.selection, &count );
581 for (long i = 1; i <= count; ++i)
582 {
583 err = ::AEGetNthPtr(
584 &(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
585 &theFSRef, sizeof(theFSRef), &actualSize );
586 if (err != noErr)
587 break;
588
589 if (HasFdFlag(wxFD_SAVE))
590 thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName );
591 else
592 thePath = wxMacFSRefToPath( &theFSRef );
593
594 if (!thePath)
595 {
596 ::NavDisposeReply(&navReply);
597 ::NavDialogDispose(dialog);
598 return wxID_CANCEL;
599 }
600
601 m_path = thePath;
602 m_paths.Add(m_path);
603 m_fileName = wxFileNameFromPath(m_path);
604 m_fileNames.Add(m_fileName);
605 }
606
607 // set these to the first hit
608 m_path = m_paths[0];
609 m_fileName = wxFileNameFromPath(m_path);
610 m_dir = wxPathOnly(m_path);
611 }
612
613 UnsubclassWin();
614 ::NavDisposeReply(&navReply);
615 ::NavDialogDispose(dialog);
616
617 return (err == noErr) ? wxID_OK : wxID_CANCEL;
618 }
619
620 bool wxFileDialog::SupportsExtraControl() const
621 {
622 return true;
623 }
624
625 #endif // wxUSE_FILEDLG
626