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>
42 #include "PLStringFuncs.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 const int kwxMacFileTypes
= 10 ;
55 struct OpenUserDataRec
{
57 wxString name
[kwxMacFileTypes
] ;
58 wxString extensions
[kwxMacFileTypes
] ;
59 OSType filtermactypes
[kwxMacFileTypes
] ;
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
) {
84 // In Universal Headers 3.2, Apple changed the definition of
86 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
87 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
90 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
94 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
95 } else if ( inSelector
== kNavCBPopupMenuSelect
)
97 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
98 data
->currentfilter
= menu
->menuType
;
111 OSType gfiltersmac
[] =
123 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
126 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
127 *(short *) SFSaveDisk
= -1 * newVRefNum
;
128 *(long *) CurDirStore
= newDirID
;
131 static void wxMacSetupStandardFileFromPath( const char* s
)
138 Boolean isDirectory
;
140 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
147 // then copy the rest of the filename
149 for (j
=0;(s
[i
]!=0);i
++,j
++)
155 c2pstr((Ptr
) volume
) ;
159 GetVol( NULL
, &vRefNum
) ;
161 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
162 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
166 kSelectItem
= 10, // select button item number
167 kSFGetFileDlgID
= 251, // dialog resource number
168 kStrListID
= 251, // our strings
169 kSelectStrNum
= 1, // word 'Select: ' for button
170 kDesktopStrNum
= 2, // word 'Desktop' for button
171 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
173 kUseQuotes
= true, // parameter for SetButtonName
174 kDontUseQuotes
= false
177 static void GetLabelString(StringPtr theStr
, short stringNum
)
179 GetIndString(theStr
, kStrListID
, stringNum
);
182 static void CopyPStr(StringPtr src
, StringPtr dest
)
184 BlockMoveData(src
, dest
, 1 + src
[0]);
187 static char GetSelectKey(void)
189 // this is the key used to trigger the select button
191 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
196 // FlashButton briefly highlights the dialog button
197 // as feedback for key equivalents
199 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
204 unsigned long finalTicks
;
206 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
207 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
208 Delay(10, &finalTicks
);
209 HiliteControl((ControlHandle
) buttonHandle
, 0);
212 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
214 return (spec1
->vRefNum
== spec2
->vRefNum
215 && spec1
->parID
== spec2
->parID
216 && EqualString(spec1
->name
, spec2
->name
, false, false));
218 // MyModalDialogFilter maps a key to the Select button, and handles
219 // flashing of the button when the key is hit
221 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
222 short *item
, void *dataPtr
)
224 #pragma unused (dataPtr)
226 // make certain the proper dialog is showing, 'cause standard file
227 // can nest dialogs but calls the same filter for each
229 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
231 // check if the select button was hit
233 if ((eventRec->what == keyDown)
234 && (eventRec->modifiers & cmdKey)
235 && ((eventRec->message & charCodeMask) == GetSelectKey()))
238 FlashButton(theDlgPtr, kSelectItem);
246 #endif !TARGET_CARBON
248 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
250 myData
->currentfilter
= 0 ;
252 if ( filter
&& filter
[0] )
254 wxString
filter2(filter
) ;
258 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
260 if( filter2
.GetChar(i
) == wxT('|') )
263 myData
->name
[filterIndex
] = current
;
266 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
274 current
+= filter2
.GetChar(i
) ;
277 if ( filterIndex
> 0 )
279 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
280 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
284 myData
->numfilters
= filterIndex
;
285 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
288 for ( j
= 0 ; gfilters
[j
] ; j
++ )
290 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
292 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
296 if( gfilters
[j
] == NULL
)
298 myData
->filtermactypes
[i
] = '****' ;
304 myData
->numfilters
= 0 ;
308 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
311 OpenUserDataRec myData
;
314 Boolean wasAliasedFlag
;
315 DlgHookYDUPP dlgHookUPP
;
316 ModalFilterYDUPP myModalFilterUPP
;
321 // presumably we're running System 7 or later so CustomGetFile is
324 // set initial contents of Select button to a space
326 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
328 // point the user data parameter at the reply record so we can get to it later
330 MakeUserDataRec( &myData
, filter
) ;
331 // display the dialog
336 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
337 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
339 thePt
.h
= thePt
.v
= -1; // center dialog
341 ParamText( message
, NULL
, NULL
, NULL
) ;
343 CustomGetFile( fileFilter
,
344 -1, // show all types
348 thePt
, // top left point
351 nil
, // activate list
352 nil
, // activate proc
355 DisposeRoutineDescriptor(dlgHookUPP
);
356 DisposeRoutineDescriptor(myModalFilterUPP
);
359 // if cancel wasn't pressed and no fatal error occurred...
363 // if no name is in the reply record file spec,
364 // use the file spec of the parent folder
366 if (theSFR
->sfFile
.name
[0] == '\0')
368 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
372 theSFR
->sfFile
= tempSpec
;
376 // no name to return, forget it
378 theSFR
->sfGood
= false;
382 // if there is now a name in the file spec, check if it's
383 // for a folder or a volume
385 if (theSFR
->sfFile
.name
[0] != '\0')
387 // the parID of the root of a disk is always fsRtParID == 1
389 if (theSFR
->sfFile
.parID
== fsRtParID
)
391 theSFR
->sfIsVolume
= true;
392 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
395 // we have a valid FSSpec, now let's make sure it's not for an alias file
397 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
400 theSFR
->sfGood
= false;
403 // did the alias resolve to a folder?
405 if (folderFlag
&& ! theSFR
->sfIsVolume
)
407 theSFR
->sfIsFolder
= true;
413 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
416 PLstrcpy( filename
, name
) ;
418 wxString
file(filename
) ;
421 if ( data
->numfilters
> 0 )
423 //for ( int i = 0 ; i < data->numfilters ; ++i )
424 int i
= data
->currentfilter
;
425 if ( data
->extensions
[i
].Right(2) == ".*" )
429 if ( type
== data
->filtermactypes
[i
] )
432 wxString extension
= data
->extensions
[i
] ;
433 if ( extension
.GetChar(0) == '*' )
434 extension
= extension
.Mid(1) ;
436 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
) ;
471 wxString
wxFileSelector(const char *title
,
472 const char *defaultDir
, const char *defaultFileName
,
473 const char *defaultExtension
, const char *filter
, int flags
,
474 wxWindow
*parent
, int x
, int y
)
476 // If there's a default extension specified but no filter, we create a suitable
479 wxString
filter2("");
480 if ( defaultExtension
&& !filter
)
481 filter2
= wxString("*.") + wxString(defaultExtension
) ;
485 wxString defaultDirString
;
487 defaultDirString
= defaultDir
;
489 defaultDirString
= "";
491 wxString defaultFilenameString
;
493 defaultFilenameString
= defaultFileName
;
495 defaultFilenameString
= "";
497 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
499 if ( fileDialog
.ShowModal() == wxID_OK
)
501 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
505 return wxGetEmptyString();
508 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
509 const char *defaultDir
,
510 const char *defaultFileName
,
511 int* defaultFilterIndex
,
519 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
520 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
522 if ( fileDialog
.ShowModal() == wxID_OK
)
524 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
525 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
529 return wxGetEmptyString();
532 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
533 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
534 long style
, const wxPoint
& pos
)
537 m_dialogStyle
= style
;
540 m_fileName
= defaultFileName
;
542 m_wildCard
= wildCard
;
547 pascal Boolean
CrossPlatformFilterCallback (
551 NavFilterModes filterMode
555 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
557 if (filterMode
== kNavFilteringBrowserList
)
559 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
560 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
563 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
564 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
571 int wxFileDialog::ShowModal()
574 if ( !gUseNavServices
)
576 if ( m_dialogStyle
& wxSAVE
)
578 StandardFileReply reply
;
583 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
585 strcpy((char *)prompt
, m_message
) ;
586 c2pstr((char *)prompt
) ;
589 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
591 strcpy((char *)filename
, m_fileName
) ;
592 c2pstr((char *)filename
) ;
597 StandardPutFile( prompt
, filename
, &reply
) ;
601 if ( reply
.sfGood
== false )
608 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
614 OSType types
= '????' ;
619 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
621 strcpy((char *)prompt
, m_message
) ;
622 c2pstr((char *)prompt
) ;
625 c2pstrcpy((StringPtr
)path
, m_dir
) ;
627 strcpy((char *)path
, m_dir
) ;
628 c2pstr((char *)path
) ;
631 StandardFileReply reply
;
632 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
634 crossPlatformFileFilterUPP
=
635 NewFileFilterYDProc(CrossPlatformFileFilter
);
638 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
640 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
642 if ( reply
.sfGood
== false )
649 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
658 NavDialogOptions mNavOptions
;
659 NavObjectFilterUPP mNavFilterUPP
= NULL
;
660 NavPreviewUPP mNavPreviewUPP
= NULL
;
661 NavReplyRecord mNavReply
;
662 AEDesc mDefaultLocation
;
663 bool mSelectDefault
= false ;
665 ::NavGetDefaultDialogOptions(&mNavOptions
);
668 mNavPreviewUPP
= nil
;
669 mSelectDefault
= false;
670 mNavReply
.validRecord
= false;
671 mNavReply
.replacing
= false;
672 mNavReply
.isStationery
= false;
673 mNavReply
.translationNeeded
= false;
674 mNavReply
.selection
.descriptorType
= typeNull
;
675 mNavReply
.selection
.dataHandle
= nil
;
676 mNavReply
.keyScript
= smSystemScript
;
677 mNavReply
.fileTranslation
= nil
;
679 // Set default location, the location
680 // that's displayed when the dialog
684 wxMacFilename2FSSpec( m_dir
, &location
) ;
687 mDefaultLocation
.descriptorType
= typeNull
;
688 mDefaultLocation
.dataHandle
= nil
;
690 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
692 if ( mDefaultLocation
.dataHandle
) {
694 if (mSelectDefault
) {
695 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
697 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
702 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
704 strcpy((char *)mNavOptions
.message
, m_message
) ;
705 c2pstr((char *)mNavOptions
.message
) ;
708 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
710 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
711 c2pstr((char *)mNavOptions
.savedFileName
) ;
714 if ( m_dialogStyle
& wxSAVE
)
717 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
718 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
719 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
725 sStandardNavEventFilter
,
732 OpenUserDataRec myData
;
733 MakeUserDataRec( &myData
, m_wildCard
) ;
734 NavTypeListHandle typelist
= NULL
;
736 if ( myData
.numfilters
> 0 )
738 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
739 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
740 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
741 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
742 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
744 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
746 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
747 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
752 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
753 if ( m_dialogStyle
& wxMULTIPLE
)
754 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
756 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
762 sStandardNavEventFilter
,
765 typelist
/*inFileTypes.TypeListHandle() */,
766 &myData
); // User Data
768 DisposeHandle( (Handle
) typelist
) ;
771 DisposeNavObjectFilterUPP(mNavFilterUPP
);
772 if ( mDefaultLocation
.dataHandle
!= nil
)
774 ::AEDisposeDesc(&mDefaultLocation
);
777 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
782 if (mNavReply
.validRecord
) {
788 ::AECountItems( &mNavReply
.selection
, &count
) ;
789 for ( long i
= 1 ; i
<= count
; ++i
)
791 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
792 if ( err
!= noErr
) {
796 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
797 if (specDesc
.dataHandle
!= nil
) {
798 ::AEDisposeDesc(&specDesc
);
802 // outFolderDirID = thePB.dirInfo.ioDrDirID;
803 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
804 m_paths
.Add( m_path
) ;
805 m_fileNames
.Add(m_fileName
);
807 // set these to the first hit
808 m_path
= m_paths
[ 0 ] ;
809 m_fileName
= wxFileNameFromPath(m_path
);
810 m_dir
= wxPathOnly(m_path
);
818 // Generic file load/save dialog
820 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
822 char *ext
= (char *)extension
;
827 str
= "Load %s file";
829 str
= "Save %s file";
830 sprintf(prompt
, wxGetTranslation(str
), what
);
832 if (*ext
== '.') ext
++;
834 sprintf(wild
, "*.%s", ext
);
836 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
839 // Generic file load dialog
841 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
843 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
847 // Generic file save dialog
849 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
851 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);