GtkImageView

GtkImageView — General purpose image viewer for Gtk+

Screenshot of the ./tests/interactive demo application

Functions

Signals

void mouse-wheel-scroll Run Last
void pixbuf-changed Run Last
void scroll Action
void set-fitting Action
void set-scroll-adjustments Run Last
void set-zoom Action
void zoom-changed Run Last
void zoom-in Action
void zoom-out Action

Types and Values

Object Hierarchy

    GEnum
    ╰── GtkImageTransp
    GObject
    ╰── GInitiallyUnowned
        ╰── GtkObject
            ╰── GtkWidget
                ╰── GtkImageView
                    ╰── GtkAnimView

Implemented Interfaces

GtkImageView implements AtkImplementorIface and GtkBuildable.

Description

GtkImageView is a full-featured general purpose image viewer widget for GTK. It provides a scrollable, zoomable pane in which a pixbuf can be displayed.

Keybindings

When focused, GtkImageView responds to the following keybindings:

Keys Corresponding function Description
GDK_KP_Add, GDK_equal, GDK_plus gtk_image_view_zoom_in() Causes the widget to zoom in one step.
GDK_KP_Subtract, GDK_minus gtk_image_view_zoom_out() Causes the widget to zoom out one step.
GDK_1 gtk_image_view_set_zoom() Sets zoom to 100%.
GDK_2 gtk_image_view_set_zoom() Sets zoom to 200%.
GDK_3 gtk_image_view_set_zoom() Sets zoom to 300%.
GDK_x gtk_image_view_set_fitting() Sets fitting to TRUE so that the whole pixbuf becomes visible.
GDK_Page_Up, GDK_Up + GDK_SHIFT_MASK Scroll the view half a page upwards.
GDK_Page_Down, GDK_Down + GDK_SHIFT_MASK Scroll the view half a page downwards.
GDK_Left + GDK_SHIFT_MASK Scroll the view half a page leftwards.
GDK_Right + GDK_SHIFT_MASK Scroll the view half a page rightwards.


Mouse actions

When focused, GtkImageView responds to the following mouse actions:

Mouse gesture Description
Mouse wheel scroll + GDK_CONTROL_MASK Increase or decrease the zoom of the view depending on the direction of the scroll.

Coordinate systems

Operations on GtkImageView are executed in three different 2D coordinate systems:

  • Image coordinates: each coordinate represents a pixel in the image. The range of valid coordinates goes from (0,0)-(p.w,p.h), where p.w and p.h is the width and height of the image.
  • Widget coordinates: each coordinate represents a pixel in the image view widgets coordinate system. The range of valid coordinates goes from (0,0)-(a.w,a.h) where a.w and a.h is the allocated width and height of the widget. Naturally, these coordinates are only valid for as long as the widget is realized.
  • Zoom coordinates: this coordinate system is the most frequently used coordinate system in GtkImageView. The range of valid coordinates goes from (0,0)-z(p.w,p.h) where p.w and p.h is the width and height of the image and z is the current zoom of the view. In other words, this coordinate system is simply the image coordinate system scaled.

Settings

GtkImageView has a few settings that can be configured by users of the library. For example, when showing transparent images it may in certain cases be better to draw alpha transparent parts using the widgets background color instead of the default checkerboard:

1
2
3
gtk_image_view_set_transp (GTK_IMAGE_VIEW (view),
                           GTK_IMAGE_TRANSP_COLOR,
                           0x00000000);

When the window that is showing the widget is fullscreened, other settings has to be tweaked to make the view look as good as possible:

1
2
3
gtk_image_view_set_show_cursor (GTK_IMAGE_VIEW (view), FALSE);
gtk_image_view_set_show_frame (GTK_IMAGE_VIEW (view), FALSE);
gtk_image_view_set_black_bg (GTK_IMAGE_VIEW (view), TRUE);

Naturally, you should reset these settings again when the view leaves fullscreen mode.

