1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
18 #include "wx/dialog.h"
19 #include "wx/filedlg.h"
22 #include "PLStringFuncs.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
30 #include "Navigation.h"
36 #include "PLStringFuncs.h"
38 extern bool gUseNavServices
;
40 static pascal void NavEventProc(
41 NavEventCallbackMessage inSelector
,
43 NavCallBackUserData ioUserData
);
46 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
48 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
53 NavEventCallbackMessage inSelector
,
55 NavCallBackUserData
/* ioUserData */)
57 if (inSelector
== kNavCBEvent
) {
58 // In Universal Headers 3.2, Apple changed the definition of
60 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
61 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
64 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
78 OSType gfiltersmac
[] =
85 // the data we need to pass to our standard file hook routine
86 // includes a pointer to the dialog, a pointer to the standard
87 // file reply record (so we can inspect the current selection)
88 // and a copy of the "previous" file spec of the reply record
89 // so we can see if the selection has changed
91 const int kwxMacFileTypes
= 10 ;
93 struct OpenUserDataRec
{
94 StandardFileReply
*sfrPtr
;
95 FSSpec oldSelectionFSSpec
;
96 char filter
[kwxMacFileTypes
][10] ;
97 OSType filtermactypes
[kwxMacFileTypes
] ;
101 typedef struct OpenUserDataRec
102 OpenUserDataRec
, *OpenUserDataRecPtr
;
106 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
109 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
110 *(short *) SFSaveDisk
= -1 * newVRefNum
;
111 *(long *) CurDirStore
= newDirID
;
114 static void wxMacSetupStandardFileFromPath( const char* s
)
121 Boolean isDirectory
;
123 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
130 // then copy the rest of the filename
132 for (j
=0;(s
[i
]!=0);i
++,j
++)
138 c2pstr((Ptr
) volume
) ;
142 GetVol( NULL
, &vRefNum
) ;
144 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
145 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
149 kSelectItem
= 10, // select button item number
150 kSFGetFileDlgID
= 251, // dialog resource number
151 kStrListID
= 251, // our strings
152 kSelectStrNum
= 1, // word 'Select: ' for button
153 kDesktopStrNum
= 2, // word 'Desktop' for button
154 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
156 kUseQuotes
= true, // parameter for SetButtonName
157 kDontUseQuotes
= false
160 static void GetLabelString(StringPtr theStr
, short stringNum
)
162 GetIndString(theStr
, kStrListID
, stringNum
);
165 static void CopyPStr(StringPtr src
, StringPtr dest
)
167 BlockMoveData(src
, dest
, 1 + src
[0]);
170 static char GetSelectKey(void)
172 // this is the key used to trigger the select button
174 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
179 // FlashButton briefly highlights the dialog button
180 // as feedback for key equivalents
182 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
187 unsigned long finalTicks
;
189 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
190 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
191 Delay(10, &finalTicks
);
192 HiliteControl((ControlHandle
) buttonHandle
, 0);
195 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
197 return (spec1
->vRefNum
== spec2
->vRefNum
198 && spec1
->parID
== spec2
->parID
199 && EqualString(spec1
->name
, spec2
->name
, false, false));
201 // MyModalDialogFilter maps a key to the Select button, and handles
202 // flashing of the button when the key is hit
204 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
205 short *item
, Ptr dataPtr
)
207 #pragma unused (dataPtr)
209 // make certain the proper dialog is showing, 'cause standard file
210 // can nest dialogs but calls the same filter for each
212 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
214 // check if the select button was hit
216 if ((eventRec->what == keyDown)
217 && (eventRec->modifiers & cmdKey)
218 && ((eventRec->message & charCodeMask) == GetSelectKey()))
221 FlashButton(theDlgPtr, kSelectItem);
229 #endif !TARGET_CARBON
231 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
234 OpenUserDataRec myData
;
237 Boolean wasAliasedFlag
;
238 DlgHookYDUPP dlgHookUPP
;
239 ModalFilterYDUPP myModalFilterUPP
;
244 // presumably we're running System 7 or later so CustomGetFile is
247 // set initial contents of Select button to a space
249 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
251 // point the user data parameter at the reply record so we can get to it later
253 myData
.sfrPtr
= theSFR
;
254 if ( filter
&& filter
[0] )
256 myData
.numfilters
= 1 ;
257 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
261 strcpy( myData
.filter
[i
] , filter
) ;
262 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
264 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
266 for ( j
= 0 ; gfilters
[j
] ; j
++ )
268 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
270 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
274 if( gfilters
[j
] == NULL
)
276 myData
.filtermactypes
[i
] = '****' ;
282 myData
.numfilters
= 0 ;
284 // display the dialog
289 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
290 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
292 thePt
.h
= thePt
.v
= -1; // center dialog
294 ParamText( message
, NULL
, NULL
, NULL
) ;
296 CustomGetFile( fileFilter
,
297 -1, // show all types
301 thePt
, // top left point
304 nil
, // activate list
305 nil
, // activate proc
308 DisposeRoutineDescriptor(dlgHookUPP
);
309 DisposeRoutineDescriptor(myModalFilterUPP
);
312 // if cancel wasn't pressed and no fatal error occurred...
316 // if no name is in the reply record file spec,
317 // use the file spec of the parent folder
319 if (theSFR
->sfFile
.name
[0] == '\0')
321 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
325 theSFR
->sfFile
= tempSpec
;
329 // no name to return, forget it
331 theSFR
->sfGood
= false;
335 // if there is now a name in the file spec, check if it's
336 // for a folder or a volume
338 if (theSFR
->sfFile
.name
[0] != '\0')
340 // the parID of the root of a disk is always fsRtParID == 1
342 if (theSFR
->sfFile
.parID
== fsRtParID
)
344 theSFR
->sfIsVolume
= true;
345 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
348 // we have a valid FSSpec, now let's make sure it's not for an alias file
350 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
353 theSFR
->sfGood
= false;
356 // did the alias resolve to a folder?
358 if (folderFlag
&& ! theSFR
->sfIsVolume
)
360 theSFR
->sfIsFolder
= true;
366 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, Ptr dataPtr
)
369 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
370 // return true if this item is invisible or a file
375 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
376 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
378 // because the semantics of the filter proc are "true means don't show
379 // it" we need to invert the result that we return
386 if ( data
->numfilters
> 0 )
388 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
389 if ( filename
[0] >= 4 )
391 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
393 filename
[j
] = toupper( filename
[j
] ) ;
395 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
397 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
400 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
401 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
414 wxString
wxFileSelector(const char *title
,
415 const char *defaultDir
, const char *defaultFileName
,
416 const char *defaultExtension
, const char *filter
, int flags
,
417 wxWindow
*parent
, int x
, int y
)
419 // If there's a default extension specified but no filter, we create a suitable
422 wxString
filter2("");
423 if ( defaultExtension
&& !filter
)
424 filter2
= wxString("*.") + wxString(defaultExtension
) ;
428 wxString defaultDirString
;
430 defaultDirString
= defaultDir
;
432 defaultDirString
= "";
434 wxString defaultFilenameString
;
436 defaultFilenameString
= defaultFileName
;
438 defaultFilenameString
= "";
440 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
442 if ( fileDialog
.ShowModal() == wxID_OK
)
444 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
448 return wxGetEmptyString();
451 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
452 const char *defaultDir
,
453 const char *defaultFileName
,
454 int* defaultFilterIndex
,
462 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
463 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
465 if ( fileDialog
.ShowModal() == wxID_OK
)
467 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
468 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
472 return wxGetEmptyString();
475 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
476 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
477 long style
, const wxPoint
& pos
)
480 m_dialogStyle
= style
;
483 m_fileName
= defaultFileName
;
485 m_wildCard
= wildCard
;
489 int wxFileDialog::ShowModal()
492 if ( !gUseNavServices
)
494 if ( m_dialogStyle
& wxSAVE
)
496 StandardFileReply reply
;
500 strcpy((char *)prompt
, m_message
) ;
501 c2pstr((char *)prompt
) ;
503 strcpy((char *)filename
, m_fileName
) ;
504 c2pstr((char *)filename
) ;
507 StandardPutFile( prompt
, filename
, &reply
) ;
511 if ( reply
.sfGood
== false )
518 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
524 OSType types
= '????' ;
528 strcpy((char *)prompt
, m_message
) ;
529 c2pstr((char *)prompt
) ;
531 strcpy((char *)path
, m_dir
) ;
532 c2pstr((char *)path
) ;
534 StandardFileReply reply
;
535 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
537 crossPlatformFileFilterUPP
=
538 NewFileFilterYDProc(CrossPlatformFileFilter
);
541 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
543 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
545 if ( reply
.sfGood
== false )
552 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
561 NavDialogOptions mNavOptions
;
562 NavObjectFilterUPP mNavFilterUPP
= NULL
;
563 NavPreviewUPP mNavPreviewUPP
= NULL
;
564 NavReplyRecord mNavReply
;
565 AEDesc mDefaultLocation
;
566 bool mSelectDefault
= false ;
568 ::NavGetDefaultDialogOptions(&mNavOptions
);
571 mNavPreviewUPP
= nil
;
572 mSelectDefault
= false;
573 mNavReply
.validRecord
= false;
574 mNavReply
.replacing
= false;
575 mNavReply
.isStationery
= false;
576 mNavReply
.translationNeeded
= false;
577 mNavReply
.selection
.descriptorType
= typeNull
;
578 mNavReply
.selection
.dataHandle
= nil
;
579 mNavReply
.keyScript
= smSystemScript
;
580 mNavReply
.fileTranslation
= nil
;
582 // Set default location, the location
583 // that's displayed when the dialog
587 wxUnixFilename2FSSpec( m_dir
, &location
) ;
590 mDefaultLocation
.descriptorType
= typeNull
;
591 mDefaultLocation
.dataHandle
= nil
;
593 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
595 if ( mDefaultLocation
.dataHandle
) {
597 if (mSelectDefault
) {
598 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
600 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
604 strcpy((char *)mNavOptions
.message
, m_message
) ;
605 c2pstr((char *)mNavOptions
.message
) ;
607 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
608 c2pstr((char *)mNavOptions
.savedFileName
) ;
610 if ( m_dialogStyle
& wxSAVE
)
613 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
614 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
615 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
621 sStandardNavEventFilter
,
628 if ( m_dialogStyle
& wxMULTIPLE
)
629 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
631 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
637 sStandardNavEventFilter
,
640 0L /*inFileTypes.TypeListHandle() */,
644 if ( mDefaultLocation
.dataHandle
!= nil
)
646 ::AEDisposeDesc(&mDefaultLocation
);
649 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
654 if (mNavReply
.validRecord
) {
660 ::AECountItems( &mNavReply
.selection
, &count
) ;
661 for ( long i
= 1 ; i
<= count
; ++i
)
663 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
664 if ( err
!= noErr
) {
668 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
669 if (specDesc
.dataHandle
!= nil
) {
670 ::AEDisposeDesc(&specDesc
);
674 // outFolderDirID = thePB.dirInfo.ioDrDirID;
675 m_path
= wxMacFSSpec2UnixFilename( &outFileSpec
) ;
676 m_paths
.Add( m_path
) ;
677 m_fileNames
.Add(m_fileName
);
679 // set these to the first hit
680 m_path
= m_paths
[ 0 ] ;
681 m_fileName
= wxFileNameFromPath(m_path
);
682 m_dir
= wxPathOnly(m_path
);
690 // Generic file load/save dialog
692 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
694 char *ext
= (char *)extension
;
699 str
= "Load %s file";
701 str
= "Save %s file";
702 sprintf(prompt
, wxGetTranslation(str
), what
);
704 if (*ext
== '.') ext
++;
706 sprintf(wild
, "*.%s", ext
);
708 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
711 // Generic file load dialog
713 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
715 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
719 // Generic file save dialog
721 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
723 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);