Class: Canis::ToggleButton

Inherits:
Button show all
Defined in:
lib/canis/core/widgets/rwidget.rb

Overview

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

Since:

  • 1.2.0

Direct Known Subclasses

CheckBox, RadioButton

Instance Attribute Summary

Attributes inherited from Widget

#_object_created, #col_offset, #config, #curpos, #focussed, #form, #handler, #id, #key_label, #parent_component, #row_offset, #state

Instance Method Summary collapse

Methods inherited from Button

#action, #bind_hotkey, button_layout, #command, #default_button, #map_keys, #mnemonic, #repaint, #text

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #command, #destroy, #focus, #focusable, #focusable?, #hide, #init_vars, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rb_getstr, #warn

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

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

Returns a new instance of ToggleButton.

Since:

  • 1.2.0



3380
3381
3382
3383
3384
# File 'lib/canis/core/widgets/rwidget.rb', line 3380

def initialize form, config={}, &block
  super
  
  @value ||= (@variable.nil? ? false : @variable.get_value(@name)==true)
end

Instance Method Details

#checked(tf) ⇒ Object

set the value to true or false user may programmatically want to check or uncheck

Since:

  • 1.2.0



3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
# File 'lib/canis/core/widgets/rwidget.rb', line 3441

def checked tf
  @value = tf
  if @variable
    if @value 
      @variable.set_value((@onvalue || 1), @name)
    else
      @variable.set_value((@offvalue || 0), @name)
    end
  end
end

#checked?Boolean Also known as: selected?

added for some standardization 2010-09-07 20:28 alias :text :getvalue # NEXT VERSION change existing text to label

is the button on or off added 2008-12-09 19:05

Returns:

  • (Boolean)

Since:

  • 1.2.0



3397
3398
3399
# File 'lib/canis/core/widgets/rwidget.rb', line 3397

def checked?
  @value
end

#fireObject

called on :PRESS event caller should check state of itemevent passed to block

Since:

  • 1.2.0



3432
3433
3434
3435
3436
3437
# File 'lib/canis/core/widgets/rwidget.rb', line 3432

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
end

#getvalueObject

Since:

  • 1.2.0



3385
3386
3387
# File 'lib/canis/core/widgets/rwidget.rb', line 3385

def getvalue
  @value ? @onvalue : @offvalue
end

#getvalue_for_paintObject

Since:

  • 1.2.0



3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
# File 'lib/canis/core/widgets/rwidget.rb', line 3402

def getvalue_for_paint
  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

Parameters:

  • key (int)

    received

Since:

  • 1.2.0



3416
3417
3418
3419
3420
3421
3422
# File 'lib/canis/core/widgets/rwidget.rb', line 3416

def handle_key ch
  if ch == 32
    toggle
  else
    super
  end
end

#toggleObject

toggle the button value

Since:

  • 1.2.0



3426
3427
3428
# File 'lib/canis/core/widgets/rwidget.rb', line 3426

def toggle
  fire
end