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 const int kwxMacFileTypes
= 10 ;
51 struct OpenUserDataRec
{
53 wxString name
[kwxMacFileTypes
] ;
54 wxString extensions
[kwxMacFileTypes
] ;
55 OSType filtermactypes
[kwxMacFileTypes
] ;
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 // In Universal Headers 3.2, Apple changed the definition of
82 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
83 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
86 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
90 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
91 } else if ( inSelector
== kNavCBPopupMenuSelect
)
93 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
94 data
->currentfilter
= menu
->menuType
;
98 const char * gfilters
[] =
107 OSType gfiltersmac
[] =
118 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
120 myData
->currentfilter
= 0 ;
122 if ( filter
&& filter
[0] )
124 wxString
filter2(filter
) ;
128 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
130 if( filter2
.GetChar(i
) == wxT('|') )
133 myData
->name
[filterIndex
] = current
;
136 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
144 current
+= filter2
.GetChar(i
) ;
147 // we allow for compatibility reason to have a single filter expression (like *.*) without
148 // an explanatory text, in that case the first part is name and extension at the same time
150 wxASSERT_MSG( filterIndex
== 0 || !isName
, "incorrect format of format string" ) ;
151 if ( current
.IsEmpty() )
152 myData
->extensions
[filterIndex
] = myData
->name
[filterIndex
] ;
154 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
158 myData
->numfilters
= filterIndex
;
159 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
162 for ( j
= 0 ; gfilters
[j
] ; j
++ )
164 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
166 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
170 if( gfilters
[j
] == NULL
)
172 myData
->filtermactypes
[i
] = '****' ;
178 myData
->numfilters
= 0 ;
183 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
188 p2cstrcpy((char *)filename
, name
) ;
190 PLstrcpy( filename
, name
) ;
193 wxString
file(filename
) ;
196 if ( data
->numfilters
> 0 )
198 //for ( int i = 0 ; i < data->numfilters ; ++i )
199 int i
= data
->currentfilter
;
200 if ( data
->extensions
[i
].Right(2) == ".*" )
204 if ( type
== data
->filtermactypes
[i
] )
207 wxStringTokenizer
tokenizer( data
->extensions
[i
] , ";" ) ;
208 while( tokenizer
.HasMoreTokens() )
210 wxString extension
= tokenizer
.GetNextToken() ;
211 if ( extension
.GetChar(0) == '*' )
212 extension
= extension
.Mid(1) ;
214 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
224 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
226 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
227 // return true if this item is invisible or a file
232 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
233 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
235 // because the semantics of the filter proc are "true means don't show
236 // it" we need to invert the result that we return
243 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
252 wxString
wxFileSelector(const char *title
,
253 const char *defaultDir
, const char *defaultFileName
,
254 const char *defaultExtension
, const char *filter
, int flags
,
255 wxWindow
*parent
, int x
, int y
)
257 // If there's a default extension specified but no filter, we create a suitable
260 wxString
filter2("");
261 if ( defaultExtension
&& !filter
)
262 filter2
= wxString("*.") + wxString(defaultExtension
) ;
266 wxString defaultDirString
;
268 defaultDirString
= defaultDir
;
270 defaultDirString
= "";
272 wxString defaultFilenameString
;
274 defaultFilenameString
= defaultFileName
;
276 defaultFilenameString
= "";
278 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
280 if ( fileDialog
.ShowModal() == wxID_OK
)
282 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
286 return wxGetEmptyString();
289 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
290 const char *defaultDir
,
291 const char *defaultFileName
,
292 int* defaultFilterIndex
,
300 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
301 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
303 if ( fileDialog
.ShowModal() == wxID_OK
)
305 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
306 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
310 return wxGetEmptyString();
313 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
314 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
315 long style
, const wxPoint
& pos
)
317 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
319 m_dialogStyle
= style
;
322 m_fileName
= defaultFileName
;
324 m_wildCard
= wildCard
;
329 pascal Boolean
CrossPlatformFilterCallback (
333 NavFilterModes filterMode
337 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
339 if (filterMode
== kNavFilteringBrowserList
)
341 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
342 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
345 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
346 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
353 int wxFileDialog::ShowModal()
355 NavDialogOptions mNavOptions
;
356 NavObjectFilterUPP mNavFilterUPP
= NULL
;
357 NavPreviewUPP mNavPreviewUPP
= NULL
;
358 NavReplyRecord mNavReply
;
359 AEDesc mDefaultLocation
;
360 bool mSelectDefault
= false ;
362 ::NavGetDefaultDialogOptions(&mNavOptions
);
365 mNavPreviewUPP
= nil
;
366 mSelectDefault
= false;
367 mNavReply
.validRecord
= false;
368 mNavReply
.replacing
= false;
369 mNavReply
.isStationery
= false;
370 mNavReply
.translationNeeded
= false;
371 mNavReply
.selection
.descriptorType
= typeNull
;
372 mNavReply
.selection
.dataHandle
= nil
;
373 mNavReply
.keyScript
= smSystemScript
;
374 mNavReply
.fileTranslation
= nil
;
376 // Set default location, the location
377 // that's displayed when the dialog
381 wxMacFilename2FSSpec( m_dir
, &location
) ;
384 mDefaultLocation
.descriptorType
= typeNull
;
385 mDefaultLocation
.dataHandle
= nil
;
387 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
389 if ( mDefaultLocation
.dataHandle
) {
391 if (mSelectDefault
) {
392 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
394 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
399 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
401 strcpy((char *)mNavOptions
.message
, m_message
) ;
402 c2pstr((char *)mNavOptions
.message
) ;
405 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
407 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
408 c2pstr((char *)mNavOptions
.savedFileName
) ;
411 if ( m_dialogStyle
& wxSAVE
)
414 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
415 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
416 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
422 sStandardNavEventFilter
,
429 OpenUserDataRec myData
;
430 MakeUserDataRec( &myData
, m_wildCard
) ;
431 NavTypeListHandle typelist
= NULL
;
433 if ( myData
.numfilters
> 0 )
435 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
436 for ( int i
= 0 ; i
< myData
.numfilters
; ++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
) ;
449 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
450 if ( m_dialogStyle
& wxMULTIPLE
)
451 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
453 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
459 sStandardNavEventFilter
,
462 typelist
/*inFileTypes.TypeListHandle() */,
463 &myData
); // User Data
465 DisposeHandle( (Handle
) typelist
) ;
468 DisposeNavObjectFilterUPP(mNavFilterUPP
);
469 if ( mDefaultLocation
.dataHandle
!= nil
)
471 ::AEDisposeDesc(&mDefaultLocation
);
474 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
479 if (mNavReply
.validRecord
) {
486 ::AECountItems( &mNavReply
.selection
, &count
) ;
487 for ( long i
= 1 ; i
<= count
; ++i
)
489 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
490 if ( err
!= noErr
) {
494 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
495 if (specDesc
.dataHandle
!= nil
) {
496 ::AEDisposeDesc(&specDesc
);
498 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
499 m_paths
.Add( m_path
) ;
500 m_fileName
= wxFileNameFromPath(m_path
);
501 m_fileNames
.Add(m_fileName
);
503 // set these to the first hit
504 m_path
= m_paths
[ 0 ] ;
505 m_fileName
= wxFileNameFromPath(m_path
);
506 m_dir
= wxPathOnly(m_path
);
507 NavDisposeReply( &mNavReply
) ;
513 // Generic file load/save dialog
515 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
517 char *ext
= (char *)extension
;
522 str
= "Load %s file";
524 str
= "Save %s file";
525 sprintf(prompt
, wxGetTranslation(str
), what
);
527 if (*ext
== '.') ext
++;
529 sprintf(wild
, "*.%s", ext
);
531 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
534 // Generic file load dialog
536 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
538 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
542 // Generic file save dialog
544 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
546 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);