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"
25 #include "PLStringFuncs.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
34 #include "wx/mac/private.h"
36 #include <Navigation.h>
39 # include "MoreFilesX.h"
41 # include "MoreFiles.h"
42 # include "MoreFilesExtras.h"
45 extern bool gUseNavServices
;
47 // the data we need to pass to our standard file hook routine
48 // includes a pointer to the dialog, a pointer to the standard
49 // file reply record (so we can inspect the current selection)
50 // and a copy of the "previous" file spec of the reply record
51 // so we can see if the selection has changed
53 struct OpenUserDataRec
{
57 wxArrayString extensions
;
58 wxArrayLong filtermactypes
;
60 CFArrayRef menuitems
;
62 NavMenuItemSpecArrayHandle menuitems
;
66 typedef struct OpenUserDataRec
67 OpenUserDataRec
, *OpenUserDataRecPtr
;
69 static pascal void NavEventProc(
70 NavEventCallbackMessage inSelector
,
72 NavCallBackUserData ioUserData
);
75 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
77 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
82 NavEventCallbackMessage inSelector
,
84 NavCallBackUserData ioUserData
)
86 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
87 if (inSelector
== kNavCBEvent
) {
90 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
93 else if ( inSelector
== kNavCBStart
)
96 // there is no way to set this in the new API
98 if ( data
->menuitems
)
99 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
102 else if ( inSelector
== kNavCBPopupMenuSelect
)
104 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
107 if ( menu
->menuCreator
== 'WXNG' )
110 data
->currentfilter
= menu
->menuType
;
111 if ( data
->saveMode
)
113 int i
= menu
->menuType
;
114 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
115 extension
.MakeLower() ;
119 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
120 sfilename
= cfString
.AsString() ;
123 // get the current filename
124 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
125 sfilename
= wxMacMakeStringFromPascal( filename
) ;
128 int pos
= sfilename
.Find('.',TRUE
) ;
129 if ( pos
!= wxNOT_FOUND
)
131 sfilename
= sfilename
.Left(pos
+1)+extension
;
133 cfString
= sfilename
;
134 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
136 wxMacStringToPascal( sfilename
, filename
) ;
137 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
145 const wxChar
* gfilters
[] =
154 OSType gfiltersmac
[] =
165 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
167 myData
->menuitems
= NULL
;
168 myData
->currentfilter
= 0 ;
169 myData
->saveMode
= FALSE
;
171 if ( filter
&& filter
[0] )
173 wxString
filter2(filter
) ;
177 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
179 if( filter2
.GetChar(i
) == wxT('|') )
182 myData
->name
.Add( current
) ;
185 myData
->extensions
.Add( current
.MakeUpper() ) ;
189 current
= wxEmptyString
;
193 current
+= filter2
.GetChar(i
) ;
196 // we allow for compatibility reason to have a single filter expression (like *.*) without
197 // an explanatory text, in that case the first part is name and extension at the same time
199 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
200 if ( current
.IsEmpty() )
201 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
203 myData
->extensions
.Add( current
.MakeUpper() ) ;
204 if ( filterIndex
== 0 || isName
)
205 myData
->name
.Add( current
.MakeUpper() ) ;
210 const size_t extCount
= myData
->extensions
.GetCount();
211 for ( size_t i
= 0 ; i
< extCount
; i
++ )
214 for ( j
= 0 ; gfilters
[j
] ; j
++ )
216 if ( myData
->extensions
[i
] == gfilters
[j
] )
218 myData
->filtermactypes
.Add( gfiltersmac
[j
] ) ;
222 if( gfilters
[j
] == NULL
)
224 myData
->filtermactypes
.Add( '****' ) ;
230 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
232 wxString file
= filename
;
235 if ( data
->extensions
.GetCount() > 0 )
237 //for ( int i = 0 ; i < data->numfilters ; ++i )
238 int i
= data
->currentfilter
;
239 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
243 if ( type
== (OSType
)data
->filtermactypes
[i
] )
246 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
247 while( tokenizer
.HasMoreTokens() )
249 wxString extension
= tokenizer
.GetNextToken() ;
250 if ( extension
.GetChar(0) == '*' )
251 extension
= extension
.Mid(1) ;
253 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
263 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
265 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
266 // return true if this item is invisible or a file
271 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
272 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
274 // because the semantics of the filter proc are "true means don't show
275 // it" we need to invert the result that we return
282 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
283 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
292 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
293 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
294 long style
, const wxPoint
& pos
)
295 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
297 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
300 pascal Boolean
CrossPlatformFilterCallback (
304 NavFilterModes filterMode
308 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
310 if (filterMode
== kNavFilteringBrowserList
)
312 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
313 if ( !theInfo
->isFolder
)
315 if (theItem
->descriptorType
== typeFSS
)
318 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
319 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
320 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
323 else if ( theItem
->descriptorType
== typeFSRef
)
326 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
328 const short maxpath
= 1024 ;
329 FSRefMakePath( &fsref
, (UInt8
*) file
.GetWriteBuf(maxpath
+1),maxpath
) ;
330 file
.UngetWriteBuf() ;
331 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
340 int wxFileDialog::ShowModal()
343 NavDialogCreationOptions mNavOptions
;
344 NavDialogRef navDialogRef
= NULL
;
345 // since the same field has been renamed ...
346 #define dialogOptionFlags optionFlags
348 NavDialogOptions mNavOptions
;
350 NavObjectFilterUPP mNavFilterUPP
= NULL
;
351 NavPreviewUPP mNavPreviewUPP
= NULL
;
352 NavReplyRecord mNavReply
;
353 AEDesc mDefaultLocation
;
354 bool mSelectDefault
= false ;
355 OSStatus err
= noErr
;
359 mNavPreviewUPP
= nil
;
360 mSelectDefault
= false;
361 mDefaultLocation
.descriptorType
= typeNull
;
362 mDefaultLocation
.dataHandle
= nil
;
365 NavGetDefaultDialogCreationOptions( &mNavOptions
) ;
366 wxMacCFStringHolder
cfMessage(m_message
) ;
367 wxMacCFStringHolder
cfFileName(m_fileName
) ;
368 mNavOptions
.saveFileName
= cfFileName
;
369 mNavOptions
.message
= cfMessage
;
371 NavGetDefaultDialogOptions(&mNavOptions
);
372 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
373 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
375 // Set default location, the location
376 // that's displayed when the dialog
380 wxMacFilename2FSSpec( m_dir
, &location
) ;
382 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
384 if ( mDefaultLocation
.dataHandle
) {
386 if (mSelectDefault
) {
387 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
389 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
394 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
395 mNavReply
.validRecord
= false;
396 mNavReply
.replacing
= false;
397 mNavReply
.isStationery
= false;
398 mNavReply
.translationNeeded
= false;
399 mNavReply
.selection
.descriptorType
= typeNull
;
400 mNavReply
.selection
.dataHandle
= nil
;
401 mNavReply
.keyScript
= smSystemScript
;
402 mNavReply
.fileTranslation
= nil
;
403 mNavReply
.version
= kNavReplyRecordVersion
;
407 m_path
= wxEmptyString
;
408 m_fileName
= wxEmptyString
;
412 OpenUserDataRec myData
;
413 MakeUserDataRec( &myData
, m_wildCard
) ;
414 myData
.currentfilter
= m_filterIndex
;
415 if ( myData
.extensions
.GetCount() > 0 )
418 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
419 myData
.extensions
.GetCount() , &kCFTypeArrayCallBacks
) ;
420 mNavOptions
.popupExtension
= popup
;
421 myData
.menuitems
= mNavOptions
.popupExtension
;
422 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
424 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] ) ) ;
427 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
428 myData
.menuitems
= mNavOptions
.popupExtension
;
429 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
431 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
432 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
433 // TODO : according to the new docs -1 to 10 are reserved for the OS
434 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
435 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
439 if ( m_dialogStyle
& wxSAVE
)
441 myData
.saveMode
= true ;
443 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
444 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
447 err
= NavCreatePutFileDialog( &mNavOptions
, NULL
, kNavGenericSignature
, sStandardNavEventFilter
,
448 &myData
, &navDialogRef
) ;
451 err
= NavDialogRun( navDialogRef
) ;
452 NavUserAction userAction
= NavDialogGetUserAction( navDialogRef
) ;
453 if ( userAction
!= kNavUserActionCancel
&& userAction
!= kNavUserActionNone
)
455 NavDialogGetReply( navDialogRef
, &mNavReply
) ;
463 sStandardNavEventFilter
,
465 kNavGenericSignature
,
466 &myData
); // User Data
468 m_filterIndex
= myData
.currentfilter
;
472 myData
.saveMode
= false ;
474 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
475 if ( m_dialogStyle
& wxMULTIPLE
)
476 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
478 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
481 err
= NavCreateGetFileDialog( &mNavOptions
, NULL
, sStandardNavEventFilter
,
482 mNavPreviewUPP
, mNavFilterUPP
, &myData
, &navDialogRef
) ;
485 err
= NavDialogRun( navDialogRef
) ;
486 NavUserAction userAction
= NavDialogGetUserAction( navDialogRef
) ;
487 if ( userAction
!= kNavUserActionCancel
&& userAction
!= kNavUserActionNone
)
489 NavDialogGetReply( navDialogRef
, &mNavReply
) ;
497 sStandardNavEventFilter
,
503 m_filterIndex
= myData
.currentfilter
;
506 DisposeNavObjectFilterUPP(mNavFilterUPP
);
507 if ( mDefaultLocation
.dataHandle
!= nil
)
509 ::AEDisposeDesc(&mDefaultLocation
);
512 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
516 if (mNavReply
.validRecord
) {
523 ::AECountItems( &mNavReply
.selection
, &count
) ;
524 for ( long i
= 1 ; i
<= count
; ++i
)
526 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
527 if ( err
!= noErr
) {
531 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
532 if (specDesc
.dataHandle
!= nil
) {
533 ::AEDisposeDesc(&specDesc
);
535 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
537 if ( m_dialogStyle
& wxSAVE
)
539 wxMacCFStringHolder cfString
= NavDialogGetSaveFileName( navDialogRef
) ;
540 m_path
+= wxFILE_SEP_PATH
+ cfString
.AsString() ;
543 m_paths
.Add( m_path
) ;
544 m_fileName
= wxFileNameFromPath(m_path
);
545 m_fileNames
.Add(m_fileName
);
547 // set these to the first hit
548 m_path
= m_paths
[ 0 ] ;
549 m_fileName
= wxFileNameFromPath(m_path
);
550 m_dir
= wxPathOnly(m_path
);
551 NavDisposeReply( &mNavReply
) ;
554 NavDialogDispose( navDialogRef
) ;
560 NavDialogDispose( navDialogRef
) ;