Class: EideticRML::LayoutManagers::HBoxLayout

Inherits:
LayoutManager show all
Defined in:
lib/erml_layout_managers.rb

Instance Method Summary collapse

Methods inherited from LayoutManager

#after_layout, #col_grid, for_name, #initialize, #layout_absolute, #layout_relative, register, #row_grid

Constructor Details

This class inherits a constructor from EideticRML::LayoutManagers::LayoutManager

Instance Method Details

#layout(container, writer) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
# File 'lib/erml_layout_managers.rb', line 166

def layout(container, writer)
  container_full = false
  widgets, remaining = printable_widgets(container, :static)
  remaining.each { |widget| widget.visible = false if widget.printed }
  static, relative = widgets.partition { |widget| widget.position == :static }
  lpanels, unaligned = static.partition { |widget| widget.align == :left }
  rpanels, unaligned = unaligned.partition { |widget| widget.align == :right }
  percents, others = static.partition { |widget| widget.width_pct }
  specified, others = others.partition { |widget| widget.width }

  width_avail = container.content_width

  # allocate specified widths first
  specified.each do |widget|
    width_avail -= widget.width
    container_full = width_avail < 0
    widget.disabled = container_full
    width_avail -= @style.hpadding
  end

  # allocate percent widths next, with a minimum width of 1 point
  if width_avail - (percents.size - 1) * @style.hpadding >= percents.size
    width_avail -= (percents.size - 1) * @style.hpadding
    total_percents = percents.inject(0) { |total, widget| total + widget.width }
    ratio = width_avail.quo(total_percents)
    percents.each do |widget|
      widget.width(widget.width * ratio, :pt) if ratio < 1.0
      width_avail -= widget.width
    end
  else
    container_full = true
    percents.each { |widget| widget.disabled = true }
  end
  width_avail -= @style.hpadding

  # divide remaining width equally among widgets with unspecified widths
  if width_avail - (others.size - 1) * @style.hpadding >= others.size
    width_avail -= (others.size - 1) * @style.hpadding
    others_width = width_avail.quo(others.size)
    others.each { |widget| widget.width(others_width, :pt) }
  else
    container_full = true
    others.each { |widget| widget.disabled = true }
  end

  static.each do |widget|
    if container.align == :bottom
      widget.bottom(container.content_bottom, :pt)
    else
      container_full = true
      widget.top(container.content_top, :pt)
    end
    widget.height(widget.preferred_height(writer), :pt) if widget.height.nil?
  end
  left = container.content_left
  right = container.content_right
  lpanels.each do |widget|
    next if widget.disabled
    widget.left(left, :pt)
    left += (widget.width + @style.hpadding)
  end
  rpanels.reverse.each do |widget|
    next if widget.disabled
    widget.right(right, :pt)
    right -= (widget.width + @style.hpadding)
  end
  unaligned.each do |widget|
    next if widget.disabled
    widget.left(left, :pt)
    left += (widget.width + @style.hpadding)
  end
  if container.height.nil?
    content_height = static.map { |widget| widget.height }.max || 0
    container.height(content_height + container.non_content_height, :pt)
  end
  static.each { |widget| widget.layout_widget(writer) if widget.visible and !widget.disabled }
  super(container, writer)
end

#preferred_height(grid, writer) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/erml_layout_managers.rb', line 245

def preferred_height(grid, writer)
  cells = grid.row(0)
  return 0 if cells.empty?
  cell_heights = cells.map { |w| w.preferred_height(writer) }
  return nil unless cell_heights.all?
  cell_heights.max
end

#preferred_width(grid, writer) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/erml_layout_managers.rb', line 253

def preferred_width(grid, writer)
  cells = grid.row(0)
  return 0 if cells.empty?
  cell_widths = cells.map { |w| w.preferred_width(writer) }
  return nil unless cell_widths.all?
  cell_widths.inject((cells.size - 1) * @style.hpadding) { |sum, width| sum + width }
end