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 !USE_SHARED_LIBRARY
23 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
32 #include "PLStringFuncs.h"
41 OSType gfiltersmac
[] =
48 // the data we need to pass to our standard file hook routine
49 // includes a pointer to the dialog, a pointer to the standard
50 // file reply record (so we can inspect the current selection)
51 // and a copy of the "previous" file spec of the reply record
52 // so we can see if the selection has changed
54 const int kwxMacFileTypes
= 10 ;
56 struct OpenUserDataRec
{
57 StandardFileReply
*sfrPtr
;
58 FSSpec oldSelectionFSSpec
;
59 char filter
[kwxMacFileTypes
][10] ;
60 OSType filtermactypes
[kwxMacFileTypes
] ;
64 typedef struct OpenUserDataRec
65 OpenUserDataRec
, *OpenUserDataRecPtr
;
69 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
72 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
73 *(short *) SFSaveDisk
= -1 * newVRefNum
;
74 *(long *) CurDirStore
= newDirID
;
77 static void wxMacSetupStandardFileFromPath( const char* s
)
86 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
93 // then copy the rest of the filename
95 for (j
=0;(s
[i
]!=0);i
++,j
++)
101 c2pstr((Ptr
) volume
) ;
105 GetVol( NULL
, &vRefNum
) ;
107 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
108 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
112 kSelectItem
= 10, // select button item number
113 kSFGetFileDlgID
= 251, // dialog resource number
114 kStrListID
= 251, // our strings
115 kSelectStrNum
= 1, // word 'Select: ' for button
116 kDesktopStrNum
= 2, // word 'Desktop' for button
117 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
119 kUseQuotes
= true, // parameter for SetButtonName
120 kDontUseQuotes
= false
123 static void GetLabelString(StringPtr theStr
, short stringNum
)
125 GetIndString(theStr
, kStrListID
, stringNum
);
128 static void CopyPStr(StringPtr src
, StringPtr dest
)
130 BlockMoveData(src
, dest
, 1 + src
[0]);
133 static char GetSelectKey(void)
135 // this is the key used to trigger the select button
137 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
142 // FlashButton briefly highlights the dialog button
143 // as feedback for key equivalents
145 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
150 unsigned long finalTicks
;
152 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
153 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
154 Delay(10, &finalTicks
);
155 HiliteControl((ControlHandle
) buttonHandle
, 0);
158 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
160 return (spec1
->vRefNum
== spec2
->vRefNum
161 && spec1
->parID
== spec2
->parID
162 && EqualString(spec1
->name
, spec2
->name
, false, false));
164 // MyModalDialogFilter maps a key to the Select button, and handles
165 // flashing of the button when the key is hit
167 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
168 short *item
, Ptr dataPtr
)
170 #pragma unused (dataPtr)
172 // make certain the proper dialog is showing, 'cause standard file
173 // can nest dialogs but calls the same filter for each
175 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
177 // check if the select button was hit
179 if ((eventRec->what == keyDown)
180 && (eventRec->modifiers & cmdKey)
181 && ((eventRec->message & charCodeMask) == GetSelectKey()))
184 FlashButton(theDlgPtr, kSelectItem);
194 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
197 OpenUserDataRec myData
;
200 Boolean wasAliasedFlag
;
201 DlgHookYDUPP dlgHookUPP
;
202 ModalFilterYDUPP myModalFilterUPP
;
207 // presumably we're running System 7 or later so CustomGetFile is
210 // set initial contents of Select button to a space
212 memcpy( theSFR
->sfFile
.name
, "\p " , 2 ) ;
214 // point the user data parameter at the reply record so we can get to it later
216 myData
.sfrPtr
= theSFR
;
217 if ( filter
&& filter
[0] )
219 myData
.numfilters
= 1 ;
220 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
224 strcpy( myData
.filter
[i
] , filter
) ;
225 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
227 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
229 for ( j
= 0 ; gfilters
[j
] ; j
++ )
231 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
233 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
237 if( gfilters
[j
] == NULL
)
239 myData
.filtermactypes
[i
] = '****' ;
245 myData
.numfilters
= 0 ;
247 // display the dialog
252 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
253 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
255 thePt
.h
= thePt
.v
= -1; // center dialog
257 ParamText( message
, NULL
, NULL
, NULL
) ;
259 CustomGetFile( fileFilter
,
260 -1, // show all types
264 thePt
, // top left point
267 nil
, // activate list
268 nil
, // activate proc
271 DisposeRoutineDescriptor(dlgHookUPP
);
272 DisposeRoutineDescriptor(myModalFilterUPP
);
275 // if cancel wasn't pressed and no fatal error occurred...
279 // if no name is in the reply record file spec,
280 // use the file spec of the parent folder
282 if (theSFR
->sfFile
.name
[0] == '\0')
284 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
288 theSFR
->sfFile
= tempSpec
;
292 // no name to return, forget it
294 theSFR
->sfGood
= false;
298 // if there is now a name in the file spec, check if it's
299 // for a folder or a volume
301 if (theSFR
->sfFile
.name
[0] != '\0')
303 // the parID of the root of a disk is always fsRtParID == 1
305 if (theSFR
->sfFile
.parID
== fsRtParID
)
307 theSFR
->sfIsVolume
= true;
308 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
311 // we have a valid FSSpec, now let's make sure it's not for an alias file
313 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
316 theSFR
->sfGood
= false;
319 // did the alias resolve to a folder?
321 if (folderFlag
&& ! theSFR
->sfIsVolume
)
323 theSFR
->sfIsFolder
= true;
329 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, Ptr dataPtr
)
332 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
333 // return true if this item is invisible or a file
338 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
339 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
341 // because the semantics of the filter proc are "true means don't show
342 // it" we need to invert the result that we return
349 if ( data
->numfilters
> 0 )
351 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
352 if ( filename
[0] >= 4 )
354 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
356 filename
[j
] = toupper( filename
[j
] ) ;
358 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
360 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
363 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
364 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
377 wxString
wxFileSelector(const char *title
,
378 const char *defaultDir
, const char *defaultFileName
,
379 const char *defaultExtension
, const char *filter
, int flags
,
380 wxWindow
*parent
, int x
, int y
)
382 // If there's a default extension specified but no filter, we create a suitable
385 wxString
filter2("");
386 if ( defaultExtension
&& !filter
)
387 filter2
= wxString("*.") + wxString(defaultExtension
) ;
391 wxString defaultDirString
;
393 defaultDirString
= defaultDir
;
395 defaultDirString
= "";
397 wxString defaultFilenameString
;
399 defaultFilenameString
= defaultFileName
;
401 defaultFilenameString
= "";
403 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
405 if ( fileDialog
.ShowModal() == wxID_OK
)
407 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
411 return wxGetEmptyString();
414 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
415 const char *defaultDir
,
416 const char *defaultFileName
,
417 int* defaultFilterIndex
,
425 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
426 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
428 if ( fileDialog
.ShowModal() == wxID_OK
)
430 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
431 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
435 return wxGetEmptyString();
438 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
439 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
440 long style
, const wxPoint
& pos
)
443 m_dialogStyle
= style
;
446 m_fileName
= defaultFileName
;
448 m_wildCard
= wildCard
;
452 int wxFileDialog::ShowModal()
454 if ( m_dialogStyle
& wxSAVE
)
456 StandardFileReply reply
;
460 strcpy((char *)prompt
, m_message
) ;
461 c2pstr((char *)prompt
) ;
463 strcpy((char *)filename
, m_fileName
) ;
464 c2pstr((char *)filename
) ;
467 StandardPutFile( prompt
, filename
, &reply
) ;
471 if ( reply
.sfGood
== false )
478 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
484 OSType types
= '????' ;
488 strcpy((char *)prompt
, m_message
) ;
489 c2pstr((char *)prompt
) ;
491 strcpy((char *)path
, m_path
) ;
492 c2pstr((char *)path
) ;
494 StandardFileReply reply
;
495 FileFilterYDUPP crossPlatformFileFilterUPP
= 0 ;
497 crossPlatformFileFilterUPP
=
498 NewFileFilterYDProc(CrossPlatformFileFilter
);
501 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
503 DisposeFileFilterYDUPP(crossPlatformFileFilterUPP
);
505 if ( reply
.sfGood
== false )
512 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
519 // Generic file load/save dialog
521 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
523 char *ext
= (char *)extension
;
528 str
= "Load %s file";
530 str
= "Save %s file";
531 sprintf(prompt
, wxGetTranslation(str
), what
);
533 if (*ext
== '.') ext
++;
535 sprintf(wild
, "*.%s", ext
);
537 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
540 // Generic file load dialog
542 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
544 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
548 // Generic file save dialog
550 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
552 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);