0

Context

I'm new to Vala development (although I have some years of experience with C#) under Linux, and I decided to recreate one of my C# programs, however, I need to use images in the UI.

My problem

How can I embed resource files (such as images) for later use in the UI? How to access them later? And how to put them in a button?

What I'm using

I'm using Linux (Mint) and Anjuta development IDE, with the Glade UI designer integrated. The Vala project targets a GTK+ 3.0 project.

What I've tried

I have tried adding a new specific objective for images, adding them into the project... But I don't seem to succeed. I have seen the default images provided by Linux, and those work just fine, but I need to add my own.

Thanks in advance!

Lonami
  • 5,945
  • 2
  • 20
  • 38

1 Answers1

0

The normal approach would be to install your images to PREFIX/share/pixmaps/YOUR_APP. For example Gnumeric installs some .png files in /usr/share/pixmaps/gnumeric/.

You can use GResource to embed binary files (like images) into your executable if you really want to. The glib-compile-resources command can be added to your build system (see also this question).

You can also use icons from the users icon theme.

You didn't write what component you want to use to display your images, so I'll assume Gtk.Image here.

Gtk.Image has several constructors for the purpose of loading the image:

  • from_icon_name loads the image from the current icon theme (which is useful to support user themes).
  • from_resource loads the image from an embedded GResource.
  • from_file loads the image from a file.

See the main documentation of Gtk.Image for more methods. Some other widgets have similar methods to load images (for example toolbar buttons).

You should not use from_stock any more (There was a stock system in Gtk+ that is being replaced by freedesktop.org icon schemas).

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • Sorry, I'm totally new to Linux development. Is there any way to automate the process of "installing" my images into `/usr/share/pixmaps/my_app/`? What would be the best way to develop both the images that will be copied there and the binary (although that should perhaps be another question!)? Also, thanks for your answer! – Lonami Jun 05 '16 at 18:29
  • I'm assuming you use autotools (which is the default when you create a Vala project from Anjuta). When you click "Build->Install Project (Shift + F7)" it will install everything mentioned in your `Makefile.am` files (which are processed by automake + autoconf). I suggest you do some research on autotools. A good resource is [this book](http://www.freesoftwaremagazine.com/books/autotools_a_guide_to_autoconf_automake_libtool). – Jens Mühlenhoff Jun 05 '16 at 18:33
  • BTW: The `GResource` process doesn't require you to install the binary every time you do a build, but it still needs modification to the automake file(s). – Jens Mühlenhoff Jun 05 '16 at 18:36
  • Yessss! Adding `cp ./src/img/my_image /usr/share/pixmaps/my_app/my_image` into the `Makefile.am` as `install: \n \t command` did the trick, thank you! – Lonami Jun 05 '16 at 18:57
  • I must say though, the designer, for some strange reason, doesn't show the image! But the running application does, so there's no real problem on that :) – Lonami Jun 05 '16 at 19:00