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 static void wxMacSetupStandardFile(short newVRefNum
, long newDirID
)
51 { SFSaveDisk
= 0x214, CurDirStore
= 0x398 };
52 *(short *) SFSaveDisk
= -1 * newVRefNum
;
53 *(long *) CurDirStore
= newDirID
;
56 static void wxMacSetupStandardFileFromPath( const char* s
)
65 for (i
=0 ; (s
[i
]!=0) && (s
[i
]!=':') ;i
++)
72 // then copy the rest of the filename
74 for (j
=0;(s
[i
]!=0);i
++,j
++)
80 c2pstr((Ptr
) volume
) ;
84 GetVol( NULL
, &vRefNum
) ;
86 GetDirectoryID( vRefNum
, fsRtDirID
, path
, &dirRef
, &isDirectory
) ;
87 wxMacSetupStandardFile(vRefNum
, dirRef
) ;
91 kSelectItem
= 10, // select button item number
92 kSFGetFileDlgID
= 251, // dialog resource number
93 kStrListID
= 251, // our strings
94 kSelectStrNum
= 1, // word 'Select: ' for button
95 kDesktopStrNum
= 2, // word 'Desktop' for button
96 kSelectNoQuoteStrNum
= 3, // word 'Select: ' for button
98 kUseQuotes
= true, // parameter for SetButtonName
99 kDontUseQuotes
= false
102 // the data we need to pass to our standard file hook routine
103 // includes a pointer to the dialog, a pointer to the standard
104 // file reply record (so we can inspect the current selection)
105 // and a copy of the "previous" file spec of the reply record
106 // so we can see if the selection has changed
108 const int kwxMacFileTypes
= 10 ;
110 struct OpenUserDataRec
{
111 StandardFileReply
*sfrPtr
;
112 FSSpec oldSelectionFSSpec
;
113 char filter
[kwxMacFileTypes
][10] ;
114 OSType filtermactypes
[kwxMacFileTypes
] ;
118 typedef struct OpenUserDataRec
119 OpenUserDataRec
, *OpenUserDataRecPtr
;
121 static void GetLabelString(StringPtr theStr
, short stringNum
)
123 GetIndString(theStr
, kStrListID
, stringNum
);
126 static void CopyPStr(StringPtr src
, StringPtr dest
)
128 BlockMoveData(src
, dest
, 1 + src
[0]);
131 static char GetSelectKey(void)
133 // this is the key used to trigger the select button
135 // NOT INTERNATIONAL SAVVY; should at least grab it from resources
140 // FlashButton briefly highlights the dialog button
141 // as feedback for key equivalents
143 static void FlashButton(DialogPtr theDlgPtr
, short buttonID
)
148 unsigned long finalTicks
;
150 GetDialogItem(theDlgPtr
, buttonID
, &buttonType
, &buttonHandle
, &buttonRect
);
151 HiliteControl((ControlHandle
) buttonHandle
, kControlButtonPart
);
152 Delay(10, &finalTicks
);
153 HiliteControl((ControlHandle
) buttonHandle
, 0);
156 static Boolean
SameFSSpec(FSSpecPtr spec1
, FSSpecPtr spec2
)
158 return (spec1
->vRefNum
== spec2
->vRefNum
159 && spec1
->parID
== spec2
->parID
160 && EqualString(spec1
->name
, spec2
->name
, false, false));
162 // MyModalDialogFilter maps a key to the Select button, and handles
163 // flashing of the button when the key is hit
165 static pascal Boolean
SFGetFolderModalDialogFilter(DialogPtr theDlgPtr
, EventRecord
*eventRec
,
166 short *item
, Ptr dataPtr
)
168 #pragma unused (dataPtr)
170 // make certain the proper dialog is showing, 'cause standard file
171 // can nest dialogs but calls the same filter for each
173 if (((WindowPeek
) theDlgPtr
)->refCon
== sfMainDialogRefCon
)
175 // check if the select button was hit
177 if ((eventRec->what == keyDown)
178 && (eventRec->modifiers & cmdKey)
179 && ((eventRec->message & charCodeMask) == GetSelectKey()))
182 FlashButton(theDlgPtr, kSelectItem);
191 void ExtendedOpenFile( ConstStr255Param message
, ConstStr255Param path
, const char *filter
, FileFilterYDUPP fileFilter
, StandardFileReply
*theSFR
)
194 OpenUserDataRec myData
;
197 Boolean wasAliasedFlag
;
198 DlgHookYDUPP dlgHookUPP
;
199 ModalFilterYDUPP myModalFilterUPP
;
204 // presumably we're running System 7 or later so CustomGetFile is
207 // set initial contents of Select button to a space
209 CopyPStr("\p ", theSFR
->sfFile
.name
);
211 // point the user data parameter at the reply record so we can get to it later
213 myData
.sfrPtr
= theSFR
;
214 if ( filter
&& filter
[0] )
216 myData
.numfilters
= 1 ;
217 for ( int i
= 0 ; i
< myData
.numfilters
; i
++ )
221 strcpy( myData
.filter
[i
] , filter
) ;
222 for( j
= 0 ; myData
.filter
[i
][j
] ; j
++ )
224 myData
.filter
[i
][j
] = toupper( myData
.filter
[i
][j
] ) ;
226 for ( j
= 0 ; gfilters
[j
] ; j
++ )
228 if ( strcmp( myData
.filter
[i
] , gfilters
[j
] ) == 0 )
230 myData
.filtermactypes
[i
] = gfiltersmac
[j
] ;
234 if( gfilters
[j
] == NULL
)
236 myData
.filtermactypes
[i
] = '****' ;
242 myData
.numfilters
= 0 ;
244 // display the dialog
247 // dlgHookUPP = NewDlgHookYDProc(SFGetFolderDialogHook);
248 myModalFilterUPP
= NewModalFilterYDProc(SFGetFolderModalDialogFilter
);
250 thePt
.h
= thePt
.v
= -1; // center dialog
252 ParamText( message
, NULL
, NULL
, NULL
) ;
254 CustomGetFile( fileFilter
,
255 -1, // show all types
259 thePt
, // top left point
262 nil
, // activate list
263 nil
, // activate proc
266 DisposeRoutineDescriptor(dlgHookUPP
);
267 DisposeRoutineDescriptor(myModalFilterUPP
);
269 // if cancel wasn't pressed and no fatal error occurred...
273 // if no name is in the reply record file spec,
274 // use the file spec of the parent folder
276 if (theSFR
->sfFile
.name
[0] == '\0')
278 err
= FSMakeFSSpec(theSFR
->sfFile
.vRefNum
, theSFR
->sfFile
.parID
,
282 theSFR
->sfFile
= tempSpec
;
286 // no name to return, forget it
288 theSFR
->sfGood
= false;
292 // if there is now a name in the file spec, check if it's
293 // for a folder or a volume
295 if (theSFR
->sfFile
.name
[0] != '\0')
297 // the parID of the root of a disk is always fsRtParID == 1
299 if (theSFR
->sfFile
.parID
== fsRtParID
)
301 theSFR
->sfIsVolume
= true;
302 theSFR
->sfIsFolder
= false; // it would be reasonable for this to be true, too
305 // we have a valid FSSpec, now let's make sure it's not for an alias file
307 err
= ResolveAliasFile(&theSFR
->sfFile
, true, &folderFlag
, &wasAliasedFlag
);
310 theSFR
->sfGood
= false;
313 // did the alias resolve to a folder?
315 if (folderFlag
&& ! theSFR
->sfIsVolume
)
317 theSFR
->sfIsFolder
= true;
323 static pascal Boolean
CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr
, Ptr dataPtr
)
326 OpenUserDataRecPtr data
= (OpenUserDataRecPtr
) dataPtr
;
327 // return true if this item is invisible or a file
332 visibleFlag
= ! (myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
);
333 folderFlag
= (myCInfoPBPtr
->hFileInfo
.ioFlAttrib
& 0x10);
335 // because the semantics of the filter proc are "true means don't show
336 // it" we need to invert the result that we return
343 if ( data
->numfilters
> 0 )
345 PLstrcpy( filename
,myCInfoPBPtr
->hFileInfo
.ioNamePtr
) ;
346 if ( filename
[0] >= 4 )
348 for( int j
= 1 ; j
<= filename
[0] ; j
++ )
350 filename
[j
] = toupper( filename
[j
] ) ;
352 for ( int i
= 0 ; i
< data
->numfilters
; ++i
)
354 if ( myCInfoPBPtr
->hFileInfo
.ioFlFndrInfo
.fdType
== data
->filtermactypes
[i
] )
357 if ( strncmp( (char*) filename
+ 1 + filename
[0] - 4 ,
358 & data
->filter
[i
][ strlen(data
->filter
[i
]) - 4 ] , 4 ) == 0 )
371 wxString
wxFileSelector(const char *title
,
372 const char *defaultDir
, const char *defaultFileName
,
373 const char *defaultExtension
, const char *filter
, int flags
,
374 wxWindow
*parent
, int x
, int y
)
376 // If there's a default extension specified but no filter, we create a suitable
379 wxString
filter2("");
380 if ( defaultExtension
&& !filter
)
381 filter2
= wxString("*.") + wxString(defaultExtension
) ;
385 wxString defaultDirString
;
387 defaultDirString
= defaultDir
;
389 defaultDirString
= "";
391 wxString defaultFilenameString
;
393 defaultFilenameString
= defaultFileName
;
395 defaultFilenameString
= "";
397 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
399 if ( fileDialog
.ShowModal() == wxID_OK
)
401 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
405 return wxGetEmptyString();
408 WXDLLEXPORT wxString
wxFileSelectorEx(const char *title
,
409 const char *defaultDir
,
410 const char *defaultFileName
,
411 int* defaultFilterIndex
,
419 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
420 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
422 if ( fileDialog
.ShowModal() == wxID_OK
)
424 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
425 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
429 return wxGetEmptyString();
432 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
433 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
434 long style
, const wxPoint
& pos
)
437 m_dialogStyle
= style
;
440 m_fileName
= defaultFileName
;
442 m_wildCard
= wildCard
;
446 int wxFileDialog::ShowModal()
448 if ( m_dialogStyle
& wxSAVE
)
450 StandardFileReply reply
;
454 strcpy((char *)prompt
, m_message
) ;
455 c2pstr((char *)prompt
) ;
457 strcpy((char *)filename
, m_fileName
) ;
458 c2pstr((char *)filename
) ;
460 StandardPutFile( prompt
, filename
, &reply
) ;
461 if ( reply
.sfGood
== false )
468 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
474 OSType types
= '????' ;
478 strcpy((char *)prompt
, m_message
) ;
479 c2pstr((char *)prompt
) ;
481 strcpy((char *)path
, m_path
) ;
482 c2pstr((char *)path
) ;
484 FileFilterYDUPP crossPlatformFileFilterUPP
;
485 StandardFileReply reply
;
486 crossPlatformFileFilterUPP
=
487 NewFileFilterYDProc(CrossPlatformFileFilter
);
489 ExtendedOpenFile( prompt
, path
, m_wildCard
, crossPlatformFileFilterUPP
, &reply
);
491 DisposeRoutineDescriptor(crossPlatformFileFilterUPP
);
492 if ( reply
.sfGood
== false )
499 m_path
= wxMacFSSpec2UnixFilename( &reply
.sfFile
) ;
506 // Generic file load/save dialog
508 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
510 char *ext
= (char *)extension
;
515 str
= "Load %s file";
517 str
= "Save %s file";
518 sprintf(prompt
, wxGetTranslation(str
), what
);
520 if (*ext
== '.') ext
++;
522 sprintf(wild
, "*.%s", ext
);
524 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
527 // Generic file load dialog
529 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
531 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
535 // Generic file save dialog
537 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
539 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);