class RubyCurses::ModStack::BaseStack

Base class for stacks and flows. Will manage determining row col and width height of objects Stacks place objects one below another. Flows place objects to the right of the previous. Orientation can be reversed.

Attributes

components[RW]
config[R]
form[RW]

Public Class Methods

new(config={}) click to toggle source
# File lib/canis/core/util/basestack.rb, line 23
def initialize config={}, components=[]
  @config = config
  config.each do |k, v|
    instance_variable_set "@#{k}", v
  end
  @components = components
  @calc_needed = true
end

Public Instance Methods

check_coords(e) click to toggle source

Traverses the comopnent tree and calculates weightages for all components based on what has been specified by user

# File lib/canis/core/util/basestack.rb, line 175
def check_coords e # stack
  r = e.row
  c = e.col
  if r >= row + height
    $log.warn "XXX: WARN e.class is out of bounds row #{r} "
    e.visible = false
  end
  if c >= col + width
    $log.warn "XXX: WARN e.class is out of bounds col #{c} "
    e.visible = false
  end
end
decrease(c=@current_component) click to toggle source
# File lib/canis/core/util/basestack.rb, line 219
def decrease c=@current_component
  p = self #c.parent_component
  ci = p.components.index(c)
  ni = ci + 1
  if p.components[ni].nil?
    ni = nil
  end
  case p
  when Flow
    # increase width of current and reduce from neighbor
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.width -= 1
      n.width += 1
      n.col   -= 1
    end

  when Stack
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.height -= 1
      n.height += 1
      n.row   -= 1
    end
    $log.debug "XXX: INC st current #{ci}, total#{p.components.count} "
  end

end
focusable() click to toggle source
# File lib/canis/core/util/basestack.rb, line 59
def focusable; false; end
increase(c=@current_component) click to toggle source
# File lib/canis/core/util/basestack.rb, line 187
def increase c=@current_component
  p = self #c.parent_component
  ci = p.components.index(c)
  ni = ci + 1
  if p.components[ni].nil?
    ni = nil
  end
  case p
  when Flow
    # increase width of current and reduce from neighbor
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.width += 1
      n.width -= 1
      n.col   += 1
    end

  when Stack
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.height += 1
      n.height -= 1
      n.row   += 1
    end
    $log.debug "XXX: INC st current #{ci}, total#{p.components.count} "
  end

end
override_graphic(gr) click to toggle source
# File lib/canis/core/util/basestack.rb, line 56
def override_graphic gr
  @graphic = gr
end
recalc() click to toggle source

Calculates row col and width height for each subc-omponent based on coords of Container This is to be called only when the container has got its coordinates (i.e Containers repaint). This should be in this objects repaint.

# File lib/canis/core/util/basestack.rb, line 64
def recalc
  @calc_needed = false
  comp = self
  if comp.is_a? BaseStack
    check_coords comp
    @margin_left   ||= 0
    @margin_right  ||= 0
    @margin_top    ||= 0
    @margin_bottom ||= 0
    if comp.is_a? Stack
      r   = row + @margin_top
      rem = 0
      ht = height - (@margin_top + @margin_bottom)
      if @orientation == :bottom 
        mult = -1
        comps = @components.reverse
        r = row + height - @margin_bottom

      else
        mult = 1
        comps = @components
   
      end
      comps.each { |e| 
        # should only happen if expandable FIXME
        e.margin_top ||= 0
        e.margin_bottom ||= 0
        e.height = 0.01 * e.weight * (ht - (e.margin_top + e.margin_bottom)) 
        hround = e.height.floor
        rem += e.height - hround
        e.height = hround #- (@margin_top + @margin_bottom)
        # rounding creates a problem, since 0.5 gets rounded up and we can exceed bound
        # So i floor, and maintain the lost space, and add it back when it exceeds 1
        # This way the last components gets stretched to meet the end, which is required
        # when the height of the stack is odd and there's a left-over row
        if rem >= 1
          e.height += 1
          rem = 0
        end
        # Item level margins have not been accounted for when calculating weightages, and
        # should not be used on the weightage axis
        r += e.margin_top
        if @orientation == :bottom
          r += e.height * mult
          e.row = r 
        else
          e.row = r 
          r += e.height + 0
        end
        e.margin_left ||= 0
        e.margin_right ||= 0
        e.width = width - (@margin_left + @margin_right + e.margin_left + e.margin_right)
        e.col = col + @margin_left + e.margin_left # ??? XXX
        #$log.debug "XXX: recalc stack #{e.widget.class} r:#{e.row} c:#{e.col} h:#{e.height} = we:#{e.weight} * h:#{height} "
        #e.col_offset = col_offset # ??? XXX
        check_coords e
        e.repaint_all(true)
        e.recalc if e.is_a? BaseStack
      }
    elsif comp.is_a? Flow
      c = col + @margin_left #+ col_offset
      rem = 0
      wd = width - (@margin_left + @margin_right)
      # right_to_left orientation
      if @orientation == :right
        mult = -1
        comps = @components.reverse
        c = col + width - @margin_right
        $log.debug "XXX:  ORIENT1f recalc #{@orientation} "
      else
        mult = 1
        comps = @components
        $log.debug "XXX:  ORIENT2f recalc #{@orientation} "
      end
      comps.each { |e| 
        e.width = e.weight * wd  * 0.01
        wround = e.width.floor
        rem += e.width - wround
        e.width = wround
        # see comment in prev block regarding remaininder
        if rem >= 1
          e.width += 1
          rem = 0
        end
        e.height = height - (@margin_top + @margin_bottom) #* weight * 0.01
        #e.height = e.height.round
        if @orientation == :right
          c += e.width * mult # mult 1 or -1
          e.col = c
        else
          e.col = c
          c += e.width * mult # mult 1 or -1
        end
        e.row = row + @margin_top
        check_coords e
        $log.debug "XXX: recalc flow #{e.widget.class} r:#{e.row} c:#{e.col} h:#{e.height} = we:#{e.weight} * w:#{width} "
        e.repaint_all(true)   # why not happening when we change row, hieght etc
        e.recalc if e.is_a? BaseStack
      }
    end
  else
    alert "in else recalc DOES NOT COME HERE "
    comp.col    = comp.parent.col
    comp.row    = comp.parent.row
    comp.height = comp.parent.height
    comp.width  = comp.parent.width
    $log.debug "XXX: recalc else #{comp.class} r #{comp.row} c #{comp.col} . h #{comp} height w #{comp.width} "
  end
end
repaint() click to toggle source

alias :parent= :parent_component

# File lib/canis/core/util/basestack.rb, line 47
def repaint # stack
  $log.debug "XXX: stack repaint recalc #{@calc_needed} "
  @components.each { |e| e.form = @form unless e.form } #unless @calc_needed
  recalc if @calc_needed
  @components.each { |e| e.repaint }
end
repaint_all(x) click to toggle source
# File lib/canis/core/util/basestack.rb, line 53
def repaint_all x
  @calc_needed = true
end
to_s() click to toggle source
# File lib/canis/core/util/basestack.rb, line 251
def to_s
  @components
end