wxTreebook
Introduction
The wxTreebook (wxWidgets: wxTreebook Class Reference) control is a container for a set of pages that can be selected by selecting a node in a tree. The control is shown in Figure 1 below.
Simple Example
Below we show a simple example of a wxTreebook control. The example 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/BookControls/WxTreebook1.
#include "WxTreebook1Frame.h" #include <wx/panel.h> #include <wx/treebook.h> #include <wx/sizer.h> WxTreebook1Frame::WxTreebook1Frame(const wxString& title) : wxFrame(NULL, wxID_ANY, title) { // Create a top-level panel to hold all the contents of the frame wxPanel* panel = new wxPanel(this, wxID_ANY); // Create a wxTreebook control wxTreebook* treebook = new wxTreebook(panel, wxID_ANY); // Create the first page and add it to the treebook wxPanel* page1 = new wxPanel(treebook, wxID_ANY); wxTextCtrl* textCtrl1 = new wxTextCtrl(page1, wxID_ANY, "Page 1 Text", wxDefaultPosition, wxSize(150, 50)); wxBoxSizer* page1Sizer = new wxBoxSizer(wxHORIZONTAL); page1Sizer->Add(textCtrl1, 1, wxEXPAND); page1->SetSizer(page1Sizer); treebook->AddPage(page1, "Page 1"); // Create the second page and add it to the treebook wxPanel* page2 = new wxPanel(treebook, wxID_ANY); wxTextCtrl* textCtrl2 = new wxTextCtrl(page2, wxID_ANY, "Page 2 Text", wxDefaultPosition, wxSize(150, 50)); wxBoxSizer* page2Sizer = new wxBoxSizer(wxHORIZONTAL); page2Sizer->Add(textCtrl2, 1, wxEXPAND); page2->SetSizer(page2Sizer); treebook->AddPage(page2, "Page 2"); // Set up the sizer for the panel wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL); panelSizer->Add(treebook, 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->SetMinSize(225, 100); topSizer->Add(panel, 1, wxEXPAND); SetSizerAndFit(topSizer); }
The application is shown in Figure 2 below.
The rest of the source files don't contain significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WXTREEBOOK1FRAME_H_ #define _TUTORIALS_WXWIDGETS_WXTREEBOOK1FRAME_H_ #include <wx/frame.h> class WxTreebook1Frame : public wxFrame { public: WxTreebook1Frame(const wxString& title); }; #endif
#ifndef _TUTORIALS_WXWIDGETS_WXTREEBOOK1APP_H_ #define _TUTORIALS_WXWIDGETS_WXTREEBOOK1APP_H_ #include <wx/app.h> class WxTreebook1App : public wxApp { public: virtual bool OnInit(); }; #endif
#include "WxTreebook1App.h" #include "WxTreebook1Frame.h" wxIMPLEMENT_APP(WxTreebook1App); bool WxTreebook1App::OnInit() { WxTreebook1Frame* frame = new WxTreebook1Frame(L"WxTreebook1"); frame->Show(true); return true; }