Class: Umbra::Box

Inherits:
Widget show all
Defined in:
lib/umbra/box.rb

Overview

A box is a container around one, or more, widgets. Properties include ‘visible,` `justify` and `title.` It is not focusable, so no keys can be mapped to it.

FIXME box needs to resize components if it’s dimensions are changed. Or should components have a link to parent, so they can resize themselves ?

Instance Attribute Summary collapse

Attributes inherited from Widget

#col, #col_offset, #curpos, #focusable, #graphic, #handler, #key_label, #modified, #name, #repaint_required, #row, #row_offset, #state

Instance Method Summary collapse

Methods inherited from Widget

#_form=, #color_pair, #command, #getvalue, #getvalue_for_paint, #handle_key, #height, #highlight_attr, #init_vars, #modified?, #on_enter, #on_leave, #repaint_all, #rowcol, #set_form_col, #set_form_row, #text, #touch, #variable_set, #visible, #width

Methods included from KeyMappingHandler

#_process_key, #bind_key, #bind_keys, #process_key, #unbind_key

Methods included from EventHandler

#bind_event, #event?, #fire_handler, #fire_property_change, #register_events

Constructor Details

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

Returns a new instance of Box.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/umbra/box.rb', line 31

def initialize config={}, &block
  @focusable  = false
  @visible    = true
  super

  @int_height = self.height - 2
   
  @int_width  = self.width  - 2
  @hlines = []
  #@vlines = []   # UNUSED. TODO ???
end

Instance Attribute Details

#widgetWidget (readonly)

Return widget added to this box

Returns:

  • (Widget)

    return widget added to this box



27
28
29
# File 'lib/umbra/box.rb', line 27

def widget
  @widget
end

#widgetsArray<Widget> (readonly)

Return widgets added to this box

Returns:

  • (Array<Widget>)

    return widgets added to this box



25
26
27
# File 'lib/umbra/box.rb', line 25

def widgets
  @widgets
end

Instance Method Details

#add(*w) ⇒ Object Also known as: stack

Add a variable list of components to a box, which are stacked horizontally by the box.

Parameters:

  • w (Array<Widget>)

    comma separated list of widgets



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/umbra/box.rb', line 57

def add *w
  @widgets = w
  num = w.size
  wid = @int_width 
  ht  = (@int_height / num)
  srow = @row + 1
  scol = @col + 1
  w.each_with_index do |e, ix|
    e.width = wid
    e.height = ht 
    e.row    = srow
    e.col    = scol
    srow += ht + 1
    @hlines << [ srow-1, scol ]
  end
  # FIXME there will be one additional hline in the end.
  w[-1].height -= (num-1)
end

#fill(w) ⇒ Object

Fill out a single widget into the entire box leaving an inset of 1. NOTE: use if only one widget will expand into this box

Parameters:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/umbra/box.rb', line 111

def fill w
  # should have been nice if I could add widget to form, but then order might get wrong
  w.row = self.row + 1
  w.col = self.col + 1
  if w.respond_to? :width
    if @width < 0
      w.width = @width - 1   ## relative to bottom
    else
      w.width = @width - 2   ## absolute
    end
  end
  if w.respond_to? :height
    if @height < 0
      w.height = @height - 1   ## relative to bottom
    else
      w.height = @height - 2   ## absolute
    end
  end
  @widget = w
end

#flow(*w) ⇒ Object

Note:

this is best used for widgets that can be resized.

Horizontally place an array of widgets Prefer not to use for buttons since the looks gets messed (inconsistency between button and highlight). Therefore now, button calculates its own width which means that this program cannot determine what the width is and thus cannot center it.

Parameters:

  • w (Array<Widget>)

    comma separated list of widgets



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/umbra/box.rb', line 85

def flow *w
  @widgets = w
  num = w.size
  wid = (@int_width / num).floor    ## FIXME how to recalc this if RESIZE
  ht  = @int_height 
  srow = self.row + 1
  scol = self.col + 1
  w.each_with_index do |e, ix|
    # unfortunately this is messing with button width calculation
    # maybe field and button should have resizable or expandable ?
    e.width = wid unless e.width
    e.height = ht 
    e.row    = srow
    e.col    = scol
    scol += wid + 1
    #@hlines << [ srow-1, scol ]
  end
  # FIXME there will be one additional hline in the end.
  # we added 1 to the scol each time, so decrement
  w[-1].width -= (num-1)
end

#hline(row, col) ⇒ Object

Paint a horizontal line, as a separator between widgets Called by ‘repaint`.

Parameters:

  • row (Integer)

    row

  • col (Integer)

    column



136
137
138
139
140
# File 'lib/umbra/box.rb', line 136

def hline row, col
  return if row >= self.row + self.height
  $log.debug "  hline: #{row} ... #{@row}   #{@height}  "
  FFI::NCurses.mvwhline( @graphic.pointer, row, col, FFI::NCurses::ACS_HLINE, self.width()-2)
end

#repaintObject

paint border and title, called by Form.



44
45
46
47
48
49
50
51
52
# File 'lib/umbra/box.rb', line 44

def repaint                                           #:nodoc:
  return unless @visible
  print_border self.row, self.col, self.height, self.width, @color_pair || CP_BLACK, @attr || NORMAL
  print_title @title
  if !@hlines.empty?
    @hlines.each {|e| hline(e.first, e[1]) }
  end
  # what about asking for painting of widgets
end