Skip to content
Search

WxSizerFlags

lastmod 2018-08-17created 2015-05-047 edits

Container for sizer items flags providing readable names for them.

Alignment

When adding a wxWindow with Add(), to set how it is aligned, pass wxAlignment-related constants ORed together in the flag argument.

The flag values are as follows.

  • wxALIGN_LEFT
  • wxALIGN_RIGHT
  • wxALIGN_TOP
  • wxALIGN_BOTTOM
  • wxALIGN_CENTER_VERTICAL
  • wxALIGN_CENTER_HORIZONTAL
  • wxALIGN_CENTER

The important point is that the flag values above apply only to alignment. Note that they are unrelated to Stretch-related constants such as wxEXPAND.

ALIGN CENTER

There is a caveat with center alignment. First, look at the following code:

wxBoxSizer * sizer  = new (std::nothrow) wxBoxSizer(wxVERTICAL);
wxButton * btn = new (std::nothrow) wxButton(this, wxID_ANY, wxT("BUTTON"));
sizer->Add(btn, 0, wxALIGN_CENTER_VERTICAL);

Used like the code above, vertical alignment is not applied correctly. The reason is that the Orientation of the wxBoxSizer is likewise VERTICAL.

When applying wxALIGN_CENTER-related alignment in a wxBoxSizer, you must use the alignment opposite to its Orientation.

That is, in the code above, you should make the Orientation of the wxBoxSizer wxHORIZONTAL.

Also, the wxEXPAND attribute must not be present.

Border

When adding a wxWindow with Add(), to choose where the BORDER is applied, pass wxDirection-related constants ORed together in the flag argument.

The flag values are as follows.

  • wxLEFT
  • wxRIGHT
  • wxBOTTOM
  • wxTOP
  • wxALL

The important point is that the flag values above apply only to the BORDER. Note that they are unrelated to Stretch-related constants such as wxEXPAND.

For Border styles, see wxBorder.

Stretch

Use wxStretch-related constants.

The flag values are as follows.

  • wxEXPAND: Expands to fill the allocated space.
  • wxSHAPED: Expands while keeping the aspect ratio.

Empty space

To add empty space between windows, use the following.

wxBoxSizer * sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->Add(_ok);
sizer->Add(5, 0); // Empty 5px (width) size.
sizer->Add(_cancel);
panel->SetSizer(sizer);

Detailed Description

Normally, when you add an item to a sizer via wxSizer::Add, you have to specify a lot of flags and parameters which can be unwieldy. This is where wxSizerFlags comes in: it allows you to specify all parameters using the named methods instead. For example, instead of

sizer->Add(ctrl, 0, wxEXPAND | wxALL, 10);

you can now write

sizer->Add(ctrl, wxSizerFlags().Expand().Border(wxALL, 10));

Favorite site