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 static pascal void NavEventProc(
50 NavEventCallbackMessage inSelector
,
52 NavCallBackUserData ioUserData
);
55 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
57 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
62 NavEventCallbackMessage inSelector
,
64 NavCallBackUserData
/* ioUserData */)
66 if (inSelector
== kNavCBEvent
) {
67 // In Universal Headers 3.2, Apple changed the definition of
69 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
70 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
73 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
77 wxTheApp
->MacHandleOneEvent(ioParams
->eventData
.eventDataParms
.event
);
88 OSType gfiltersmac
[] =
95 // the data we need to pass to our standard file hook routine
96 // includes a pointer to the dialog, a pointer to the standard
97 // file reply record (so we can inspect the current selection)
98 // and a copy of the "previous" file spec of the reply record
99 // so we can see if the selection has changed
101 const int kwxMacFileTypes
= 10 ;
103 struct OpenUserDataRec
{
104 StandardFileReply
*sfrPtr
;
105 FSSpec oldSelectionFSSpec
;
106 char filter
[kwxMacFileTypes
][10] ;
107 OSType filtermactypes
[kwxMacFileTypes
] ;
111 typedef struct OpenUserDataRec
112 OpenUserDataRec
, *OpenUserDataRecPtr
;
116 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
119 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
120 *(short *) SFSaveDisk
= -1 * newVRefNum
;
121 *(long *) CurDirStore
= newDirID
;
124 static void wxMacSetupStandardFileFromPath( const char* s
)
131 Boolean isDirectory
;
133 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
140 // then copy the rest of the filename
142 for (j
=0;(s
[i
]!=0);i
++,j
++)
148 c2pstr((Ptr
) volume
) ;
152 GetVol( NULL
, &vRefNum
) ;
154 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
155 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
159 kSelectItem
= 10, // select button item number
160 kSFGetFileDlgID
= 251, // dialog resource number
161 kStrListID
= 251, // our strings
162 kSelectStrNum
= 1, // word 'Select: ' for button
163 kDesktopStrNum
= 2, // word 'Desktop' for button
164 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
166 kUseQuotes
= true, // parameter for SetButtonName
167 kDontUseQuotes
= false
170 static void GetLabelString(StringPtr theStr
, short stringNum
)
172 GetIndString(theStr
, kStrListID
, stringNum
);
175 static void CopyPStr(StringPtr src
, StringPtr dest
)
177 BlockMoveData(src
, dest
, 1 + src
[0]);
180 static char GetSelectKey(void)
182 // this is the key used to trigger the select button
184 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
189 // FlashButton briefly highlights the dialog button
190 // as feedback for key equivalents
192 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
197 unsigned long finalTicks
;
199 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
200 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
201 Delay(10, &finalTicks
);
202 HiliteControl((ControlHandle
) buttonHandle
, 0);
205 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
207 return (spec1
->vRefNum
== spec2
->vRefNum
208 && spec1
->parID
== spec2
->parID
209 && EqualString(spec1
->name
, spec2
->name
, false, false));
211 // MyModalDialogFilter maps a key to the Select button, and handles
212 // flashing of the button when the key is hit
214 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
215 short *item
, void *dataPtr
)
217 #pragma unused (dataPtr)
219 // make certain the proper dialog is showing, 'cause standard file
220 // can nest dialogs but calls the same filter for each
222 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
224 // check if the select button was hit
226 if ((eventRec->what == keyDown)
227 && (eventRec->modifiers & cmdKey)
228 && ((eventRec->message & charCodeMask) == GetSelectKey()))
231 FlashButton(theDlgPtr, kSelectItem);
239 #endif !TARGET_CARBON
241 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
244 OpenUserDataRec myData
;
247 Boolean wasAliasedFlag
;
248 DlgHookYDUPP dlgHookUPP
;
249 ModalFilterYDUPP myModalFilterUPP
;
254 // presumably we're running System 7 or later so CustomGetFile is
257 // set initial contents of Select button to a space
259 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
261 // point the user data parameter at the reply record so we can get to it later
263 myData
.sfrPtr
= theSFR
;
264 if ( filter
&& filter
[0] )
266 myData
.numfilters
= 1 ;
267 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
271 strcpy( myData
.filter
[i
] , filter
) ;
272 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
274 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
276 for ( j
= 0 ; gfilters
[j
] ; j
++ )
278 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
280 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
284 if( gfilters
[j
] == NULL
)
286 myData
.filtermactypes
[i
] = '****' ;
292 myData
.numfilters
= 0 ;
294 // display the dialog
299 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
300 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
302 thePt
.h
= thePt
.v
= -1; // center dialog
304 ParamText( message
, NULL
, NULL
, NULL
) ;
306 CustomGetFile( fileFilter
,
307 -1, // show all types
311 thePt
, // top left point
314 nil
, // activate list
315 nil
, // activate proc
318 DisposeRoutineDescriptor(dlgHookUPP
);
319 DisposeRoutineDescriptor(myModalFilterUPP
);
322 // if cancel wasn't pressed and no fatal error occurred...
326 // if no name is in the reply record file spec,
327 // use the file spec of the parent folder
329 if (theSFR
->sfFile
.name
[0] == '\0')
331 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
335 theSFR
->sfFile
= tempSpec
;
339 // no name to return, forget it
341 theSFR
->sfGood
= false;
345 // if there is now a name in the file spec, check if it's
346 // for a folder or a volume
348 if (theSFR
->sfFile
.name
[0] != '\0')
350 // the parID of the root of a disk is always fsRtParID == 1
352 if (theSFR
->sfFile
.parID
== fsRtParID
)
354 theSFR
->sfIsVolume
= true;
355 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
358 // we have a valid FSSpec, now let's make sure it's not for an alias file
360 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
363 theSFR
->sfGood
= false;
366 // did the alias resolve to a folder?
368 if (folderFlag
&& ! theSFR
->sfIsVolume
)
370 theSFR
->sfIsFolder
= true;
376 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, void *dataPtr
)
379 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
380 // return true if this item is invisible or a file
385 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
386 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
388 // because the semantics of the filter proc are "true means don't show
389 // it" we need to invert the result that we return
396 if ( data
->numfilters
> 0 )
398 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
399 if ( filename
[0] >= 4 )
401 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
403 filename
[j
] = toupper( filename
[j
] ) ;
405 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
407 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
410 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
411 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
424 wxString
wxFileSelector(const char *title
,
425 const char *defaultDir
, const char *defaultFileName
,
426 const char *defaultExtension
, const char *filter
, int flags
,
427 wxWindow
*parent
, int x
, int y
)
429 // If there's a default extension specified but no filter, we create a suitable
432 wxString
filter2("");
433 if ( defaultExtension
&& !filter
)
434 filter2
= wxString("*.") + wxString(defaultExtension
) ;
438 wxString defaultDirString
;
440 defaultDirString
= defaultDir
;
442 defaultDirString
= "";
444 wxString defaultFilenameString
;
446 defaultFilenameString
= defaultFileName
;
448 defaultFilenameString
= "";
450 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
452 if ( fileDialog
.ShowModal() == wxID_OK
)
454 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
458 return wxGetEmptyString();
461 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
462 const char *defaultDir
,
463 const char *defaultFileName
,
464 int* defaultFilterIndex
,
472 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
473 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
475 if ( fileDialog
.ShowModal() == wxID_OK
)
477 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
478 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
482 return wxGetEmptyString();
485 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
486 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
487 long style
, const wxPoint
& pos
)
490 m_dialogStyle
= style
;
493 m_fileName
= defaultFileName
;
495 m_wildCard
= wildCard
;
499 int wxFileDialog::ShowModal()
502 if ( !gUseNavServices
)
504 if ( m_dialogStyle
& wxSAVE
)
506 StandardFileReply reply
;
511 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
513 strcpy((char *)prompt
, m_message
) ;
514 c2pstr((char *)prompt
) ;
517 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
519 strcpy((char *)filename
, m_fileName
) ;
520 c2pstr((char *)filename
) ;
525 StandardPutFile( prompt
, filename
, &reply
) ;
529 if ( reply
.sfGood
== false )
536 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
542 OSType types
= '????' ;
547 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
549 strcpy((char *)prompt
, m_message
) ;
550 c2pstr((char *)prompt
) ;
553 c2pstrcpy((StringPtr
)path
, m_dir
) ;
555 strcpy((char *)path
, m_dir
) ;
556 c2pstr((char *)path
) ;
559 StandardFileReply reply
;
560 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
562 crossPlatformFileFilterUPP
=
563 NewFileFilterYDProc(CrossPlatformFileFilter
);
566 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
568 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
570 if ( reply
.sfGood
== false )
577 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
586 NavDialogOptions mNavOptions
;
587 NavObjectFilterUPP mNavFilterUPP
= NULL
;
588 NavPreviewUPP mNavPreviewUPP
= NULL
;
589 NavReplyRecord mNavReply
;
590 AEDesc mDefaultLocation
;
591 bool mSelectDefault
= false ;
593 ::NavGetDefaultDialogOptions(&mNavOptions
);
596 mNavPreviewUPP
= nil
;
597 mSelectDefault
= false;
598 mNavReply
.validRecord
= false;
599 mNavReply
.replacing
= false;
600 mNavReply
.isStationery
= false;
601 mNavReply
.translationNeeded
= false;
602 mNavReply
.selection
.descriptorType
= typeNull
;
603 mNavReply
.selection
.dataHandle
= nil
;
604 mNavReply
.keyScript
= smSystemScript
;
605 mNavReply
.fileTranslation
= nil
;
607 // Set default location, the location
608 // that's displayed when the dialog
612 wxUnixFilename2FSSpec( m_dir
, &location
) ;
615 mDefaultLocation
.descriptorType
= typeNull
;
616 mDefaultLocation
.dataHandle
= nil
;
618 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
620 if ( mDefaultLocation
.dataHandle
) {
622 if (mSelectDefault
) {
623 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
625 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
630 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
632 strcpy((char *)mNavOptions
.message
, m_message
) ;
633 c2pstr((char *)mNavOptions
.message
) ;
636 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
638 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
639 c2pstr((char *)mNavOptions
.savedFileName
) ;
642 if ( m_dialogStyle
& wxSAVE
)
645 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
646 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
647 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
653 sStandardNavEventFilter
,
660 if ( m_dialogStyle
& wxMULTIPLE
)
661 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
663 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
669 sStandardNavEventFilter
,
672 0L /*inFileTypes.TypeListHandle() */,
676 if ( mDefaultLocation
.dataHandle
!= nil
)
678 ::AEDisposeDesc(&mDefaultLocation
);
681 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
686 if (mNavReply
.validRecord
) {
692 ::AECountItems( &mNavReply
.selection
, &count
) ;
693 for ( long i
= 1 ; i
<= count
; ++i
)
695 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
696 if ( err
!= noErr
) {
700 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
701 if (specDesc
.dataHandle
!= nil
) {
702 ::AEDisposeDesc(&specDesc
);
706 // outFolderDirID = thePB.dirInfo.ioDrDirID;
707 m_path
= wxMacFSSpec2UnixFilename( &outFileSpec
) ;
708 m_paths
.Add( m_path
) ;
709 m_fileNames
.Add(m_fileName
);
711 // set these to the first hit
712 m_path
= m_paths
[ 0 ] ;
713 m_fileName
= wxFileNameFromPath(m_path
);
714 m_dir
= wxPathOnly(m_path
);
722 // Generic file load/save dialog
724 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
726 char *ext
= (char *)extension
;
731 str
= "Load %s file";
733 str
= "Save %s file";
734 sprintf(prompt
, wxGetTranslation(str
), what
);
736 if (*ext
== '.') ext
++;
738 sprintf(wild
, "*.%s", ext
);
740 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
743 // Generic file load dialog
745 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
747 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
751 // Generic file save dialog
753 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
755 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);