1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/dialog.h"
21 #include "wx/filedlg.h"
23 #include "wx/tokenzr.h"
24 #include "wx/filename.h"
27 #include "PLStringFuncs.h"
30 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
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
144 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
146 wxMacStringToPascal( sfilename
, filename
) ;
147 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
156 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
158 myData
->menuitems
= NULL
;
159 myData
->currentfilter
= 0 ;
160 myData
->saveMode
= false ;
162 if ( filter
&& filter
[0] )
164 wxString
filter2(filter
) ;
168 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
170 if( filter2
.GetChar(i
) == wxT('|') )
173 myData
->name
.Add( current
) ;
176 myData
->extensions
.Add( current
.MakeUpper() ) ;
180 current
= wxEmptyString
;
184 current
+= filter2
.GetChar(i
) ;
187 // we allow for compatibility reason to have a single filter expression (like *.*) without
188 // an explanatory text, in that case the first part is name and extension at the same time
190 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
191 if ( current
.empty() )
192 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
194 myData
->extensions
.Add( current
.MakeUpper() ) ;
195 if ( filterIndex
== 0 || isName
)
196 myData
->name
.Add( current
.MakeUpper() ) ;
200 const size_t extCount
= myData
->extensions
.GetCount();
201 for ( size_t i
= 0 ; i
< extCount
; i
++ )
205 wxString extension
= myData
->extensions
[i
];
207 if (extension
.GetChar(0) == '*')
208 extension
= extension
.Mid(1); // Remove leading *
210 if (extension
.GetChar(0) == '.')
212 extension
= extension
.Mid(1); // Remove leading .
215 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
217 myData
->filtermactypes
.Add( (OSType
)fileType
);
221 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
227 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
229 wxString
file(filename
) ;
232 if ( data
->extensions
.GetCount() > 0 )
234 //for ( int i = 0 ; i < data->numfilters ; ++i )
235 int i
= data
->currentfilter
;
236 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
240 if ( type
== (OSType
)data
->filtermactypes
[i
] )
243 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
244 while( tokenizer
.HasMoreTokens() )
246 wxString extension
= tokenizer
.GetNextToken() ;
247 if ( extension
.GetChar(0) == '*' )
248 extension
= extension
.Mid(1) ;
250 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
260 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
262 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
263 // return true if this item is invisible or a file
268 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
269 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
271 // because the semantics of the filter proc are "true means don't show
272 // it" we need to invert the result that we return
279 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
280 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
289 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
290 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
291 long style
, const wxPoint
& pos
)
292 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
294 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
297 pascal Boolean
CrossPlatformFilterCallback (
301 NavFilterModes filterMode
305 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
307 if (filterMode
== kNavFilteringBrowserList
)
309 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
310 if ( !theInfo
->isFolder
)
312 if (theItem
->descriptorType
== typeFSS
)
315 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
316 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
317 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
320 else if ( theItem
->descriptorType
== typeFSRef
)
323 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
328 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &fsref
);
330 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
332 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
334 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
335 ::CFRelease( fullURLRef
) ;
336 wxString file
= wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
338 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
347 int wxFileDialog::ShowModal()
351 NavDialogCreationOptions dialogCreateOptions
;
352 // set default options
353 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
355 // this was always unset in the old code
356 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
358 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
359 dialogCreateOptions
.windowTitle
= message
;
361 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
362 dialogCreateOptions
.saveFileName
= defaultFileName
;
366 NavObjectFilterUPP navFilterUPP
= NULL
;
367 CFArrayRef cfArray
= NULL
; // for popupExtension
368 OpenUserDataRec myData
;
369 myData
.defaultLocation
= m_dir
;
371 if (m_dialogStyle
& wxSAVE
)
373 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
374 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
375 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
377 // The extension is important
378 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
380 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
383 sStandardNavEventFilter
,
384 &myData
, // for defaultLocation
389 MakeUserDataRec(&myData
, m_wildCard
);
390 size_t numfilters
= myData
.extensions
.GetCount();
393 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
394 numfilters
, &kCFTypeArrayCallBacks
) ;
395 dialogCreateOptions
.popupExtension
= popup
;
396 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
397 for ( size_t i
= 0 ; i
< numfilters
; ++i
)
399 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
403 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
404 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
405 NULL
, // NavTypeListHandle
406 sStandardNavEventFilter
,
407 NULL
, // NavPreviewUPP
409 (void *) &myData
, // inClientData
414 err
= ::NavDialogRun(dialog
);
416 // clean up filter related data, etc.
418 ::DisposeNavObjectFilterUPP(navFilterUPP
);
420 ::CFRelease(cfArray
);
425 NavReplyRecord navReply
;
426 err
= ::NavDialogGetReply(dialog
, &navReply
);
427 if (err
== noErr
&& navReply
.validRecord
)
429 AEKeyword theKeyword
;
435 ::AECountItems(&navReply
.selection
, &count
);
436 for (long i
= 1; i
<= count
; ++i
)
438 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
439 &theFSRef
, sizeof(theFSRef
), &actualSize
);
444 if (m_dialogStyle
& wxSAVE
)
446 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
451 ::CFURLCreateCopyAppendingPathComponent(NULL
,
453 navReply
.saveFileName
,
455 ::CFRelease(parentURLRef
);
460 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
463 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
465 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
467 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
468 thePath
= wxMacCFStringHolder(cfString
).AsString(m_font
.GetEncoding());
471 ::NavDisposeReply(&navReply
);
476 m_fileName
= wxFileNameFromPath(m_path
);
477 m_fileNames
.Add(m_fileName
);
479 // set these to the first hit
481 m_fileName
= wxFileNameFromPath(m_path
);
482 m_dir
= wxPathOnly(m_path
);
484 ::NavDisposeReply(&navReply
);
486 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
487 #else // TARGET_CARBON
489 NavDialogOptions mNavOptions
;
490 NavObjectFilterUPP mNavFilterUPP
= NULL
;
491 NavPreviewUPP mNavPreviewUPP
= NULL
;
492 NavReplyRecord mNavReply
;
493 AEDesc mDefaultLocation
;
494 bool mSelectDefault
= false ;
495 OSStatus err
= noErr
;
499 mNavPreviewUPP
= nil
;
500 mSelectDefault
= false;
501 mDefaultLocation
.descriptorType
= typeNull
;
502 mDefaultLocation
.dataHandle
= nil
;
504 NavGetDefaultDialogOptions(&mNavOptions
);
505 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
506 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
508 // Set default location, the location
509 // that's displayed when the dialog
513 wxMacFilename2FSSpec( m_dir
, &location
) ;
515 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
517 if ( mDefaultLocation
.dataHandle
)
521 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
523 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
527 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
528 mNavReply
.validRecord
= false;
529 mNavReply
.replacing
= false;
530 mNavReply
.isStationery
= false;
531 mNavReply
.translationNeeded
= false;
532 mNavReply
.selection
.descriptorType
= typeNull
;
533 mNavReply
.selection
.dataHandle
= nil
;
534 mNavReply
.keyScript
= smSystemScript
;
535 mNavReply
.fileTranslation
= nil
;
536 mNavReply
.version
= kNavReplyRecordVersion
;
540 m_path
= wxEmptyString
;
541 m_fileName
= wxEmptyString
;
545 OpenUserDataRec myData
;
546 MakeUserDataRec( &myData
, m_wildCard
) ;
547 myData
.currentfilter
= m_filterIndex
;
548 if ( myData
.extensions
.GetCount() > 0 )
550 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
551 myData
.menuitems
= mNavOptions
.popupExtension
;
552 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
554 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
555 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
556 // TODO : according to the new docs -1 to 10 are reserved for the OS
557 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
558 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
561 if ( m_dialogStyle
& wxSAVE
)
563 myData
.saveMode
= true ;
565 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
566 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
572 sStandardNavEventFilter
,
574 kNavGenericSignature
,
575 &myData
); // User Data
576 m_filterIndex
= myData
.currentfilter
;
580 myData
.saveMode
= false ;
582 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
583 if ( m_dialogStyle
& wxMULTIPLE
)
584 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
586 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
592 sStandardNavEventFilter
,
597 m_filterIndex
= myData
.currentfilter
;
600 DisposeNavObjectFilterUPP(mNavFilterUPP
);
601 if ( mDefaultLocation
.dataHandle
!= nil
)
603 ::AEDisposeDesc(&mDefaultLocation
);
606 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
610 if (mNavReply
.validRecord
)
617 ::AECountItems( &mNavReply
.selection
, &count
) ;
618 for ( long i
= 1 ; i
<= count
; ++i
)
620 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
623 m_path
= wxEmptyString
;
626 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
627 if (specDesc
.dataHandle
!= nil
) {
628 ::AEDisposeDesc(&specDesc
);
630 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
632 m_paths
.Add( m_path
) ;
633 m_fileName
= wxFileNameFromPath(m_path
);
634 m_fileNames
.Add(m_fileName
);
636 // set these to the first hit
637 m_path
= m_paths
[ 0 ] ;
638 m_fileName
= wxFileNameFromPath(m_path
);
639 m_dir
= wxPathOnly(m_path
);
640 NavDisposeReply( &mNavReply
) ;
644 #endif // TARGET_CARBON