Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

Gtk::IconTheme

class Gtk::IconTheme

Gtk::IconTheme provides a facility for looking up icons by name and size. It acts as a database of information about an icon theme. 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 selecetd by the user. The operation of icon themes on Linux and Unix follows the Icon Theme Specification. There is a default icon theme, named hicolor where applications should install their icons, but more additional application themes can be installed as operating system vendors and users choose.

Named icons are similar to the Themeable Stock Images(Gtk::IconSource, Gtk::IconFactory, Gtk::IconSet) facility, and the distinction between the two may be a bit confusing. A few things to keep in mind:

  • Stock images usually are used in conjunction with Stock Items(Gtk::Stock), such as Gtk::Stock::OK or Gtk:Stock::OPEN. Named icons are easier to set up and therefore are more useful for new icons that an application wants to add, such as application icons or window icons.
  • Stock images can only be loaded at the symbolic sizes defined by the Gtk::IconSize constants(Gtk::IconSize::IconSize), or by custom sizes defined by Gtk::IconSize.register, while named icons are more flexible and any pixel size can be specified.
  • Because stock images are closely tied to stock items, and thus to actions in the user interface, stock images may come in multiple variants for different widget states or writing directions.

A good rule of thumb is that if there is a stock image for what you want to use, use it, otherwise use a named icon. It turns out that internally stock images are generally defined in terms of one or more named icons. (An example of the more than one case is icons that depend on writing direction; Gtk::Stock::GO_FORWARD uses the two themed icons "gtk-stock-go-forward-ltr" and "gtk-stock-go-forward-rtl".)

In many cases, named themes are used indirectly, via Gtk::Image or stock items, 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 its much more efficient to use the standard icon theme for the Gdk::Screen so that the icon information is shared with other people looking up icons. In the case where the default screen is being used, looking up an icon can be as simple as:

icon_theme = Gtk::IconTheme.default
begin
  pixbuf = icon_theme.load_icon("gtk-stock-quit",                  # icon name
                                 48,                               # size
                                 Gtk::IconTheme::LOOKUP_FORCE_SVG) # flags
rescue RuntimeError => e
  puts "Couldn't load icon: #{e.message}"
end

Class Methods

Gtk::IconTheme.new
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.default or Gtk::IconTheme.get_for_screen rather than creating a new icon theme object for scratch. Since 2.4
Gtk::IconTheme.default
Gets the icon theme for the default screen. See Gtk::IconThem.get_for_screen?. Since 2.4
  • Returns: A unique Gtk::IconTheme associated with the default screen. This icon theme is associated with the screen and can be used as long as the screen is open.
Gtk::IconTheme.get_for_screen(screen)
Gets the icon theme object associated with screen; if this function has not previously been called for the given screen, a new icon theme object will be created and associated with the screen. Icon theme objects are fairly expensive to create, so using this method is usually a better choice than calling than Gtk::IconTheme.new and setting the screen yourself; by using this method a single icon theme object will be shared between users. Since 2.4
  • screen: a Gdk::Screen
  • Returns: A unique Gtk::IconTheme associated with the given screen. This icon theme is associated with the screen and can be used as long as the screen is open
Gtk::IconTheme.add_builtin_icon(icon_name, size, pixbuf)
Registers a built-in icon for icon theme lookups. The idea of built-in icons is to allow an application or library that uses themed icons to function requiring files to be present in the file system. For instance, the default images for all of GTK+'s stock icons are registered as built-icons. In general, if you use Gtk::IconTheme.add_builtin_icon you should also install the icon in the icon theme, so that the icon is generally available. This method will generally be used with pixbufs loaded via Gdk::Pixbuf.new(data, copy_pixels). Since 2.4
  • size: the size at which to register the icon (different images can be registered for the same icon name at different sizes.)
  • pixbuf: Gdk::Pixbuf that contains the image to use for icon_name.
  • Returns: self

Instance Methods

screen=(screen)
Sets the screen for an icon theme; the screen is used to track the user's currently configured icon theme, which might be different for different screens. Since 2.4
set_screen(screen)
Same as Gtk::IconTheme#screen=. Since 2.4
search_path
Gets the current search path. See Gtk::IconTheme#search_path=. Since 2.4
  • Returns: an Array of icon theme path directories(String).
search_path=(paths)
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. (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 default icon theme, which is called DEFAULT_THEME_NAME, rather than directly on the icon path.) Since 2.4
  • paths: an Array of directories(String) that are searched for icon themes
  • Returns: paths
