Re: Help request: gnome-canvas and images from data



On Tue, Dec 31, 2002 at 06:28:05PM -0600, Frazer Williams wrote:
> I've figured out how to display a .png image from a file on the canvas,
> but I've bogged down on displaying an image from data (the functionality
> provided by gdk-pixbuf_new_from_data(), for example).
> 

I have rewritten your code. Now it works :-)


#include <gnome.h>

#define WIDTH   700
#define HEIGHT  600

static gint delete_event_cb(GtkWidget *window, GdkEventAny *e, gpointer data);

GtkWidget *app;

static void 
free_pixels(guchar* pixels, gpointer data)
{
    g_free(pixels);
}

static void
create_canvas_items(GtkWidget *canvas)
{
    GdkPixbuf        *pixbuf;
    guchar           *pixels;
    GnomeCanvasItem  *citem;
    GnomeCanvasGroup *group;
    gint             x, y;
    gint             pixel_offset;
    gint             rowstride;
    gfloat           step, color_offset;

    pixels = g_malloc(WIDTH*HEIGHT*3);

    rowstride = WIDTH * 3;
    step = 255.0 / HEIGHT;
    color_offset = 0.0;
    for(y=0; y<HEIGHT; y++) {
        for(x=0; x<WIDTH; x++) {
            pixel_offset = y * rowstride + x * 3;
            pixels[pixel_offset] = (guint)color_offset;     /* R */
            pixels[pixel_offset + 1] = 255;                 /* G */
            pixels[pixel_offset + 2] = (guint)color_offset; /* B */
        }

        color_offset += step;
    }

    group = gnome_canvas_root(GNOME_CANVAS(canvas));

    pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB,
                                      FALSE, 8, WIDTH, HEIGHT,
                                      WIDTH*3, free_pixels, NULL);
 
    citem = gnome_canvas_item_new (group,
                                   GNOME_TYPE_CANVAS_PIXBUF,
                                   "pixbuf", (GtkWidget *)pixbuf,
                                   "x", .0, "y", 0.,
                                   NULL);
    gdk_pixbuf_unref(pixbuf);
    gnome_canvas_item_show(citem);
}

int
main(int argc, char *argv[])
{
    GtkWidget *sw;
    GtkWidget *canvas;

    gnome_init("canvas-example", "0.0", argc, argv);

    app = gnome_app_new("mytest", "My test program");
    gtk_widget_set_usize(GTK_WIDGET(app), WIDTH, HEIGHT);
    gtk_signal_connect(GTK_OBJECT(app), "delete_event",
                       GTK_SIGNAL_FUNC(delete_event_cb),
                       NULL);
    
    sw = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
                                   GTK_POLICY_AUTOMATIC,
                                   GTK_POLICY_AUTOMATIC);

    canvas = gnome_canvas_new_aa();

    gnome_canvas_set_scroll_region(GNOME_CANVAS(canvas), 0, 0,
                                   WIDTH, HEIGHT);

    gtk_container_add(GTK_CONTAINER(sw), canvas);
    gnome_app_set_contents(GNOME_APP(app), sw);
    gtk_widget_show_all(app);
    create_canvas_items(canvas);
    gtk_widget_show_all(app);

    gtk_main();

    return 0;
}

static gint
delete_event_cb(GtkWidget *window, GdkEventAny *e, gpointer data)
{
    gtk_main_quit();
    return FALSE;
}

-- 
Ayose Cazorla León
Debian GNU/Linux - setepo



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