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"
23 #if !defined(__UNIX__)
24 #include "PLStringFuncs.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
34 #include <Carbon/Carbon.h>
36 #include <Navigation.h>
44 #include "PLStringFuncs.h"
47 extern bool gUseNavServices
;
49 // the data we need to pass to our standard file hook routine
50 // includes a pointer to the dialog, a pointer to the standard
51 // file reply record (so we can inspect the current selection)
52 // and a copy of the "previous" file spec of the reply record
53 // so we can see if the selection has changed
55 const int kwxMacFileTypes
= 10 ;
57 struct OpenUserDataRec
{
59 wxString name
[kwxMacFileTypes
] ;
60 wxString extensions
[kwxMacFileTypes
] ;
61 OSType filtermactypes
[kwxMacFileTypes
] ;
64 typedef struct OpenUserDataRec
65 OpenUserDataRec
, *OpenUserDataRecPtr
;
67 static pascal void NavEventProc(
68 NavEventCallbackMessage inSelector
,
70 NavCallBackUserData ioUserData
);
73 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
75 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
80 NavEventCallbackMessage inSelector
,
82 NavCallBackUserData ioUserData
)
84 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
85 if (inSelector
== kNavCBEvent
) {
86 // In Universal Headers 3.2, Apple changed the definition of
88 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
89 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
92 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
96 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
97 } else if ( inSelector
== kNavCBPopupMenuSelect
)
99 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
100 data
->currentfilter
= menu
->menuType
;
113 OSType gfiltersmac
[] =
125 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
128 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
129 *(short *) SFSaveDisk
= -1 * newVRefNum
;
130 *(long *) CurDirStore
= newDirID
;
133 static void wxMacSetupStandardFileFromPath( const char* s
)
140 Boolean isDirectory
;
142 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
149 // then copy the rest of the filename
151 for (j
=0;(s
[i
]!=0);i
++,j
++)
157 c2pstr((Ptr
) volume
) ;
161 GetVol( NULL
, &vRefNum
) ;
163 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
164 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
168 kSelectItem
= 10, // select button item number
169 kSFGetFileDlgID
= 251, // dialog resource number
170 kStrListID
= 251, // our strings
171 kSelectStrNum
= 1, // word 'Select: ' for button
172 kDesktopStrNum
= 2, // word 'Desktop' for button
173 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
175 kUseQuotes
= true, // parameter for SetButtonName
176 kDontUseQuotes
= false
179 static void GetLabelString(StringPtr theStr
, short stringNum
)
181 GetIndString(theStr
, kStrListID
, stringNum
);
184 static void CopyPStr(StringPtr src
, StringPtr dest
)
186 BlockMoveData(src
, dest
, 1 + src
[0]);
189 static char GetSelectKey(void)
191 // this is the key used to trigger the select button
193 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
198 // FlashButton briefly highlights the dialog button
199 // as feedback for key equivalents
201 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
206 unsigned long finalTicks
;
208 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
209 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
210 Delay(10, &finalTicks
);
211 HiliteControl((ControlHandle
) buttonHandle
, 0);
214 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
216 return (spec1
->vRefNum
== spec2
->vRefNum
217 && spec1
->parID
== spec2
->parID
218 && EqualString(spec1
->name
, spec2
->name
, false, false));
220 // MyModalDialogFilter maps a key to the Select button, and handles
221 // flashing of the button when the key is hit
223 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
224 short *item
, void *dataPtr
)
226 #pragma unused (dataPtr)
228 // make certain the proper dialog is showing, 'cause standard file
229 // can nest dialogs but calls the same filter for each
231 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
233 // check if the select button was hit
235 if ((eventRec->what == keyDown)
236 && (eventRec->modifiers & cmdKey)
237 && ((eventRec->message & charCodeMask) == GetSelectKey()))
240 FlashButton(theDlgPtr, kSelectItem);
248 #endif !TARGET_CARBON
250 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
252 myData
->currentfilter
= 0 ;
254 if ( filter
&& filter
[0] )
256 wxString
filter2(filter
) ;
260 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
262 if( filter2
.GetChar(i
) == wxT('|') )
265 myData
->name
[filterIndex
] = current
;
268 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
276 current
+= filter2
.GetChar(i
) ;
279 if ( filterIndex
> 0 )
281 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
282 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
286 myData
->numfilters
= filterIndex
;
287 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
290 for ( j
= 0 ; gfilters
[j
] ; j
++ )
292 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
294 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
298 if( gfilters
[j
] == NULL
)
300 myData
->filtermactypes
[i
] = '****' ;
306 myData
->numfilters
= 0 ;
310 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
313 OpenUserDataRec myData
;
316 Boolean wasAliasedFlag
;
317 DlgHookYDUPP dlgHookUPP
;
318 ModalFilterYDUPP myModalFilterUPP
;
323 // presumably we're running System 7 or later so CustomGetFile is
326 // set initial contents of Select button to a space
328 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
330 // point the user data parameter at the reply record so we can get to it later
332 MakeUserDataRec( &myData
, filter
) ;
333 // display the dialog
338 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
339 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
341 thePt
.h
= thePt
.v
= -1; // center dialog
343 ParamText( message
, NULL
, NULL
, NULL
) ;
345 CustomGetFile( fileFilter
,
346 -1, // show all types
350 thePt
, // top left point
353 nil
, // activate list
354 nil
, // activate proc
357 DisposeRoutineDescriptor(dlgHookUPP
);
358 DisposeRoutineDescriptor(myModalFilterUPP
);
361 // if cancel wasn't pressed and no fatal error occurred...
365 // if no name is in the reply record file spec,
366 // use the file spec of the parent folder
368 if (theSFR
->sfFile
.name
[0] == '\0')
370 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
374 theSFR
->sfFile
= tempSpec
;
378 // no name to return, forget it
380 theSFR
->sfGood
= false;
384 // if there is now a name in the file spec, check if it's
385 // for a folder or a volume
387 if (theSFR
->sfFile
.name
[0] != '\0')
389 // the parID of the root of a disk is always fsRtParID == 1
391 if (theSFR
->sfFile
.parID
== fsRtParID
)
393 theSFR
->sfIsVolume
= true;
394 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
397 // we have a valid FSSpec, now let's make sure it's not for an alias file
399 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
402 theSFR
->sfGood
= false;
405 // did the alias resolve to a folder?
407 if (folderFlag
&& ! theSFR
->sfIsVolume
)
409 theSFR
->sfIsFolder
= true;
415 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
418 PLstrcpy( filename
, name
) ;
420 if ( data
->numfilters
> 0 )
422 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
424 filename
[j
] = toupper( filename
[j
] ) ;
426 //for ( int i = 0 ; i < data->numfilters ; ++i )
427 int i
= data
->currentfilter
;
428 if ( data
->extensions
[i
].Right(2) == ".*" )
432 if ( type
== data
->filtermactypes
[i
] )
435 wxString extension
= data
->extensions
[i
] ;
436 if ( extension
.GetChar(0) == '*' )
437 extension
= extension
.Mid(1) ;
439 if ( filename
[0] >= extension
.Len() && extension
== (char*) filename
+ 1 + filename
[0] - extension
.Len() )
447 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
449 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
450 // return true if this item is invisible or a file
455 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
456 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
458 // because the semantics of the filter proc are "true means don't show
459 // it" we need to invert the result that we return
466 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
474 wxString
wxFileSelector(const char *title
,
475 const char *defaultDir
, const char *defaultFileName
,
476 const char *defaultExtension
, const char *filter
, int flags
,
477 wxWindow
*parent
, int x
, int y
)
479 // If there's a default extension specified but no filter, we create a suitable
482 wxString
filter2("");
483 if ( defaultExtension
&& !filter
)
484 filter2
= wxString("*.") + wxString(defaultExtension
) ;
488 wxString defaultDirString
;
490 defaultDirString
= defaultDir
;
492 defaultDirString
= "";
494 wxString defaultFilenameString
;
496 defaultFilenameString
= defaultFileName
;
498 defaultFilenameString
= "";
500 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
502 if ( fileDialog
.ShowModal() == wxID_OK
)
504 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
508 return wxGetEmptyString();
511 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
512 const char *defaultDir
,
513 const char *defaultFileName
,
514 int* defaultFilterIndex
,
522 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
523 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
525 if ( fileDialog
.ShowModal() == wxID_OK
)
527 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
528 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
532 return wxGetEmptyString();
535 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
536 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
537 long style
, const wxPoint
& pos
)
540 m_dialogStyle
= style
;
543 m_fileName
= defaultFileName
;
545 m_wildCard
= wildCard
;
550 Boolean
CrossPlatformFilterCallback (
554 NavFilterModes filterMode
558 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
560 if (filterMode
== kNavFilteringBrowserList
)
562 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
563 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
566 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
567 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
574 int wxFileDialog::ShowModal()
577 if ( !gUseNavServices
)
579 if ( m_dialogStyle
& wxSAVE
)
581 StandardFileReply reply
;
586 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
588 strcpy((char *)prompt
, m_message
) ;
589 c2pstr((char *)prompt
) ;
592 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
594 strcpy((char *)filename
, m_fileName
) ;
595 c2pstr((char *)filename
) ;
600 StandardPutFile( prompt
, filename
, &reply
) ;
604 if ( reply
.sfGood
== false )
611 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
617 OSType types
= '????' ;
622 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
624 strcpy((char *)prompt
, m_message
) ;
625 c2pstr((char *)prompt
) ;
628 c2pstrcpy((StringPtr
)path
, m_dir
) ;
630 strcpy((char *)path
, m_dir
) ;
631 c2pstr((char *)path
) ;
634 StandardFileReply reply
;
635 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
637 crossPlatformFileFilterUPP
=
638 NewFileFilterYDProc(CrossPlatformFileFilter
);
641 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
643 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
645 if ( reply
.sfGood
== false )
652 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
661 NavDialogOptions mNavOptions
;
662 NavObjectFilterUPP mNavFilterUPP
= NULL
;
663 NavPreviewUPP mNavPreviewUPP
= NULL
;
664 NavReplyRecord mNavReply
;
665 AEDesc mDefaultLocation
;
666 bool mSelectDefault
= false ;
668 ::NavGetDefaultDialogOptions(&mNavOptions
);
671 mNavPreviewUPP
= nil
;
672 mSelectDefault
= false;
673 mNavReply
.validRecord
= false;
674 mNavReply
.replacing
= false;
675 mNavReply
.isStationery
= false;
676 mNavReply
.translationNeeded
= false;
677 mNavReply
.selection
.descriptorType
= typeNull
;
678 mNavReply
.selection
.dataHandle
= nil
;
679 mNavReply
.keyScript
= smSystemScript
;
680 mNavReply
.fileTranslation
= nil
;
682 // Set default location, the location
683 // that's displayed when the dialog
687 wxMacFilename2FSSpec( m_dir
, &location
) ;
690 mDefaultLocation
.descriptorType
= typeNull
;
691 mDefaultLocation
.dataHandle
= nil
;
693 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
695 if ( mDefaultLocation
.dataHandle
) {
697 if (mSelectDefault
) {
698 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
700 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
705 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
707 strcpy((char *)mNavOptions
.message
, m_message
) ;
708 c2pstr((char *)mNavOptions
.message
) ;
711 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
713 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
714 c2pstr((char *)mNavOptions
.savedFileName
) ;
717 if ( m_dialogStyle
& wxSAVE
)
720 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
721 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
722 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
728 sStandardNavEventFilter
,
735 OpenUserDataRec myData
;
736 MakeUserDataRec( &myData
, m_wildCard
) ;
737 NavTypeListHandle typelist
= NULL
;
739 if ( myData
.numfilters
> 0 )
741 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
742 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
743 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
744 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
745 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
747 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
749 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
750 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
755 mNavFilterUPP
= NewNavObjectFilterProc( CrossPlatformFilterCallback
) ;
756 if ( m_dialogStyle
& wxMULTIPLE
)
757 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
759 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
765 sStandardNavEventFilter
,
768 typelist
/*inFileTypes.TypeListHandle() */,
769 &myData
); // User Data
771 DisposeHandle( (Handle
) typelist
) ;
774 DisposeNavObjectFilterUPP(mNavFilterUPP
);
775 if ( mDefaultLocation
.dataHandle
!= nil
)
777 ::AEDisposeDesc(&mDefaultLocation
);
780 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
785 if (mNavReply
.validRecord
) {
791 ::AECountItems( &mNavReply
.selection
, &count
) ;
792 for ( long i
= 1 ; i
<= count
; ++i
)
794 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
795 if ( err
!= noErr
) {
799 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
800 if (specDesc
.dataHandle
!= nil
) {
801 ::AEDisposeDesc(&specDesc
);
805 // outFolderDirID = thePB.dirInfo.ioDrDirID;
806 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
807 m_paths
.Add( m_path
) ;
808 m_fileNames
.Add(m_fileName
);
810 // set these to the first hit
811 m_path
= m_paths
[ 0 ] ;
812 m_fileName
= wxFileNameFromPath(m_path
);
813 m_dir
= wxPathOnly(m_path
);
821 // Generic file load/save dialog
823 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
825 char *ext
= (char *)extension
;
830 str
= "Load %s file";
832 str
= "Save %s file";
833 sprintf(prompt
, wxGetTranslation(str
), what
);
835 if (*ext
== '.') ext
++;
837 sprintf(wild
, "*.%s", ext
);
839 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
842 // Generic file load dialog
844 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
846 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
850 // Generic file save dialog
852 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
854 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);