Class: Umbra::ToggleButton
- 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
Instance Attribute Summary collapse
-
#offvalue ⇒ Object
set or get text to display for on value and off value.
-
#onvalue ⇒ Object
set or get text to display for on value and off value.
Attributes inherited from Button
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
-
#checked(tf) ⇒ Object
set the value to true or false user may programmatically want to check or uncheck ## duplicate of value ??? 2018-05-26 -.
-
#checked? ⇒ true, false
(also: #selected?)
is the button on or off.
-
#fire ⇒ Object
Toggles the button’s value.
-
#getvalue ⇒ onvalue, offvalue
Returns on or off value depending on @value.
- #getvalue_for_paint ⇒ Object
-
#handle_key(ch) ⇒ Object
toggle button handle key.
-
#initialize(config = {}, &block) ⇒ ToggleButton
constructor
Just calls super.
-
#toggle ⇒ Object
toggle the button value.
-
#value ⇒ true, false
Current value.
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.
35 36 37 38 |
# File 'lib/umbra/togglebutton.rb', line 35 def initialize config={}, &block super end |
Instance Attribute Details
#offvalue ⇒ Object
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 |
#onvalue ⇒ Object
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
55 56 57 |
# File 'lib/umbra/togglebutton.rb', line 55 def checked? @value end |
#fire ⇒ Object
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 |
#getvalue ⇒ onvalue, offvalue
Returns on or off value depending on @value.
41 42 43 |
# File 'lib/umbra/togglebutton.rb', line 41 def getvalue @value ? @onvalue : @offvalue end |
#getvalue_for_paint ⇒ Object
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 = getvalue().center(@width) @text_offset = @surround_chars[0].length @surround_chars[0] + + @surround_chars[1] end |
#handle_key(ch) ⇒ Object
toggle button handle key. Handles only ‘space` (32), all others are passed to parent classes.
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 |
#toggle ⇒ Object
toggle the button value. Calls fire
.
86 87 88 |
# File 'lib/umbra/togglebutton.rb', line 86 def toggle fire end |
#value ⇒ true, false
Returns current value.
27 |
# File 'lib/umbra/togglebutton.rb', line 27 attr_property :value |