Page 1 of 1

button right align by AddStretchSpacer not work

Posted: Mon Sep 16, 2019 9:24 am
by Ronald

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?

Re: button right align by AddStretchSpacer not work

Posted: Mon Sep 16, 2019 12:52 pm
by Manolo
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".

Re: button right align by AddStretchSpacer not work

Posted: Mon Sep 16, 2019 1:16 pm
by evstevemd
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.

Re: button right align by AddStretchSpacer not work

Posted: Mon Sep 16, 2019 3:42 pm
by doublemax
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.

Re: button right align by AddStretchSpacer not work

Posted: Tue Sep 17, 2019 3:13 am
by Ronald
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();
}

Re: button right align by AddStretchSpacer not work

Posted: Tue Sep 17, 2019 5:05 am
by doublemax
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);

Re: button right align by AddStretchSpacer not work

Posted: Tue Sep 17, 2019 10:40 am
by Ronald
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