1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/filedlg.h"
21 #include "wx/dialog.h"
24 #include "wx/tokenzr.h"
25 #include "wx/filename.h"
27 #include "wx/osx/private.h"
28 #include "wx/modalhook.h"
31 #include <Navigation.h>
32 #include "PLStringFuncs.h"
35 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
37 // the data we need to pass to our standard file hook routine
38 // includes a pointer to the dialog, a pointer to the standard
39 // file reply record (so we can inspect the current selection)
40 // and a copy of the "previous" file spec of the reply record
41 // so we can see if the selection has changed
46 OpenUserDataRec( wxFileDialog
* dialog
);
48 bool FilterCallback( AEDesc
*theItem
, void *info
, NavFilterModes filterMode
);
49 void EventProc( NavEventCallbackMessage inSelector
, NavCBRecPtr ioParams
);
51 int GetCurrentFilter() const {return m_currentfilter
;}
52 CFArrayRef
GetMenuItems() const { return m_menuitems
;}
56 void EventProcCBEvent( NavCBRecPtr ioParams
);
57 void EventProcCBEventMouseDown( NavCBRecPtr ioParams
);
58 void EventProcCBStart( NavCBRecPtr ioParams
);
59 void EventProcCBPopupMenuSelect( NavCBRecPtr ioParams
);
60 void EventProcCBCustomize( NavCBRecPtr ioParams
);
61 void EventProcCBAdjustRect( NavCBRecPtr ioParams
);
62 bool CheckFile( const wxString
&filename
, OSType type
);
63 void MakeUserDataRec( const wxString
& filter
);
65 wxFileDialog
* m_dialog
;
67 wxString m_defaultLocation
;
68 wxArrayString m_extensions
;
69 wxArrayLong m_filtermactypes
;
70 CFMutableArrayRef m_menuitems
;
78 OpenUserDataRec::OpenUserDataRec( wxFileDialog
* d
)
81 m_controlAdded
= false;
82 m_saveMode
= m_dialog
->HasFdFlag(wxFD_SAVE
);
84 m_defaultLocation
= m_dialog
->GetDirectory();
85 MakeUserDataRec(m_dialog
->GetWildcard());
86 m_currentfilter
= m_dialog
->GetFilterIndex();
90 size_t numFilters
= m_extensions
.GetCount();
93 m_menuitems
= CFArrayCreateMutable( kCFAllocatorDefault
,
94 numFilters
, &kCFTypeArrayCallBacks
) ;
95 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
97 CFArrayAppendValue( m_menuitems
, (CFStringRef
) wxCFStringRef( m_name
[i
] ) ) ;
100 m_lastRight
= m_lastBottom
= 0;
103 void OpenUserDataRec::EventProc(NavEventCallbackMessage inSelector
,NavCBRecPtr ioParams
)
108 EventProcCBEvent(ioParams
);
111 EventProcCBStart(ioParams
);
113 case kNavCBPopupMenuSelect
:
114 EventProcCBPopupMenuSelect(ioParams
);
116 case kNavCBCustomize
:
117 EventProcCBCustomize(ioParams
);
119 case kNavCBAdjustRect
:
120 EventProcCBAdjustRect(ioParams
);
127 void OpenUserDataRec::EventProcCBEvent(NavCBRecPtr callBackParms
)
129 switch (callBackParms
->eventData
.eventDataParms
.event
->what
)
133 EventProcCBEventMouseDown(callBackParms
);
139 void OpenUserDataRec::EventProcCBEventMouseDown(NavCBRecPtr callBackParms
)
141 EventRecord
*evt
= callBackParms
->eventData
.eventDataParms
.event
;
142 Point where
= evt
->where
;
143 QDGlobalToLocalPoint(GetWindowPort(callBackParms
->window
), &where
);
145 ControlRef whichControl
= FindControlUnderMouse(where
, callBackParms
->window
, NULL
);
146 if (whichControl
!= NULL
)
149 GetControlKind(whichControl
, &theKind
);
151 // Moving the focus if we clicked in an focusable control
152 if ((theKind
.kind
== kControlKindEditUnicodeText
) ||
153 (theKind
.kind
== kControlKindEditText
) ||
154 (theKind
.kind
== kControlKindDataBrowser
) ||
155 (theKind
.kind
== kControlKindListBox
))
157 ControlRef currentlyFocusedControl
;
158 GetKeyboardFocus(callBackParms
->window
, ¤tlyFocusedControl
);
159 if (currentlyFocusedControl
!= whichControl
)
160 SetKeyboardFocus(callBackParms
->window
, whichControl
, kControlFocusNextPart
);
162 HandleControlClick(whichControl
, where
, evt
->modifiers
, NULL
);
166 void OpenUserDataRec::EventProcCBStart(NavCBRecPtr ioParams
)
168 if (!m_defaultLocation
.empty())
170 // Set default location for the modern Navigation APIs
171 // Apple Technical Q&A 1151
173 wxMacPathToFSRef(m_defaultLocation
, &theFile
);
174 AEDesc theLocation
= { typeNull
, NULL
};
175 if (noErr
== ::AECreateDesc(typeFSRef
, &theFile
, sizeof(FSRef
), &theLocation
))
176 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
179 if( m_extensions
.GetCount() > 0 )
181 NavMenuItemSpec menuItem
;
182 memset( &menuItem
, 0, sizeof(menuItem
) );
183 menuItem
.version
= kNavMenuItemSpecVersion
;
184 menuItem
.menuType
= m_currentfilter
;
185 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
188 if (m_dialog
->GetExtraControl())
190 m_controlAdded
= true;
191 ControlRef ref
= m_dialog
->GetExtraControl()->GetPeer()->GetControlRef();
192 NavCustomControl(ioParams
->context
, kNavCtlAddControl
, ref
);
197 void OpenUserDataRec::EventProcCBPopupMenuSelect(NavCBRecPtr ioParams
)
199 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
200 const size_t numFilters
= m_extensions
.GetCount();
202 if ( menu
->menuType
< numFilters
)
204 m_currentfilter
= menu
->menuType
;
207 int i
= menu
->menuType
;
209 // isolate the first extension string
210 wxString firstExtension
= m_extensions
[i
].BeforeFirst('|').BeforeFirst(';');
212 wxString extension
= firstExtension
.AfterLast('.') ;
215 wxCFStringRef
cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams
->context
) ) );
216 sfilename
= cfString
.AsString() ;
218 int pos
= sfilename
.Find('.', true) ;
219 if ( pos
!= wxNOT_FOUND
&& extension
!= wxT("*") )
221 sfilename
= sfilename
.Left(pos
+1)+extension
;
222 cfString
= wxCFStringRef( sfilename
, wxFONTENCODING_DEFAULT
) ;
223 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
229 void OpenUserDataRec::EventProcCBCustomize(NavCBRecPtr ioParams
)
231 wxWindow
* control
= m_dialog
->GetExtraControl();
235 SInt16 neededRight
, neededBottom
;
237 wxSize size
= m_dialog
->GetExtraControl()->GetSize();
238 neededRight
= ioParams
->customRect
.left
+ size
.x
;
239 neededBottom
= ioParams
->customRect
.top
+ size
.y
;
241 if (ioParams
->customRect
.right
== 0 && ioParams
->customRect
.bottom
== 0)
243 ioParams
->customRect
.right
= neededRight
;
244 ioParams
->customRect
.bottom
= neededBottom
;
248 if ( ioParams
->customRect
.right
!= m_lastRight
)
250 if ( ioParams
->customRect
.right
< neededRight
)
251 ioParams
->customRect
.right
= neededRight
;
253 if ( ioParams
->customRect
.bottom
!= m_lastBottom
)
255 if ( ioParams
->customRect
.bottom
< neededBottom
)
256 ioParams
->customRect
.bottom
= neededBottom
;
259 m_lastRight
= ioParams
->customRect
.right
;
260 m_lastBottom
= ioParams
->customRect
.bottom
;
264 void OpenUserDataRec::EventProcCBAdjustRect(NavCBRecPtr ioParams
)
266 wxWindow
* control
= m_dialog
->GetExtraControl();
268 if ( control
&& m_controlAdded
)
270 control
->SetSize(ioParams
->customRect
.left
, ioParams
->customRect
.top
,
271 ioParams
->customRect
.right
- ioParams
->customRect
.left
,
272 ioParams
->customRect
.bottom
- ioParams
->customRect
.top
);
276 void OpenUserDataRec::MakeUserDataRec( const wxString
& filter
)
278 if ( !filter
.empty() )
280 wxString
filter2(filter
) ;
285 for ( unsigned int i
= 0; i
< filter2
.length() ; i
++ )
287 if ( filter2
.GetChar(i
) == wxT('|') )
291 m_name
.Add( current
) ;
295 m_extensions
.Add( current
) ;
300 current
= wxEmptyString
;
304 current
+= filter2
.GetChar(i
) ;
307 // we allow for compatibility reason to have a single filter expression (like *.*) without
308 // an explanatory text, in that case the first part is name and extension at the same time
310 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
311 if ( current
.empty() )
312 m_extensions
.Add( m_name
[filterIndex
] ) ;
314 m_extensions
.Add( current
) ;
315 if ( filterIndex
== 0 || isName
)
316 m_name
.Add( current
) ;
320 const size_t extCount
= m_extensions
.GetCount();
321 for ( size_t i
= 0 ; i
< extCount
; i
++ )
323 wxUint32 fileType
, creator
;
324 wxString extension
= m_extensions
[i
];
326 // Remove leading '*'
327 if ( !extension
.empty() && (extension
.GetChar(0) == '*') )
328 extension
= extension
.Mid( 1 );
330 // Remove leading '.'
331 if ( !extension
.empty() && (extension
.GetChar(0) == '.') )
332 extension
= extension
.Mid( 1 );
334 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
335 m_filtermactypes
.Add( (OSType
)fileType
);
337 m_filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
342 bool OpenUserDataRec::CheckFile( const wxString
&filename
, OSType type
)
344 wxString
file(filename
) ;
347 if ( m_extensions
.GetCount() > 0 )
349 //for ( int i = 0 ; i < data->numfilters ; ++i )
350 int i
= m_currentfilter
;
351 if ( m_extensions
[i
].Right(2) == wxT(".*") )
355 if ( type
== (OSType
)m_filtermactypes
[i
] )
358 wxStringTokenizer
tokenizer( m_extensions
[i
] , wxT(";") ) ;
359 while ( tokenizer
.HasMoreTokens() )
361 wxString extension
= tokenizer
.GetNextToken() ;
362 if ( extension
.GetChar(0) == '*' )
363 extension
= extension
.Mid(1) ;
364 extension
.MakeUpper();
366 if ( file
.length() >= extension
.length() && extension
== file
.Right(extension
.length() ) )
377 bool OpenUserDataRec::FilterCallback(
380 NavFilterModes filterMode
)
382 if (filterMode
== kNavFilteringBrowserList
)
384 // We allow navigation to all folders. For files, we check against the current
386 // However, packages should be dealt with like files and not like folders. So
387 // check if a folder is a package before deciding what to do.
388 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
391 if ( theInfo
->isFolder
)
393 // check bundle bit (using Finder Services - used by OS9 on some bundles)
394 FSCatalogInfo catalogInfo
;
395 if (FSGetCatalogInfo (&fsref
, kFSCatInfoFinderInfo
, &catalogInfo
, NULL
, NULL
, NULL
) != noErr
)
398 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
399 LSItemInfoRecord lsInfo
;
400 if (LSCopyItemInfoForRef(&fsref
, kLSRequestBasicFlagsOnly
, &lsInfo
) != noErr
)
403 // If it's not a bundle, then it's a normal folder and it passes our filter
404 FileInfo
*fileInfo
= (FileInfo
*) catalogInfo
.finderInfo
;
405 if ( !(fileInfo
->finderFlags
& kHasBundle
) &&
406 !(lsInfo
.flags
& (kLSItemInfoIsApplication
| kLSItemInfoIsPackage
)) )
411 AECoerceDesc (theItem
, typeFSRef
, theItem
);
412 if ( AEGetDescData (theItem
, &fsref
, sizeof (FSRef
)) == noErr
)
414 wxString file
= wxMacFSRefToPath( &fsref
) ;
415 return CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
) ;
425 pascal Boolean
CrossPlatformFilterCallback(
429 NavFilterModes filterMode
);
431 pascal Boolean
CrossPlatformFilterCallback(
435 NavFilterModes filterMode
)
437 OpenUserDataRec
* data
= (OpenUserDataRec
*) callBackUD
;
438 return data
->FilterCallback(theItem
,info
,filterMode
);
441 static pascal void NavEventProc(
442 NavEventCallbackMessage inSelector
,
443 NavCBRecPtr ioParams
,
444 NavCallBackUserData ioUserData
);
446 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
448 static pascal void NavEventProc(
449 NavEventCallbackMessage inSelector
,
450 NavCBRecPtr ioParams
,
451 NavCallBackUserData ioUserData
)
453 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
454 data
->EventProc(inSelector
, ioParams
);
458 void wxFileDialog::Init()
462 void wxFileDialog::Create(
463 wxWindow
*parent
, const wxString
& message
,
464 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
465 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
467 wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
469 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
472 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow
)
474 wxTopLevelWindow::Create( GetParent(), nativeWindow
);
476 if (HasExtraControlCreator())
478 CreateExtraControl();
482 int wxFileDialog::ShowModal()
484 WX_HOOK_MODAL_DIALOG();
490 NavDialogCreationOptions dialogCreateOptions
;
492 // set default options
493 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
495 // this was always unset in the old code
496 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
498 wxCFStringRef
message(m_message
, GetFont().GetEncoding());
499 dialogCreateOptions
.windowTitle
= message
;
501 wxCFStringRef
defaultFileName(m_fileName
, GetFont().GetEncoding());
502 dialogCreateOptions
.saveFileName
= defaultFileName
;
505 NavObjectFilterUPP navFilterUPP
= NULL
;
506 OpenUserDataRec
myData( this );
508 dialogCreateOptions
.popupExtension
= myData
.GetMenuItems();
510 if (HasFdFlag(wxFD_SAVE
))
512 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
513 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
514 if (dialogCreateOptions
.popupExtension
== NULL
)
515 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
517 // The extension is important
518 if ( dialogCreateOptions
.popupExtension
== NULL
|| CFArrayGetCount(dialogCreateOptions
.popupExtension
)<2)
519 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
521 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
522 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
524 err
= ::NavCreatePutFileDialog(
525 &dialogCreateOptions
,
526 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
527 kNavGenericSignature
,
528 sStandardNavEventFilter
,
529 &myData
, // for defaultLocation
534 // let the user select bundles/programs in dialogs
535 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
537 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
538 err
= ::NavCreateGetFileDialog(
539 &dialogCreateOptions
,
540 NULL
, // NavTypeListHandle
541 sStandardNavEventFilter
,
542 NULL
, // NavPreviewUPP
544 (void *) &myData
, // inClientData
548 SetupExtraControls(NavDialogGetWindow(dialog
));
552 wxDialog::OSXBeginModalDialog();
553 err
= ::NavDialogRun(dialog
);
554 wxDialog::OSXEndModalDialog();
557 // clean up filter related data, etc.
559 ::DisposeNavObjectFilterUPP(navFilterUPP
);
563 ::NavDialogDispose(dialog
);
567 NavReplyRecord navReply
;
568 err
= ::NavDialogGetReply(dialog
, &navReply
);
569 if (err
== noErr
&& navReply
.validRecord
)
571 AEKeyword theKeyword
;
578 m_filterIndex
= myData
.GetCurrentFilter();
579 ::AECountItems( &navReply
.selection
, &count
);
580 for (long i
= 1; i
<= count
; ++i
)
583 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
584 &theFSRef
, sizeof(theFSRef
), &actualSize
);
588 if (HasFdFlag(wxFD_SAVE
))
589 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
591 thePath
= wxMacFSRefToPath( &theFSRef
);
595 ::NavDisposeReply(&navReply
);
596 ::NavDialogDispose(dialog
);
602 m_fileName
= wxFileNameFromPath(m_path
);
603 m_fileNames
.Add(m_fileName
);
606 // set these to the first hit
608 m_fileName
= wxFileNameFromPath(m_path
);
609 m_dir
= wxPathOnly(m_path
);
613 ::NavDisposeReply(&navReply
);
614 ::NavDialogDispose(dialog
);
616 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
619 bool wxFileDialog::SupportsExtraControl() const
624 #endif // wxUSE_FILEDLG