Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

Gtk::Image

class Gtk::Image

<URL:http://library.gnome.org/devel/gtk/stable/image.png>

The Gtk::Image widget displays an image. Various kinds of object can be displayed as an image; most typically, you would load a Gdk::Pixbuf ("pixel buffer") from a file, and then display that. There's a convenience function to do this, Gtk::Image.new, used as follows:

image = Gtk::Image.new("myfile.png")

If the file isn't loaded successfully, the image will contain a "broken image" icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with Gdk::Pixbuf.new, then create the Gtk::Image with Gtk::Image.new(Gdk::Pixbuf), using Gdk::Pixbuf as the given input.

The image file may contain an animation. If this is the case, then Gtk::Image will display this animation (Gdk::PixbufAnimation) instead of a static image.

Gtk::Image is a subclass of Gtk::Misc, which implies that you can align it (center, left, right) and add padding to it, using Gtk::Misc methods.

Gtk::Image is a "no window" widget (has no Gdk::Window of its own), so by default does not receive events. If you want to receive events on the image, such as button clicks, place the image inside a Gtk::EventBox, then connect to the event signals on the event box.

Example 2. Handling button press events on a Gtk::Image.

image = Gtk::Image.new("myfile.png")
event_box = Gtk::EventBox.new.add(image)
event_box.signal_connect("button_press_event") do
  puts "Clicked."
end

When handling events on the event box, keep in mind that coordinates in the image may be different from event box coordinates due to the alignment and padding settings on the image (see Gtk::Misc). The simplest way to solve this is to set the alignment to 0.0 (left/top), and set the padding to zero. Then the origin of the image will be the same as the origin of the event box.

Class Methods

