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>
37 #include "MoreFiles.h"
38 #include "MoreFilesExtras.h"
40 extern bool gUseNavServices
;
42 // the data we need to pass to our standard file hook routine
43 // includes a pointer to the dialog, a pointer to the standard
44 // file reply record (so we can inspect the current selection)
45 // and a copy of the "previous" file spec of the reply record
46 // so we can see if the selection has changed
48 const int kwxMacFileTypes
= 10 ;
50 struct OpenUserDataRec
{
52 wxString name
[kwxMacFileTypes
] ;
53 wxString extensions
[kwxMacFileTypes
] ;
54 OSType filtermactypes
[kwxMacFileTypes
] ;
57 typedef struct OpenUserDataRec
58 OpenUserDataRec
, *OpenUserDataRecPtr
;
60 static pascal void NavEventProc(
61 NavEventCallbackMessage inSelector
,
63 NavCallBackUserData ioUserData
);
66 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
68 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
73 NavEventCallbackMessage inSelector
,
75 NavCallBackUserData ioUserData
)
77 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
78 if (inSelector
== kNavCBEvent
) {
79 // In Universal Headers 3.2, Apple changed the definition of
81 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
82 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
85 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
89 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
90 } else if ( inSelector
== kNavCBPopupMenuSelect
)
92 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
93 data
->currentfilter
= menu
->menuType
;
106 OSType gfiltersmac
[] =
118 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
121 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
122 *(short *) SFSaveDisk
= -1 * newVRefNum
;
123 *(long *) CurDirStore
= newDirID
;
126 static void wxMacSetupStandardFileFromPath( const char* s
)
133 Boolean isDirectory
;
135 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
142 // then copy the rest of the filename
144 for (j
=0;(s
[i
]!=0);i
++,j
++)
150 c2pstr((Ptr
) volume
) ;
154 GetVol( NULL
, &vRefNum
) ;
156 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
157 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
161 kSelectItem
= 10, // select button item number
162 kSFGetFileDlgID
= 251, // dialog resource number
163 kStrListID
= 251, // our strings
164 kSelectStrNum
= 1, // word 'Select: ' for button
165 kDesktopStrNum
= 2, // word 'Desktop' for button
166 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
168 kUseQuotes
= true, // parameter for SetButtonName
169 kDontUseQuotes
= false
172 static void GetLabelString(StringPtr theStr
, short stringNum
)
174 GetIndString(theStr
, kStrListID
, stringNum
);
177 static void CopyPStr(StringPtr src
, StringPtr dest
)
179 BlockMoveData(src
, dest
, 1 + src
[0]);
182 static char GetSelectKey(void)
184 // this is the key used to trigger the select button
186 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
191 // FlashButton briefly highlights the dialog button
192 // as feedback for key equivalents
194 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
199 unsigned long finalTicks
;
201 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
202 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
203 Delay(10, &finalTicks
);
204 HiliteControl((ControlHandle
) buttonHandle
, 0);
207 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
209 return (spec1
->vRefNum
== spec2
->vRefNum
210 && spec1
->parID
== spec2
->parID
211 && EqualString(spec1
->name
, spec2
->name
, false, false));
213 // MyModalDialogFilter maps a key to the Select button, and handles
214 // flashing of the button when the key is hit
216 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
217 short *item
, void *dataPtr
)
219 #pragma unused (dataPtr)
221 // make certain the proper dialog is showing, 'cause standard file
222 // can nest dialogs but calls the same filter for each
224 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
226 // check if the select button was hit
228 if ((eventRec->what == keyDown)
229 && (eventRec->modifiers & cmdKey)
230 && ((eventRec->message & charCodeMask) == GetSelectKey()))
233 FlashButton(theDlgPtr, kSelectItem);
241 #endif !TARGET_CARBON
243 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
245 myData
->currentfilter
= 0 ;
247 if ( filter
&& filter
[0] )
249 wxString
filter2(filter
) ;
253 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
255 if( filter2
.GetChar(i
) == wxT('|') )
258 myData
->name
[filterIndex
] = current
;
261 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
269 current
+= filter2
.GetChar(i
) ;
272 if ( filterIndex
> 0 )
274 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
275 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
279 myData
->numfilters
= filterIndex
;
280 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
283 for ( j
= 0 ; gfilters
[j
] ; j
++ )
285 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
287 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
291 if( gfilters
[j
] == NULL
)
293 myData
->filtermactypes
[i
] = '****' ;
299 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
;
317 // presumably we're running System 7 or later so CustomGetFile is
320 // set initial contents of Select button to a space
322 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
324 // point the user data parameter at the reply record so we can get to it later
326 MakeUserDataRec( &myData
, filter
) ;
327 // display the dialog
330 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
331 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
333 thePt
.h
= thePt
.v
= -1; // center dialog
335 ParamText( message
, NULL
, NULL
, NULL
) ;
337 CustomGetFile( fileFilter
,
338 -1, // show all types
342 thePt
, // top left point
345 nil
, // activate list
346 nil
, // activate proc
349 DisposeRoutineDescriptor(dlgHookUPP
);
350 DisposeRoutineDescriptor(myModalFilterUPP
);
352 // if cancel wasn't pressed and no fatal error occurred...
356 // if no name is in the reply record file spec,
357 // use the file spec of the parent folder
359 if (theSFR
->sfFile
.name
[0] == '\0')
361 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
365 theSFR
->sfFile
= tempSpec
;
369 // no name to return, forget it
371 theSFR
->sfGood
= false;
375 // if there is now a name in the file spec, check if it's
376 // for a folder or a volume
378 if (theSFR
->sfFile
.name
[0] != '\0')
380 // the parID of the root of a disk is always fsRtParID == 1
382 if (theSFR
->sfFile
.parID
== fsRtParID
)
384 theSFR
->sfIsVolume
= true;
385 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
388 // we have a valid FSSpec, now let's make sure it's not for an alias file
390 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
393 theSFR
->sfGood
= false;
396 // did the alias resolve to a folder?
398 if (folderFlag
&& ! theSFR
->sfIsVolume
)
400 theSFR
->sfIsFolder
= true;
407 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
412 p2cstrcpy((char *)filename
, name
) ;
414 PLstrcpy( filename
, name
) ;
417 wxString
file(filename
) ;
420 if ( data
->numfilters
> 0 )
422 //for ( int i = 0 ; i < data->numfilters ; ++i )
423 int i
= data
->currentfilter
;
424 if ( data
->extensions
[i
].Right(2) == ".*" )
428 if ( type
== data
->filtermactypes
[i
] )
431 wxString extension
= data
->extensions
[i
] ;
432 if ( extension
.GetChar(0) == '*' )
433 extension
= extension
.Mid(1) ;
435 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
444 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
446 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
447 // return true if this item is invisible or a file
452 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
453 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
455 // because the semantics of the filter proc are "true means don't show
456 // it" we need to invert the result that we return
463 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
472 wxString
wxFileSelector(const char *title
,
473 const char *defaultDir
, const char *defaultFileName
,
474 const char *defaultExtension
, const char *filter
, int flags
,
475 wxWindow
*parent
, int x
, int y
)
477 // If there's a default extension specified but no filter, we create a suitable
480 wxString
filter2("");
481 if ( defaultExtension
&& !filter
)
482 filter2
= wxString("*.") + wxString(defaultExtension
) ;
486 wxString defaultDirString
;
488 defaultDirString
= defaultDir
;
490 defaultDirString
= "";
492 wxString defaultFilenameString
;
494 defaultFilenameString
= defaultFileName
;
496 defaultFilenameString
= "";
498 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
500 if ( fileDialog
.ShowModal() == wxID_OK
)
502 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
506 return wxGetEmptyString();
509 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
510 const char *defaultDir
,
511 const char *defaultFileName
,
512 int* defaultFilterIndex
,
520 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
521 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
523 if ( fileDialog
.ShowModal() == wxID_OK
)
525 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
526 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
530 return wxGetEmptyString();
533 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
534 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
535 long style
, const wxPoint
& pos
)
538 m_dialogStyle
= style
;
541 m_fileName
= defaultFileName
;
543 m_wildCard
= wildCard
;
548 pascal Boolean
CrossPlatformFilterCallback (
552 NavFilterModes filterMode
556 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
558 if (filterMode
== kNavFilteringBrowserList
)
560 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
561 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
565 ::AEGetDescData(theItem
, &spec
, sizeof(FSSpec
) ) ;
567 memcpy( &spec
, (*theItem
->dataHandle
) , sizeof(FSSpec
) ) ;
569 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
576 int wxFileDialog::ShowModal()
579 if ( !gUseNavServices
)
581 if ( m_dialogStyle
& wxSAVE
)
583 StandardFileReply reply
;
587 strcpy((char *)prompt
, m_message
) ;
588 c2pstr((char *)prompt
) ;
589 strcpy((char *)filename
, m_fileName
) ;
590 c2pstr((char *)filename
) ;
592 StandardPutFile( prompt
, filename
, &reply
) ;
593 if ( reply
.sfGood
== false )
600 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
606 OSType types
= '????' ;
610 strcpy((char *)prompt
, m_message
) ;
611 c2pstr((char *)prompt
) ;
612 strcpy((char *)path
, m_dir
) ;
613 c2pstr((char *)path
) ;
615 StandardFileReply reply
;
616 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
617 crossPlatformFileFilterUPP
=
618 NewFileFilterYDProc(CrossPlatformFileFilter
);
619 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
620 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
621 if ( reply
.sfGood
== false )
628 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
637 NavDialogOptions mNavOptions
;
638 NavObjectFilterUPP mNavFilterUPP
= NULL
;
639 NavPreviewUPP mNavPreviewUPP
= NULL
;
640 NavReplyRecord mNavReply
;
641 AEDesc mDefaultLocation
;
642 bool mSelectDefault
= false ;
644 ::NavGetDefaultDialogOptions(&mNavOptions
);
647 mNavPreviewUPP
= nil
;
648 mSelectDefault
= false;
649 mNavReply
.validRecord
= false;
650 mNavReply
.replacing
= false;
651 mNavReply
.isStationery
= false;
652 mNavReply
.translationNeeded
= false;
653 mNavReply
.selection
.descriptorType
= typeNull
;
654 mNavReply
.selection
.dataHandle
= nil
;
655 mNavReply
.keyScript
= smSystemScript
;
656 mNavReply
.fileTranslation
= nil
;
658 // Set default location, the location
659 // that's displayed when the dialog
663 wxMacFilename2FSSpec( m_dir
, &location
) ;
666 mDefaultLocation
.descriptorType
= typeNull
;
667 mDefaultLocation
.dataHandle
= nil
;
669 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
671 if ( mDefaultLocation
.dataHandle
) {
673 if (mSelectDefault
) {
674 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
676 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
681 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
683 strcpy((char *)mNavOptions
.message
, m_message
) ;
684 c2pstr((char *)mNavOptions
.message
) ;
687 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
689 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
690 c2pstr((char *)mNavOptions
.savedFileName
) ;
693 if ( m_dialogStyle
& wxSAVE
)
696 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
697 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
698 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
704 sStandardNavEventFilter
,
711 OpenUserDataRec myData
;
712 MakeUserDataRec( &myData
, m_wildCard
) ;
713 NavTypeListHandle typelist
= NULL
;
715 if ( myData
.numfilters
> 0 )
717 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
718 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
719 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
720 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
721 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
723 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
725 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
726 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
731 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
732 if ( m_dialogStyle
& wxMULTIPLE
)
733 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
735 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
741 sStandardNavEventFilter
,
744 typelist
/*inFileTypes.TypeListHandle() */,
745 &myData
); // User Data
747 DisposeHandle( (Handle
) typelist
) ;
750 DisposeNavObjectFilterUPP(mNavFilterUPP
);
751 if ( mDefaultLocation
.dataHandle
!= nil
)
753 ::AEDisposeDesc(&mDefaultLocation
);
756 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
761 if (mNavReply
.validRecord
) {
767 ::AECountItems( &mNavReply
.selection
, &count
) ;
768 for ( long i
= 1 ; i
<= count
; ++i
)
770 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
771 if ( err
!= noErr
) {
775 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
776 if (specDesc
.dataHandle
!= nil
) {
777 ::AEDisposeDesc(&specDesc
);
781 // outFolderDirID = thePB.dirInfo.ioDrDirID;
782 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
783 m_paths
.Add( m_path
) ;
784 m_fileNames
.Add(m_fileName
);
786 // set these to the first hit
787 m_path
= m_paths
[ 0 ] ;
788 m_fileName
= wxFileNameFromPath(m_path
);
789 m_dir
= wxPathOnly(m_path
);
797 // Generic file load/save dialog
799 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
801 char *ext
= (char *)extension
;
806 str
= "Load %s file";
808 str
= "Save %s file";
809 sprintf(prompt
, wxGetTranslation(str
), what
);
811 if (*ext
== '.') ext
++;
813 sprintf(wild
, "*.%s", ext
);
815 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
818 // Generic file load dialog
820 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
822 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
826 // Generic file save dialog
828 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
830 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);