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"
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 currentfilter
;}
52 CFArrayRef
GetMenuItems() const { return 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
);
67 wxString defaultLocation
;
68 wxArrayString extensions
;
69 wxArrayLong filtermactypes
;
70 CFMutableArrayRef menuitems
;
75 OpenUserDataRec::OpenUserDataRec( wxFileDialog
* d
)
78 saveMode
= dialog
->HasFdFlag(wxFD_SAVE
);
80 defaultLocation
= dialog
->GetDirectory();
81 MakeUserDataRec(dialog
->GetWildcard());
82 currentfilter
= dialog
->GetFilterIndex();
86 size_t numFilters
= extensions
.GetCount();
89 menuitems
= CFArrayCreateMutable( kCFAllocatorDefault
,
90 numFilters
, &kCFTypeArrayCallBacks
) ;
91 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
93 CFArrayAppendValue( menuitems
, (CFStringRef
) wxCFStringRef( name
[i
] ) ) ;
99 void OpenUserDataRec::EventProc(NavEventCallbackMessage inSelector
,NavCBRecPtr ioParams
)
104 EventProcCBEvent(ioParams
);
107 EventProcCBStart(ioParams
);
109 case kNavCBPopupMenuSelect
:
110 EventProcCBPopupMenuSelect(ioParams
);
112 case kNavCBCustomize
:
113 EventProcCBCustomize(ioParams
);
115 case kNavCBAdjustRect
:
116 EventProcCBAdjustRect(ioParams
);
123 void OpenUserDataRec::EventProcCBEvent(NavCBRecPtr callBackParms
)
125 switch (callBackParms
->eventData
.eventDataParms
.event
->what
)
129 EventProcCBEventMouseDown(callBackParms
);
135 void OpenUserDataRec::EventProcCBEventMouseDown(NavCBRecPtr callBackParms
)
137 EventRecord
*evt
= callBackParms
->eventData
.eventDataParms
.event
;
138 Point where
= evt
->where
;
139 GlobalToLocal(&where
);
141 ControlRef whichControl
= FindControlUnderMouse(where
, callBackParms
->window
, NULL
);
142 if (whichControl
!= NULL
)
145 GetControlKind(whichControl
, &theKind
);
147 // Moving the focus if we clicked in an editable text control
148 // In this sample, we only have a Clock and an Unicode Edit controls
149 if ((theKind
.kind
== kControlKindEditUnicodeText
) || (theKind
.kind
== kControlKindClock
))
151 ControlRef currentlyFocusedControl
;
152 GetKeyboardFocus(callBackParms
->window
, ¤tlyFocusedControl
);
153 if (currentlyFocusedControl
!= whichControl
)
154 SetKeyboardFocus(callBackParms
->window
, whichControl
, kControlFocusNextPart
);
156 HandleControlClick(whichControl
, where
, evt
->modifiers
, NULL
);
160 void OpenUserDataRec::EventProcCBStart(NavCBRecPtr ioParams
)
162 if (!defaultLocation
.empty())
164 // Set default location for the modern Navigation APIs
165 // Apple Technical Q&A 1151
167 wxMacPathToFSRef(defaultLocation
, &theFile
);
168 AEDesc theLocation
= { typeNull
, NULL
};
169 if (noErr
== ::AECreateDesc(typeFSRef
, &theFile
, sizeof(FSRef
), &theLocation
))
170 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
173 if( extensions
.GetCount() > 0 )
175 NavMenuItemSpec menuItem
;
176 memset( &menuItem
, 0, sizeof(menuItem
) );
177 menuItem
.version
= kNavMenuItemSpecVersion
;
178 menuItem
.menuType
= currentfilter
;
179 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
182 if (dialog
->GetExtraControl())
184 ControlRef ref
= dialog
->GetExtraControl()->GetPeer()->GetControlRef();
185 NavCustomControl(ioParams
->context
, kNavCtlAddControl
, ref
);
190 void OpenUserDataRec::EventProcCBPopupMenuSelect(NavCBRecPtr ioParams
)
192 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
193 const size_t numFilters
= extensions
.GetCount();
195 if ( menu
->menuType
< numFilters
)
197 currentfilter
= menu
->menuType
;
200 int i
= menu
->menuType
;
202 // isolate the first extension string
203 wxString firstExtension
= extensions
[i
].BeforeFirst('|').BeforeFirst(';');
205 wxString extension
= firstExtension
.AfterLast('.') ;
208 wxCFStringRef
cfString( wxCFRetain( NavDialogGetSaveFileName( ioParams
->context
) ) );
209 sfilename
= cfString
.AsString() ;
211 int pos
= sfilename
.Find('.', true) ;
212 if ( pos
!= wxNOT_FOUND
&& extension
!= wxT("*") )
214 sfilename
= sfilename
.Left(pos
+1)+extension
;
215 cfString
= wxCFStringRef( sfilename
, wxFONTENCODING_DEFAULT
) ;
216 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
222 void OpenUserDataRec::EventProcCBCustomize(NavCBRecPtr ioParams
)
224 if (ioParams
->customRect
.right
== 0 && ioParams
->customRect
.bottom
== 0 && dialog
->GetExtraControl())
226 wxSize size
= dialog
->GetExtraControl()->GetSize();
227 ioParams
->customRect
.right
= size
.x
;
228 ioParams
->customRect
.bottom
= size
.y
;
232 void OpenUserDataRec::EventProcCBAdjustRect(NavCBRecPtr ioParams
)
236 void OpenUserDataRec::MakeUserDataRec( const wxString
& filter
)
242 if ( !filter
.empty() )
244 wxString
filter2(filter
) ;
249 for ( unsigned int i
= 0; i
< filter2
.length() ; i
++ )
251 if ( filter2
.GetChar(i
) == wxT('|') )
255 name
.Add( current
) ;
259 extensions
.Add( current
) ;
264 current
= wxEmptyString
;
268 current
+= filter2
.GetChar(i
) ;
271 // we allow for compatibility reason to have a single filter expression (like *.*) without
272 // an explanatory text, in that case the first part is name and extension at the same time
274 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
275 if ( current
.empty() )
276 extensions
.Add( name
[filterIndex
] ) ;
278 extensions
.Add( current
) ;
279 if ( filterIndex
== 0 || isName
)
280 name
.Add( current
) ;
284 const size_t extCount
= extensions
.GetCount();
285 for ( size_t i
= 0 ; i
< extCount
; i
++ )
287 wxUint32 fileType
, creator
;
288 wxString extension
= extensions
[i
];
290 // Remove leading '*'
291 if (extension
.length() && (extension
.GetChar(0) == '*'))
292 extension
= extension
.Mid( 1 );
294 // Remove leading '.'
295 if (extension
.length() && (extension
.GetChar(0) == '.'))
296 extension
= extension
.Mid( 1 );
298 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
299 filtermactypes
.Add( (OSType
)fileType
);
301 filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
306 bool OpenUserDataRec::CheckFile( const wxString
&filename
, OSType type
)
308 wxString
file(filename
) ;
311 if ( extensions
.GetCount() > 0 )
313 //for ( int i = 0 ; i < data->numfilters ; ++i )
314 int i
= currentfilter
;
315 if ( extensions
[i
].Right(2) == wxT(".*") )
319 if ( type
== (OSType
)filtermactypes
[i
] )
322 wxStringTokenizer
tokenizer( extensions
[i
] , wxT(";") ) ;
323 while ( tokenizer
.HasMoreTokens() )
325 wxString extension
= tokenizer
.GetNextToken() ;
326 if ( extension
.GetChar(0) == '*' )
327 extension
= extension
.Mid(1) ;
328 extension
.MakeUpper();
330 if ( file
.length() >= extension
.length() && extension
== file
.Right(extension
.length() ) )
341 bool OpenUserDataRec::FilterCallback(
344 NavFilterModes filterMode
)
346 if (filterMode
== kNavFilteringBrowserList
)
348 // We allow navigation to all folders. For files, we check against the current
350 // However, packages should be dealt with like files and not like folders. So
351 // check if a folder is a package before deciding what to do.
352 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
355 if ( theInfo
->isFolder
)
357 // check bundle bit (using Finder Services - used by OS9 on some bundles)
358 FSCatalogInfo catalogInfo
;
359 if (FSGetCatalogInfo (&fsref
, kFSCatInfoFinderInfo
, &catalogInfo
, NULL
, NULL
, NULL
) != noErr
)
362 // Check bundle item (using Launch Services - used by OS-X through info.plist or APP)
363 LSItemInfoRecord lsInfo
;
364 if (LSCopyItemInfoForRef(&fsref
, kLSRequestBasicFlagsOnly
, &lsInfo
) != noErr
)
367 // If it's not a bundle, then it's a normal folder and it passes our filter
368 FileInfo
*fileInfo
= (FileInfo
*) catalogInfo
.finderInfo
;
369 if ( !(fileInfo
->finderFlags
& kHasBundle
) &&
370 !(lsInfo
.flags
& (kLSItemInfoIsApplication
| kLSItemInfoIsPackage
)) )
375 AECoerceDesc (theItem
, typeFSRef
, theItem
);
376 if ( AEGetDescData (theItem
, &fsref
, sizeof (FSRef
)) == noErr
)
378 wxString file
= wxMacFSRefToPath( &fsref
) ;
379 return CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
) ;
389 pascal Boolean
CrossPlatformFilterCallback(
393 NavFilterModes filterMode
);
395 pascal Boolean
CrossPlatformFilterCallback(
399 NavFilterModes filterMode
)
401 OpenUserDataRec
* data
= (OpenUserDataRec
*) callBackUD
;
402 return data
->FilterCallback(theItem
,info
,filterMode
);
405 static pascal void NavEventProc(
406 NavEventCallbackMessage inSelector
,
407 NavCBRecPtr ioParams
,
408 NavCallBackUserData ioUserData
);
410 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
412 static pascal void NavEventProc(
413 NavEventCallbackMessage inSelector
,
414 NavCBRecPtr ioParams
,
415 NavCallBackUserData ioUserData
)
417 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
418 data
->EventProc(inSelector
, ioParams
);
422 wxFileDialog::wxFileDialog(
423 wxWindow
*parent
, const wxString
& message
,
424 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
425 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
426 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
428 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
431 int wxFileDialog::ShowModal()
437 NavDialogCreationOptions dialogCreateOptions
;
439 // set default options
440 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
442 // this was always unset in the old code
443 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
445 wxCFStringRef
message(m_message
, GetFont().GetEncoding());
446 dialogCreateOptions
.windowTitle
= message
;
448 wxCFStringRef
defaultFileName(m_fileName
, GetFont().GetEncoding());
449 dialogCreateOptions
.saveFileName
= defaultFileName
;
452 NavObjectFilterUPP navFilterUPP
= NULL
;
453 OpenUserDataRec
myData( this );
455 dialogCreateOptions
.popupExtension
= myData
.GetMenuItems();
457 if (HasFdFlag(wxFD_SAVE
))
459 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
460 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
461 if (dialogCreateOptions
.popupExtension
== NULL
)
462 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
464 // The extension is important
465 if ( dialogCreateOptions
.popupExtension
== NULL
|| CFArrayGetCount(dialogCreateOptions
.popupExtension
)<2)
466 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
468 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
469 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
471 err
= ::NavCreatePutFileDialog(
472 &dialogCreateOptions
,
473 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
474 kNavGenericSignature
,
475 sStandardNavEventFilter
,
476 &myData
, // for defaultLocation
481 // let the user select bundles/programs in dialogs
482 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
484 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
485 err
= ::NavCreateGetFileDialog(
486 &dialogCreateOptions
,
487 NULL
, // NavTypeListHandle
488 sStandardNavEventFilter
,
489 NULL
, // NavPreviewUPP
491 (void *) &myData
, // inClientData
495 wxNonOwnedWindow::Create( GetParent(), NavDialogGetWindow(dialog
) );
497 if (HasExtraControlCreator())
499 CreateExtraControl();
503 err
= ::NavDialogRun(dialog
);
505 // clean up filter related data, etc.
507 ::DisposeNavObjectFilterUPP(navFilterUPP
);
511 ::NavDialogDispose(dialog
);
515 NavReplyRecord navReply
;
516 err
= ::NavDialogGetReply(dialog
, &navReply
);
517 if (err
== noErr
&& navReply
.validRecord
)
519 AEKeyword theKeyword
;
526 m_filterIndex
= myData
.GetCurrentFilter();
527 ::AECountItems( &navReply
.selection
, &count
);
528 for (long i
= 1; i
<= count
; ++i
)
531 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
532 &theFSRef
, sizeof(theFSRef
), &actualSize
);
536 if (HasFdFlag(wxFD_SAVE
))
537 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
539 thePath
= wxMacFSRefToPath( &theFSRef
);
543 ::NavDisposeReply(&navReply
);
544 ::NavDialogDispose(dialog
);
550 m_fileName
= wxFileNameFromPath(m_path
);
551 m_fileNames
.Add(m_fileName
);
554 // set these to the first hit
556 m_fileName
= wxFileNameFromPath(m_path
);
557 m_dir
= wxPathOnly(m_path
);
560 ::NavDisposeReply(&navReply
);
561 ::NavDialogDispose(dialog
);
563 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
566 bool wxFileDialog::SupportsExtraControl() const
571 #endif // wxUSE_FILEDLG