GtkImageView aggresively caches the scaled image data. This behaviour is most often beneficial and makes the widget very fast. For example, try opening a very large image (4000x2000 pixels or so) in GtkImageView. The widget will spend some time bilinearly scaling the image at the start. Then try minimizing and unminimizing the window. The image will reappear immedately because the view has cached it.

However, this feature means that a client application must signal to the view when it changes the pixels on the pixbuf the view shows. The right way to do that is to use the gtk_image_view_damage_pixels() function. Code that merely tries to update the view by requesting that it should be redrawn will not work.

1
2
// Do some operation on the pixbuf data here
gtk_widget_queue_draw_area (10, 10, 50, 50) // Incorrect!

Example

This is the minimal code needed for using GtkImageView.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <gtkimageview/gtkimageview.h>
int
main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GtkWidget *view = gtk_image_view_new ();
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file ("tests/gnome_logo.jpg", NULL);

    gtk_image_view_set_pixbuf (GTK_IMAGE_VIEW (view), pixbuf, TRUE);
    gtk_container_add (GTK_CONTAINER (window), view);
    gtk_widget_show_all (window);
    gtk_main ();
}

Compile and run with:

1
2
$ gcc -o minimal minimal.c `pkg-config --cflags --libs gtkimageview`
$ ./minimal

The result should look something like the following:

Note that because the example doesn't use GtkImageScrollWin many nice features aren't available.

Functions

gtk_image_view_new ()

GtkWidget *
gtk_image_view_new (void);

Creates a new image view with default values. The default values are:

  • black bg : FALSE
  • fitting : TRUE
  • image tool : a GtkImageToolDragger instance
  • interpolation mode : GDK_INTERP_BILINEAR
  • offset : (0, 0)
  • pixbuf : NULL
  • show cursor: TRUE
  • show frame : TRUE
  • transp : GTK_IMAGE_TRANSP_GRID
  • zoom : 1.0

Returns

a new GtkImageView.


gtk_image_view_get_viewport ()

gboolean
gtk_image_view_get_viewport (GtkImageView *view,
                             GdkRectangle *rect);

Fills in the rectangle with the current viewport. If pixbuf is NULL, there is no viewport, rect is left untouched and FALSE is returned. If the view is not allocated, the rectangles coordinates are set to the views offset and its width and height to zero.

The current viewport is defined as the rectangle, in zoomspace coordinates as the area of the loaded pixbuf the GtkImageView is currently showing.

Parameters

view

a GtkImageView

 

rect

a GdkRectangle to fill in with the current viewport or NULL.

 

Returns

TRUE if a GdkPixbuf is shown, FALSE otherwise.


gtk_image_view_get_draw_rect ()

gboolean
gtk_image_view_get_draw_rect (GtkImageView *view,
                              GdkRectangle *rect);

Get the rectangle in the widget where the pixbuf is painted.

For example, if the widgets allocated size is 100, 100 and the pixbufs size is 50, 50 and the zoom factor is 1.0, then the pixbuf will be drawn centered on the widget. rect will then be (25,25)-[50,50].

If the view is not allocated, then the rectangle will be set to (0,0)-[0,0] and TRUE is returned.

This method is useful when converting from widget to image or zoom space coordinates.

Parameters

view

a GtkImageView

 

rect

a GdkRectangle to fill in with the area of the widget in which the pixbuf is drawn.

 

Returns

TRUE if the view has a pixbuf, FALSE otherwise.


gtk_image_view_get_check_colors ()

void
gtk_image_view_get_check_colors (GtkImageView *view,
                                 int *check_color1,
                                 int *check_color2);

Reads the two colors used to draw transparent parts of images with an alpha channel. Note that if the transp setting of the view is GTK_IMAGE_TRANSP_BACKGROUND or GTK_IMAGE_TRANSP_COLOR, then both colors will be equal.

Parameters

view

A GtkImageView.

 

check_color1

A pointer to an integer where the first check color should be stored.

 

check_color2

A pointer to an integer wherer the second check color should be stored.

 

gtk_image_view_image_to_widget_rect ()

gboolean
gtk_image_view_image_to_widget_rect (GtkImageView *view,
                                     GdkRectangle *rect_in,
                                     GdkRectangle *rect_out);