set_search_path(paths)
Same as Gtk::IconTheme#search_path=. Since 2.4
  • paths: an Array of directories(String) that are searched for icon themes
  • Returns: self
append_search_path(path)
Appends a directory to the search path. See Gtk::IconTheme#search_path=. Since 2.4
  • path: A directory name(String) to append to the icon path
  • Returns: self
prepend_search_path(path)
Prepends a directory to the search path. See Gtk::IconTheme#search_path=. Since 2.4
  • path: A directory name(String) to append to the icon path
  • Returns: self
custom_theme=(theme_name)
Sets the name of the icon theme that the Gtk::IconTheme object uses overriding system configuration. This method cannot be called on the icon theme objects returned from Gtk::IconTheme.default and Gtk::IconTheme.get_for_screen(screen). Since 2.4
  • theme_name: name of icon theme to use instead of configured theme or nil to cancel the custom theme
  • Returns: theme_name
set_custom_theme(theme_name)
Same as Gtk::IconTheme#custom_theme=. Since 2.4
  • theme_name: name of icon theme to use instead of configured theme or nil to cancel the custom theme
  • Returns: self
has_icon?(icon_name)
Checks whether an icon theme includes an icon for a particular name. Since 2.4
  • icon_name: the name of an icon
  • Returns: true if icon_theme includes an icon for icon_name.
lookup_icon(icon_name, size, flags)
Looks up a named icon and returns a Gtk::IconInfo. The icon can then be rendered into a pixbuf using Gtk::IconInfo#load_icon. (Gtk::IconTheme#load_icon combines these two steps if all you need is the pixbuf.) Since 2.4
  • icon_name: the name of the icon to lookup
  • size: desired icon size
  • flags: flags modifying the behavior of the icon lookup GtkIconLookupFlags
  • Returns: a Gtk::IconInfo containing information about the icon, or nil if the icon wasn't found.
load_icon(icon_name, flags)
Looks up an icon in an icon theme, scales it to the given size and renders it into a pixbuf. This is a convenience method; if more details about the icon are needed, use Gtk::IconTheme#lookup_icon followed by Gtk::IconInfo#load_icon. Gtk::IconThemeError is raised if failure. Since 2.4
  • icon_name: the name of the icon to lookup
  • size: the desired icon size. The resulting icon may not be exactly this size; see Gtk::IconInfo#load_icon.
  • flags: flags modifying the behavior of the icon lookup GtkIconLookupFlags
  • Returns: the rendered icon or nil; this may be a newly created icon or a new reference to an internal icon, so you must not modify the icon.
icons(context = nil)
Lists the icons in the current icon theme. Only a subset of the icons can be listed by providing a context string. The set of values for the context string is system dependent, but will typically include such values as 'apps' and 'mimetypes'. Since 2.4
  • context: a String identifying a particular type of icon, or nil to list all icons.
  • Returns: an Array holding the names of all the icons in the theme.
get_icon_sizes(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. Since 2.6
  • icon_name: the name of an icon
  • Returns: An Array describing the sizes at which the icon is available.
example_icon_name
Gets the name of an icon that is representative of the current theme (for instance, to use when presenting a list of themes to the user.) Since 2.4
  • Returns: the name of an example icon or nil
rescan_if_needed
Checks to see if the icon theme has changed; if it has, any currently cached information is discarded and will be reloaded next time icon_theme is accessed. Since 2.4
  • Returns: true if the icon theme has changed and needed to be reloaded.
choose_icon
  • Returns: self: FIXME
contexts
  • Returns: self: FIXME

Constants

GtkIconLookupFlags

Used to specify options for Gtk::IconTheme#lookup_icon. Since 2.4

LOOKUP_NO_SVG
Never return SVG icons, even if gdk-pixbuf supports them. Cannot be used together with Gtk::IconTheme::LOOKUP_FORCE_SVG. Since 2.4
LOOKUP_FORCE_SVG
Return SVG icons, even if gdk-pixbuf doesn't support them. Cannot be used together with Gtk::IconTheme::LOOKUP_NO_SVG. Since 2.4
LOOKUP_USE_BUILTIN
When passed to Gtk::IconTheme#lookup_icon includes builtin icons as well as files. For a builtin icon, Gtk::IconInfo#filename returns nil and you need to call Gtk::IconInfo#builtin_pixbuf. Since 2.4
LOOKUP_GENERIC_FALLBACK
LOOKUP_FORCE_SIZE

Signals

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

ChangeLog