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
)
35 #include <Navigation.h>
38 #include "MoreFiles.h"
39 #include "MoreFilesExtras.h"
41 extern bool gUseNavServices
;
43 // the data we need to pass to our standard file hook routine
44 // includes a pointer to the dialog, a pointer to the standard
45 // file reply record (so we can inspect the current selection)
46 // and a copy of the "previous" file spec of the reply record
47 // so we can see if the selection has changed
49 const int kwxMacFileTypes
= 10 ;
51 struct OpenUserDataRec
{
53 wxString name
[kwxMacFileTypes
] ;
54 wxString extensions
[kwxMacFileTypes
] ;
55 OSType filtermactypes
[kwxMacFileTypes
] ;
58 typedef struct OpenUserDataRec
59 OpenUserDataRec
, *OpenUserDataRecPtr
;
61 static pascal void NavEventProc(
62 NavEventCallbackMessage inSelector
,
64 NavCallBackUserData ioUserData
);
67 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
69 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
74 NavEventCallbackMessage inSelector
,
76 NavCallBackUserData ioUserData
)
78 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
79 if (inSelector
== kNavCBEvent
) {
80 // In Universal Headers 3.2, Apple changed the definition of
82 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
83 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
86 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
90 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
91 } else if ( inSelector
== kNavCBPopupMenuSelect
)
93 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
94 data
->currentfilter
= menu
->menuType
;
107 OSType gfiltersmac
[] =
119 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
122 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
123 *(short *) SFSaveDisk
= -1 * newVRefNum
;
124 *(long *) CurDirStore
= newDirID
;
127 static void wxMacSetupStandardFileFromPath( const char* s
)
134 Boolean isDirectory
;
136 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
143 // then copy the rest of the filename
145 for (j
=0;(s
[i
]!=0);i
++,j
++)
151 c2pstr((Ptr
) volume
) ;
155 GetVol( NULL
, &vRefNum
) ;
157 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
158 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
162 kSelectItem
= 10, // select button item number
163 kSFGetFileDlgID
= 251, // dialog resource number
164 kStrListID
= 251, // our strings
165 kSelectStrNum
= 1, // word 'Select: ' for button
166 kDesktopStrNum
= 2, // word 'Desktop' for button
167 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
169 kUseQuotes
= true, // parameter for SetButtonName
170 kDontUseQuotes
= false
173 static void GetLabelString(StringPtr theStr
, short stringNum
)
175 GetIndString(theStr
, kStrListID
, stringNum
);
178 static void CopyPStr(StringPtr src
, StringPtr dest
)
180 BlockMoveData(src
, dest
, 1 + src
[0]);
183 static char GetSelectKey(void)
185 // this is the key used to trigger the select button
187 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
192 // FlashButton briefly highlights the dialog button
193 // as feedback for key equivalents
195 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
200 unsigned long finalTicks
;
202 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
203 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
204 Delay(10, &finalTicks
);
205 HiliteControl((ControlHandle
) buttonHandle
, 0);
208 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
210 return (spec1
->vRefNum
== spec2
->vRefNum
211 && spec1
->parID
== spec2
->parID
212 && EqualString(spec1
->name
, spec2
->name
, false, false));
214 // MyModalDialogFilter maps a key to the Select button, and handles
215 // flashing of the button when the key is hit
217 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
218 short *item
, void *dataPtr
)
220 #pragma unused (dataPtr)
222 // make certain the proper dialog is showing, 'cause standard file
223 // can nest dialogs but calls the same filter for each
225 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
227 // check if the select button was hit
229 if ((eventRec->what == keyDown)
230 && (eventRec->modifiers & cmdKey)
231 && ((eventRec->message & charCodeMask) == GetSelectKey()))
234 FlashButton(theDlgPtr, kSelectItem);
242 #endif !TARGET_CARBON
244 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
246 myData
->currentfilter
= 0 ;
248 if ( filter
&& filter
[0] )
250 wxString
filter2(filter
) ;
254 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
256 if( filter2
.GetChar(i
) == wxT('|') )
259 myData
->name
[filterIndex
] = current
;
262 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
270 current
+= filter2
.GetChar(i
) ;
273 // if ( filterIndex > 0 )
275 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
276 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
280 myData
->numfilters
= filterIndex
;
281 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
284 for ( j
= 0 ; gfilters
[j
] ; j
++ )
286 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
288 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
292 if( gfilters
[j
] == NULL
)
294 myData
->filtermactypes
[i
] = '****' ;
300 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;
409 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
414 p2cstrcpy((char *)filename
, name
) ;
416 PLstrcpy( filename
, name
) ;
419 wxString
file(filename
) ;
422 if ( data
->numfilters
> 0 )
424 //for ( int i = 0 ; i < data->numfilters ; ++i )
425 int i
= data
->currentfilter
;
426 if ( data
->extensions
[i
].Right(2) == ".*" )
430 if ( type
== data
->filtermactypes
[i
] )
433 wxStringTokenizer
tokenizer( data
->extensions
[i
] , ";" ) ;
434 while( tokenizer
.HasMoreTokens() )
436 wxString extension
= tokenizer
.GetNextToken() ;
437 if ( extension
.GetChar(0) == '*' )
438 extension
= extension
.Mid(1) ;
440 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
449 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
451 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
452 // return true if this item is invisible or a file
457 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
458 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
460 // because the semantics of the filter proc are "true means don't show
461 // it" we need to invert the result that we return
468 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
476 wxString
wxFileSelector(const char *title
,
477 const char *defaultDir
, const char *defaultFileName
,
478 const char *defaultExtension
, const char *filter
, int flags
,
479 wxWindow
*parent
, int x
, int y
)
481 // If there's a default extension specified but no filter, we create a suitable
484 wxString
filter2("");
485 if ( defaultExtension
&& !filter
)
486 filter2
= wxString("*.") + wxString(defaultExtension
) ;
490 wxString defaultDirString
;
492 defaultDirString
= defaultDir
;
494 defaultDirString
= "";
496 wxString defaultFilenameString
;
498 defaultFilenameString
= defaultFileName
;
500 defaultFilenameString
= "";
502 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
504 if ( fileDialog
.ShowModal() == wxID_OK
)
506 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
510 return wxGetEmptyString();
513 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
514 const char *defaultDir
,
515 const char *defaultFileName
,
516 int* defaultFilterIndex
,
524 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
525 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
527 if ( fileDialog
.ShowModal() == wxID_OK
)
529 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
530 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
534 return wxGetEmptyString();
537 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
538 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
539 long style
, const wxPoint
& pos
)
542 m_dialogStyle
= style
;
545 m_fileName
= defaultFileName
;
547 m_wildCard
= wildCard
;
552 pascal Boolean
CrossPlatformFilterCallback (
556 NavFilterModes filterMode
560 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
562 if (filterMode
== kNavFilteringBrowserList
)
564 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
565 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
568 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 ;
618 crossPlatformFileFilterUPP
=
619 NewFileFilterYDProc(CrossPlatformFileFilter
);
622 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
624 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
626 if ( reply
.sfGood
== false )
633 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
642 NavDialogOptions mNavOptions
;
643 NavObjectFilterUPP mNavFilterUPP
= NULL
;
644 NavPreviewUPP mNavPreviewUPP
= NULL
;
645 NavReplyRecord mNavReply
;
646 AEDesc mDefaultLocation
;
647 bool mSelectDefault
= false ;
649 ::NavGetDefaultDialogOptions(&mNavOptions
);
652 mNavPreviewUPP
= nil
;
653 mSelectDefault
= false;
654 mNavReply
.validRecord
= false;
655 mNavReply
.replacing
= false;
656 mNavReply
.isStationery
= false;
657 mNavReply
.translationNeeded
= false;
658 mNavReply
.selection
.descriptorType
= typeNull
;
659 mNavReply
.selection
.dataHandle
= nil
;
660 mNavReply
.keyScript
= smSystemScript
;
661 mNavReply
.fileTranslation
= nil
;
663 // Set default location, the location
664 // that's displayed when the dialog
668 wxMacFilename2FSSpec( m_dir
, &location
) ;
671 mDefaultLocation
.descriptorType
= typeNull
;
672 mDefaultLocation
.dataHandle
= nil
;
674 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
676 if ( mDefaultLocation
.dataHandle
) {
678 if (mSelectDefault
) {
679 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
681 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
686 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
688 strcpy((char *)mNavOptions
.message
, m_message
) ;
689 c2pstr((char *)mNavOptions
.message
) ;
692 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
694 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
695 c2pstr((char *)mNavOptions
.savedFileName
) ;
698 if ( m_dialogStyle
& wxSAVE
)
701 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
702 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
703 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
709 sStandardNavEventFilter
,
716 OpenUserDataRec myData
;
717 MakeUserDataRec( &myData
, m_wildCard
) ;
718 NavTypeListHandle typelist
= NULL
;
720 if ( myData
.numfilters
> 0 )
722 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
723 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
724 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
725 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
726 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
728 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
730 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
731 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
736 mNavFilterUPP
= NewNavObjectFilterUPP( CrossPlatformFilterCallback
) ;
737 if ( m_dialogStyle
& wxMULTIPLE
)
738 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
740 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
746 sStandardNavEventFilter
,
749 typelist
/*inFileTypes.TypeListHandle() */,
750 &myData
); // User Data
752 DisposeHandle( (Handle
) typelist
) ;
755 DisposeNavObjectFilterUPP(mNavFilterUPP
);
756 if ( mDefaultLocation
.dataHandle
!= nil
)
758 ::AEDisposeDesc(&mDefaultLocation
);
761 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
766 if (mNavReply
.validRecord
) {
773 ::AECountItems( &mNavReply
.selection
, &count
) ;
774 for ( long i
= 1 ; i
<= count
; ++i
)
776 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, &keyWord
, &specDesc
);
777 if ( err
!= noErr
) {
781 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
782 if (specDesc
.dataHandle
!= nil
) {
783 ::AEDisposeDesc(&specDesc
);
787 // outFolderDirID = thePB.dirInfo.ioDrDirID;
788 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
789 m_paths
.Add( m_path
) ;
790 m_fileNames
.Add(m_fileName
);
792 // set these to the first hit
793 m_path
= m_paths
[ 0 ] ;
794 m_fileName
= wxFileNameFromPath(m_path
);
795 m_dir
= wxPathOnly(m_path
);
796 NavDisposeReply( &mNavReply
) ;
803 // Generic file load/save dialog
805 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
807 char *ext
= (char *)extension
;
812 str
= "Load %s file";
814 str
= "Save %s file";
815 sprintf(prompt
, wxGetTranslation(str
), what
);
817 if (*ext
== '.') ext
++;
819 sprintf(wild
, "*.%s", ext
);
821 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
824 // Generic file load dialog
826 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
828 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
832 // Generic file save dialog
834 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
836 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);