HowTo : StaticBoxSizer - mcorino/wxRuby3 GitHub Wiki

In this tutorial we will discuss the wxRuby StaticBoxSizer Layout Sizer.
The BoxSizer is one of the sizers in wxRuby designed to help with the layout management of widgets in the window. The StaticBoxSizer is just a variant of the BoxSizer, with a single difference.
As the name implies, this Sizer is a combination of the BoxSizer and the StaticBox widget. A StaticBox can be provided when creating the Sizer or the Sizer can create the StaticBox itself when created.
In this example, we’ll demonstrate a simple use of the StaticBoxSizer layout in wxRuby.
First, we create a BoxSizer to act as a wrapper around the StaticBoxSizer. The idea is to use the BoxSizer to create some padding between the widgets in the StaticBoxSizer and the edges of the window. (Try it both ways, with and without the wrapper).
wrapper = Wx::VBoxSizer.new
Next, we create the StaticBoxSizer, which takes two arguments and an optional third. The first is an orientation, which
is either Wx::VERTICAL or Wx::HORIZONTAL (just like the BoxSizer). Secondly, we pass in the parent for the StaticBox
widget that the StaticBoxSizer will create for us. The optional third argument is a label text for the StaticBox (empty
by default) for which we pass in 'Box'
in this case.
sizer = Wx::StaticBoxSizer.new(Wx::VERTICAL, @panel, 'Box')
Alternatively, we could have created the StaticBox ourselves first and passed that in for the Sizer creation.
box = Wx::StaticBox.new(@panel, 'Box')
sizer = Wx::StaticBoxSizer.new(box, Wx::VERTICAL)
Now we add in a bunch of widgets.
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 1'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 2'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 3'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
Note: Notice how we retrieve the StaticBox widget from the StaticBoxSizer to pass in as parent for the Button widgets? Creating widgets inside a StaticBox as children of that StaticBox is the recommended way for current wxRuby releases.
Finally, we add the StaticBoxSizer into the BoxSizer and set the BoxSizer to the panel.
wrapper.add(sizer, flag: Wx::ALL, border: 5)
@panel.set_sizer(wrapper)
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)
wrapper = Wx::VBoxSizer.new
sizer = Wx::StaticBoxSizer.new(Wx::VERTICAL, @panel, 'Box')
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 1'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 2'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
sizer.add(Wx::Button.new(sizer.static_box, label: 'Button 3'), flag: Wx::BOTTOM|Wx::LEFT|Wx::RIGHT, border: 5)
wrapper.add(sizer, flag: Wx::ALL, border: 5)
@panel.set_sizer(wrapper)
centre
end
end
Wx::App.run do
window = MyWindow.new("wxRuby StaticBoxSizer Guide")
window.show
end
The output:
There are many other Sizers in wxRuby that you can use instead of StaticBoxSizer. 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
- WrapSizer (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.