Class: Fidgit::Group

Inherits:
Packer show all
Defined in:
lib/fidgit/elements/group.rb

Constant Summary

Constants inherited from Element

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

Instance Attribute Summary collapse

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 Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #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(options = {}, &block) ⇒ Group

Returns a new instance of Group.

Examples:

group do
  horizontal do
    radio_button 1, text: '1', checked: true
    radio_button 2, text: '2'
    subscribe :changed do |sender, value|
      puts value
    end
  end
end


23
24
25
26
27
28
# File 'lib/fidgit/elements/group.rb', line 23

def initialize(options = {}, &block)
  super(options)

  @selected = nil
  @buttons = []
end

Instance Attribute Details

#selectedObject (readonly)

Returns the value of attribute selected.



3
4
5
# File 'lib/fidgit/elements/group.rb', line 3

def selected
  @selected
end

Instance Method Details

#add_button(button) ⇒ Object



30
31
32
33
34
# File 'lib/fidgit/elements/group.rb', line 30

def add_button(button)
  @buttons.push button
  self.value = button.value if button.checked?
  nil
end

#remove_button(button) ⇒ Object



36
37
38
39
40
# File 'lib/fidgit/elements/group.rb', line 36

def remove_button(button)
  self.value = nil if button == @selected
  @buttons.delete button
  nil
end

#valueObject



7
# File 'lib/fidgit/elements/group.rb', line 7

def value; @selected ? @selected.value : nil; end

#value=(value) ⇒ Object

Examples:

@my_group = group do
  horizontal do
    radio_button(1, text: '1', checked: true)
    radio_button(2, text: '2')
  end
 end

# later
@my_group.value = 2


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

def value=(value)
  if value != self.value
    button = @buttons.find { |b| b.value == value }
    @selected.uncheck if @selected and @selected.checked?
    @selected = button
    @selected.check if @selected and not @selected.checked?
    publish :changed, self.value
  end

  value
end