Public Types | Public Member Functions

# wxFileDialog Class Reference
[Common Dialogs]

`#include <wx/filedlg.h>`

Inheritance diagram for wxFileDialog:
[legend]

## Detailed Description

This class represents the file chooser dialog.

The path and filename are distinct elements of a full file pathname. If path is wxEmptyString, the current directory will be used. If filename is wxEmptyString, no default filename will be supplied. The wildcard determines what files are displayed in the file selector, and file extension supplies a type extension for the required filename.

The typical usage for the open file dialog is:

        void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
        {
            if (...current content has not been saved...)
            {
                if (wxMessageBox(_("Current content has not been saved! Proceed?"), _("Please confirm"),
                                 wxICON_QUESTION | wxYES_NO, this) == wxNO )
                    return;
                //else: proceed asking to the user the new file to open
            }

            wxFileDialog
                openFileDialog(this, _("Open XYZ file"), "", "",
                               "XYZ files (*.xyz)|*.xyz", wxFD_OPEN|wxFD_FILE_MUST_EXIST);

            if (openFileDialog.ShowModal() == wxID_CANCEL)
                return;     // the user changed idea...

            // proceed loading the file chosen by the user;
            // this can be done with e.g. wxWidgets input streams:
            wxFileInputStream input_stream(openFileDialog.GetPath());
            if (!input_stream.IsOk())
            {
                wxLogError("Cannot open file '%s'.", openFileDialog.GetPath());
                return;
            }

            ...
        }

The typical usage for the save file dialog is instead somewhat simpler:

        void MyFrame::OnSaveAs(wxCommandEvent& WXUNUSED(event))
        {
            wxFileDialog
                saveFileDialog(this, _("Save XYZ file"), "", "",
                               "XYZ files (*.xyz)|*.xyz", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);

            if (saveFileDialog.ShowModal() == wxID_CANCEL)
                return;     // the user changed idea...

            // save the current contents in the file;
            // this can be done with e.g. wxWidgets output streams:
            wxFileOutputStream output_stream(saveFileDialog.GetPath());
            if (!output_stream.IsOk())
            {
                wxLogError("Cannot save current contents in file '%s'.", saveFileDialog.GetPath());
                return;
            }

            ...
        }

Remarks:
  All implementations of the wxFileDialog provide a wildcard filter. Typing a filename containing wildcards (*, ?) in the filename text item, and clicking on Ok, will result in only those files matching the pattern being displayed. The wildcard may be a specification for multiple types of file with a description for each, such as:

               "BMP and GIF files (*.bmp;*.gif)|*.bmp;*.gif|PNG files (*.png)|*.png"

  It must be noted that wildcard support in the native Motif file dialog is quite limited: only one file type is supported, and it is displayed without the descriptive test; "BMP files (*.bmp)|*.bmp" is displayed as "*.bmp", and both "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" and "Image files|*.bmp;*.gif" are errors.

## Styles

This class supports the following styles:

- wxFD_DEFAULT_STYLE:
  Equivalent to `wxFD_OPEN`.
- wxFD_OPEN:
  This is an open dialog; usually this means that the default button's label of the dialog is "Open". Cannot be combined with `wxFD_SAVE`.
- wxFD_SAVE:
  This is a save dialog; usually this means that the default button's label of the dialog is "Save". Cannot be combined with `wxFD_OPEN`.
- wxFD_OVERWRITE_PROMPT:
  For save dialog only: prompt for a confirmation if a file will be overwritten.
- wxFD_FILE_MUST_EXIST:
  For open dialog only: the user may only select files that actually exist. Notice that under OS X the file dialog with `wxFD_OPEN` style always behaves as if this style was specified, because it is impossible to choose a file that doesn't exist from a standard OS X file dialog.
- wxFD_MULTIPLE:
  For open dialog only: allows selecting multiple files.
- wxFD_CHANGE_DIR:
  Change the current working directory (when the dialog is dismissed) to the directory where the file(s) chosen by the user are.
- wxFD_PREVIEW:
  Show the preview of the selected files (currently only supported by wxGTK).

##

Library: wxCore
Category: Common Dialogs

See also:
  wxFileDialog Overview, wxFileSelector()

## Public Types
typedef wxWindow *(* | ExtraControlCreatorFunction )(wxWindow *)
| The type of function used as an argument for SetExtraControlCreator().
## Public Member Functions
| wxFileDialog (wxWindow *parent, const wxString &message=wxFileSelectorPromptStr, const wxString &defaultDir=wxEmptyString, const wxString &defaultFile=wxEmptyString, const wxString &wildcard=wxFileSelectorDefaultWildcardStr, long style=wxFD_DEFAULT_STYLE, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, const wxString &name=wxFileDialogNameStr)
| Constructor.
virtual | ~wxFileDialog ()
| Destructor.
virtual wxString | GetDirectory () const
| Returns the default directory.
wxWindow * | GetExtraControl () const
| If functions SetExtraControlCreator() and ShowModal() were called, returns the extra window.
virtual wxString | GetFilename () const
| Returns the default filename.
virtual void | GetFilenames (wxArrayString &filenames) const
| Fills the array filenames with the names of the files chosen.
virtual int | GetFilterIndex () const
| Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
virtual wxString | GetMessage () const
| Returns the message that will be displayed on the dialog.
virtual wxString | GetPath () const
| Returns the full path (directory and filename) of the selected file.
virtual void | GetPaths (wxArrayString &paths) const
| Fills the array paths with the full paths of the files chosen.
virtual wxString | GetWildcard () const
| Returns the file dialog wildcard.
virtual void | SetDirectory (const wxString &directory)
| Sets the default directory.
bool | SetExtraControlCreator (ExtraControlCreatorFunction creator)
| Customize file dialog by adding extra window, which is typically placed below the list of files and above the buttons.
virtual void | SetFilename (const wxString &setfilename)
| Sets the default filename.
virtual void | SetFilterIndex (int filterIndex)
| Sets the default filter index, starting from zero.
virtual void | SetMessage (const wxString &message)
| Sets the message that will be displayed on the dialog.
virtual void | SetPath (const wxString &path)
| Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
virtual void | SetWildcard (const wxString &wildCard)
| Sets the wildcard, which can contain multiple file types, for example: "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".
virtual int | ShowModal ()
| Shows the dialog, returning `wxID_OK` if the user pressed OK, and `wxID_CANCEL` otherwise.

