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"
23 #if !defined(__UNIX__)
24 #include "PLStringFuncs.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
34 #include <Carbon/Carbon.h>
36 #include <Navigation.h>
44 #include "PLStringFuncs.h"
47 extern bool gUseNavServices
;
49 // the data we need to pass to our standard file hook routine
50 // includes a pointer to the dialog, a pointer to the standard
51 // file reply record (so we can inspect the current selection)
52 // and a copy of the "previous" file spec of the reply record
53 // so we can see if the selection has changed
55 const int kwxMacFileTypes
= 10 ;
57 struct OpenUserDataRec
{
59 wxString name
[kwxMacFileTypes
] ;
60 wxString extensions
[kwxMacFileTypes
] ;
61 OSType filtermactypes
[kwxMacFileTypes
] ;
64 typedef struct OpenUserDataRec
65 OpenUserDataRec
, *OpenUserDataRecPtr
;
67 static pascal void NavEventProc(
68 NavEventCallbackMessage inSelector
,
70 NavCallBackUserData ioUserData
);
73 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
75 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
80 NavEventCallbackMessage inSelector
,
82 NavCallBackUserData ioUserData
)
84 OpenUserDataRec
* data
= ( OpenUserDataRec
*) ioUserData
;
85 if (inSelector
== kNavCBEvent
) {
86 // In Universal Headers 3.2, Apple changed the definition of
88 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
89 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
92 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
96 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
97 } else if ( inSelector
== kNavCBPopupMenuSelect
)
99 NavMenuItemSpec
* menu
= (NavMenuItemSpec
*) ioParams
->eventData
.eventDataParms
.param
;
100 data
->currentfilter
= menu
->menuType
;
113 OSType gfiltersmac
[] =
125 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
128 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
129 *(short *) SFSaveDisk
= -1 * newVRefNum
;
130 *(long *) CurDirStore
= newDirID
;
133 static void wxMacSetupStandardFileFromPath( const char* s
)
140 Boolean isDirectory
;
142 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
149 // then copy the rest of the filename
151 for (j
=0;(s
[i
]!=0);i
++,j
++)
157 c2pstr((Ptr
) volume
) ;
161 GetVol( NULL
, &vRefNum
) ;
163 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
164 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
168 kSelectItem
= 10, // select button item number
169 kSFGetFileDlgID
= 251, // dialog resource number
170 kStrListID
= 251, // our strings
171 kSelectStrNum
= 1, // word 'Select: ' for button
172 kDesktopStrNum
= 2, // word 'Desktop' for button
173 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
175 kUseQuotes
= true, // parameter for SetButtonName
176 kDontUseQuotes
= false
179 static void GetLabelString(StringPtr theStr
, short stringNum
)
181 GetIndString(theStr
, kStrListID
, stringNum
);
184 static void CopyPStr(StringPtr src
, StringPtr dest
)
186 BlockMoveData(src
, dest
, 1 + src
[0]);
189 static char GetSelectKey(void)
191 // this is the key used to trigger the select button
193 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
198 // FlashButton briefly highlights the dialog button
199 // as feedback for key equivalents
201 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
206 unsigned long finalTicks
;
208 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
209 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
210 Delay(10, &finalTicks
);
211 HiliteControl((ControlHandle
) buttonHandle
, 0);
214 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
216 return (spec1
->vRefNum
== spec2
->vRefNum
217 && spec1
->parID
== spec2
->parID
218 && EqualString(spec1
->name
, spec2
->name
, false, false));
220 // MyModalDialogFilter maps a key to the Select button, and handles
221 // flashing of the button when the key is hit
223 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
224 short *item
, void *dataPtr
)
226 #pragma unused (dataPtr)
228 // make certain the proper dialog is showing, 'cause standard file
229 // can nest dialogs but calls the same filter for each
231 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
233 // check if the select button was hit
235 if ((eventRec->what == keyDown)
236 && (eventRec->modifiers & cmdKey)
237 && ((eventRec->message & charCodeMask) == GetSelectKey()))
240 FlashButton(theDlgPtr, kSelectItem);
248 #endif !TARGET_CARBON
250 void MakeUserDataRec(OpenUserDataRec
*myData
, const wxString
& filter
)
252 myData
->currentfilter
= 0 ;
254 if ( filter
&& filter
[0] )
256 wxString
filter2(filter
) ;
260 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
262 if( filter2
.GetChar(i
) == wxT('|') )
265 myData
->name
[filterIndex
] = current
;
268 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
276 current
+= filter2
.GetChar(i
) ;
279 if ( filterIndex
> 0 )
281 wxASSERT_MSG( !isName
, "incorrect format of format string" ) ;
282 myData
->extensions
[filterIndex
] = current
.MakeUpper() ;
286 myData
->numfilters
= filterIndex
;
287 for ( int i
= 0 ; i
< myData
->numfilters
; i
++ )
290 for ( j
= 0 ; gfilters
[j
] ; j
++ )
292 if ( strcmp( myData
->extensions
[i
] , gfilters
[j
] ) == 0 )
294 myData
->filtermactypes
[i
] = gfiltersmac
[j
] ;
298 if( gfilters
[j
] == NULL
)
300 myData
->filtermactypes
[i
] = '****' ;
306 myData
->numfilters
= 0 ;
310 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
313 OpenUserDataRec myData
;
316 Boolean wasAliasedFlag
;
317 DlgHookYDUPP dlgHookUPP
;
318 ModalFilterYDUPP myModalFilterUPP
;
323 // presumably we're running System 7 or later so CustomGetFile is
326 // set initial contents of Select button to a space
328 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
330 // point the user data parameter at the reply record so we can get to it later
332 MakeUserDataRec( &myData
, filter
) ;
333 // display the dialog
338 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
339 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
341 thePt
.h
= thePt
.v
= -1; // center dialog
343 ParamText( message
, NULL
, NULL
, NULL
) ;
345 CustomGetFile( fileFilter
,
346 -1, // show all types
350 thePt
, // top left point
353 nil
, // activate list
354 nil
, // activate proc
357 DisposeRoutineDescriptor(dlgHookUPP
);
358 DisposeRoutineDescriptor(myModalFilterUPP
);
361 // if cancel wasn't pressed and no fatal error occurred...
365 // if no name is in the reply record file spec,
366 // use the file spec of the parent folder
368 if (theSFR
->sfFile
.name
[0] == '\0')
370 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
374 theSFR
->sfFile
= tempSpec
;
378 // no name to return, forget it
380 theSFR
->sfGood
= false;
384 // if there is now a name in the file spec, check if it's
385 // for a folder or a volume
387 if (theSFR
->sfFile
.name
[0] != '\0')
389 // the parID of the root of a disk is always fsRtParID == 1
391 if (theSFR
->sfFile
.parID
== fsRtParID
)
393 theSFR
->sfIsVolume
= true;
394 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
397 // we have a valid FSSpec, now let's make sure it's not for an alias file
399 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
402 theSFR
->sfGood
= false;
405 // did the alias resolve to a folder?
407 if (folderFlag
&& ! theSFR
->sfIsVolume
)
409 theSFR
->sfIsFolder
= true;
415 static Boolean
CheckFile( ConstStr255Param name
, OSType type
, OpenUserDataRecPtr data
)
418 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 wxString extension
= data
->extensions
[i
] ;
435 if ( extension
.GetChar(0) == '*' )
436 extension
= extension
.Mid(1) ;
438 if ( file
.Len() >= extension
.Len() && extension
== file
.Right(extension
.Len() ) )
446 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
448 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
449 // return true if this item is invisible or a file
454 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
455 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
457 // because the semantics of the filter proc are "true means don't show
458 // it" we need to invert the result that we return
465 return !CheckFile( myCInfoPBPtr
->hFileInfo
.ioNamePtr
, myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
, data
) ;
473 wxString
wxFileSelector(const char *title
,
474 const char *defaultDir
, const char *defaultFileName
,
475 const char *defaultExtension
, const char *filter
, int flags
,
476 wxWindow
*parent
, int x
, int y
)
478 // If there's a default extension specified but no filter, we create a suitable
481 wxString
filter2("");
482 if ( defaultExtension
&& !filter
)
483 filter2
= wxString("*.") + wxString(defaultExtension
) ;
487 wxString defaultDirString
;
489 defaultDirString
= defaultDir
;
491 defaultDirString
= "";
493 wxString defaultFilenameString
;
495 defaultFilenameString
= defaultFileName
;
497 defaultFilenameString
= "";
499 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
501 if ( fileDialog
.ShowModal() == wxID_OK
)
503 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
507 return wxGetEmptyString();
510 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
511 const char *defaultDir
,
512 const char *defaultFileName
,
513 int* defaultFilterIndex
,
521 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
522 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
524 if ( fileDialog
.ShowModal() == wxID_OK
)
526 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
527 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
531 return wxGetEmptyString();
534 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
535 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
536 long style
, const wxPoint
& pos
)
539 m_dialogStyle
= style
;
542 m_fileName
= defaultFileName
;
544 m_wildCard
= wildCard
;
549 Boolean
CrossPlatformFilterCallback (
553 NavFilterModes filterMode
557 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) callBackUD
;
559 if (filterMode
== kNavFilteringBrowserList
)
561 NavFileOrFolderInfo
* theInfo
= (NavFileOrFolderInfo
*) info
;
562 if (theItem
->descriptorType
== typeFSS
&& !theInfo
->isFolder
)
565 memcpy( &spec
, *theItem
->dataHandle
, sizeof(FSSpec
) ) ;
566 display
= CheckFile( spec
.name
, theInfo
->fileAndFolder
.fileInfo
.finderInfo
.fdType
, data
) ;
573 int wxFileDialog::ShowModal()
576 if ( !gUseNavServices
)
578 if ( m_dialogStyle
& wxSAVE
)
580 StandardFileReply reply
;
585 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
587 strcpy((char *)prompt
, m_message
) ;
588 c2pstr((char *)prompt
) ;
591 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
593 strcpy((char *)filename
, m_fileName
) ;
594 c2pstr((char *)filename
) ;
599 StandardPutFile( prompt
, filename
, &reply
) ;
603 if ( reply
.sfGood
== false )
610 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
616 OSType types
= '????' ;
621 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
623 strcpy((char *)prompt
, m_message
) ;
624 c2pstr((char *)prompt
) ;
627 c2pstrcpy((StringPtr
)path
, m_dir
) ;
629 strcpy((char *)path
, m_dir
) ;
630 c2pstr((char *)path
) ;
633 StandardFileReply reply
;
634 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
636 crossPlatformFileFilterUPP
=
637 NewFileFilterYDProc(CrossPlatformFileFilter
);
640 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
642 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
644 if ( reply
.sfGood
== false )
651 m_path
= wxMacFSSpec2MacFilename( &reply
.sfFile
) ;
660 NavDialogOptions mNavOptions
;
661 NavObjectFilterUPP mNavFilterUPP
= NULL
;
662 NavPreviewUPP mNavPreviewUPP
= NULL
;
663 NavReplyRecord mNavReply
;
664 AEDesc mDefaultLocation
;
665 bool mSelectDefault
= false ;
667 ::NavGetDefaultDialogOptions(&mNavOptions
);
670 mNavPreviewUPP
= nil
;
671 mSelectDefault
= false;
672 mNavReply
.validRecord
= false;
673 mNavReply
.replacing
= false;
674 mNavReply
.isStationery
= false;
675 mNavReply
.translationNeeded
= false;
676 mNavReply
.selection
.descriptorType
= typeNull
;
677 mNavReply
.selection
.dataHandle
= nil
;
678 mNavReply
.keyScript
= smSystemScript
;
679 mNavReply
.fileTranslation
= nil
;
681 // Set default location, the location
682 // that's displayed when the dialog
686 wxMacFilename2FSSpec( m_dir
, &location
) ;
689 mDefaultLocation
.descriptorType
= typeNull
;
690 mDefaultLocation
.dataHandle
= nil
;
692 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
694 if ( mDefaultLocation
.dataHandle
) {
696 if (mSelectDefault
) {
697 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
699 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
704 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
706 strcpy((char *)mNavOptions
.message
, m_message
) ;
707 c2pstr((char *)mNavOptions
.message
) ;
710 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
712 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
713 c2pstr((char *)mNavOptions
.savedFileName
) ;
716 if ( m_dialogStyle
& wxSAVE
)
719 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
720 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
721 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
727 sStandardNavEventFilter
,
734 OpenUserDataRec myData
;
735 MakeUserDataRec( &myData
, m_wildCard
) ;
736 NavTypeListHandle typelist
= NULL
;
738 if ( myData
.numfilters
> 0 )
740 mNavOptions
.popupExtension
= (NavMenuItemSpecArrayHandle
) NewHandle( sizeof( NavMenuItemSpec
) * myData
.numfilters
) ;
741 for ( int i
= 0 ; i
< myData
.numfilters
; ++i
) {
742 (*mNavOptions
.popupExtension
)[i
].version
= kNavMenuItemSpecVersion
;
743 (*mNavOptions
.popupExtension
)[i
].menuCreator
= 'WXNG' ;
744 (*mNavOptions
.popupExtension
)[i
].menuType
= i
;
746 c2pstrcpy((StringPtr
)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
748 strcpy((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
, myData
.name
[i
]) ;
749 c2pstr((char *)(*mNavOptions
.popupExtension
)[i
].menuItemName
) ;
754 mNavFilterUPP
= NewNavObjectFilterProc( CrossPlatformFilterCallback
) ;
755 if ( m_dialogStyle
& wxMULTIPLE
)
756 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
758 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
764 sStandardNavEventFilter
,
767 typelist
/*inFileTypes.TypeListHandle() */,
768 &myData
); // User Data
770 DisposeHandle( (Handle
) typelist
) ;
773 DisposeNavObjectFilterUPP(mNavFilterUPP
);
774 if ( mDefaultLocation
.dataHandle
!= nil
)
776 ::AEDisposeDesc(&mDefaultLocation
);
779 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
784 if (mNavReply
.validRecord
) {
790 ::AECountItems( &mNavReply
.selection
, &count
) ;
791 for ( long i
= 1 ; i
<= count
; ++i
)
793 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
794 if ( err
!= noErr
) {
798 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
799 if (specDesc
.dataHandle
!= nil
) {
800 ::AEDisposeDesc(&specDesc
);
804 // outFolderDirID = thePB.dirInfo.ioDrDirID;
805 m_path
= wxMacFSSpec2MacFilename( &outFileSpec
) ;
806 m_paths
.Add( m_path
) ;
807 m_fileNames
.Add(m_fileName
);
809 // set these to the first hit
810 m_path
= m_paths
[ 0 ] ;
811 m_fileName
= wxFileNameFromPath(m_path
);
812 m_dir
= wxPathOnly(m_path
);
820 // Generic file load/save dialog
822 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
824 char *ext
= (char *)extension
;
829 str
= "Load %s file";
831 str
= "Save %s file";
832 sprintf(prompt
, wxGetTranslation(str
), what
);
834 if (*ext
== '.') ext
++;
836 sprintf(wild
, "*.%s", ext
);
838 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
841 // Generic file load dialog
843 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
845 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
849 // Generic file save dialog
851 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
853 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);