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"
14 #include "wx/filedlg.h"
20 #include "wx/dialog.h"
23 #include "wx/tokenzr.h"
24 #include "wx/filename.h"
26 #include "wx/mac/private.h"
29 #include <Navigation.h>
30 #include "PLStringFuncs.h"
33 #include "MoreFilesX.h"
35 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
37 extern bool gUseNavServices
;
39 // the data we need to pass to our standard file hook routine
40 // includes a pointer to the dialog, a pointer to the standard
41 // file reply record (so we can inspect the current selection)
42 // and a copy of the "previous" file spec of the reply record
43 // so we can see if the selection has changed
45 struct OpenUserDataRec
50 wxArrayString extensions
;
51 wxArrayLong filtermactypes
;
52 wxString defaultLocation
;
53 CFArrayRef menuitems
;
56 typedef struct OpenUserDataRec
57 OpenUserDataRec
, *OpenUserDataRecPtr
;
59 static pascal void NavEventProc(
60 NavEventCallbackMessage inSelector
,
62 NavCallBackUserData ioUserData
);
64 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
66 static pascal void NavEventProc(
67 NavEventCallbackMessage inSelector
,
69 NavCallBackUserData ioUserData
)
71 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
72 if (inSelector
== kNavCBEvent
)
75 else if ( inSelector
== kNavCBStart
)
77 if (data
&& !(data
->defaultLocation
).empty())
79 // Set default location for the modern Navigation APIs
80 // Apple Technical Q&A 1151
82 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
83 AEDesc theLocation
= { typeNull
, NULL
};
84 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
85 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
88 NavMenuItemSpec menuItem
;
89 menuItem
.version
= kNavMenuItemSpecVersion
;
90 menuItem
.menuCreator
= 'WXNG';
91 menuItem
.menuType
= data
->currentfilter
;
92 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
93 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
95 else if ( inSelector
== kNavCBPopupMenuSelect
)
97 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
98 const size_t numFilters
= data
->extensions
.GetCount();
100 if ( menu
->menuType
< numFilters
)
102 data
->currentfilter
= menu
->menuType
;
103 if ( data
->saveMode
)
105 int i
= menu
->menuType
;
106 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
107 extension
.MakeLower() ;
110 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
111 sfilename
= cfString
.AsString() ;
113 int pos
= sfilename
.Find('.', true) ;
114 if ( pos
!= wxNOT_FOUND
)
116 sfilename
= sfilename
.Left(pos
+1)+extension
;
117 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
118 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
125 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
127 myData
->menuitems
= NULL
;
128 myData
->currentfilter
= 0 ;
129 myData
->saveMode
= false ;
131 if ( filter
&& filter
[0] )
133 wxString
filter2(filter
) ;
138 for ( unsigned int i
= 0; i
< filter2
.length() ; i
++ )
140 if ( filter2
.GetChar(i
) == wxT('|') )
144 myData
->name
.Add( current
) ;
148 myData
->extensions
.Add( current
.MakeUpper() ) ;
153 current
= wxEmptyString
;
157 current
+= filter2
.GetChar(i
) ;
160 // we allow for compatibility reason to have a single filter expression (like *.*) without
161 // an explanatory text, in that case the first part is name and extension at the same time
163 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
164 if ( current
.empty() )
165 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
167 myData
->extensions
.Add( current
.MakeUpper() ) ;
168 if ( filterIndex
== 0 || isName
)
169 myData
->name
.Add( current
.MakeUpper() ) ;
173 const size_t extCount
= myData
->extensions
.GetCount();
174 for ( size_t i
= 0 ; i
< extCount
; i
++ )
176 wxUint32 fileType
, creator
;
177 wxString extension
= myData
->extensions
[i
];
179 // Remove leading '*'
180 if (extension
.length() && (extension
.GetChar(0) == '*'))
181 extension
= extension
.Mid( 1 );
183 // Remove leading '.'
184 if (extension
.length() && (extension
.GetChar(0) == '.'))
185 extension
= extension
.Mid( 1 );
187 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
188 myData
->filtermactypes
.Add( (OSType
)fileType
);
190 myData
->filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
195 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
197 wxString
file(filename
) ;
200 if ( data
->extensions
.GetCount() > 0 )
202 //for ( int i = 0 ; i < data->numfilters ; ++i )
203 int i
= data
->currentfilter
;
204 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
208 if ( type
== (OSType
)data
->filtermactypes
[i
] )
211 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
212 while ( tokenizer
.HasMoreTokens() )
214 wxString extension
= tokenizer
.GetNextToken() ;
215 if ( extension
.GetChar(0) == '*' )
216 extension
= extension
.Mid(1) ;
218 if ( file
.length() >= extension
.length() && extension
== file
.Right(extension
.length() ) )
229 #if !TARGET_API_MAC_OSX
230 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
232 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
233 // return true if this item is invisible or a file
238 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
239 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
241 // because the semantics of the filter proc are "true means don't show
242 // it" we need to invert the result that we return
249 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
250 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
259 wxFileDialog::wxFileDialog(
260 wxWindow
*parent
, const wxString
& message
,
261 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
262 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
263 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
265 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
268 pascal Boolean
CrossPlatformFilterCallback(
272 NavFilterModes filterMode
)
275 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
277 if (filterMode
== kNavFilteringBrowserList
)
279 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
280 if ( !theInfo
->isFolder
)
282 if (theItem
->descriptorType
== typeFSS
)
285 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
286 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
287 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
289 else if ( theItem
->descriptorType
== typeFSRef
)
292 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
293 wxString file
= wxMacFSRefToPath( &fsref
) ;
294 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
302 int wxFileDialog::ShowModal()
305 NavDialogCreationOptions dialogCreateOptions
;
307 // set default options
308 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
310 // this was always unset in the old code
311 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
313 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
314 dialogCreateOptions
.windowTitle
= message
;
316 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
317 dialogCreateOptions
.saveFileName
= defaultFileName
;
321 NavObjectFilterUPP navFilterUPP
= NULL
;
322 CFArrayRef cfArray
= NULL
; // for popupExtension
323 OpenUserDataRec myData
;
324 myData
.defaultLocation
= m_dir
;
326 MakeUserDataRec(&myData
, m_wildCard
);
327 myData
.currentfilter
= m_filterIndex
;
328 size_t numFilters
= myData
.extensions
.GetCount();
331 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
332 numFilters
, &kCFTypeArrayCallBacks
) ;
333 dialogCreateOptions
.popupExtension
= popup
;
334 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
335 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
337 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
341 if (HasFlag(wxFD_SAVE
))
343 myData
.saveMode
= true;
345 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
346 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
348 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
350 // The extension is important
352 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
354 #if TARGET_API_MAC_OSX
355 if (!(m_windowStyle
& wxFD_OVERWRITE_PROMPT
))
356 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
359 err
= ::NavCreatePutFileDialog(
360 &dialogCreateOptions
,
361 kNavGenericSignature
, // Suppresses the 'Default' (top) menu item
362 kNavGenericSignature
,
363 sStandardNavEventFilter
,
364 &myData
, // for defaultLocation
369 // let the user select bundles/programs in dialogs
370 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
372 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
373 err
= ::NavCreateGetFileDialog(
374 &dialogCreateOptions
,
375 NULL
, // NavTypeListHandle
376 sStandardNavEventFilter
,
377 NULL
, // NavPreviewUPP
379 (void *) &myData
, // inClientData
384 err
= ::NavDialogRun(dialog
);
386 // clean up filter related data, etc.
388 ::DisposeNavObjectFilterUPP(navFilterUPP
);
390 ::CFRelease(cfArray
);
395 NavReplyRecord navReply
;
396 err
= ::NavDialogGetReply(dialog
, &navReply
);
397 if (err
== noErr
&& navReply
.validRecord
)
399 AEKeyword theKeyword
;
406 m_filterIndex
= myData
.currentfilter
;
407 ::AECountItems( &navReply
.selection
, &count
);
408 for (long i
= 1; i
<= count
; ++i
)
411 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
412 &theFSRef
, sizeof(theFSRef
), &actualSize
);
416 if (HasFlag(wxFD_SAVE
))
417 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
419 thePath
= wxMacFSRefToPath( &theFSRef
);
423 ::NavDisposeReply(&navReply
);
429 m_fileName
= wxFileNameFromPath(m_path
);
430 m_fileNames
.Add(m_fileName
);
433 // set these to the first hit
435 m_fileName
= wxFileNameFromPath(m_path
);
436 m_dir
= wxPathOnly(m_path
);
439 ::NavDisposeReply(&navReply
);
441 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;