HowTo : WrapSizer - mcorino/wxRuby3 GitHub Wiki

In this tutorial, we will discuss the wxRuby WrapSizer Layout Sizer.
The BoxSizer is one of the sizers in wxRuby designed to help with the layout management of widgets in the window. The WrapSizer is a variant of the BoxSizer, with a single but rather significant difference.
A WrapSizer lays out its items in a sequence in a single direction (horizontal or vertical), like a BoxSizer as long as there is space available in that direction. Once all available space in the primary direction has been used however, a new sequence is added and items are added there. So a WrapSizer has a primary orientation for adding items, and adds sequences (rows or columns) as needed in the secondary direction.
In this example, we’ll demonstrate a simple use of the WrapSizer layout in wxRuby.
We create the WrapSizer, which takes a mandatory orientation argument and an optional flag argument. The orientation
is either Wx::VERTICAL or Wx::HORIZONTAL (just like the BoxSizer). The optional second argument is a flag value
(Wx::WRAPSIZER_DEFAULT_FLAGS
) for which we pass in 0 in this case
(see Wx::WrapSizer#initialize for more details on this flag).
sizer = Wx::WrapSizer.new(Wx::HORIZONTAL, 0) # or Wx::HWrapSizer.new(0)
Note: wxRuby provides two simple but useful shortcut derivatives that will make code easier to write and read: Wx::HWrapSizer and Wx::VWrapSizer
Now we add in a bunch of widgets.
15.times do |i|
sizer.add(Wx::CheckBox.new(@panel, label: "Option #{'%.2i' % (i+1)}"), 0, Wx::ALL, 12)
end
Finally, we set the WrapSizer to the panel.
@panel.sizer = sizer
The complete code and output is as follows.
require 'wx'
class MyWindow < Wx::Frame
def initialize(title)
super(nil, title: title)
@panel = Wx::Panel.new(self)
sizer = Wx::WrapSizer.new(Wx::HORIZONTAL, 0) # or Wx::HWrapSizer.new(0)
15.times do |i|
sizer.add(Wx::CheckBox.new(@panel, label: "Option #{'%.2i' % (i+1)}"), 0, Wx::ALL, 12)
end
@panel.sizer = sizer
centre
end
end
Wx::App.run do
window = MyWindow.new("wxRuby WrapSizer Guide")
window.show
end
The output:
There are many other Sizers in wxRuby that you can use instead of WrapSizer. In fact, the best layouts are often the result of combining several different types of Sizers. This can be done by simply nesting them into each other. There is no limitation to this ability.
- BoxSizer
- StaticBoxSizer (another variant of BoxSizer)
- GridSizer
- FlexGridSizer (a variant of GridSizer)
- GridBagSizer (a variant of FlexGridSizer)
You can learn more about these sizers by reading the guide on layout managers in wxRuby.