1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
8 // Copyright: (c) AUTHOR
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
, wxDialog
)
34 #include "wx/mac/private.h"
36 #include <Navigation.h>
38 #include "MoreFiles.h"
39 #include "MoreFilesExtras.h"
41 extern bool gUseNavServices
;
43 // the data we need to pass to our standard file hook routine
44 // includes a pointer to the dialog, a pointer to the standard
45 // file reply record (so we can inspect the current selection)
46 // and a copy of the "previous" file spec of the reply record
47 // so we can see if the selection has changed
49 struct OpenUserDataRec
{
53 wxArrayString extensions
;
54 wxArrayLong filtermactypes
;
55 NavMenuItemSpecArrayHandle menuitems
;
58 typedef struct OpenUserDataRec
59 OpenUserDataRec
, *OpenUserDataRecPtr
;
61 static pascal void NavEventProc(
62 NavEventCallbackMessage inSelector
,
64 NavCallBackUserData ioUserData
);
67 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
69 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
74 NavEventCallbackMessage inSelector
,
76 NavCallBackUserData ioUserData
)
78 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
79 if (inSelector
== kNavCBEvent
) {
80 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
82 else if ( inSelector
== kNavCBStart
)
84 if ( data
->menuitems
)
85 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
87 else if ( inSelector
== kNavCBPopupMenuSelect
)
89 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
90 if ( menu
->menuCreator
== 'WXNG' )
92 data
->currentfilter
= menu
->menuType
;
95 int i
= menu
->menuType
;
96 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
97 extension
.MakeLower() ;
99 // get the current filename
100 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
101 CopyPascalStringToC( filename
, (char*) filename
) ;
102 wxString
sfilename( filename
) ;
103 int pos
= sfilename
.Find('.',TRUE
) ;
104 if ( pos
!= wxNOT_FOUND
)
106 sfilename
= sfilename
.Left(pos
+1)+extension
;
107 CopyCStringToPascal( sfilename
.c_str() , filename
) ;
108 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
115 const char * gfilters
[] =
124 OSType gfiltersmac
[] =
135 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
137 myData
->menuitems
= NULL
;
138 myData
->currentfilter
= 0 ;
139 myData
->saveMode
= FALSE
;
141 if ( filter
&& filter
[0] )
143 wxString
filter2(filter
) ;
147 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
149 if( filter2
.GetChar(i
) == wxT('|') )
152 myData
->name
.Add( current
) ;
155 myData
->extensions
.Add( current
.MakeUpper() ) ;
163 current
+= filter2
.GetChar(i
) ;
166 // we allow for compatibility reason to have a single filter expression (like *.*) without
167 // an explanatory text, in that case the first part is name and extension at the same time
169 wxASSERT_MSG( filterIndex
== 0 || !isName
, "incorrect format of format string" ) ;
170 if ( current
.IsEmpty() )
171 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
173 myData
->extensions
.Add( current
.MakeUpper() ) ;
174 if ( filterIndex
== 0 || isName
)
175 myData
->name
.Add( current
.MakeUpper() ) ;
180 const size_t extCount
= myData
->extensions
.GetCount();
181 for ( size_t i
= 0 ; i
< extCount
; i
++ )
184 for ( j
= 0 ; gfilters
[j
] ; j
++ )
186 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
188 myData
->filtermactypes
.Add( gfiltersmac
[j
] ) ;
192 if( gfilters
[j
] == NULL
)
194 myData
->filtermactypes
.Add( '****' ) ;
200 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
205 p2cstrcpy((char *)filename
, name
) ;
207 PLstrcpy( filename
, name
) ;
210 wxString
file(filename
) ;
213 if ( data
->extensions
.GetCount() > 0 )
215 //for ( int i = 0 ; i < data->numfilters ; ++i )
216 int i
= data
->currentfilter
;
217 if ( data
->extensions
[i
].Right(2) == ".*" )
221 if ( type
== (OSType
)data
->filtermactypes
[i
] )
224 wxStringTokenizer
tokenizer( data
->extensions
[i
] , ";" ) ;
225 while( tokenizer
.HasMoreTokens() )
227 wxString extension
= tokenizer
.GetNextToken() ;
228 if ( extension
.GetChar(0) == '*' )
229 extension
= extension
.Mid(1) ;
231 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
241 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
243 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
244 // return true if this item is invisible or a file
249 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
250 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
252 // because the semantics of the filter proc are "true means don't show
253 // it" we need to invert the result that we return
260 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
269 wxString
wxFileSelector(const char *title
,
270 const char *defaultDir
, const char *defaultFileName
,
271 const char *defaultExtension
, const char *filter
, int flags
,
272 wxWindow
*parent
, int x
, int y
)
274 // If there's a default extension specified but no filter, we create a suitable
277 wxString
filter2("");
278 if ( defaultExtension
&& !filter
)
279 filter2
= wxString("*.") + wxString(defaultExtension
) ;
283 wxString defaultDirString
;
285 defaultDirString
= defaultDir
;
287 defaultDirString
= "";
289 wxString defaultFilenameString
;
291 defaultFilenameString
= defaultFileName
;
293 defaultFilenameString
= "";
295 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
297 if ( fileDialog
.ShowModal() == wxID_OK
)
299 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
303 return wxGetEmptyString();
306 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
307 const char *defaultDir
,
308 const char *defaultFileName
,
309 int* defaultFilterIndex
,
317 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
318 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
320 if ( fileDialog
.ShowModal() == wxID_OK
)
322 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
323 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
327 return wxGetEmptyString();
330 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
331 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
332 long style
, const wxPoint
& pos
)
334 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
336 m_dialogStyle
= style
;
339 m_fileName
= defaultFileName
;
341 m_wildCard
= wildCard
;
346 pascal Boolean
CrossPlatformFilterCallback (
350 NavFilterModes filterMode
354 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
356 if (filterMode
== kNavFilteringBrowserList
)
358 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
359 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
362 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
363 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
370 int wxFileDialog::ShowModal()
372 NavDialogOptions mNavOptions
;
373 NavObjectFilterUPP mNavFilterUPP
= NULL
;
374 NavPreviewUPP mNavPreviewUPP
= NULL
;
375 NavReplyRecord mNavReply
;
376 AEDesc mDefaultLocation
;
377 bool mSelectDefault
= false ;
379 ::NavGetDefaultDialogOptions(&mNavOptions
);
382 mNavPreviewUPP
= nil
;
383 mSelectDefault
= false;
384 mNavReply
.validRecord
= false;
385 mNavReply
.replacing
= false;
386 mNavReply
.isStationery
= false;
387 mNavReply
.translationNeeded
= false;
388 mNavReply
.selection
.descriptorType
= typeNull
;
389 mNavReply
.selection
.dataHandle
= nil
;
390 mNavReply
.keyScript
= smSystemScript
;
391 mNavReply
.fileTranslation
= nil
;
393 // Set default location, the location
394 // that's displayed when the dialog
398 wxMacFilename2FSSpec( m_dir
, &location
) ;
401 mDefaultLocation
.descriptorType
= typeNull
;
402 mDefaultLocation
.dataHandle
= nil
;
404 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
406 if ( mDefaultLocation
.dataHandle
) {
408 if (mSelectDefault
) {
409 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
411 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
416 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
418 strcpy((char *)mNavOptions
.message
, m_message
) ;
419 c2pstr((char *)mNavOptions
.message
) ;
422 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
424 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
425 c2pstr((char *)mNavOptions
.savedFileName
) ;
428 OpenUserDataRec myData
;
429 MakeUserDataRec( &myData
, m_wildCard
) ;
430 myData
.currentfilter
= m_filterIndex
;
431 if ( myData
.extensions
.GetCount() > 0 )
433 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
434 myData
.menuitems
= mNavOptions
.popupExtension
;
435 for ( int i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
437 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
438 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
439 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
441 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
443 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
444 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
448 if ( m_dialogStyle
& wxSAVE
)
450 myData
.saveMode
= true ;
452 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
453 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
459 sStandardNavEventFilter
,
461 kNavGenericSignature
,
462 &myData
); // User Data
463 m_filterIndex
= myData
.currentfilter
;
467 myData
.saveMode
= false ;
469 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
470 if ( m_dialogStyle
& wxMULTIPLE
)
471 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
473 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
479 sStandardNavEventFilter
,
484 m_filterIndex
= myData
.currentfilter
;
487 DisposeNavObjectFilterUPP(mNavFilterUPP
);
488 if ( mDefaultLocation
.dataHandle
!= nil
)
490 ::AEDisposeDesc(&mDefaultLocation
);
493 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
498 if (mNavReply
.validRecord
) {
505 ::AECountItems( &mNavReply
.selection
, &count
) ;
506 for ( long i
= 1 ; i
<= count
; ++i
)
508 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
509 if ( err
!= noErr
) {
513 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
514 if (specDesc
.dataHandle
!= nil
) {
515 ::AEDisposeDesc(&specDesc
);
517 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
518 m_paths
.Add( m_path
) ;
519 m_fileName
= wxFileNameFromPath(m_path
);
520 m_fileNames
.Add(m_fileName
);
522 // set these to the first hit
523 m_path
= m_paths
[ 0 ] ;
524 m_fileName
= wxFileNameFromPath(m_path
);
525 m_dir
= wxPathOnly(m_path
);
526 NavDisposeReply( &mNavReply
) ;
532 // Generic file load/save dialog
534 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
536 char *ext
= (char *)extension
;
541 str
= "Load %s file";
543 str
= "Save %s file";
544 sprintf(prompt
, wxGetTranslation(str
), what
);
546 if (*ext
== '.') ext
++;
548 sprintf(wild
, "*.%s", ext
);
550 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
553 // Generic file load dialog
555 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
557 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
561 // Generic file save dialog
563 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
565 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);