gtk.IconTheme

gtk.IconTheme — look up icons by name and size (new in PyGTK 2.4)

Synopsis

class gtk.IconTheme(gobject.GObject):
    gtk.IconTheme()
def set_screen(screen)
def set_search_path(path)
def get_search_path()
def append_search_path(path)
def prepend_search_path(path)
def set_custom_theme(theme_name)
def has_icon(icon_name)
def lookup_icon(icon_name, size, flags)
def load_icon(icon_name, size, flags)
def list_icons(context=None)
def get_example_icon_name()
def rescan_if_needed()
def get_icon_sizes(icon_name)
Functions

    def gtk.icon_theme_get_default()
def gtk.icon_theme_get_for_screen(screen)
def gtk.icon_theme_add_builtin_icon(icon_name, size, pixbuf)

Ancestry

+-- gobject.GObject
  +-- gtk.IconTheme

gtk.IconTheme Signal Prototypes

gobject.GObject Signal Prototypes

"changed" def callback(icontheme, user_param1, ...)

Description

Note

This object is available in PyGTK 2.4 and above.

gtk.IconTheme 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 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 facility (see gtk.IconFactory, gtk.IconSet and gtk.IconSource for more detail), 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, 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 standard icon sizes (see the gtk.icon_size_lookup() function), or by custom sizes defined by the gtk.icon_size_register() function, 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 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 gtk.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.icon_theme_get_default()
  try:
      pixbuf = icon_theme.load_icon("my-icon-name", 48, 0)
  except gobject.GError, exc:
      print "can't load icon", exc

Constructor

    gtk.IconTheme()
Returns :the newly created gtk.IconTheme object.

Note

This constructor is available in PyGTK 2.4 and above.

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 the gtk.icon_theme_get_default() or gtk.icon_theme_get_for_screen() functions rather than creating a new icon theme object from scratch.

Methods

gtk.IconTheme.set_screen

    def set_screen(screen)
screen :a gtk.gdk.Screen

Note

This method is available in PyGTK 2.4 and above.

The set_screen() method sets the gtk.gdk.Screen for an icon theme to screen. The screen is used to track the user's currently configured icon theme, which might be different for different screens.

gtk.IconTheme.set_search_path

    def set_search_path(path)
path :a list or tuple of directories that are searched for icon themes

Note

This method is available in PyGTK 2.4 and above.

The set_search_path() method sets the search path for the icon theme object to the list of directory names contained in path. 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 a legacy feature, and new icons should be put into the default icon theme, which is called "hicolor", rather than directly on the icon path.)

gtk.IconTheme.get_search_path

    def get_search_path()
Returns :a tuple containing the list of directory names that are searched for icon themes

Note

This method is available in PyGTK 2.4 and above.

The get_search_path() method returns a tuple containing the current search path. See the set_search_path() method for more details.

gtk.IconTheme.append_search_path

    def append_search_path(path)
path :a directory name to append to the icon path

Note

This method is available in PyGTK 2.4 and above.

The append_search_path() method appends the directory name specified by path to the search path. See the set_search_path() method for more information.

gtk.IconTheme.prepend_search_path

    def prepend_search_path(path)
path :a directory name to prepend to the icon path

Note

This method is available in PyGTK 2.4 and above.

The prepend_search_path() method prepends the directory name specified by path to the search path. See the set_search_path() method for more information.

gtk.IconTheme.set_custom_theme

    def set_custom_theme(theme_name)
theme_name :the name of icon theme to use instead of the configured theme

Note

This method is available in PyGTK 2.4 and above.

The set_custom_theme() method sets the theme used by the icon theme object to the theme named by theme_name usually replacing system configured theme. This method cannot be called on the icon theme objects returned from the gtk.icon_theme_get_default() and gtk.icon_theme_get_default() functions.

gtk.IconTheme.has_icon

    def has_icon(icon_name)
icon_name :the name of an icon
Returns :TRUE if the icon theme includes an icon for icon_name.

Note

This method is available in PyGTK 2.4 and above.

The has_icon() method checks whether the icon theme includes an icon for the icon name specified by icon_name.

gtk.IconTheme.lookup_icon

    def lookup_icon(icon_name, size, flags)
icon_name :the name of the icon to lookup
size :the desired icon size
flags :the flags modifying the behavior of the icon lookup
Returns :a gtk.IconInfo object containing information about the icon, or None if the icon wasn't found. Free with the gtk.IconInfo.free() method

Note

This method is available in PyGTK 2.4 and above.

The lookup_icon() method looks up the icon specified by icon_name, size and flags and returns a gtk.IconInfo object containing information such as the filename of the icon. The icon can then be rendered into a pixbuf using the gtk.IconInfo.load_icon() method. (the load_icon() method combines these two steps if all you need is the pixbuf.)

The value of flags must be a combination of:

