Yurii Komarnytskyi
Yurii Komarnytskyi

Reputation: 173

Spacer added to inner wxBoxSizer doesn't work in wxWidgets

I have an issue with spacer inside of the inner wxBoxSizer. Here is the sample code.

class Frame : public wxFrame
{
public:
    Frame()
        : wxFrame(nullptr,
                  wxID_ANY,
                  wxEmptyString,
                  wxDefaultPosition,
                  wxSize(600, 200))
    {
        wxPanel* panel = new wxPanel(this);

        // Some OUTER SIZER
        wxBoxSizer* mainS = new wxBoxSizer(wxVERTICAL);

        // INNER HORIZONTAL SIZER
        wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);

        wxStaticText* text = new wxStaticText(panel, wxID_ANY, wxT("Some Text"));        
        sizer->Add(text, 0, wxALL, 5);

        wxComboBox* comboBox = new wxComboBox(panel, wxID_ANY, wxT("Combo"));
        sizer->Add(comboBox, 0, wxALL, 5);

        // SPACER
        sizer->Add(0, 0, 1, wxEXPAND, 5);

        wxButton* button = new wxButton(panel, wxID_ANY, wxT("Some button"));
        sizer->Add(button, 0, wxALL, 5);
        mainS->Add(sizer, 0, wxALL, 5); 

        panel->SetSizer(mainS);

        // PANEL SIZER
        wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
        panelSizer->Add(panel, 1, wxEXPAND, 5);

        SetSizer(panelSizer);
        Layout();
        Centre(wxBOTH);
    }
};

class WxguiApp
    : public wxApp
{
public:
    bool OnInit() override
    {
        Frame* w = new Frame();
        w->Show();
        SetTopWindow(w);

        return true;
    }
};

IMPLEMENT_APP(WxguiApp);

If I remove outer sizer spacer starts working fine. Is it bug in my code? Or maybe some issue with wxWidgets?

I couldn't find answer, maybe someone help? wxWidgets 3.0.2

Upvotes: 0

Views: 739

Answers (1)

VZ.
VZ.

Reputation: 22688

Your inner sizer (sizer) won't expand in the horizontal direction because you didn't tell its parent sizer (mainS) to do it when adding it, so your spacer will always be 0 pixels wide, which is its initial and minimal width.

To fix this, you just need to change the mainS->Add(sizer, 0, wxALL, 5) line to use wxALL | wxEXPAND.

Additionally, I strongly encourage you to look at using wxSizerFlags instead of the cryptic syntax shown above. And, last and really least, but still worth mentioning, prefer using much more readable AddStretchSpacer() to the confusing Add(0, 0, 1, wxEXPAND, 5) call (it's confusing because expanding your spacer doesn't do anything and border size is not taken into account without any border flags).

Upvotes: 3

Related Questions