]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/filedlg.h
Add wxEventLoop::ScheduleExit().
[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
e9321277
RD
9enum
10{
11 wxFD_OPEN = 0x0001,
12 wxFD_SAVE = 0x0002,
13 wxFD_OVERWRITE_PROMPT = 0x0004,
14 wxFD_FILE_MUST_EXIST = 0x0010,
15 wxFD_MULTIPLE = 0x0020,
16 wxFD_CHANGE_DIR = 0x0080,
17 wxFD_PREVIEW = 0x0100
18};
19
50e55c13
RD
20#define wxFD_DEFAULT_STYLE wxFD_OPEN
21
0b59366f
VZ
22/**
23 Default wildcard string used in wxFileDialog corresponding to all files.
24
25 It is defined as "*.*" under MSW and OS/2 and "*" everywhere else.
26*/
27const char wxFileSelectorDefaultWildcardStr[];
28
23324ae1
FM
29/**
30 @class wxFileDialog
7c913512 31
23324ae1 32 This class represents the file chooser dialog.
7c913512 33
0ce6d6c8 34 The path and filename are distinct elements of a full file pathname.
9d90f08b
FM
35 If path is ::wxEmptyString, the current directory will be used.
36 If filename is ::wxEmptyString, no default filename will be supplied.
e9c3992c
FM
37 The wildcard determines what files are displayed in the file selector,
38 and file extension supplies a type extension for the required filename.
0ce6d6c8 39
9d90f08b
FM
40 The typical usage for the open file dialog is:
41 @code
42 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
43 {
44 if (...current content has not been saved...)
45 {
46 if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
47 wxICON_QUESTION | wxYES_NO, this) == wxNO )
48 return;
49 //else: proceed asking to the user the new file to open
50 }
51
52 wxFileDialog
53 openFileDialog(this, _("Open XYZ file"), "", "",
54 "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
55
56 if (openFileDialog.ShowModal() == wxID_CANCEL)
57 return; // the user changed idea...
58
59 // proceed loading the file chosen by the user;
60 // this can be done with e.g. wxWidgets input streams:
61 wxFileInputStream input_stream(openFileDialog.GetPath());
62 if (!input_stream.IsOk())
63 {
64 wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
65 return;
66 }
67
68 ...
69 }
70 @endcode
71
72 The typical usage for the save file dialog is instead somewhat simpler:
73 @code
74 void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
75 {
76 wxFileDialog
77 saveFileDialog(this, _("Save XYZ file"), "", "",
78 "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
79
80 if (saveFileDialog.ShowModal() == wxID_CANCEL)
81 return; // the user changed idea...
82
83 // save the current contents in the file;
84 // this can be done with e.g. wxWidgets output streams:
85 wxFileOutputStream output_stream(saveFileDialog.GetPath());
86 if (!output_stream.IsOk())
87 {
88 wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
89 return;
90 }
91
92 ...
93 }
94 @endcode
95
0ce6d6c8
FM
96 @remarks
97 All implementations of the wxFileDialog provide a wildcard filter. Typing a filename
98 containing wildcards (*, ?) in the filename text item, and clicking on Ok, will
99 result in only those files matching the pattern being displayed.
100 The wildcard may be a specification for multiple types of file with a description
101 for each, such as:
0b70c946 102 @code
0ce6d6c8 103 "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"
0b70c946 104 @endcode
0ce6d6c8 105 It must be noted that wildcard support in the native Motif file dialog is quite
806f8df3 106 limited: only one file type is supported, and it is displayed without the
0ce6d6c8
FM
107 descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both
108 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif"
109 are errors.
110
23324ae1 111 @beginStyleTable
8c6791e4 112 @style{wxFD_DEFAULT_STYLE}
9d90f08b 113 Equivalent to @c wxFD_OPEN.
8c6791e4 114 @style{wxFD_OPEN}
23324ae1 115 This is an open dialog; usually this means that the default
9d90f08b 116 button's label of the dialog is "Open". Cannot be combined with @c wxFD_SAVE.
8c6791e4 117 @style{wxFD_SAVE}
23324ae1 118 This is a save dialog; usually this means that the default button's
9d90f08b 119 label of the dialog is "Save". Cannot be combined with @c wxFD_OPEN.
8c6791e4 120 @style{wxFD_OVERWRITE_PROMPT}
23324ae1
FM
121 For save dialog only: prompt for a confirmation if a file will be
122 overwritten.
8c6791e4 123 @style{wxFD_FILE_MUST_EXIST}
83527a0a
VZ
124 For open dialog only: the user may only select files that actually
125 exist. Notice that under OS X the file dialog with @c wxFD_OPEN
126 style always behaves as if this style was specified, because it is
127 impossible to choose a file that doesn't exist from a standard OS X
128 file dialog.
8c6791e4 129 @style{wxFD_MULTIPLE}
23324ae1 130 For open dialog only: allows selecting multiple files.
8c6791e4 131 @style{wxFD_CHANGE_DIR}
9d90f08b
FM
132 Change the current working directory (when the dialog is dismissed)
133 to the directory where the file(s) chosen by the user are.
8c6791e4 134 @style{wxFD_PREVIEW}
23324ae1 135 Show the preview of the selected files (currently only supported by
d6dae1b4 136 wxGTK).
23324ae1 137 @endStyleTable
7c913512 138
23324ae1
FM
139 @library{wxcore}
140 @category{cmndlg}
7c913512 141
23b7f0cb 142 @see @ref overview_cmndlg_file, ::wxFileSelector()
23324ae1
FM
143*/
144class wxFileDialog : public wxDialog
145{
146public:
147 /**
148 Constructor. Use ShowModal() to show the dialog.
3c4f71cc 149
7c913512 150 @param parent
4cc4bfaf 151 Parent window.
7c913512 152 @param message
4cc4bfaf 153 Message to show on the dialog.
7c913512 154 @param defaultDir
4cc4bfaf 155 The default directory, or the empty string.
7c913512 156 @param defaultFile
4cc4bfaf 157 The default filename, or the empty string.
7c913512 158 @param wildcard
0ce6d6c8 159 A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
4cc4bfaf
FM
160 Note that the native Motif dialog has some limitations with respect to
161 wildcards; see the Remarks section above.
7c913512 162 @param style
9d90f08b 163 A dialog style. See @c wxFD_* styles for more info.
7c913512 164 @param pos
4cc4bfaf 165 Dialog position. Not implemented.
7c913512 166 @param size
4cc4bfaf 167 Dialog size. Not implemented.
7c913512 168 @param name
4cc4bfaf 169 Dialog name. Not implemented.
23324ae1
FM
170 */
171 wxFileDialog(wxWindow* parent,
a44f3b5a
FM
172 const wxString& message = wxFileSelectorPromptStr,
173 const wxString& defaultDir = wxEmptyString,
174 const wxString& defaultFile = wxEmptyString,
175 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
23324ae1
FM
176 long style = wxFD_DEFAULT_STYLE,
177 const wxPoint& pos = wxDefaultPosition,
76e9224e 178 const wxSize& size = wxDefaultSize,
a44f3b5a 179 const wxString& name = wxFileDialogNameStr);
23324ae1
FM
180
181 /**
182 Destructor.
183 */
adaaa686 184 virtual ~wxFileDialog();
23324ae1 185
926df8a1
VZ
186 /**
187 Returns the path of the file currently selected in dialog.
188
189 Notice that this file is not necessarily going to be accepted by the
190 user, so calling this function mostly makes sense from an update UI
191 event handler of a custom file dialog extra control to update its state
192 depending on the currently selected file.
193
194 Currently this function is fully implemented under GTK and MSW and
195 always returns an empty string elsewhere.
196
197 @since 2.9.5
198
199 @return The path of the currently selected file or an empty string if
200 nothing is selected.
201
202 @see SetExtraControlCreator()
203 */
204 virtual wxString GetCurrentlySelectedFilename() const;
205
23324ae1
FM
206 /**
207 Returns the default directory.
208 */
adaaa686 209 virtual wxString GetDirectory() const;
23324ae1
FM
210
211 /**
0ce6d6c8 212 If functions SetExtraControlCreator() and ShowModal() were called,
23324ae1 213 returns the extra window. Otherwise returns @NULL.
d6dae1b4
VZ
214
215 @since 2.9.0
23324ae1 216 */
328f5751 217 wxWindow* GetExtraControl() const;
23324ae1
FM
218
219 /**
220 Returns the default filename.
221 */
adaaa686 222 virtual wxString GetFilename() const;
23324ae1
FM
223
224 /**
0ce6d6c8
FM
225 Fills the array @a filenames with the names of the files chosen.
226
227 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
23324ae1 228 use GetFilename() for the others.
0ce6d6c8 229
23324ae1
FM
230 Note that under Windows, if the user selects shortcuts, the filenames
231 include paths, since the application cannot determine the full path
232 of each referenced file by appending the directory containing the shortcuts
233 to the filename.
234 */
adaaa686 235 virtual void GetFilenames(wxArrayString& filenames) const;
23324ae1
FM
236
237 /**
238 Returns the index into the list of filters supplied, optionally, in the
239 wildcard parameter.
0ce6d6c8 240
23324ae1
FM
241 Before the dialog is shown, this is the index which will be used when the
242 dialog is first displayed.
0ce6d6c8 243
23324ae1
FM
244 After the dialog is shown, this is the index selected by the user.
245 */
adaaa686 246 virtual int GetFilterIndex() const;
23324ae1
FM
247
248 /**
249 Returns the message that will be displayed on the dialog.
250 */
adaaa686 251 virtual wxString GetMessage() const;
23324ae1
FM
252
253 /**
254 Returns the full path (directory and filename) of the selected file.
255 */
adaaa686 256 virtual wxString GetPath() const;
23324ae1
FM
257
258 /**
0ce6d6c8
FM
259 Fills the array @a paths with the full paths of the files chosen.
260
261 This function should only be used with the dialogs which have @c wxFD_MULTIPLE style,
23324ae1
FM
262 use GetPath() for the others.
263 */
adaaa686 264 virtual void GetPaths(wxArrayString& paths) const;
23324ae1
FM
265
266 /**
267 Returns the file dialog wildcard.
268 */
adaaa686 269 virtual wxString GetWildcard() const;
23324ae1
FM
270
271 /**
272 Sets the default directory.
273 */
adaaa686 274 virtual void SetDirectory(const wxString& directory);
23324ae1 275
d6dae1b4
VZ
276 /**
277 The type of function used as an argument for SetExtraControlCreator().
278
279 @since 2.9.0
280 */
281 typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*);
282
23324ae1
FM
283 /**
284 Customize file dialog by adding extra window, which is typically placed
285 below the list of files and above the buttons.
0ce6d6c8
FM
286
287 SetExtraControlCreator() can be called only once, before calling ShowModal().
288
23324ae1
FM
289 The @c creator function should take pointer to parent window (file dialog)
290 and should return a window allocated with operator new.
0ce6d6c8 291
d6dae1b4
VZ
292 Supported platforms: wxGTK, wxMSW, wxUniv.
293
294 @since 2.9.0
23324ae1 295 */
d6dae1b4 296 bool SetExtraControlCreator(ExtraControlCreatorFunction creator);
23324ae1
FM
297
298 /**
299 Sets the default filename.
02a40b8a
JS
300
301 In wxGTK this will have little effect unless a default directory has previously been set.
23324ae1 302 */
adaaa686 303 virtual void SetFilename(const wxString& setfilename);
23324ae1
FM
304
305 /**
306 Sets the default filter index, starting from zero.
307 */
adaaa686 308 virtual void SetFilterIndex(int filterIndex);
23324ae1
FM
309
310 /**
311 Sets the message that will be displayed on the dialog.
312 */
adaaa686 313 virtual void SetMessage(const wxString& message);
23324ae1
FM
314
315 /**
316 Sets the path (the combined directory and filename that will be returned when
317 the dialog is dismissed).
318 */
adaaa686 319 virtual void SetPath(const wxString& path);
23324ae1
FM
320
321 /**
322 Sets the wildcard, which can contain multiple file types, for example:
0ce6d6c8
FM
323 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
324
23324ae1
FM
325 Note that the native Motif dialog has some limitations with respect to
326 wildcards; see the Remarks section above.
327 */
adaaa686 328 virtual void SetWildcard(const wxString& wildCard);
23324ae1
FM
329
330 /**
9d90f08b 331 Shows the dialog, returning @c wxID_OK if the user pressed OK, and @c wxID_CANCEL
23324ae1
FM
332 otherwise.
333 */
adaaa686 334 virtual int ShowModal();
23324ae1
FM
335};
336
337
e54c96f1 338
23324ae1
FM
339// ============================================================================
340// Global functions/macros
341// ============================================================================
342
b21126db 343/** @addtogroup group_funcmacro_dialog */
ba2874ff
BP
344//@{
345
23324ae1
FM
346/**
347 Pops up a file selector box. In Windows, this is the common file selector
ba2874ff
BP
348 dialog. In X, this is a file selector box with the same functionality. The
349 path and filename are distinct elements of a full file pathname. If path
350 is empty, the current directory will be used. If filename is empty, no
351 default filename will be supplied. The wildcard determines what files are
352 displayed in the file selector, and file extension supplies a type
353 extension for the required filename. Flags may be a combination of
354 wxFD_OPEN, wxFD_SAVE, wxFD_OVERWRITE_PROMPT or wxFD_FILE_MUST_EXIST.
355
356 @note wxFD_MULTIPLE can only be used with wxFileDialog and not here since
357 this function only returns a single file name.
358
23324ae1
FM
359 Both the Unix and Windows versions implement a wildcard filter. Typing a
360 filename containing wildcards (*, ?) in the filename text item, and
361 clicking on Ok, will result in only those files matching the pattern being
362 displayed.
ba2874ff
BP
363
364 The wildcard may be a specification for multiple types of file with a
365 description for each, such as:
4cc4bfaf 366
23324ae1
FM
367 @code
368 "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
369 @endcode
7c913512 370
23324ae1
FM
371 The application must check for an empty return value (the user pressed
372 Cancel). For example:
4cc4bfaf 373
23324ae1
FM
374 @code
375 wxString filename = wxFileSelector("Choose a file to open");
376 if ( !filename.empty() )
377 {
378 // work with the file
379 ...
380 }
381 //else: cancelled by user
382 @endcode
ba2874ff
BP
383
384 @header{wx/filedlg.h}
23324ae1
FM
385*/
386wxString wxFileSelector(const wxString& message,
e9c3992c
FM
387 const wxString& default_path = wxEmptyString,
388 const wxString& default_filename = wxEmptyString,
389 const wxString& default_extension = wxEmptyString,
0b59366f 390 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
23324ae1 391 int flags = 0,
4cc4bfaf 392 wxWindow* parent = NULL,
0b59366f
VZ
393 int x = wxDefaultCoord,
394 int y = wxDefaultCoord);
23324ae1 395
68a8473f
VZ
396/**
397 An extended version of wxFileSelector
398*/
399wxString wxFileSelectorEx(const wxString& message = wxFileSelectorPromptStr,
400 const wxString& default_path = wxEmptyString,
401 const wxString& default_filename = wxEmptyString,
402 int *indexDefaultExtension = NULL,
403 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
404 int flags = 0,
405 wxWindow *parent = NULL,
406 int x = wxDefaultCoord,
407 int y = wxDefaultCoord);
408
409/**
410 Ask for filename to load
411*/
412wxString wxLoadFileSelector(const wxString& what,
413 const wxString& extension,
414 const wxString& default_name = wxEmptyString,
415 wxWindow *parent = NULL);
416
417/**
418 Ask for filename to save
419*/
420wxString wxSaveFileSelector(const wxString& what,
421 const wxString& extension,
422 const wxString& default_name = wxEmptyString,
423 wxWindow *parent = NULL);
424
ba2874ff
BP
425//@}
426