1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
19 #include "wx/dialog.h"
20 #include "wx/filedlg.h"
22 #include "wx/tokenzr.h"
23 #include "wx/filename.h"
26 #include "PLStringFuncs.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
35 #include "wx/mac/private.h"
37 #include <Navigation.h>
39 #include "MoreFilesX.h"
41 extern bool gUseNavServices
;
43 // the data we need to pass to our standard file hook routine
44 // includes a pointer to the dialog, a pointer to the standard
45 // file reply record (so we can inspect the current selection)
46 // and a copy of the "previous" file spec of the reply record
47 // so we can see if the selection has changed
49 struct OpenUserDataRec
{
53 wxArrayString extensions
;
54 wxArrayLong filtermactypes
;
55 wxString defaultLocation
;
56 CFArrayRef menuitems
;
59 typedef struct OpenUserDataRec
60 OpenUserDataRec
, *OpenUserDataRecPtr
;
62 static pascal void NavEventProc(
63 NavEventCallbackMessage inSelector
,
65 NavCallBackUserData ioUserData
);
67 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
71 NavEventCallbackMessage inSelector
,
73 NavCallBackUserData ioUserData
)
75 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
76 if (inSelector
== kNavCBEvent
) {
78 else if ( inSelector
== kNavCBStart
)
80 if (data
&& !(data
->defaultLocation
).IsEmpty())
82 // Set default location for the modern Navigation APIs
83 // Apple Technical Q&A 1151
85 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
86 AEDesc theLocation
= {typeNull
, NULL
};
87 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
88 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
91 NavMenuItemSpec menuItem
;
92 menuItem
.version
= kNavMenuItemSpecVersion
;
93 menuItem
.menuCreator
= 'WXNG';
94 menuItem
.menuType
= data
->currentfilter
;
95 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
96 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
98 else if ( inSelector
== kNavCBPopupMenuSelect
)
100 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
101 const size_t numFilters
= data
->extensions
.GetCount();
103 if ( menu
->menuType
< numFilters
)
105 data
->currentfilter
= menu
->menuType
;
106 if ( data
->saveMode
)
108 int i
= menu
->menuType
;
109 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
110 extension
.MakeLower() ;
113 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
114 sfilename
= cfString
.AsString() ;
116 int pos
= sfilename
.Find('.', true) ;
117 if ( pos
!= wxNOT_FOUND
)
119 sfilename
= sfilename
.Left(pos
+1)+extension
;
120 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
121 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
129 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
131 myData
->menuitems
= NULL
;
132 myData
->currentfilter
= 0 ;
133 myData
->saveMode
= false ;
135 if ( filter
&& filter
[0] )
137 wxString
filter2(filter
) ;
141 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
143 if( filter2
.GetChar(i
) == wxT('|') )
146 myData
->name
.Add( current
) ;
149 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
.IsEmpty() )
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
++ )
178 wxString extension
= myData
->extensions
[i
];
180 if (extension
.GetChar(0) == '*')
181 extension
= extension
.Mid(1); // Remove leading *
183 if (extension
.GetChar(0) == '.')
185 extension
= extension
.Mid(1); // Remove leading .
188 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
190 myData
->filtermactypes
.Add( (OSType
)fileType
);
194 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
200 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
202 wxString
file(filename
) ;
205 if ( data
->extensions
.GetCount() > 0 )
207 //for ( int i = 0 ; i < data->numfilters ; ++i )
208 int i
= data
->currentfilter
;
209 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
213 if ( type
== (OSType
)data
->filtermactypes
[i
] )
216 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
217 while( tokenizer
.HasMoreTokens() )
219 wxString extension
= tokenizer
.GetNextToken() ;
220 if ( extension
.GetChar(0) == '*' )
221 extension
= extension
.Mid(1) ;
223 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
232 #if !TARGET_API_MAC_OSX
233 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
235 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
236 // return true if this item is invisible or a file
241 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
242 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
244 // because the semantics of the filter proc are "true means don't show
245 // it" we need to invert the result that we return
252 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
253 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
262 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
263 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
264 long style
, const wxPoint
& pos
)
265 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
267 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
270 pascal Boolean
CrossPlatformFilterCallback (
274 NavFilterModes filterMode
278 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
280 if (filterMode
== kNavFilteringBrowserList
)
282 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
283 if ( !theInfo
->isFolder
)
285 if (theItem
->descriptorType
== typeFSS
)
288 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
289 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
290 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
292 else if ( theItem
->descriptorType
== typeFSRef
)
295 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
296 wxString file
= wxMacFSRefToPath( &fsref
) ;
297 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
305 int wxFileDialog::ShowModal()
308 NavDialogCreationOptions dialogCreateOptions
;
309 // set default options
310 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
312 // this was always unset in the old code
313 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
315 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
316 dialogCreateOptions
.windowTitle
= message
;
318 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
319 dialogCreateOptions
.saveFileName
= defaultFileName
;
323 NavObjectFilterUPP navFilterUPP
= NULL
;
324 CFArrayRef cfArray
= NULL
; // for popupExtension
325 OpenUserDataRec myData
;
326 myData
.defaultLocation
= m_dir
;
328 MakeUserDataRec(&myData
, m_wildCard
);
329 myData
.currentfilter
= m_filterIndex
;
330 size_t numFilters
= myData
.extensions
.GetCount();
333 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
334 numFilters
, &kCFTypeArrayCallBacks
) ;
335 dialogCreateOptions
.popupExtension
= popup
;
336 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
337 for ( size_t i
= 0 ; i
< numFilters
; ++i
)
339 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
343 if (m_dialogStyle
& wxSAVE
)
345 myData
.saveMode
= true;
349 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
351 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
352 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
354 // The extension is important
356 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
358 #if TARGET_API_MAC_OSX
359 if (!(m_dialogStyle
& wxOVERWRITE_PROMPT
))
361 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
364 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
365 // Suppresses the 'Default' (top) menu item
366 kNavGenericSignature
, kNavGenericSignature
,
367 sStandardNavEventFilter
,
368 &myData
, // for defaultLocation
374 //let people select bundles/programs in dialogs
375 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
377 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
378 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
379 NULL
, // NavTypeListHandle
380 sStandardNavEventFilter
,
381 NULL
, // NavPreviewUPP
383 (void *) &myData
, // inClientData
388 err
= ::NavDialogRun(dialog
);
390 // clean up filter related data, etc.
392 ::DisposeNavObjectFilterUPP(navFilterUPP
);
394 ::CFRelease(cfArray
);
399 NavReplyRecord navReply
;
400 err
= ::NavDialogGetReply(dialog
, &navReply
);
401 if (err
== noErr
&& navReply
.validRecord
)
403 AEKeyword theKeyword
;
409 m_filterIndex
= myData
.currentfilter
;
412 ::AECountItems(&navReply
.selection
, &count
);
413 for (long i
= 1; i
<= count
; ++i
)
415 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
416 &theFSRef
, sizeof(theFSRef
), &actualSize
);
420 if (m_dialogStyle
& wxSAVE
)
421 thePath
= wxMacFSRefToPath( &theFSRef
, navReply
.saveFileName
) ;
423 thePath
= wxMacFSRefToPath( &theFSRef
) ;
427 ::NavDisposeReply(&navReply
);
432 m_fileName
= wxFileNameFromPath(m_path
);
433 m_fileNames
.Add(m_fileName
);
435 // set these to the first hit
437 m_fileName
= wxFileNameFromPath(m_path
);
438 m_dir
= wxPathOnly(m_path
);
440 ::NavDisposeReply(&navReply
);
442 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;