drawing with GTK 3.0
- From: Allin Cottrell <cottrell wfu edu>
- To: gtk-app-devel-list gnome org
- Subject: drawing with GTK 3.0
- Date: Sat, 12 Feb 2011 11:08:14 -0500 (EST)
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]