Gtk::Image.new(value = nil)
Creates a new Gtk::Image.
  • value
    • nil - Creates a new empty Gtk::Image.
    • filename(String) - Creates a new Gtk::Image displaying the file filename. If the file isn't found or can't be loaded, the resulting Gtk::Image will display a "broken image" icon. This function never returns nil, it always returns a valid Gtk::Image widget. If the file contains an animation, the image will contain an animation. If you need to detect failures to load the file, use Gdk::Pixbuf.new(filename) to load the file yourself, then create the Gtk::Image from the pixbuf. (Or for animations, use Gdk::PixbufAnimation.new(filenam. The storage type (Gtk::Image#storage_type) of the returned image is not defined, it will be whatever is appropriate for displaying the file.
    • Gdk::Pixbuf - Creates a new Gtk::Image from Gdk::Pixbuf. This function just creates an Gtk::Image. The Gtk::Image created will not react to state changes. Should you want that, you should use Gtk::IconSet.
    • Gdk::PixbufAnimation - Creates a Gtk::Image displaying the given animation. The Gtk::Image does not assume a reference to the animation; you still need to unref it if you own references. Gtk::Image will add its own reference rather than adopting yours.
Gtk::Image.new(stock_id, size)
Creates a Gtk::Image displaying a stock icon. Sample stock icon names are Gtk::Stock::OPEN, Gtk::Stock::EXIT. Sample stock sizes are Gtk::IconSize::MENU, Gtk::IconSize::SMALL_TOOLBAR. If the stock icon name isn't known, a "broken image" icon will be displayed instead. You can register your own stock icon names, see Gtk::IconFactory#add_default and Gtk::IconFactory#add.
Gtk::Image.new(image, mask)
Creates a Gtk::Image widget displaying a image with a mask. A Gdk::Image is a client-side image buffer in the pixel format of the current display. The Gtk::Image does not assume a reference to the image or mask; you still need to unref them if you own references. Gtk::Image will add its own reference rather than adopting yours.
Gtk::Image.new(pixmap, mask)
Creates a Gtk::Image widget displaying pixmap with a mask. A Gdk::Image is a server-side image buffer in the pixel format of the current display. The Gtk::Image does not assume a reference to the pixmap or mask; you still need to unref them if you own references. Gtk::Image will add its own reference rather than adopting yours.
Gtk::Image.new(iconset, size)
Creates a Gtk::Image displaying an icon set. Sample stock sizes are Gtk::IconSize::MENU, Gtk::IconSize::SMALL_TOOLBAR. Instead of using this method, usually it's better to create a Gtk::IconFactory, put your icon sets in the icon factory, add the icon factory to the list of default factories with Gtk::IconFactory#add_default, and then use Gtk::Image.new(stock_id, size). This will allow themes to override the icon you ship with your application. The Gtk::Image does not assume a reference to the icon set; you still need to unref it if you own references. Gtk::Image will add its own reference rather than adopting yours.
Gtk::Image.new(iconname, size)
Creates a new Gtk::Image displaying an icon from the current icon theme. If the icon name isn't known, a "broken image" icon will be displayed instead. If the current icon theme is changed, the icon will be updated appropriately. Since 2.6
  • iconname: icon name (String)
  • size: size (GtkIconSize - the constants of Gtk::IconSize)

Instance Methods

icon_set
Gets the icon set being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::ICON_SET (see Gtk::Image#storage_type).
icon_set=(iconset)
Sets the icon set.
set_icon_set(iconset)
Same as Gtk::Image#icon_set=
icon_size
Gets the icon size being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::ICON_SET (see Gtk::Image#storage_type).
  • Returns: icon size (GtkIconSize - the constants of Gtk::IconSize)
icon_size=(size)
Sets the icon size.
  • size: a new icon size (GtkIconSize? - the constants of Gtk::IconSize)
  • Returns: icon size
set_icon_size(size)
Same as Gtk::Image#icon_size=.
  • size: a new icon size (GtkIconSize? - the constants of Gtk::IconSize)
  • Returns: self
image
Gets the Gdk::Image being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::IMAGE (see Gtk::Image::storage_type). The caller of this function does not own a reference to the returned image and mask.
image=(image)
Sets the image.
  • image: the new image (Gdk::Image).
  • Returns: image
set_image(image)
Same as Gtk::Image#image=.
  • image: the new image (Gdk::Image).
  • Returns: self
pixbuf
Gets the Gdk::Pixbuf being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::PIXBUF (see Gtk::Image#storage_type). The caller of this function does not own a reference to the returned pixbuf.
pixbuf=(pixbuf)
Sets the pixbuf.
  • pixbuf: the new pixbuf (Gdk::Pixbuf).
  • Returns: pixbuf
set_pixbuf(pixbuf)
Same as Gtk::Image#pixbuf=.
pixbuf_animation
Gets the Gdk::PixbufAnimation being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::ANIMATION (see Gtk::Image::storage_type).
pixbuf_animation=(animation)
Sets the animation.
set_pixbuf_animation(animation)
Same as Gtk::Image#pixbuf_animation=.
pixmap
Gets the Gdk::Pixmap being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::PIXMAP (see Gtk::Image::storage_type).
pixmap=(pixmap)
Sets the pixmap.
  • pixmap: the new pixmap (Gdk::Pixmap).
  • Returns: pixmap
set_pixmap(pixmap)
Same as Gtk::Image#pixmap=.
mask
Gets the Gdk::Pixmap being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::PIXMAP (see Gtk::Image::storage_type).
  • mask: the new mask (Gdk::Pixmap) (depth = 1).
  • Returns: mask
mask=(mask)
Sets the mask.
  • mask: the new mask (Gdk::Pixmap) (depth = 1).
  • Returns: mask
set_mask(mask)
Same as Gtk::Image#mask=.
  • mask: the new mask (Gdk::Pixmap) (depth = 1).
  • Returns: self
stock
Gets the stock item being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image::EMPTY or Gtk::Image::STOCK (see Gtk::Image::storage_type).
  • stock: the new stock item (Gtk::Stock constant value).
  • Returns: stock
stock=(stock)
Sets the stock item.
  • stock: the new stock item (Gtk::Stock constant value).
  • Returns: stock
set_stock(stock)
Same as Gtk::Image#stock=.
  • stock: the new stock item (Gtk::Stock constant value).
  • Returns: self
file=(filename)
Sets the filename.
  • filename: the new filename(String).
  • Returns: filename
set_file(filename)
Same as Gtk::Image#file=.
  • filename: the new filename(String).
  • Returns: self
icon_name
Gets the icon name being displayed by the Gtk::Image. The storage type of the image must be Gtk::Image#EMPTY or Gtk::Image#ICON_NAME. Since 2.6
pixel_size
Gets the pixel size used for named icons. Since 2.6
pixel_size=(pixel_size)
Sets the pixel size to use for named icons. If the pixel size is set to a value != -1, it is used instead of the icon size set by Gtk::Image#set(iconname, size). Since 2.6
set_pixel_size(pixel_size)
Same as . Since 2.6
set(value)
Sets a new value.
  • value
    • filename(String) - Creates a new Gtk::Image displaying the file filename. If the file isn't found or can't be loaded, the resulting Gtk::Image will display a "broken image" icon. This function never returns nil, it always returns a valid Gtk::Image widget. If the file contains an animation, the image will contain an animation. If you need to detect failures to load the file, use Gdk::Pixbuf.new(filename) to load the file yourself, then create the Gtk::Image from the pixbuf. (Or for animations, use Gdk::PixbufAnimation.new(filenam. The storage type (Gtk::Image#storage_type) of the returned image is not defined, it will be whatever is appropriate for displaying the file.
    • Gdk::Pixbuf - Create a new Gtk::Image from Gdk::Pixbuf. This function just creates an Gtk::Image. The Gtk::Image created will not react to state changes. Should you want that, you should use Gtk::IconSet.
    • Gdk::PixbufAnimation - Creates a Gtk::Image displaying the given animation. The Gtk::Image does not assume a reference to the animation; you still need to unref it if you own references. Gtk::Image will add its own reference rather than adopting yours.
  • Returns: self
set(stock_id, size)
Sets a Gtk::Image displaying a stock icon. Sample stock icon names are Gtk::Stock::OPEN, Gtk::Stock::EXIT. Sample stock sizes are Gtk::IconSize::MENU, Gtk::IconSize::SMALL_TOOLBAR. If the stock icon name isn't known, a "broken image" icon will be displayed instead. You can register your own stock icon names, see Gtk::IconFactory#add_default and Gtk::IconFactory#add.
set(image, mask)
Sets a Gtk::Image widget displaying a image with a mask. A Gdk::Image is a client-side image buffer in the pixel format of the current display. The Gtk::Image does not assume a reference to the image or mask; you still need to unref them if you own references. Gtk::Image will add its own reference rather than adopting yours.
set(pixmap, mask)
Sets a Gtk::Image widget displaying pixmap with a mask. A Gdk::Image is a server-side image buffer in the pixel format of the current display. The Gtk::Image does not assume a reference to the pixmap or mask; you still need to unref them if you own references. Gtk::Image will add its own reference rather than adopting yours.
set(iconset, size)
Sets a Gtk::Image displaying an icon set. Sample stock sizes are Gtk::IconSize::MENU, Gtk::IconSize::SMALL_TOOLBAR. Instead of using this method, usually it's better to create a Gtk::IconFactory, put your icon sets in the icon factory, add the icon factory to the list of default factories with Gtk::IconFactory#add_default, and then use Gtk::Image.new(stock_id, size). This will allow themes to override the icon you ship with your application. The Gtk::Image does not assume a reference to the icon set; you still need to unref it if you own references. Gtk::Image will add its own reference rather than adopting yours.
set(iconname, size)
Sets an icon from the current icon theme. If the icon name isn't known, a "broken image" icon will be displayed instead. If the current icon theme is changed, the icon will be updated appropriately. Since 2.6
storage_type
Gets the type(Gtk::Image#Type) of representation being used by the Gtk::Image to store image data. If the Gtk::Image has no image data, the return value will be Gtk::Image::EMPTY.
clear
Resets the image to be empty. Since 2.8
  • Returns: self
file
Gets the Filename to load and display.
  • Returns: Filename to load and display
icon_name=(icon_name)
Sets the name of the icon from the icon theme.
  • icon_name: The name of the icon from the icon theme
  • Returns: icon_name
ref_accessible
See Atk::Implementor#ref_accessible.
set_icon_name(icon_name)
Same as icon_name=.
  • icon_name: The name of the icon from the icon theme
  • Returns: self
add_child
See Gtk::Buildable#add_child.
construct_child
See Gtk::Buildable#construct_child.
get_internal_child
See Gtk::Buildable#get_internal_child.
name
See Gtk::Buildable#name.
name=
See Gtk::Buildable#name=.
set_buildable_property
See Gtk::Buildable#set_buildable_property.
set_name
See Gtk::Buildable#set_name.
builder_name
See Gtk::Buildable#builder_name.
builder_name=
See Gtk::Buildable#builder_name=.
gicon
Gets the GIcon being displayed.
  • Returns: The GIcon being displayed
gicon=(gicon)
Sets the GIcon being displayed.
  • gicon: The GIcon being displayed
  • Returns: gicon
set_builder_name
See Gtk::Buildable#set_builder_name.
set_gicon(gicon)
Same as gicon=.
  • gicon: The GIcon being displayed
  • Returns: self

Constants

Type

Describes the image data representation used by a Gtk::Image. If you want to get the image from the widget, you can only get the currently-stored representation. e.g. if the Gtk::Image#storage_type returns Gtk::Image::PIXBUF, then you can call Gtk::Image#pixbuf but not Gtk::Image#stock. For empty images, you can request any storage type (call any of the "get" functions), but they will all return nil values.

EMPTY
there is no image displayed by the widget.
PIXMAP
the widget contains a Gdk::Pixmap.
IMAGE
the widget contains a Gdk::Image.
PIXBUF
the widget contains a Gdk::Pixbuf.
STOCK
the widget contains a stock icon (Gtk::Stock constant value).
ICON_SET
the widget contains a Gtk::IconSet.
ANIMATION
the widget contains a Gdk::PixbufAnimation.
ICON_NAME
Since 2.6
GICON

Properties

file: String (Write)
Filename to load and display
  • Default value: nil
icon-name: String (Read/Write)
The name of the icon in the icon theme. If the icon theme is changed, the image will be updated automatically. Since 2.6
  • Default value: nil
icon-set: Gtk::IconSet (Read/Write)
Icon set to display
icon-size: Integer (Read/Write)
Size to use for stock icon, icon set or named icon
  • Allowed values: >= 0
  • Default value: 4
image: Gdk::Image (Read/Write)
A Gdk::Image to display
mask: Gdk::Pixmap (Read/Write)
Mask bitmap to use with Gdk::Image or Gdk::Pixmap
pixbuf: Gdk::Pixbuf (Read/Write)
A Gdk::Pixbuf to display
pixbuf-animation: Gdk::PixbufAnimation (Read/Write)
Gdk::PixbufAnimation to display
pixel-size: Integer (Read/Write)
The :pixel-size property can be used to specify a fixed size overriding the :icon-size property for images of type Gtk::Image::ICON_NAME. Since 2.6
  • Allowed values: >= -1
  • Default value: -1
pixmap: Gdk::Pixmap (Read/Write)
A Gdk::Pixmap to display
stock: String (Read/Write)
Stock ID(Gtk::Stock constant value) for a stock image to display
storage-type: Gtk::Image::Type (Read)
The representation being used for image data
gicon: GIcon (Read/Write)
The GIcon being displayed

ChangeLog

  • 2007-02-13 Apply 2.10. Modified - Masao

- Masao