Class: Gtk::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk3/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gtk3/builder.rb', line 65

def initialize(options={})
  path      = options[:path] || options[:file]
  resource  = options[:resource]
  string    = options[:string]

  if path
    path = path.to_path if path.respond_to?(:to_path)
    initialize_new_from_file(path)
  elsif resource
    initialize_new_from_resource(resource)
  elsif string
    initialize_new_from_string(string, string.bytesize)
  else
    initialize_raw
  end
end

Class Method Details

.connect_signal(builder, object, signal_name, handler_name, connect_object, flags, &block) ⇒ Object


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
# File 'lib/gtk3/builder.rb', line 20

def connect_signal(builder,
                   object,
                   signal_name,
                   handler_name,
                   connect_object,
                   flags,
                   &block)
  handler_name = normalize_handler_name(handler_name)
  if connect_object
    handler = connect_object.method(handler_name)
  else
    handler = block.call(handler_name)
  end

  unless handler
    $stderr.puts("Undefined handler: #{handler_name}") if $DEBUG
    return
  end

  if flags.is_a?(Integer)
    flags = GLib::ConnectFlags.new(flags)
  end

  if flags.after?
    signal_connect_method = :signal_connect_after
  else
    signal_connect_method = :signal_connect
  end

  if handler.arity.zero?
    object.__send__(signal_connect_method, signal_name) do
      handler.call
    end
  else
    object.__send__(signal_connect_method, signal_name, &handler)
  end
end

Instance Method Details

#<<(target) ⇒ Object


143
144
145
146
# File 'lib/gtk3/builder.rb', line 143

def <<(target)
  add(target)
  self
end

#add(target_or_options = {}) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gtk3/builder.rb', line 94

def add(target_or_options={})
  if target_or_options.is_a?(Hash)
    options = target_or_options
  else
    target = target_or_options
    options = {}
    if target.respond_to?(:to_path)
      options[:path] = target.to_path
    elsif target.start_with?("<") or target.start_with?(" ")
      options[:string] = target
    elsif File.exist?(target)
      options[:path] = target
    else
      options[:resource] = target
    end
  end

  string   = options[:string]
  path     = options[:path] || options[:file]
  resource = options[:resource]

  object_ids = options[:object_ids]

  if path
    path = path.to_path if path.respond_to?(:to_path)
    if object_ids
      add_objects_from_file(path, object_ids)
    else
      add_from_file(path)
    end
  elsif resource
    if object_ids
      add_objects_from_resource(resource, object_ids)
    else
      add_from_resource(resource)
    end
  elsif string
    if object_ids
      add_objects_from_string(string, object_ids)
    else
      add_from_string(string)
    end
  else
    message = ":path (:file), :resource or :string " +
      "must be specified: #{options.inspect}"
    raise InvalidArgument, message
  end
end

#add_from_string(string) ⇒ Object


85
86
87
# File 'lib/gtk3/builder.rb', line 85

def add_from_string(string)
  add_from_string_raw(string, string.bytesize)
end

#add_from_string_rawObject


84
# File 'lib/gtk3/builder.rb', line 84

alias_method :add_from_string_raw, :add_from_string

#add_objects_from_string(string, object_ids) ⇒ Object


90
91
92
# File 'lib/gtk3/builder.rb', line 90

def add_objects_from_string(string, object_ids)
  add_objects_from_string_raw(string, string.bytesize, object_ids)
end

#add_objects_from_string_rawObject


89
# File 'lib/gtk3/builder.rb', line 89

alias_method :add_objects_from_string_raw, :add_objects_from_string

#connect_signals(&block) ⇒ Object


149
150
151
152
153
# File 'lib/gtk3/builder.rb', line 149

def connect_signals(&block)
  connect_signals_raw do |*args|
    self.class.connect_signal(*args, &block)
  end
end

#connect_signals_rawObject


148
# File 'lib/gtk3/builder.rb', line 148

alias_method :connect_signals_raw, :connect_signals

#initialize_rawObject


64
# File 'lib/gtk3/builder.rb', line 64

alias_method :initialize_raw, :initialize