1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlg.h"
16 #include "wx/wxprec.h"
20 #include "wx/dialog.h"
21 #include "wx/filedlg.h"
23 #include "wx/tokenzr.h"
24 #include "wx/filename.h"
27 #include "PLStringFuncs.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
36 #include "wx/mac/private.h"
38 #include <Navigation.h>
40 #include "MoreFilesX.h"
42 extern bool gUseNavServices
;
44 // the data we need to pass to our standard file hook routine
45 // includes a pointer to the dialog, a pointer to the standard
46 // file reply record (so we can inspect the current selection)
47 // and a copy of the "previous" file spec of the reply record
48 // so we can see if the selection has changed
50 struct OpenUserDataRec
{
54 wxArrayString extensions
;
55 wxArrayLong filtermactypes
;
56 wxString defaultLocation
;
57 CFArrayRef menuitems
;
60 typedef struct OpenUserDataRec
61 OpenUserDataRec
, *OpenUserDataRecPtr
;
63 static pascal void NavEventProc(
64 NavEventCallbackMessage inSelector
,
66 NavCallBackUserData ioUserData
);
68 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
72 NavEventCallbackMessage inSelector
,
74 NavCallBackUserData ioUserData
)
76 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
77 if (inSelector
== kNavCBEvent
) {
79 else if ( inSelector
== kNavCBStart
)
81 if (data
&& !(data
->defaultLocation
).IsEmpty())
83 // Set default location for the modern Navigation APIs
84 // Apple Technical Q&A 1151
86 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
87 AEDesc theLocation
= {typeNull
, NULL
};
88 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
89 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
92 NavMenuItemSpec menuItem
;
93 menuItem
.version
= kNavMenuItemSpecVersion
;
94 menuItem
.menuCreator
= 'WXNG';
95 menuItem
.menuType
= data
->currentfilter
;
96 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
97 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
99 else if ( inSelector
== kNavCBPopupMenuSelect
)
101 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
102 const size_t numFilters
= data
->extensions
.GetCount();
104 if ( menu
->menuType
< numFilters
)
106 data
->currentfilter
= menu
->menuType
;
107 if ( data
->saveMode
)
109 int i
= menu
->menuType
;
110 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
111 extension
.MakeLower() ;
114 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
115 sfilename
= cfString
.AsString() ;
117 int pos
= sfilename
.Find('.', true) ;
118 if ( pos
!= wxNOT_FOUND
)
120 sfilename
= sfilename
.Left(pos
+1)+extension
;
121 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
122 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
130 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
132 myData
->menuitems
= NULL
;
133 myData
->currentfilter
= 0 ;
134 myData
->saveMode
= false ;
136 if ( filter
&& filter
[0] )
138 wxString
filter2(filter
) ;
142 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
144 if( filter2
.GetChar(i
) == wxT('|') )
147 myData
->name
.Add( current
) ;
150 myData
->extensions
.Add( current
.MakeUpper() ) ;
154 current
= wxEmptyString
;
158 current
+= filter2
.GetChar(i
) ;
161 // we allow for compatibility reason to have a single filter expression (like *.*) without
162 // an explanatory text, in that case the first part is name and extension at the same time
164 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
165 if ( current
.IsEmpty() )
166 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
168 myData
->extensions
.Add( current
.MakeUpper() ) ;
169 if ( filterIndex
== 0 || isName
)
170 myData
->name
.Add( current
.MakeUpper() ) ;
174 const size_t extCount
= myData
->extensions
.GetCount();
175 for ( size_t i
= 0 ; i
< extCount
; i
++ )
179 wxString extension
= myData
->extensions
[i
];
181 if (extension
.GetChar(0) == '*')
182 extension
= extension
.Mid(1); // Remove leading *
184 if (extension
.GetChar(0) == '.')
186 extension
= extension
.Mid(1); // Remove leading .
189 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
191 myData
->filtermactypes
.Add( (OSType
)fileType
);
195 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
201 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
203 wxString
file(filename
) ;
206 if ( data
->extensions
.GetCount() > 0 )
208 //for ( int i = 0 ; i < data->numfilters ; ++i )
209 int i
= data
->currentfilter
;
210 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
214 if ( type
== (OSType
)data
->filtermactypes
[i
] )
217 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
218 while( tokenizer
.HasMoreTokens() )
220 wxString extension
= tokenizer
.GetNextToken() ;
221 if ( extension
.GetChar(0) == '*' )
222 extension
= extension
.Mid(1) ;
224 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
233 #if !TARGET_API_MAC_OSX
234 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
236 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
237 // return true if this item is invisible or a file
242 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
243 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
245 // because the semantics of the filter proc are "true means don't show
246 // it" we need to invert the result that we return
253 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
254 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
263 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
264 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
265 long style
, const wxPoint
& pos
)
266 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
268 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
271 pascal Boolean
CrossPlatformFilterCallback (
275 NavFilterModes filterMode
279 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
281 if (filterMode
== kNavFilteringBrowserList
)
283 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
284 if ( !theInfo
->isFolder
)
286 if (theItem
->descriptorType
== typeFSS
)
289 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
290 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
291 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
293 else if ( theItem
->descriptorType
== typeFSRef
)
296 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
297 wxString file
= wxMacFSRefToPath( &fsref
) ;
298 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
306 int wxFileDialog::ShowModal()
309 NavDialogCreationOptions dialogCreateOptions
;
310 // set default options
311 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
313 // this was always unset in the old code
314 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
316 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
317 dialogCreateOptions
.windowTitle
= message
;
319 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
320 dialogCreateOptions
.saveFileName
= defaultFileName
;
324 NavObjectFilterUPP navFilterUPP
= NULL
;
325 CFArrayRef cfArray
= NULL
; // for popupExtension
326 OpenUserDataRec myData
;
327 myData
.defaultLocation
= m_dir
;
329 MakeUserDataRec(&myData
, m_wildCard
);
330 myData
.currentfilter
= m_filterIndex
;
331 size_t numFilters
= myData
.extensions
.GetCount();
334 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
335 numFilters
, &kCFTypeArrayCallBacks
) ;
336 dialogCreateOptions
.popupExtension
= popup
;
337 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
338 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
340 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
344 if (m_dialogStyle
& wxSAVE
)
346 myData
.saveMode
= true;
350 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
352 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
353 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
355 // The extension is important
357 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
359 #if TARGET_API_MAC_OSX
360 if (!(m_dialogStyle
& wxOVERWRITE_PROMPT
))
362 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
365 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
366 // Suppresses the 'Default' (top) menu item
367 kNavGenericSignature
, kNavGenericSignature
,
368 sStandardNavEventFilter
,
369 &myData
, // for defaultLocation
375 //let people select bundles/programs in dialogs
376 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
378 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
379 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
380 NULL
, // NavTypeListHandle
381 sStandardNavEventFilter
,
382 NULL
, // NavPreviewUPP
384 (void *) &myData
, // inClientData
389 err
= ::NavDialogRun(dialog
);
391 // clean up filter related data, etc.
393 ::DisposeNavObjectFilterUPP(navFilterUPP
);
395 ::CFRelease(cfArray
);
400 NavReplyRecord navReply
;
401 err
= ::NavDialogGetReply(dialog
, &navReply
);
402 if (err
== noErr
&& navReply
.validRecord
)
404 AEKeyword theKeyword
;
410 m_filterIndex
= myData
.currentfilter
;
413 ::AECountItems(&navReply
.selection
, &count
);
414 for (long i
= 1; i
<= count
; ++i
)
416 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
417 &theFSRef
, sizeof(theFSRef
), &actualSize
);
421 if (m_dialogStyle
& wxSAVE
)
422 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
) ;
424 thePath
= wxMacFSRefToPath( &theFSRef
) ;
428 ::NavDisposeReply(&navReply
);
433 m_fileName
= wxFileNameFromPath(m_path
);
434 m_fileNames
.Add(m_fileName
);
436 // set these to the first hit
438 m_fileName
= wxFileNameFromPath(m_path
);
439 m_dir
= wxPathOnly(m_path
);
441 ::NavDisposeReply(&navReply
);
443 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;