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 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
34 #include "wx/mac/private.h"
36 #include <Navigation.h>
38 #include "MoreFilesX.h"
40 extern bool gUseNavServices
;
42 // the data we need to pass to our standard file hook routine
43 // includes a pointer to the dialog, a pointer to the standard
44 // file reply record (so we can inspect the current selection)
45 // and a copy of the "previous" file spec of the reply record
46 // so we can see if the selection has changed
48 struct OpenUserDataRec
{
52 wxArrayString extensions
;
53 wxArrayLong filtermactypes
;
54 wxString defaultLocation
;
55 CFArrayRef menuitems
;
58 typedef struct OpenUserDataRec
59 OpenUserDataRec
, *OpenUserDataRecPtr
;
61 static pascal void NavEventProc(
62 NavEventCallbackMessage inSelector
,
64 NavCallBackUserData ioUserData
);
66 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
70 NavEventCallbackMessage inSelector
,
72 NavCallBackUserData ioUserData
)
74 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
75 if (inSelector
== kNavCBEvent
) {
77 else if ( inSelector
== kNavCBStart
)
79 if (data
&& !(data
->defaultLocation
).IsEmpty())
81 // Set default location for the modern Navigation APIs
82 // Apple Technical Q&A 1151
84 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
85 AEDesc theLocation
= {typeNull
, NULL
};
86 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
87 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
90 NavMenuItemSpec menuItem
;
91 menuItem
.version
= kNavMenuItemSpecVersion
;
92 menuItem
.menuCreator
= 'WXNG';
93 menuItem
.menuType
= data
->currentfilter
;
94 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
95 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
97 else if ( inSelector
== kNavCBPopupMenuSelect
)
99 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
100 const size_t numFilters
= data
->extensions
.GetCount();
102 if ( menu
->menuType
< numFilters
)
104 data
->currentfilter
= menu
->menuType
;
105 if ( data
->saveMode
)
107 int i
= menu
->menuType
;
108 wxString extension
= data
->extensions
[i
].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
) ;
128 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
130 myData
->menuitems
= NULL
;
131 myData
->currentfilter
= 0 ;
132 myData
->saveMode
= false ;
134 if ( filter
&& filter
[0] )
136 wxString
filter2(filter
) ;
140 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
142 if( filter2
.GetChar(i
) == wxT('|') )
145 myData
->name
.Add( current
) ;
148 myData
->extensions
.Add( current
.MakeUpper() ) ;
152 current
= wxEmptyString
;
156 current
+= filter2
.GetChar(i
) ;
159 // we allow for compatibility reason to have a single filter expression (like *.*) without
160 // an explanatory text, in that case the first part is name and extension at the same time
162 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
163 if ( current
.IsEmpty() )
164 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
166 myData
->extensions
.Add( current
.MakeUpper() ) ;
167 if ( filterIndex
== 0 || isName
)
168 myData
->name
.Add( current
.MakeUpper() ) ;
172 const size_t extCount
= myData
->extensions
.GetCount();
173 for ( size_t i
= 0 ; i
< extCount
; i
++ )
177 wxString extension
= myData
->extensions
[i
];
179 if (extension
.GetChar(0) == '*')
180 extension
= extension
.Mid(1); // Remove leading *
182 if (extension
.GetChar(0) == '.')
184 extension
= extension
.Mid(1); // Remove leading .
187 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
189 myData
->filtermactypes
.Add( (OSType
)fileType
);
193 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
199 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
201 wxString
file(filename
) ;
204 if ( data
->extensions
.GetCount() > 0 )
206 //for ( int i = 0 ; i < data->numfilters ; ++i )
207 int i
= data
->currentfilter
;
208 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
212 if ( type
== (OSType
)data
->filtermactypes
[i
] )
215 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
216 while( tokenizer
.HasMoreTokens() )
218 wxString extension
= tokenizer
.GetNextToken() ;
219 if ( extension
.GetChar(0) == '*' )
220 extension
= extension
.Mid(1) ;
222 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
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(wxWindow
*parent
, const wxString
& message
,
262 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
263 long style
, const wxPoint
& pos
)
264 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
266 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
269 pascal Boolean
CrossPlatformFilterCallback (
273 NavFilterModes filterMode
277 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
279 if (filterMode
== kNavFilteringBrowserList
)
281 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
282 if ( !theInfo
->isFolder
)
284 if (theItem
->descriptorType
== typeFSS
)
287 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
288 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
289 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
291 else if ( theItem
->descriptorType
== typeFSRef
)
294 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
295 wxString file
= wxMacFSRefToPath( &fsref
) ;
296 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
304 int wxFileDialog::ShowModal()
307 NavDialogCreationOptions dialogCreateOptions
;
308 // set default options
309 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
311 // this was always unset in the old code
312 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
314 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
315 dialogCreateOptions
.windowTitle
= message
;
317 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
318 dialogCreateOptions
.saveFileName
= defaultFileName
;
322 NavObjectFilterUPP navFilterUPP
= NULL
;
323 CFArrayRef cfArray
= NULL
; // for popupExtension
324 OpenUserDataRec myData
;
325 myData
.defaultLocation
= m_dir
;
327 MakeUserDataRec(&myData
, m_wildCard
);
328 myData
.currentfilter
= m_filterIndex
;
329 size_t numFilters
= myData
.extensions
.GetCount();
332 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
333 numFilters
, &kCFTypeArrayCallBacks
) ;
334 dialogCreateOptions
.popupExtension
= popup
;
335 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
336 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
338 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
342 if (m_dialogStyle
& wxSAVE
)
344 myData
.saveMode
= true;
348 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
350 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
351 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
353 // The extension is important
355 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
357 #if TARGET_API_MAC_OSX
358 if (!(m_dialogStyle
& wxOVERWRITE_PROMPT
))
360 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
363 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
364 // Suppresses the 'Default' (top) menu item
365 kNavGenericSignature
, kNavGenericSignature
,
366 sStandardNavEventFilter
,
367 &myData
, // for defaultLocation
373 //let people select bundles/programs in dialogs
374 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
376 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
377 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
378 NULL
, // NavTypeListHandle
379 sStandardNavEventFilter
,
380 NULL
, // NavPreviewUPP
382 (void *) &myData
, // inClientData
387 err
= ::NavDialogRun(dialog
);
389 // clean up filter related data, etc.
391 ::DisposeNavObjectFilterUPP(navFilterUPP
);
393 ::CFRelease(cfArray
);
398 NavReplyRecord navReply
;
399 err
= ::NavDialogGetReply(dialog
, &navReply
);
400 if (err
== noErr
&& navReply
.validRecord
)
402 AEKeyword theKeyword
;
408 m_filterIndex
= myData
.currentfilter
;
411 ::AECountItems(&navReply
.selection
, &count
);
412 for (long i
= 1; i
<= count
; ++i
)
414 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
415 &theFSRef
, sizeof(theFSRef
), &actualSize
);
419 if (m_dialogStyle
& wxSAVE
)
420 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
) ;
422 thePath
= wxMacFSRefToPath( &theFSRef
) ;
426 ::NavDisposeReply(&navReply
);
431 m_fileName
= wxFileNameFromPath(m_path
);
432 m_fileNames
.Add(m_fileName
);
434 // set these to the first hit
436 m_fileName
= wxFileNameFromPath(m_path
);
437 m_dir
= wxPathOnly(m_path
);
439 ::NavDisposeReply(&navReply
);
441 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;