1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/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
43 struct OpenUserDataRec
48 wxArrayString extensions
;
49 wxArrayLong filtermactypes
;
50 wxString defaultLocation
;
51 CFArrayRef menuitems
;
54 typedef struct OpenUserDataRec
55 OpenUserDataRec
, *OpenUserDataRecPtr
;
57 static pascal void NavEventProc(
58 NavEventCallbackMessage inSelector
,
60 NavCallBackUserData ioUserData
);
62 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
64 static pascal void NavEventProc(
65 NavEventCallbackMessage inSelector
,
67 NavCallBackUserData ioUserData
)
69 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
70 if (inSelector
== kNavCBEvent
)
73 else if ( inSelector
== kNavCBStart
)
75 if (data
&& !(data
->defaultLocation
).empty())
77 // Set default location for the modern Navigation APIs
78 // Apple Technical Q&A 1151
80 wxMacPathToFSRef(data
->defaultLocation
, &theFile
);
81 AEDesc theLocation
= { typeNull
, NULL
};
82 if (noErr
== ::AECreateDesc(typeFSRef
, &theFile
, sizeof(FSRef
), &theLocation
))
83 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
86 NavMenuItemSpec menuItem
;
87 menuItem
.version
= kNavMenuItemSpecVersion
;
88 menuItem
.menuCreator
= 'WXNG';
89 menuItem
.menuType
= data
->currentfilter
;
90 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
91 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
93 else if ( inSelector
== kNavCBPopupMenuSelect
)
95 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
96 const size_t numFilters
= data
->extensions
.GetCount();
98 if ( menu
->menuType
< numFilters
)
100 data
->currentfilter
= menu
->menuType
;
101 if ( data
->saveMode
)
103 int i
= menu
->menuType
;
105 // isolate the first extension string
106 wxString firstExtension
= data
->extensions
[i
].BeforeFirst('|').BeforeFirst(';');
108 wxString extension
= firstExtension
.AfterLast('.') ;
109 extension
.MakeLower() ;
112 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
113 sfilename
= cfString
.AsString() ;
115 int pos
= sfilename
.Find('.', true) ;
116 if ( pos
!= wxNOT_FOUND
)
118 sfilename
= sfilename
.Left(pos
+1)+extension
;
119 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
120 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
127 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
129 myData
->menuitems
= NULL
;
130 myData
->currentfilter
= 0 ;
131 myData
->saveMode
= false ;
133 if ( !filter
.empty() )
135 wxString
filter2(filter
) ;
140 for ( unsigned int i
= 0; i
< filter2
.length() ; i
++ )
142 if ( filter2
.GetChar(i
) == wxT('|') )
146 myData
->name
.Add( current
) ;
150 myData
->extensions
.Add( current
.MakeUpper() ) ;
155 current
= wxEmptyString
;
159 current
+= filter2
.GetChar(i
) ;
162 // we allow for compatibility reason to have a single filter expression (like *.*) without
163 // an explanatory text, in that case the first part is name and extension at the same time
165 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
166 if ( current
.empty() )
167 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
169 myData
->extensions
.Add( current
.MakeUpper() ) ;
170 if ( filterIndex
== 0 || isName
)
171 myData
->name
.Add( current
.MakeUpper() ) ;
175 const size_t extCount
= myData
->extensions
.GetCount();
176 for ( size_t i
= 0 ; i
< extCount
; i
++ )
178 wxUint32 fileType
, creator
;
179 wxString extension
= myData
->extensions
[i
];
181 // Remove leading '*'
182 if (extension
.length() && (extension
.GetChar(0) == '*'))
183 extension
= extension
.Mid( 1 );
185 // Remove leading '.'
186 if (extension
.length() && (extension
.GetChar(0) == '.'))
187 extension
= extension
.Mid( 1 );
189 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
190 myData
->filtermactypes
.Add( (OSType
)fileType
);
192 myData
->filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
197 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
199 wxString
file(filename
) ;
202 if ( data
->extensions
.GetCount() > 0 )
204 //for ( int i = 0 ; i < data->numfilters ; ++i )
205 int i
= data
->currentfilter
;
206 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
210 if ( type
== (OSType
)data
->filtermactypes
[i
] )
213 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
214 while ( tokenizer
.HasMoreTokens() )
216 wxString extension
= tokenizer
.GetNextToken() ;
217 if ( extension
.GetChar(0) == '*' )
218 extension
= extension
.Mid(1) ;
220 if ( file
.length() >= extension
.length() && extension
== file
.Right(extension
.length() ) )
231 #if !TARGET_API_MAC_OSX
232 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
234 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
235 // return true if this item is invisible or a file
240 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
241 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
243 // because the semantics of the filter proc are "true means don't show
244 // it" we need to invert the result that we return
251 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
252 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
261 wxFileDialog::wxFileDialog(
262 wxWindow
*parent
, const wxString
& message
,
263 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
264 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
265 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
267 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
270 pascal Boolean
CrossPlatformFilterCallback(
274 NavFilterModes filterMode
)
277 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
279 if (filterMode
== kNavFilteringBrowserList
)
281 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
282 if ( !theInfo
->isFolder
)
284 AECoerceDesc (theItem
, typeFSRef
, theItem
);
287 if ( AEGetDescData (theItem
, &fsref
, sizeof (FSRef
)) == noErr
)
289 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
290 wxString file
= wxMacFSRefToPath( &fsref
) ;
291 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
299 int wxFileDialog::ShowModal()
302 NavDialogCreationOptions dialogCreateOptions
;
304 // set default options
305 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
307 // this was always unset in the old code
308 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
310 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
311 dialogCreateOptions
.windowTitle
= message
;
313 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
314 dialogCreateOptions
.saveFileName
= defaultFileName
;
318 NavObjectFilterUPP navFilterUPP
= NULL
;
319 OpenUserDataRec myData
;
320 myData
.defaultLocation
= m_dir
;
322 MakeUserDataRec(&myData
, m_wildCard
);
323 myData
.currentfilter
= m_filterIndex
;
324 size_t numFilters
= myData
.extensions
.GetCount();
327 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
328 numFilters
, &kCFTypeArrayCallBacks
) ;
329 dialogCreateOptions
.popupExtension
= popup
;
330 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
331 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
333 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
337 if (HasFdFlag(wxFD_SAVE
))
339 myData
.saveMode
= true;
341 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
342 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
344 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
346 // The extension is important
348 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
350 #if TARGET_API_MAC_OSX
351 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
352 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
355 err
= ::NavCreatePutFileDialog(
356 &dialogCreateOptions
,
357 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
358 kNavGenericSignature
,
359 sStandardNavEventFilter
,
360 &myData
, // for defaultLocation
365 // let the user select bundles/programs in dialogs
366 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
368 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
369 err
= ::NavCreateGetFileDialog(
370 &dialogCreateOptions
,
371 NULL
, // NavTypeListHandle
372 sStandardNavEventFilter
,
373 NULL
, // NavPreviewUPP
375 (void *) &myData
, // inClientData
380 err
= ::NavDialogRun(dialog
);
382 // clean up filter related data, etc.
384 ::DisposeNavObjectFilterUPP(navFilterUPP
);
389 NavReplyRecord navReply
;
390 err
= ::NavDialogGetReply(dialog
, &navReply
);
391 if (err
== noErr
&& navReply
.validRecord
)
393 AEKeyword theKeyword
;
400 m_filterIndex
= myData
.currentfilter
;
401 ::AECountItems( &navReply
.selection
, &count
);
402 for (long i
= 1; i
<= count
; ++i
)
405 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
406 &theFSRef
, sizeof(theFSRef
), &actualSize
);
410 if (HasFdFlag(wxFD_SAVE
))
411 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
413 thePath
= wxMacFSRefToPath( &theFSRef
);
417 ::NavDisposeReply(&navReply
);
423 m_fileName
= wxFileNameFromPath(m_path
);
424 m_fileNames
.Add(m_fileName
);
427 // set these to the first hit
429 m_fileName
= wxFileNameFromPath(m_path
);
430 m_dir
= wxPathOnly(m_path
);
433 ::NavDisposeReply(&navReply
);
435 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
438 #endif // wxUSE_FILEDLG