GtkImageView

GtkImageView — General purpose image viewer widget for GTK.

Screenshot of the ./tests/interactive demo application

Synopsis




enum                GtkFitMode;
enum                GtkImageTransp;
                    GtkImageView;
GtkWidget*          gtk_image_view_new                  (void);
gboolean            gtk_image_view_get_viewport         (GtkImageView *view,
                                                         GdkRectangle *rect);
void                gtk_image_view_get_check_colors     (GtkImageView *view,
                                                         int *check_color1,
                                                         int *check_color2);
void                gtk_image_view_set_offset           (GtkImageView *view,
                                                         gdouble x,
                                                         gdouble y);
void                gtk_image_view_set_offset_invalidating
                                                        (GtkImageView *view,
                                                         gdouble x,
                                                         gdouble y);
void                gtk_image_view_set_transp           (GtkImageView *view,
                                                         GtkImageTransp transp,
                                                         int transp_color);
GtkFitMode          gtk_image_view_get_fit_mode         (GtkImageView *view);
void                gtk_image_view_set_fit_mode         (GtkImageView *view,
                                                         GtkFitMode fit_mode);
GdkPixbuf*          gtk_image_view_get_pixbuf           (GtkImageView *view);
void                gtk_image_view_set_pixbuf           (GtkImageView *view,
                                                         GdkPixbuf *pixbuf,
                                                         gboolean reset_fit);
gdouble             gtk_image_view_get_zoom             (GtkImageView *view);
void                gtk_image_view_set_zoom             (GtkImageView *view,
                                                         gdouble zoom);
void                gtk_image_view_set_black_bg         (GtkImageView *view,
                                                         gboolean black_bg);
gboolean            gtk_image_view_get_black_bg         (GtkImageView *view);
void                gtk_image_view_set_show_frame       (GtkImageView *view,
                                                         gboolean show_frame);
gboolean            gtk_image_view_get_show_frame       (GtkImageView *view);
void                gtk_image_view_set_interpolation    (GtkImageView *view,
                                                         GdkInterpType interp);
GdkInterpType       gtk_image_view_get_interpolation    (GtkImageView *view);
void                gtk_image_view_set_show_cursor      (GtkImageView *view,
                                                         gboolean show_cursor);
gboolean            gtk_image_view_get_show_cursor      (GtkImageView *view);
void                gtk_image_view_set_tool             (GtkImageView *view,
                                                         GtkIImageTool *tool);
GtkIImageTool*      gtk_image_view_get_tool             (GtkImageView *view);
void                gtk_image_view_zoom_in              (GtkImageView *view);
void                gtk_image_view_zoom_out             (GtkImageView *view);

Object Hierarchy


  GObject
   +----GInitiallyUnowned
         +----GtkObject
               +----GtkWidget
                     +----GtkImageView
                           +----GtkAnimView

Implemented Interfaces

GtkImageView implements AtkImplementorIface.

Signals


  "mouse-wheel-scroll"                             : Run Last
  "pixbuf-changed"                                 : Run Last
  "scroll"                                         : Run Last / Action
  "set-fit-mode"                                   : Run Last / Action
  "set-scroll-adjustments"                         : Run Last
  "set-zoom"                                       : Run Last / Action
  "zoom-changed"                                   : Run Last
  "zoom-in"                                        : Run Last / Action
  "zoom-out"                                       : Run Last / Action

Description

GtkImageView is a full-featured general purpose 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_fit_mode() Sets fit mode to GTK_FIT_SIZE_IF_LARGER 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:

        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:

        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);
      

Example

This is the minimal code needed for using GtkImageView.

#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:

$ 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.

Details

enum GtkFitMode

typedef enum
{
    GTK_FIT_NONE = 0,
    GTK_FIT_SIZE_IF_LARGER
} GtkFitMode;

To be fixed. Use a boolean instead.


enum GtkImageTransp

typedef enum
{
    GTK_IMAGE_TRANSP_COLOR = 0,
    GTK_IMAGE_TRANSP_BACKGROUND,
    GTK_IMAGE_TRANSP_GRID
} 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.


GtkImageView

typedef struct _GtkImageView 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.


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
  • fit mode : GTK_FIT_SIZE_IF_LARGER
  • interpolation mode : GDK_INTERP_BILINEAR
  • offset : (0, 0)
  • pixbuf : NULL
  • show cursor: TRUE
  • show frame : TRUE
  • zoom : 1.0
  • transp : GTK_IMAGE_TRANSP_GRID

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 and rect is left untouched and FALSE is returned.

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

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_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.

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_set_offset ()

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

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.

view : A GtkImageView.
x : X-component of the offset in zoom space coordinates.
y : Y-component of the offset in zoom space coordinates.

gtk_image_view_set_offset_invalidating ()

void                gtk_image_view_set_offset_invalidating
                                                        (GtkImageView *view,
                                                         gdouble x,
                                                         gdouble y);

