GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Apr 18, 2014 12:36 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Fitting image to GtkImage size
PostPosted: Thu Dec 29, 2011 11:50 am 
Offline
Familiar Face

Joined: Thu Dec 29, 2011 11:45 am
Posts: 5
I use gtk_image_set_from_file to load a picture into GtkImage, but it resizes the control so that the whole image is shown. I would like it to be scaled down to the current size of GtkImage widget. How to fit the picture in GtkImage?


Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Thu Dec 29, 2011 1:43 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
First you have to load you picture in an GdkPixBuf by using gdk_pixbuf_new_from_file, then you have to scale it by using gdk_pixbuf_scale or gdk_pixbuf_scale_simple. Something like this should work in gtk+2.0:

Code:
#define WIDTH 500
#define HEIGHT 500

#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    GtkWidget *image=gtk_image_new();
    gtk_container_add(GTK_CONTAINER(window),image);
    gtk_widget_set_size_request(image,WIDTH,HEIGHT);

    GdkPixbuf *original=gdk_pixbuf_new_from_file("test.jpg",NULL);
// error handling is missing
    GdkPixbuf *scaled=
        gdk_pixbuf_scale_simple(original,WIDTH,HEIGHT,GDK_INTERP_HYPER);
    gtk_image_set_from_pixbuf(GTK_IMAGE(image),scaled);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}


Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Thu Dec 29, 2011 6:26 pm 
Offline
Familiar Face

Joined: Thu Dec 29, 2011 11:45 am
Posts: 5
Seems fine but the thing is, that my GtkImage has to fill one part of VBox which changes size whenever the window is resizes. Nevertheless, I haven't got fixed width and height, I would have to extract it somehow from GtkImage control size. How to do that?


Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Thu Dec 29, 2011 8:47 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
It might be easier to draw your image in a drawing area by using cairo:

Code:
#include <gtk/gtk.h>

GdkPixbuf *scaled=NULL;
GdkPixbuf *original=NULL;

void on_size(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
{
    static gint height_old=-1;
    static gint width_old=-1;
    if ((width_old==allocation->width) && (height_old==allocation->height))
    {
        return;
    }
    if (scaled)
    {
        g_object_unref(scaled); // not sure if this prevents memory overflow
    }
    scaled=gdk_pixbuf_scale_simple(original,
                                   allocation->width,allocation->height, 
                                   GDK_INTERP_HYPER);
    height_old=allocation->height;
    width_old=allocation->width;
}

gboolean on_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    cairo_t *cr=gdk_cairo_create(gtk_widget_get_window(widget));
    gdk_cairo_set_source_pixbuf(cr,scaled,0,0);
    cairo_paint(cr);
    cairo_destroy(cr);
    return TRUE;
}

int main(int argc, char **argv)
{
    gtk_init(&argc,&argv);
    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    GtkWidget *drawing_area=gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(window),drawing_area);
    gtk_widget_set_app_paintable(drawing_area,FALSE);

    original=gdk_pixbuf_new_from_file("test.jpg",NULL);

    g_signal_connect(G_OBJECT(drawing_area),"size-allocate",
                     G_CALLBACK(on_size),NULL);
    g_signal_connect(G_OBJECT(drawing_area),"expose-event",
                     G_CALLBACK(on_expose),NULL);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}



Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Fri Dec 30, 2011 2:16 pm 
Offline
Familiar Face

Joined: Thu Dec 29, 2011 11:45 am
Posts: 5
Okay, It doesn't seem to work, or, which is more likely, I am unable to place this code into my application. I need the image to be shown after selecting it from GtkIconView. I think I have already handled that - in function which is called when item-activated event (signal? I'm getting used to GTK...) occurs I retrieve the path of the file to be shown and I'd like to trigger image drawing (in GtkDrawingArea as you suggested) from there - when the file is selected.

Nevertheless, what I got in the output using your solution was
Code:
(App1.exe:932): Gtk-CRITICAL **: gtk_widget_set_app_paintable: assertion `GTK
_IS_WIDGET (widget)' failed

(App1.exe:932): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(App1.exe:932): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion `G
_TYPE_CHECK_INSTANCE (instance)' failed

(App1.exe:932): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(App1.exe:932): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion `G
_TYPE_CHECK_INSTANCE (instance)' failed


Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Fri Dec 30, 2011 6:07 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
You have called gtk_widget_set_app_paintable() with the argument widget=NULL. You have forgotten to initialize widget=gtk_widget_drawing_area_new() or whatever you have called the drawing area..


Top
 Profile  
 
 Post subject: Re: Fitting image to GtkImage size
PostPosted: Sun Jan 01, 2012 3:24 pm 
Offline
Familiar Face

Joined: Thu Dec 29, 2011 11:45 am
Posts: 5
How to get GdkRectangle of GtkDrawingArea manually? Or else: I'd like to paint the picture after loading it from file (file name got with dialog in Button-Clicked event). By the way, can I put GtkDrawingArea in a scrolled window container?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group