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
;
59 wxString defaultLocation
;
61 CFArrayRef menuitems
;
63 NavMenuItemSpecArrayHandle menuitems
;
67 typedef struct OpenUserDataRec
68 OpenUserDataRec
, *OpenUserDataRecPtr
;
70 static pascal void NavEventProc(
71 NavEventCallbackMessage inSelector
,
73 NavCallBackUserData ioUserData
);
76 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
78 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
83 NavEventCallbackMessage inSelector
,
85 NavCallBackUserData ioUserData
)
87 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
88 if (inSelector
== kNavCBEvent
) {
91 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
94 else if ( inSelector
== kNavCBStart
)
97 if (data
&& !(data
->defaultLocation
).IsEmpty())
99 // Set default location for the modern Navigation APIs
100 // Apple Technical Q&A 1151
102 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
103 AEDesc theLocation
= {typeNull
, NULL
};
104 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
105 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
108 if ( data
->menuitems
)
109 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
112 else if ( inSelector
== kNavCBPopupMenuSelect
)
114 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
117 if ( menu
->menuCreator
== 'WXNG' )
120 data
->currentfilter
= menu
->menuType
;
121 if ( data
->saveMode
)
123 int i
= menu
->menuType
;
124 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
125 extension
.MakeLower() ;
129 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
130 sfilename
= cfString
.AsString() ;
133 // get the current filename
134 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
135 sfilename
= wxMacMakeStringFromPascal( filename
) ;
138 int pos
= sfilename
.Find('.', true) ;
139 if ( pos
!= wxNOT_FOUND
)
141 sfilename
= sfilename
.Left(pos
+1)+extension
;
143 cfString
= sfilename
;
144 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
146 wxMacStringToPascal( sfilename
, filename
) ;
147 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
155 const wxChar
* gfilters
[] =
164 OSType gfiltersmac
[] =
174 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
176 myData
->menuitems
= NULL
;
177 myData
->currentfilter
= 0 ;
178 myData
->saveMode
= false ;
180 if ( filter
&& filter
[0] )
182 wxString
filter2(filter
) ;
186 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
188 if( filter2
.GetChar(i
) == wxT('|') )
191 myData
->name
.Add( current
) ;
194 myData
->extensions
.Add( current
.MakeUpper() ) ;
198 current
= wxEmptyString
;
202 current
+= filter2
.GetChar(i
) ;
205 // we allow for compatibility reason to have a single filter expression (like *.*) without
206 // an explanatory text, in that case the first part is name and extension at the same time
208 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
209 if ( current
.IsEmpty() )
210 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
212 myData
->extensions
.Add( current
.MakeUpper() ) ;
213 if ( filterIndex
== 0 || isName
)
214 myData
->name
.Add( current
.MakeUpper() ) ;
218 const size_t extCount
= myData
->extensions
.GetCount();
219 for ( size_t i
= 0 ; i
< extCount
; i
++ )
222 for ( j
= 0 ; gfilters
[j
] ; j
++ )
224 if ( myData
->extensions
[i
] == gfilters
[j
] )
226 myData
->filtermactypes
.Add( gfiltersmac
[j
] ) ;
230 if( gfilters
[j
] == NULL
)
232 myData
->filtermactypes
.Add( '****' ) ;
238 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
240 wxString
file(filename
) ;
243 if ( data
->extensions
.GetCount() > 0 )
245 //for ( int i = 0 ; i < data->numfilters ; ++i )
246 int i
= data
->currentfilter
;
247 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
251 if ( type
== (OSType
)data
->filtermactypes
[i
] )
254 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
255 while( tokenizer
.HasMoreTokens() )
257 wxString extension
= tokenizer
.GetNextToken() ;
258 if ( extension
.GetChar(0) == '*' )
259 extension
= extension
.Mid(1) ;
261 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
271 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
273 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
274 // return true if this item is invisible or a file
279 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
280 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
282 // because the semantics of the filter proc are "true means don't show
283 // it" we need to invert the result that we return
290 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
291 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
300 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
301 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
302 long style
, const wxPoint
& pos
)
303 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
305 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
308 pascal Boolean
CrossPlatformFilterCallback (
312 NavFilterModes filterMode
316 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
318 if (filterMode
== kNavFilteringBrowserList
)
320 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
321 if ( !theInfo
->isFolder
)
323 if (theItem
->descriptorType
== typeFSS
)
326 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
327 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
328 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
331 else if ( theItem
->descriptorType
== typeFSRef
)
334 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
336 const short maxpath
= 1024 ;
337 FSRefMakePath( &fsref
, (UInt8
*) file
.GetWriteBuf(maxpath
+1),maxpath
) ;
338 file
.UngetWriteBuf() ;
339 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
348 int wxFileDialog::ShowModal()
352 NavDialogCreationOptions dialogCreateOptions
;
353 // set default options
354 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
356 // this was always unset in the old code
357 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
360 // tried using wxMacCFStringHolder in the code below, but it seems
361 // the CFStrings were being released before the save dialog was called,
362 // causing a crash - open dialog works fine with or without wxMacCFStringHolder
363 CFStringRef titleRef
= ::CFStringCreateWithCharacters( kCFAllocatorDefault
,
364 (const unsigned short*)m_message
.wc_str(),
367 CFStringRef titleRef
= ::CFStringCreateWithCString(NULL
,
369 CFStringGetSystemEncoding());
371 dialogCreateOptions
.windowTitle
= titleRef
;
373 CFStringRef defaultFileNameRef
= ::CFStringCreateWithCharacters( kCFAllocatorDefault
,
374 (const unsigned short*)m_fileName
.wc_str(),
377 CFStringRef defaultFileNameRef
= ::CFStringCreateWithCString(NULL
,
379 CFStringGetSystemEncoding());
381 dialogCreateOptions
.saveFileName
= defaultFileNameRef
;
383 NavObjectFilterUPP navFilterUPP
= NULL
;
384 CFArrayRef cfArray
= NULL
; // for popupExtension
385 OpenUserDataRec myData
;
386 myData
.defaultLocation
= m_dir
;
388 if (m_dialogStyle
& wxSAVE
)
390 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
391 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
392 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
394 // The extension is important
395 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
397 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
400 sStandardNavEventFilter
,
401 &myData
, // for defaultLocation
406 MakeUserDataRec(&myData
, m_wildCard
);
407 size_t numfilters
= myData
.extensions
.GetCount();
410 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
411 numfilters
, &kCFTypeArrayCallBacks
) ;
412 dialogCreateOptions
.popupExtension
= popup
;
413 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
414 for ( size_t i
= 0 ; i
< numfilters
; ++i
)
416 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] ) ) ;
420 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
421 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
422 NULL
, // NavTypeListHandle
423 sStandardNavEventFilter
,
424 NULL
, // NavPreviewUPP
426 (void *) &myData
, // inClientData
431 err
= ::NavDialogRun(dialog
);
433 // clean up filter related data, etc.
435 ::DisposeNavObjectFilterUPP(navFilterUPP
);
438 CFIndex n
= ::CFArrayGetCount(cfArray
);
439 for (CFIndex i
= 0; i
< n
; i
++)
441 CFStringRef str
= (CFStringRef
) ::CFArrayGetValueAtIndex(cfArray
, i
);
445 ::CFRelease(cfArray
);
448 ::CFRelease(titleRef
);
449 if (defaultFileNameRef
)
450 ::CFRelease(defaultFileNameRef
);
454 NavReplyRecord navReply
;
455 err
= ::NavDialogGetReply(dialog
, &navReply
);
456 if (err
== noErr
&& navReply
.validRecord
)
458 AEKeyword theKeyword
;
464 ::AECountItems(&navReply
.selection
, &count
);
465 for (long i
= 1; i
<= count
; ++i
)
467 err
= ::AEGetNthPtr(&(navReply
.selection
), 1, typeFSRef
, &theKeyword
, &actualType
,
468 &theFSRef
, sizeof(theFSRef
), &actualSize
);
473 if (m_dialogStyle
& wxSAVE
)
475 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
480 ::CFURLCreateCopyAppendingPathComponent(NULL
,
482 navReply
.saveFileName
,
484 ::CFRelease(parentURLRef
);
489 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
492 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
494 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
496 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
497 thePath
= wxMacCFStringHolder(cfString
).AsString();
500 ::NavDisposeReply(&navReply
);
505 m_fileName
= wxFileNameFromPath(m_path
);
506 m_fileNames
.Add(m_fileName
);
508 // set these to the first hit
510 m_fileName
= wxFileNameFromPath(m_path
);
511 m_dir
= wxPathOnly(m_path
);
513 ::NavDisposeReply(&navReply
);
515 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
516 #else // TARGET_CARBON
518 NavDialogOptions mNavOptions
;
519 NavObjectFilterUPP mNavFilterUPP
= NULL
;
520 NavPreviewUPP mNavPreviewUPP
= NULL
;
521 NavReplyRecord mNavReply
;
522 AEDesc mDefaultLocation
;
523 bool mSelectDefault
= false ;
524 OSStatus err
= noErr
;
528 mNavPreviewUPP
= nil
;
529 mSelectDefault
= false;
530 mDefaultLocation
.descriptorType
= typeNull
;
531 mDefaultLocation
.dataHandle
= nil
;
533 NavGetDefaultDialogOptions(&mNavOptions
);
534 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
535 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
537 // Set default location, the location
538 // that's displayed when the dialog
542 wxMacFilename2FSSpec( m_dir
, &location
) ;
544 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
546 if ( mDefaultLocation
.dataHandle
)
550 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
552 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
556 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
557 mNavReply
.validRecord
= false;
558 mNavReply
.replacing
= false;
559 mNavReply
.isStationery
= false;
560 mNavReply
.translationNeeded
= false;
561 mNavReply
.selection
.descriptorType
= typeNull
;
562 mNavReply
.selection
.dataHandle
= nil
;
563 mNavReply
.keyScript
= smSystemScript
;
564 mNavReply
.fileTranslation
= nil
;
565 mNavReply
.version
= kNavReplyRecordVersion
;
569 m_path
= wxEmptyString
;
570 m_fileName
= wxEmptyString
;
574 OpenUserDataRec myData
;
575 MakeUserDataRec( &myData
, m_wildCard
) ;
576 myData
.currentfilter
= m_filterIndex
;
577 if ( myData
.extensions
.GetCount() > 0 )
579 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
580 myData
.menuitems
= mNavOptions
.popupExtension
;
581 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
583 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
584 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
585 // TODO : according to the new docs -1 to 10 are reserved for the OS
586 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
587 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
590 if ( m_dialogStyle
& wxSAVE
)
592 myData
.saveMode
= true ;
594 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
595 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
601 sStandardNavEventFilter
,
603 kNavGenericSignature
,
604 &myData
); // User Data
605 m_filterIndex
= myData
.currentfilter
;
609 myData
.saveMode
= false ;
611 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
612 if ( m_dialogStyle
& wxMULTIPLE
)
613 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
615 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
621 sStandardNavEventFilter
,
626 m_filterIndex
= myData
.currentfilter
;
629 DisposeNavObjectFilterUPP(mNavFilterUPP
);
630 if ( mDefaultLocation
.dataHandle
!= nil
)
632 ::AEDisposeDesc(&mDefaultLocation
);
635 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
639 if (mNavReply
.validRecord
)
646 ::AECountItems( &mNavReply
.selection
, &count
) ;
647 for ( long i
= 1 ; i
<= count
; ++i
)
649 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
655 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
656 if (specDesc
.dataHandle
!= nil
) {
657 ::AEDisposeDesc(&specDesc
);
659 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
661 m_paths
.Add( m_path
) ;
662 m_fileName
= wxFileNameFromPath(m_path
);
663 m_fileNames
.Add(m_fileName
);
665 // set these to the first hit
666 m_path
= m_paths
[ 0 ] ;
667 m_fileName
= wxFileNameFromPath(m_path
);
668 m_dir
= wxPathOnly(m_path
);
669 NavDisposeReply( &mNavReply
) ;
673 #endif // TARGET_CARBON