A variant of gtk_image_view_set_offset() that redraws the view by invalidating its entire area. This method is very experimental and may disappear. It is used by the GtkImageToolSelector class.

view : A GtkImageView.
x : X-component of the offset in zoom space coordinates.
y : Y-component of the offset in zoom space coordinates.

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 immidiately 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:

  • transp : GTK_IMAGE_TRANSP_GRID
  • transp_color : 0x000000

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_fit_mode ()

GtkFitMode          gtk_image_view_get_fit_mode         (GtkImageView *view);

Returns the fit mode of the view.

view : A GtkImageView.
Returns : The current fit mode.

gtk_image_view_set_fit_mode ()

void                gtk_image_view_set_fit_mode         (GtkImageView *view,
                                                         GtkFitMode fit_mode);

Sets the fit mode to use. GTK_FIT_SIZE_IF_LARGER means that the view will adapt the zoom so that the whole pixbuf is visible.

Setting the fit mode causes the widget to immidiately repaint itself.

The default fit mode is GTK_FIT_SIZE_IF_LARGER.

view : A GtkImageView.
fit_mode : Fit mode to use (one of GTK_FIT_NONE and GTK_FIT_SIZE_IF_LARGER).

gtk_image_view_get_pixbuf ()

GdkPixbuf*          gtk_image_view_get_pixbuf           (GtkImageView *view);

Returns the pixbuf this view shows.

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 causes the fit mode to be set to GTK_FIT_SIZE_IF_LARGER. 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 must also be used to signal the view that the contents of the pixbuf it display has been changed. This is the right way to force the redraw:

    GdkPixbuf *pixbuf = gtk_image_view_get_pixbuf (GTK_IMAGE_VIEW (view));
    gtk_image_view_set_pixbuf (GTK_IMAGE_VIEW (view), pixbuf, FALSE);
  

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.

view : A GtkImageView.
pixbuf : The pixbuf to display.
reset_fit : Whether to reset fit mode or not.

gtk_image_view_get_zoom ()

gdouble             gtk_image_view_get_zoom             (GtkImageView *view);

Get the current zoom factor of the view.

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.

Fit mode is always reset to GTK_FIT_NONE. The ::zoom-changed signal is unconditionally emitted.

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.

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.

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 immidiately repaint itself.

The default value is TRUE.

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.

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 immidiately repaint itself.

The default interpolation mode is GDK_INTERP_BILINEAR.

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.

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.

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.

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 invoked so that the tool has a chance to generate initial data for the pixbuf.

Setting the tool causes the widget to immidiately repaint itself.

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);

view : A GtkImageView
Returns : The currently bound 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 immidiately repaint itself.

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 immidiately repaint itself.

view : A GtkImageView

Signal Details

The "mouse-wheel-scroll" signal

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

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

view : The GtkImageView that emitted the signal.
direction : The direction of the scroll; either GDK_SCROLL_UP or GDK_SCROLL_DOWN.
user_data : user data set when the signal handler was connected.

The "pixbuf-changed" signal

void                user_function                      (GtkImageView *view,
                                                        gpointer      user_data)      : Run Last

The ::pixbuf-changed signal is emitted when the pixbuf the image view shows 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.

    // 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);
  

view : The view that emitted the signal.
user_data : user data set when the signal handler was connected.

The "scroll" signal

void                user_function                      (GtkImageView *view,
                                                        GtkScrollType xscroll,
                                                        GtkScrollType yscroll,
                                                        gpointer      user_data)      : Run Last / Action

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.

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.

The "set-fit-mode" signal

void                user_function                      (GtkImageView *view,
                                                        gint          fit_mode,
                                                        gpointer      user_data)      : Run Last / Action

The ::set-fit-mode signal is a keybinding signal emitted when the GDK_W key is pressed on the widget. The signal should not be used by clients of this library.

view : The GtkImageView that received the signal.
fit_mode : The new fit mode.
user_data : user data set when the signal handler was connected.

The "set-scroll-adjustments" signal

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

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

user_data : user data set when the signal handler was connected.

The "set-zoom" signal

void                user_function                      (GtkImageView *view,
                                                        gdouble       zoom,
                                                        gpointer      user_data)      : Run Last / Action

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.

view : The GtkImageView that received the signal.
zoom : The new zoom factor.
user_data : user data set when the signal handler was connected.

The "zoom-changed" signal

void                user_function                      (GtkImageView *view,
                                                        gpointer      user_data)      : Run Last

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:

    // 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);
  

view : The GtkImageView that emitted the signal.
user_data : user data set when the signal handler was connected.

The "zoom-in" signal

void                user_function                      (GtkImageView *imageview,
                                                        gpointer      user_data)      : Run Last / Action

imageview : the object which received the signal.
user_data : user data set when the signal handler was connected.

The "zoom-out" signal

void                user_function                      (GtkImageView *view,
                                                        gpointer      user_data)      : Run Last / Action

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.

view : The GtkImageView that received the signal.
user_data : user data set when the signal handler was connected.