Class: EideticRML::LayoutManagers::VBoxLayout

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/erml_layout_managers.rb', line 267

def layout(container, writer)
  # $stderr.puts "layout container: #{container.tag}"
  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 }
  headers, unaligned = static.partition { |widget| widget.align == :top }
  footers, unaligned = unaligned.partition { |widget| widget.align == :bottom }
  static.each do |widget|
    widget.before_layout
    # puts "<1> vbox widget width: #{widget.width} #{widget.path}"
    widget.width([widget.preferred_width(writer) || container.content_width, container.content_width].min, :pt) if widget.width.nil?
    # puts "<2> vbox widget width: #{widget.width} #{widget.path}"
    widget.left(container.content_left, :pt)
  end
  top, dy = container.content_top, 0
  bottom = container.content_top + container.max_content_height

  headers.each_with_index do |widget, index|
    widget.top(top, :pt)
    widget.layout_widget(writer)                                              # swapped
    widget.height(widget.preferred_height(writer), :pt) if widget.height.nil? # swapped
    top += (widget.height + @style.vpadding)
    dy += widget.height + ((index > 0) ? @style.vpadding : 0)
  end
  headers.each { |widget| widget.visible = (widget.bottom <= bottom) } # or first static widget?

  unless footers.empty?
    container.height('100%') if container.height.nil?
    footers.reverse.each do |widget|
      widget.bottom(bottom, :pt)
      widget.layout_widget(writer)                                              # swapped
      widget.height(widget.preferred_height(writer), :pt) if widget.height.nil? # swapped
      bottom -= (widget.height + @style.vpadding)
    end
  end
  footers.each { |widget| widget.visible = (widget.top >= top) } # or first static widget?

  widgets_visible = 0
  unaligned.each_with_index do |widget, index|
    widget.visible = !container_full
    next if container_full
    widget.top(top, :pt)
    # puts "<1> vbox widget height: #{widget.height} #{widget.path}"
    widget.layout_widget(writer)                                              # swapped
    # puts "<2> vbox widget height: #{widget.height} #{widget.path}"
    widget.height(widget.preferred_height(writer), :pt) if widget.height.nil? # swapped
    # puts "<3> vbox widget height: #{widget.height} #{widget.path}"
    top += widget.height
    dy += widget.height + (index > 0 ? @style.vpadding : 0) #if widget.visible
    if top > bottom
      container_full = true
      widget.visible = (widgets_visible == 0)
      # widget.visible = widget.leaves > 0 and container.root_page.positioned_widgets[:static] == 0
      # $stderr.puts "+++vbox+++ #{container.root_page.positioned_widgets[:static]}, tag: #{widget.tag}, visible: #{widget.visible}"
    end
    widgets_visible += 1 if widget.visible
    top += @style.vpadding
  end
  # set_height = container.height.nil?
  # container.height(container.max_height_avail, :pt) if set_height
  # unaligned.each_with_index do |widget, index|
  #   # widget.visible = (widget.bottom <= bottom) || (index == 0) #|| (container.overflow && widget.top < bottom)
  #   widget.visible = (widget.bottom <= bottom) || (container.root_page.positioned_widgets[:static] == 0) #|| (container.overflow && widget.top < bottom)
  #   # if widget.visible and widget.bottom > bottom and container.overflow
  #   #   widget.layout_widget(writer)
  #   # end
  # end

  container_full = unaligned.last && !unaligned.last.visible
  container.more(true) if container_full and container.overflow
  # container.height(top - container.content_top + @style.vpadding, :pt) if container.height.nil?
  # container.height(dy + container.non_content_height, :pt) if container.height.nil?
  super(container, writer)
end

#preferred_height(grid, writer) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/erml_layout_managers.rb', line 343

def preferred_height(grid, writer)
  cells = grid.col(0)
  return 0 if cells.empty?
  cell_heights = cells.map { |w| w.preferred_height(writer) }
  return nil unless cell_heights.all?
  cell_heights.inject((cells.size - 1) * @style.vpadding) { |sum, height| sum + height }
end

#preferred_width(grid, writer) ⇒ Object



351
352
353
354
355
356
357
# File 'lib/erml_layout_managers.rb', line 351

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