wxDirDialog
Introduction
The wxDirDialog (wxWidgets: wxDirDialog Class Reference) dialog is the standard directory selection dialog.
Example
The wxDirDialog dialog is easy to use. The example below is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/Dialogs/WxDirDialog1.
In this example a directory selection dialog can be lauched by selecting the "Open..." menu item. When the dialog is closed the selected path is displayed in a wxTextCtrl in the main window.
The source files are shown below. The following modifications were made:
- We added a wxTextCtrl control to the main frame to display the selected path.
- We added a menu with an item to launch the wxDirDialog dialog.
- We added the OnChooseDir event handler for the menu item. It creates the wxDirDialog dialog and when the dialog is closed it sets the value of the wxTextCtrl to the selected path unless the user cancelled the dialog.
#ifndef _TUTORIALS_WXWIDGETS_WXDIRDIALOG1FRAME_H_ #define _TUTORIALS_WXWIDGETS_WXDIRDIALOG1FRAME_H_ #include <wx/frame.h> #include <wx/textctrl.h> class WxDirDialog1Frame : public wxFrame { public: WxDirDialog1Frame(const wxString& title); private: void OnChooseDir(wxCommandEvent& evt); private: wxTextCtrl* m_textCtrl; wxDECLARE_EVENT_TABLE(); }; #endif
#include "WxDirDialog1Frame.h" #include <wx/menu.h> #include <wx/panel.h> #include <wx/sizer.h> #include <wx/dirdlg.h> WxDirDialog1Frame::WxDirDialog1Frame(const wxString& title) : wxFrame(NULL, wxID_ANY, title), m_textCtrl(0) { // The Open dialog is usually accessible from // the "File" menu so we create one. wxMenuBar* menuBar = new wxMenuBar; wxMenu* menuFile = new wxMenu; menuFile->Append(wxID_OPEN); menuBar->Append(menuFile, "&File"); SetMenuBar(menuBar); // Create a top-level panel to hold all the contents of the frame wxPanel* panel = new wxPanel(this, wxID_ANY); // Add a wxTextCtrl that will be updated when the // user selects a directory m_textCtrl = new wxTextCtrl(panel, wxID_ANY, L"", wxDefaultPosition, wxSize(400, 200)); // Set up the sizer for the panel wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL); panelSizer->Add(m_textCtrl, 1, wxEXPAND); panel->SetSizer(panelSizer); // Set up the sizer for the frame and resize the frame // according to its contents wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL); topSizer->Add(panel, 1, wxEXPAND); SetSizerAndFit(topSizer); } // Event handler for the menu item. It will // display a wxDirDialog. void WxDirDialog1Frame::OnChooseDir(wxCommandEvent& evt) { // Create a new wxDirDialog dialog wxDirDialog* dirDialog = new wxDirDialog(this); // Display the dialog and transfer the contents to // the wxTextCtrl on the main frame if the user // doesn't cancel if (dirDialog->ShowModal() == wxID_OK) { wxString selectedFile = dirDialog->GetPath(); m_textCtrl->SetValue(selectedFile); } dirDialog->Destroy(); } // Add the event handler for the menu item // to the event table. wxBEGIN_EVENT_TABLE(WxDirDialog1Frame, wxFrame) EVT_MENU(wxID_OPEN, WxDirDialog1Frame::OnChooseDir) wxEND_EVENT_TABLE()
Figure 1 shows the application when the wxDirDialog dialog is displayed and Figure 2 shows the application once the dialog has been closed.
The rest of the source files don't contain significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WXDIRDIALOG1APP_H_ #define _TUTORIALS_WXWIDGETS_WXDIRDIALOG1APP_H_ #include <wx/app.h> class WxDirDialog1App : public wxApp { public: virtual bool OnInit(); }; #endif
#include "WxDirDialog1App.h" #include "WxDirDialog1Frame.h" wxIMPLEMENT_APP(WxDirDialog1App); bool WxDirDialog1App::OnInit() { WxDirDialog1Frame* frame = new WxDirDialog1Frame("WxDirDialog1"); frame->Show(true); return true; }