1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/dialog.h"
16 #include "wx/filedlg.h"
18 #include "wx/tokenzr.h"
19 #include "wx/filename.h"
22 #include "PLStringFuncs.h"
25 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
29 #include "wx/mac/private.h"
31 #include <Navigation.h>
34 # include "MoreFilesX.h"
36 # include "MoreFiles.h"
37 # include "MoreFilesExtras.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
;
56 CFArrayRef menuitems
;
58 NavMenuItemSpecArrayHandle menuitems
;
62 typedef struct OpenUserDataRec
63 OpenUserDataRec
, *OpenUserDataRecPtr
;
65 static pascal void NavEventProc(
66 NavEventCallbackMessage inSelector
,
68 NavCallBackUserData ioUserData
);
71 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
73 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
78 NavEventCallbackMessage inSelector
,
80 NavCallBackUserData ioUserData
)
82 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
83 if (inSelector
== kNavCBEvent
) {
86 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
89 else if ( inSelector
== kNavCBStart
)
92 if (data
&& !(data
->defaultLocation
).IsEmpty())
94 // Set default location for the modern Navigation APIs
95 // Apple Technical Q&A 1151
97 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
98 AEDesc theLocation
= {typeNull
, NULL
};
99 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
100 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
103 if ( data
->menuitems
)
104 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
107 else if ( inSelector
== kNavCBPopupMenuSelect
)
109 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
112 if ( menu
->menuCreator
== 'WXNG' )
115 data
->currentfilter
= menu
->menuType
;
116 if ( data
->saveMode
)
118 int i
= menu
->menuType
;
119 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
120 extension
.MakeLower() ;
124 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
125 sfilename
= cfString
.AsString() ;
128 // get the current filename
129 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
130 sfilename
= wxMacMakeStringFromPascal( filename
) ;
133 int pos
= sfilename
.Find('.', true) ;
134 if ( pos
!= wxNOT_FOUND
)
136 sfilename
= sfilename
.Left(pos
+1)+extension
;
138 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
139 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
141 wxMacStringToPascal( sfilename
, filename
) ;
142 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
151 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
153 myData
->menuitems
= NULL
;
154 myData
->currentfilter
= 0 ;
155 myData
->saveMode
= false ;
157 if ( filter
&& filter
[0] )
159 wxString
filter2(filter
) ;
163 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
165 if( filter2
.GetChar(i
) == wxT('|') )
168 myData
->name
.Add( current
) ;
171 myData
->extensions
.Add( current
.MakeUpper() ) ;
175 current
= wxEmptyString
;
179 current
+= filter2
.GetChar(i
) ;
182 // we allow for compatibility reason to have a single filter expression (like *.*) without
183 // an explanatory text, in that case the first part is name and extension at the same time
185 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
186 if ( current
.IsEmpty() )
187 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
189 myData
->extensions
.Add( current
.MakeUpper() ) ;
190 if ( filterIndex
== 0 || isName
)
191 myData
->name
.Add( current
.MakeUpper() ) ;
195 const size_t extCount
= myData
->extensions
.GetCount();
196 for ( size_t i
= 0 ; i
< extCount
; i
++ )
200 wxString extension
= myData
->extensions
[i
];
202 if (extension
.GetChar(0) == '*')
203 extension
= extension
.Mid(1); // Remove leading *
205 if (extension
.GetChar(0) == '.')
207 extension
= extension
.Mid(1); // Remove leading .
210 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
212 myData
->filtermactypes
.Add( (OSType
)fileType
);
216 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
222 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
224 wxString
file(filename
) ;
227 if ( data
->extensions
.GetCount() > 0 )
229 //for ( int i = 0 ; i < data->numfilters ; ++i )
230 int i
= data
->currentfilter
;
231 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
235 if ( type
== (OSType
)data
->filtermactypes
[i
] )
238 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
239 while( tokenizer
.HasMoreTokens() )
241 wxString extension
= tokenizer
.GetNextToken() ;
242 if ( extension
.GetChar(0) == '*' )
243 extension
= extension
.Mid(1) ;
245 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
255 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
257 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
258 // return true if this item is invisible or a file
263 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
264 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
266 // because the semantics of the filter proc are "true means don't show
267 // it" we need to invert the result that we return
274 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
275 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
284 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
285 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
286 long style
, const wxPoint
& pos
)
287 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
289 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
292 pascal Boolean
CrossPlatformFilterCallback (
296 NavFilterModes filterMode
300 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
302 if (filterMode
== kNavFilteringBrowserList
)
304 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
305 if ( !theInfo
->isFolder
)
307 if (theItem
->descriptorType
== typeFSS
)
310 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
311 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
312 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
315 else if ( theItem
->descriptorType
== typeFSRef
)
318 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
323 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &fsref
);
325 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
327 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
329 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
330 ::CFRelease( fullURLRef
) ;
331 wxString file
= wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
333 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
342 int wxFileDialog::ShowModal()
346 NavDialogCreationOptions dialogCreateOptions
;
347 // set default options
348 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
350 // this was always unset in the old code
351 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
353 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
354 dialogCreateOptions
.windowTitle
= message
;
356 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
357 dialogCreateOptions
.saveFileName
= defaultFileName
;
361 NavObjectFilterUPP navFilterUPP
= NULL
;
362 CFArrayRef cfArray
= NULL
; // for popupExtension
363 OpenUserDataRec myData
;
364 myData
.defaultLocation
= m_dir
;
366 if (m_dialogStyle
& wxSAVE
)
368 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
369 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
370 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
372 // The extension is important
373 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
375 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
378 sStandardNavEventFilter
,
379 &myData
, // for defaultLocation
384 MakeUserDataRec(&myData
, m_wildCard
);
385 size_t numfilters
= myData
.extensions
.GetCount();
388 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
389 numfilters
, &kCFTypeArrayCallBacks
) ;
390 dialogCreateOptions
.popupExtension
= popup
;
391 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
392 for ( size_t i
= 0 ; i
< numfilters
; ++i
)
394 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
398 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
399 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
400 NULL
, // NavTypeListHandle
401 sStandardNavEventFilter
,
402 NULL
, // NavPreviewUPP
404 (void *) &myData
, // inClientData
409 err
= ::NavDialogRun(dialog
);
411 // clean up filter related data, etc.
413 ::DisposeNavObjectFilterUPP(navFilterUPP
);
415 ::CFRelease(cfArray
);
420 NavReplyRecord navReply
;
421 err
= ::NavDialogGetReply(dialog
, &navReply
);
422 if (err
== noErr
&& navReply
.validRecord
)
424 AEKeyword theKeyword
;
430 ::AECountItems(&navReply
.selection
, &count
);
431 for (long i
= 1; i
<= count
; ++i
)
433 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
434 &theFSRef
, sizeof(theFSRef
), &actualSize
);
439 if (m_dialogStyle
& wxSAVE
)
441 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
446 ::CFURLCreateCopyAppendingPathComponent(NULL
,
448 navReply
.saveFileName
,
450 ::CFRelease(parentURLRef
);
455 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
458 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
460 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
462 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
463 thePath
= wxMacCFStringHolder(cfString
).AsString(m_font
.GetEncoding());
466 ::NavDisposeReply(&navReply
);
471 m_fileName
= wxFileNameFromPath(m_path
);
472 m_fileNames
.Add(m_fileName
);
474 // set these to the first hit
476 m_fileName
= wxFileNameFromPath(m_path
);
477 m_dir
= wxPathOnly(m_path
);
479 ::NavDisposeReply(&navReply
);
481 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
482 #else // TARGET_CARBON
484 NavDialogOptions mNavOptions
;
485 NavObjectFilterUPP mNavFilterUPP
= NULL
;
486 NavPreviewUPP mNavPreviewUPP
= NULL
;
487 NavReplyRecord mNavReply
;
488 AEDesc mDefaultLocation
;
489 bool mSelectDefault
= false ;
490 OSStatus err
= noErr
;
494 mNavPreviewUPP
= nil
;
495 mSelectDefault
= false;
496 mDefaultLocation
.descriptorType
= typeNull
;
497 mDefaultLocation
.dataHandle
= nil
;
499 NavGetDefaultDialogOptions(&mNavOptions
);
500 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
501 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
503 // Set default location, the location
504 // that's displayed when the dialog
508 wxMacFilename2FSSpec( m_dir
, &location
) ;
510 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
512 if ( mDefaultLocation
.dataHandle
)
516 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
518 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
522 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
523 mNavReply
.validRecord
= false;
524 mNavReply
.replacing
= false;
525 mNavReply
.isStationery
= false;
526 mNavReply
.translationNeeded
= false;
527 mNavReply
.selection
.descriptorType
= typeNull
;
528 mNavReply
.selection
.dataHandle
= nil
;
529 mNavReply
.keyScript
= smSystemScript
;
530 mNavReply
.fileTranslation
= nil
;
531 mNavReply
.version
= kNavReplyRecordVersion
;
535 m_path
= wxEmptyString
;
536 m_fileName
= wxEmptyString
;
540 OpenUserDataRec myData
;
541 MakeUserDataRec( &myData
, m_wildCard
) ;
542 myData
.currentfilter
= m_filterIndex
;
543 if ( myData
.extensions
.GetCount() > 0 )
545 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
546 myData
.menuitems
= mNavOptions
.popupExtension
;
547 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
549 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
550 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
551 // TODO : according to the new docs -1 to 10 are reserved for the OS
552 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
553 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
556 if ( m_dialogStyle
& wxSAVE
)
558 myData
.saveMode
= true ;
560 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
561 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
567 sStandardNavEventFilter
,
569 kNavGenericSignature
,
570 &myData
); // User Data
571 m_filterIndex
= myData
.currentfilter
;
575 myData
.saveMode
= false ;
577 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
578 if ( m_dialogStyle
& wxMULTIPLE
)
579 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
581 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
587 sStandardNavEventFilter
,
592 m_filterIndex
= myData
.currentfilter
;
595 DisposeNavObjectFilterUPP(mNavFilterUPP
);
596 if ( mDefaultLocation
.dataHandle
!= nil
)
598 ::AEDisposeDesc(&mDefaultLocation
);
601 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
605 if (mNavReply
.validRecord
)
612 ::AECountItems( &mNavReply
.selection
, &count
) ;
613 for ( long i
= 1 ; i
<= count
; ++i
)
615 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
621 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
622 if (specDesc
.dataHandle
!= nil
) {
623 ::AEDisposeDesc(&specDesc
);
625 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
627 m_paths
.Add( m_path
) ;
628 m_fileName
= wxFileNameFromPath(m_path
);
629 m_fileNames
.Add(m_fileName
);
631 // set these to the first hit
632 m_path
= m_paths
[ 0 ] ;
633 m_fileName
= wxFileNameFromPath(m_path
);
634 m_dir
= wxPathOnly(m_path
);
635 NavDisposeReply( &mNavReply
) ;
639 #endif // TARGET_CARBON