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>
40 # include "MoreFilesX.h"
42 # include "MoreFiles.h"
43 # include "MoreFilesExtras.h"
46 extern bool gUseNavServices
;
48 // the data we need to pass to our standard file hook routine
49 // includes a pointer to the dialog, a pointer to the standard
50 // file reply record (so we can inspect the current selection)
51 // and a copy of the "previous" file spec of the reply record
52 // so we can see if the selection has changed
54 struct OpenUserDataRec
{
58 wxArrayString extensions
;
59 wxArrayLong filtermactypes
;
60 wxString defaultLocation
;
62 CFArrayRef menuitems
;
64 NavMenuItemSpecArrayHandle menuitems
;
68 typedef struct OpenUserDataRec
69 OpenUserDataRec
, *OpenUserDataRecPtr
;
71 static pascal void NavEventProc(
72 NavEventCallbackMessage inSelector
,
74 NavCallBackUserData ioUserData
);
77 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
79 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
84 NavEventCallbackMessage inSelector
,
86 NavCallBackUserData ioUserData
)
88 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
89 if (inSelector
== kNavCBEvent
) {
92 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
95 else if ( inSelector
== kNavCBStart
)
98 if (data
&& !(data
->defaultLocation
).IsEmpty())
100 // Set default location for the modern Navigation APIs
101 // Apple Technical Q&A 1151
103 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
104 AEDesc theLocation
= {typeNull
, NULL
};
105 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
106 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
109 NavMenuItemSpec menuItem
;
110 menuItem
.version
= kNavMenuItemSpecVersion
;
111 menuItem
.menuCreator
= 'WXNG';
112 menuItem
.menuType
= data
->currentfilter
;
113 wxMacStringToPascal( data
->name
[data
->currentfilter
] , (StringPtr
)(menuItem
.menuItemName
) ) ;
114 ::NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &menuItem
);
117 if ( data
->menuitems
)
118 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
121 else if ( inSelector
== kNavCBPopupMenuSelect
)
123 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
125 const size_t numFilters
= data
->extensions
.GetCount();
127 if ( menu
->menuType
< numFilters
)
129 if ( menu
->menuCreator
== 'WXNG' )
132 data
->currentfilter
= menu
->menuType
;
133 if ( data
->saveMode
)
135 int i
= menu
->menuType
;
136 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
137 extension
.MakeLower() ;
141 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
142 sfilename
= cfString
.AsString() ;
145 // get the current filename
146 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
147 sfilename
= wxMacMakeStringFromPascal( filename
) ;
150 int pos
= sfilename
.Find('.', true) ;
151 if ( pos
!= wxNOT_FOUND
)
153 sfilename
= sfilename
.Left(pos
+1)+extension
;
155 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
156 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
158 wxMacStringToPascal( sfilename
, filename
) ;
159 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
168 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
170 myData
->menuitems
= NULL
;
171 myData
->currentfilter
= 0 ;
172 myData
->saveMode
= false ;
174 if ( filter
&& filter
[0] )
176 wxString
filter2(filter
) ;
180 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
182 if( filter2
.GetChar(i
) == wxT('|') )
185 myData
->name
.Add( current
) ;
188 myData
->extensions
.Add( current
.MakeUpper() ) ;
192 current
= wxEmptyString
;
196 current
+= filter2
.GetChar(i
) ;
199 // we allow for compatibility reason to have a single filter expression (like *.*) without
200 // an explanatory text, in that case the first part is name and extension at the same time
202 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
203 if ( current
.IsEmpty() )
204 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
206 myData
->extensions
.Add( current
.MakeUpper() ) ;
207 if ( filterIndex
== 0 || isName
)
208 myData
->name
.Add( current
.MakeUpper() ) ;
212 const size_t extCount
= myData
->extensions
.GetCount();
213 for ( size_t i
= 0 ; i
< extCount
; i
++ )
217 wxString extension
= myData
->extensions
[i
];
219 if (extension
.GetChar(0) == '*')
220 extension
= extension
.Mid(1); // Remove leading *
222 if (extension
.GetChar(0) == '.')
224 extension
= extension
.Mid(1); // Remove leading .
227 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
229 myData
->filtermactypes
.Add( (OSType
)fileType
);
233 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
239 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
241 wxString
file(filename
) ;
244 if ( data
->extensions
.GetCount() > 0 )
246 //for ( int i = 0 ; i < data->numfilters ; ++i )
247 int i
= data
->currentfilter
;
248 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
252 if ( type
== (OSType
)data
->filtermactypes
[i
] )
255 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
256 while( tokenizer
.HasMoreTokens() )
258 wxString extension
= tokenizer
.GetNextToken() ;
259 if ( extension
.GetChar(0) == '*' )
260 extension
= extension
.Mid(1) ;
262 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
272 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
274 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
275 // return true if this item is invisible or a file
280 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
281 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
283 // because the semantics of the filter proc are "true means don't show
284 // it" we need to invert the result that we return
291 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
292 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
301 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
302 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
303 long style
, const wxPoint
& pos
)
304 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
306 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
309 pascal Boolean
CrossPlatformFilterCallback (
313 NavFilterModes filterMode
317 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
319 if (filterMode
== kNavFilteringBrowserList
)
321 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
322 if ( !theInfo
->isFolder
)
324 if (theItem
->descriptorType
== typeFSS
)
327 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
328 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
329 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
332 else if ( theItem
->descriptorType
== typeFSRef
)
335 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
340 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &fsref
);
342 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
344 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
346 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
347 ::CFRelease( fullURLRef
) ;
348 wxString file
= wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
350 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
359 int wxFileDialog::ShowModal()
363 NavDialogCreationOptions dialogCreateOptions
;
364 // set default options
365 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
367 // this was always unset in the old code
368 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
370 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
371 dialogCreateOptions
.windowTitle
= message
;
373 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
374 dialogCreateOptions
.saveFileName
= defaultFileName
;
378 NavObjectFilterUPP navFilterUPP
= NULL
;
379 CFArrayRef cfArray
= NULL
; // for popupExtension
380 OpenUserDataRec myData
;
381 myData
.defaultLocation
= m_dir
;
383 MakeUserDataRec(&myData
, m_wildCard
);
384 myData
.currentfilter
= m_filterIndex
;
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 if (m_dialogStyle
& wxSAVE
)
400 myData
.saveMode
= true;
404 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
406 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
407 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
409 // The extension is important
411 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
413 #if TARGET_API_MAC_OSX
414 if (!(m_dialogStyle
& wxOVERWRITE_PROMPT
))
416 dialogCreateOptions
.optionFlags
|= kNavDontConfirmReplacement
;
419 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
420 // Suppresses the 'Default' (top) menu item
421 kNavGenericSignature
, kNavGenericSignature
,
422 sStandardNavEventFilter
,
423 &myData
, // for defaultLocation
429 //let people select bundles/programs in dialogs
430 dialogCreateOptions
.optionFlags
|= kNavSupportPackages
;
432 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
433 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
434 NULL
, // NavTypeListHandle
435 sStandardNavEventFilter
,
436 NULL
, // NavPreviewUPP
438 (void *) &myData
, // inClientData
443 err
= ::NavDialogRun(dialog
);
445 // clean up filter related data, etc.
447 ::DisposeNavObjectFilterUPP(navFilterUPP
);
449 ::CFRelease(cfArray
);
454 NavReplyRecord navReply
;
455 err
= ::NavDialogGetReply(dialog
, &navReply
);
456 if (err
== noErr
&& navReply
.validRecord
)
458 AEKeyword theKeyword
;
464 m_filterIndex
= myData
.currentfilter
;
467 ::AECountItems(&navReply
.selection
, &count
);
468 for (long i
= 1; i
<= count
; ++i
)
470 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
471 &theFSRef
, sizeof(theFSRef
), &actualSize
);
475 CFURLRef fullURLRef
= 0 ;
476 if (m_dialogStyle
& wxSAVE
)
478 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
483 ::CFURLCreateCopyAppendingPathComponent(NULL
,
485 navReply
.saveFileName
,
487 ::CFRelease(parentURLRef
);
492 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
495 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
497 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
499 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
500 thePath
= wxMacCFStringHolder(cfString
).AsString(m_font
.GetEncoding());
503 ::NavDisposeReply(&navReply
);
508 m_fileName
= wxFileNameFromPath(m_path
);
509 m_fileNames
.Add(m_fileName
);
511 // set these to the first hit
513 m_fileName
= wxFileNameFromPath(m_path
);
514 m_dir
= wxPathOnly(m_path
);
516 ::NavDisposeReply(&navReply
);
518 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
519 #else // TARGET_CARBON
521 NavDialogOptions mNavOptions
;
522 NavObjectFilterUPP mNavFilterUPP
= NULL
;
523 NavPreviewUPP mNavPreviewUPP
= NULL
;
524 NavReplyRecord mNavReply
;
525 AEDesc mDefaultLocation
;
526 bool mSelectDefault
= false ;
527 OSStatus err
= noErr
;
531 mNavPreviewUPP
= nil
;
532 mSelectDefault
= false;
533 mDefaultLocation
.descriptorType
= typeNull
;
534 mDefaultLocation
.dataHandle
= nil
;
536 NavGetDefaultDialogOptions(&mNavOptions
);
537 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
538 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
540 // Set default location, the location
541 // that's displayed when the dialog
545 wxMacFilename2FSSpec( m_dir
, &location
) ;
547 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
549 if ( mDefaultLocation
.dataHandle
)
553 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
555 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
559 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
560 mNavReply
.validRecord
= false;
561 mNavReply
.replacing
= false;
562 mNavReply
.isStationery
= false;
563 mNavReply
.translationNeeded
= false;
564 mNavReply
.selection
.descriptorType
= typeNull
;
565 mNavReply
.selection
.dataHandle
= nil
;
566 mNavReply
.keyScript
= smSystemScript
;
567 mNavReply
.fileTranslation
= nil
;
568 mNavReply
.version
= kNavReplyRecordVersion
;
572 m_path
= wxEmptyString
;
573 m_fileName
= wxEmptyString
;
577 OpenUserDataRec myData
;
578 MakeUserDataRec( &myData
, m_wildCard
) ;
579 myData
.currentfilter
= m_filterIndex
;
580 if ( myData
.extensions
.GetCount() > 0 )
582 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
583 myData
.menuitems
= mNavOptions
.popupExtension
;
584 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
586 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
587 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
588 // TODO : according to the new docs -1 to 10 are reserved for the OS
589 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
590 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
593 if ( m_dialogStyle
& wxSAVE
)
595 myData
.saveMode
= true ;
597 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
598 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
604 sStandardNavEventFilter
,
606 kNavGenericSignature
,
607 &myData
); // User Data
608 m_filterIndex
= myData
.currentfilter
;
612 myData
.saveMode
= false ;
614 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
615 if ( m_dialogStyle
& wxMULTIPLE
)
616 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
618 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
624 sStandardNavEventFilter
,
629 m_filterIndex
= myData
.currentfilter
;
632 DisposeNavObjectFilterUPP(mNavFilterUPP
);
633 if ( mDefaultLocation
.dataHandle
!= nil
)
635 ::AEDisposeDesc(&mDefaultLocation
);
638 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
642 if (mNavReply
.validRecord
)
649 ::AECountItems( &mNavReply
.selection
, &count
) ;
650 for ( long i
= 1 ; i
<= count
; ++i
)
652 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
658 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
659 if (specDesc
.dataHandle
!= nil
) {
660 ::AEDisposeDesc(&specDesc
);
662 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
664 m_paths
.Add( m_path
) ;
665 m_fileName
= wxFileNameFromPath(m_path
);
666 m_fileNames
.Add(m_fileName
);
668 // set these to the first hit
669 m_path
= m_paths
[ 0 ] ;
670 m_fileName
= wxFileNameFromPath(m_path
);
671 m_dir
= wxPathOnly(m_path
);
672 NavDisposeReply( &mNavReply
) ;
676 #endif // TARGET_CARBON