Class: Merrol::WidgetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/merrol/lib/widget_builder.rb

Class Method Summary collapse

Class Method Details

.build(*view_names) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/merrol/lib/widget_builder.rb', line 3

def self.build *view_names
  widgets = {}
  view_names.each do |view_name|
    yaml = YAML.load_view(view_name.to_s)
    widget = nil
    add_to = nil
    pack = {:expand => false, :fill => false, :padding => 0}
    yaml['layout'].merge(yaml['options'] || {}).each do |key, value|
      if key == 'type'
        widget = eval('Gtk::' + value).new
      elsif value.is_a?(Hash)
        widget.send("#{key}=", constants_from(value))
      elsif key == 'add_to'
        add_to = widgets[value.to_sym]
      elsif %w(expand fill padding).include?(key)
        pack[key.to_sym] = value
      else
        widget.send("#{key}=", value)
      end
    end

    if add_to
      container = add_to.container if add_to.respond_to?(:container)
      container ||=  add_to
      if container.is_a?(Gtk::Bin)
        container.add(widget)
      else
        container.pack_start(widget, pack[:expand], pack[:fill], pack[:padding])
      end
    end
    widgets[view_name] = widget
  end
  widgets
end