Search code examples
c++wxwidgetswxnotebook

How can I make a sizer expand to the whole wxNotebook page?


I have a wxNotebook with two added pages. Each page uses a wxFlexGridSizer to manage the various widgets on each page.

The first page has three columns of widgets, the second page only two. The first page is wider than the second page.

The notebook is resized to the size of its widest page (the first one). However, on the second page I would like to have the sizer expand to fill the entire page, too. Instead, it just resizes to fit the widgets on that page.

So my question is: How can I make the sizer of the second page expand to the whole notebook width, which is determined by the width of the first page?

I have tried calling the Layout() function of the parent wxNotebook, but that did not help.

Edit:

wxNotebook *notebook = new wxNotebook(this, wxID_ANY);

wxPanel* pageOne = new wxPanel(notebook, wxID_ANY);
wxFlexGridSizer* sizer = new wxFlexGridSizer(3);
wxSizerFlags flags = wxSizerFlags().Align(wxLEFT).Border(wxRIGHT, 5);
wxStaticText* label = new wxStaticText(pageOne, wxID_ANY, wxT("Label 1"));
sizer->Add(label, flags);
label = new wxStaticText(pageOne, wxID_ANY, wxT("Label 2"));
sizer->Add(label, flags);
wxSpinCtrlDouble* value = new wxSpinCtrlDouble(pageOne, wxID_ANY, wxT("50.0"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
sizer->Add(value, flags);
pageOne->SetSizer(sizer);
notebook->AddPage(pageOne, wxT("Page 1"));

wxPanel* pageTwo = new wxPanel(notebook, wxID_ANY);
sizer = new wxFlexGridSizer(2);
flags = wxSizerFlags().Align(wxLEFT).Border(wxRIGHT, 5);
label = new wxStaticText(pageTwo, wxID_ANY, wxT("Label 1"));
sizer->Add(label, flags);
value = new wxSpinCtrlDouble(pageTwo, wxID_ANY, wxT("50.0"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
sizer->Add(value, flags);
pageTwo->SetSizer(sizer);
notebook->AddPage(pageTwo, wxT("Page 2"));

topLevelSizer->Add(notebook);
this->SetSizerAndFit(topLevelSizer);

Solution

  • You just need to make 2 small changes. First with the flex grid sizer on the second page you need to call AddGrowableCol(1). This means that the second column can expand. (The indicies start at 0, so column 1 is the second column.)

    Second, you need to use slightly different sizer flags when adding the spin control double. For example, something like

    sizer->Add(value, wxSizerFlags(0).Expand().Border(wxRIGHT, 5));
    

    Here's the snippet you posted with these two changes:

    wxNotebook *notebook = new wxNotebook(this, wxID_ANY);
    
    wxPanel* pageOne = new wxPanel(notebook, wxID_ANY);
    wxFlexGridSizer* sizer = new wxFlexGridSizer(3);
    wxSizerFlags flags = wxSizerFlags().Align(wxLEFT).Border(wxRIGHT, 5);
    wxStaticText* label = new wxStaticText(pageOne, wxID_ANY, wxT("Label 1"));
    sizer->Add(label, flags);
    label = new wxStaticText(pageOne, wxID_ANY, wxT("Label 2"));
    sizer->Add(label, flags);
    wxSpinCtrlDouble* value = new wxSpinCtrlDouble(pageOne, wxID_ANY, wxT("50.0"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    sizer->Add(value, flags);
    pageOne->SetSizer(sizer);
    notebook->AddPage(pageOne, wxT("Page 1"));
    
    wxPanel* pageTwo = new wxPanel(notebook, wxID_ANY);
    sizer = new wxFlexGridSizer(2);
    sizer->AddGrowableCol(1);
    flags = wxSizerFlags().Align(wxLEFT).Border(wxRIGHT, 5);
    label = new wxStaticText(pageTwo, wxID_ANY, wxT("Label 1"));
    sizer->Add(label, flags);
    value = new wxSpinCtrlDouble(pageTwo, wxID_ANY, wxT("50.0"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    sizer->Add(value, wxSizerFlags(0).Expand().Border(wxRIGHT, 5));
    pageTwo->SetSizer(sizer);
    notebook->AddPage(pageTwo, wxT("Page 2"));
    
    topLevelSizer->Add(notebook);
    

    will let the spin control expand to take up all the remaining space while matching the border from the first page.