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"
22 #include "wx/tokenzr.h"
25 #include "PLStringFuncs.h"
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
34 #include <Navigation.h>
36 #include "MoreFiles.h"
37 #include "MoreFilesExtras.h"
39 extern bool gUseNavServices
;
41 // the data we need to pass to our standard file hook routine
42 // includes a pointer to the dialog, a pointer to the standard
43 // file reply record (so we can inspect the current selection)
44 // and a copy of the "previous" file spec of the reply record
45 // so we can see if the selection has changed
47 const int kwxMacFileTypes
= 10 ;
49 struct OpenUserDataRec
{
51 wxString name
[kwxMacFileTypes
] ;
52 wxString extensions
[kwxMacFileTypes
] ;
53 OSType filtermactypes
[kwxMacFileTypes
] ;
56 typedef struct OpenUserDataRec
57 OpenUserDataRec
, *OpenUserDataRecPtr
;
59 static pascal void NavEventProc(
60 NavEventCallbackMessage inSelector
,
62 NavCallBackUserData ioUserData
);
65 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
67 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
72 NavEventCallbackMessage inSelector
,
74 NavCallBackUserData ioUserData
)
76 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
77 if (inSelector
== kNavCBEvent
) {
78 // In Universal Headers 3.2, Apple changed the definition of
80 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
81 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
84 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
88 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
89 } else if ( inSelector
== kNavCBPopupMenuSelect
)
91 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
92 data
->currentfilter
= menu
->menuType
;
96 const char * gfilters
[] =
105 OSType gfiltersmac
[] =
117 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
120 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
121 *(short *) SFSaveDisk
= -1 * newVRefNum
;
122 *(long *) CurDirStore
= newDirID
;
125 static void wxMacSetupStandardFileFromPath( const char* s
)
132 Boolean isDirectory
;
134 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
141 // then copy the rest of the filename
143 for (j
=0;(s
[i
]!=0);i
++,j
++)
149 c2pstr((Ptr
) volume
) ;
153 GetVol( NULL
, &vRefNum
) ;
155 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
156 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
160 kSelectItem
= 10, // select button item number
161 kSFGetFileDlgID
= 251, // dialog resource number
162 kStrListID
= 251, // our strings
163 kSelectStrNum
= 1, // word 'Select: ' for button
164 kDesktopStrNum
= 2, // word 'Desktop' for button
165 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
167 kUseQuotes
= true, // parameter for SetButtonName
168 kDontUseQuotes
= false
171 static void GetLabelString(StringPtr theStr
, short stringNum
)
173 GetIndString(theStr
, kStrListID
, stringNum
);
176 static void CopyPStr(StringPtr src
, StringPtr dest
)
178 BlockMoveData(src
, dest
, 1 + src
[0]);
181 static char GetSelectKey(void)
183 // this is the key used to trigger the select button
185 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
190 // FlashButton briefly highlights the dialog button
191 // as feedback for key equivalents
193 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
198 unsigned long finalTicks
;
200 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
201 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
202 Delay(10, &finalTicks
);
203 HiliteControl((ControlHandle
) buttonHandle
, 0);
206 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
208 return (spec1
->vRefNum
== spec2
->vRefNum
209 && spec1
->parID
== spec2
->parID
210 && EqualString(spec1
->name
, spec2
->name
, false, false));
212 // MyModalDialogFilter maps a key to the Select button, and handles
213 // flashing of the button when the key is hit
215 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
216 short *item
, void *dataPtr
)
218 #pragma unused (dataPtr)
220 // make certain the proper dialog is showing, 'cause standard file
221 // can nest dialogs but calls the same filter for each
223 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
225 // check if the select button was hit
227 if ((eventRec->what == keyDown)
228 && (eventRec->modifiers & cmdKey)
229 && ((eventRec->message & charCodeMask) == GetSelectKey()))
232 FlashButton(theDlgPtr, kSelectItem);
240 #endif !TARGET_CARBON
242 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
244 myData
->currentfilter
= 0 ;
246 if ( filter
&& filter
[0] )
248 wxString
filter2(filter
) ;
252 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
254 if( filter2
.GetChar(i
) == wxT('|') )
257 myData
->name
[filterIndex
] = current
;
260 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
268 current
+= filter2
.GetChar(i
) ;
271 // if ( filterIndex > 0 )
273 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
274 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
278 myData
->numfilters
= filterIndex
;
279 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
282 for ( j
= 0 ; gfilters
[j
] ; j
++ )
284 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
286 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
290 if( gfilters
[j
] == NULL
)
292 myData
->filtermactypes
[i
] = '****' ;
298 myData
->numfilters
= 0 ;
304 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
307 OpenUserDataRec myData
;
310 Boolean wasAliasedFlag
;
311 DlgHookYDUPP dlgHookUPP
;
312 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
332 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
333 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
335 thePt
.h
= thePt
.v
= -1; // center dialog
337 ParamText( message
, NULL
, NULL
, NULL
) ;
339 CustomGetFile( fileFilter
,
340 -1, // show all types
344 thePt
, // top left point
347 nil
, // activate list
348 nil
, // activate proc
351 DisposeRoutineDescriptor(dlgHookUPP
);
352 DisposeRoutineDescriptor(myModalFilterUPP
);
355 // if cancel wasn't pressed and no fatal error occurred...
359 // if no name is in the reply record file spec,
360 // use the file spec of the parent folder
362 if (theSFR
->sfFile
.name
[0] == '\0')
364 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
368 theSFR
->sfFile
= tempSpec
;
372 // no name to return, forget it
374 theSFR
->sfGood
= false;
378 // if there is now a name in the file spec, check if it's
379 // for a folder or a volume
381 if (theSFR
->sfFile
.name
[0] != '\0')
383 // the parID of the root of a disk is always fsRtParID == 1
385 if (theSFR
->sfFile
.parID
== fsRtParID
)
387 theSFR
->sfIsVolume
= true;
388 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
391 // we have a valid FSSpec, now let's make sure it's not for an alias file
393 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
396 theSFR
->sfGood
= false;
399 // did the alias resolve to a folder?
401 if (folderFlag
&& ! theSFR
->sfIsVolume
)
403 theSFR
->sfIsFolder
= true;
410 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
415 p2cstrcpy((char *)filename
, name
) ;
417 PLstrcpy( filename
, name
) ;
420 wxString
file(filename
) ;
423 if ( data
->numfilters
> 0 )
425 //for ( int i = 0 ; i < data->numfilters ; ++i )
426 int i
= data
->currentfilter
;
427 if ( data
->extensions
[i
].Right(2) == ".*" )
431 if ( type
== data
->filtermactypes
[i
] )
434 wxStringTokenizer
tokenizer( data
->extensions
[i
] , ";" ) ;
435 while( tokenizer
.HasMoreTokens() )
437 wxString extension
= tokenizer
.GetNextToken() ;
438 if ( extension
.GetChar(0) == '*' )
439 extension
= extension
.Mid(1) ;
441 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
451 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
453 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
454 // return true if this item is invisible or a file
459 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
460 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
462 // because the semantics of the filter proc are "true means don't show
463 // it" we need to invert the result that we return
470 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
479 wxString
wxFileSelector(const char *title
,
480 const char *defaultDir
, const char *defaultFileName
,
481 const char *defaultExtension
, const char *filter
, int flags
,
482 wxWindow
*parent
, int x
, int y
)
484 // If there's a default extension specified but no filter, we create a suitable
487 wxString
filter2("");
488 if ( defaultExtension
&& !filter
)
489 filter2
= wxString("*.") + wxString(defaultExtension
) ;
493 wxString defaultDirString
;
495 defaultDirString
= defaultDir
;
497 defaultDirString
= "";
499 wxString defaultFilenameString
;
501 defaultFilenameString
= defaultFileName
;
503 defaultFilenameString
= "";
505 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
507 if ( fileDialog
.ShowModal() == wxID_OK
)
509 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
513 return wxGetEmptyString();
516 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
517 const char *defaultDir
,
518 const char *defaultFileName
,
519 int* defaultFilterIndex
,
527 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
528 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
530 if ( fileDialog
.ShowModal() == wxID_OK
)
532 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
533 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
537 return wxGetEmptyString();
540 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
541 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
542 long style
, const wxPoint
& pos
)
545 m_dialogStyle
= style
;
548 m_fileName
= defaultFileName
;
550 m_wildCard
= wildCard
;
555 pascal Boolean
CrossPlatformFilterCallback (
559 NavFilterModes filterMode
563 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
565 if (filterMode
== kNavFilteringBrowserList
)
567 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
568 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
571 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
572 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
579 int wxFileDialog::ShowModal()
582 if ( !gUseNavServices
)
584 if ( m_dialogStyle
& wxSAVE
)
586 StandardFileReply reply
;
590 strcpy((char *)prompt
, m_message
) ;
591 c2pstr((char *)prompt
) ;
592 strcpy((char *)filename
, m_fileName
) ;
593 c2pstr((char *)filename
) ;
595 StandardPutFile( prompt
, filename
, &reply
) ;
596 if ( reply
.sfGood
== false )
603 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
609 OSType types
= '????' ;
613 strcpy((char *)prompt
, m_message
) ;
614 c2pstr((char *)prompt
) ;
615 strcpy((char *)path
, m_dir
) ;
616 c2pstr((char *)path
) ;
618 StandardFileReply reply
;
619 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
621 crossPlatformFileFilterUPP
=
622 NewFileFilterYDProc(CrossPlatformFileFilter
);
625 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
627 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
629 if ( reply
.sfGood
== false )
636 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
645 NavDialogOptions mNavOptions
;
646 NavObjectFilterUPP mNavFilterUPP
= NULL
;
647 NavPreviewUPP mNavPreviewUPP
= NULL
;
648 NavReplyRecord mNavReply
;
649 AEDesc mDefaultLocation
;
650 bool mSelectDefault
= false ;
652 ::NavGetDefaultDialogOptions(&mNavOptions
);
655 mNavPreviewUPP
= nil
;
656 mSelectDefault
= false;
657 mNavReply
.validRecord
= false;
658 mNavReply
.replacing
= false;
659 mNavReply
.isStationery
= false;
660 mNavReply
.translationNeeded
= false;
661 mNavReply
.selection
.descriptorType
= typeNull
;
662 mNavReply
.selection
.dataHandle
= nil
;
663 mNavReply
.keyScript
= smSystemScript
;
664 mNavReply
.fileTranslation
= nil
;
666 // Set default location, the location
667 // that's displayed when the dialog
671 wxMacFilename2FSSpec( m_dir
, &location
) ;
674 mDefaultLocation
.descriptorType
= typeNull
;
675 mDefaultLocation
.dataHandle
= nil
;
677 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
679 if ( mDefaultLocation
.dataHandle
) {
681 if (mSelectDefault
) {
682 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
684 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
689 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
691 strcpy((char *)mNavOptions
.message
, m_message
) ;
692 c2pstr((char *)mNavOptions
.message
) ;
695 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
697 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
698 c2pstr((char *)mNavOptions
.savedFileName
) ;
701 if ( m_dialogStyle
& wxSAVE
)
704 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
705 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
706 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
712 sStandardNavEventFilter
,
719 OpenUserDataRec myData
;
720 MakeUserDataRec( &myData
, m_wildCard
) ;
721 NavTypeListHandle typelist
= NULL
;
723 if ( myData
.numfilters
> 0 )
725 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
726 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
727 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
728 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
729 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
731 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
733 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
734 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
739 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
740 if ( m_dialogStyle
& wxMULTIPLE
)
741 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
743 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
749 sStandardNavEventFilter
,
752 typelist
/*inFileTypes.TypeListHandle() */,
753 &myData
); // User Data
755 DisposeHandle( (Handle
) typelist
) ;
758 DisposeNavObjectFilterUPP(mNavFilterUPP
);
759 if ( mDefaultLocation
.dataHandle
!= nil
)
761 ::AEDisposeDesc(&mDefaultLocation
);
764 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
769 if (mNavReply
.validRecord
) {
776 ::AECountItems( &mNavReply
.selection
, &count
) ;
777 for ( long i
= 1 ; i
<= count
; ++i
)
779 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
780 if ( err
!= noErr
) {
784 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
785 if (specDesc
.dataHandle
!= nil
) {
786 ::AEDisposeDesc(&specDesc
);
790 // outFolderDirID = thePB.dirInfo.ioDrDirID;
791 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
792 m_paths
.Add( m_path
) ;
793 m_fileNames
.Add(m_fileName
);
795 // set these to the first hit
796 m_path
= m_paths
[ 0 ] ;
797 m_fileName
= wxFileNameFromPath(m_path
);
798 m_dir
= wxPathOnly(m_path
);
799 NavDisposeReply( &mNavReply
) ;
806 // Generic file load/save dialog
808 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
810 char *ext
= (char *)extension
;
815 str
= "Load %s file";
817 str
= "Save %s file";
818 sprintf(prompt
, wxGetTranslation(str
), what
);
820 if (*ext
== '.') ext
++;
822 sprintf(wild
, "*.%s", ext
);
824 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
827 // Generic file load dialog
829 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
831 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
835 // Generic file save dialog
837 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
839 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);