1 /////////////////////////////////////////////////////////////////////////////
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/dialog.h"
17 #include "wx/filedlg.h"
19 #include "wx/tokenzr.h"
20 #include "wx/filename.h"
23 #include "PLStringFuncs.h"
26 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
30 #include "wx/mac/private.h"
33 #include <Navigation.h>
36 #include "MoreFilesX.h"
38 extern bool gUseNavServices
;
40 // the data we need to pass to our standard file hook routine
41 // includes a pointer to the dialog, a pointer to the standard
42 // file reply record (so we can inspect the current selection)
43 // and a copy of the "previous" file spec of the reply record
44 // so we can see if the selection has changed
46 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
);
68 NavEventCallbackMessage inSelector
,
70 NavCallBackUserData ioUserData
)
72 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
73 if (inSelector
== kNavCBEvent
) {
75 else if ( inSelector
== kNavCBStart
)
77 if (data
&& !(data
->defaultLocation
).IsEmpty())
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
) ;
126 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
128 myData
->menuitems
= NULL
;
129 myData
->currentfilter
= 0 ;
130 myData
->saveMode
= false ;
132 if ( filter
&& filter
[0] )
134 wxString
filter2(filter
) ;
138 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
140 if( filter2
.GetChar(i
) == wxT('|') )
143 myData
->name
.Add( current
) ;
146 myData
->extensions
.Add( current
.MakeUpper() ) ;
150 current
= wxEmptyString
;
154 current
+= filter2
.GetChar(i
) ;
157 // we allow for compatibility reason to have a single filter expression (like *.*) without
158 // an explanatory text, in that case the first part is name and extension at the same time
160 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
161 if ( current
.IsEmpty() )
162 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
164 myData
->extensions
.Add( current
.MakeUpper() ) ;
165 if ( filterIndex
== 0 || isName
)
166 myData
->name
.Add( current
.MakeUpper() ) ;
170 const size_t extCount
= myData
->extensions
.GetCount();
171 for ( size_t i
= 0 ; i
< extCount
; i
++ )
175 wxString extension
= myData
->extensions
[i
];
177 if (extension
.GetChar(0) == '*')
178 extension
= extension
.Mid(1); // Remove leading *
180 if (extension
.GetChar(0) == '.')
182 extension
= extension
.Mid(1); // Remove leading .
185 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
187 myData
->filtermactypes
.Add( (OSType
)fileType
);
191 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
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
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(wxWindow
*parent
, const wxString
& message
,
260 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
261 long style
, const wxPoint
& pos
)
262 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
264 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
267 pascal Boolean
CrossPlatformFilterCallback (
271 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
;
306 // set default options
307 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
309 // this was always unset in the old code
310 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
312 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
313 dialogCreateOptions
.windowTitle
= message
;
315 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
316 dialogCreateOptions
.saveFileName
= defaultFileName
;
320 NavObjectFilterUPP navFilterUPP
= NULL
;
321 CFArrayRef cfArray
= NULL
; // for popupExtension
322 OpenUserDataRec myData
;
323 myData
.defaultLocation
= m_dir
;
325 MakeUserDataRec(&myData
, m_wildCard
);
326 myData
.currentfilter
= m_filterIndex
;
327 size_t numFilters
= myData
.extensions
.GetCount();
330 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
331 numFilters
, &kCFTypeArrayCallBacks
) ;
332 dialogCreateOptions
.popupExtension
= popup
;
333 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
334 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
336 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
340 if (m_dialogStyle
& wxSAVE
)
342 myData
.saveMode
= true;
346 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
348 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
349 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
351 // The extension is important
353 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
355 #if TARGET_API_MAC_OSX
356 if (!(m_dialogStyle
& wxOVERWRITE_PROMPT
))
358 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
361 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
362 // Suppresses the 'Default' (top) menu item
363 kNavGenericSignature
, kNavGenericSignature
,
364 sStandardNavEventFilter
,
365 &myData
, // for defaultLocation
371 //let people select bundles/programs in dialogs
372 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
374 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
375 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
376 NULL
, // NavTypeListHandle
377 sStandardNavEventFilter
,
378 NULL
, // NavPreviewUPP
380 (void *) &myData
, // inClientData
385 err
= ::NavDialogRun(dialog
);
387 // clean up filter related data, etc.
389 ::DisposeNavObjectFilterUPP(navFilterUPP
);
391 ::CFRelease(cfArray
);
396 NavReplyRecord navReply
;
397 err
= ::NavDialogGetReply(dialog
, &navReply
);
398 if (err
== noErr
&& navReply
.validRecord
)
400 AEKeyword theKeyword
;
406 m_filterIndex
= myData
.currentfilter
;
409 ::AECountItems(&navReply
.selection
, &count
);
410 for (long i
= 1; i
<= count
; ++i
)
412 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
413 &theFSRef
, sizeof(theFSRef
), &actualSize
);
417 if (m_dialogStyle
& wxSAVE
)
418 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
) ;
420 thePath
= wxMacFSRefToPath( &theFSRef
) ;
424 ::NavDisposeReply(&navReply
);
429 m_fileName
= wxFileNameFromPath(m_path
);
430 m_fileNames
.Add(m_fileName
);
432 // set these to the first hit
434 m_fileName
= wxFileNameFromPath(m_path
);
435 m_dir
= wxPathOnly(m_path
);
437 ::NavDisposeReply(&navReply
);
439 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;