Convert a rectangle in image space coordinates to widget space coordinates. If the view is not realized, or if it contains no pixbuf, then the conversion was unsuccessful, FALSE is returned and rect_out is left unmodified.

The size of rect_out is rounded up. For example, if the zoom factor is 0.25 and the width of the input rectangle is 2, then its with in widget space coordinates is 0.5 which is rounded up to 1.

Note that this function may return a rectangle that is not visible on the widget.

Parameters

view

a GtkImageView

 

rect_in

a GdkRectangle in image space coordinates to convert

 

rect_out

a GdkRectangle to fill in with the widget space coordinates

 

Returns

TRUE if the conversion was successful, FALSE otherwise


gtk_image_view_set_offset ()

void
gtk_image_view_set_offset (GtkImageView *view,
                           gdouble x,
                           gdouble y,
                           gboolean invalidate);

Sets the offset of where in the image the GtkImageView should begin displaying image data.

The offset is clamped so that it will never cause the GtkImageView to display pixels outside the pixbuf. Setting this attribute causes the widget to repaint itself if it is realized.

If invalidate is TRUE, the views entire area will be invalidated instead of redrawn immediately. The view is then queued for redraw, which means that additional operations can be performed on it before it is redrawn.

The difference can sometimes be important like when you are overlaying data and get flicker or artifacts when setting the offset. If that happens, setting invalidate to TRUE could fix the problem. See the source code to GtkImageToolSelector for an example.

Normally, invalidate should always be FALSE because it is much faster to repaint immedately than invalidating.

Parameters

view

A GtkImageView.

 

x

X-component of the offset in zoom space coordinates.

 

y

Y-component of the offset in zoom space coordinates.

 

invalidate

whether to invalidate the view or redraw immediately.

 

gtk_image_view_set_transp ()

void
gtk_image_view_set_transp (GtkImageView *view,
                           GtkImageTransp transp,
                           int transp_color);

Sets how the view should draw transparent parts of images with an alpha channel. If transp is GTK_IMAGE_TRANSP_COLOR, the specified color will be used. Otherwise the transp_color argument is ignored. If it is GTK_IMAGE_TRANSP_BACKGROUND, the background color of the widget will be used. If it is GTK_IMAGE_TRANSP_GRID, then a grid with light and dark gray boxes will be drawn on the transparent parts.

Calling this method causes the widget to immediately repaint. It also causes the ::pixbuf-changed signal to be emitted. This is done so that other widgets (such as GtkImageNav) will have a chance to render a view of the pixbuf with the new transparency settings.

The default values are:

Parameters

view

A GtkImageView.

 

transp

The transparency type to use when drawing transparent images.

 

transp_color

Color to use when drawing transparent images.

 

gtk_image_view_get_fitting ()

gboolean
gtk_image_view_get_fitting (GtkImageView *view);

Returns the fitting setting of the view.

Parameters

view

a GtkImageView

 

Returns

TRUE if the view is fitting the image, FALSE otherwise.


gtk_image_view_set_fitting ()

void
gtk_image_view_set_fitting (GtkImageView *view,
                            gboolean fitting);

Sets whether to fit or not. If TRUE, then the view will adapt the zoom so that the whole pixbuf is visible.

Setting the fitting causes the widget to immediately repaint itself.

Fitting is by default TRUE.

Parameters

view

a GtkImageView.

 

fitting

whether to fit the image or not

 

gtk_image_view_get_pixbuf ()

GdkPixbuf *
gtk_image_view_get_pixbuf (GtkImageView *view);

Returns the pixbuf this view shows.

Parameters

view

A GtkImageView.

 

Returns

The pixbuf this view shows.


gtk_image_view_set_pixbuf ()

void
gtk_image_view_set_pixbuf (GtkImageView *view,
                           GdkPixbuf *pixbuf,
                           gboolean reset_fit);

Sets the pixbuf to display, or NULL to not display any pixbuf. Normally, reset_fit should be TRUE which enables fitting. Which means that, initially, the whole pixbuf will be shown.

