![]() |
![]() |
![]() |
GtkImageView Reference Manual | ![]() |
---|---|---|---|---|
GtkImageViewGtkImageView — General purpose image viewer for Gtk+ ![]() Screenshot of the
./tests/interactive demo
application |
enum GtkImageTransp; GtkImageView; GtkWidget* gtk_image_view_new (void); gboolean gtk_image_view_get_viewport (GtkImageView *view, GdkRectangle *rect); gboolean gtk_image_view_get_draw_rect (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, gboolean invalidate); void gtk_image_view_set_transp (GtkImageView *view, GtkImageTransp transp, int transp_color); gboolean gtk_image_view_get_fitting (GtkImageView *view); void gtk_image_view_set_fitting (GtkImageView *view, gboolean fitting); 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);
GObject +----GInitiallyUnowned +----GtkObject +----GtkWidget +----GtkImageView +----GtkAnimView
"mouse-wheel-scroll" : Run Last "pixbuf-changed" : Run Last "scroll" : Run Last / Action "set-fitting" : 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 "mouse-wheel-scroll" : Run Last "pixbuf-changed" : Run Last "scroll" : Run Last / Action "set-fitting" : 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
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.
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. |
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. |
Operations on GtkImageView are executed in three different 2D coordinate systems:
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);
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 downsampling the image at the start. Then try minimizing and unminimizing the window. The image will reappear immidately 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
that the view shows. The right way to do that is to use the
gtk_image_view_set_pixbuf()
function with the old pixbuf as the
argument. In particular, code that merely tries to update the
view by requesting that it should be redrawn will not work.
// Do some operation on the pixbuf data here gtk_widget_queue_draw_area (10, 10, 50, 50) // Incorrect!
In future versions of GtkImageView a method will be added that marks an area of the pixbuf as dirty and forces the view to flush its cache.
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.
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:
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.
GtkWidget* gtk_image_view_new (void);
Creates a new image view with default values. The default values are:
FALSE
TRUE
GDK_INTERP_BILINEAR
NULL
TRUE
TRUE
Returns : |
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.
The current viewport is defined as the rectangle, in zoomspace coordinates as the area of the loaded pixbuf the GtkImageView is currently showing.
view : |
|
rect : |
|
Returns : |
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].
This method is useful when converting from widget to image or zoom space coordinates.
view : |
|
rect : |
|
Returns : |
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 : |
|
check_color1 : |
|
check_color2 : |
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 immidiately. 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 immidately than invalidating.
view : |
|
x : |
|
y : |
|
invalidate : |
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:
GTK_IMAGE_TRANSP_GRID
0x000000
view : |
|
transp : |
|
transp_color : |
gboolean gtk_image_view_get_fitting (GtkImageView *view);
Returns the fitting setting of the view.
view : |
|
Returns : |
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 immidiately repaint itself.
Fitting is by default TRUE
.
view : |
|
fitting : |
GdkPixbuf* gtk_image_view_get_pixbuf (GtkImageView *view);
Returns the pixbuf this view shows.
view : |
|
Returns : |
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 : |
|
pixbuf : |
|
reset_fit : |
gdouble gtk_image_view_get_zoom (GtkImageView *view);
Get the current zoom factor of the view.
view : |
|
Returns : |
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.
view : |
|
zoom : |
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 : |
|
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 : |
|
Returns : |
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 : |
|
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 : |
|
Returns : |
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 : |
|
interp : |
GdkInterpType gtk_image_view_get_interpolation (GtkImageView *view);
Returns the current interpolation mode of the view.
view : |
|
Returns : |
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 : |
|
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 : |
|
Returns : |
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 immidiately repaint itself.
The default image tool is a GtkImageToolDragger instance. See also GtkIImageTool.
view : |
|
tool : |
GtkIImageTool* gtk_image_view_get_tool (GtkImageView *view);
view : |
|
Returns : |
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 : |
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 : |
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. |
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. |
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. |
void user_function (GtkImageView *imageview, gint arg1, gpointer user_data) : Run Last / Action
@: @:
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. |
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. |
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. |
void user_function (GtkImageView *imageview, gpointer user_data) : Run Last / Action
@:
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. |
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. |
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. |
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. |
void user_function (GtkImageView *imageview, gint arg1, gpointer user_data) : Run Last / Action
@: @:
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. |
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. |
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. |
void user_function (GtkImageView *imageview, gpointer user_data) : Run Last / Action
@:
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. |