1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxFileDialog
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
12 wxFD_OVERWRITE_PROMPT
= 0x0004,
13 wxFD_FILE_MUST_EXIST
= 0x0010,
14 wxFD_MULTIPLE
= 0x0020,
15 wxFD_CHANGE_DIR
= 0x0080,
19 #define wxFD_DEFAULT_STYLE wxFD_OPEN
22 Default wildcard string used in wxFileDialog corresponding to all files.
24 It is defined as "*.*" under MSW and OS/2 and "*" everywhere else.
26 const char wxFileSelectorDefaultWildcardStr
[];
31 This class represents the file chooser dialog.
33 The path and filename are distinct elements of a full file pathname.
34 If path is ::wxEmptyString, the current directory will be used.
35 If filename is ::wxEmptyString, no default filename will be supplied.
36 The wildcard determines what files are displayed in the file selector,
37 and file extension supplies a type extension for the required filename.
39 The typical usage for the open file dialog is:
41 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
43 if (...current content has not been saved...)
45 if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
46 wxICON_QUESTION | wxYES_NO, this) == wxNO )
48 //else: proceed asking to the user the new file to open
52 openFileDialog(this, _("Open XYZ file"), "", "",
53 "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
55 if (openFileDialog.ShowModal() == wxID_CANCEL)
56 return; // the user changed idea...
58 // proceed loading the file chosen by the user;
59 // this can be done with e.g. wxWidgets input streams:
60 wxFileInputStream input_stream(openFileDialog.GetPath());
61 if (!input_stream.IsOk())
63 wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
71 The typical usage for the save file dialog is instead somewhat simpler:
73 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
76 saveFileDialog(this, _("Save XYZ file"), "", "",
77 "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
79 if (saveFileDialog.ShowModal() == wxID_CANCEL)
80 return; // the user changed idea...
82 // save the current contents in the file;
83 // this can be done with e.g. wxWidgets output streams:
84 wxFileOutputStream output_stream(saveFileDialog.GetPath());
85 if (!output_stream.IsOk())
87 wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
96 All implementations of the wxFileDialog provide a wildcard filter. Typing a filename
97 containing wildcards (*, ?) in the filename text item, and clicking on Ok, will
98 result in only those files matching the pattern being displayed.
99 The wildcard may be a specification for multiple types of file with a description
102 "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
104 It must be noted that wildcard support in the native Motif file dialog is quite
105 limited: only one file type is supported, and it is displayed without the
106 descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both
107 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif"
111 @style{wxFD_DEFAULT_STYLE}
112 Equivalent to @c wxFD_OPEN.
114 This is an open dialog; usually this means that the default
115 button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
117 This is a save dialog; usually this means that the default button's
118 label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
119 @style{wxFD_OVERWRITE_PROMPT}
120 For save dialog only: prompt for a confirmation if a file will be
122 @style{wxFD_FILE_MUST_EXIST}
123 For open dialog only: the user may only select files that actually
124 exist. Notice that under OS X the file dialog with @c wxFD_OPEN
125 style always behaves as if this style was specified, because it is
126 impossible to choose a file that doesn't exist from a standard OS X
128 @style{wxFD_MULTIPLE}
129 For open dialog only: allows selecting multiple files.
130 @style{wxFD_CHANGE_DIR}
131 Change the current working directory (when the dialog is dismissed)
132 to the directory where the file(s) chosen by the user are.
134 Show the preview of the selected files (currently only supported by
141 @see @ref overview_cmndlg_file, ::wxFileSelector()
143 class wxFileDialog
: public wxDialog
147 Constructor. Use ShowModal() to show the dialog.
152 Message to show on the dialog.
154 The default directory, or the empty string.
156 The default filename, or the empty string.
158 A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
159 Note that the native Motif dialog has some limitations with respect to
160 wildcards; see the Remarks section above.
162 A dialog style. See @c wxFD_* styles for more info.
164 Dialog position. Not implemented.
166 Dialog size. Not implemented.
168 Dialog name. Not implemented.
170 wxFileDialog(wxWindow
* parent
,
171 const wxString
& message
= wxFileSelectorPromptStr
,
172 const wxString
& defaultDir
= wxEmptyString
,
173 const wxString
& defaultFile
= wxEmptyString
,
174 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
175 long style
= wxFD_DEFAULT_STYLE
,
176 const wxPoint
& pos
= wxDefaultPosition
,
177 const wxSize
& size
= wxDefaultSize
,
178 const wxString
& name
= wxFileDialogNameStr
);
183 virtual ~wxFileDialog();
186 Returns the path of the file currently selected in dialog.
188 Notice that this file is not necessarily going to be accepted by the
189 user, so calling this function mostly makes sense from an update UI
190 event handler of a custom file dialog extra control to update its state
191 depending on the currently selected file.
193 Currently this function is fully implemented under GTK and MSW and
194 always returns an empty string elsewhere.
198 @return The path of the currently selected file or an empty string if
201 @see SetExtraControlCreator()
203 virtual wxString
GetCurrentlySelectedFilename() const;
206 Returns the default directory.
208 virtual wxString
GetDirectory() const;
211 If functions SetExtraControlCreator() and ShowModal() were called,
212 returns the extra window. Otherwise returns @NULL.
216 wxWindow
* GetExtraControl() const;
219 Returns the default filename.
221 virtual wxString
GetFilename() const;
224 Fills the array @a filenames with the names of the files chosen.
226 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
227 use GetFilename() for the others.
229 Note that under Windows, if the user selects shortcuts, the filenames
230 include paths, since the application cannot determine the full path
231 of each referenced file by appending the directory containing the shortcuts
234 virtual void GetFilenames(wxArrayString
& filenames
) const;
237 Returns the index into the list of filters supplied, optionally, in the
240 Before the dialog is shown, this is the index which will be used when the
241 dialog is first displayed.
243 After the dialog is shown, this is the index selected by the user.
245 virtual int GetFilterIndex() const;
248 Returns the message that will be displayed on the dialog.
250 virtual wxString
GetMessage() const;
253 Returns the full path (directory and filename) of the selected file.
255 virtual wxString
GetPath() const;
258 Fills the array @a paths with the full paths of the files chosen.
260 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
261 use GetPath() for the others.
263 virtual void GetPaths(wxArrayString
& paths
) const;
266 Returns the file dialog wildcard.
268 virtual wxString
GetWildcard() const;
271 Sets the default directory.
273 virtual void SetDirectory(const wxString
& directory
);
276 The type of function used as an argument for SetExtraControlCreator().
280 typedef wxWindow
*(*ExtraControlCreatorFunction
)(wxWindow
*);
283 Customize file dialog by adding extra window, which is typically placed
284 below the list of files and above the buttons.
286 SetExtraControlCreator() can be called only once, before calling ShowModal().
288 The @c creator function should take pointer to parent window (file dialog)
289 and should return a window allocated with operator new.
291 Supported platforms: wxGTK, wxMSW, wxUniv.
295 bool SetExtraControlCreator(ExtraControlCreatorFunction creator
);
298 Sets the default filename.
300 In wxGTK this will have little effect unless a default directory has previously been set.
302 virtual void SetFilename(const wxString
& setfilename
);
305 Sets the default filter index, starting from zero.
307 virtual void SetFilterIndex(int filterIndex
);
310 Sets the message that will be displayed on the dialog.
312 virtual void SetMessage(const wxString
& message
);
315 Sets the path (the combined directory and filename that will be returned when
316 the dialog is dismissed).
318 virtual void SetPath(const wxString
& path
);
321 Sets the wildcard, which can contain multiple file types, for example:
322 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
324 Note that the native Motif dialog has some limitations with respect to
325 wildcards; see the Remarks section above.
327 virtual void SetWildcard(const wxString
& wildCard
);
330 Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
333 virtual int ShowModal();
338 // ============================================================================
339 // Global functions/macros
340 // ============================================================================
342 /** @addtogroup group_funcmacro_dialog */
346 Pops up a file selector box. In Windows, this is the common file selector
347 dialog. In X, this is a file selector box with the same functionality. The
348 path and filename are distinct elements of a full file pathname. If path
349 is empty, the current directory will be used. If filename is empty, no
350 default filename will be supplied. The wildcard determines what files are
351 displayed in the file selector, and file extension supplies a type
352 extension for the required filename. Flags may be a combination of
353 wxFD_OPEN, wxFD_SAVE, wxFD_OVERWRITE_PROMPT or wxFD_FILE_MUST_EXIST.
355 @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
356 this function only returns a single file name.
358 Both the Unix and Windows versions implement a wildcard filter. Typing a
359 filename containing wildcards (*, ?) in the filename text item, and
360 clicking on Ok, will result in only those files matching the pattern being
363 The wildcard may be a specification for multiple types of file with a
364 description for each, such as:
367 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
370 The application must check for an empty return value (the user pressed
371 Cancel). For example:
374 wxString filename = wxFileSelector("Choose a file to open");
375 if ( !filename.empty() )
377 // work with the file
380 //else: cancelled by user
383 @header{wx/filedlg.h}
385 wxString
wxFileSelector(const wxString
& message
,
386 const wxString
& default_path
= wxEmptyString
,
387 const wxString
& default_filename
= wxEmptyString
,
388 const wxString
& default_extension
= wxEmptyString
,
389 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
391 wxWindow
* parent
= NULL
,
392 int x
= wxDefaultCoord
,
393 int y
= wxDefaultCoord
);
396 An extended version of wxFileSelector
398 wxString
wxFileSelectorEx(const wxString
& message
= wxFileSelectorPromptStr
,
399 const wxString
& default_path
= wxEmptyString
,
400 const wxString
& default_filename
= wxEmptyString
,
401 int *indexDefaultExtension
= NULL
,
402 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
404 wxWindow
*parent
= NULL
,
405 int x
= wxDefaultCoord
,
406 int y
= wxDefaultCoord
);
409 Ask for filename to load
411 wxString
wxLoadFileSelector(const wxString
& what
,
412 const wxString
& extension
,
413 const wxString
& default_name
= wxEmptyString
,
414 wxWindow
*parent
= NULL
);
417 Ask for filename to save
419 wxString
wxSaveFileSelector(const wxString
& what
,
420 const wxString
& extension
,
421 const wxString
& default_name
= wxEmptyString
,
422 wxWindow
*parent
= NULL
);