Sometimes, the fit mode should not be reset. For example, if GtkImageView is showing an animation, it would be bad to reset the fit mode for each new frame. The parameter should then be FALSE which leaves the fit mode of the view untouched.

This method should not be used if merely the contents of the pixbuf has changed. See gtk_image_view_damage_pixels() for that.

If reset_fit is TRUE, the ::zoom-changed signal is emitted, otherwise not. The ::pixbuf-changed signal is also emitted.

The default pixbuf is NULL.

Parameters

view

A GtkImageView.

 

pixbuf

The pixbuf to display.

 

reset_fit

Whether to reset fitting or not.

 

gtk_image_view_get_zoom ()

gdouble
gtk_image_view_get_zoom (GtkImageView *view);

Get the current zoom factor of the view.

Parameters

view

a GtkImageView

 

Returns

the current zoom factor


gtk_image_view_set_zoom ()

void
gtk_image_view_set_zoom (GtkImageView *view,
                         gdouble zoom);

Sets the zoom of the view.

Fitting is always disabled after this method has run. The ::zoom-changed signal is unconditionally emitted.

The default value is 1.0.

Parameters

view

a GtkImageView

 

zoom

the new zoom factor

 

gtk_image_view_set_black_bg ()

void
gtk_image_view_set_black_bg (GtkImageView *view,
                             gboolean black_bg);

If TRUE, the view uses a black background. If FALSE, the view uses the default (normally gray) background.

The default value is FALSE.

Parameters

view

A GtkImageView.

 

black_bg

Whether to use a black background or not.

 

gtk_image_view_get_black_bg ()

gboolean
gtk_image_view_get_black_bg (GtkImageView *view);

Returns whether the view renders the widget on a black background or not.

Parameters

view

A GtkImageView.

 

Returns

TRUE if a black background is used, otherwise FALSE.


gtk_image_view_set_show_frame ()

void
gtk_image_view_set_show_frame (GtkImageView *view,
                               gboolean show_frame);

Sets whether to draw a frame around the image or not. When TRUE, a one pixel wide frame is shown around the image. Setting this attribute causes the widget to immediately repaint itself.

The default value is TRUE.

Parameters

view

a GtkImageView

 

show_frame

whether to show a frame around the pixbuf or not

 

gtk_image_view_get_show_frame ()

gboolean
gtk_image_view_get_show_frame (GtkImageView *view);

Returns whether a one pixel frame is drawn around the pixbuf or not.

Parameters

view

A GtkImageView.

 

Returns

TRUE if a frame is drawn around the pixbuf, otherwise FALSE.


gtk_image_view_set_interpolation ()

void
gtk_image_view_set_interpolation (GtkImageView *view,
                                  GdkInterpType interp);

Sets the interpolation mode of how the view. GDK_INTERP_HYPER is the slowest, but produces the best results. GDK_INTERP_NEAREST is the fastest, but provides bad rendering quality. GDK_INTERP_BILINEAR is a good compromise.

Setting the interpolation mode causes the widget to immediately repaint itself.

The default interpolation mode is GDK_INTERP_BILINEAR.

Parameters

view

A GtkImageView.

 

interp

The interpolation to use. One of GDK_INTERP_NEAREST, GDK_INTERP_BILINEAR and GDK_INTERP_HYPER.

 

gtk_image_view_get_interpolation ()

GdkInterpType
gtk_image_view_get_interpolation (GtkImageView *view);

Returns the current interpolation mode of the view.

Parameters

view

a GtkImageView

 

Returns

the interpolation


gtk_image_view_set_show_cursor ()

void
gtk_image_view_set_show_cursor (GtkImageView *view,
                                gboolean show_cursor);

Sets whether to show the mouse cursor when the mouse is over the widget or not. Hiding the cursor is useful when the widget is fullscreened.

The default value is TRUE.

Parameters

view

A GtkImageView.

 

show_cursor

whether to show the cursor or not

 

gtk_image_view_get_show_cursor ()

gboolean
gtk_image_view_get_show_cursor (GtkImageView *view);

