loading an image with gtk_image_new_from_pixbuf and gtk_image_set_from_pixbuf



I tried to write a little application in gtk 2.0.
My aim is to load a bitmap , and then that pressing a button will load a different
bitmap.
I want to use the gtk_image_new_from_pixbuf() and gtk_image_set_from_pixbuf() methods of gtk pixbuf ; I do not want to use drawing area and gdk_draw() methods.

Now, loading a bitmap works; but pressing a button to call a callback
which loads an image from a file and displays it does not work.
But I see that I do reach the callback and gdk_pixbuf_new_from_file(()
does succeed,

(I was little inspired by the stockbrowser example)

Here is the code ; It s not long ; It can be compiled and run.

I will appreciate any help/idea

// TEST.C

#include <gtk/gtk.h>

static GtkWidget* window;

typedef struct WidgetDisplay
        {
 GtkWidget* myImage;
        } WidgetDisplay;

WidgetDisplay* display;


static void close(GtkWidget *widget, gpointer data)
{
        gtk_main_quit();
}



static void refresh_cb(GtkWidget* w, gpointer data)
        {
        GtkWidget *image;

        char* filenmae = "ice-blue.jpg";
        printf("in refresh_cb \n");
        GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(filenmae,NULL);
        if (pixbuf == NULL)
                printf("pixbuf == NULL \n");
        else
                printf("pixbuf != NULL \n");
        display->myImage = gtk_image_new_from_pixbuf(pixbuf);
        if (display->myImage == NULL)
                {
                printf("display->myImage is NULL\n");
                }

 gtk_image_set_from_pixbuf(GTK_IMAGE(display->myImage),
                          GDK_PIXBUF(pixbuf));
        gtk_widget_show_all(window);

        printf("after gtk_widget_show_all \n");

        }


//////////////////////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{
        GtkWidget *image;
        GdkPixbuf *pixbuf;
 GtkWidget *vbox;
        GtkWidget* frame;
        GtkWidget* hbox;
 GtkWidget *align;

        GtkWidget* b;
        GError *err = NULL;
        int width;
 int height;


        gtk_init(&argc, &argv);
        b = gtk_button_new();

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        frame = gtk_frame_new("test");

 display = g_new (WidgetDisplay, 1);

        hbox = gtk_hbox_new (FALSE, 8);
        gtk_container_add (GTK_CONTAINER (window), hbox);

        align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
        gtk_box_pack_end (GTK_BOX (hbox), align, FALSE, FALSE, 0);

        frame = gtk_frame_new ("Selected Item");
        gtk_container_add (GTK_CONTAINER (align), frame);

        vbox = gtk_vbox_new (FALSE, 8);
        gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
        gtk_container_add (GTK_CONTAINER (frame), vbox);


        if (argc != 2)
                {
                g_error("Usage: %s file\n\n", argv[0]);
                }

        gtk_window_set_default_size(GTK_WINDOW(window),200,200);

g_signal_connect (G_OBJECT(window), "delete-event", G_CALLBACK(close), NULL);

        gtk_signal_connect(GTK_OBJECT(b),"clicked",GTK_SIGNAL_FUNC(refresh_cb),NULL);

        pixbuf = gdk_pixbuf_new_from_file(argv[1], &err);

        if (pixbuf == NULL)
                {
g_error("Can't open %s\nerror: (%d) %s", argv[0], err->code, err->message);
                printf("Can't open \n");
                }
        else
                printf("pixbuf is not NULL \n");

        display->myImage=gtk_image_new_from_pixbuf(pixbuf);
        gtk_container_add(GTK_CONTAINER(vbox), b);


        gtk_box_pack_start (GTK_BOX (vbox), display->myImage,
                                                                                        FALSE, FALSE, 0);

        gdk_pixbuf_unref(pixbuf);
        gtk_widget_show_all(window);

        gtk_main();
        }



regards,
John

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]