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/testing.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 wxFileDialog::wxFileDialog(
460 wxWindow
*parent
, const wxString
& message
,
461 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
462 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
463 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
465 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
468 void wxFileDialog::SetupExtraControls(WXWindow nativeWindow
)
470 wxTopLevelWindow::Create( GetParent(), nativeWindow
);
472 if (HasExtraControlCreator())
474 CreateExtraControl();
478 int wxFileDialog::ShowModal()
480 WX_TESTING_SHOW_MODAL_HOOK();
486 NavDialogCreationOptions dialogCreateOptions
;
488 // set default options
489 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
491 // this was always unset in the old code
492 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
494 wxCFStringRef
message(m_message
, GetFont().GetEncoding());
495 dialogCreateOptions
.windowTitle
= message
;
497 wxCFStringRef
defaultFileName(m_fileName
, GetFont().GetEncoding());
498 dialogCreateOptions
.saveFileName
= defaultFileName
;
501 NavObjectFilterUPP navFilterUPP
= NULL
;
502 OpenUserDataRec
myData( this );
504 dialogCreateOptions
.popupExtension
= myData
.GetMenuItems();
506 if (HasFdFlag(wxFD_SAVE
))
508 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
509 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
510 if (dialogCreateOptions
.popupExtension
== NULL
)
511 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
513 // The extension is important
514 if ( dialogCreateOptions
.popupExtension
== NULL
|| CFArrayGetCount(dialogCreateOptions
.popupExtension
)<2)
515 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
517 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
518 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
520 err
= ::NavCreatePutFileDialog(
521 &dialogCreateOptions
,
522 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
523 kNavGenericSignature
,
524 sStandardNavEventFilter
,
525 &myData
, // for defaultLocation
530 // let the user select bundles/programs in dialogs
531 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
533 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
534 err
= ::NavCreateGetFileDialog(
535 &dialogCreateOptions
,
536 NULL
, // NavTypeListHandle
537 sStandardNavEventFilter
,
538 NULL
, // NavPreviewUPP
540 (void *) &myData
, // inClientData
544 SetupExtraControls(NavDialogGetWindow(dialog
));
548 wxDialog::OSXBeginModalDialog();
549 err
= ::NavDialogRun(dialog
);
550 wxDialog::OSXEndModalDialog();
553 // clean up filter related data, etc.
555 ::DisposeNavObjectFilterUPP(navFilterUPP
);
559 ::NavDialogDispose(dialog
);
563 NavReplyRecord navReply
;
564 err
= ::NavDialogGetReply(dialog
, &navReply
);
565 if (err
== noErr
&& navReply
.validRecord
)
567 AEKeyword theKeyword
;
574 m_filterIndex
= myData
.GetCurrentFilter();
575 ::AECountItems( &navReply
.selection
, &count
);
576 for (long i
= 1; i
<= count
; ++i
)
579 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
580 &theFSRef
, sizeof(theFSRef
), &actualSize
);
584 if (HasFdFlag(wxFD_SAVE
))
585 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
587 thePath
= wxMacFSRefToPath( &theFSRef
);
591 ::NavDisposeReply(&navReply
);
592 ::NavDialogDispose(dialog
);
598 m_fileName
= wxFileNameFromPath(m_path
);
599 m_fileNames
.Add(m_fileName
);
602 // set these to the first hit
604 m_fileName
= wxFileNameFromPath(m_path
);
605 m_dir
= wxPathOnly(m_path
);
609 ::NavDisposeReply(&navReply
);
610 ::NavDialogDispose(dialog
);
612 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
615 bool wxFileDialog::SupportsExtraControl() const
620 #endif // wxUSE_FILEDLG