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 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
33 #include "wx/mac/private.h"
35 #include <Navigation.h>
38 # include "MoreFilesX.h"
40 # include "MoreFiles.h"
41 # include "MoreFilesExtras.h"
44 extern bool gUseNavServices
;
46 // the data we need to pass to our standard file hook routine
47 // includes a pointer to the dialog, a pointer to the standard
48 // file reply record (so we can inspect the current selection)
49 // and a copy of the "previous" file spec of the reply record
50 // so we can see if the selection has changed
52 struct OpenUserDataRec
{
56 wxArrayString extensions
;
57 wxArrayLong filtermactypes
;
58 wxString defaultLocation
;
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 if (data
&& !(data
->defaultLocation
).IsEmpty())
98 // Set default location for the modern Navigation APIs
99 // Apple Technical Q&A 1151
101 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
102 AEDesc theLocation
= {typeNull
, NULL
};
103 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
104 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
107 if ( data
->menuitems
)
108 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
111 else if ( inSelector
== kNavCBPopupMenuSelect
)
113 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
116 if ( menu
->menuCreator
== 'WXNG' )
119 data
->currentfilter
= menu
->menuType
;
120 if ( data
->saveMode
)
122 int i
= menu
->menuType
;
123 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
124 extension
.MakeLower() ;
128 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
129 sfilename
= cfString
.AsString() ;
132 // get the current filename
133 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
134 sfilename
= wxMacMakeStringFromPascal( filename
) ;
137 int pos
= sfilename
.Find('.', true) ;
138 if ( pos
!= wxNOT_FOUND
)
140 sfilename
= sfilename
.Left(pos
+1)+extension
;
142 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
143 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
145 wxMacStringToPascal( sfilename
, filename
) ;
146 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
155 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
157 myData
->menuitems
= NULL
;
158 myData
->currentfilter
= 0 ;
159 myData
->saveMode
= false ;
161 if ( filter
&& filter
[0] )
163 wxString
filter2(filter
) ;
167 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
169 if( filter2
.GetChar(i
) == wxT('|') )
172 myData
->name
.Add( current
) ;
175 myData
->extensions
.Add( current
.MakeUpper() ) ;
179 current
= wxEmptyString
;
183 current
+= filter2
.GetChar(i
) ;
186 // we allow for compatibility reason to have a single filter expression (like *.*) without
187 // an explanatory text, in that case the first part is name and extension at the same time
189 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
190 if ( current
.IsEmpty() )
191 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
193 myData
->extensions
.Add( current
.MakeUpper() ) ;
194 if ( filterIndex
== 0 || isName
)
195 myData
->name
.Add( current
.MakeUpper() ) ;
199 const size_t extCount
= myData
->extensions
.GetCount();
200 for ( size_t i
= 0 ; i
< extCount
; i
++ )
204 wxString extension
= myData
->extensions
[i
];
206 if (extension
.GetChar(0) == '*')
207 extension
= extension
.Mid(1); // Remove leading *
209 if (extension
.GetChar(0) == '.')
211 extension
= extension
.Mid(1); // Remove leading .
214 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
216 myData
->filtermactypes
.Add( (OSType
)fileType
);
220 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
226 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
228 wxString
file(filename
) ;
231 if ( data
->extensions
.GetCount() > 0 )
233 //for ( int i = 0 ; i < data->numfilters ; ++i )
234 int i
= data
->currentfilter
;
235 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
239 if ( type
== (OSType
)data
->filtermactypes
[i
] )
242 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
243 while( tokenizer
.HasMoreTokens() )
245 wxString extension
= tokenizer
.GetNextToken() ;
246 if ( extension
.GetChar(0) == '*' )
247 extension
= extension
.Mid(1) ;
249 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
259 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
261 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
262 // return true if this item is invisible or a file
267 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
268 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
270 // because the semantics of the filter proc are "true means don't show
271 // it" we need to invert the result that we return
278 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
279 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
288 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
289 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
290 long style
, const wxPoint
& pos
)
291 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
293 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
296 pascal Boolean
CrossPlatformFilterCallback (
300 NavFilterModes filterMode
304 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
306 if (filterMode
== kNavFilteringBrowserList
)
308 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
309 if ( !theInfo
->isFolder
)
311 if (theItem
->descriptorType
== typeFSS
)
314 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
315 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
316 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
319 else if ( theItem
->descriptorType
== typeFSRef
)
322 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
327 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &fsref
);
329 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
331 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
333 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
334 ::CFRelease( fullURLRef
) ;
335 wxString file
= wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
337 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
346 int wxFileDialog::ShowModal()
350 NavDialogCreationOptions dialogCreateOptions
;
351 // set default options
352 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
354 // this was always unset in the old code
355 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
357 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
358 dialogCreateOptions
.windowTitle
= message
;
360 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
361 dialogCreateOptions
.saveFileName
= defaultFileName
;
365 NavObjectFilterUPP navFilterUPP
= NULL
;
366 CFArrayRef cfArray
= NULL
; // for popupExtension
367 OpenUserDataRec myData
;
368 myData
.defaultLocation
= m_dir
;
370 if (m_dialogStyle
& wxSAVE
)
372 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
373 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
374 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
376 // The extension is important
377 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
379 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
382 sStandardNavEventFilter
,
383 &myData
, // for defaultLocation
388 MakeUserDataRec(&myData
, m_wildCard
);
389 size_t numfilters
= myData
.extensions
.GetCount();
392 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
393 numfilters
, &kCFTypeArrayCallBacks
) ;
394 dialogCreateOptions
.popupExtension
= popup
;
395 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
396 for ( size_t i
= 0 ; i
< numfilters
; ++i
)
398 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
402 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
403 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
404 NULL
, // NavTypeListHandle
405 sStandardNavEventFilter
,
406 NULL
, // NavPreviewUPP
408 (void *) &myData
, // inClientData
413 err
= ::NavDialogRun(dialog
);
415 // clean up filter related data, etc.
417 ::DisposeNavObjectFilterUPP(navFilterUPP
);
419 ::CFRelease(cfArray
);
424 NavReplyRecord navReply
;
425 err
= ::NavDialogGetReply(dialog
, &navReply
);
426 if (err
== noErr
&& navReply
.validRecord
)
428 AEKeyword theKeyword
;
434 ::AECountItems(&navReply
.selection
, &count
);
435 for (long i
= 1; i
<= count
; ++i
)
437 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
438 &theFSRef
, sizeof(theFSRef
), &actualSize
);
443 if (m_dialogStyle
& wxSAVE
)
445 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
450 ::CFURLCreateCopyAppendingPathComponent(NULL
,
452 navReply
.saveFileName
,
454 ::CFRelease(parentURLRef
);
459 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
462 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
464 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
466 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
467 thePath
= wxMacCFStringHolder(cfString
).AsString(m_font
.GetEncoding());
470 ::NavDisposeReply(&navReply
);
475 m_fileName
= wxFileNameFromPath(m_path
);
476 m_fileNames
.Add(m_fileName
);
478 // set these to the first hit
480 m_fileName
= wxFileNameFromPath(m_path
);
481 m_dir
= wxPathOnly(m_path
);
483 ::NavDisposeReply(&navReply
);
485 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
486 #else // TARGET_CARBON
488 NavDialogOptions mNavOptions
;
489 NavObjectFilterUPP mNavFilterUPP
= NULL
;
490 NavPreviewUPP mNavPreviewUPP
= NULL
;
491 NavReplyRecord mNavReply
;
492 AEDesc mDefaultLocation
;
493 bool mSelectDefault
= false ;
494 OSStatus err
= noErr
;
498 mNavPreviewUPP
= nil
;
499 mSelectDefault
= false;
500 mDefaultLocation
.descriptorType
= typeNull
;
501 mDefaultLocation
.dataHandle
= nil
;
503 NavGetDefaultDialogOptions(&mNavOptions
);
504 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
505 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
507 // Set default location, the location
508 // that's displayed when the dialog
512 wxMacFilename2FSSpec( m_dir
, &location
) ;
514 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
516 if ( mDefaultLocation
.dataHandle
)
520 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
522 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
526 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
527 mNavReply
.validRecord
= false;
528 mNavReply
.replacing
= false;
529 mNavReply
.isStationery
= false;
530 mNavReply
.translationNeeded
= false;
531 mNavReply
.selection
.descriptorType
= typeNull
;
532 mNavReply
.selection
.dataHandle
= nil
;
533 mNavReply
.keyScript
= smSystemScript
;
534 mNavReply
.fileTranslation
= nil
;
535 mNavReply
.version
= kNavReplyRecordVersion
;
539 m_path
= wxEmptyString
;
540 m_fileName
= wxEmptyString
;
544 OpenUserDataRec myData
;
545 MakeUserDataRec( &myData
, m_wildCard
) ;
546 myData
.currentfilter
= m_filterIndex
;
547 if ( myData
.extensions
.GetCount() > 0 )
549 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
550 myData
.menuitems
= mNavOptions
.popupExtension
;
551 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
553 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
554 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
555 // TODO : according to the new docs -1 to 10 are reserved for the OS
556 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
557 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
560 if ( m_dialogStyle
& wxSAVE
)
562 myData
.saveMode
= true ;
564 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
565 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
571 sStandardNavEventFilter
,
573 kNavGenericSignature
,
574 &myData
); // User Data
575 m_filterIndex
= myData
.currentfilter
;
579 myData
.saveMode
= false ;
581 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
582 if ( m_dialogStyle
& wxMULTIPLE
)
583 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
585 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
591 sStandardNavEventFilter
,
596 m_filterIndex
= myData
.currentfilter
;
599 DisposeNavObjectFilterUPP(mNavFilterUPP
);
600 if ( mDefaultLocation
.dataHandle
!= nil
)
602 ::AEDisposeDesc(&mDefaultLocation
);
605 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
609 if (mNavReply
.validRecord
)
616 ::AECountItems( &mNavReply
.selection
, &count
) ;
617 for ( long i
= 1 ; i
<= count
; ++i
)
619 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
625 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
626 if (specDesc
.dataHandle
!= nil
) {
627 ::AEDisposeDesc(&specDesc
);
629 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
631 m_paths
.Add( m_path
) ;
632 m_fileName
= wxFileNameFromPath(m_path
);
633 m_fileNames
.Add(m_fileName
);
635 // set these to the first hit
636 m_path
= m_paths
[ 0 ] ;
637 m_fileName
= wxFileNameFromPath(m_path
);
638 m_dir
= wxPathOnly(m_path
);
639 NavDisposeReply( &mNavReply
) ;
643 #endif // TARGET_CARBON