Sha256: 07352d3429069ff2d83f1e4c4034788c93c1bf0884c826d245100bb7f2b12e90

Contents?: true

Size: 1.95 KB

Versions: 21

Compression:

Stored size: 1.95 KB

Contents

# encoding: utf-8

module Fidgit
  class RadioButton < Button
    attr_reader :group, :value

    event :changed

    def checked?; @checked; end

    # @param (see Button#initialize)
    # @param [Object] value
    #
    # @option (see Button#initialize)
    # @option options [Boolean] :checked
    def initialize(text, value, options = {}, &block)
      options = {
        checked: false,
        checked_border_color: default(:checked, :border_color),
      }.merge! options

      @checked = options[:checked]
      @value = value

      super(text, options)

      @checked_border_color = options[:checked_border_color].dup
      @unchecked_border_color = border_color
      add_to_group

      @border_color = (checked? ? @checked_border_color : @unchecked_border_color).dup
    end

    def clicked_left_mouse_button(sender, x, y)
      super
      check
      nil
    end

    # Check the button and update its group. This may uncheck another button in the group if one is selected.
    def check
      return if checked?

      @checked = true
      @group.value = value
      @border_color = @checked_border_color.dup
      publish :changed, @checked

      nil
    end

    # Uncheck the button and update its group.
    def uncheck
      return unless checked?

      @checked = false
      @group.value = value
      @border_color = @unchecked_border_color.dup
      publish :changed, @checked

      nil
    end

    protected
    def parent=(parent)
      @group.remove_button self if @parent
      super(parent)
      add_to_group if parent
      parent
    end

    protected
    def add_to_group
      container = parent
      while container and not container.is_a? Group
        container = container.parent
      end

      raise "#{self.class.name} must be placed inside a group element" unless container

      @group = container
      @group.add_button self
      nil
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
fidgit-0.2.4 lib/fidgit/elements/radio_button.rb
fidgit-0.2.3 lib/fidgit/elements/radio_button.rb
fidgit-0.2.2 lib/fidgit/elements/radio_button.rb
fidgit-0.2.1 lib/fidgit/elements/radio_button.rb
fidgit-0.2.0 lib/fidgit/elements/radio_button.rb
fidgit-0.1.10 lib/fidgit/elements/radio_button.rb
fidgit-0.1.9 lib/fidgit/elements/radio_button.rb
fidgit-0.1.8 lib/fidgit/elements/radio_button.rb
fidgit-0.1.7 lib/fidgit/elements/radio_button.rb
fidgit-0.1.6 lib/fidgit/elements/radio_button.rb
fidgit-0.1.5 lib/fidgit/elements/radio_button.rb
fidgit-0.1.4 lib/fidgit/elements/radio_button.rb
fidgit-0.1.3 lib/fidgit/elements/radio_button.rb
fidgit-0.1.2 lib/fidgit/elements/radio_button.rb
fidgit-0.1.1 lib/fidgit/elements/radio_button.rb
fidgit-0.1.0 lib/fidgit/elements/radio_button.rb
fidgit-0.0.6alpha lib/fidgit/elements/radio_button.rb
fidgit-0.0.5alpha lib/fidgit/elements/radio_button.rb
fidgit-0.0.4alpha lib/fidgit/elements/radio_button.rb
fidgit-0.0.3alpha lib/fidgit/elements/radio_button.rb