Class: Umbra::Button

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

Overview

Widget that has an action associated with ‘:PRESS` event.

Direct Known Subclasses

ToggleButton

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#_form=, #color_pair, #command, #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) ⇒ Button

Returns a new instance of Button.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/umbra/button.rb', line 27

def initialize config={}, &block
  @focusable = true
  @editable = false
  @highlight_attr = REVERSE
  
  register_events([:PRESS])
  @default_chars = ['> ', ' <'] # a default button is painted differently. UNUSED. ???
  super

  @surround_chars ||= ['[ ', ' ]'] 
  @col_offset = @surround_chars[0].length 
  @text_offset = 0      # used to determine where underline should fall TODO ???
  map_keys
end

Instance Attribute Details

#mnemonicObject

char to be underlined, and bound to Alt-char



24
25
26
# File 'lib/umbra/button.rb', line 24

def mnemonic
  @mnemonic
end

#surround_charsObject

characters to use to surround the button, def is square brackets



21
22
23
# File 'lib/umbra/button.rb', line 21

def surround_chars
  @surround_chars
end

Class Method Details

.button_layout(buttons, row, startcol = 0, cols = FFI::NCurses.COLS-1, gap = 5) ⇒ Object

layout an array of buttons horizontally {{{



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/umbra/button.rb', line 119

def self.button_layout buttons, row, startcol=0, cols=FFI::NCurses.COLS-1, gap=5
  col = startcol
  buttons.each_with_index do |b, ix|
    $log.debug " BUTTON #{b}: #{b.col} "
    b.row = row
    b.col col
    $log.debug " after BUTTON #{b}: #{b.col} "
    len = b.text.length + gap
    col += len
  end
end

Instance Method Details

#action(a) ⇒ Object

set button based on Action 2018-03-22 - is this still used ? This allows action objects to be used in multiple places such as buttons, menus, popups etc.



47
48
49
50
51
# File 'lib/umbra/button.rb', line 47

def action a
  text a.name
  mnemonic a.mnemonic unless a.mnemonic.nil?
  command { a.call }
end

#fireObject

fires ‘PRESS` event of button



100
101
102
# File 'lib/umbra/button.rb', line 100

def fire
  fire_handler :PRESS, ActionEvent.new(self, :PRESS, text)
end

#getvalueObject



53
54
55
# File 'lib/umbra/button.rb', line 53

def getvalue
  @text
end

#getvalue_for_paintObject

ensure text has been passed or action



58
59
60
61
62
# File 'lib/umbra/button.rb', line 58

def getvalue_for_paint
  ret = getvalue
  @text_offset = @surround_chars[0].length
  @surround_chars[0] + ret + @surround_chars[1]
end

#handle_key(ch) ⇒ Object

Button’s key handler, just calls super



114
115
116
# File 'lib/umbra/button.rb', line 114

def handle_key ch
  super
end

#map_keysObject



108
109
110
111
# File 'lib/umbra/button.rb', line 108

def map_keys
  return if @keys_mapped
  bind_key(32, "fire") { fire } if respond_to? :fire
end

#repaintObject

button



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
# File 'lib/umbra/button.rb', line 64

def repaint  # button
  return unless @repaint_required

    $log.debug("BUTTON repaint : #{self}  r:#{@row} c:#{@col} , cp:#{@color_pair}, st:#{@state}, #{getvalue_for_paint}" )
    r,c = @row, @col 
    _attr = @attr || NORMAL
    _color = @color_pair
    if @state == :HIGHLIGHTED
      _color = @highlight_color_pair || @color_pair
      _attr = @highlight_attr || _attr
    elsif selected? # only for certain buttons lie toggle and radio
      _color = @selected_color_pair || @color_pair
    end
    #$log.debug "XXX: button #{text}   STATE is #{@state} color #{_color} , attr:#{_attr}"
    value = getvalue_for_paint
    #$log.debug("button repaint :#{self} r:#{r} c:#{c} col:#{_color} v: #{value} ul #{@underline} mnem #{@mnemonic} ")
    #len = @width || value.length # 2018-04-07 - width is not serving a purpose right now
    #                             # since surround chars still come where they do, and only highlight uses the width
    #                             which looks wrong.
    len = value.length
    @graphic.printstring r, c, "%-*s" % [len, value], _color, _attr

    # if a mnemonic character has been defined, then locate the index and highlight it.
    # TODO a mnemonic can also be defined in the text with an ampersand.
    if @mnemonic
      index = value.index(@mnemonic) || value.index(@mnemonic.swapcase)
      if index
        y = c + index
        x = r
        @graphic.mvchgat(x, y, max=1, FFI::NCurses::A_BOLD|UNDERLINE, FFI::NCurses.COLOR_PAIR(_color || 1), nil)
      end
    end
    @repaint_required = false
end

#selected?false

for campatibility with all buttons, will apply to radio buttons mostly

Returns:

  • (false)


106
# File 'lib/umbra/button.rb', line 106

def selected?; false; end