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
, wxDialog
)
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 NavMenuItemSpecArrayHandle menuitems
;
62 typedef struct OpenUserDataRec
63 OpenUserDataRec
, *OpenUserDataRecPtr
;
65 static pascal void NavEventProc(
66 NavEventCallbackMessage inSelector
,
68 NavCallBackUserData ioUserData
);
71 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
73 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
78 NavEventCallbackMessage inSelector
,
80 NavCallBackUserData ioUserData
)
82 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
83 if (inSelector
== kNavCBEvent
) {
85 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
88 else if ( inSelector
== kNavCBStart
)
90 if ( data
->menuitems
)
91 NavCustomControl(ioParams
->context
, kNavCtlSelectCustomType
, &(*data
->menuitems
)[data
->currentfilter
]);
93 else if ( inSelector
== kNavCBPopupMenuSelect
)
95 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
96 if ( menu
->menuCreator
== 'WXNG' )
98 data
->currentfilter
= menu
->menuType
;
101 int i
= menu
->menuType
;
102 wxString extension
= data
->extensions
[i
].AfterLast('.') ;
103 extension
.MakeLower() ;
105 // get the current filename
106 NavCustomControl(ioParams
->context
, kNavCtlGetEditFileName
, &filename
);
107 CopyPascalStringToC( filename
, (char*) filename
) ;
108 wxString
sfilename( filename
) ;
109 int pos
= sfilename
.Find('.',TRUE
) ;
110 if ( pos
!= wxNOT_FOUND
)
112 sfilename
= sfilename
.Left(pos
+1)+extension
;
113 CopyCStringToPascal( sfilename
.c_str() , filename
) ;
114 NavCustomControl(ioParams
->context
, kNavCtlSetEditFileName
, &filename
);
121 const char * gfilters
[] =
130 OSType gfiltersmac
[] =
141 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
143 myData
->menuitems
= NULL
;
144 myData
->currentfilter
= 0 ;
145 myData
->saveMode
= FALSE
;
147 if ( filter
&& filter
[0] )
149 wxString
filter2(filter
) ;
153 for( unsigned int i
= 0; i
< filter2
.Len() ; i
++ )
155 if( filter2
.GetChar(i
) == wxT('|') )
158 myData
->name
.Add( current
) ;
161 myData
->extensions
.Add( current
.MakeUpper() ) ;
169 current
+= filter2
.GetChar(i
) ;
172 // we allow for compatibility reason to have a single filter expression (like *.*) without
173 // an explanatory text, in that case the first part is name and extension at the same time
175 wxASSERT_MSG( filterIndex
== 0 || !isName
, "incorrect format of format string" ) ;
176 if ( current
.IsEmpty() )
177 myData
->extensions
.Add( myData
->name
[filterIndex
] ) ;
179 myData
->extensions
.Add( current
.MakeUpper() ) ;
180 if ( filterIndex
== 0 || isName
)
181 myData
->name
.Add( current
.MakeUpper() ) ;
186 const size_t extCount
= myData
->extensions
.GetCount();
187 for ( size_t i
= 0 ; i
< extCount
; i
++ )
190 for ( j
= 0 ; gfilters
[j
] ; j
++ )
192 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
194 myData
->filtermactypes
.Add( gfiltersmac
[j
] ) ;
198 if( gfilters
[j
] == NULL
)
200 myData
->filtermactypes
.Add( '****' ) ;
206 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
211 p2cstrcpy((char *)filename
, name
) ;
213 PLstrcpy( filename
, name
) ;
216 wxString
file(filename
) ;
219 if ( data
->extensions
.GetCount() > 0 )
221 //for ( int i = 0 ; i < data->numfilters ; ++i )
222 int i
= data
->currentfilter
;
223 if ( data
->extensions
[i
].Right(2) == ".*" )
227 if ( type
== (OSType
)data
->filtermactypes
[i
] )
230 wxStringTokenizer
tokenizer( data
->extensions
[i
] , ";" ) ;
231 while( tokenizer
.HasMoreTokens() )
233 wxString extension
= tokenizer
.GetNextToken() ;
234 if ( extension
.GetChar(0) == '*' )
235 extension
= extension
.Mid(1) ;
237 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
247 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
249 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
250 // return true if this item is invisible or a file
255 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
256 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
258 // because the semantics of the filter proc are "true means don't show
259 // it" we need to invert the result that we return
266 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
275 wxString
wxFileSelector(const char *title
,
276 const char *defaultDir
, const char *defaultFileName
,
277 const char *defaultExtension
, const char *filter
, int flags
,
278 wxWindow
*parent
, int x
, int y
)
280 // If there's a default extension specified but no filter, we create a suitable
283 wxString
filter2("");
284 if ( defaultExtension
&& !filter
)
285 filter2
= wxString("*.") + wxString(defaultExtension
) ;
289 wxString defaultDirString
;
291 defaultDirString
= defaultDir
;
293 defaultDirString
= "";
295 wxString defaultFilenameString
;
297 defaultFilenameString
= defaultFileName
;
299 defaultFilenameString
= "";
301 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
303 if ( fileDialog
.ShowModal() == wxID_OK
)
305 return fileDialog
.GetPath();
308 return wxGetEmptyString();
311 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
312 const char *defaultDir
,
313 const char *defaultFileName
,
314 int* defaultFilterIndex
,
322 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
323 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
325 if ( fileDialog
.ShowModal() == wxID_OK
)
327 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
328 return fileDialog
.GetPath();
331 return wxGetEmptyString();
334 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
335 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
336 long style
, const wxPoint
& pos
)
338 wxASSERT_MSG( NavServicesAvailable() , "Navigation Services are not running" ) ;
340 m_dialogStyle
= style
;
343 m_fileName
= defaultFileName
;
345 m_wildCard
= wildCard
;
350 pascal Boolean
CrossPlatformFilterCallback (
354 NavFilterModes filterMode
358 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
360 if (filterMode
== kNavFilteringBrowserList
)
362 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
363 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
366 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
367 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
374 int wxFileDialog::ShowModal()
376 NavDialogOptions mNavOptions
;
377 NavObjectFilterUPP mNavFilterUPP
= NULL
;
378 NavPreviewUPP mNavPreviewUPP
= NULL
;
379 NavReplyRecord mNavReply
;
380 AEDesc mDefaultLocation
;
381 bool mSelectDefault
= false ;
385 m_path
= wxEmptyString
;
386 m_fileName
= wxEmptyString
;
392 ::NavGetDefaultDialogOptions(&mNavOptions
);
395 mNavPreviewUPP
= nil
;
396 mSelectDefault
= false;
397 mNavReply
.validRecord
= false;
398 mNavReply
.replacing
= false;
399 mNavReply
.isStationery
= false;
400 mNavReply
.translationNeeded
= false;
401 mNavReply
.selection
.descriptorType
= typeNull
;
402 mNavReply
.selection
.dataHandle
= nil
;
403 mNavReply
.keyScript
= smSystemScript
;
404 mNavReply
.fileTranslation
= nil
;
406 // Set default location, the location
407 // that's displayed when the dialog
411 wxMacFilename2FSSpec( m_dir
, &location
) ;
414 mDefaultLocation
.descriptorType
= typeNull
;
415 mDefaultLocation
.dataHandle
= nil
;
417 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
419 if ( mDefaultLocation
.dataHandle
) {
421 if (mSelectDefault
) {
422 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
424 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
429 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
431 strcpy((char *)mNavOptions
.message
, m_message
) ;
432 c2pstr((char *)mNavOptions
.message
) ;
435 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
437 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
438 c2pstr((char *)mNavOptions
.savedFileName
) ;
441 OpenUserDataRec myData
;
442 MakeUserDataRec( &myData
, m_wildCard
) ;
443 myData
.currentfilter
= m_filterIndex
;
444 if ( myData
.extensions
.GetCount() > 0 )
446 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.extensions
.GetCount() ) ;
447 myData
.menuitems
= mNavOptions
.popupExtension
;
448 for ( size_t i
= 0 ; i
< myData
.extensions
.GetCount() ; ++i
)
450 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
451 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
452 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
454 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
456 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
457 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
461 if ( m_dialogStyle
& wxSAVE
)
463 myData
.saveMode
= true ;
465 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
466 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
472 sStandardNavEventFilter
,
474 kNavGenericSignature
,
475 &myData
); // User Data
476 m_filterIndex
= myData
.currentfilter
;
480 myData
.saveMode
= false ;
482 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
483 if ( m_dialogStyle
& wxMULTIPLE
)
484 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
486 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
492 sStandardNavEventFilter
,
497 m_filterIndex
= myData
.currentfilter
;
500 DisposeNavObjectFilterUPP(mNavFilterUPP
);
501 if ( mDefaultLocation
.dataHandle
!= nil
)
503 ::AEDisposeDesc(&mDefaultLocation
);
506 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
510 if (mNavReply
.validRecord
) {
517 ::AECountItems( &mNavReply
.selection
, &count
) ;
518 for ( long i
= 1 ; i
<= count
; ++i
)
520 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
521 if ( err
!= noErr
) {
525 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
526 if (specDesc
.dataHandle
!= nil
) {
527 ::AEDisposeDesc(&specDesc
);
529 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
530 m_paths
.Add( m_path
) ;
531 m_fileName
= wxFileNameFromPath(m_path
);
532 m_fileNames
.Add(m_fileName
);
534 // set these to the first hit
535 m_path
= m_paths
[ 0 ] ;
536 m_fileName
= wxFileNameFromPath(m_path
);
537 m_dir
= wxPathOnly(m_path
);
538 NavDisposeReply( &mNavReply
) ;
544 // Generic file load/save dialog
546 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
548 char *ext
= (char *)extension
;
553 str
= "Load %s file";
555 str
= "Save %s file";
556 sprintf(prompt
, wxGetTranslation(str
), what
);
558 if (*ext
== '.') ext
++;
560 sprintf(wild
, "*.%s", ext
);
562 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
565 // Generic file load dialog
567 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
569 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
573 // Generic file save dialog
575 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
577 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);