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 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
30 #include "PLStringFuncs.h"
39 OSType gfiltersmac
[] =
46 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
49 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
50 *(short *) SFSaveDisk
= -1 * newVRefNum
;
51 *(long *) CurDirStore
= newDirID
;
54 static void wxMacSetupStandardFileFromPath( const char* s
)
63 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
70 // then copy the rest of the filename
72 for (j
=0;(s
[i
]!=0);i
++,j
++)
78 c2pstr((Ptr
) volume
) ;
82 GetVol( NULL
, &vRefNum
) ;
84 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
85 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
89 kSelectItem
= 10, // select button item number
90 kSFGetFileDlgID
= 251, // dialog resource number
91 kStrListID
= 251, // our strings
92 kSelectStrNum
= 1, // word 'Select: ' for button
93 kDesktopStrNum
= 2, // word 'Desktop' for button
94 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
96 kUseQuotes
= true, // parameter for SetButtonName
97 kDontUseQuotes
= false
100 // the data we need to pass to our standard file hook routine
101 // includes a pointer to the dialog, a pointer to the standard
102 // file reply record (so we can inspect the current selection)
103 // and a copy of the "previous" file spec of the reply record
104 // so we can see if the selection has changed
106 const int kwxMacFileTypes
= 10 ;
108 struct OpenUserDataRec
{
109 StandardFileReply
*sfrPtr
;
110 FSSpec oldSelectionFSSpec
;
111 char filter
[kwxMacFileTypes
][10] ;
112 OSType filtermactypes
[kwxMacFileTypes
] ;
116 typedef struct OpenUserDataRec
117 OpenUserDataRec
, *OpenUserDataRecPtr
;
119 static void GetLabelString(StringPtr theStr
, short stringNum
)
121 GetIndString(theStr
, kStrListID
, stringNum
);
124 static void CopyPStr(StringPtr src
, StringPtr dest
)
126 BlockMoveData(src
, dest
, 1 + src
[0]);
129 static char GetSelectKey(void)
131 // this is the key used to trigger the select button
133 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
138 // FlashButton briefly highlights the dialog button
139 // as feedback for key equivalents
141 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
146 unsigned long finalTicks
;
148 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
149 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
150 Delay(10, &finalTicks
);
151 HiliteControl((ControlHandle
) buttonHandle
, 0);
154 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
156 return (spec1
->vRefNum
== spec2
->vRefNum
157 && spec1
->parID
== spec2
->parID
158 && EqualString(spec1
->name
, spec2
->name
, false, false));
160 // MyModalDialogFilter maps a key to the Select button, and handles
161 // flashing of the button when the key is hit
163 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
164 short *item
, Ptr dataPtr
)
166 #pragma unused (dataPtr)
168 // make certain the proper dialog is showing, 'cause standard file
169 // can nest dialogs but calls the same filter for each
171 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
173 // check if the select button was hit
175 if ((eventRec->what == keyDown)
176 && (eventRec->modifiers & cmdKey)
177 && ((eventRec->message & charCodeMask) == GetSelectKey()))
180 FlashButton(theDlgPtr, kSelectItem);
189 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
192 OpenUserDataRec myData
;
195 Boolean wasAliasedFlag
;
196 DlgHookYDUPP dlgHookUPP
;
197 ModalFilterYDUPP myModalFilterUPP
;
202 // presumably we're running System 7 or later so CustomGetFile is
205 // set initial contents of Select button to a space
207 CopyPStr("\p ", theSFR
->sfFile
.name
);
209 // point the user data parameter at the reply record so we can get to it later
211 myData
.sfrPtr
= theSFR
;
212 if ( filter
&& filter
[0] )
214 myData
.numfilters
= 1 ;
215 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
219 strcpy( myData
.filter
[i
] , filter
) ;
220 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
222 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
224 for ( j
= 0 ; gfilters
[j
] ; j
++ )
226 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
228 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
232 if( gfilters
[j
] == NULL
)
234 myData
.filtermactypes
[i
] = '****' ;
240 myData
.numfilters
= 0 ;
242 // display the dialog
245 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
246 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
248 thePt
.h
= thePt
.v
= -1; // center dialog
250 ParamText( message
, NULL
, NULL
, NULL
) ;
252 CustomGetFile( fileFilter
,
253 -1, // show all types
257 thePt
, // top left point
260 nil
, // activate list
261 nil
, // activate proc
264 DisposeRoutineDescriptor(dlgHookUPP
);
265 DisposeRoutineDescriptor(myModalFilterUPP
);
267 // if cancel wasn't pressed and no fatal error occurred...
271 // if no name is in the reply record file spec,
272 // use the file spec of the parent folder
274 if (theSFR
->sfFile
.name
[0] == '\0')
276 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
280 theSFR
->sfFile
= tempSpec
;
284 // no name to return, forget it
286 theSFR
->sfGood
= false;
290 // if there is now a name in the file spec, check if it's
291 // for a folder or a volume
293 if (theSFR
->sfFile
.name
[0] != '\0')
295 // the parID of the root of a disk is always fsRtParID == 1
297 if (theSFR
->sfFile
.parID
== fsRtParID
)
299 theSFR
->sfIsVolume
= true;
300 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
303 // we have a valid FSSpec, now let's make sure it's not for an alias file
305 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
308 theSFR
->sfGood
= false;
311 // did the alias resolve to a folder?
313 if (folderFlag
&& ! theSFR
->sfIsVolume
)
315 theSFR
->sfIsFolder
= true;
321 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, Ptr dataPtr
)
324 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
325 // return true if this item is invisible or a file
330 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
331 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
333 // because the semantics of the filter proc are "true means don't show
334 // it" we need to invert the result that we return
341 if ( data
->numfilters
> 0 )
343 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
344 if ( filename
[0] >= 4 )
346 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
348 filename
[j
] = toupper( filename
[j
] ) ;
350 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
352 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
355 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
356 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
369 wxString
wxFileSelector(const char *title
,
370 const char *defaultDir
, const char *defaultFileName
,
371 const char *defaultExtension
, const char *filter
, int flags
,
372 wxWindow
*parent
, int x
, int y
)
374 // If there's a default extension specified but no filter, we create a suitable
377 wxString
filter2("");
378 if ( defaultExtension
&& !filter
)
379 filter2
= wxString("*.") + wxString(defaultExtension
) ;
383 wxString defaultDirString
;
385 defaultDirString
= defaultDir
;
387 defaultDirString
= "";
389 wxString defaultFilenameString
;
391 defaultFilenameString
= defaultFileName
;
393 defaultFilenameString
= "";
395 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
397 if ( fileDialog
.ShowModal() == wxID_OK
)
399 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
403 return wxGetEmptyString();
406 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
407 const char *defaultDir
,
408 const char *defaultFileName
,
409 int* defaultFilterIndex
,
417 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
418 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
420 if ( fileDialog
.ShowModal() == wxID_OK
)
422 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
423 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
427 return wxGetEmptyString();
430 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
431 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
432 long style
, const wxPoint
& pos
)
435 m_dialogStyle
= style
;
438 m_fileName
= defaultFileName
;
440 m_wildCard
= wildCard
;
444 int wxFileDialog::ShowModal()
446 if ( m_dialogStyle
& wxSAVE
)
448 StandardFileReply reply
;
452 strcpy((char *)prompt
, m_message
) ;
453 c2pstr((char *)prompt
) ;
455 strcpy((char *)filename
, m_fileName
) ;
456 c2pstr((char *)filename
) ;
458 StandardPutFile( prompt
, filename
, &reply
) ;
459 if ( reply
.sfGood
== false )
466 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
472 OSType types
= '????' ;
476 strcpy((char *)prompt
, m_message
) ;
477 c2pstr((char *)prompt
) ;
479 strcpy((char *)path
, m_path
) ;
480 c2pstr((char *)path
) ;
482 FileFilterYDUPP crossPlatformFileFilterUPP
;
483 StandardFileReply reply
;
484 crossPlatformFileFilterUPP
=
485 NewFileFilterYDProc(CrossPlatformFileFilter
);
487 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
489 DisposeRoutineDescriptor(crossPlatformFileFilterUPP
);
490 if ( reply
.sfGood
== false )
497 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
504 // Generic file load/save dialog
506 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
508 char *ext
= (char *)extension
;
513 str
= "Load %s file";
515 str
= "Save %s file";
516 sprintf(prompt
, wxGetTranslation(str
), what
);
518 if (*ext
== '.') ext
++;
520 sprintf(wild
, "*.%s", ext
);
522 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
525 // Generic file load dialog
527 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
529 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
533 // Generic file save dialog
535 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
537 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);