1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/filedlg.h"
22 #include "wx/dialog.h"
25 #include "wx/tokenzr.h"
26 #include "wx/filename.h"
28 #include "wx/osx/private.h"
29 #include "wx/modalhook.h"
32 #include <Navigation.h>
33 #include "PLStringFuncs.h"
36 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
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
47 OpenUserDataRec( wxFileDialog
* dialog
);
49 bool FilterCallback( AEDesc
*theItem
, void *info
, NavFilterModes filterMode
);
50 void EventProc( NavEventCallbackMessage inSelector
, NavCBRecPtr ioParams
);
52 int GetCurrentFilter() const {return m_currentfilter
;}
53 CFArrayRef
GetMenuItems() const { return m_menuitems
;}
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
);
66 wxFileDialog
* m_dialog
;
68 wxString m_defaultLocation
;
69 wxArrayString m_extensions
;
70 wxArrayLong m_filtermactypes
;
71 CFMutableArrayRef m_menuitems
;
79 OpenUserDataRec::OpenUserDataRec( wxFileDialog
* d
)
82 m_controlAdded
= false;
83 m_saveMode
= m_dialog
->HasFdFlag(wxFD_SAVE
);
85 m_defaultLocation
= m_dialog
->GetDirectory();
86 MakeUserDataRec(m_dialog
->GetWildcard());
87 m_currentfilter
= m_dialog
->GetFilterIndex();
91 size_t numFilters
= m_extensions
.GetCount();
94 m_menuitems
= CFArrayCreateMutable( kCFAllocatorDefault
,
95 numFilters
, &kCFTypeArrayCallBacks
) ;
96 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
98 CFArrayAppendValue( m_menuitems
, (CFStringRef
) wxCFStringRef( m_name
[i
] ) ) ;
101 m_lastRight
= m_lastBottom
= 0;
104 void OpenUserDataRec::EventProc(NavEventCallbackMessage inSelector
,NavCBRecPtr ioParams
)
109 EventProcCBEvent(ioParams
);
112 EventProcCBStart(ioParams
);
114 case kNavCBPopupMenuSelect
:
115 EventProcCBPopupMenuSelect(ioParams
);
117 case kNavCBCustomize
:
118 EventProcCBCustomize(ioParams
);
120 case kNavCBAdjustRect
:
121 EventProcCBAdjustRect(ioParams
);
128 void OpenUserDataRec::EventProcCBEvent(NavCBRecPtr callBackParms
)
130 switch (callBackParms
->eventData
.eventDataParms
.event
->what
)
134 EventProcCBEventMouseDown(callBackParms
);
140 void OpenUserDataRec::EventProcCBEventMouseDown(NavCBRecPtr callBackParms
)
142 EventRecord
*evt
= callBackParms
->eventData
.eventDataParms
.event
;
143 Point where
= evt
->where
;
144 QDGlobalToLocalPoint(GetWindowPort(callBackParms
->window
), &where
);
146 ControlRef whichControl
= FindControlUnderMouse(where
, callBackParms
->window
, NULL
);
147 if (whichControl
!= NULL
)
150 GetControlKind(whichControl
, &theKind
);
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
))
158 ControlRef currentlyFocusedControl
;
159 GetKeyboardFocus(callBackParms
->window
, ¤tlyFocusedControl
);
160 if (currentlyFocusedControl
!= whichControl
)
161 SetKeyboardFocus(callBackParms
->window
, whichControl
, kControlFocusNextPart
);
163 HandleControlClick(whichControl
, where
, evt
->modifiers
, NULL
);
167 void OpenUserDataRec::EventProcCBStart(NavCBRecPtr ioParams
)
169 if (!m_defaultLocation
.empty())
171 // Set default location for the modern Navigation APIs
172 // Apple Technical Q&A 1151
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
);
180 if( m_extensions
.GetCount() > 0 )
182 NavMenuItemSpec menuItem
;
183 memset( &menuItem
, 0, sizeof(menuItem
) );
184 menuItem
.version
= kNavMenuItemSpecVersion
;
185 menuItem
.menuType
= m_currentfilter
;
186 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
189 if (m_dialog
->GetExtraControl())
191 m_controlAdded
= true;
192 ControlRef ref
= m_dialog
->GetExtraControl()->GetPeer()->GetControlRef();
193 NavCustomControl(ioParams
->context
, kNavCtlAddControl
, ref
);
198 void OpenUserDataRec::EventProcCBPopupMenuSelect(NavCBRecPtr ioParams
)
200 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
201 const size_t numFilters
= m_extensions
.GetCount();
203 if ( menu
->menuType
< numFilters
)
205 m_currentfilter
= menu
->menuType
;
208 int i
= menu
->menuType
;
210 // isolate the first extension string
211 wxString firstExtension
= m_extensions
[i
].BeforeFirst('|').BeforeFirst(';');
213 wxString extension
= firstExtension
.AfterLast('.') ;
216 wxCFStringRef
cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams
->context
) ) );
217 sfilename
= cfString
.AsString() ;
219 int pos
= sfilename
.Find('.', true) ;
220 if ( pos
!= wxNOT_FOUND
&& extension
!= wxT("*") )
222 sfilename
= sfilename
.Left(pos
+1)+extension
;
223 cfString
= wxCFStringRef( sfilename
, wxFONTENCODING_DEFAULT
) ;
224 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
230 void OpenUserDataRec::EventProcCBCustomize(NavCBRecPtr ioParams
)
232 wxWindow
* control
= m_dialog
->GetExtraControl();
236 SInt16 neededRight
, neededBottom
;
238 wxSize size
= m_dialog
->GetExtraControl()->GetSize();
239 neededRight
= ioParams
->customRect
.left
+ size
.x
;
240 neededBottom
= ioParams
->customRect
.top
+ size
.y
;
242 if (ioParams
->customRect
.right
== 0 && ioParams
->customRect
.bottom
== 0)
244 ioParams
->customRect
.right
= neededRight
;
245 ioParams
->customRect
.bottom
= neededBottom
;
249 if ( ioParams
->customRect
.right
!= m_lastRight
)
251 if ( ioParams
->customRect
.right
< neededRight
)
252 ioParams
->customRect
.right
= neededRight
;
254 if ( ioParams
->customRect
.bottom
!= m_lastBottom
)
256 if ( ioParams
->customRect
.bottom
< neededBottom
)
257 ioParams
->customRect
.bottom
= neededBottom
;
260 m_lastRight
= ioParams
->customRect
.right
;
261 m_lastBottom
= ioParams
->customRect
.bottom
;
265 void OpenUserDataRec::EventProcCBAdjustRect(NavCBRecPtr ioParams
)
267 wxWindow
* control
= m_dialog
->GetExtraControl();
269 if ( control
&& m_controlAdded
)
271 control
->SetSize(ioParams
->customRect
.left
, ioParams
->customRect
.top
,
272 ioParams
->customRect
.right
- ioParams
->customRect
.left
,
273 ioParams
->customRect
.bottom
- ioParams
->customRect
.top
);
277 void OpenUserDataRec::MakeUserDataRec( const wxString
& filter
)
279 if ( !filter
.empty() )
281 wxString
filter2(filter
) ;
286 for ( unsigned int i
= 0; i
< filter2
.length() ; i
++ )
288 if ( filter2
.GetChar(i
) == wxT('|') )
292 m_name
.Add( current
) ;
296 m_extensions
.Add( current
) ;
301 current
= wxEmptyString
;
305 current
+= filter2
.GetChar(i
) ;
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
311 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
312 if ( current
.empty() )
313 m_extensions
.Add( m_name
[filterIndex
] ) ;
315 m_extensions
.Add( current
) ;
316 if ( filterIndex
== 0 || isName
)
317 m_name
.Add( current
) ;
321 const size_t extCount
= m_extensions
.GetCount();
322 for ( size_t i
= 0 ; i
< extCount
; i
++ )
324 wxUint32 fileType
, creator
;
325 wxString extension
= m_extensions
[i
];
327 // Remove leading '*'
328 if ( !extension
.empty() && (extension
.GetChar(0) == '*') )
329 extension
= extension
.Mid( 1 );
331 // Remove leading '.'
332 if ( !extension
.empty() && (extension
.GetChar(0) == '.') )
333 extension
= extension
.Mid( 1 );
335 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
336 m_filtermactypes
.Add( (OSType
)fileType
);
338 m_filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
343 bool OpenUserDataRec::CheckFile( const wxString
&filename
, OSType type
)
345 wxString
file(filename
) ;
348 if ( m_extensions
.GetCount() > 0 )
350 //for ( int i = 0 ; i < data->numfilters ; ++i )
351 int i
= m_currentfilter
;
352 if ( m_extensions
[i
].Right(2) == wxT(".*") )
356 if ( type
== (OSType
)m_filtermactypes
[i
] )
359 wxStringTokenizer
tokenizer( m_extensions
[i
] , wxT(";") ) ;
360 while ( tokenizer
.HasMoreTokens() )
362 wxString extension
= tokenizer
.GetNextToken() ;
363 if ( extension
.GetChar(0) == '*' )
364 extension
= extension
.Mid(1) ;
365 extension
.MakeUpper();
367 if ( file
.length() >= extension
.length() && extension
== file
.Right(extension
.length() ) )
378 bool OpenUserDataRec::FilterCallback(
381 NavFilterModes filterMode
)
383 if (filterMode
== kNavFilteringBrowserList
)
385 // We allow navigation to all folders. For files, we check against the current
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
;
392 if ( theInfo
->isFolder
)
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
)
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
)
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
)) )
412 AECoerceDesc (theItem
, typeFSRef
, theItem
);
413 if ( AEGetDescData (theItem
, &fsref
, sizeof (FSRef
)) == noErr
)
415 wxString file
= wxMacFSRefToPath( &fsref
) ;
416 return CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
) ;
426 pascal Boolean
CrossPlatformFilterCallback(
430 NavFilterModes filterMode
);
432 pascal Boolean
CrossPlatformFilterCallback(
436 NavFilterModes filterMode
)
438 OpenUserDataRec
* data
= (OpenUserDataRec
*) callBackUD
;
439 return data
->FilterCallback(theItem
,info
,filterMode
);
442 static pascal void NavEventProc(
443 NavEventCallbackMessage inSelector
,
444 NavCBRecPtr ioParams
,
445 NavCallBackUserData ioUserData
);
447 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
449 static pascal void NavEventProc(
450 NavEventCallbackMessage inSelector
,
451 NavCBRecPtr ioParams
,
452 NavCallBackUserData ioUserData
)
454 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
455 data
->EventProc(inSelector
, ioParams
);
459 void wxFileDialog::Init()
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
)
468 wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
470 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
473 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow
)
475 wxTopLevelWindow::Create( GetParent(), nativeWindow
);
477 if (HasExtraControlCreator())
479 CreateExtraControl();
483 int wxFileDialog::ShowModal()
485 WX_HOOK_MODAL_DIALOG();
491 NavDialogCreationOptions dialogCreateOptions
;
493 // set default options
494 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
496 // this was always unset in the old code
497 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
499 wxCFStringRef
message(m_message
, GetFont().GetEncoding());
500 dialogCreateOptions
.windowTitle
= message
;
502 wxCFStringRef
defaultFileName(m_fileName
, GetFont().GetEncoding());
503 dialogCreateOptions
.saveFileName
= defaultFileName
;
506 NavObjectFilterUPP navFilterUPP
= NULL
;
507 OpenUserDataRec
myData( this );
509 dialogCreateOptions
.popupExtension
= myData
.GetMenuItems();
511 if (HasFdFlag(wxFD_SAVE
))
513 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
514 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
515 if (dialogCreateOptions
.popupExtension
== NULL
)
516 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
518 // The extension is important
519 if ( dialogCreateOptions
.popupExtension
== NULL
|| CFArrayGetCount(dialogCreateOptions
.popupExtension
)<2)
520 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
522 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
523 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
525 err
= ::NavCreatePutFileDialog(
526 &dialogCreateOptions
,
527 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
528 kNavGenericSignature
,
529 sStandardNavEventFilter
,
530 &myData
, // for defaultLocation
535 // let the user select bundles/programs in dialogs
536 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
538 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
539 err
= ::NavCreateGetFileDialog(
540 &dialogCreateOptions
,
541 NULL
, // NavTypeListHandle
542 sStandardNavEventFilter
,
543 NULL
, // NavPreviewUPP
545 (void *) &myData
, // inClientData
549 SetupExtraControls(NavDialogGetWindow(dialog
));
553 wxDialog::OSXBeginModalDialog();
554 err
= ::NavDialogRun(dialog
);
555 wxDialog::OSXEndModalDialog();
558 // clean up filter related data, etc.
560 ::DisposeNavObjectFilterUPP(navFilterUPP
);
564 ::NavDialogDispose(dialog
);
568 NavReplyRecord navReply
;
569 err
= ::NavDialogGetReply(dialog
, &navReply
);
570 if (err
== noErr
&& navReply
.validRecord
)
572 AEKeyword theKeyword
;
579 m_filterIndex
= myData
.GetCurrentFilter();
580 ::AECountItems( &navReply
.selection
, &count
);
581 for (long i
= 1; i
<= count
; ++i
)
584 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
585 &theFSRef
, sizeof(theFSRef
), &actualSize
);
589 if (HasFdFlag(wxFD_SAVE
))
590 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
592 thePath
= wxMacFSRefToPath( &theFSRef
);
596 ::NavDisposeReply(&navReply
);
597 ::NavDialogDispose(dialog
);
603 m_fileName
= wxFileNameFromPath(m_path
);
604 m_fileNames
.Add(m_fileName
);
607 // set these to the first hit
609 m_fileName
= wxFileNameFromPath(m_path
);
610 m_dir
= wxPathOnly(m_path
);
614 ::NavDisposeReply(&navReply
);
615 ::NavDialogDispose(dialog
);
617 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
620 bool wxFileDialog::SupportsExtraControl() const
625 #endif // wxUSE_FILEDLG