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"
18 #include "wx/filedlg.h"
24 #include "wx/dialog.h"
27 #include "wx/tokenzr.h"
28 #include "wx/filename.h"
31 #include "PLStringFuncs.h"
34 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
38 #include "wx/mac/private.h"
40 #include <Navigation.h>
43 # include "MoreFilesX.h"
45 # include "MoreFiles.h"
46 # include "MoreFilesExtras.h"
49 extern bool gUseNavServices
;
51 // the data we need to pass to our standard file hook routine
52 // includes a pointer to the dialog, a pointer to the standard
53 // file reply record (so we can inspect the current selection)
54 // and a copy of the "previous" file spec of the reply record
55 // so we can see if the selection has changed
57 struct OpenUserDataRec
{
61 wxArrayString extensions
;
62 wxArrayLong filtermactypes
;
63 wxString defaultLocation
;
65 CFArrayRef menuitems
;
67 NavMenuItemSpecArrayHandle menuitems
;
71 typedef struct OpenUserDataRec
72 OpenUserDataRec
, *OpenUserDataRecPtr
;
74 static pascal void NavEventProc(
75 NavEventCallbackMessage inSelector
,
77 NavCallBackUserData ioUserData
);
80 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
82 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
87 NavEventCallbackMessage inSelector
,
89 NavCallBackUserData ioUserData
)
91 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
92 if (inSelector
== kNavCBEvent
) {
95 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
98 else if ( inSelector
== kNavCBStart
)
101 if (data
&& !(data
->defaultLocation
).empty())
103 // Set default location for the modern Navigation APIs
104 // Apple Technical Q&A 1151
106 wxMacFilename2FSSpec(data
->defaultLocation
, &theFSSpec
);
107 AEDesc theLocation
= {typeNull
, NULL
};
108 if (noErr
== ::AECreateDesc(typeFSS
, &theFSSpec
, sizeof(FSSpec
), &theLocation
))
109 ::NavCustomControl(ioParams
->context
, kNavCtlSetLocation
, (void *) &theLocation
);
112 if ( data
->menuitems
)
113 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
116 else if ( inSelector
== kNavCBPopupMenuSelect
)
118 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
121 if ( menu
->menuCreator
== 'WXNG' )
124 data
->currentfilter
= menu
->menuType
;
125 if ( data
->saveMode
)
127 int i
= menu
->menuType
;
128 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
129 extension
.MakeLower() ;
133 wxMacCFStringHolder
cfString( NavDialogGetSaveFileName( ioParams
->context
) , false );
134 sfilename
= cfString
.AsString() ;
137 // get the current filename
138 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
139 sfilename
= wxMacMakeStringFromPascal( filename
) ;
142 int pos
= sfilename
.Find('.', true) ;
143 if ( pos
!= wxNOT_FOUND
)
145 sfilename
= sfilename
.Left(pos
+1)+extension
;
147 cfString
.Assign( sfilename
, wxFONTENCODING_DEFAULT
) ;
148 NavDialogSetSaveFileName( ioParams
->context
, cfString
) ;
150 wxMacStringToPascal( sfilename
, filename
) ;
151 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
160 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
162 myData
->menuitems
= NULL
;
163 myData
->currentfilter
= 0 ;
164 myData
->saveMode
= false ;
166 if ( filter
&& filter
[0] )
168 wxString
filter2(filter
) ;
172 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
174 if( filter2
.GetChar(i
) == wxT('|') )
177 myData
->name
.Add( current
) ;
180 myData
->extensions
.Add( current
.MakeUpper() ) ;
184 current
= wxEmptyString
;
188 current
+= filter2
.GetChar(i
) ;
191 // we allow for compatibility reason to have a single filter expression (like *.*) without
192 // an explanatory text, in that case the first part is name and extension at the same time
194 wxASSERT_MSG( filterIndex
== 0 || !isName
, wxT("incorrect format of format string") ) ;
195 if ( current
.empty() )
196 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
198 myData
->extensions
.Add( current
.MakeUpper() ) ;
199 if ( filterIndex
== 0 || isName
)
200 myData
->name
.Add( current
.MakeUpper() ) ;
204 const size_t extCount
= myData
->extensions
.GetCount();
205 for ( size_t i
= 0 ; i
< extCount
; i
++ )
209 wxString extension
= myData
->extensions
[i
];
211 if (extension
.GetChar(0) == '*')
212 extension
= extension
.Mid(1); // Remove leading *
214 if (extension
.GetChar(0) == '.')
216 extension
= extension
.Mid(1); // Remove leading .
219 if (wxFileName::MacFindDefaultTypeAndCreator( extension
, &fileType
, &creator
))
221 myData
->filtermactypes
.Add( (OSType
)fileType
);
225 myData
->filtermactypes
.Add( '****' ) ; // We'll fail safe if it's not recognized
231 static Boolean
CheckFile( const wxString
&filename
, OSType type
, OpenUserDataRecPtr data
)
233 wxString
file(filename
) ;
236 if ( data
->extensions
.GetCount() > 0 )
238 //for ( int i = 0 ; i < data->numfilters ; ++i )
239 int i
= data
->currentfilter
;
240 if ( data
->extensions
[i
].Right(2) == wxT(".*") )
244 if ( type
== (OSType
)data
->filtermactypes
[i
] )
247 wxStringTokenizer
tokenizer( data
->extensions
[i
] , wxT(";") ) ;
248 while( tokenizer
.HasMoreTokens() )
250 wxString extension
= tokenizer
.GetNextToken() ;
251 if ( extension
.GetChar(0) == '*' )
252 extension
= extension
.Mid(1) ;
254 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
264 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
266 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
267 // return true if this item is invisible or a file
272 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
273 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
275 // because the semantics of the filter proc are "true means don't show
276 // it" we need to invert the result that we return
283 wxString file
= wxMacMakeStringFromPascal( myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
284 return !CheckFile( file
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
293 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
294 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
295 long style
, const wxPoint
& pos
, const wxSize
& sz
, const wxString
& name
)
296 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
)
298 wxASSERT_MSG( NavServicesAvailable() , wxT("Navigation Services are not running") ) ;
301 pascal Boolean
CrossPlatformFilterCallback (
305 NavFilterModes filterMode
309 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
311 if (filterMode
== kNavFilteringBrowserList
)
313 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
314 if ( !theInfo
->isFolder
)
316 if (theItem
->descriptorType
== typeFSS
)
319 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
320 wxString file
= wxMacMakeStringFromPascal( spec
.name
) ;
321 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
324 else if ( theItem
->descriptorType
== typeFSRef
)
327 memcpy( &fsref
, *theItem
->dataHandle
, sizeof(FSRef
) ) ;
332 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &fsref
);
334 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
336 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
338 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
339 ::CFRelease( fullURLRef
) ;
340 wxString file
= wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
342 display
= CheckFile( file
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
351 int wxFileDialog::ShowModal()
355 NavDialogCreationOptions dialogCreateOptions
;
356 // set default options
357 ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions
);
359 // this was always unset in the old code
360 dialogCreateOptions
.optionFlags
&= ~kNavSelectDefaultLocation
;
362 wxMacCFStringHolder
message(m_message
, m_font
.GetEncoding());
363 dialogCreateOptions
.windowTitle
= message
;
365 wxMacCFStringHolder
defaultFileName(m_fileName
, m_font
.GetEncoding());
366 dialogCreateOptions
.saveFileName
= defaultFileName
;
370 NavObjectFilterUPP navFilterUPP
= NULL
;
371 CFArrayRef cfArray
= NULL
; // for popupExtension
372 OpenUserDataRec myData
;
373 myData
.defaultLocation
= m_dir
;
375 if (HasFlag(wxFD_SAVE
))
377 dialogCreateOptions
.optionFlags
|= kNavNoTypePopup
;
378 dialogCreateOptions
.optionFlags
|= kNavDontAutoTranslate
;
379 dialogCreateOptions
.optionFlags
|= kNavDontAddTranslateItems
;
381 // The extension is important
382 dialogCreateOptions
.optionFlags
|= kNavPreserveSaveFileExtension
;
384 err
= ::NavCreatePutFileDialog(&dialogCreateOptions
,
387 sStandardNavEventFilter
,
388 &myData
, // for defaultLocation
393 MakeUserDataRec(&myData
, m_wildCard
);
394 size_t numfilters
= myData
.extensions
.GetCount();
397 CFMutableArrayRef popup
= CFArrayCreateMutable( kCFAllocatorDefault
,
398 numfilters
, &kCFTypeArrayCallBacks
) ;
399 dialogCreateOptions
.popupExtension
= popup
;
400 myData
.menuitems
= dialogCreateOptions
.popupExtension
;
401 for ( size_t i
= 0 ; i
< numfilters
; ++i
)
403 CFArrayAppendValue( popup
, (CFStringRef
) wxMacCFStringHolder( myData
.name
[i
] , m_font
.GetEncoding() ) ) ;
407 navFilterUPP
= NewNavObjectFilterUPP(CrossPlatformFilterCallback
);
408 err
= ::NavCreateGetFileDialog(&dialogCreateOptions
,
409 NULL
, // NavTypeListHandle
410 sStandardNavEventFilter
,
411 NULL
, // NavPreviewUPP
413 (void *) &myData
, // inClientData
418 err
= ::NavDialogRun(dialog
);
420 // clean up filter related data, etc.
422 ::DisposeNavObjectFilterUPP(navFilterUPP
);
424 ::CFRelease(cfArray
);
429 NavReplyRecord navReply
;
430 err
= ::NavDialogGetReply(dialog
, &navReply
);
431 if (err
== noErr
&& navReply
.validRecord
)
433 AEKeyword theKeyword
;
439 ::AECountItems(&navReply
.selection
, &count
);
440 for (long i
= 1; i
<= count
; ++i
)
442 err
= ::AEGetNthPtr(&(navReply
.selection
), i
, typeFSRef
, &theKeyword
, &actualType
,
443 &theFSRef
, sizeof(theFSRef
), &actualSize
);
448 if (HasFlag(wxFD_SAVE
))
450 CFURLRef parentURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
455 ::CFURLCreateCopyAppendingPathComponent(NULL
,
457 navReply
.saveFileName
,
459 ::CFRelease(parentURLRef
);
464 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theFSRef
);
467 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
469 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
471 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
472 thePath
= wxMacCFStringHolder(cfString
).AsString(m_font
.GetEncoding());
475 ::NavDisposeReply(&navReply
);
480 m_fileName
= wxFileNameFromPath(m_path
);
481 m_fileNames
.Add(m_fileName
);
483 // set these to the first hit
485 m_fileName
= wxFileNameFromPath(m_path
);
486 m_dir
= wxPathOnly(m_path
);
488 ::NavDisposeReply(&navReply
);
490 return (err
== noErr
) ? wxID_OK
: wxID_CANCEL
;
491 #else // TARGET_CARBON
493 NavDialogOptions mNavOptions
;
494 NavObjectFilterUPP mNavFilterUPP
= NULL
;
495 NavPreviewUPP mNavPreviewUPP
= NULL
;
496 NavReplyRecord mNavReply
;
497 AEDesc mDefaultLocation
;
498 bool mSelectDefault
= false ;
499 OSStatus err
= noErr
;
503 mNavPreviewUPP
= nil
;
504 mSelectDefault
= false;
505 mDefaultLocation
.descriptorType
= typeNull
;
506 mDefaultLocation
.dataHandle
= nil
;
508 NavGetDefaultDialogOptions(&mNavOptions
);
509 wxMacStringToPascal( m_message
, (StringPtr
)mNavOptions
.message
) ;
510 wxMacStringToPascal( m_fileName
, (StringPtr
)mNavOptions
.savedFileName
) ;
512 // Set default location, the location
513 // that's displayed when the dialog
517 wxMacFilename2FSSpec( m_dir
, &location
) ;
519 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
521 if ( mDefaultLocation
.dataHandle
)
525 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
527 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
531 memset( &mNavReply
, 0 , sizeof( mNavReply
) ) ;
532 mNavReply
.validRecord
= false;
533 mNavReply
.replacing
= false;
534 mNavReply
.isStationery
= false;
535 mNavReply
.translationNeeded
= false;
536 mNavReply
.selection
.descriptorType
= typeNull
;
537 mNavReply
.selection
.dataHandle
= nil
;
538 mNavReply
.keyScript
= smSystemScript
;
539 mNavReply
.fileTranslation
= nil
;
540 mNavReply
.version
= kNavReplyRecordVersion
;
544 m_path
= wxEmptyString
;
545 m_fileName
= wxEmptyString
;
549 OpenUserDataRec myData
;
550 MakeUserDataRec( &myData
, m_wildCard
) ;
551 myData
.currentfilter
= m_filterIndex
;
552 if ( myData
.extensions
.GetCount() > 0 )
554 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
555 myData
.menuitems
= mNavOptions
.popupExtension
;
556 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
558 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
559 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
560 // TODO : according to the new docs -1 to 10 are reserved for the OS
561 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
562 wxMacStringToPascal( myData
.name
[i
] , (StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
565 if ( HasFlag(wxFD_SAVE
) )
567 myData
.saveMode
= true ;
569 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
570 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
576 sStandardNavEventFilter
,
578 kNavGenericSignature
,
579 &myData
); // User Data
580 m_filterIndex
= myData
.currentfilter
;
584 myData
.saveMode
= false ;
586 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
587 if ( m_windowStyle
& wxMULTIPLE
)
588 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
590 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
596 sStandardNavEventFilter
,
601 m_filterIndex
= myData
.currentfilter
;
604 DisposeNavObjectFilterUPP(mNavFilterUPP
);
605 if ( mDefaultLocation
.dataHandle
!= nil
)
607 ::AEDisposeDesc(&mDefaultLocation
);
610 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
614 if (mNavReply
.validRecord
)
621 ::AECountItems( &mNavReply
.selection
, &count
) ;
622 for ( long i
= 1 ; i
<= count
; ++i
)
624 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
627 m_path
= wxEmptyString
;
630 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
631 if (specDesc
.dataHandle
!= nil
) {
632 ::AEDisposeDesc(&specDesc
);
634 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
636 m_paths
.Add( m_path
) ;
637 m_fileName
= wxFileNameFromPath(m_path
);
638 m_fileNames
.Add(m_fileName
);
640 // set these to the first hit
641 m_path
= m_paths
[ 0 ] ;
642 m_fileName
= wxFileNameFromPath(m_path
);
643 m_dir
= wxPathOnly(m_path
);
644 NavDisposeReply( &mNavReply
) ;
648 #endif // TARGET_CARBON