Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 837
cedlemo Avatar asked Sep 27 '22 22:09

cedlemo


1 Answers

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.

like image 154
gianmt Avatar answered Oct 03 '22 03:10

gianmt