]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/filedlg.h
Added wxPGProperty::Enable() for conveniency. Refactored related code and improved...
[wxWidgets.git] / interface / wx / filedlg.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: filedlg.h
e54c96f1 3// Purpose: interface of wxFileDialog
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
23324ae1
FM
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxFileDialog
7c913512 11
23324ae1 12 This class represents the file chooser dialog.
7c913512 13
0ce6d6c8 14 The path and filename are distinct elements of a full file pathname.
9d90f08b
FM
15 If path is ::wxEmptyString, the current directory will be used.
16 If filename is ::wxEmptyString, no default filename will be supplied.
e9c3992c
FM
17 The wildcard determines what files are displayed in the file selector,
18 and file extension supplies a type extension for the required filename.
0ce6d6c8 19
9d90f08b
FM
20 The typical usage for the open file dialog is:
21 @code
22 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
23 {
24 if (...current content has not been saved...)
25 {
26 if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
27 wxICON_QUESTION | wxYES_NO, this) == wxNO )
28 return;
29 //else: proceed asking to the user the new file to open
30 }
31
32 wxFileDialog
33 openFileDialog(this, _("Open XYZ file"), "", "",
34 "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
35
36 if (openFileDialog.ShowModal() == wxID_CANCEL)
37 return; // the user changed idea...
38
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())
43 {
44 wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
45 return;
46 }
47
48 ...
49 }
50 @endcode
51
52 The typical usage for the save file dialog is instead somewhat simpler:
53 @code
54 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
55 {
56 wxFileDialog
57 saveFileDialog(this, _("Save XYZ file"), "", "",
58 "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
59
60 if (saveFileDialog.ShowModal() == wxID_CANCEL)
61 return; // the user changed idea...
62
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())
67 {
68 wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
69 return;
70 }
71
72 ...
73 }
74 @endcode
75
0ce6d6c8
FM
76 @remarks
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
81 for each, such as:
0b70c946 82 @code
0ce6d6c8 83 "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
0b70c946 84 @endcode
0ce6d6c8
FM
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"
89 are errors.
90
23324ae1 91 @beginStyleTable
8c6791e4 92 @style{wxFD_DEFAULT_STYLE}
9d90f08b 93 Equivalent to @c wxFD_OPEN.
8c6791e4 94 @style{wxFD_OPEN}
23324ae1 95 This is an open dialog; usually this means that the default
9d90f08b 96 button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
8c6791e4 97 @style{wxFD_SAVE}
23324ae1 98 This is a save dialog; usually this means that the default button's
9d90f08b 99 label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
8c6791e4 100 @style{wxFD_OVERWRITE_PROMPT}
23324ae1
FM
101 For save dialog only: prompt for a confirmation if a file will be
102 overwritten.
8c6791e4 103 @style{wxFD_FILE_MUST_EXIST}
0ce6d6c8 104 For open dialog only: the user may only select files that actually exist.
8c6791e4 105 @style{wxFD_MULTIPLE}
23324ae1 106 For open dialog only: allows selecting multiple files.
8c6791e4 107 @style{wxFD_CHANGE_DIR}
9d90f08b
FM
108 Change the current working directory (when the dialog is dismissed)
109 to the directory where the file(s) chosen by the user are.
8c6791e4 110 @style{wxFD_PREVIEW}
23324ae1 111 Show the preview of the selected files (currently only supported by
d6dae1b4 112 wxGTK).
23324ae1 113 @endStyleTable
7c913512 114
23324ae1
FM
115 @library{wxcore}
116 @category{cmndlg}
7c913512 117
23b7f0cb 118 @see @ref overview_cmndlg_file, ::wxFileSelector()
23324ae1
FM
119*/
120class wxFileDialog : public wxDialog
121{
122public:
123 /**
124 Constructor. Use ShowModal() to show the dialog.
3c4f71cc 125
7c913512 126 @param parent
4cc4bfaf 127 Parent window.
7c913512 128 @param message
4cc4bfaf 129 Message to show on the dialog.
7c913512 130 @param defaultDir
4cc4bfaf 131 The default directory, or the empty string.
7c913512 132 @param defaultFile
4cc4bfaf 133 The default filename, or the empty string.
7c913512 134 @param wildcard
0ce6d6c8 135 A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
4cc4bfaf
FM
136 Note that the native Motif dialog has some limitations with respect to
137 wildcards; see the Remarks section above.
7c913512 138 @param style
9d90f08b 139 A dialog style. See @c wxFD_* styles for more info.
7c913512 140 @param pos
4cc4bfaf 141 Dialog position. Not implemented.
7c913512 142 @param size
4cc4bfaf 143 Dialog size. Not implemented.
7c913512 144 @param name
4cc4bfaf 145 Dialog name. Not implemented.
23324ae1
FM
146 */
147 wxFileDialog(wxWindow* parent,
a44f3b5a
FM
148 const wxString& message = wxFileSelectorPromptStr,
149 const wxString& defaultDir = wxEmptyString,
150 const wxString& defaultFile = wxEmptyString,
151 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
23324ae1
FM
152 long style = wxFD_DEFAULT_STYLE,
153 const wxPoint& pos = wxDefaultPosition,
76e9224e 154 const wxSize& size = wxDefaultSize,
a44f3b5a 155 const wxString& name = wxFileDialogNameStr);
23324ae1
FM
156
157 /**
158 Destructor.
159 */
adaaa686 160 virtual ~wxFileDialog();
23324ae1
FM
161
162 /**
163 Returns the default directory.
164 */
adaaa686 165 virtual wxString GetDirectory() const;
23324ae1
FM
166
167 /**
0ce6d6c8 168 If functions SetExtraControlCreator() and ShowModal() were called,
23324ae1 169 returns the extra window. Otherwise returns @NULL.
d6dae1b4
VZ
170
171 @since 2.9.0
23324ae1 172 */
328f5751 173 wxWindow* GetExtraControl() const;
23324ae1
FM
174
175 /**
176 Returns the default filename.
177 */
adaaa686 178 virtual wxString GetFilename() const;
23324ae1
FM
179
180 /**
0ce6d6c8
FM
181 Fills the array @a filenames with the names of the files chosen.
182
183 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
23324ae1 184 use GetFilename() for the others.
0ce6d6c8 185
23324ae1
FM
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
189 to the filename.
190 */
adaaa686 191 virtual void GetFilenames(wxArrayString& filenames) const;
23324ae1
FM
192
193 /**
194 Returns the index into the list of filters supplied, optionally, in the
195 wildcard parameter.
0ce6d6c8 196
23324ae1
FM
197 Before the dialog is shown, this is the index which will be used when the
198 dialog is first displayed.
0ce6d6c8 199
23324ae1
FM
200 After the dialog is shown, this is the index selected by the user.
201 */
adaaa686 202 virtual int GetFilterIndex() const;
23324ae1
FM
203
204 /**
205 Returns the message that will be displayed on the dialog.
206 */
adaaa686 207 virtual wxString GetMessage() const;
23324ae1
FM
208
209 /**
210 Returns the full path (directory and filename) of the selected file.
211 */
adaaa686 212 virtual wxString GetPath() const;
23324ae1
FM
213
214 /**
0ce6d6c8
FM
215 Fills the array @a paths with the full paths of the files chosen.
216
217 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
23324ae1
FM
218 use GetPath() for the others.
219 */
adaaa686 220 virtual void GetPaths(wxArrayString& paths) const;
23324ae1
FM
221
222 /**
223 Returns the file dialog wildcard.
224 */
adaaa686 225 virtual wxString GetWildcard() const;
23324ae1
FM
226
227 /**
228 Sets the default directory.
229 */
adaaa686 230 virtual void SetDirectory(const wxString& directory);
23324ae1 231
d6dae1b4
VZ
232 /**
233 The type of function used as an argument for SetExtraControlCreator().
234
235 @since 2.9.0
236 */
237 typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*);
238
23324ae1
FM
239 /**
240 Customize file dialog by adding extra window, which is typically placed
241 below the list of files and above the buttons.
0ce6d6c8
FM
242
243 SetExtraControlCreator() can be called only once, before calling ShowModal().
244
23324ae1
FM
245 The @c creator function should take pointer to parent window (file dialog)
246 and should return a window allocated with operator new.
0ce6d6c8 247
d6dae1b4
VZ
248 Supported platforms: wxGTK, wxMSW, wxUniv.
249
250 @since 2.9.0
23324ae1 251 */
d6dae1b4 252 bool SetExtraControlCreator(ExtraControlCreatorFunction creator);
23324ae1
FM
253
254 /**
255 Sets the default filename.
02a40b8a
JS
256
257 In wxGTK this will have little effect unless a default directory has previously been set.
23324ae1 258 */
adaaa686 259 virtual void SetFilename(const wxString& setfilename);
23324ae1
FM
260
261 /**
262 Sets the default filter index, starting from zero.
263 */
adaaa686 264 virtual void SetFilterIndex(int filterIndex);
23324ae1
FM
265
266 /**
267 Sets the message that will be displayed on the dialog.
268 */
adaaa686 269 virtual void SetMessage(const wxString& message);
23324ae1
FM
270
271 /**
272 Sets the path (the combined directory and filename that will be returned when
273 the dialog is dismissed).
274 */
adaaa686 275 virtual void SetPath(const wxString& path);
23324ae1
FM
276
277 /**
278 Sets the wildcard, which can contain multiple file types, for example:
0ce6d6c8
FM
279 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
280
23324ae1
FM
281 Note that the native Motif dialog has some limitations with respect to
282 wildcards; see the Remarks section above.
283 */
adaaa686 284 virtual void SetWildcard(const wxString& wildCard);
23324ae1
FM
285
286 /**
9d90f08b 287 Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
23324ae1
FM
288 otherwise.
289 */
adaaa686 290 virtual int ShowModal();
23324ae1
FM
291};
292
293
e54c96f1 294
23324ae1
FM
295// ============================================================================
296// Global functions/macros
297// ============================================================================
298
b21126db 299/** @addtogroup group_funcmacro_dialog */
ba2874ff
BP
300//@{
301
23324ae1
FM
302/**
303 Pops up a file selector box. In Windows, this is the common file selector
ba2874ff
BP
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.
311
312 @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
313 this function only returns a single file name.
314
23324ae1
FM
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
318 displayed.
ba2874ff
BP
319
320 The wildcard may be a specification for multiple types of file with a
321 description for each, such as:
4cc4bfaf 322
23324ae1
FM
323 @code
324 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
325 @endcode
7c913512 326
23324ae1
FM
327 The application must check for an empty return value (the user pressed
328 Cancel). For example:
4cc4bfaf 329
23324ae1
FM
330 @code
331 wxString filename = wxFileSelector("Choose a file to open");
332 if ( !filename.empty() )
333 {
334 // work with the file
335 ...
336 }
337 //else: cancelled by user
338 @endcode
ba2874ff
BP
339
340 @header{wx/filedlg.h}
23324ae1
FM
341*/
342wxString wxFileSelector(const wxString& message,
e9c3992c
FM
343 const wxString& default_path = wxEmptyString,
344 const wxString& default_filename = wxEmptyString,
345 const wxString& default_extension = wxEmptyString,
23324ae1
FM
346 const wxString& wildcard = ".",
347 int flags = 0,
4cc4bfaf 348 wxWindow* parent = NULL,
23324ae1
FM
349 int x = -1,
350 int y = -1);
351
ba2874ff
BP
352//@}
353