Page 1 of 1

wxSizer align controls left-end and right-end.

Posted: Mon Feb 08, 2016 4:39 am
by rajan_m
Hi,

I want to align two buttons one at the left and the other to the right. I'm using horizontal sizer to align buttons left and right with StretchSpacer which works properly on expansion but overlaps on shrinking.

Code: Select all

 
   wxPanel* pnl = new wxPanel(this);
	
	wxButton* left_button = new wxButton(pnl,wxID_ANY,"left button");
	wxButton* right_button = new wxButton(pnl,wxID_ANY,"rignt button");

	wxSizer* hsizer = new wxBoxSizer(wxHORIZONTAL);

	hsizer->Add(left_button);
	hsizer->AddStretchSpacer();
	hsizer->Add(right_button);

	pnl->SetSizer(hsizer);
	pnl->SetAutoLayout(true);

	wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);

	sizer->Add(pnl,1,wxEXPAND|wxALL,10);

	SetSizer(sizer);
	SetAutoLayout(true);

Re: wxSizer align controls left-end and right-end.

Posted: Mon Feb 08, 2016 9:43 am
by doublemax
The overlapping happens because there is not enough room for the controls. The only way to avoid this is to set a proper minimum size for the outer window.

wxSizer::SetSizeHints() does that. Alternatively you can call wxWindow::SetSizerAndFit instead of wxWindow::SetSizer to set the sizer.

Re: wxSizer align controls left-end and right-end.

Posted: Mon Feb 08, 2016 11:52 am
by rajan_m
doublemax wrote: Alternatively you can call wxWindow::SetSizerAndFit instead of wxWindow::SetSizer to set the sizer.
I've tried SetSizerAndFit but unable to shrink the dialog beyond the content size.

User should be able to shrink the dialog completely without overlapping of controls as shown below.

Re: wxSizer align controls left-end and right-end.

Posted: Mon Feb 08, 2016 12:40 pm
by doublemax
User should be able to shrink the dialog completely without overlapping of controls as shown below.
There is no way to control what a sizer does when there is not enough room to show all controls.

Re: wxSizer align controls left-end and right-end.

Posted: Mon Feb 08, 2016 3:42 pm
by rajan_m
doublemax wrote:There is no way to control what a sizer does when there is not enough room to show all controls.
Shrinking frame doesn't have to shrink the controls as shown in the above image <shrink without overlap>. it can cut the view which is the behaviour if we don't set min size.

Update: Sorry for being not clear. What I meant is, without StretchSpacer(in the code I've posted above) buttons wont move on expand but at the same time wont overlap on shrinking the frame but view will be cut which I believe is the default behaviour.

Re: wxSizer align controls left-end and right-end.

Posted: Tue Feb 09, 2016 3:49 am
by Manolo
What is supposed to happen when a window is resized smaller than a minimum value is a design decision,
Personally I don't like to hide any control, nor behind another control neither outside the window. So I agree with using SetMinSize().

Anyhow, you can handle size-event and if it comes to a special size, change sizers: detach, re-attach controls with other flags.

Re: wxSizer align controls left-end and right-end.

Posted: Tue Feb 09, 2016 2:41 pm
by rajan_m
Manolo wrote:What is supposed to happen when a window is resized smaller than a minimum value is a design decision
I believe the default behaviour(not my opinion) is, when we shrink the frame with controls beyond the min size it'll cut the view. In my case when frame expands it should align the controls to left and right and when we shrink the frame beyond min size it can cut the view.

Re: wxSizer align controls left-end and right-end.

Posted: Wed Feb 10, 2016 1:52 am
by Manolo
Sizers in your example are working as you decided: the left button is letf-aligned with the window and the right botton with the right border of the window (spacers apart). It happens that the only way of fulfilling those settings is overlapping buttons.