File and Directory Dialogs
There are two dialogs you can use to get file and directory information from the user: wxFileDialog and wxDirDialog.
wxFileDialog
wxFileDialog can handle the selection of one file or several files, and it has variants for opening and saving files.Figure 8-7 shows the file dialog under Windows.
Figure 8-7. wxFileDialog under Windows
[View full size image]

Figure 8-8. Generic wxFileDialog under GTK+
[View full size image]

Figure 8-9. Native wxFileDialog under GTK+2.4 and above
[View full size image]

Figure 8-10. wxFileDialog under Mac OS X
[View full size image]

Typing a file name containing wildcards ("*", "?") in the file name text item and clicking on OK will result in only those files matching the pattern being displayed.
"BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
wxFileDialog Styles
The file dialog has the styles shown in Table 8-3.
wxSAVE | Specifies a "save" dialog. |
wxOPEN | Specifies an "open" dialog (the default). |
wxOVERWRITE_PROMPT | For a "save" dialog, the user will be prompted if the chosen file already exists. |
wxFILE_MUST_EXIT | The user is forced to select an existing file. |
wxMULTIPLE | The user can select multiple files. |
wxFileDialog Functions
The wxFileDialog functions are as follows.Getdirectory returns the default directory or the directory component of the selected file for a single-selection file dialog. Use SetDirectory to specify the default directory.GetFilename returns the default file name (without the directory) or the selected file name for a single-selection file dialog. Use SetFilename to set the default file name.GetFilenames returns a wxArrayString of the file names of all selections in a multiple-selection dialog. Generally, these file names do not include the directory, but under Windows, if any shortcuts are selected, the file names do include directories. This is because the application cannot determine the full path of each referenced file by appending the file name to the selected directory. Use GetPaths if you want to get an array of the selections including their directories.GetFilterIndex returns a zero-based index of the default or selected filter. Filters are usually displayed in a drop-down list under the list of files. Use SetFilterIndex to set the default index to be displayed when the dialog is shown.GetMessage returns the dialog caption. Use SetMessage to set the caption.GetPath returns the full path (directory and file name) of the file selected by the user or the default path. Use SetPath to set the default path. For a multiple-selection dialog, use GetPaths to get a wxArrayString of all selections including their directories.GetWildcard returns the wildcard specification, and SetWildcard sets it.
wxFileDialog Example
Here's an example of using wxFileDialog to open a single BMP or GIF file:
#include "wx/filedlg.h"
wxString caption = wxT("Choose a file");
wxString wildcard =
wxT("BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif");
wxString defaultDir = wxT("c:\\temp"));
wxString defaultFilename = wxEmptyString;
wxFileDialog dialog(parent, caption, defaultDir, defaultFilename,
wildcard, wxOPEN);
if (dialog.ShowModal() == wxID_OK)
{
wxString path = dialog.GetPath();
int filterIndex = dialog.GetFilterIndex();
}
wxDirDialog
wxDirDialog allows the user to choose a local or network directory (folder). Optionally, it can allow the user to create a new directory if the wxDD_NEW_ DIR_BUTTON style is passed to the constructor.Figure 8-11 shows wxDirDialog under Windows, where Windows supplies the dialog. The generic version of wxDirDialog is used for GTK+ on Linux, as Figure 8-12 shows.
Figure 8-11. wxDirDialog under Windows

Figure 8-12. wxDirDialog under GTK+

Figure 8-13. wxDirDialog under Mac OS X

wxDirDialog Functions
The functions for this dialog are described in the following.SetPath and GetPath are accessors for the default or user-selected directory.SetMessage sets the message that appears on the dialog, and GetMessage returns the current value.
wxDirDialog Example
Using wxDirDialog is easy, as this example shows:
#include "wx/dirdlg.h"
wxString defaultPath = wxT("/");
wxDirDialog dialog(parent,
wxT("Testing directory picker"),
defaultPath, wxDD_NEW_DIR_BUTTON);
if (dialog.ShowModal() == wxID_OK)
{
wxString path = dialog.GetPath();
wxMessageBox(path);
}