Image
open class Image : Misc, ImageProtocol
The GtkImage
widget displays an image. Various kinds of object
can be displayed as an image; most typically, you would load a
GdkPixbuf
(“pixel buffer”) from a file, and then display that.
There’s a convenience function to do this, gtk_image_new_from_file()
,
used as follows:
(C Language Example):
GtkWidget *image;
image = gtk_image_new_from_file ("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_from_file()
, then create the GtkImage
with
gtk_image_new_from_pixbuf()
.
The image file may contain an animation, if so the GtkImage
will
display an animation (GdkPixbufAnimation
) instead of a static image.
GtkImage
is a subclass of GtkMisc
, which implies that you can
align it (center, left, right) and add padding to it, using
GtkMisc
methods.
GtkImage
is a “no window” widget (has no GdkWindow
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
GtkEventBox
, then connect to the event signals on the event box.
Handling button press events on a GtkImage
.
(C Language Example):
static gboolean
button_press_callback (GtkWidget *event_box,
GdkEventButton *event,
gpointer data)
{
g_print ("Event box clicked at coordinates %f,%f\n",
event->x, event->y);
// Returning TRUE means we handled the event, so the signal
// emission should be stopped (don’t call any further callbacks
// that may be connected). Return FALSE to continue invoking callbacks.
return TRUE;
}
static GtkWidget*
create_image (void)
{
GtkWidget *image;
GtkWidget *event_box;
image = gtk_image_new_from_file ("myfile.png");
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER (event_box), image);
g_signal_connect (G_OBJECT (event_box),
"button_press_event",
G_CALLBACK (button_press_callback),
image);
return image;
}
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 GtkMisc
).
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.
Sometimes an application will want to avoid depending on external data
files, such as image files. GTK+ comes with a program to avoid this,
called “gdk-pixbuf-csource”. This library
allows you to convert an image into a C variable declaration, which
can then be loaded into a GdkPixbuf
using
gdk_pixbuf_new_from_inline()
.
CSS nodes
GtkImage has a single CSS node with the name image. The style classes may appear on image CSS nodes: .icon-dropshadow, .lowres-icon.
The Image
type acts as a reference-counted owner of an underlying GtkImage
instance.
It provides the methods that can operate on this data type through ImageProtocol
conformance.
Use Image
as a strong reference or owner of a GtkImage
instance.
-
Designated initialiser from the underlying `C` data type.
This creates an instance without performing an unbalanced retain i.e., ownership is transferred to the
Image
instance.Declaration
Swift
@inlinable public init(_ op: UnsafeMutablePointer<GtkImage>)
Parameters
op
pointer to the underlying object
-
Designated initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable public init(_ op: UnsafePointer<GtkImage>)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a non-mutating
gpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable override public init!(gpointer op: gpointer?)
Parameters
op
gpointer to the underlying object
-
Optional initialiser from a non-mutating
gconstpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable override public init!(gconstpointer op: gconstpointer?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafePointer<GtkImage>?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafeMutablePointer<GtkImage>?)
Parameters
op
pointer to the underlying object
-
Designated initialiser from the underlying
C
data type. Will retainGtkImage
. i.e., ownership is transferred to theImage
instance.Declaration
Swift
@inlinable public init(retaining op: UnsafeMutablePointer<GtkImage>)
Parameters
op
pointer to the underlying object
-
Reference intialiser for a related type that implements
ImageProtocol
Will retainGtkImage
.Declaration
Swift
@inlinable public init<T>(image other: T) where T : ImageProtocol
Parameters
other
an instance of a related type that implements
ImageProtocol
-
Unsafe typed initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init<T>(cPointer p: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe typed, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init<T>(retainingCPointer cPointer: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init(raw p: UnsafeRawPointer)
Parameters
p
raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init(retainingRaw raw: UnsafeRawPointer)
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable public required init(raw p: UnsafeMutableRawPointer)
Parameters
p
mutable raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable required public init(retainingRaw raw: UnsafeMutableRawPointer)
Parameters
raw
mutable raw pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init(opaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ImageProtocol
.Declaration
Swift
@inlinable override public init(retainingOpaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Creates a new empty
GtkImage
widget.Declaration
Swift
@inlinable public init()
-
Creates a
GtkImage
displaying the given animation. TheGtkImage
does not assume a reference to the animation; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Note that the animation frames are shown using a timeout with
G_PRIORITY_DEFAULT
. When using animations to indicate busyness, keep in mind that the animation will only be shown if the main loop is not busy with something that has a higher priority.Declaration
Swift
@inlinable public init<PixbufAnimationT>(animation: PixbufAnimationT) where PixbufAnimationT : PixbufAnimationProtocol
-
Creates a new
GtkImage
displaying the filefilename
. If the file isn’t found or can’t be loaded, the resultingGtkImage
will display a “broken image” icon. This function never returnsnil
, it always returns a validGtkImage
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_from_file()
to load the file yourself, then create theGtkImage
from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()
).The storage type (
gtk_image_get_storage_type()
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.Declaration
Swift
@inlinable public init(file filename: UnsafePointer<gchar>!)
-
Creates a
GtkImage
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.Declaration
Swift
@inlinable public init<IconT>(gicon icon: IconT, size: GtkIconSize) where IconT : IconProtocol
-
Creates a
GtkImage
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.Declaration
Swift
@inlinable public init(iconName: UnsafePointer<gchar>? = nil, size: GtkIconSize)
-
Creates a
GtkImage
displaying an icon set. Sample stock sizes areGTK_ICON_SIZE_MENU
,GTK_ICON_SIZE_SMALL_TOOLBAR
. Instead of using this function, usually it’s better to create aGtkIconFactory
, put your icon sets in the icon factory, add the icon factory to the list of default factories withgtk_icon_factory_add_default()
, and then usegtk_image_new_from_stock()
. This will allow themes to override the icon you ship with your application.The
GtkImage
does not assume a reference to the icon set; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.new_from_icon_set is deprecated: Use gtk_image_new_from_icon_name() instead.
Declaration
Swift
@available(*, deprecated) @inlinable public init<IconSetT>(iconSet: IconSetT, size: GtkIconSize) where IconSetT : IconSetProtocol
-
Creates a new
GtkImage
displayingpixbuf
. TheGtkImage
does not assume a reference to the pixbuf; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Note that this function just creates an
GtkImage
from the pixbuf. TheGtkImage
created will not react to state changes. Should you want that, you should usegtk_image_new_from_icon_name()
.Declaration
Swift
@inlinable public init<PixbufT>(pixbuf: PixbufT?) where PixbufT : PixbufProtocol
-
Creates a new
GtkImage
displaying the resource fileresource_path
. If the file isn’t found or can’t be loaded, the resultingGtkImage
will display a “broken image” icon. This function never returnsnil
, it always returns a validGtkImage
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_from_file()
to load the file yourself, then create theGtkImage
from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()
).The storage type (
gtk_image_get_storage_type()
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.Declaration
Swift
@inlinable public init(resource resourcePath: UnsafePointer<gchar>!)
-
Creates a
GtkImage
displaying a stock icon. Sample stock icon names areGTK_STOCK_OPEN
,GTK_STOCK_QUIT
. Sample stock sizes areGTK_ICON_SIZE_MENU
,GTK_ICON_SIZE_SMALL_TOOLBAR
. If the stock icon name isn’t known, the image will be empty. You can register your own stock icon names, seegtk_icon_factory_add_default()
andgtk_icon_factory_add()
.new_from_stock is deprecated: Use gtk_image_new_from_icon_name() instead.
Declaration
Swift
@available(*, deprecated) @inlinable public init(stock stockID: UnsafePointer<gchar>!, size: GtkIconSize)
-
Creates a new
GtkImage
displayingsurface
. TheGtkImage
does not assume a reference to the surface; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Declaration
Swift
@inlinable public init<SurfaceT>(surface: SurfaceT?) where SurfaceT : SurfaceProtocol
-
Creates a
GtkImage
displaying the given animation. TheGtkImage
does not assume a reference to the animation; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Note that the animation frames are shown using a timeout with
G_PRIORITY_DEFAULT
. When using animations to indicate busyness, keep in mind that the animation will only be shown if the main loop is not busy with something that has a higher priority.Declaration
Swift
@inlinable public static func newFrom<PixbufAnimationT>(animation: PixbufAnimationT) -> Widget! where PixbufAnimationT : PixbufAnimationProtocol
-
Creates a new
GtkImage
displaying the filefilename
. If the file isn’t found or can’t be loaded, the resultingGtkImage
will display a “broken image” icon. This function never returnsnil
, it always returns a validGtkImage
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_from_file()
to load the file yourself, then create theGtkImage
from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()
).The storage type (
gtk_image_get_storage_type()
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.Declaration
Swift
@inlinable public static func newFrom(file filename: UnsafePointer<gchar>!) -> Widget!
-
Creates a
GtkImage
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.Declaration
Swift
@inlinable public static func newFromG<IconT>(gicon icon: IconT, size: GtkIconSize) -> Widget! where IconT : IconProtocol
-
Creates a
GtkImage
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.Declaration
Swift
@inlinable public static func newFrom(iconName: UnsafePointer<gchar>? = nil, size: GtkIconSize) -> Widget!
-
Creates a
GtkImage
displaying an icon set. Sample stock sizes areGTK_ICON_SIZE_MENU
,GTK_ICON_SIZE_SMALL_TOOLBAR
. Instead of using this function, usually it’s better to create aGtkIconFactory
, put your icon sets in the icon factory, add the icon factory to the list of default factories withgtk_icon_factory_add_default()
, and then usegtk_image_new_from_stock()
. This will allow themes to override the icon you ship with your application.The
GtkImage
does not assume a reference to the icon set; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.new_from_icon_set is deprecated: Use gtk_image_new_from_icon_name() instead.
Declaration
Swift
@available(*, deprecated) @inlinable public static func newFrom<IconSetT>(iconSet: IconSetT, size: GtkIconSize) -> Widget! where IconSetT : IconSetProtocol
-
Creates a new
GtkImage
displayingpixbuf
. TheGtkImage
does not assume a reference to the pixbuf; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Note that this function just creates an
GtkImage
from the pixbuf. TheGtkImage
created will not react to state changes. Should you want that, you should usegtk_image_new_from_icon_name()
.Declaration
Swift
@inlinable public static func newFrom<PixbufT>(pixbuf: PixbufT?) -> Widget! where PixbufT : PixbufProtocol
-
Creates a new
GtkImage
displaying the resource fileresource_path
. If the file isn’t found or can’t be loaded, the resultingGtkImage
will display a “broken image” icon. This function never returnsnil
, it always returns a validGtkImage
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_from_file()
to load the file yourself, then create theGtkImage
from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()
).The storage type (
gtk_image_get_storage_type()
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.Declaration
Swift
@inlinable public static func newFrom(resource resourcePath: UnsafePointer<gchar>!) -> Widget!
-
Creates a
GtkImage
displaying a stock icon. Sample stock icon names areGTK_STOCK_OPEN
,GTK_STOCK_QUIT
. Sample stock sizes areGTK_ICON_SIZE_MENU
,GTK_ICON_SIZE_SMALL_TOOLBAR
. If the stock icon name isn’t known, the image will be empty. You can register your own stock icon names, seegtk_icon_factory_add_default()
andgtk_icon_factory_add()
.new_from_stock is deprecated: Use gtk_image_new_from_icon_name() instead.
Declaration
Swift
@available(*, deprecated) @inlinable public static func newFrom(stock stockID: UnsafePointer<gchar>!, size: GtkIconSize) -> Widget!
-
Creates a new
GtkImage
displayingsurface
. TheGtkImage
does not assume a reference to the surface; you still need to unref it if you own references.GtkImage
will add its own reference rather than adopting yours.Declaration
Swift
@inlinable public static func newFrom<SurfaceT>(surface: SurfaceT?) -> Widget! where SurfaceT : SurfaceProtocol