How to create GtkPixbuf from llike stock item (new_from_stock)?

I want to get a stock item to use in the tree view, but I cannot get it as pixbuf directly, since there is no new_from_stock method for pixbufs !!

+4
source share
4 answers

Say you want to get pixbuf from stock_item .

There are two ways:

First (simple):

 pixbuf = gtk_widget_render_icon ( widget, stock_item, size ) 

Second (hard):

You need to find it in the list of default icon factories:

 icon_set = gtk_style_lookup_icon_set ( style, stock_item ) 

OR

 icon_set = gtk_icon_factory_lookup_default ( $stock_item ) 

then check the available sizes with get_sizes . Check if the size you want is available or get the largest size that will be the last in the returned list. Then you need to display it to get pixbuf:

 pixbuf = gtk_icon_set_render_icon ( icon_set, style, direction, state, size, widget ) 

Then scale it to whatever size you want to use:

 gdk_pixbuf_scale_simple ( pixbuf, width, height, GdkInterpType ) 

Hope you got it

+4
source

Do you know the icon property in GtkCellRendererPixbuf? This should solve the problem of displaying the stock icon in the tree structure.

+2
source

A slightly easier (and more reliable version) ophidion answer is this:

 #define ICON_NAME "go-next" #define ICON_SIZE 96 /* pixels */ GError *error = NULL; GtkIconTheme *icon_theme = gtk_icon_theme_get_default(); GdkPixbuf *pixbuf = gtk_icon_theme_load_icon(icon_theme, ICON_NAME, ICON_SIZE, GTK_ICON_LOOKUP_USE_BUILTIN, &error); 

Assuming you don't get an error message, you can put the resulting GdkPixbuf in the treemodel or use it with GtkButton or whatever you would like to do with it.

As others have noted, GTK Stock Elements are likely to be deprecated , so you should use the names according to the Icon Naming Spec instead, if you want to be future proof. See this Google Docs table for equivalent icon specification names for GTK stock items.

0
source

You can use gtk_image_new_from_stock() , then get the raw pixbuf with gtk_image_get_pixbuf() .

But first check out @jku , which seems like the right solution. I just wanted to add how you would do it in any other context.

-one
source

Source: https://habr.com/ru/post/1498595/


All Articles