Class: GtkApp::View

Inherits:
Gtk::Builder
  • Object
show all
Includes:
Helpers, ViewHelpers
Defined in:
lib/gtk_app/view.rb

Direct Known Subclasses

Partial

Instance Method Summary collapse

Methods included from ViewHelpers

#build_listview, #desensitize, #sensitize

Methods included from Helpers

#log

Constructor Details

#initialize(controller, builder_file, *args) ⇒ View

Returns a new instance of View.

Parameters:



8
9
10
11
12
13
14
15
# File 'lib/gtk_app/view.rb', line 8

def initialize(controller, builder_file, *args)
  super()
  # options = args.extract_options!
  self.add_from_file(builder_file)
  self.connect_signals { |handler| controller.method(handler) }

  # self.title = options[:title] if options.key?(:title)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



17
18
19
20
21
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gtk_app/view.rb', line 17

def method_missing(id, *args, &block)
  method_name = id.to_s

  widget_name = (method_name =~ /([!=]|<{2})$/ ? method_name.chop : method_name)
  widget = self["#{widget_name}"]
  super unless widget

  bang_proc, equal_proc, append_proc = case widget
  when Gtk::TextView then
    [ lambda { widget.buffer.text },
      lambda { |*argv| widget.buffer.text = argv[0].to_s },
      lambda { |text| widget.buffer.text << text.to_s } ]
  when Gtk::ComboBox then
    [ lambda { widget.active_text },
      lambda do |*argv|
        if argv[0].is_a?(Fixnum)
          widget.active = argv[0]
        elsif widget.model
          result = nil
          widget.model.each do |m, p, i|
            result = iter if iter[0] =~ /\A#{argv[0]}/i
          end
          widget.active_iter = result if result
        end
      end,
      lambda { |text| widget.append_text(text.to_s) } ]
  when Gtk::ToggleButton, Gtk::CheckButton then
    [ lambda { widget.active? },
      lambda { |*argv| widget.active = argv[0] },
      nil ]
  when Gtk::TreeView then
    [ lambda { widget.selection.selected },
      nil,
      lambda do |row|
        iter = widget.model.append
        if row.is_a?(Gtk::TreeIter)
          (0..model.n_columns).each { |i| iter[i] = row[i] }
        else
          row.each_with_index { |v,i| iter[i] = v }
        end
        iter
      end ]
  else
    if widget.respond_to?(:text)
      [ lambda { widget.text },
        lambda { |*argv| widget.text = argv[0].to_s },
        lambda { |text| widget.text = ("#{widget.text}" << text) } ]
    else [nil, nil, nil]; end
  end

  class_eval do
    define_method(:"#{widget_name}", lambda { widget })
    define_method(:"#{widget_name}!", bang_proc) if bang_proc
    define_method(:"#{widget_name}=", equal_proc) if equal_proc
  end

  widget.class_eval do
    define_method("<<", append_proc)
  end if append_proc

  send(:"#{method_name}", *args, &block)
end