Module: WxSugar::Arranger
- Defined in:
- lib/wx_sugar/layout.rb
Instance Attribute Summary collapse
-
#padding ⇒ Object
Returns the value of attribute padding.
Class Method Summary collapse
Instance Method Summary collapse
-
#arrange(a_sizer, layout = {}) ⇒ Object
Set the main or current sizer of this container window to be
a_sizer
. -
#arrange_grid(layout, &block) ⇒ Object
takes hash arguments
layout
. - #arrange_horizontally(layout = { }, &block) ⇒ Object
- #arrange_linear(layout = { }, &block) ⇒ Object
- #arrange_vertically(layout = { }, &block) ⇒ Object
-
#build_child(child) ⇒ Object
Construct a WxWidget as specified by
child
to add to my arrangement. -
#current_sizer ⇒ Object
Returns the underlying sizer currently being used by this container.
-
#hints_to_constants(layout_hints) ⇒ Object
Convert a hash of layout hints to WxWidgets Sizer constants.
-
#nest(child, layout_hints = {}) {|child| ... } ⇒ Object
Add
child
to the container window’s layout, sizing and placing it according tolayout_hints
.
Instance Attribute Details
#padding ⇒ Object
Returns the value of attribute padding.
60 61 62 |
# File 'lib/wx_sugar/layout.rb', line 60 def padding @padding end |
Class Method Details
.included(klass) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/wx_sugar/layout.rb', line 53 def self.included(klass) unless klass.instance_methods.include?('add') # don't over-write methods in sizer klass.module_eval { alias_method :add, :nest } end end |
Instance Method Details
#arrange(a_sizer, layout = {}) ⇒ Object
Set the main or current sizer of this container window to be a_sizer
. If no block is given, then a_sizer
is used as the main default sizer for this window. Note that this form should only be called once, before any child widgets have been added to the container.
If a block is passed, then the a_sizer
is nested within the container window’s main default sizer. For the duration of the block Widgets added to the container will be added to the nested sizer. If the nested form is called, layout
may contain a layout hint hash. This can contain the key :proportion
, which should specify the integer resizing proportion for this nested sizer within the container’s main arrangement.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/wx_sugar/layout.rb', line 91 def arrange(a_sizer, layout = {}) # run as a subordinate block if block_given? and @current_sizer if layout[:padding] old_padding = @padding @padding = layout[:padding] end superior_sizer, @current_sizer = @current_sizer, a_sizer yield(a_sizer) @current_sizer = superior_sizer proportion = layout[:proportion] || 0 this_padding = padding || 0 superior_sizer.add( a_sizer, proportion, Wx::EXPAND|Wx::ALL|Wx::ADJUST_MINSIZE, this_padding) if layout[:padding] @padding = old_padding end # set as main sizer else if @current_sizer warn "Sizer already specified, redefining layout for #{self}" end @padding = layout[:padding] if layout[:padding] @current_sizer = a_sizer yield(a_sizer) if block_given? self.set_sizer(a_sizer) end end |
#arrange_grid(layout, &block) ⇒ Object
takes hash arguments layout
:rows - integer, number of rows (mandatory, see below)
:cols - integer, number of columns (mandatory, see below)
:vgap - integer, extra vertical space between each child (optional)
:hgap - integer, extra horizontal space between each child (optional)
At least one of :rows
and :cols
must be specified. If one is not specified, the other will be calculated dynamically based on the total number of child widgets added.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/wx_sugar/layout.rb', line 184 def arrange_grid(layout, &block) unless layout[:rows] or layout[:cols] Kernel.raise ArgumentError, "At least one of :rows or :cols must be specified" end if layout[:padding] layout[:vgap] = layout[:hgap] = layout[:padding] end # wxruby wants them in this order, and with nought if null args = [ :rows, :cols, :vgap, :hgap ].map { | arg | layout[arg] or 0 } sizer = Wx::FlexGridSizer.new(*args) arrange( sizer, layout, &block ) end |
#arrange_horizontally(layout = { }, &block) ⇒ Object
147 148 149 |
# File 'lib/wx_sugar/layout.rb', line 147 def arrange_horizontally( layout = { }, &block ) arrange_linear( layout.merge( :direction => :horizontal ), &block ) end |
#arrange_linear(layout = { }, &block) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/wx_sugar/layout.rb', line 151 def arrange_linear( layout = { }, &block) if layout[:direction].to_s.downcase == 'horizontal' direction = Wx::HORIZONTAL elsif layout[:direction].to_s.downcase == 'vertical' direction = Wx::VERTICAL else Kernel.raise ArgumentError, "Unknown direction '#{layout[:direction].inspect}'" end if layout[:label] sb = Wx::StaticBox.new( self, -1, layout[:label] ) sizer = Wx::StaticBoxSizer.new( sb, direction ) elsif layout[:box] sb = Wx::StaticBox.new( self, -1, '' ) sizer = Wx::StaticBoxSizer.new( sb, direction ) else sizer = Wx::BoxSizer.new(direction) end arrange(sizer, layout, &block) end |
#arrange_vertically(layout = { }, &block) ⇒ Object
143 144 145 |
# File 'lib/wx_sugar/layout.rb', line 143 def arrange_vertically( layout = { }, &block ) arrange_linear( layout.merge( :direction => :vertical ), &block ) end |
#build_child(child) ⇒ Object
Construct a WxWidget as specified by child
to add to my arrangement
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/wx_sugar/layout.rb', line 201 def build_child(child) case child when Proc # delayed constructor child = child.call(self) when Class # bare class child = child.new(self) when Wx::Window, Wx::Sizer # ready-to-go widget child = child else Kernel.raise ArgumentError, "Cannot add #{child.inspect} to #{self}" end end |
#current_sizer ⇒ Object
Returns the underlying sizer currently being used by this container. User code should not normally need to call this method.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wx_sugar/layout.rb', line 64 def current_sizer() if @current_sizer return @current_sizer else begin return self.get_sizer rescue RuntimeError end end @current_sizer = Wx::BoxSizer.new(Wx::VERTICAL) self.set_sizer(@current_sizer) @current_sizer end |
#hints_to_constants(layout_hints) ⇒ Object
Convert a hash of layout hints to WxWidgets Sizer constants
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/wx_sugar/layout.rb', line 217 def hints_to_constants(layout_hints) if layout_hints[:pad] layout = layout_hints[:pad].split(',').inject(0) do | l, edge | l | Wx::const_get(edge.upcase) end else layout = Wx::ALL end if layout_hints[:minsize] layout = layout | Wx::ADJUST_MINSIZE|Wx::EXPAND end if layout_hints[:proportion] layout = layout | Wx::EXPAND end if align = layout_hints[:align] begin align_const = Wx::const_get('ALIGN_' << align.to_s.upcase) layout = layout | align_const rescue NameError Kernel.raise ArgumentError, "Invalid align argument #{layout_hints[:align]}" end end layout end |
#nest(child, layout_hints = {}) {|child| ... } ⇒ Object
Add child
to the container window’s layout, sizing and placing it according to layout_hints
Layout hints may contain the keys
:proportion
:minsize
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/wx_sugar/layout.rb', line 129 def nest(child, layout_hints = {}) child = build_child(child) layout = hints_to_constants(layout_hints) proportion = layout_hints[:proportion] || 0 siz = self.current_sizer padding = layout_hints[:padding] || @padding siz.add(child, proportion, layout, padding || 0) siz.layout() yield child if block_given? return child end |