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"
22 #include "wx/mac/private.h"
25 #include <Navigation.h>
26 #include "PLStringFuncs.h"
29 #include "MoreFilesX.h"
31 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
33 extern bool gUseNavServices
;
35 // the data we need to pass to our standard file hook routine
36 // includes a pointer to the dialog, a pointer to the standard
37 // file reply record (so we can inspect the current selection)
38 // and a copy of the "previous" file spec of the reply record
39 // so we can see if the selection has changed
41 struct OpenUserDataRec
46 wxArrayString extensions
;
47 wxArrayLong filtermactypes
;
48 wxString defaultLocation
;
49 CFArrayRef menuitems
;
52 typedef struct OpenUserDataRec
53 OpenUserDataRec
, *OpenUserDataRecPtr
;
55 static pascal void NavEventProc(
56 NavEventCallbackMessage inSelector
,
58 NavCallBackUserData ioUserData
);
60 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
62 static pascal void NavEventProc(
63 NavEventCallbackMessage inSelector
,
65 NavCallBackUserData ioUserData
)
67 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
68 if (inSelector
== kNavCBEvent
)
71 else if ( inSelector
== kNavCBStart
)
73 if (data
&& !(data
->defaultLocation
).IsEmpty())
75 // Set default location for the modern Navigation APIs
76 // Apple Technical Q&A 1151
78 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
79 AEDesc theLocation
= { typeNull
, NULL
};
80 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
81 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
84 NavMenuItemSpec menuItem
;
85 menuItem
.version
= kNavMenuItemSpecVersion
;
86 menuItem
.menuCreator
= 'WXNG';
87 menuItem
.menuType
= data
->currentfilter
;
88 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
89 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
91 else if ( inSelector
== kNavCBPopupMenuSelect
)
93 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
94 const size_t numFilters
= data
->extensions
.GetCount();
96 if ( menu
->menuType
< numFilters
)
98 data
->currentfilter
= menu
->menuType
;
101 int i
= menu
->menuType
;
102 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
103 extension
.MakeLower() ;
106 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
107 sfilename
= cfString
.AsString() ;
109 int pos
= sfilename
.Find('.', true) ;
110 if ( pos
!= wxNOT_FOUND
)
112 sfilename
= sfilename
.Left(pos
+1)+extension
;
113 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
114 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
121 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
123 myData
->menuitems
= NULL
;
124 myData
->currentfilter
= 0 ;
125 myData
->saveMode
= false ;
127 if ( filter
&& filter
[0] )
129 wxString
filter2(filter
) ;
134 for ( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
136 if ( filter2
.GetChar(i
) == wxT('|') )
140 myData
->name
.Add( current
) ;
144 myData
->extensions
.Add( current
.MakeUpper() ) ;
149 current
= wxEmptyString
;
153 current
+= filter2
.GetChar(i
) ;
156 // we allow for compatibility reason to have a single filter expression (like *.*) without
157 // an explanatory text, in that case the first part is name and extension at the same time
159 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
160 if ( current
.IsEmpty() )
161 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
163 myData
->extensions
.Add( current
.MakeUpper() ) ;
164 if ( filterIndex
== 0 || isName
)
165 myData
->name
.Add( current
.MakeUpper() ) ;
169 const size_t extCount
= myData
->extensions
.GetCount();
170 for ( size_t i
= 0 ; i
< extCount
; i
++ )
172 wxUint32 fileType
, creator
;
173 wxString extension
= myData
->extensions
[i
];
175 // Remove leading '*'
176 if (extension
.GetChar(0) == '*')
177 extension
= extension
.Mid( 1 );
179 // Remove leading '.'
180 if (extension
.GetChar(0) == '.')
181 extension
= extension
.Mid( 1 );
183 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
184 myData
->filtermactypes
.Add( (OSType
)fileType
);
186 myData
->filtermactypes
.Add( '****' ); // We'll fail safe if it's not recognized
191 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
193 wxString
file(filename
) ;
196 if ( data
->extensions
.GetCount() > 0 )
198 //for ( int i = 0 ; i < data->numfilters ; ++i )
199 int i
= data
->currentfilter
;
200 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
204 if ( type
== (OSType
)data
->filtermactypes
[i
] )
207 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
208 while ( tokenizer
.HasMoreTokens() )
210 wxString extension
= tokenizer
.GetNextToken() ;
211 if ( extension
.GetChar(0) == '*' )
212 extension
= extension
.Mid(1) ;
214 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
225 #if !TARGET_API_MAC_OSX
226 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
228 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
229 // return true if this item is invisible or a file
234 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
235 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
237 // because the semantics of the filter proc are "true means don't show
238 // it" we need to invert the result that we return
245 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
246 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
255 wxFileDialog::wxFileDialog(
256 wxWindow
*parent
, const wxString
& message
,
257 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
258 long style
, const wxPoint
& pos
)
259 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
261 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
264 pascal Boolean
CrossPlatformFilterCallback(
268 NavFilterModes filterMode
)
271 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
273 if (filterMode
== kNavFilteringBrowserList
)
275 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
276 if ( !theInfo
->isFolder
)
278 if (theItem
->descriptorType
== typeFSS
)
281 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
282 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
283 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
285 else if ( theItem
->descriptorType
== typeFSRef
)
288 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
289 wxString file
= wxMacFSRefToPath( &fsref
) ;
290 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
298 int wxFileDialog::ShowModal()
301 NavDialogCreationOptions dialogCreateOptions
;
303 // set default options
304 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
306 // this was always unset in the old code
307 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
309 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
310 dialogCreateOptions
.windowTitle
= message
;
312 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
313 dialogCreateOptions
.saveFileName
= defaultFileName
;
317 NavObjectFilterUPP navFilterUPP
= NULL
;
318 CFArrayRef cfArray
= NULL
; // for popupExtension
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 (m_dialogStyle
& wxSAVE
)
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_dialogStyle
& wxOVERWRITE_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
);
386 ::CFRelease(cfArray
);
391 NavReplyRecord navReply
;
392 err
= ::NavDialogGetReply(dialog
, &navReply
);
393 if (err
== noErr
&& navReply
.validRecord
)
395 AEKeyword theKeyword
;
402 m_filterIndex
= myData
.currentfilter
;
403 ::AECountItems( &navReply
.selection
, &count
);
404 for (long i
= 1; i
<= count
; ++i
)
407 &(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
408 &theFSRef
, sizeof(theFSRef
), &actualSize
);
412 if (m_dialogStyle
& wxSAVE
)
413 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
);
415 thePath
= wxMacFSRefToPath( &theFSRef
);
419 ::NavDisposeReply(&navReply
);
425 m_fileName
= wxFileNameFromPath(m_path
);
426 m_fileNames
.Add(m_fileName
);
429 // set these to the first hit
431 m_fileName
= wxFileNameFromPath(m_path
);
432 m_dir
= wxPathOnly(m_path
);
435 ::NavDisposeReply(&navReply
);
437 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;