Search code examples
c++wxwidgets

C++ wxWidgets Sizer issue (beginner)


I'm new to wxWidgets and sizers. I've created the following structure inside of a frame constructor

wxBoxSizer *frameSizer = new wxBoxSizer(wxHORIZONTAL);

wxBoxSizer *deckListLeftSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *deckListRightSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *logOutputSizer = new wxBoxSizer(wxVERTICAL);

// 10 inputs on left
for ( int i = 0; i < 10; i++ )
{
    wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1));
    deckInputCastables.push_back(textCtrl);
    deckListLeftSizer->Add(textCtrl, wxEXPAND );
}

// 10 inputs on right
for ( int i = 0; i < 10; i++ )
{
    wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(-1, -1));
    deckInputLands.push_back(textCtrl);
    deckListRightSizer->Add(textCtrl, wxEXPAND );
}

logOutput = new wxStaticText(this, wxID_ANY, "Output Terminal", wxPoint(-1, -1), wxSize(-1, -1));
logOutput->SetBackgroundColour(*wxRED);
logOutputSizer->Add(logOutput);

frameSizer->Add(deckListLeftSizer, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5);
frameSizer->Add(deckListRightSizer, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5);
frameSizer->Add(logOutputSizer, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5);

frameSizer->Fit(this);
SetSizer(frameSizer);

This is what it looks like:

enter image description here

What I would like is the blue StaticText to fill its entire sizer, so it takes up the right 1/3 of the window, stretching vertically and horizontally. Also, each TextCtrl has stretched Vertically to fill its sizer, but not horizontally. I don't want any unused space when I adjust the frame size, so each textctrl expands vertically and horizontally. What am I doing wrong?

Thanks


Solution

  • @GeoffL,

    First - there is no blue Static Text only the Red one.

    Second - wxALIGN_* options as you can see work only in the major direction, i.e. the one you pass to the wxBoxSizer() constructor.

    What you probably want is (untested):

    logOutput = new wxStaticText(this, wxID_ANY, "Output Terminal", wxPoint(-1, -1), wxSize(-1, -1));
    logOutput->SetBackgroundColour(*wxRED);
    logOutputSizer->Add(logOutput, 0, wxEXPAND | wxALIGN_CENTER_VERTICAL, 0 );
    

    wxEXPAND | wxALIGN_CENTER_VERTICAL will expand the sizer and center the control vertically.

    You also omit wxALL option everywhere as it is not useful with the wxALIGN options present.

    You can also omit wxPoint() and wxSize() parameters in control constructor as (-1, -1) are default values