class Canis::ButtonGroup

This is not a visual class or a widget. This class allows us to attach several RadioButtons to it, so it can maintain which one is the selected one. It also allows for assigning of commands to be executed whenever a button is pressed, akin to binding to the fire of the button, except that one would not have to bind to each button, but only once here.

@example

group = ButtonGroup.new
group.add(r1).add(r2).add(r3)
# change the color of +somelabel+ to the color specified by the value of clicked radio.
group.command(somelabel) do |grp, label| label.color(grp.value); end

Added on 2014-04-30

Attributes

elements[R]

Array of buttons that have been added.

value[R]

the value of the radio button that is selected. To get the button itself, use selection.

Public Class Methods

new() click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3503
def initialize 
  @elements = []
  @hash = {}
end

Public Instance Methods

add(e) click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3508
def add e
  @elements << e
  @hash[e.value] = e
  e.variable(self)
  self
end
command(*args, &block) click to toggle source

install trigger to call whenever a value is updated @public called by user components

# File lib/canis/core/widgets/rwidget.rb, line 3536
def command *args, &block
  @commands ||= []
  @args ||= []
  @commands << block
  @args << args
end
get_value(name=nil) click to toggle source

returns the value of the selected button NOTE: This is used by RadioButton class for backward compat with Variable. User programs should use +value()+

# File lib/canis/core/widgets/rwidget.rb, line 3569
def get_value name=nil
  @value
end
remove(e) click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3514
def remove e
  @elements.delete e
  @hash.delete e.value
  self
end
select(button) click to toggle source

select the given button or value. This may be called by user programs to programmatically select a button

# File lib/canis/core/widgets/rwidget.rb, line 3544
def select button
  if button.is_a? String
    ;
  else
    button = button.value
  end
  set_value button
end
selected?(val) click to toggle source

@param [String, RadioButton] value of a button, or Button itself to check if selected. @return [true or false] for wether the given value or button is the selected one

# File lib/canis/core/widgets/rwidget.rb, line 3527
def selected? val
  if val.is_a? String
    @value == val
  else
    @hash[@value] == val
  end
end
selection() click to toggle source

@return the radiobutton that is selected

# File lib/canis/core/widgets/rwidget.rb, line 3521
def selection
  @hash[@value]
end
set_value(value, name=nil) click to toggle source

when a radio button is pressed, it calls set_value giving the value of that radio. it also gives the name (optionally) since Variables can also be passed and be used across groups. Here, since a button group is only for one group, so we discard name. @param [String] value (text) of radio button that is selected

This is used by RadioButton class for backward compat with Variable.

# File lib/canis/core/widgets/rwidget.rb, line 3559
def set_value value, name=nil
  @value = value
  return unless @commands
  @commands.each_with_index do |comm, ix|
    comm.call(self, *@args[ix]) unless comm.nil?
  end
end