gtkgtk3lgi

How do I convert a symbolic GtkIconSize to an integer size in pixels?


I'm updating a GTK+ program, trying to replace all instances of the "stock icons" using "stock ids" to icons using the "icon-name" property. One place that I am a bit confused is how to get the size of an icon.

I have some code that uses the gtk_render_icon_pixbuf function to obtain a pixbuf from an image with a stock icon:

if image.storage_type == 'STOCK' then
    return image:render_icon_pixbuf(image.stock, image.icon_size)
end

Now I want to make the equivalent of this code for images that use icon_name instead of stock ids. I tried using the gtk_icon_theme_load_icon function:

if image.storage_type == 'ICON_NAME' then
   local icon_theme = Gtk.IconTheme.get_default()
   return icon_theme:load_icon(image.icon_name, image.icon_size, 0, nil)

However, the icon size comes out wrong (its always the smallest icon size available). I think its because the icon-size property is a symbolic value in the GtkIconSize enumeration while gtk_icon_theme_load_icon expects an icon size in pixels.

Is there a way to convert a GtkIconSize to a value in pixels?

Or maybe should I should specify my icon sizes with some other property instead of icon-size?


Solution

  • You can use the gtk_icon_size_lookup function to convert a symbolic icon size to a length in pixels.

    In lgi it can be called like this:

    local width, height = Gtk.icon_size_lookup(image.icon_size)