How to limit a sizer within a certain space? (wxWidgets)

311 views Asked by At

I'm using a sizer to place the buttons in a grid:

enter image description here

However, I don't want the sizer to use all the space in the window, but just a small area, like the following (in the white area):

enter image description here

Is this possible to achieve? And if yes, how? Please know that I'm VERY new to wxWidgets.

By the way, here's the code behind the frame:

Main::Main() : wxFrame(nullptr, wxID_ANY, "IKE Calculator", wxDefaultPosition, wxSize(500, 650), wxDEFAULT_FRAME_STYLE & ~wxMAXIMIZE_BOX & ~wxRESIZE_BORDER)
{
    int w = 3, h = 3;

    NumBtn = new wxButton*[w*h];
    NumSizer = new wxGridSizer(w, h, 0, 0);

    //Populate sizer
    int cycle = 1;
    for (int x = 0; x < w; x++)
    {
        for (int y = 0; y < h; y++)
        {
            int current = (y * w + x);
            NumBtn[current] = new wxButton(this, BUTTON_NUM + current, std::to_string(cycle));
            NumSizer->Add(NumBtn[current], 1, wxEXPAND | wxALL);

            cycle++;
        }
    }

    this->SetSizer(NumSizer);
    NumSizer->Layout();
}

Main::~Main()
{
    delete[] NumBtn;
}
1

There are 1 answers

9
VZ. On

Sizers are used to position children of a window and they use the entire window client area (i.e. everything inside it) for this. This doesn't mean that you can't position your buttons in the way you want to do it, of course, it just means that you have to do it in the right way:

  1. Either you can create a child window which takes just the area you want it to occupy and then you create your buttons as its children and associate the sizer with this window. This, of course, assumes you can position this window correctly, but windows have SetSize() method, so in principle this could be done. But setting sizers manually is not the right thing to do and you risk running into a common issue with positioning the only child of a window, so it's better to do it in another way.
  2. This other way is to use "spacers", which are sizer elements that take "space", without doing anything else, to occupy the unused parts of the window. For example, in this case you could create a vertical box sizer (wxBoxSizer(wxVERTICAL)) containing a spacer of the given height (the top area) and a horizontal box sizer containing a spacer corresponding to the left gap and your current sizer.

Note that a layout such as what you want to create is rather unusual, typically you don't need to just leave arbitrarily-sized parts of the window empty. So you wouldn't do something like this often, but if you really want to, you can do it, of course.