List of all members.

## Member Typedef Documentation

typedef wxWindow*(* wxFileDialog::ExtraControlCreatorFunction)(wxWindow *)

The type of function used as an argument for SetExtraControlCreator().

Since:
  2.9.0

## Constructor & Destructor Documentation

wxFileDialog::wxFileDialog | ( | wxWindow * | parent,
|  | const wxString & | message = `wxFileSelectorPromptStr`,
|  | const wxString & | defaultDir = `wxEmptyString`,
|  | const wxString & | defaultFile = `wxEmptyString`,
|  | const wxString & | wildcard = `wxFileSelectorDefaultWildcardStr`,
|  | long | style = `wxFD_DEFAULT_STYLE`,
|  | const wxPoint & | pos = `wxDefaultPosition`,
|  | const wxSize & | size = `wxDefaultSize`,
|  | const wxString & | name = `wxFileDialogNameStr` |
| ) |  |  |

Constructor.

Use ShowModal() to show the dialog.

Parameters:

  | parent | Parent window.
  | message | Message to show on the dialog.
  | defaultDir | The default directory, or the empty string.
  | defaultFile | The default filename, or the empty string.
  | wildcard | A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif". Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above.
  | style | A dialog style. See `wxFD_*` styles for more info.
  | pos | Dialog position. Not implemented.
  | size | Dialog size. Not implemented.
  | name | Dialog name. Not implemented.

virtual wxFileDialog::~wxFileDialog | ( |  | ) | `[virtual]`

Destructor.

## Member Function Documentation

virtual wxString wxFileDialog::GetDirectory | ( |  | ) | const`[virtual]`

Returns the default directory.

wxWindow* wxFileDialog::GetExtraControl | ( |  | ) | const

If functions SetExtraControlCreator() and ShowModal() were called, returns the extra window.

Otherwise returns NULL.

Since:
  2.9.0

virtual wxString wxFileDialog::GetFilename | ( |  | ) | const`[virtual]`

Returns the default filename.

virtual void wxFileDialog::GetFilenames | ( | wxArrayString & | filenames | ) | const`[virtual]`

Fills the array filenames with the names of the files chosen.

This function should only be used with the dialogs which have `wxFD_MULTIPLE` style, use GetFilename() for the others.

Note that under Windows, if the user selects shortcuts, the filenames include paths, since the application cannot determine the full path of each referenced file by appending the directory containing the shortcuts to the filename.

virtual int wxFileDialog::GetFilterIndex | ( |  | ) | const`[virtual]`

Returns the index into the list of filters supplied, optionally, in the wildcard parameter.

Before the dialog is shown, this is the index which will be used when the dialog is first displayed.

After the dialog is shown, this is the index selected by the user.

virtual wxString wxFileDialog::GetMessage | ( |  | ) | const`[virtual]`

Returns the message that will be displayed on the dialog.

virtual wxString wxFileDialog::GetPath | ( |  | ) | const`[virtual]`

Returns the full path (directory and filename) of the selected file.

virtual void wxFileDialog::GetPaths | ( | wxArrayString & | paths | ) | const`[virtual]`

Fills the array paths with the full paths of the files chosen.

This function should only be used with the dialogs which have `wxFD_MULTIPLE` style, use GetPath() for the others.

virtual wxString wxFileDialog::GetWildcard | ( |  | ) | const`[virtual]`

Returns the file dialog wildcard.

virtual void wxFileDialog::SetDirectory | ( | const wxString & | directory | ) | `[virtual]`

Sets the default directory.

bool wxFileDialog::SetExtraControlCreator | ( | ExtraControlCreatorFunction | creator | ) |

Customize file dialog by adding extra window, which is typically placed below the list of files and above the buttons.

SetExtraControlCreator() can be called only once, before calling ShowModal().

The `creator` function should take pointer to parent window (file dialog) and should return a window allocated with operator new.

Supported platforms: wxGTK, wxMSW, wxUniv.

Since:
  2.9.0

virtual void wxFileDialog::SetFilename | ( | const wxString & | setfilename | ) | `[virtual]`

Sets the default filename.

In wxGTK this will have little effect unless a default directory has previously been set.

virtual void wxFileDialog::SetFilterIndex | ( | int | filterIndex | ) | `[virtual]`

Sets the default filter index, starting from zero.

virtual void wxFileDialog::SetMessage | ( | const wxString & | message | ) | `[virtual]`

Sets the message that will be displayed on the dialog.

virtual void wxFileDialog::SetPath | ( | const wxString & | path | ) | `[virtual]`

Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).

virtual void wxFileDialog::SetWildcard | ( | const wxString & | wildCard | ) | `[virtual]`

Sets the wildcard, which can contain multiple file types, for example: "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif".

Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above.

virtual int wxFileDialog::ShowModal | ( |  | ) | `[virtual]`

Shows the dialog, returning `wxID_OK` if the user pressed OK, and `wxID_CANCEL` otherwise.

Reimplemented from wxDialog.

|  | [ top ]