Class: Rubygoo::Container

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

Direct Known Subclasses

App, Dialog, RadioGroup

Constant Summary

Constants inherited from Widget

Widget::DEFAULT_PARAMS

Instance Attribute Summary collapse

Attributes inherited from Widget

#app, #container, #enabled, #focus_priority, #focussed, #h, #mouse_over, #opts, #parent, #relative, #visible, #w, #x, #x_pad, #y, #y_pad

Instance Method Summary collapse

Methods inherited from Widget

#_focus, #_mouse_dragging, #_unfocus, #contains?, #disable, #draw, #enable, #enabled?, #focus, #focussed?, #get_color, #hide, #key_pressed, #key_released, #modal?, #mouse_down, #mouse_drag, #mouse_dragging, #mouse_enter, #mouse_exit, #mouse_motion, #mouse_over?, #mouse_up, #removed, #show, #theme_property, #unfocus, #update, #update_rect, #visible?

Constructor Details

#initialize(opts = {}) ⇒ Container

Returns a new instance of Container.



5
6
7
8
9
10
11
12
# File 'lib/rubygoo/container.rb', line 5

def initialize(opts={})
  super opts
  # cannot get this if we don't have an app yet
  @bg_color = theme_property :bg_color if self.app
  @queued_widgets = []
  @widgets = []
  @modal_widgets = []
end

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color.



3
4
5
# File 'lib/rubygoo/container.rb', line 3

def bg_color
  @bg_color
end

#queued_widgetsObject

Returns the value of attribute queued_widgets.



3
4
5
# File 'lib/rubygoo/container.rb', line 3

def queued_widgets
  @queued_widgets
end

#rectObject

Returns the value of attribute rect.



3
4
5
# File 'lib/rubygoo/container.rb', line 3

def rect
  @rect
end

#widgetsObject

Returns the value of attribute widgets.



3
4
5
# File 'lib/rubygoo/container.rb', line 3

def widgets
  @widgets
end

Instance Method Details

#_draw(adapter) ⇒ Object

draw ourself and our children



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubygoo/container.rb', line 65

def _draw(adapter)
  # any container specific code here (border_colors?)
  if @bg_color
    if app == self
      adapter.fill_screen @bg_color
    else
      x1 = @rect[0]
      y1 = @rect[1]
      x2 = @rect[2] + x1
      y2 = @rect[3] + y1
      adapter.fill x1, y1, x2, y2, @bg_color
    end
  end
  draw adapter unless app == self

  # draw kiddies 
  @widgets.select{|w|w.visible?}.each do |w|
    w._draw adapter
  end
end

#_key_pressed(event) ⇒ Object

pass on the key press to our widgets



119
120
121
122
123
124
# File 'lib/rubygoo/container.rb', line 119

def _key_pressed(event)
  key_pressed event
  @widgets.select{|w| w.enabled? and w.focussed?}.each do |w|
    w._key_pressed event
  end
end

#_key_released(event) ⇒ Object

pass on the key release to our widgets



127
128
129
130
131
132
# File 'lib/rubygoo/container.rb', line 127

def _key_released(event)
  key_released event
  @widgets.select{|w| w.enabled? and w.focussed?}.each do |w|
    w._key_released event 
  end
end

#_mouse_down(event) ⇒ Object

called when there is a mouse click



95
96
97
98
99
100
# File 'lib/rubygoo/container.rb', line 95

def _mouse_down(event)
  mouse_down event
  @widgets.select{|w|w.enabled? and w.contains? event.data[:x],event.data[:y] }.each do |w|
    w._mouse_down event
  end
end

#_mouse_drag(event) ⇒ Object

called when there is a mouse release after dragging



111
112
113
114
115
116
# File 'lib/rubygoo/container.rb', line 111

def _mouse_drag(event)
  mouse_drag event
  @widgets.select{|w| w.contains? event.data[:x],event.data[:y] and w.enabled?}.each do |w|
    w._mouse_drag event
  end
end

#_mouse_motion(event) ⇒ Object

called when there is a mouse motion



87
88
89
90
91
92
# File 'lib/rubygoo/container.rb', line 87

def _mouse_motion(event)
  mouse_motion event
  @widgets.select{|w|w.enabled?}.each do |w|
    w._mouse_motion event
  end
end

#_mouse_up(event) ⇒ Object

called when there is a mouse release



103
104
105
106
107
108
# File 'lib/rubygoo/container.rb', line 103

def _mouse_up(event)
  mouse_up event
  @widgets.select{|w| w.contains? event.data[:x],event.data[:y] and w.enabled?}.each do |w|
    w._mouse_up event 
  end
end

#_update(time) ⇒ Object

called each update cycle with the amount of time that has passed. useful for animations, etc



136
137
138
139
140
141
# File 'lib/rubygoo/container.rb', line 136

def _update(time)
  update time unless app == self
  @widgets.select{|w|w.enabled?}.each do |w|
    w._update time
  end
end

#add(*widgets) ⇒ Object

Add widget(s) to the container.



22
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
49
50
# File 'lib/rubygoo/container.rb', line 22

def add(*widgets)
  widgets.uniq.each do |w|
    unless @widgets.include? w
      if self.app
        if w.relative
          w.x += @x
          w.y += @y
          w.update_rect
        end
        w.container = self
        w.parent = self
        w.app = self.app
        if !modal? and w.tab_to? and !w.modal?
          w.app.add_tabbed_widget w 
        end
        w.added

        w.when :resized do |resized_widget|
          resize resized_widget
        end

        @widgets << w
      else
        @queued_widgets << w
      end
    end
  end
  widgets
end

#addedObject

called when we are added to another container



15
16
17
18
19
# File 'lib/rubygoo/container.rb', line 15

def added() 
  @bg_color = theme_property :bg_color
  add *@queued_widgets
  @queued_widgets = []
end

#remove(*widgets) ⇒ Object

Remove widget(s) to the container.



53
54
55
56
57
58
59
60
61
62
# File 'lib/rubygoo/container.rb', line 53

def remove(*widgets)
  widgets.uniq.each do |w|
    widget = @widgets.delete(w)
    queued_widget = @queued_widgets.delete(w)
    modal_widget = @modal_widgets.delete(w)
    if widget or queued_widget or modal_widget
      w.removed
    end
  end
end

#resize(w) ⇒ Object

called when we need to update our size



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rubygoo/container.rb', line 149

def resize(w)
  # check the rects of all our children?
  max_w = 1
  max_h = 1
  @widgets.each do |w|
    w_width = w.x + w.w
    w_height = w.y + w.h
    max_w = w_width if w_width > max_w
    max_h = w_height if w_height > max_h
  end
  @w = max_w - @x + 2*@x_pad
  @h = max_h - @y + 2*@y_pad
  update_rect
end

#tab_to?Boolean

does this widget want tabbed focus? Containers don’t usually

Returns:



144
145
146
# File 'lib/rubygoo/container.rb', line 144

def tab_to?
  false
end