Returns whether to show the mouse cursor when the mouse is over the widget or not.

Parameters

view

A GtkImageView.

 

Returns

TRUE if the cursor is shown, otherwise FALSE.


gtk_image_view_set_tool ()

void
gtk_image_view_set_tool (GtkImageView *view,
                         GtkIImageTool *tool);

Set the image tool to use. If the new tool is the same as the current tool, then nothing will be done. Otherwise gtk_iimage_tool_pixbuf_changed() is called so that the tool has a chance to generate initial data for the pixbuf.

Setting the tool causes the widget to immediately repaint itself.

The default image tool is a GtkImageToolDragger instance. See also GtkIImageTool.

Parameters

view

A GtkImageView.

 

tool

The image tool to use (must not be NULL)

 

gtk_image_view_get_tool ()

GtkIImageTool *
gtk_image_view_get_tool (GtkImageView *view);

Gets the image tool GtkImageView uses for rendering and handling input events.

Parameters

view

A GtkImageView

 

Returns

The currently bound image tool


gtk_image_view_zoom_in ()

void
gtk_image_view_zoom_in (GtkImageView *view);

Zoom in the view one step. Calling this method causes the widget to immediately repaint itself.

Parameters

view

a GtkImageView

 

gtk_image_view_zoom_out ()

void
gtk_image_view_zoom_out (GtkImageView *view);

Zoom out the view one step. Calling this method causes the widget to immediately repaint itself.

Parameters

view

a GtkImageView

 

gtk_image_view_damage_pixels ()

void
gtk_image_view_damage_pixels (GtkImageView *view,
                              GdkRectangle *rect);

Mark the pixels in the rectangle as damaged. That the pixels are damaged, means that they have been modified and that the view must redraw them to ensure that the visible part of the image corresponds to the pixels in that image. Calling this method emits the ::pixbuf-changed signal.

This method must be used when modifying the image data:

1
2
3
4
// Drawing something cool in the area 20,20 - 60,60 here...
...
// And force an update
gtk_image_view_damage_pixels (view, &(GdkRectangle){20, 20, 60, 60});

If the whole pixbuf has been modified then rect should be NULL to indicate that a total update is needed.

See also gtk_image_view_set_pixbuf().

Parameters

view

a GtkImageView

 

rect

rectangle in image space coordinates to mark as damaged or NULL, to mark the whole pixbuf as damaged.

 

gtk_image_view_library_version ()

const char *
gtk_image_view_library_version (void);

Returns a string with the format "major.minor.micro" which denotes the runtime version of GtkImageView being used.

Returns

a string describing the version of GtkImageView.The returned string is owned by GtkImageView and should not be modified or freed.

Types and Values

enum GtkImageTransp

This enum defines the valid transparency settings for how the image view should draw transparent parts of alpha images. Their primary use is as a value for the first parameter to the gtk_image_view_set_transp() method.

Their interpretation is as follows:

  • GTK_IMAGE_TRANSP_COLOR : Use a single color.
  • GTK_IMAGE_TRANSP_BACKGROUND : Use the background color of the widget
  • GTK_IMAGE_TRANSP_GRID : Use light and dark gray checkerboard pattern.

Members

GTK_IMAGE_TRANSP_COLOR

   

GTK_IMAGE_TRANSP_BACKGROUND

   

GTK_IMAGE_TRANSP_GRID

   

struct GtkImageView

struct GtkImageView;

GtkImageView is the main class in the library. All of its fields are private, they are only shown here for completeness. Use gtk_image_view_new() to instantiate GtkImageView objects.

Signal Details

The “mouse-wheel-scroll” signal

void
user_function (GtkImageView      *view,
               GdkScrollDirection direction,
               gpointer           user_data)

The ::mouse-wheel-scroll signal is emitted when the mouse wheel is scrolled on the view and GTK_CONTROL_MASK is not held down.

Parameters

view

The GtkImageView that emitted the signal.

 

direction

