GtkIconTheme

GtkIconTheme — Looking up icons by name

Properties

GdkDisplay * display Read / Write
GStrv icon-names Read
GStrv resource-path Read / Write
GStrv search-path Read / Write
gchar * theme-name Read / Write

Signals

void changed Run Last

Object Hierarchy

    GObject
    ╰── GtkIconTheme

Includes

#include <gtk/gtk.h>

Description

GtkIconTheme provides a facility for looking up icons by name and size. The main reason for using a name rather than simply providing a filename is to allow different icons to be used depending on what “icon theme” is selected by the user. The operation of icon themes on Linux and Unix follows the Icon Theme Specification There is a fallback icon theme, named hicolor, where applications should install their icons, but additional icon themes can be installed as operating system vendors and users choose.

In many cases, named themes are used indirectly, via GtkImage rather than directly, but looking up icons directly is also simple. The GtkIconTheme object acts as a database of all the icons in the current theme. You can create new GtkIconTheme objects, but it’s much more efficient to use the standard icon theme of the GtkWidget so that the icon information is shared with other people looking up icons.

1
2
3
4
5
6
7
8
9
10
11
12
13
GtkIconTheme *icon_theme;
GtkIconPaintable *icon;
GdkPaintable *paintable;

icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (my_widget));
icon = gtk_icon_theme_lookup_icon (icon_theme,
                                   "my-icon-name", // icon name
                                   48, // icon size
                                   1,  // scale
                                   0,  // flags);
 paintable = GDK_PAINTABLE (icon);
 // Use the paintable
 g_object_unref (icon);

Functions

gtk_icon_theme_new ()

GtkIconTheme *
gtk_icon_theme_new (void);

Creates a new icon theme object. Icon theme objects are used to lookup up an icon by name in a particular icon theme. Usually, you’ll want to use gtk_icon_theme_get_for_display() rather than creating a new icon theme object for scratch.

Returns

the newly created GtkIconTheme object.


gtk_icon_theme_get_for_display ()

GtkIconTheme *
gtk_icon_theme_get_for_display (GdkDisplay *display);

Gets the icon theme object associated with display ; if this function has not previously been called for the given display, a new icon theme object will be created and associated with the display. Icon theme objects are fairly expensive to create, so using this function is usually a better choice than calling than gtk_icon_theme_new() and setting the display yourself; by using this function a single icon theme object will be shared between users.

Parameters

display

a GdkDisplay

 

Returns

A unique GtkIconTheme associated with the given display. This icon theme is associated with the display and can be used as long as the display is open. Do not ref or unref it.

[transfer none]


gtk_icon_theme_set_search_path ()

void
gtk_icon_theme_set_search_path (GtkIconTheme *self,
                                const char * const *path);

Sets the search path for the icon theme object. When looking for an icon theme, GTK will search for a subdirectory of one or more of the directories in path with the same name as the icon theme containing an index.theme file. (Themes from multiple of the path elements are combined to allow themes to be extended by adding icons in the user’s home directory.)

In addition if an icon found isn’t found either in the current icon theme or the default icon theme, and an image file with the right name is found directly in one of the elements of path , then that image will be used for the icon name. (This is legacy feature, and new icons should be put into the fallback icon theme, which is called hicolor, rather than directly on the icon path.)

Parameters

self

a GtkIconTheme

 

path

NULL-terminated array of directories that are searched for icon themes.

[array zero-terminated=1][element-type filename][nullable]

gtk_icon_theme_get_search_path ()

char **
gtk_icon_theme_get_search_path (GtkIconTheme *self);

Gets the current search path. See gtk_icon_theme_set_search_path().

Parameters

self

a GtkIconTheme

 

Returns

a list of icon theme path directories or NULL. The returned value should be freed with g_strfreev().

[transfer full][array zero-terminated=1][element-type filename][nullable]


gtk_icon_theme_add_search_path ()

void
gtk_icon_theme_add_search_path (GtkIconTheme *self,
                                const char *path);

Appends a directory to the search path. See gtk_icon_theme_set_search_path().

Parameters

self

a GtkIconTheme

 

path

directory name to append to the icon path.

[type filename]

gtk_icon_theme_set_resource_path ()

void
gtk_icon_theme_set_resource_path (GtkIconTheme *self,
                                  const char * const *path);

Sets the resource paths that will be looked at when looking for icons, similar to search paths.

The resources are considered as part of the hicolor icon theme and must be located in subdirectories that are defined in the hicolor icon theme, such as @path/16x16/actions/run.png. Icons that are directly placed in the resource path instead of a subdirectory are also considered as ultimate fallback.

Parameters

self

a GtkIconTheme

 

path

NULL-terminated array of resource paths that are searched for icons

 

gtk_icon_theme_get_resource_path ()

char **
gtk_icon_theme_get_resource_path (GtkIconTheme *self);

Gets the current resource path.

See gtk_icon_theme_set_resource_path().

Parameters

self

a GtkIconTheme

 

Returns

A list of resource paths or NULL. The returned value should be freed with g_strfreev().

[transfer full][array zero-terminated=1][element-type utf8][nullable]


gtk_icon_theme_add_resource_path ()

void
gtk_icon_theme_add_resource_path (GtkIconTheme *self,
                                  const char *path);

Adds a resource path that will be looked at when looking for icons, similar to search paths.

See gtk_icon_theme_set_resource_path().

This function should be used to make application-specific icons available as part of the icon theme.

Parameters

self

a GtkIconTheme

 

path

a resource path

 

gtk_icon_theme_set_theme_name ()

void
gtk_icon_theme_set_theme_name (GtkIconTheme *self,
                               const char *theme_name);

Sets the name of the icon theme that the GtkIconTheme object uses overriding system configuration. This function cannot be called on the icon theme objects returned from gtk_icon_theme_get_for_display().

Parameters

self

a GtkIconTheme

 

theme_name

name of icon theme to use instead of configured theme, or NULL to unset a previously set custom theme.

[allow-none]

gtk_icon_theme_get_theme_name ()

char *
gtk_icon_theme_get_theme_name (GtkIconTheme *self);

Gets the current icon theme name.

Returns (transfer full): the current icon theme name,

Parameters

self

a GtkIconTheme

 

gtk_icon_theme_has_icon ()

gboolean
gtk_icon_theme_has_icon (GtkIconTheme *self,
                         const gchar *icon_name);

Checks whether an icon theme includes an icon for a particular name.

Parameters

self

a GtkIconTheme

 

icon_name

the name of an icon

 

Returns

TRUE if self includes an icon for icon_name .


gtk_icon_theme_lookup_icon ()

GtkIconPaintable *
gtk_icon_theme_lookup_icon (GtkIconTheme *self,
                            const char *icon_name,
                            const char *fallbacks[],
                            gint size,
                            gint scale,
                            GtkTextDirection direction,
                            GtkIconLookupFlags flags);

Looks up a named icon for a desired size and window scale, returning a GtkIconPaintable. The icon can then be rendered by using it as a GdkPaintable, or you can get information such as the filename and size.

If the available icon_name is not available and fallbacks are provided, they will be tried in order.

If no matching icon is found, then a paintable that renders the "missing icon" icon is returned. If you need to do something else for missing icons you need to use gtk_icon_theme_has_icon().

Note that you probably want to listen for icon theme changes and update the icon. This is usually done by overriding the GtkWidgetClass.css-changed() function.

Parameters

self

a GtkIconTheme

 

icon_name

the name of the icon to lookup

 

fallbacks

.

[nullable][array zero-terminated=1]

size

desired icon size.

 

scale

the window scale this will be displayed on

 

direction

text direction the icon will be displayed in

 

flags

flags modifying the behavior of the icon lookup

 

Returns

a GtkIconPaintable object containing the icon.

[transfer full]


gtk_icon_theme_lookup_by_gicon ()

GtkIconPaintable *
gtk_icon_theme_lookup_by_gicon (GtkIconTheme *self,
                                GIcon *icon,
                                gint size,
                                gint scale,
                                GtkTextDirection direction,
                                GtkIconLookupFlags flags);

Looks up a icon for a desired size and window scale, returning a GtkIconPaintable. The icon can then be rendered by using it as a GdkPaintable, or you can get information such as the filename and size.

Parameters

self

a GtkIconTheme

 

icon

the GIcon to look up

 

size

desired icon size

 

scale

the desired scale

 

direction

text direction the icon will be displayed in

 

flags

flags modifying the behavior of the icon lookup

 

Returns

a GtkIconPaintable containing information about the icon. Unref with g_object_unref().

[transfer full]


gtk_icon_theme_get_icon_names ()

char **
gtk_icon_theme_get_icon_names (GtkIconTheme *self);

Lists the names of icons in the current icon theme.

Parameters

self

a GtkIconTheme

 

Returns

a string array holding the names of all the icons in the theme. You must free the array using g_strfreev().

[element-type utf8][transfer full]


gtk_icon_theme_get_icon_sizes ()

gint *
gtk_icon_theme_get_icon_sizes (GtkIconTheme *self,
                               const gchar *icon_name);

Returns an array of integers describing the sizes at which the icon is available without scaling. A size of -1 means that the icon is available in a scalable format. The array is zero-terminated.

Parameters

self

a GtkIconTheme

 

icon_name

the name of an icon

 

Returns

A newly allocated array describing the sizes at which the icon is available. The array should be freed with g_free() when it is no longer needed.

[array zero-terminated=1][transfer full]


gtk_icon_paintable_new_for_file ()

GtkIconPaintable *
gtk_icon_paintable_new_for_file (GFile *file,
                                 gint size,
                                 gint scale);

Creates a GtkIconPaintable for a file with a given size and scale GtkIconPaintable. The icon can then be rendered by using it as a GdkPaintable.

Parameters

file

a GFile

 

size

desired icon size

 

scale

the desired scale

 

Returns

a GtkIconPaintable containing for the icon. Unref with g_object_unref().

[transfer full]


gtk_icon_paintable_get_file ()

GFile *
gtk_icon_paintable_get_file (GtkIconPaintable *self);

Gets the GFile that was used to load the icon, or NULL if the icon was not loaded from a file.

Parameters

self

a GtkIconPaintable

 

Returns

the GFile for the icon, or NULL. Free with g_object_unref().

[nullable][transfer full]


gtk_icon_paintable_get_icon_name ()

const gchar *
gtk_icon_paintable_get_icon_name (GtkIconPaintable *self);

Get the icon name being used for this icon.

When an icon looked up in the icon theme was not available, the icon theme may use fallback icons - either those specified to gtk_icon_theme_lookup_icon() or the always-available "image-missing". The icon chosen is returned by this function.

If the icon was created without an icon theme, this function returns NULL.

Parameters

self

a GtkIconPaintable

 

Returns

the themed icon-name for the icon, or NULL if its not a themed icon.

[nullable][type filename]


gtk_icon_paintable_is_symbolic ()

gboolean
gtk_icon_paintable_is_symbolic (GtkIconPaintable *self);

Checks if the icon is symbolic or not. This currently uses only the file name and not the file contents for determining this. This behaviour may change in the future.

Parameters

self

a GtkIconPaintable

 

Returns

TRUE if the icon is symbolic, FALSE otherwise

Types and Values

GtkIconPaintable

typedef struct _GtkIconPaintable GtkIconPaintable;

Contains information found when looking up an icon in an icon theme and supports painting it as a GdkPaintable.


GtkIconTheme

typedef struct _GtkIconTheme GtkIconTheme;

Acts as a database of information about an icon theme. Normally, you retrieve the icon theme for a particular display using gtk_icon_theme_get_for_display() and it will contain information about current icon theme for that display, but you can also create a new GtkIconTheme object and set the icon theme name explicitly using gtk_icon_theme_set_theme_name().


enum GtkIconLookupFlags

Used to specify options for gtk_icon_theme_lookup_icon()

Members

GTK_ICON_LOOKUP_FORCE_REGULAR

Try to always load regular icons, even when symbolic icon names are given

 

GTK_ICON_LOOKUP_FORCE_SYMBOLIC

Try to always load symbolic icons, even when regular icon names are given

 

GTK_ICON_LOOKUP_PRELOAD

Starts loading the texture in the background so it is ready when later needed.

 

GTK_ICON_THEME_ERROR

#define GTK_ICON_THEME_ERROR gtk_icon_theme_error_quark ()

The GQuark used for GtkIconThemeError errors.


GTK_TYPE_ICON_THEME_ERROR

#define GTK_TYPE_ICON_THEME_ERROR (gtk_icon_theme_error_get_type ())

GTK_TYPE_ICON_LOOKUP_FLAGS

#define GTK_TYPE_ICON_LOOKUP_FLAGS (gtk_icon_lookup_flags_get_type ())

enum GtkIconThemeError

Error codes for GtkIconTheme operations.

Members

GTK_ICON_THEME_NOT_FOUND

The icon specified does not exist in the theme

 

GTK_ICON_THEME_FAILED

An unspecified error occurred.

 

Property Details

The “display” property

  “display”                  GdkDisplay *

The display that this icon theme object is attached to.

Owner: GtkIconTheme

Flags: Read / Write


The “icon-names” property

  “icon-names”               GStrv

The icon names that are supported by the icon theme.

Owner: GtkIconTheme

Flags: Read


The “resource-path” property

  “resource-path”            GStrv

Resource paths that will be looked at when looking for icons, similar to search paths.

The resources are considered as part of the hicolor icon theme and must be located in subdirectories that are defined in the hicolor icon theme, such as @path/16x16/actions/run.png. Icons that are directly placed in the resource path instead of a subdirectory are also considered as ultimate fallback.

Owner: GtkIconTheme

Flags: Read / Write


The “search-path” property

  “search-path”              GStrv

The search path for this icon theme.

When looking for icons, GTK will search for a subdirectory of one or more of the directories in the search path with the same name as the icon theme containing an index.theme file. (Themes from multiple of the path elements are combined to allow themes to be extended by adding icons in the user’s home directory.)

Owner: GtkIconTheme

Flags: Read / Write


The “theme-name” property

  “theme-name”               gchar *

The name of the icon theme that is being used.

Unless set to a different value, this will be the value of the “gtk-icon-theme-name” property of the GtkSettings object associated to the display of the icontheme object.

Owner: GtkIconTheme

Flags: Read / Write

Default value: NULL

Signal Details

The “changed” signal

void
user_function (GtkIconTheme *self,
               gpointer      user_data)

Emitted when the current icon theme is switched or GTK+ detects that a change has occurred in the contents of the current icon theme.

Parameters

self

the icon theme

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last