Class: FlowLayout

Inherits:
AbstractLayout show all
Defined in:
lib/canis/core/include/layouts/flowlayout.rb

Instance Attribute Summary

Attributes inherited from AbstractLayout

#bottom_margin, #components, #form, #gap, #height, #height_pc, #ignore_list, #left_margin, #right_margin, #top_margin, #width, #width_pc

Instance Method Summary collapse

Methods inherited from AbstractLayout

#_init_layout, #add, #add_with_weight, #cget, #clear, #configure_item, #cset, #push, #remove

Constructor Details

#initialize(arg, config = {}, &block) ⇒ FlowLayout

Returns a new instance of FlowLayout.

Parameters:

  • optional (Form)

    give a form

  • optional (Hash)

    give settings/attributes which will be set into variables



24
25
26
# File 'lib/canis/core/include/layouts/flowlayout.rb', line 24

def initialize arg, config={}, &block
  super
end

Instance Method Details

#do_layoutObject

This program lays out the widgets deciding their row and columm and height and weight. This program is called once at start of application, and again whenever a RESIZE event happens.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/canis/core/include/layouts/flowlayout.rb', line 31

def do_layout
  _init_layout
  r = @top_margin
  c = @left_margin
  #
  # determine fixed widths and how much is left to share with others,
  # and how many variable width components there are.
  ht = 0   # accumulate fixed height
  fixed_ctr = 0 # how many items have a fixed wt
  var_ctr = 0
  var_wt = 0.0
  @components.each do |e|
    $log.debug "  looping 1 #{e.name} "
    _tmpwt = cget(e, :weight) || 0
    # what of field and button placed side by side
    if e.is_a? Field or e.is_a? Button or e.is_a? Label
      # what to do here ?
      @wts[e] ||= 1
      ht += @wts[e] || 1
      fixed_ctr += 1
    elsif _tmpwt >= 1
        ht += _tmpwt || 0
        fixed_ctr += 1
    elsif _tmpwt > 0 and _tmpwt <= 1
      # FIXME how to specify 100 % ???
      var_ctr += 1
      var_wt += _tmpwt
    end
  end
  unaccounted = @components.count - (fixed_ctr + var_ctr)
  $log.debug "  unacc #{unaccounted} , fixed #{fixed_ctr} , var : #{var_ctr} , ht #{ht} height #{@height}  "
  balance_ht = @width - ht # use this for those who have specified a %
  balance_ht1 = balance_ht * (1 - var_wt )
  average_ht = (balance_ht1 / unaccounted).floor # give this to those who have not specified ht
  average_ht = (balance_ht1 / unaccounted) # give this to those who have not specified ht
  $log.debug "  #{balance_ht} , #{balance_ht1} , #{average_ht} "
  # not accounted for gap in heights
  rem = 0 # remainder to be carried over
  @components.each do |e|
    $log.debug "  looping 2 #{e.name} #{e.class.to_s.downcase} "
    next if @ignore_list.include? e.class.to_s.downcase
    $log.debug "  looping 3 #{e.name} "
    e.row = r
    e.col = c
    wt = cget(e, :weight)
    if wt
      if wt.is_a? Integer
        e.width = wt
      elsif wt.is_a? Float
        e.width = (wt * balance_ht).floor
      end
    else
      # no wt specified, give average of balance wt
      e.width = average_ht
      hround = e.width.floor

      rem += e.width - hround
      e.width = hround
      # see comment in prev block regarding remaininder
      if rem >= 1
        e.width += 1
        rem = 0
      end
    end
    $log.debug "  layout #{e.name} , w: #{e.width} r: #{e.row} , c = #{e.col} "

    e.height = @height
    c += e.width.floor
    c += @gap
  end
  $log.debug "  layout finished "
end