Class: Umbra::ToggleButton

Inherits:
Button show all
Defined in:
lib/umbra/togglebutton.rb

Overview

A button that may be switched off an on. Extended by ‘RadioButton` and `Checkbox`. WARNING, pls do not override text otherwise checkboxes etc will stop functioning. TODO: add editable here and prevent toggling if not so.

Direct Known Subclasses

Checkbox, RadioButton

Instance Attribute Summary collapse

Attributes inherited from Button

#mnemonic, #surround_chars

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 Button

#action, button_layout, #map_keys, #repaint

Methods inherited from Widget

#_form=, #color_pair, #command, #height, #highlight_attr, #init_vars, #modified?, #on_enter, #on_leave, #repaint, #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) ⇒ ToggleButton

Just calls super.

Parameters:

  • config (Hash) (defaults to: {})

    config values such as row, col, onvalue, offvalue and value.



35
36
37
38
# File 'lib/umbra/togglebutton.rb', line 35

def initialize config={}, &block
  super

end

Instance Attribute Details

#offvalueObject

set or get text to display for on value and off value



23
24
25
# File 'lib/umbra/togglebutton.rb', line 23

def offvalue
  @offvalue
end

#onvalueObject

set or get text to display for on value and off value



23
24
25
# File 'lib/umbra/togglebutton.rb', line 23

def onvalue
  @onvalue
end

Instance Method Details

#checked(tf) ⇒ Object

set the value to true or false user may programmatically want to check or uncheck ## duplicate of value ??? 2018-05-26 -



106
107
108
109
# File 'lib/umbra/togglebutton.rb', line 106

def checked tf
  @value = tf
  @repaint_required = true
end

#checked?true, false Also known as: selected?

is the button on or off

Returns:

  • (true, false)

    returns @value, has the button been checked or not.



55
56
57
# File 'lib/umbra/togglebutton.rb', line 55

def checked?
  @value
end

#fireObject

Toggles the button’s value. Called by toggle (when users pressed SPACE). Calls :PRESS event



93
94
95
96
97
98
99
100
# File 'lib/umbra/togglebutton.rb', line 93

def fire
  checked(!@value)
  #@item_event = ItemEvent.new self, self if @item_event.nil?
  #@item_event.set(@value ? :SELECTED : :DESELECTED)
  #fire_handler :PRESS, @item_event # should the event itself be ITEM_EVENT
  ## 2018-05-27 - trying to use self in most cases. Above was not needed.
  fire_handler :PRESS, self
end

#getvalueonvalue, offvalue

Returns on or off value depending on @value.

Returns:



41
42
43
# File 'lib/umbra/togglebutton.rb', line 41

def getvalue
  @value ? @onvalue : @offvalue
end

#getvalue_for_paintObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/umbra/togglebutton.rb', line 60

def getvalue_for_paint
  # when the width is set externally then the surround chars sit outside the width
  #unless @width
  if @onvalue && @offvalue
    @width = [ @onvalue.length, @offvalue.length ].max 
  end
  #end
  buttontext = getvalue().center(@width)
  @text_offset = @surround_chars[0].length
  @surround_chars[0] + buttontext + @surround_chars[1]
end

#handle_key(ch) ⇒ Object

toggle button handle key. Handles only ‘space` (32), all others are passed to parent classes.

Parameters:

  • ch (Integer)

    key received



75
76
77
78
79
80
81
82
# File 'lib/umbra/togglebutton.rb', line 75

def handle_key ch
  if ch == 32
    toggle
    @repaint_required = true # need to change the label
  else
    super
  end
end

#toggleObject

toggle the button value. Calls fire.



86
87
88
# File 'lib/umbra/togglebutton.rb', line 86

def toggle
  fire
end

#valuetrue, false

Returns current value.

Parameters:

  • value (true, false)

    Which value to use currently, onvalue or offvalue

Returns:

  • (true, false)

    current value



27
# File 'lib/umbra/togglebutton.rb', line 27

attr_property :value