wxBoxSizer behavior

8 views
Skip to first unread message

Adam

unread,
Nov 17, 2009, 7:48:53 AM11/17/09
to wx-users
The sizing behavior I'm trying to get is...
- A wxWindow that expands as the frame resizes
- Below this, some text aligned on the left side of the frame and some
text aligned on the right side

I would expect to be able to do this with...

wxWindow *expanded_window = ...

wxBoxSizer *hbox(new wxBoxSizer(wxHORIZONTAL, parent));
hbox->Add(new wxStaticText(parent, wxID_ANY, "Left"), 0,
wxALIGN_LEFT);
hbox->Add(new wxStaticText(parent, wxID_ANY, "Right"), 0,
wxALIGN_RIGHT);

wxBoxSizer *vbox(new wxBoxSizer(wxVERTICAL, parent));
vbox->Add(expanded_window, 1, wxEXPAND | wxALL);
vbox->Add(hbox, 0, wxEXPAND | wxLEFT | wxRIGHT);

When I do this though, the "Right" text appears immediately to the
right of the "Left" text. The horizontal box does expand left to
right (I can see this if I use a wxStaticBoxSizer), so it seems like
the wxALIGN_RIGHT is not being honored. If I add the static text's
directly to the vertical box, i.e.

wxBoxSizer *vbox(new wxBoxSizer(wxVERTICAL, parent));
vbox->Add(expanded_window, 1, wxEXPAND | wxALL);
vbox->Add(new wxStaticText(parent, wxID_ANY, "Left"), 0,
wxALIGN_LEFT);
vbox->Add(new wxStaticText(parent, wxID_ANY, "Right"), 0,
wxALIGN_RIGHT);

Then the alignment is honored. Is the alignment behavior in a
horizontal box not what I'm expecting?

Thanks.
adam

Vadim Zeitlin

unread,
Nov 17, 2009, 9:27:57 AM11/17/09
to wx-u...@googlegroups.com
On Tue, 17 Nov 2009 04:48:53 -0800 (PST) Adam <adam.sy...@gmail.com> wrote:

A> The sizing behavior I'm trying to get is...
A> - A wxWindow that expands as the frame resizes
A> - Below this, some text aligned on the left side of the frame and some
A> text aligned on the right side
A>
A> I would expect to be able to do this with...
A>
A> wxWindow *expanded_window = ...
A>
A> wxBoxSizer *hbox(new wxBoxSizer(wxHORIZONTAL, parent));
A> hbox->Add(new wxStaticText(parent, wxID_ANY, "Left"), 0,
A> wxALIGN_LEFT);
A> hbox->Add(new wxStaticText(parent, wxID_ANY, "Right"), 0,
A> wxALIGN_RIGHT);
...
A> Is the alignment behavior in a horizontal box not what I'm expecting?

Indeed, it isn't. wxALIGN_LEFT/RIGHT only work in vertical sizer because
the flags only specify what happens in the "minor" sizer direction (the one
orthogonal to the main, "major", one, which is horizontal for wxHORIZONTAL
sizers and vertical for wxVERTICAL ones). The behaviour in the major
direction is determined solely by the proportion. So to achieve what you
want you'd need to do:

hbox->Add(new wxStaticText(... "left"));
hbox->AddStretchSpacer();
hbox->Add(new wxStaticText(... "right"));


FWIW I regret that sizers API allows you to specify the flags which don't
make sense and also that this isn't detected even during run-time. It's too
late to change the former but it would be great to add at least the
latter...

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Adam

unread,
Nov 17, 2009, 10:26:00 AM11/17/09
to wx-users
VZ,

Thanks - the code you provided did exactly what I wanted. I agree
that it'd be nice if there was a run-time check to help debug issues
like this. I'm sure the documentation explains the wxBoxSizer
behavior as you state above but I must have skimmed over it...

adam

On Nov 17, 9:27 am, Vadim Zeitlin <va...@wxwidgets.org> wrote:
>  application_pgp-signature_part
> < 1KViewDownload
Reply all
Reply to author
Forward
0 new messages