1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxFileDialog
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 This class represents the file chooser dialog.
14 The path and filename are distinct elements of a full file pathname.
15 If path is ::wxEmptyString, the current directory will be used.
16 If filename is ::wxEmptyString, no default filename will be supplied.
17 The wildcard determines what files are displayed in the file selector,
18 and file extension supplies a type extension for the required filename.
20 The typical usage for the open file dialog is:
22 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
24 if (...current content has not been saved...)
26 if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
27 wxICON_QUESTION | wxYES_NO, this) == wxNO )
29 //else: proceed asking to the user the new file to open
33 openFileDialog(this, _("Open XYZ file"), "", "",
34 "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
36 if (openFileDialog.ShowModal() == wxID_CANCEL)
37 return; // the user changed idea...
39 // proceed loading the file chosen by the user;
40 // this can be done with e.g. wxWidgets input streams:
41 wxFileInputStream input_stream(openFileDialog.GetPath());
42 if (!input_stream.IsOk())
44 wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
52 The typical usage for the save file dialog is instead somewhat simpler:
54 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
57 saveFileDialog(this, _("Save XYZ file"), "", "",
58 "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
60 if (saveFileDialog.ShowModal() == wxID_CANCEL)
61 return; // the user changed idea...
63 // save the current contents in the file;
64 // this can be done with e.g. wxWidgets output streams:
65 wxFileOutputStream output_stream(saveFileDialog.GetPath());
66 if (!output_stream.IsOk())
68 wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
77 All implementations of the wxFileDialog provide a wildcard filter. Typing a filename
78 containing wildcards (*, ?) in the filename text item, and clicking on Ok, will
79 result in only those files matching the pattern being displayed.
80 The wildcard may be a specification for multiple types of file with a description
83 "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
85 It must be noted that wildcard support in the native Motif file dialog is quite
86 limited: only one alternative is supported, and it is displayed without the
87 descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both
88 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif"
92 @style{wxFD_DEFAULT_STYLE}
93 Equivalent to @c wxFD_OPEN.
95 This is an open dialog; usually this means that the default
96 button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
98 This is a save dialog; usually this means that the default button's
99 label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
100 @style{wxFD_OVERWRITE_PROMPT}
101 For save dialog only: prompt for a confirmation if a file will be
103 @style{wxFD_FILE_MUST_EXIST}
104 For open dialog only: the user may only select files that actually exist.
105 @style{wxFD_MULTIPLE}
106 For open dialog only: allows selecting multiple files.
107 @style{wxFD_CHANGE_DIR}
108 Change the current working directory (when the dialog is dismissed)
109 to the directory where the file(s) chosen by the user are.
111 Show the preview of the selected files (currently only supported by
118 @see @ref overview_cmndlg_file, ::wxFileSelector()
120 class wxFileDialog
: public wxDialog
124 Constructor. Use ShowModal() to show the dialog.
129 Message to show on the dialog.
131 The default directory, or the empty string.
133 The default filename, or the empty string.
135 A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
136 Note that the native Motif dialog has some limitations with respect to
137 wildcards; see the Remarks section above.
139 A dialog style. See @c wxFD_* styles for more info.
141 Dialog position. Not implemented.
143 Dialog size. Not implemented.
145 Dialog name. Not implemented.
147 wxFileDialog(wxWindow
* parent
,
148 const wxString
& message
= wxFileSelectorPromptStr
,
149 const wxString
& defaultDir
= wxEmptyString
,
150 const wxString
& defaultFile
= wxEmptyString
,
151 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
152 long style
= wxFD_DEFAULT_STYLE
,
153 const wxPoint
& pos
= wxDefaultPosition
,
154 const wxSize
& size
= wxDefaultSize
,
155 const wxString
& name
= wxFileDialogNameStr
);
160 virtual ~wxFileDialog();
163 Returns the default directory.
165 virtual wxString
GetDirectory() const;
168 If functions SetExtraControlCreator() and ShowModal() were called,
169 returns the extra window. Otherwise returns @NULL.
173 wxWindow
* GetExtraControl() const;
176 Returns the default filename.
178 virtual wxString
GetFilename() const;
181 Fills the array @a filenames with the names of the files chosen.
183 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
184 use GetFilename() for the others.
186 Note that under Windows, if the user selects shortcuts, the filenames
187 include paths, since the application cannot determine the full path
188 of each referenced file by appending the directory containing the shortcuts
191 virtual void GetFilenames(wxArrayString
& filenames
) const;
194 Returns the index into the list of filters supplied, optionally, in the
197 Before the dialog is shown, this is the index which will be used when the
198 dialog is first displayed.
200 After the dialog is shown, this is the index selected by the user.
202 virtual int GetFilterIndex() const;
205 Returns the message that will be displayed on the dialog.
207 virtual wxString
GetMessage() const;
210 Returns the full path (directory and filename) of the selected file.
212 virtual wxString
GetPath() const;
215 Fills the array @a paths with the full paths of the files chosen.
217 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
218 use GetPath() for the others.
220 virtual void GetPaths(wxArrayString
& paths
) const;
223 Returns the file dialog wildcard.
225 virtual wxString
GetWildcard() const;
228 Sets the default directory.
230 virtual void SetDirectory(const wxString
& directory
);
233 The type of function used as an argument for SetExtraControlCreator().
237 typedef wxWindow
*(*ExtraControlCreatorFunction
)(wxWindow
*);
240 Customize file dialog by adding extra window, which is typically placed
241 below the list of files and above the buttons.
243 SetExtraControlCreator() can be called only once, before calling ShowModal().
245 The @c creator function should take pointer to parent window (file dialog)
246 and should return a window allocated with operator new.
248 Supported platforms: wxGTK, wxMSW, wxUniv.
252 bool SetExtraControlCreator(ExtraControlCreatorFunction creator
);
255 Sets the default filename.
257 In wxGTK this will have little effect unless a default directory has previously been set.
259 virtual void SetFilename(const wxString
& setfilename
);
262 Sets the default filter index, starting from zero.
264 virtual void SetFilterIndex(int filterIndex
);
267 Sets the message that will be displayed on the dialog.
269 virtual void SetMessage(const wxString
& message
);
272 Sets the path (the combined directory and filename that will be returned when
273 the dialog is dismissed).
275 virtual void SetPath(const wxString
& path
);
278 Sets the wildcard, which can contain multiple file types, for example:
279 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
281 Note that the native Motif dialog has some limitations with respect to
282 wildcards; see the Remarks section above.
284 virtual void SetWildcard(const wxString
& wildCard
);
287 Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
290 virtual int ShowModal();
295 // ============================================================================
296 // Global functions/macros
297 // ============================================================================
299 /** @addtogroup group_funcmacro_dialog */
303 Pops up a file selector box. In Windows, this is the common file selector
304 dialog. In X, this is a file selector box with the same functionality. The
305 path and filename are distinct elements of a full file pathname. If path
306 is empty, the current directory will be used. If filename is empty, no
307 default filename will be supplied. The wildcard determines what files are
308 displayed in the file selector, and file extension supplies a type
309 extension for the required filename. Flags may be a combination of
310 wxFD_OPEN, wxFD_SAVE, wxFD_OVERWRITE_PROMPT or wxFD_FILE_MUST_EXIST.
312 @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
313 this function only returns a single file name.
315 Both the Unix and Windows versions implement a wildcard filter. Typing a
316 filename containing wildcards (*, ?) in the filename text item, and
317 clicking on Ok, will result in only those files matching the pattern being
320 The wildcard may be a specification for multiple types of file with a
321 description for each, such as:
324 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
327 The application must check for an empty return value (the user pressed
328 Cancel). For example:
331 wxString filename = wxFileSelector("Choose a file to open");
332 if ( !filename.empty() )
334 // work with the file
337 //else: cancelled by user
340 @header{wx/filedlg.h}
342 wxString
wxFileSelector(const wxString
& message
,
343 const wxString
& default_path
= wxEmptyString
,
344 const wxString
& default_filename
= wxEmptyString
,
345 const wxString
& default_extension
= wxEmptyString
,
346 const wxString
& wildcard
= ".",
348 wxWindow
* parent
= NULL
,