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 #if !defined(__UNIX__)
23 #include "PLStringFuncs.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
33 #include <NavigationServices/Navigation.h>
35 #include <Navigation.h>
43 #include "PLStringFuncs.h"
46 extern bool gUseNavServices
;
48 static pascal void NavEventProc(
49 NavEventCallbackMessage inSelector
,
51 NavCallBackUserData ioUserData
);
54 static NavEventUPP sStandardNavEventFilter
= NewNavEventUPP(NavEventProc
);
56 static NavEventUPP sStandardNavEventFilter
= NewNavEventProc(NavEventProc
);
61 NavEventCallbackMessage inSelector
,
63 NavCallBackUserData
/* ioUserData */)
65 if (inSelector
== kNavCBEvent
) {
66 // In Universal Headers 3.2, Apple changed the definition of
68 #if UNIVERSAL_INTERFACES_VERSION >= 0x0320 // Universal Headers 3.2
69 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.eventDataParms.event));
72 UModalAlerts::ProcessModalEvent(*(ioParams->eventData.event));
86 OSType gfiltersmac
[] =
93 // the data we need to pass to our standard file hook routine
94 // includes a pointer to the dialog, a pointer to the standard
95 // file reply record (so we can inspect the current selection)
96 // and a copy of the "previous" file spec of the reply record
97 // so we can see if the selection has changed
99 const int kwxMacFileTypes
= 10 ;
101 struct OpenUserDataRec
{
102 StandardFileReply
*sfrPtr
;
103 FSSpec oldSelectionFSSpec
;
104 char filter
[kwxMacFileTypes
][10] ;
105 OSType filtermactypes
[kwxMacFileTypes
] ;
109 typedef struct OpenUserDataRec
110 OpenUserDataRec
, *OpenUserDataRecPtr
;
114 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
117 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
118 *(short *) SFSaveDisk
= -1 * newVRefNum
;
119 *(long *) CurDirStore
= newDirID
;
122 static void wxMacSetupStandardFileFromPath( const char* s
)
129 Boolean isDirectory
;
131 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
138 // then copy the rest of the filename
140 for (j
=0;(s
[i
]!=0);i
++,j
++)
146 c2pstr((Ptr
) volume
) ;
150 GetVol( NULL
, &vRefNum
) ;
152 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
153 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
157 kSelectItem
= 10, // select button item number
158 kSFGetFileDlgID
= 251, // dialog resource number
159 kStrListID
= 251, // our strings
160 kSelectStrNum
= 1, // word 'Select: ' for button
161 kDesktopStrNum
= 2, // word 'Desktop' for button
162 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
164 kUseQuotes
= true, // parameter for SetButtonName
165 kDontUseQuotes
= false
168 static void GetLabelString(StringPtr theStr
, short stringNum
)
170 GetIndString(theStr
, kStrListID
, stringNum
);
173 static void CopyPStr(StringPtr src
, StringPtr dest
)
175 BlockMoveData(src
, dest
, 1 + src
[0]);
178 static char GetSelectKey(void)
180 // this is the key used to trigger the select button
182 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
187 // FlashButton briefly highlights the dialog button
188 // as feedback for key equivalents
190 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
195 unsigned long finalTicks
;
197 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
198 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
199 Delay(10, &finalTicks
);
200 HiliteControl((ControlHandle
) buttonHandle
, 0);
203 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
205 return (spec1
->vRefNum
== spec2
->vRefNum
206 && spec1
->parID
== spec2
->parID
207 && EqualString(spec1
->name
, spec2
->name
, false, false));
209 // MyModalDialogFilter maps a key to the Select button, and handles
210 // flashing of the button when the key is hit
212 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
213 short *item
, Ptr dataPtr
)
215 #pragma unused (dataPtr)
217 // make certain the proper dialog is showing, 'cause standard file
218 // can nest dialogs but calls the same filter for each
220 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
222 // check if the select button was hit
224 if ((eventRec->what == keyDown)
225 && (eventRec->modifiers & cmdKey)
226 && ((eventRec->message & charCodeMask) == GetSelectKey()))
229 FlashButton(theDlgPtr, kSelectItem);
237 #endif !TARGET_CARBON
239 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
242 OpenUserDataRec myData
;
245 Boolean wasAliasedFlag
;
246 DlgHookYDUPP dlgHookUPP
;
247 ModalFilterYDUPP myModalFilterUPP
;
252 // presumably we're running System 7 or later so CustomGetFile is
255 // set initial contents of Select button to a space
257 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
259 // point the user data parameter at the reply record so we can get to it later
261 myData
.sfrPtr
= theSFR
;
262 if ( filter
&& filter
[0] )
264 myData
.numfilters
= 1 ;
265 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
269 strcpy( myData
.filter
[i
] , filter
) ;
270 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
272 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
274 for ( j
= 0 ; gfilters
[j
] ; j
++ )
276 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
278 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
282 if( gfilters
[j
] == NULL
)
284 myData
.filtermactypes
[i
] = '****' ;
290 myData
.numfilters
= 0 ;
292 // display the dialog
297 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
298 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
300 thePt
.h
= thePt
.v
= -1; // center dialog
302 ParamText( message
, NULL
, NULL
, NULL
) ;
304 CustomGetFile( fileFilter
,
305 -1, // show all types
309 thePt
, // top left point
312 nil
, // activate list
313 nil
, // activate proc
316 DisposeRoutineDescriptor(dlgHookUPP
);
317 DisposeRoutineDescriptor(myModalFilterUPP
);
320 // if cancel wasn't pressed and no fatal error occurred...
324 // if no name is in the reply record file spec,
325 // use the file spec of the parent folder
327 if (theSFR
->sfFile
.name
[0] == '\0')
329 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
333 theSFR
->sfFile
= tempSpec
;
337 // no name to return, forget it
339 theSFR
->sfGood
= false;
343 // if there is now a name in the file spec, check if it's
344 // for a folder or a volume
346 if (theSFR
->sfFile
.name
[0] != '\0')
348 // the parID of the root of a disk is always fsRtParID == 1
350 if (theSFR
->sfFile
.parID
== fsRtParID
)
352 theSFR
->sfIsVolume
= true;
353 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
356 // we have a valid FSSpec, now let's make sure it's not for an alias file
358 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
361 theSFR
->sfGood
= false;
364 // did the alias resolve to a folder?
366 if (folderFlag
&& ! theSFR
->sfIsVolume
)
368 theSFR
->sfIsFolder
= true;
374 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, Ptr dataPtr
)
377 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
378 // return true if this item is invisible or a file
383 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
384 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
386 // because the semantics of the filter proc are "true means don't show
387 // it" we need to invert the result that we return
394 if ( data
->numfilters
> 0 )
396 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
397 if ( filename
[0] >= 4 )
399 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
401 filename
[j
] = toupper( filename
[j
] ) ;
403 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
405 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
408 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
409 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
422 wxString
wxFileSelector(const char *title
,
423 const char *defaultDir
, const char *defaultFileName
,
424 const char *defaultExtension
, const char *filter
, int flags
,
425 wxWindow
*parent
, int x
, int y
)
427 // If there's a default extension specified but no filter, we create a suitable
430 wxString
filter2("");
431 if ( defaultExtension
&& !filter
)
432 filter2
= wxString("*.") + wxString(defaultExtension
) ;
436 wxString defaultDirString
;
438 defaultDirString
= defaultDir
;
440 defaultDirString
= "";
442 wxString defaultFilenameString
;
444 defaultFilenameString
= defaultFileName
;
446 defaultFilenameString
= "";
448 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
450 if ( fileDialog
.ShowModal() == wxID_OK
)
452 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
456 return wxGetEmptyString();
459 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
460 const char *defaultDir
,
461 const char *defaultFileName
,
462 int* defaultFilterIndex
,
470 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
471 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
473 if ( fileDialog
.ShowModal() == wxID_OK
)
475 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
476 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
480 return wxGetEmptyString();
483 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
484 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
485 long style
, const wxPoint
& pos
)
488 m_dialogStyle
= style
;
491 m_fileName
= defaultFileName
;
493 m_wildCard
= wildCard
;
497 int wxFileDialog::ShowModal()
500 if ( !gUseNavServices
)
502 if ( m_dialogStyle
& wxSAVE
)
504 StandardFileReply reply
;
509 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
511 strcpy((char *)prompt
, m_message
) ;
512 c2pstr((char *)prompt
) ;
515 c2pstrcpy((StringPtr
)filename
, m_fileName
) ;
517 strcpy((char *)filename
, m_fileName
) ;
518 c2pstr((char *)filename
) ;
523 StandardPutFile( prompt
, filename
, &reply
) ;
527 if ( reply
.sfGood
== false )
534 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
540 OSType types
= '????' ;
545 c2pstrcpy((StringPtr
)prompt
, m_message
) ;
547 strcpy((char *)prompt
, m_message
) ;
548 c2pstr((char *)prompt
) ;
551 c2pstrcpy((StringPtr
)path
, m_dir
) ;
553 strcpy((char *)path
, m_dir
) ;
554 c2pstr((char *)path
) ;
557 StandardFileReply reply
;
558 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
560 crossPlatformFileFilterUPP
=
561 NewFileFilterYDProc(CrossPlatformFileFilter
);
564 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
566 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
568 if ( reply
.sfGood
== false )
575 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
584 NavDialogOptions mNavOptions
;
585 NavObjectFilterUPP mNavFilterUPP
= NULL
;
586 NavPreviewUPP mNavPreviewUPP
= NULL
;
587 NavReplyRecord mNavReply
;
588 AEDesc mDefaultLocation
;
589 bool mSelectDefault
= false ;
591 ::NavGetDefaultDialogOptions(&mNavOptions
);
594 mNavPreviewUPP
= nil
;
595 mSelectDefault
= false;
596 mNavReply
.validRecord
= false;
597 mNavReply
.replacing
= false;
598 mNavReply
.isStationery
= false;
599 mNavReply
.translationNeeded
= false;
600 mNavReply
.selection
.descriptorType
= typeNull
;
601 mNavReply
.selection
.dataHandle
= nil
;
602 mNavReply
.keyScript
= smSystemScript
;
603 mNavReply
.fileTranslation
= nil
;
605 // Set default location, the location
606 // that's displayed when the dialog
610 wxUnixFilename2FSSpec( m_dir
, &location
) ;
613 mDefaultLocation
.descriptorType
= typeNull
;
614 mDefaultLocation
.dataHandle
= nil
;
616 err
= ::AECreateDesc(typeFSS
, &location
, sizeof(FSSpec
), &mDefaultLocation
);
618 if ( mDefaultLocation
.dataHandle
) {
620 if (mSelectDefault
) {
621 mNavOptions
.dialogOptionFlags
|= kNavSelectDefaultLocation
;
623 mNavOptions
.dialogOptionFlags
&= ~kNavSelectDefaultLocation
;
628 c2pstrcpy((StringPtr
)mNavOptions
.message
, m_message
) ;
630 strcpy((char *)mNavOptions
.message
, m_message
) ;
631 c2pstr((char *)mNavOptions
.message
) ;
634 c2pstrcpy((StringPtr
)mNavOptions
.savedFileName
, m_fileName
) ;
636 strcpy((char *)mNavOptions
.savedFileName
, m_fileName
) ;
637 c2pstr((char *)mNavOptions
.savedFileName
) ;
640 if ( m_dialogStyle
& wxSAVE
)
643 mNavOptions
.dialogOptionFlags
|= kNavNoTypePopup
;
644 mNavOptions
.dialogOptionFlags
|= kNavDontAutoTranslate
;
645 mNavOptions
.dialogOptionFlags
|= kNavDontAddTranslateItems
;
651 sStandardNavEventFilter
,
658 if ( m_dialogStyle
& wxMULTIPLE
)
659 mNavOptions
.dialogOptionFlags
|= kNavAllowMultipleFiles
;
661 mNavOptions
.dialogOptionFlags
&= ~kNavAllowMultipleFiles
;
667 sStandardNavEventFilter
,
670 0L /*inFileTypes.TypeListHandle() */,
674 if ( mDefaultLocation
.dataHandle
!= nil
)
676 ::AEDisposeDesc(&mDefaultLocation
);
679 if ( (err
!= noErr
) && (err
!= userCanceledErr
) ) {
684 if (mNavReply
.validRecord
) {
690 ::AECountItems( &mNavReply
.selection
, &count
) ;
691 for ( long i
= 1 ; i
<= count
; ++i
)
693 OSErr err
= ::AEGetNthDesc( &mNavReply
.selection
, i
, typeFSS
, NULL
, &specDesc
);
694 if ( err
!= noErr
) {
698 outFileSpec
= **(FSSpec
**) specDesc
.dataHandle
;
699 if (specDesc
.dataHandle
!= nil
) {
700 ::AEDisposeDesc(&specDesc
);
704 // outFolderDirID = thePB.dirInfo.ioDrDirID;
705 m_path
= wxMacFSSpec2UnixFilename( &outFileSpec
) ;
706 m_paths
.Add( m_path
) ;
707 m_fileNames
.Add(m_fileName
);
709 // set these to the first hit
710 m_path
= m_paths
[ 0 ] ;
711 m_fileName
= wxFileNameFromPath(m_path
);
712 m_dir
= wxPathOnly(m_path
);
720 // Generic file load/save dialog
722 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
724 char *ext
= (char *)extension
;
729 str
= "Load %s file";
731 str
= "Save %s file";
732 sprintf(prompt
, wxGetTranslation(str
), what
);
734 if (*ext
== '.') ext
++;
736 sprintf(wild
, "*.%s", ext
);
738 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
741 // Generic file load dialog
743 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
745 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
749 // Generic file save dialog
751 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
753 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);