The direction of the scroll; GDK_SCROLL_UP, GDK_SCROLL_DOWN, GDK_SCROLL_LEFT or GDK_SCROLL_RIGHT.

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “pixbuf-changed” signal

void
user_function (GtkImageView *view,
               gpointer      user_data)

The ::pixbuf-changed signal is emitted when the pixbuf the image view shows is changed and when its image data is changed. Listening to this signal is useful if you, for example, have a label that displays the width and height of the pixbuf in the view.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Handler that will be called when the pixbuf changes.
static void
pixbuf_cb (GtkImageView *view,
           GtkLabel     *label)
{
    GdkPixbuf *new_pb = gtk_image_view_get_pixbuf (view);
    if (!new_pb)
    {
        // Empty label if no pixbuf.
        gtk_label_set_text (label, "");
        return;
    }
    int width = gdk_pixbuf_get_width (new_pb);
    int height = gdk_pixbuf_get_height (new_pb);
    char *text = g_strdup_printf ("%d, %d", width, height);
    gtk_label_set_text (label, text);
    g_free (text);
}
...
GtkWidget *label = gtk_label_new ("");
g_signal_connect (G_OBJECT (view), "pixbuf-changed",
                  G_CALLBACK (pixbuf_cb), label);

Parameters

view

The view that emitted the signal.

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “scroll” signal

void
user_function (GtkImageView *view,
               GtkScrollType xscroll,
               GtkScrollType yscroll,
               gpointer      user_data)

The ::scroll signal is a keybinding signal emitted when a key is used to scroll the view. The signal should not be used by clients of this library.

Parameters

view

The GtkImageView that received the signal.

 

xscroll

Horizontal scroll constant.

 

yscroll

Vertical scroll constant.

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “set-fitting” signal

void
user_function (GtkImageView *imageview,
               gint          arg1,
               gpointer      user_data)

Parameters

imageview

the object which received the signal.

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “set-scroll-adjustments” signal

void
user_function (GtkImageView  *arg0,
               GtkAdjustment *arg1,
               GtkAdjustment *arg2,
               gpointer       user_data)

Do we really need this signal? It should be intrinsic to the GtkWidget class, shouldn't it?

Parameters

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “set-zoom” signal

void
user_function (GtkImageView *view,
               gdouble       zoom,
               gpointer      user_data)

The ::set-zoom signal is a keybinding signal emitted when GDK_1, GDK_2 or GDK_3 is pressed on the widget which causes the zoom to be set to 100%, 200% or 300%. The signal should not be used by clients of this library.

Parameters

view

The GtkImageView that received the signal.

 

zoom

The new zoom factor.

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “zoom-changed” signal

void
user_function (GtkImageView *view,
               gpointer      user_data)

The ::zoom-changed signal is emitted when the zoom factor of the view changes. Listening to this signal is useful if, for example, you have a label that displays the zoom factor of the view. Use gtk_image_view_get_zoom() to retrieve the value. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Handler that will be called when the zoom changes.
static void
zoom_cb (GtkImageView *view,
         GtkLabel     *label)
{
    gdouble zoom = gtk_image_view_get_zoom (view);
    char *text = g_strdup_printf ("%d%%", (int)(zoom * 100.0));
    gtk_label_set_text (label, text);
    g_free (text);
}
...
// Connect the callback to the signal.
GtkWidget *label = gtk_label_new ("100%");
g_signal_connect (G_OBJECT (view), "zoom-changed",
                  G_CALLBACK (zoom_cb), label);

Parameters

view

The GtkImageView that emitted the signal.

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last


The “zoom-in” signal

void
user_function (GtkImageView *imageview,
               gpointer      user_data)

Parameters

imageview

the object which received the signal.

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “zoom-out” signal

void
user_function (GtkImageView *view,
               gpointer      user_data)

The ::zoom-out signal is a keybinding signal emitted when GDK_minus or GDK_KP_Subtract is pressed on the widget. The signal should not be used by clients of this library.

Parameters

view

The GtkImageView that received the signal.

 

user_data

user data set when the signal handler was connected.

 

Flags: Action

See Also

GtkAnimView