Re: drawing with GTK 3.0

2011-02-12 Thread Allin Cottrell
On Sat, 12 Feb 2011, Allin Cottrell wrote:

> I've got my app to build against gtk 3.0 but I'm having trouble
> getting some drawing code ported...

Ah, I now see what part of my problem was: I hadn't looked up the
latest signature for the GtkWidget "draw" callback. I had this as

> void plot_draw (GtkWidget *canvas, GdkRectangle *rect,
> gpointer data)

but the second argument should now be a cairo_t * (the doc
actually says a "CairoContext *", but does that type exist?).
So my revised callback looks like this:

void plot_draw (GtkWidget *widget, cairo_t *cr, gpointer data)
{
png_plot *plot = data;

gdk_cairo_set_source_pixbuf(cr, plot->pixbuf, 0, 0);
cairo_paint(cr);
}

Now my image will display, but dynamic changes to the image (e.g.
rubber-banding of a selection rectangle) that were smooth in gtk
2.24 are now jerky, and I guess this is because the above code
(re-)paints the entire image, whereas the gtk 2.24 version gets an
event rectangle from the "expose-event" signal and so redraws only
the affected area. If that diagnosis is plausible (?) how can we
avoid doing a full redraw with the new mechanism?

Allin Cottrell
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


drawing with GTK 3.0

2011-02-12 Thread Allin Cottrell
I've got my app to build against gtk 3.0 but I'm having trouble
getting some drawing code ported. It was OK as of the
"transitional" state part-way through Benjamin Otte's gdk cleanup
and it works with gtk 2.24, but my attempt at gtk 3.0 code fails:
I get an empty canvas window where there's supposed to be an
image.

I'd be very grateful if anyone can tell me what I'm missing. Below
is an extract of code showing what works and what doesn't; I've
tried to keep it to the minimal relevant portion. ("plot" is just
a wrapper struct for the various bits and pieces: it has a
GdkPixmap member in the 2.24 variant and I'm struggling to figure
how exactly that should be replaced.)

0. basic setup of canvas

common to gtk 2.24, gtk 3.0 --

plot->canvas = gtk_drawing_area_new();
gtk_widget_set_size_request(GTK_WIDGET(plot->canvas),
plot->pixel_width, plot->pixel_height);
gtk_widget_set_events (plot->canvas, GDK_EXPOSURE_MASK
   | GDK_LEAVE_NOTIFY_MASK
   | GDK_BUTTON_PRESS_MASK
   | GDK_BUTTON_RELEASE_MASK
   | GDK_POINTER_MOTION_MASK
   | GDK_POINTER_MOTION_HINT_MASK);
gtk_widget_show(plot->canvas);
gtk_widget_realize(plot->canvas);

gtk 2.24 --

gdk_window_set_back_pixmap(gtk_widget_get_window(plot->canvas),
   NULL, FALSE);
plot->pixmap = gdk_pixmap_new(gtk_widget_get_window(plot->canvas),
  plot->pixel_width, plot->pixel_height,
  -1);
g_signal_connect(G_OBJECT(plot->canvas), "expose-event",
 G_CALLBACK(plot_expose), plot);

gtk 3.0 --

/* no counterpart to pixmap operations above? */
g_signal_connect(G_OBJECT(plot->canvas), "draw",
 G_CALLBACK(plot_draw), plot);

1. initialize with GdkPixbuf, pbuf, obtained from PNG file:

gtk 2.24 --

plot->cr = gdk_cairo_create(plot->pixmap);
gdk_cairo_set_source_pixbuf(plot->cr, pbuf, 0, 0);
cairo_paint(plot->cr);
cairo_destroy(plot->cr);
g_object_unref(pbuf); /* done with pbuf */

gtk 3.0 --

plot->cr = gdk_cairo_create(gtk_widget_get_window(plot->canvas));
gdk_cairo_set_source_pixbuf(plot->cr, pbuf, 0, 0);
cairo_paint(plot->cr);
cairo_destroy(plot->cr);
plot->pixbuf = pbuf; /* need to attach pbuf? */

2. "expose-event"/"draw" callback:

gtk 2.24 --

void plot_expose (GtkWidget *canvas, GdkEventExpose *event,
  gpointer data)
{
GdkWindow *window = gtk_widget_get_window(canvas);
png_plot *plot = data;

plot->cr = gdk_cairo_create(window);
gdk_cairo_set_source_pixmap(plot->cr, plot->pixmap, 0, 0);
gdk_cairo_rectangle(plot->cr, &event->area);
cairo_fill(plot->cr);
cairo_destroy(plot->cr);
}

gtk 3.0

void plot_draw (GtkWidget *canvas, GdkRectangle *rect,
gpointer data)
{
GdkWindow *window = gtk_widget_get_window(canvas);
png_plot *plot = data;

plot->cr = gdk_cairo_create(window);
/* ?? */
gdk_cairo_set_source_pixbuf(plot->cr, plot->pixbuf, 0, 0);
gdk_cairo_rectangle(plot->cr, rect);
cairo_fill(plot->cr);
cairo_destroy(plot->cr);
}

-- 
Allin Cottrell
Department of Economics
Wake Forest University

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list