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
)
564 ::AEGetDescData(theItem
, &spec
, sizeof(FSSpec
) ) ;
566 memcpy( &spec
, (*theItem
->dataHandle
) , sizeof(FSSpec
) ) ;
568 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
575 int wxFileDialog::ShowModal()
578 if ( !gUseNavServices
)
580 if ( m_dialogStyle
& wxSAVE
)
582 StandardFileReply reply
;
587 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
589 strcpy((char *)prompt
, m_message
) ;
590 c2pstr((char *)prompt
) ;
593 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
595 strcpy((char *)filename
, m_fileName
) ;
596 c2pstr((char *)filename
) ;
601 StandardPutFile( prompt
, filename
, &reply
) ;
605 if ( reply
.sfGood
== false )
612 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
618 OSType types
= '????' ;
623 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
625 strcpy((char *)prompt
, m_message
) ;
626 c2pstr((char *)prompt
) ;
629 c2pstrcpy((StringPtr
)path
, m_dir
) ;
631 strcpy((char *)path
, m_dir
) ;
632 c2pstr((char *)path
) ;
635 StandardFileReply reply
;
636 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
638 crossPlatformFileFilterUPP
=
639 NewFileFilterYDProc(CrossPlatformFileFilter
);
642 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
644 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
646 if ( reply
.sfGood
== false )
653 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
662 NavDialogOptions mNavOptions
;
663 NavObjectFilterUPP mNavFilterUPP
= NULL
;
664 NavPreviewUPP mNavPreviewUPP
= NULL
;
665 NavReplyRecord mNavReply
;
666 AEDesc mDefaultLocation
;
667 bool mSelectDefault
= false ;
669 ::NavGetDefaultDialogOptions(&mNavOptions
);
672 mNavPreviewUPP
= nil
;
673 mSelectDefault
= false;
674 mNavReply
.validRecord
= false;
675 mNavReply
.replacing
= false;
676 mNavReply
.isStationery
= false;
677 mNavReply
.translationNeeded
= false;
678 mNavReply
.selection
.descriptorType
= typeNull
;
679 mNavReply
.selection
.dataHandle
= nil
;
680 mNavReply
.keyScript
= smSystemScript
;
681 mNavReply
.fileTranslation
= nil
;
683 // Set default location, the location
684 // that's displayed when the dialog
688 wxMacFilename2FSSpec( m_dir
, &location
) ;
691 mDefaultLocation
.descriptorType
= typeNull
;
692 mDefaultLocation
.dataHandle
= nil
;
694 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
696 if ( mDefaultLocation
.dataHandle
) {
698 if (mSelectDefault
) {
699 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
701 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
706 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
708 strcpy((char *)mNavOptions
.message
, m_message
) ;
709 c2pstr((char *)mNavOptions
.message
) ;
712 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
714 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
715 c2pstr((char *)mNavOptions
.savedFileName
) ;
718 if ( m_dialogStyle
& wxSAVE
)
721 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
722 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
723 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
729 sStandardNavEventFilter
,
736 OpenUserDataRec myData
;
737 MakeUserDataRec( &myData
, m_wildCard
) ;
738 NavTypeListHandle typelist
= NULL
;
740 if ( myData
.numfilters
> 0 )
742 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
743 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
744 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
745 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
746 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
748 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
750 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
751 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
756 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
757 if ( m_dialogStyle
& wxMULTIPLE
)
758 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
760 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
766 sStandardNavEventFilter
,
769 typelist
/*inFileTypes.TypeListHandle() */,
770 &myData
); // User Data
772 DisposeHandle( (Handle
) typelist
) ;
775 DisposeNavObjectFilterUPP(mNavFilterUPP
);
776 if ( mDefaultLocation
.dataHandle
!= nil
)
778 ::AEDisposeDesc(&mDefaultLocation
);
781 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
786 if (mNavReply
.validRecord
) {
792 ::AECountItems( &mNavReply
.selection
, &count
) ;
793 for ( long i
= 1 ; i
<= count
; ++i
)
795 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
796 if ( err
!= noErr
) {
800 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
801 if (specDesc
.dataHandle
!= nil
) {
802 ::AEDisposeDesc(&specDesc
);
806 // outFolderDirID = thePB.dirInfo.ioDrDirID;
807 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
808 m_paths
.Add( m_path
) ;
809 m_fileNames
.Add(m_fileName
);
811 // set these to the first hit
812 m_path
= m_paths
[ 0 ] ;
813 m_fileName
= wxFileNameFromPath(m_path
);
814 m_dir
= wxPathOnly(m_path
);
822 // Generic file load/save dialog
824 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
826 char *ext
= (char *)extension
;
831 str
= "Load %s file";
833 str
= "Save %s file";
834 sprintf(prompt
, wxGetTranslation(str
), what
);
836 if (*ext
== '.') ext
++;
838 sprintf(wild
, "*.%s", ext
);
840 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
843 // Generic file load dialog
845 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
847 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
851 // Generic file save dialog
853 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
855 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);