Class: Rubygoo::CheckBox

Inherits:
Widget
  • Object
show all
Defined in:
lib/rubygoo/check_box.rb

Direct Known Subclasses

RadioButton

Constant Summary collapse

DEFAULT_PARAMS =
{:align=>:right}

Instance Attribute Summary collapse

Attributes inherited from Widget

#app, #container, #enabled, #focus_priority, #focussed, #goo_id, #h, #mouse_over, #opts, #padding_left, #padding_top, #parent, #relative, #visible, #w, #x, #y

Instance Method Summary collapse

Methods inherited from Widget

#_draw, #_focus, #_key_pressed, #_key_released, #_mouse_down, #_mouse_drag, #_mouse_dragging, #_mouse_motion, #_mouse_up, #_unfocus, #_update, #contains?, #disable, #enable, #enabled?, #focus, #focussed?, #get, #get_color, goo_prop, #hide, inherited, #key_released, #modal?, #mouse_down, #mouse_dragging, #mouse_enter, #mouse_exit, #mouse_motion, #mouse_over?, #removed, #show, #tab_to?, #theme_property, #unfocus, #update, #update_rect, #visible?

Methods included from Inflector

#camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, #inflections, #ordinalize, #pluralize, #singularize, #tableize, #titleize, #underscore

Constructor Details

#initialize(opts = {}) ⇒ CheckBox

Returns a new instance of CheckBox.



13
14
15
16
17
18
19
20
21
# File 'lib/rubygoo/check_box.rb', line 13

def initialize(opts={})
  opts = DEFAULT_PARAMS.merge opts
  @checked = opts[:checked]

  @label_text = opts[:label_text]
  @label_alignment = opts[:align]

  super opts
end

Instance Attribute Details

#checkedObject

Returns the value of attribute checked.



4
5
6
# File 'lib/rubygoo/check_box.rb', line 4

def checked
  @checked
end

#label_text(new_val = nil) ⇒ Object (readonly)

DSL methods



120
121
122
# File 'lib/rubygoo/check_box.rb', line 120

def label_text
  @label_text
end

Instance Method Details

#addedObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubygoo/check_box.rb', line 23

def added()
  @color = theme_property :color
  @bg_color = theme_property :bg_color
  @border_color = theme_property :border_color
  @focus_color = theme_property :focus_color
  @checked_color = theme_property :checked_color
  @hover_color = theme_property :hover_color

  @rect = Rect.new [@x-@padding_left,@y-@padding_top,@w+2*@padding_left,@h+2*@padding_top]
  unless @label_text.nil? or @label_text.empty?
    @label = Label.new @label_text, :x=>0,:y=>0, :relative=>@relative, :visible=>false
    @parent.add @label

    case @label_alignment
    when :right
      lx = @x+2*@padding_left+@w
      ly = @y
    when :left
      ly = @y
      lx = @x-2*@padding_left-@label.w
    end
    @label.x = lx
    @label.y = ly
    @label.show
  end
end

#checkObject



62
63
64
65
# File 'lib/rubygoo/check_box.rb', line 62

def check()
  @checked = true
  fire :checked, self
end

#checked?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubygoo/check_box.rb', line 50

def checked?()
  @checked
end

#draw(adapter) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rubygoo/check_box.rb', line 90

def draw(adapter)
  x1 = @rect[0]
  y1 = @rect[1]
  x2 = @rect[2] + x1
  y2 = @rect[3] + y1
  if @focussed
    adapter.fill x1, y1, x2, y2, @focus_color
  elsif @bg_color
    adapter.fill x1, y1, x2, y2, @bg_color
  end

  if @checked
    rect = @rect.inflate(-@padding_left,-@padding_top)
    cx1 = rect[0]
    cy1 = rect[1]
    cx2 = rect[2] + x1
    cy2 = rect[3] + y1
    adapter.fill cx1, cy1, cx2, cy2, @checked_color
  end

  if mouse_over? and @hover_color
    adapter.fill x1, y1, x2, y2, @hover_color
  end

  if @border_color
    adapter.draw_box x1, y1, x2, y2, @border_color
  end
end

#key_pressed(event) ⇒ Object

called when a key press is sent to us



83
84
85
86
87
88
# File 'lib/rubygoo/check_box.rb', line 83

def key_pressed(event)
  case event.data[:key]
  when K_SPACE
    toggle
  end
end

#mouse_drag(event) ⇒ Object

called when there is a mouse click at the end of a drag



78
79
80
# File 'lib/rubygoo/check_box.rb', line 78

def mouse_drag(event)
  toggle
end

#mouse_up(event) ⇒ Object

called when there is a mouse click



73
74
75
# File 'lib/rubygoo/check_box.rb', line 73

def mouse_up(event)
  toggle
end

#toggleObject



54
55
56
57
58
59
60
# File 'lib/rubygoo/check_box.rb', line 54

def toggle()
  if checked?
    uncheck
  else
    check
  end
end

#uncheckObject



67
68
69
70
# File 'lib/rubygoo/check_box.rb', line 67

def uncheck()
  @checked = false
  fire :checked, self
end