What is a resource path that have to be used with gtk_builder_new_from_resource?
Asked Answered
E

1

6

I understand the what are the arguments for the functions gtk_builder_new_from_file or gtk_builder_new_from_string but I struggle a little to see what is a resource path like:

GtkBuilder *
gtk_builder_new_from_resource (const gchar *resource_path);

I can not found any example (C, python, vala or other I don't mind).

Edit: Solution

Thanks to the help of gnianmt here is a basic example in ruby (https://github.com/ruby-gnome2/ruby-gnome2):

first a simple ui file simple_window.ui :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLabel" id="label">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">label</property>
        <property name="ellipsize">end</property>
      </object>
    </child>
  </object>
</interface>

Create then a simple_window.gresource.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/simple_window">
    <file>simple_window.ui</file>
  </gresource>
</gresources>

Package this with :

glib-compile-resources simple_window.gresource.xml

Which create a simple_window.gresource binary file.

Now the ruby script:

#!/usr/bin/env ruby

require "gtk3"

path = File.expand_path(File.dirname(__FILE__))
resource = Gio::Resource.load("#{path}/simple_window.gresource")
Gio::Resources.register(resource)
builder = Gtk::Builder.new(:resource => "/simple_window/simple_window.ui")
window = builder.get_object("window")
window.show_all
Gtk.main
Entwistle answered 18/5, 2015 at 16:46 Comment(1)
The documentation for the GLib.Resource class gives a lengthy description that might help.Counterman
M
5

The tag should actually be pygobject and not pygtk since the latter does not cover GTK+3, anyhow if you want an example of using a Glib.Resource from python you can have a look at gtk-demo, I've used the resource to hold the CSS data.

You prepare the resource by describing the location of each individual file contained in the resource with an XML representation: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demos/data/demo.gresource.xml

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/css_accordion">
    <file>css_accordion.css</file>
    <file>reset.css</file>
  </gresource>
  <gresource prefix="/css_basics">
    <file>css_basics.css</file>
    <file>reset.css</file>
  </gresource>
  <gresource prefix="/css_multiplebgs">
    <file>css_multiplebgs.css</file>
    <file>brick.png</file>
    <file>brick2.png</file>
    <file>cssview.css</file>
    <file>reset.css</file>
  </gresource>
</gresources>

The resource is then compiled using glib-compile-resources https://developer.gnome.org/gio/stable/glib-compile-resources.html

The resource can be loaded application wide: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demo.py#L117

base_path = os.path.abspath(os.path.dirname(__file__))
resource_path = os.path.join(base_path, 'demos/data/demo.gresource')
resource = Gio.Resource.load(resource_path)

then you can load each individual resource when needed: https://gitlab.gnome.org/GNOME/pygobject/-/blob/master/examples/demo/demos/Css/css_accordion.py#L48

bytes = Gio.resources_lookup_data("/css_accordion/css_accordion.css", 0)

I hope this help understanding how to use the resource, you would place the Builder resource in the same way I've placed the CSS files.

Musaceous answered 18/5, 2015 at 20:10 Comment(3)
Your informations are really usefull, I found this link too : blogs.gnome.org/alexl/2012/01/26/resources-in-glib . But I am still struggling with the use of gtk_builder_new_from_resource. For example if I use the files from the demo (I have recreated the demo.gresourcefile), I have this kind of error: Gtk-ERROR **: failed to add UI: Error at line ... The document must begin with an element ( ex. <book>).Entwistle
can you post a small example of the code that loads the resource? Remember that the string that you use in new_from_resource is the relative path to the inside resource, prefix+filenameMusaceous
Ok, I have find out to make it works, it is a builder so I have to provide a full ui file. I have edited my main post to show how it works. Thanks for your help.Entwistle

© 2022 - 2024 — McMap. All rights reserved.