button right align by AddStretchSpacer not work 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
Ronald
I live to help wx-kind
I live to help wx-kind
Posts: 159
Joined: Mon Mar 05, 2018 4:17 am

button right align by AddStretchSpacer not work

Post by Ronald » Mon Sep 16, 2019 9:24 am

Code: Select all

wxBoxSizer * pSizer_0_0 = new wxBoxSizer(wxHORIZONTAL);
pSizer_0_0->AddStretchSpacer();
pSizer_0_0->Add(new wxButton(this, wxID_OK, L"AAA"));
pSizer_0_0->Add(new wxButton(this, wxID_CANCEL, L"BBB"));
With pSizer_0_0->AddStretchSpacer(); or not, the two buttons are always left aligned, what's the problem?

Manolo
Can't get richer than this
Can't get richer than this
Posts: 704
Joined: Mon Apr 30, 2012 11:07 pm

Re: button right align by AddStretchSpacer not work

Post by Manolo » Mon Sep 16, 2019 12:52 pm

Tell the sizer how to align the buttons using some flag of https://docs.wxwidgets.org/trunk/classw ... izer_flags

Code: Select all

pSizer_0_0->Add(new wxButton(this, wxID_OK, L"AAA"), 0, wxALIGN_RIGHT);
pSizer_0_0->Add(new wxButton(this, wxID_CANCEL, L"BBB"),1, wxALIGN_RIGHT);
Notice the "0" I added to the first button means "don't change size of this button". The "1" in the second button, together with the default "1" in the AddStretchSpacer(), means "the remaining space will be evenly distributed (because of the 1/1 relation) between this second button and the spacer".

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2282
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: button right align by AddStretchSpacer not work

Post by evstevemd » Mon Sep 16, 2019 1:16 pm

As @manolo already pointed out, you have not specified ratio so it is taking the button size. Add ratio to define button distribution and EXPAND flag to make them expand and fill the available space.
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

User avatar
doublemax
Moderator
Moderator
Posts: 14314
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: button right align by AddStretchSpacer not work

Post by doublemax » Mon Sep 16, 2019 3:42 pm

Using wxALIGN_RIGHT like Manolo wrote will not work in this case. This is only allowed in a vertical boxsizer and serves a (slightly) different purpose.

Your code looks ok, the problem is mostly how you insert pSizer_0_0 into another sizer. E.g. if you put it into a vertical sizer, set the wxEXPAND flag so that it takes the whole width. And if you put it into a horizontal sizer, use a "proportion" value > 0 so that it can take extra space.

If you can't get it to work, please show the whole sizer related code.
Use the source, Luke!

Ronald
I live to help wx-kind
I live to help wx-kind
Posts: 159
Joined: Mon Mar 05, 2018 4:17 am

Re: button right align by AddStretchSpacer not work

Post by Ronald » Tue Sep 17, 2019 3:13 am

Manolo wrote:
Mon Sep 16, 2019 12:52 pm
Tell the sizer how to align the buttons using some flag of https://docs.wxwidgets.org/trunk/classw ... izer_flags

Code: Select all

pSizer_0_0->Add(new wxButton(this, wxID_OK, L"AAA"), 0, wxALIGN_RIGHT);
pSizer_0_0->Add(new wxButton(this, wxID_CANCEL, L"BBB"),1, wxALIGN_RIGHT);
Notice the "0" I added to the first button means "don't change size of this button". The "1" in the second button, together with the default "1" in the AddStretchSpacer(), means "the remaining space will be evenly distributed (because of the 1/1 relation) between this second button and the spacer".
ASSERTS: "Horizontal alignment flags are ignored in horizontal sizers"
evstevemd wrote:
Mon Sep 16, 2019 1:16 pm
As @manolo already pointed out, you have not specified ratio so it is taking the button size. Add ratio to define button distribution and EXPAND flag to make them expand and fill the available space.
I think the default proportions in wxBoxSizer::AddStretchSpacer and wxBoxSizer::Add in the case are OK.

doublemax wrote:
Mon Sep 16, 2019 3:42 pm
Using wxALIGN_RIGHT like Manolo wrote will not work in this case. This is only allowed in a vertical boxsizer and serves a (slightly) different purpose.

Your code looks ok, the problem is mostly how you insert pSizer_0_0 into another sizer. E.g. if you put it into a vertical sizer, set the wxEXPAND flag so that it takes the whole width. And if you put it into a horizontal sizer, use a "proportion" value > 0 so that it can take extra space.

If you can't get it to work, please show the whole sizer related code.
Buttons should not expand their sizes, just right aligned in horizontal sizer.
When adding to sizer, by deafault, wxBoxSizer::AddStretchSpacer's proportion is 1 and wxBoxSizer::Add's is 0, it seems ok.

Code: Select all

void MyDlg::Init()
{
    wxBoxSizer * pSizer_0 = new wxBoxSizer(wxVERTICAL);

    // add some children to pSizer_0

    {
        wxBoxSizer * pSizer_0_0 = new wxBoxSizer(wxHORIZONTAL);

        pSizer_0_0->AddStretchSpacer();
        pSizer_0_0->Add(new wxButton(this, wxID_OK, L"OK"));
        pSizer_0_0->Add(new wxButton(this, wxID_CANCEL, L"Cancel"));

        pSizer_0->Add(pSizer_0_0);
    }

    SetSizer(pSizer_0);

    Fit();
}
Last edited by Ronald on Tue Sep 17, 2019 3:25 am, edited 1 time in total.

User avatar
doublemax
Moderator
Moderator
Posts: 14314
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: button right align by AddStretchSpacer not work

Post by doublemax » Tue Sep 17, 2019 5:05 am

Your code looks ok, the problem is mostly how you insert pSizer_0_0 into another sizer. E.g. if you put it into a vertical sizer, set the wxEXPAND flag so that it takes the whole width.

Code: Select all

pSizer_0->Add(pSizer_0_0, 0, wxEXPAND);
Use the source, Luke!

Ronald
I live to help wx-kind
I live to help wx-kind
Posts: 159
Joined: Mon Mar 05, 2018 4:17 am

Re: button right align by AddStretchSpacer not work

Post by Ronald » Tue Sep 17, 2019 10:40 am

doublemax wrote:
Tue Sep 17, 2019 5:05 am
Your code looks ok, the problem is mostly how you insert pSizer_0_0 into another sizer. E.g. if you put it into a vertical sizer, set the wxEXPAND flag so that it takes the whole width.

Code: Select all

pSizer_0->Add(pSizer_0_0, 0, wxEXPAND);
Works, the design by wx is fine, I just haven't realized it.
Thanks

Post Reply