wxSizer align controls left-end and right-end.

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
rajan_m
Knows some wx things
Knows some wx things
Posts: 39
Joined: Tue Jan 20, 2009 10:37 am
Location: chennai

wxSizer align controls left-end and right-end.

Post 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);
Attachments
shrink issue
shrink issue
expand_layout.jpg (60.31 KiB) Viewed 4273 times
Every exit is an entry somewhere else!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post 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.
Use the source, Luke!
rajan_m
Knows some wx things
Knows some wx things
Posts: 39
Joined: Tue Jan 20, 2009 10:37 am
Location: chennai

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

Post 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.
Attachments
shrink without overlap
shrink without overlap
shrink.jpg (11.28 KiB) Viewed 4255 times
Every exit is an entry somewhere else!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post 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.
Use the source, Luke!
rajan_m
Knows some wx things
Knows some wx things
Posts: 39
Joined: Tue Jan 20, 2009 10:37 am
Location: chennai

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

Post 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.
Last edited by rajan_m on Tue Feb 09, 2016 2:23 pm, edited 1 time in total.
Every exit is an entry somewhere else!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

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

Post 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.
rajan_m
Knows some wx things
Knows some wx things
Posts: 39
Joined: Tue Jan 20, 2009 10:37 am
Location: chennai

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

Post 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.
Every exit is an entry somewhere else!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

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

Post 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.
Post Reply