Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get icon name for a stock ID?

Tags:

gtk

Question

I'm trying to retrieve the (string) icon name(s) corresponding to a given (string) Gtk stock ID.

Example

Gtk stock id 'gtk-go-forward' has two corresponding icon names, 'gtk-go-forward-ltr' and 'gtk-go-forward-rtl'. I'd like to retrieve either:

  • both icon names 'gtk-go-forward-ltr' and 'gtk-go-forward-rtl' given the ID 'gtk-go-forward'; OR
  • just the icon name 'gtk-go-forward-ltr' (the "appropriate" one for my text direction).

Potential restrictions

I'm using the gobject introspection API, not the base C one.

Rationale (optional to read)

I'm trying to make a St.Icon for the icon specified by the stock ID, but at a largish size (96 px).

I can use gtk_icon_set_render_icon with the stock ID 'gtk-go-forward' directly like so (it picks the LTR version) (the syntax is GNOME javscript but this is irrelevant to the quesiton; I'm using the GObject introspection API):

let style = Gtk.Widget.get_default_style();
let iconset = style.lookup_icon_set('gtk-go-forward');
let icon = new St.Icon({
    gicon: iconset.render_icon(gtkStyle, Gtk.TextDirection.LTR,
                               Gtk.StateType.NORMAL, Gtk.IconSize.DIALOG,
                               null, null),
    icon_type: St.IconType.FULLCOLOR,
    icon_size: 96
});

This will give me an icon for stock ID 'gtk-go-forward' (it picks 'gtk-go-forward-ltr'), but the icon is very fuzzy.

Alternatively if I feed in the icon name 'gtk-go-forward-ltr' directly, the icon is not fuzzy:

let icon = new St.Icon({
    gicon: Gio.ThemedIcon.new_with_default_fallbacks('gtk-go-forward-ltr'),
    icon_type: St.IconType.FULLCOLOR,
    icon_size: 96
});

So in summary, I think that my best bet at getting a large, non-fuzzy icon is to find the icon name for the stock ID, rather than using the stock ID (feeding gtk-go-forward to the second method above produces the "missing icon" icon as it is not a valid icon name).

Icon using stock ID (top); using icon name (bottom)

In the picture above the top icon uses the first method (stock ID with render_icon) and the bottom icon uses the second method (use the icon name directly). Big difference in clarity.

like image 884
mathematical.coffee Avatar asked Nov 12 '12 23:11

mathematical.coffee


1 Answers

General Stock Icon Names

This question is a little old now, but for future reference: it's best to use "go-next" rather than "gtk-go-forward" -- this is the correct name according to the Icon Naming Spec, and recommended since GTK developers are planning on deprecating stock icons shortly.

To directly answer the question as stated in the title: here is a handy Google Docs spreadsheet giving the equivalent icon spec names for GTK stock icons.

Non-fuzzy, large versions of GTK Icons

As to getting non-fuzzy versions of default icons, the best way I've found to render an icon at 96px in GTK is to load it into a GdkPixbuf, and then use that rather than a GtkIcon (which is limited to 48px, that being the largest GtkIconSize, as j__m pointed out). An example of how to do this in Vala (easily converted to JS or Python etc):

var icon_theme = Gtk.IconTheme.get_default();
try {
    var pixbuf = icon_theme.load_icon("go-next", 96, Gtk.IconLookupFlags.USE_BUILTIN);
    // Do something with the pixbuf, for example use it with a CellRendererPixbuf,
    // or a GtkButton or whatever
catch (GLib.Error e) {
    GLib.critical("Could not load icon: %s", e.message);
}

You might also want to experiment with Gtk.IconLookupFlags.FORCE_SIZE and/or FORCE_SVG, particularly if you want really huge icons.

I'm afraid I've never use the Shell Toolkit, so I don't know whether you particularly need an St.Icon, but in plain GTK you can use a GdkPixbuf pretty much anywhere you can use a stock icon so this is a workaround I've found very useful.

like image 72
Tristan Brindle Avatar answered Oct 06 '22 01:10

Tristan Brindle