Class: Fidgit::RadioButton

Inherits:
Button show all
Defined in:
lib/fidgit/elements/radio_button.rb

Direct Known Subclasses

ColorWell, List::Item

Constant Summary

Constants inherited from Label

Label::ICON_POSITIONS

Constants inherited from Composite

Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary collapse

Attributes inherited from Label

#background_color, #border_color, #icon_position

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary collapse

Methods inherited from Button

#activate, #enabled=, #enter, #leave, #text=

Methods inherited from Label

#hit_element, #icon, #icon=

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #hit_element, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #to_s, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

#initialize(text, value, options = {}, &block) ⇒ RadioButton

Returns a new instance of RadioButton.

Parameters:

  • value (Object)
  • text (String)

    The string to display in the label.

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

    a customizable set of options

Options Hash (options):

  • :checked (Boolean)
  • :shortcut (Symbol) — default: nil

    Adds a shortcut key for this element, that activates it. :auto takes the first letter of the text.

  • :icon (Gosu::Image, nil) — default: nil
  • :justify (:left, :right, :center) — default: :left

    Text justification.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fidgit/elements/radio_button.rb', line 16

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

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/fidgit/elements/radio_button.rb', line 5

def group
  @group
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/fidgit/elements/radio_button.rb', line 5

def value
  @value
end

Instance Method Details

#checkObject

Check the button and update its group. This may uncheck another button in the group if one is selected.



41
42
43
44
45
46
47
48
49
50
# File 'lib/fidgit/elements/radio_button.rb', line 41

def check
  return if checked?

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

  nil
end

#checked?Boolean

Returns:

  • (Boolean)


9
# File 'lib/fidgit/elements/radio_button.rb', line 9

def checked?; @checked; end

#clicked_left_mouse_button(sender, x, y) ⇒ Object



34
35
36
37
38
# File 'lib/fidgit/elements/radio_button.rb', line 34

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

#uncheckObject

Uncheck the button and update its group.



53
54
55
56
57
58
59
60
61
62
# File 'lib/fidgit/elements/radio_button.rb', line 53

def uncheck
  return unless checked?

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

  nil
end