File: builder-from-resource.rb

package info (click to toggle)
ruby-gnome2 3.1.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,072 kB
  • ctags: 17,433
  • sloc: ansic: 93,621; ruby: 62,273; xml: 335; sh: 246; makefile: 25
file content (53 lines) | stat: -rwxr-xr-x 1,385 bytes parent folder | download | duplicates (5)
1
2
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
=begin
  builder_from_resource.rb - Ruby/GTK sample script.

  Copyright (c) 2015 Ruby-GNOME2 Project Team
  This program is licenced under the same licence as Ruby-GNOME2.
=end

require "gtk3"
require "fileutils"

current_path = File.expand_path(File.dirname(__FILE__))

# The gresource file name
gresource_bin = "#{current_path}/simple_window.gresource"

# The gresource xml file name
# see here https://developer.gnome.org/gio/stable/GResource.html
gresource_xml = "#{current_path}/simple_window.gresource.xml"

# Generate the resource file:
# see here https://developer.gnome.org/gio/stable/glib-compile-resources.html
Dir.chdir(File.dirname(gresource_xml)) do
  system("glib-compile-resources",
         "--target", gresource_bin,
         File.basename(gresource_xml))
end

at_exit do
  FileUtils.rm_f(gresource_bin)
end

resource = Gio::Resource.load(gresource_bin)
Gio::Resources.register(resource)

# Reference:
# https://developer.gnome.org/gtk3/stable/GtkBuilder.html
builder = Gtk::Builder.new(:resource => "/simple_window/simple_window.ui")

window = builder.get_object("window")
window.set_title "Build interface from resource"
window.set_default_size 300, 300

label = builder["label"]
label.text = "My UI was created with Glade"

window.show_all

window.signal_connect "destroy" do
  Gtk.main_quit
  Gio::Resources.unregister(resource)
end
Gtk.main