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
)
97 if ( data
->menuitems
)
98 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
101 else if ( inSelector
== kNavCBPopupMenuSelect
)
103 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
106 if ( menu
->menuCreator
== 'WXNG' )
109 data
->currentfilter
= menu
->menuType
;
110 if ( data
->saveMode
)
112 int i
= menu
->menuType
;
113 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
114 extension
.MakeLower() ;
118 wxMacCFStringHolder cfString
= NavDialogGetSaveFileName( ioParams
->context
) ;
119 sfilename
= cfString
.AsString() ;
122 // get the current filename
123 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
124 sfilename
= wxMacMakeStringFromPascal( filename
) ;
127 int pos
= sfilename
.Find('.',TRUE
) ;
128 if ( pos
!= wxNOT_FOUND
)
130 sfilename
= sfilename
.Left(pos
+1)+extension
;
132 cfString
= sfilename
;
133 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
135 wxMacStringToPascal( sfilename
, filename
) ;
136 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
144 const wxChar
* gfilters
[] =
153 OSType gfiltersmac
[] =
164 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
166 myData
->menuitems
= NULL
;
167 myData
->currentfilter
= 0 ;
168 myData
->saveMode
= FALSE
;
170 if ( filter
&& filter
[0] )
172 wxString
filter2(filter
) ;
176 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
178 if( filter2
.GetChar(i
) == wxT('|') )
181 myData
->name
.Add( current
) ;
184 myData
->extensions
.Add( current
.MakeUpper() ) ;
188 current
= wxEmptyString
;
192 current
+= filter2
.GetChar(i
) ;
195 // we allow for compatibility reason to have a single filter expression (like *.*) without
196 // an explanatory text, in that case the first part is name and extension at the same time
198 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
199 if ( current
.IsEmpty() )
200 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
202 myData
->extensions
.Add( current
.MakeUpper() ) ;
203 if ( filterIndex
== 0 || isName
)
204 myData
->name
.Add( current
.MakeUpper() ) ;
209 const size_t extCount
= myData
->extensions
.GetCount();
210 for ( size_t i
= 0 ; i
< extCount
; i
++ )
213 for ( j
= 0 ; gfilters
[j
] ; j
++ )
215 if ( myData
->extensions
[i
] == gfilters
[j
] )
217 myData
->filtermactypes
.Add( gfiltersmac
[j
] ) ;
221 if( gfilters
[j
] == NULL
)
223 myData
->filtermactypes
.Add( '****' ) ;
229 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
231 wxString file
= filename
;
234 if ( data
->extensions
.GetCount() > 0 )
236 //for ( int i = 0 ; i < data->numfilters ; ++i )
237 int i
= data
->currentfilter
;
238 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
242 if ( type
== (OSType
)data
->filtermactypes
[i
] )
245 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
246 while( tokenizer
.HasMoreTokens() )
248 wxString extension
= tokenizer
.GetNextToken() ;
249 if ( extension
.GetChar(0) == '*' )
250 extension
= extension
.Mid(1) ;
252 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
262 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
264 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
265 // return true if this item is invisible or a file
270 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
271 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
273 // because the semantics of the filter proc are "true means don't show
274 // it" we need to invert the result that we return
281 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
282 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
291 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
292 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
293 long style
, const wxPoint
& pos
)
294 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
296 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
299 pascal Boolean
CrossPlatformFilterCallback (
303 NavFilterModes filterMode
307 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
309 if (filterMode
== kNavFilteringBrowserList
)
311 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
312 if ( !theInfo
->isFolder
)
314 if (theItem
->descriptorType
== typeFSS
)
317 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
318 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
319 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
322 else if ( theItem
->descriptorType
== typeFSRef
)
325 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
327 const short maxpath
= 1024 ;
328 FSRefMakePath( &fsref
, (UInt8
*) file
.GetWriteBuf(maxpath
+1),maxpath
) ;
329 file
.UngetWriteBuf() ;
330 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
339 int wxFileDialog::ShowModal()
342 NavDialogCreationOptions mNavOptions
;
343 NavDialogRef navDialogRef
= NULL
;
344 // since the same field has been renamed ...
345 #define dialogOptionFlags optionFlags
347 NavDialogOptions mNavOptions
;
349 NavObjectFilterUPP mNavFilterUPP
= NULL
;
350 NavPreviewUPP mNavPreviewUPP
= NULL
;
351 NavReplyRecord mNavReply
;
352 AEDesc mDefaultLocation
;
353 bool mSelectDefault
= false ;
354 OSStatus err
= noErr
;
358 mNavPreviewUPP
= nil
;
359 mSelectDefault
= false;
360 mDefaultLocation
.descriptorType
= typeNull
;
361 mDefaultLocation
.dataHandle
= nil
;
364 NavGetDefaultDialogCreationOptions( &mNavOptions
) ;
365 wxMacCFStringHolder
cfMessage(m_message
) ;
366 wxMacCFStringHolder
cfFileName(m_fileName
) ;
367 mNavOptions
.saveFileName
= cfFileName
;
368 mNavOptions
.message
= cfMessage
;
370 NavGetDefaultDialogOptions(&mNavOptions
);
371 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
372 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
374 // Set default location, the location
375 // that's displayed when the dialog
379 wxMacFilename2FSSpec( m_dir
, &location
) ;
381 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
383 if ( mDefaultLocation
.dataHandle
) {
385 if (mSelectDefault
) {
386 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
388 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
393 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
394 mNavReply
.validRecord
= false;
395 mNavReply
.replacing
= false;
396 mNavReply
.isStationery
= false;
397 mNavReply
.translationNeeded
= false;
398 mNavReply
.selection
.descriptorType
= typeNull
;
399 mNavReply
.selection
.dataHandle
= nil
;
400 mNavReply
.keyScript
= smSystemScript
;
401 mNavReply
.fileTranslation
= nil
;
402 mNavReply
.version
= kNavReplyRecordVersion
;
406 m_path
= wxEmptyString
;
407 m_fileName
= wxEmptyString
;
411 OpenUserDataRec myData
;
412 MakeUserDataRec( &myData
, m_wildCard
) ;
413 myData
.currentfilter
= m_filterIndex
;
414 if ( myData
.extensions
.GetCount() > 0 )
417 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
418 myData
.extensions
.GetCount() , &kCFTypeArrayCallBacks
) ;
419 mNavOptions
.popupExtension
= popup
;
420 myData
.menuitems
= mNavOptions
.popupExtension
;
421 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
423 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] ) ) ;
426 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
427 myData
.menuitems
= mNavOptions
.popupExtension
;
428 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
430 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
431 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
432 // TODO : according to the new docs -1 to 10 are reserved for the OS
433 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
434 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
438 if ( m_dialogStyle
& wxSAVE
)
440 myData
.saveMode
= true ;
442 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
443 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
446 err
= NavCreatePutFileDialog( &mNavOptions
, NULL
, kNavGenericSignature
, sStandardNavEventFilter
,
447 &myData
, &navDialogRef
) ;
450 err
= NavDialogRun( navDialogRef
) ;
451 NavUserAction userAction
= NavDialogGetUserAction( navDialogRef
) ;
452 if ( userAction
!= kNavUserActionCancel
&& userAction
!= kNavUserActionNone
)
454 NavDialogGetReply( navDialogRef
, &mNavReply
) ;
462 sStandardNavEventFilter
,
464 kNavGenericSignature
,
465 &myData
); // User Data
467 m_filterIndex
= myData
.currentfilter
;
471 myData
.saveMode
= false ;
473 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
474 if ( m_dialogStyle
& wxMULTIPLE
)
475 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
477 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
480 err
= NavCreateGetFileDialog( &mNavOptions
, NULL
, sStandardNavEventFilter
,
481 mNavPreviewUPP
, mNavFilterUPP
, &myData
, &navDialogRef
) ;
484 err
= NavDialogRun( navDialogRef
) ;
485 NavUserAction userAction
= NavDialogGetUserAction( navDialogRef
) ;
486 if ( userAction
!= kNavUserActionCancel
&& userAction
!= kNavUserActionNone
)
488 NavDialogGetReply( navDialogRef
, &mNavReply
) ;
496 sStandardNavEventFilter
,
502 m_filterIndex
= myData
.currentfilter
;
505 DisposeNavObjectFilterUPP(mNavFilterUPP
);
506 if ( mDefaultLocation
.dataHandle
!= nil
)
508 ::AEDisposeDesc(&mDefaultLocation
);
511 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
515 if (mNavReply
.validRecord
) {
522 ::AECountItems( &mNavReply
.selection
, &count
) ;
523 for ( long i
= 1 ; i
<= count
; ++i
)
525 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
526 if ( err
!= noErr
) {
530 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
531 if (specDesc
.dataHandle
!= nil
) {
532 ::AEDisposeDesc(&specDesc
);
534 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
536 if ( m_dialogStyle
& wxSAVE
)
538 wxMacCFStringHolder cfString
= NavDialogGetSaveFileName( navDialogRef
) ;
539 m_path
+= wxFILE_SEP_PATH
+ cfString
.AsString() ;
542 m_paths
.Add( m_path
) ;
543 m_fileName
= wxFileNameFromPath(m_path
);
544 m_fileNames
.Add(m_fileName
);
546 // set these to the first hit
547 m_path
= m_paths
[ 0 ] ;
548 m_fileName
= wxFileNameFromPath(m_path
);
549 m_dir
= wxPathOnly(m_path
);
550 NavDisposeReply( &mNavReply
) ;
553 NavDialogDispose( navDialogRef
) ;
559 NavDialogDispose( navDialogRef
) ;