|
There are two dialogs you can use to get file and directory information from the user: wxFileDialog and wxDirDialog. wxFileDialogwxFileDialog 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 WindowsFigure 8-8 and Figure 8-9 show the file dialog under Linux using GTK+ versions 1 and 2, respectively. Figure 8-8. Generic wxFileDialog under GTK+Figure 8-9. Native wxFileDialog under GTK+2.4 and aboveThe file dialog appearance under Mac OS X is shown in Figure 8-10. Figure 8-10. wxFileDialog under Mac OS XTo create a wxFileDialog, pass a parent window, a message to display in the dialog caption, a default directory, a default file name, a wildcard, the dialog style, and a position (ignored on some platforms). Call ShowModal and test for a wxID_OK return value, which is returned if the user confirms a selection. Directory and file name are distinct elements of a full path name. If the directory is empty, the current directory will be used. If the file name is empty, no default file name will be supplied. The wildcard determines what files are displayed in the file selector. The wildcard may be a specification for multiple types of file with a description for each, such as "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif" 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. wxFileDialog StylesThe file dialog has the styles shown in Table 8-3.
wxFileDialog FunctionsThe 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 ExampleHere'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(); } wxDirDialogwxDirDialog 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 WindowsFigure 8-12. wxDirDialog under GTK+wxDirDialog on Mac OS X (Figure 8-13) looks very much like the file dialog. Figure 8-13. wxDirDialog under Mac OS XTo create the directory dialog, pass a parent window, a message to show on the dialog, a default directory, a window style, a position, and a size (these last two may be ignored, depending on implementation). Call ShowModal and test for a wxID_OK return value, which indicates that the user confirmed a directory selection. wxDirDialog FunctionsThe 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 ExampleUsing 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); } |
|