gtk.ICON_LOOKUP_NO_SVGNever return Scalable Vector Graphics (SVG) icons, even if gdk-pixbuf supports them. Cannot be used together with gtk.ICON_LOOKUP_FORCE_SVG.
gtk.ICON_LOOKUP_FORCE_SVG Return SVG icons, even if gdk-pixbuf doesn't support them. Cannot be used together with gtk.ICON_LOOKUP_NO_SVG.
gtk.ICON_LOOKUP_USE_BUILTINWhen passed to the lookup_icon() method includes builtin icons as well as files. For a builtin icon, the gtk.IconInfo.get_filename() method returns None and you need to call the get_builtin_pixbuf() method.

gtk.IconTheme.load_icon

    def load_icon(icon_name, size, flags)
icon_name :the name of the icon to lookup
size :the desired icon size. The resulting icon may not be exactly this size; see the gtk.IconInfo.load_icon() method.
flags :the flags modifying the behavior of the icon lookup
Returns :a gtk.gdk.Pixbuf containing the rendered icon; this may be a newly created icon or a new reference to an internal icon, so you must not modify the icon.

Note

This method is available in PyGTK 2.4 and above.

The load_icon() method looks up the icon specified by icon_name in an icon theme, scales it to the size specified by size and renders it into a gtk.gdk.Pixbuf. flags is a combination of: gtk.ICON_LOOKUP_FORCE_SVG, gtk.ICON_LOOKUP_NO_SVG and gtk.ICON_LOOKUP_USE_BUILTIN (see the lookup_icon() method for more details). Since this is a convenience function, if more details about the icon are needed, use the lookup_icon() method followed by the GtkIconInfo.load_icon() method.

This method raise the GError exception if an error occurs during rendering of the icon.

gtk.IconTheme.list_icons

    def list_icons(context=None)
context :a string identifying a particular type of icon, or None to list all icons.
Returns :a tuple containing the names of all the icons in the theme.

Note

This method is available in PyGTK 2.4 and above.

The list_icons() method lists the icons in the current icon theme. Only a subset of the icons can be listed by providing a string specified by context. The set of values for the context string is system dependent, but will typically include such values as 'apps' and 'mimetypes'.

gtk.IconTheme.get_example_icon_name

    def get_example_icon_name()
Returns :the name of an example icon or None.

Note

This method is available in PyGTK 2.4 and above.

The get_example_icon_name() method returns 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.)

gtk.IconTheme.rescan_if_needed

    def rescan_if_needed()
Returns :TRUE if the icon theme has changed and needed to be reloaded.

Note

This method is available in PyGTK 2.4 and above.

The rescan_if_needed() method checks to see if the icon theme has changed; if it has, any currently cached information is discarded and will be reloaded next time the icon theme is accessed.

gtk.IconTheme.get_icon_sizes

    def get_icon_sizes(icon_name)
icon_name :the name of an icon
Returns :a tuple containing the sizes that the icon is available in.

Note

This method is available in PyGTK 2.6 and above.

The get_icon_sizes() method returns a tuple containing the sizes available for the icon named by icon_name. A size of -1 means the icon is scalable. If the icon is not found an empty tuple is returned.

Functions

gtk.icon_theme_get_default

    def gtk.icon_theme_get_default()
Returns :A unique gtk.IconTheme associated with the default gtk.gdk.Screen. This icon theme is associated with the screen and can be used as long as the screen is open.

Note

This function is available in PyGTK 2.4 and above.

The gtk.icon_them_get_default() function returns the icon theme for the default screen. See the gtk.icon_theme_get_for_screen() function.

gtk.icon_theme_get_for_screen

    def gtk.icon_theme_get_for_screen(screen)
screen :a gtk.gdk.Screen object
Returns :A unique gtk.IconTheme associated with the default gtk.gdk.Screen. This icon theme is associated with the screen and can be used as long as the screen is open.

Note

This function is available in PyGTK 2.4 and above.

The gtk.icon_theme_get_for_screen() function returns the icon theme object associated with the gtk.gdk.Screen specified by 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 function is usually a better choice than calling the gtk.IconTheme() constructor and setting the screen yourself; by using this function a single icon theme object will be shared between users.

gtk.icon_theme_add_builtin_icon

    def gtk.icon_theme_add_builtin_icon(icon_name, size, pixbuf)
icon_name : the name of the icon to register
size : the size at which to register the icon (different images can be registered for the same icon name at different sizes.)
pixbuf :a gtk.gdk.Pixbuf that contains the image to use for icon_name.
Returns :

Note

This function is available in PyGTK 2.4 and above.

The gtk.icon_theme_add_builtin_icon() function registers a built-in icon for icon theme lookups using icon_name as the icon name, size as the icon size and pixbuf as the icon image. The idea of built-in icons is to allow an application or library that uses themed icons to function without requiring specific icon 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.icon_theme_add_builtin_icon() you should also install the icon in the icon theme, so that the icon is generally available. This function will generally be used with pixbufs loaded via the gtk.gdk.pixbuf_new_from_inline() function.

Signals

The "changed" gtk.IconTheme Signal

    def callback(icontheme, user_param1, ...)
icontheme :the icontheme that received the signal
user_param1 :the first user parameter (if any) specified with the connect() method
... :additional user parameters (if any)

Note

This signal is available in PyGTK 2.4 and above.

The "changed" signal is emitted when the current icon theme is switched or GTK+ detects that a change has occurred in the contents of the current icon theme.