class Gtk::IconTheme

Overview

Gtk::IconTheme provides a facility for loading themed icons.

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 Gtk::Image rather than directly, but looking up icons directly is also simple. The Gtk::IconTheme object acts as a database of all the icons in the current theme. You can create new Gtk::IconTheme objects, but it’s much more efficient to use the standard icon theme of the Gtk::Widget so that the icon information is shared with other people looking up icons.

WARNING ⚠️ The following code is in c ⚠️

Gtk::IconTheme *icon_theme;
Gtk::IconPaintable *icon;
Gdk::Paintable *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);

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/icon_theme.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new : self #

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::IconTheme#for_display rather than creating a new icon theme object for scratch.


[View source]
def self.new(*, display : Gdk::Display | Nil = nil, icon_names : Enumerable(String) | Nil = nil, resource_path : Enumerable(String) | Nil = nil, search_path : Enumerable(String) | Nil = nil, theme_name : String | Nil = nil) #

[View source]

Class Method Detail

def self.for_display(display : Gdk::Display) : Gtk::IconTheme #

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 Gtk::IconTheme.new and setting the display yourself; by using this function a single icon theme object will be shared between users.


[View source]
def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def add_resource_path(path : String) : Nil #

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

See Gtk::IconTheme#resource_path=.

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


[View source]
def add_search_path(path : String) : Nil #

Appends a directory to the search path.

See Gtk::IconTheme#search_path=.


[View source]
def changed_signal #

[View source]
def display : Gdk::Display | Nil #

Returns the display that the Gtk::IconTheme object was created for.


[View source]
def display=(value : Gdk::Display | Nil) : Gdk::Display | Nil #

[View source]
def has_gicon(gicon : Gio::Icon) : Bool #

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


[View source]
def has_icon(icon_name : String) : Bool #

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


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def icon_names : Enumerable(String) #

Lists the names of icons in the current icon theme.


[View source]
def icon_sizes(icon_name : String) : Enumerable(Int32) #

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.


[View source]
def lookup_by_gicon(icon : Gio::Icon, size : Int32, scale : Int32, direction : Gtk::TextDirection, flags : Gtk::IconLookupFlags) : Gtk::IconPaintable #

Looks up a icon for a desired size and window scale.

The icon can then be rendered by using it as a Gdk::Paintable, or you can get information such as the filename and size.


[View source]
def lookup_icon(icon_name : String, fallbacks : Enumerable(String) | Nil, size : Int32, scale : Int32, direction : Gtk::TextDirection, flags : Gtk::IconLookupFlags) : Gtk::IconPaintable #

Looks up a named icon for a desired size and window scale, returning a Gtk::IconPaintable.

The icon can then be rendered by using it as a Gdk::Paintable, 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::IconTheme#has_icon.

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


[View source]
def resource_path : Enumerable(String) | Nil #

Gets the current resource path.

See Gtk::IconTheme#resource_path=.


[View source]
def resource_path=(value : Enumerable(String)) : Enumerable(String) #

[View source]
def resource_path=(path : Enumerable(String) | Nil) : Nil #

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 or @path/scalable/actions/run.svg.

Icons that are directly placed in the resource path instead of a subdirectory are also considered as ultimate fallback, but they are treated like unthemed icons.


[View source]
def search_path : Enumerable(String) | Nil #

Gets the current search path.

See Gtk::IconTheme#search_path=.


[View source]
def search_path=(value : Enumerable(String)) : Enumerable(String) #

[View source]
def search_path=(path : Enumerable(String) | Nil) : Nil #

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.)


[View source]
def theme_name : String #

Gets the current icon theme name.

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


[View source]
def theme_name=(value : String) : String #

[View source]
def theme_name=(value : Nil) : Nil #

Set #theme_name property to nil.


[View source]
def theme_name=(theme_name : String | Nil) : Nil #

Sets the name of the icon theme that the Gtk::IconTheme object uses overriding system configuration.

This function cannot be called on the icon theme objects returned from Gtk::IconTheme#for_display.


[View source]
def theme_name? : String | Nil #

Same as #theme_name, but can return nil.


[View source]