1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxFileDialog
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
13 wxFD_OVERWRITE_PROMPT
= 0x0004,
14 wxFD_FILE_MUST_EXIST
= 0x0010,
15 wxFD_MULTIPLE
= 0x0020,
16 wxFD_CHANGE_DIR
= 0x0080,
23 This class represents the file chooser dialog.
25 The path and filename are distinct elements of a full file pathname.
26 If path is ::wxEmptyString, the current directory will be used.
27 If filename is ::wxEmptyString, no default filename will be supplied.
28 The wildcard determines what files are displayed in the file selector,
29 and file extension supplies a type extension for the required filename.
31 The typical usage for the open file dialog is:
33 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
35 if (...current content has not been saved...)
37 if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
38 wxICON_QUESTION | wxYES_NO, this) == wxNO )
40 //else: proceed asking to the user the new file to open
44 openFileDialog(this, _("Open XYZ file"), "", "",
45 "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
47 if (openFileDialog.ShowModal() == wxID_CANCEL)
48 return; // the user changed idea...
50 // proceed loading the file chosen by the user;
51 // this can be done with e.g. wxWidgets input streams:
52 wxFileInputStream input_stream(openFileDialog.GetPath());
53 if (!input_stream.IsOk())
55 wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
63 The typical usage for the save file dialog is instead somewhat simpler:
65 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
68 saveFileDialog(this, _("Save XYZ file"), "", "",
69 "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
71 if (saveFileDialog.ShowModal() == wxID_CANCEL)
72 return; // the user changed idea...
74 // save the current contents in the file;
75 // this can be done with e.g. wxWidgets output streams:
76 wxFileOutputStream output_stream(saveFileDialog.GetPath());
77 if (!output_stream.IsOk())
79 wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
88 All implementations of the wxFileDialog provide a wildcard filter. Typing a filename
89 containing wildcards (*, ?) in the filename text item, and clicking on Ok, will
90 result in only those files matching the pattern being displayed.
91 The wildcard may be a specification for multiple types of file with a description
94 "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
96 It must be noted that wildcard support in the native Motif file dialog is quite
97 limited: only one file type is supported, and it is displayed without the
98 descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both
99 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif"
103 @style{wxFD_DEFAULT_STYLE}
104 Equivalent to @c wxFD_OPEN.
106 This is an open dialog; usually this means that the default
107 button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
109 This is a save dialog; usually this means that the default button's
110 label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
111 @style{wxFD_OVERWRITE_PROMPT}
112 For save dialog only: prompt for a confirmation if a file will be
114 @style{wxFD_FILE_MUST_EXIST}
115 For open dialog only: the user may only select files that actually
116 exist. Notice that under OS X the file dialog with @c wxFD_OPEN
117 style always behaves as if this style was specified, because it is
118 impossible to choose a file that doesn't exist from a standard OS X
120 @style{wxFD_MULTIPLE}
121 For open dialog only: allows selecting multiple files.
122 @style{wxFD_CHANGE_DIR}
123 Change the current working directory (when the dialog is dismissed)
124 to the directory where the file(s) chosen by the user are.
126 Show the preview of the selected files (currently only supported by
133 @see @ref overview_cmndlg_file, ::wxFileSelector()
135 class wxFileDialog
: public wxDialog
139 Constructor. Use ShowModal() to show the dialog.
144 Message to show on the dialog.
146 The default directory, or the empty string.
148 The default filename, or the empty string.
150 A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
151 Note that the native Motif dialog has some limitations with respect to
152 wildcards; see the Remarks section above.
154 A dialog style. See @c wxFD_* styles for more info.
156 Dialog position. Not implemented.
158 Dialog size. Not implemented.
160 Dialog name. Not implemented.
162 wxFileDialog(wxWindow
* parent
,
163 const wxString
& message
= wxFileSelectorPromptStr
,
164 const wxString
& defaultDir
= wxEmptyString
,
165 const wxString
& defaultFile
= wxEmptyString
,
166 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
167 long style
= wxFD_DEFAULT_STYLE
,
168 const wxPoint
& pos
= wxDefaultPosition
,
169 const wxSize
& size
= wxDefaultSize
,
170 const wxString
& name
= wxFileDialogNameStr
);
175 virtual ~wxFileDialog();
178 Returns the default directory.
180 virtual wxString
GetDirectory() const;
183 If functions SetExtraControlCreator() and ShowModal() were called,
184 returns the extra window. Otherwise returns @NULL.
188 wxWindow
* GetExtraControl() const;
191 Returns the default filename.
193 virtual wxString
GetFilename() const;
196 Fills the array @a filenames with the names of the files chosen.
198 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
199 use GetFilename() for the others.
201 Note that under Windows, if the user selects shortcuts, the filenames
202 include paths, since the application cannot determine the full path
203 of each referenced file by appending the directory containing the shortcuts
206 virtual void GetFilenames(wxArrayString
& filenames
) const;
209 Returns the index into the list of filters supplied, optionally, in the
212 Before the dialog is shown, this is the index which will be used when the
213 dialog is first displayed.
215 After the dialog is shown, this is the index selected by the user.
217 virtual int GetFilterIndex() const;
220 Returns the message that will be displayed on the dialog.
222 virtual wxString
GetMessage() const;
225 Returns the full path (directory and filename) of the selected file.
227 virtual wxString
GetPath() const;
230 Fills the array @a paths with the full paths of the files chosen.
232 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
233 use GetPath() for the others.
235 virtual void GetPaths(wxArrayString
& paths
) const;
238 Returns the file dialog wildcard.
240 virtual wxString
GetWildcard() const;
243 Sets the default directory.
245 virtual void SetDirectory(const wxString
& directory
);
248 The type of function used as an argument for SetExtraControlCreator().
252 typedef wxWindow
*(*ExtraControlCreatorFunction
)(wxWindow
*);
255 Customize file dialog by adding extra window, which is typically placed
256 below the list of files and above the buttons.
258 SetExtraControlCreator() can be called only once, before calling ShowModal().
260 The @c creator function should take pointer to parent window (file dialog)
261 and should return a window allocated with operator new.
263 Supported platforms: wxGTK, wxMSW, wxUniv.
267 bool SetExtraControlCreator(ExtraControlCreatorFunction creator
);
270 Sets the default filename.
272 In wxGTK this will have little effect unless a default directory has previously been set.
274 virtual void SetFilename(const wxString
& setfilename
);
277 Sets the default filter index, starting from zero.
279 virtual void SetFilterIndex(int filterIndex
);
282 Sets the message that will be displayed on the dialog.
284 virtual void SetMessage(const wxString
& message
);
287 Sets the path (the combined directory and filename that will be returned when
288 the dialog is dismissed).
290 virtual void SetPath(const wxString
& path
);
293 Sets the wildcard, which can contain multiple file types, for example:
294 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
296 Note that the native Motif dialog has some limitations with respect to
297 wildcards; see the Remarks section above.
299 virtual void SetWildcard(const wxString
& wildCard
);
302 Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
305 virtual int ShowModal();
310 // ============================================================================
311 // Global functions/macros
312 // ============================================================================
314 /** @addtogroup group_funcmacro_dialog */
318 Pops up a file selector box. In Windows, this is the common file selector
319 dialog. In X, this is a file selector box with the same functionality. The
320 path and filename are distinct elements of a full file pathname. If path
321 is empty, the current directory will be used. If filename is empty, no
322 default filename will be supplied. The wildcard determines what files are
323 displayed in the file selector, and file extension supplies a type
324 extension for the required filename. Flags may be a combination of
325 wxFD_OPEN, wxFD_SAVE, wxFD_OVERWRITE_PROMPT or wxFD_FILE_MUST_EXIST.
327 @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
328 this function only returns a single file name.
330 Both the Unix and Windows versions implement a wildcard filter. Typing a
331 filename containing wildcards (*, ?) in the filename text item, and
332 clicking on Ok, will result in only those files matching the pattern being
335 The wildcard may be a specification for multiple types of file with a
336 description for each, such as:
339 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
342 The application must check for an empty return value (the user pressed
343 Cancel). For example:
346 wxString filename = wxFileSelector("Choose a file to open");
347 if ( !filename.empty() )
349 // work with the file
352 //else: cancelled by user
355 @header{wx/filedlg.h}
357 wxString
wxFileSelector(const wxString
& message
,
358 const wxString
& default_path
= wxEmptyString
,
359 const wxString
& default_filename
= wxEmptyString
,
360 const wxString
& default_extension
= wxEmptyString
,
361 const wxString
& wildcard
= ".",
363 wxWindow
* parent
= NULL
,