Class: Gtk3assist::Builder

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

Overview

Helper class for Gtk::Builder which can do automatic signal-connect by scanning the Xml-file and more.

Constant Summary collapse

ARGS_ALLOWED =

Array containing allowed arguments for contructor.

[:builder]

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Builder

Constructor.



7
8
9
10
11
12
13
14
# File 'lib/gtk3assist_builder.rb', line 7

def initialize(args = {})
  raise "'args' was not a hash." if !args.is_a?(Hash)
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ARGS_ALLOWED.include?(key)
  end
  
  @builder = args[:builder]
end

Instance Method Details

#[](key) ⇒ Object

Returns a widget from a given key.



27
28
29
30
31
# File 'lib/gtk3assist_builder.rb', line 27

def [](key)
  obj = @builder.get_object(key)
  raise "No object by that name: '#{key}'." if !obj
  return obj
end

#add_from_file(fpath) ⇒ Object

Adds the given filepath to the builder and sets filepath for signal-connects.



17
18
19
20
21
22
23
24
# File 'lib/gtk3assist_builder.rb', line 17

def add_from_file(fpath)
  @fpath = fpath
  
  @builder = Gtk::Builder.new if !@builder
  @builder.add_from_file(fpath)
  
  return self
end

#connect_signals(&blk) ⇒ Object

Connects all signals to methods given by the supplied block.



34
35
36
37
38
39
40
# File 'lib/gtk3assist_builder.rb', line 34

def connect_signals(&blk)
  str = File.read(@fpath)
  
  require "xmlsimple"
  data = XmlSimple.xml_in(str)
  connect_signals_from_filepath_helper(data, blk)
end