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"
24 #include "PLStringFuncs.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
34 #include <Navigation.h>
38 #include "MoreFiles.h"
39 #include "MoreFilesExtras.h"
42 extern bool gUseNavServices
;
44 // the data we need to pass to our standard file hook routine
45 // includes a pointer to the dialog, a pointer to the standard
46 // file reply record (so we can inspect the current selection)
47 // and a copy of the "previous" file spec of the reply record
48 // so we can see if the selection has changed
50 const int kwxMacFileTypes
= 10 ;
52 struct OpenUserDataRec
{
54 wxString name
[kwxMacFileTypes
] ;
55 wxString extensions
[kwxMacFileTypes
] ;
56 OSType filtermactypes
[kwxMacFileTypes
] ;
59 typedef struct OpenUserDataRec
60 OpenUserDataRec
, *OpenUserDataRecPtr
;
62 static pascal void NavEventProc(
63 NavEventCallbackMessage inSelector
,
65 NavCallBackUserData ioUserData
);
68 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
70 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
75 NavEventCallbackMessage inSelector
,
77 NavCallBackUserData ioUserData
)
79 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
80 if (inSelector
== kNavCBEvent
) {
81 // In Universal Headers 3.2, Apple changed the definition of
83 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
84 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
87 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
91 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
92 } else if ( inSelector
== kNavCBPopupMenuSelect
)
94 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
95 data
->currentfilter
= menu
->menuType
;
108 OSType gfiltersmac
[] =
120 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
123 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
124 *(short *) SFSaveDisk
= -1 * newVRefNum
;
125 *(long *) CurDirStore
= newDirID
;
128 static void wxMacSetupStandardFileFromPath( const char* s
)
135 Boolean isDirectory
;
137 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
144 // then copy the rest of the filename
146 for (j
=0;(s
[i
]!=0);i
++,j
++)
152 c2pstr((Ptr
) volume
) ;
156 GetVol( NULL
, &vRefNum
) ;
158 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
159 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
163 kSelectItem
= 10, // select button item number
164 kSFGetFileDlgID
= 251, // dialog resource number
165 kStrListID
= 251, // our strings
166 kSelectStrNum
= 1, // word 'Select: ' for button
167 kDesktopStrNum
= 2, // word 'Desktop' for button
168 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
170 kUseQuotes
= true, // parameter for SetButtonName
171 kDontUseQuotes
= false
174 static void GetLabelString(StringPtr theStr
, short stringNum
)
176 GetIndString(theStr
, kStrListID
, stringNum
);
179 static void CopyPStr(StringPtr src
, StringPtr dest
)
181 BlockMoveData(src
, dest
, 1 + src
[0]);
184 static char GetSelectKey(void)
186 // this is the key used to trigger the select button
188 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
193 // FlashButton briefly highlights the dialog button
194 // as feedback for key equivalents
196 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
201 unsigned long finalTicks
;
203 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
204 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
205 Delay(10, &finalTicks
);
206 HiliteControl((ControlHandle
) buttonHandle
, 0);
209 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
211 return (spec1
->vRefNum
== spec2
->vRefNum
212 && spec1
->parID
== spec2
->parID
213 && EqualString(spec1
->name
, spec2
->name
, false, false));
215 // MyModalDialogFilter maps a key to the Select button, and handles
216 // flashing of the button when the key is hit
218 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
219 short *item
, void *dataPtr
)
221 #pragma unused (dataPtr)
223 // make certain the proper dialog is showing, 'cause standard file
224 // can nest dialogs but calls the same filter for each
226 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
228 // check if the select button was hit
230 if ((eventRec->what == keyDown)
231 && (eventRec->modifiers & cmdKey)
232 && ((eventRec->message & charCodeMask) == GetSelectKey()))
235 FlashButton(theDlgPtr, kSelectItem);
243 #endif !TARGET_CARBON
245 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
247 myData
->currentfilter
= 0 ;
249 if ( filter
&& filter
[0] )
251 wxString
filter2(filter
) ;
255 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
257 if( filter2
.GetChar(i
) == wxT('|') )
260 myData
->name
[filterIndex
] = current
;
263 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
271 current
+= filter2
.GetChar(i
) ;
274 if ( filterIndex
> 0 )
276 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
277 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
281 myData
->numfilters
= filterIndex
;
282 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
285 for ( j
= 0 ; gfilters
[j
] ; j
++ )
287 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
289 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
293 if( gfilters
[j
] == NULL
)
295 myData
->filtermactypes
[i
] = '****' ;
301 myData
->numfilters
= 0 ;
305 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
308 OpenUserDataRec myData
;
311 Boolean wasAliasedFlag
;
312 DlgHookYDUPP dlgHookUPP
;
313 ModalFilterYDUPP myModalFilterUPP
;
318 // presumably we're running System 7 or later so CustomGetFile is
321 // set initial contents of Select button to a space
323 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
325 // point the user data parameter at the reply record so we can get to it later
327 MakeUserDataRec( &myData
, filter
) ;
328 // display the dialog
333 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
334 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
336 thePt
.h
= thePt
.v
= -1; // center dialog
338 ParamText( message
, NULL
, NULL
, NULL
) ;
340 CustomGetFile( fileFilter
,
341 -1, // show all types
345 thePt
, // top left point
348 nil
, // activate list
349 nil
, // activate proc
352 DisposeRoutineDescriptor(dlgHookUPP
);
353 DisposeRoutineDescriptor(myModalFilterUPP
);
356 // if cancel wasn't pressed and no fatal error occurred...
360 // if no name is in the reply record file spec,
361 // use the file spec of the parent folder
363 if (theSFR
->sfFile
.name
[0] == '\0')
365 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
369 theSFR
->sfFile
= tempSpec
;
373 // no name to return, forget it
375 theSFR
->sfGood
= false;
379 // if there is now a name in the file spec, check if it's
380 // for a folder or a volume
382 if (theSFR
->sfFile
.name
[0] != '\0')
384 // the parID of the root of a disk is always fsRtParID == 1
386 if (theSFR
->sfFile
.parID
== fsRtParID
)
388 theSFR
->sfIsVolume
= true;
389 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
392 // we have a valid FSSpec, now let's make sure it's not for an alias file
394 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
397 theSFR
->sfGood
= false;
400 // did the alias resolve to a folder?
402 if (folderFlag
&& ! theSFR
->sfIsVolume
)
404 theSFR
->sfIsFolder
= true;
410 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
413 PLstrcpy( filename
, name
) ;
415 wxString
file(filename
) ;
418 if ( data
->numfilters
> 0 )
420 //for ( int i = 0 ; i < data->numfilters ; ++i )
421 int i
= data
->currentfilter
;
422 if ( data
->extensions
[i
].Right(2) == ".*" )
426 if ( type
== data
->filtermactypes
[i
] )
429 wxString extension
= data
->extensions
[i
] ;
430 if ( extension
.GetChar(0) == '*' )
431 extension
= extension
.Mid(1) ;
433 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
441 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
443 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
444 // return true if this item is invisible or a file
449 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
450 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
452 // because the semantics of the filter proc are "true means don't show
453 // it" we need to invert the result that we return
460 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
468 wxString
wxFileSelector(const char *title
,
469 const char *defaultDir
, const char *defaultFileName
,
470 const char *defaultExtension
, const char *filter
, int flags
,
471 wxWindow
*parent
, int x
, int y
)
473 // If there's a default extension specified but no filter, we create a suitable
476 wxString
filter2("");
477 if ( defaultExtension
&& !filter
)
478 filter2
= wxString("*.") + wxString(defaultExtension
) ;
482 wxString defaultDirString
;
484 defaultDirString
= defaultDir
;
486 defaultDirString
= "";
488 wxString defaultFilenameString
;
490 defaultFilenameString
= defaultFileName
;
492 defaultFilenameString
= "";
494 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
496 if ( fileDialog
.ShowModal() == wxID_OK
)
498 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
502 return wxGetEmptyString();
505 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
506 const char *defaultDir
,
507 const char *defaultFileName
,
508 int* defaultFilterIndex
,
516 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
517 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
519 if ( fileDialog
.ShowModal() == wxID_OK
)
521 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
522 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
526 return wxGetEmptyString();
529 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
530 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
531 long style
, const wxPoint
& pos
)
534 m_dialogStyle
= style
;
537 m_fileName
= defaultFileName
;
539 m_wildCard
= wildCard
;
544 pascal Boolean
CrossPlatformFilterCallback (
548 NavFilterModes filterMode
552 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
554 if (filterMode
== kNavFilteringBrowserList
)
556 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
557 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
561 ::AEGetDescData(theItem
, &spec
, sizeof(FSSpec
) ) ;
563 memcpy( &spec
, (*theItem
->dataHandle
) , sizeof(FSSpec
) ) ;
565 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
572 int wxFileDialog::ShowModal()
575 if ( !gUseNavServices
)
577 if ( m_dialogStyle
& wxSAVE
)
579 StandardFileReply reply
;
584 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
586 strcpy((char *)prompt
, m_message
) ;
587 c2pstr((char *)prompt
) ;
590 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
592 strcpy((char *)filename
, m_fileName
) ;
593 c2pstr((char *)filename
) ;
598 StandardPutFile( prompt
, filename
, &reply
) ;
602 if ( reply
.sfGood
== false )
609 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
615 OSType types
= '????' ;
620 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
622 strcpy((char *)prompt
, m_message
) ;
623 c2pstr((char *)prompt
) ;
626 c2pstrcpy((StringPtr
)path
, m_dir
) ;
628 strcpy((char *)path
, m_dir
) ;
629 c2pstr((char *)path
) ;
632 StandardFileReply reply
;
633 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
635 crossPlatformFileFilterUPP
=
636 NewFileFilterYDProc(CrossPlatformFileFilter
);
639 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
641 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
643 if ( reply
.sfGood
== false )
650 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
659 NavDialogOptions mNavOptions
;
660 NavObjectFilterUPP mNavFilterUPP
= NULL
;
661 NavPreviewUPP mNavPreviewUPP
= NULL
;
662 NavReplyRecord mNavReply
;
663 AEDesc mDefaultLocation
;
664 bool mSelectDefault
= false ;
666 ::NavGetDefaultDialogOptions(&mNavOptions
);
669 mNavPreviewUPP
= nil
;
670 mSelectDefault
= false;
671 mNavReply
.validRecord
= false;
672 mNavReply
.replacing
= false;
673 mNavReply
.isStationery
= false;
674 mNavReply
.translationNeeded
= false;
675 mNavReply
.selection
.descriptorType
= typeNull
;
676 mNavReply
.selection
.dataHandle
= nil
;
677 mNavReply
.keyScript
= smSystemScript
;
678 mNavReply
.fileTranslation
= nil
;
680 // Set default location, the location
681 // that's displayed when the dialog
685 wxMacFilename2FSSpec( m_dir
, &location
) ;
688 mDefaultLocation
.descriptorType
= typeNull
;
689 mDefaultLocation
.dataHandle
= nil
;
691 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
693 if ( mDefaultLocation
.dataHandle
) {
695 if (mSelectDefault
) {
696 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
698 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
703 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
705 strcpy((char *)mNavOptions
.message
, m_message
) ;
706 c2pstr((char *)mNavOptions
.message
) ;
709 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
711 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
712 c2pstr((char *)mNavOptions
.savedFileName
) ;
715 if ( m_dialogStyle
& wxSAVE
)
718 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
719 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
720 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
726 sStandardNavEventFilter
,
733 OpenUserDataRec myData
;
734 MakeUserDataRec( &myData
, m_wildCard
) ;
735 NavTypeListHandle typelist
= NULL
;
737 if ( myData
.numfilters
> 0 )
739 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
740 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
741 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
742 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
743 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
745 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
747 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
748 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
753 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
754 if ( m_dialogStyle
& wxMULTIPLE
)
755 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
757 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
763 sStandardNavEventFilter
,
766 typelist
/*inFileTypes.TypeListHandle() */,
767 &myData
); // User Data
769 DisposeHandle( (Handle
) typelist
) ;
772 DisposeNavObjectFilterUPP(mNavFilterUPP
);
773 if ( mDefaultLocation
.dataHandle
!= nil
)
775 ::AEDisposeDesc(&mDefaultLocation
);
778 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
783 if (mNavReply
.validRecord
) {
789 ::AECountItems( &mNavReply
.selection
, &count
) ;
790 for ( long i
= 1 ; i
<= count
; ++i
)
792 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
793 if ( err
!= noErr
) {
797 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
798 if (specDesc
.dataHandle
!= nil
) {
799 ::AEDisposeDesc(&specDesc
);
803 // outFolderDirID = thePB.dirInfo.ioDrDirID;
804 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
805 m_paths
.Add( m_path
) ;
806 m_fileNames
.Add(m_fileName
);
808 // set these to the first hit
809 m_path
= m_paths
[ 0 ] ;
810 m_fileName
= wxFileNameFromPath(m_path
);
811 m_dir
= wxPathOnly(m_path
);
819 // Generic file load/save dialog
821 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
823 char *ext
= (char *)extension
;
828 str
= "Load %s file";
830 str
= "Save %s file";
831 sprintf(prompt
, wxGetTranslation(str
), what
);
833 if (*ext
== '.') ext
++;
835 sprintf(wild
, "*.%s", ext
);
837 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
840 // Generic file load dialog
842 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
844 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
848 // Generic file save dialog
850 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
852 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);