Usage of BoxSizer proportion Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Usage of BoxSizer proportion

Post by cookielau »

Hi all,

I think I misunderstand or misuse the proportion parameter in the wxBoxSizer, from my understanding, if two panels are added into a horizontal sizer with proportion argument 2 and 3, their width should be fixed at 2:3 when resizing the window. But in fact, I see the first panel size is fixed regardless of the whole size. I'm not sure whether I need to add some flags to achieve this function like wxEXPAND or wxGROW, etc.

Besides, a more strange behavior is when Grid is added into the sizer. e.g. I want to create a ui like this:
Capture3.PNG
Capture3.PNG (16.04 KiB) Viewed 309 times
So I write my code like this:

Code: Select all

#include <wx/wx.h>
#include <wx/grid.h>
#include <wx/srchctrl.h>

class MyFrame : public wxFrame
{
public:
	MyFrame(wxWindow* parent = nullptr) : wxFrame(parent, wxID_ANY, "Test", wxDefaultPosition, wxSize(1200, 900))
	{
		wxPanel* search_panel = new wxPanel(this);
		wxBoxSizer* col_sizer = new wxBoxSizer(wxVERTICAL);

		wxSearchCtrl* search_ctrl = new wxSearchCtrl(search_panel, wxID_ANY, "Search");
		col_sizer->Add(search_ctrl, 0, wxEXPAND | wxALL, 4);

		wxGrid* product_grid = new wxGrid(search_panel, wxID_ANY);
		product_grid->CreateGrid(100, 5);
		col_sizer->Add(product_grid, 0);

		search_panel->SetSizer(col_sizer);
		col_sizer->SetSizeHints(search_panel);

		wxPanel* blank_panel = new wxPanel(this);

		wxBoxSizer* row_sizer = new wxBoxSizer(wxHORIZONTAL);
		row_sizer->Add(search_panel, 2, wxALL, 1);
		row_sizer->Add(blank_panel, 3, wxALL, 1);

		wxGrid* bottom_grid = new wxGrid(this, wxID_ANY);
		bottom_grid->CreateGrid(100, 100);
		wxBoxSizer* top_col_sizer = new wxBoxSizer(wxVERTICAL);
		top_col_sizer->Add(row_sizer, 2, wxALL, 1);
		top_col_sizer->Add(bottom_grid, 3, wxALL, 1);
		SetSizerAndFit(top_col_sizer);
		Centre();
	}
private:
	wxGrid* grid_;
};

class MyApp : public wxApp
{
public:
	bool OnInit() override
	{
		(new MyFrame())->Show();
		return true;
	}
}; wxIMPLEMENT_APP(MyApp);
But the fact is, the left side grid would occupy the whole window, and until I make the window long enough I can see the grid at the bottom. Does anyone know the reason behind?

Thanks in advance.

Besides, I also find the auimanager seemingly powerful and enable the pane inside it to be resized, is there any examples beside the auidemo in samples?
Windows 10, wxWidgits v3.2.2.1
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Re: Usage of BoxSizer proportion

Post by cookielau »

After deeper study, I now know several things more:
1. the proportion is to describe how the size will be changed when the window is resized, 0 means the item keeps the same as before, the positive number means the proportion distributed to each item to the change made.
2. that is to say, I need to first initialize a proper size for each of my components and set their proportion to 2:3 to make them still keeps the same ratio when I changed the window
3. Splitter would also be helpful in my program.
4. The wxAuiManager in sample/ directory is powerful enough.

I think I will try by myself first now. Thanks
Windows 10, wxWidgits v3.2.2.1
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Usage of BoxSizer proportion

Post by doublemax »

cookielau wrote: Thu Jun 08, 2023 6:55 am 1. the proportion is to describe how the size will be changed when the window is resized, 0 means the item keeps the same as before, the positive number means the proportion distributed to each item to the change made.
To be exact: The proportion determines how any additional space after minimum size calculation is distributed.
2. that is to say, I need to first initialize a proper size for each of my components and set their proportion to 2:3 to make them still keeps the same ratio when I changed the window
Good that you already figured that out by yourself :)
cookielau wrote: Thu Jun 08, 2023 6:55 am 4. The wxAuiManager in sample/ directory is powerful enough.
Be careful with wxAUI. Basically it's good if you really need panels that can be floated and docked, but i wouldn't use it just as a replacement for sizers.

An additional issue with wxGrid is: Their reported "best size" is the size needed to display its whole content (= all cells). But this will also be solved by #2
Use the source, Luke!
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Re: Usage of BoxSizer proportion

Post by cookielau »

doublemax wrote: Thu Jun 08, 2023 7:29 am
cookielau wrote: Thu Jun 08, 2023 6:55 am 1. the proportion is to describe how the size will be changed when the window is resized, 0 means the item keeps the same as before, the positive number means the proportion distributed to each item to the change made.
To be exact: The proportion determines how any additional space after minimum size calculation is distributed.
2. that is to say, I need to first initialize a proper size for each of my components and set their proportion to 2:3 to make them still keeps the same ratio when I changed the window
Good that you already figured that out by yourself :)
cookielau wrote: Thu Jun 08, 2023 6:55 am 4. The wxAuiManager in sample/ directory is powerful enough.
Be careful with wxAUI. Basically it's good if you really need panels that can be floated and docked, but i wouldn't use it just as a replacement for sizers.

An additional issue with wxGrid is: Their reported "best size" is the size needed to display its whole content (= all cells). But this will also be solved by #2

Thanks for your prompt reply, doublemax!
Windows 10, wxWidgits v3.2.2.1
Post Reply