Re: GdkPixmap and GdkPixbuf
- From: y g <odysseus lost gmail com>
- To: "David Necas (Yeti)" <yeti physics muni cz>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GdkPixmap and GdkPixbuf
- Date: Mon, 6 Jun 2005 15:52:32 +0100
On 6/6/05, David Necas (Yeti) <yeti physics muni cz> wrote:
On Mon, Jun 06, 2005 at 03:22:51PM +0100, y g wrote:
after following the scribble example on the Gtk tutorial I try to save
the image produced. Looking on the documentation of GdkPixbuf I found
I could use the following which however gives me a segmentation fault
in my program.
static GdkPixmap *pixmap = NULL;
gdk_pixbuf_save(GDK_PIXBUF(pixmap), "temp.bmp", "bmp", NULL);
I know that pixmap is of type of GdkPixmap and that arg 1 of the
function must be a GDK_PIXBUF and hence the cast. But as I said I am
getting a segmentation fault. Any clues why?
No matter the bad typecast, you are trying to save a NULL
pointer. So what else you can expect than a segfault?
Sorry for that my fault as i wasn't very clear, It is a global and
initialised as null but it is assigned later.
And to the bad typecast. Neither of GdkPixbuf, GdkPixmap is
a child of the other. See gdk_pixbuf_get_from_drawable()
for how to get a GdkPixbuf from a drawable.
I already did as I realised that neither is a child of the other as
you say. But I am still get seg fault with the following:
static void
save_image(GtkWidget* widget, gpointer user_data)
{
GdkPixbuf* pixbuf;
g_print("Saving image...\n");
//gdk_pixbuf_save(gdk_pixbuf_get_from_drawable(NULL,
GDK_DRAWABLE(pixmap), NULL, 0, 0, 0, 0, 600, 400), "temp.bmp", "bmp",
NULL);
gdk_pixbuf_get_from_drawable(pixbuf, GDK_DRAWABLE(pixmap), NULL, 0,
0, 0, 0, 600, 400);
gdk_pixbuf_save(pixbuf, "temp.bmp", "bmp", NULL);
}
Here is the full program, not big, just the scribble from what I
understood of it.
#include <gtk/gtk.h>
#include <gdk/gdk.h>
/* Backing pixmap for drawing area */
static GdkPixmap *pixmap = NULL;
static void
save_image(GtkWidget* widget, gpointer user_data)
{
GdkPixbuf* pixbuf;
g_print("Saving image...\n");
//gdk_pixbuf_save(gdk_pixbuf_get_from_drawable(NULL,
GDK_DRAWABLE(pixmap), NULL, 0, 0, 0, 0, 600, 400), "temp.bmp", "bmp",
NULL);
gdk_pixbuf_get_from_drawable(pixbuf, GDK_DRAWABLE(pixmap), NULL, 0,
0, 0, 0, 600, 400);
gdk_pixbuf_save(pixbuf, "temp.bmp", "bmp", NULL);
}
/* Draw a rectangle on the screen */
static void
draw_brush (GtkWidget *widget, gdouble x, gdouble y)
{
g_print("draw_brush\n");
GdkRectangle update_rect;
update_rect.x = x - 5;
update_rect.y = y - 5;
update_rect.width = 10;
update_rect.height = 10;
gdk_draw_rectangle (pixmap,
widget->style->black_gc,
TRUE,
update_rect.x, update_rect.y,
update_rect.width, update_rect.height);
gtk_widget_queue_draw_area (widget,
update_rect.x, update_rect.y,
update_rect.width, update_rect.height);
}
/* Redraw the screen from the backing pixmap */
static gboolean
expose_event( GtkWidget *widget, GdkEventExpose *event )
{
g_print("expose_event\n");
gdk_draw_drawable(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
return FALSE;
}
/* Create a new backing pixmap of the appropriate size */
static gboolean
configure_event( GtkWidget *widget, GdkEventConfigure *event )
{
g_print("configure_event\n");
if (pixmap)
g_object_unref(pixmap);
pixmap = gdk_pixmap_new(widget->window,
widget->allocation.width,
widget->allocation.height,
-1);
gdk_draw_rectangle (pixmap,
widget->style->white_gc,
TRUE,
0, 0,
widget->allocation.width,
widget->allocation.height);
return TRUE;
}
static gboolean
button_press_event( GtkWidget *widget, GdkEventButton *event )
{
g_print("button_press_event\n");
if (event->button == 1 && pixmap != NULL)
draw_brush (widget, event->x, event->y);
return TRUE;
}
static gboolean
motion_notify_event( GtkWidget *widget, GdkEventMotion *event )
{
int x, y;
GdkModifierType state;
g_print("motion_notify_event\n");
if (event->is_hint)
gdk_window_get_pointer (event->window, &x, &y, &state);
else
{
x = event->x;
y = event->y;
state = event->state;
}
if (state & GDK_BUTTON1_MASK && pixmap != NULL)
draw_brush (widget, x, y);
return TRUE;
}
int
main(int argc, char *argv[])
{
GtkWidget* main;
GtkWidget* drawing_area;
GtkWidget* vbox1;
GtkWidget* buttonSave;
gtk_set_locale ();
gtk_init (&argc, &argv);
main = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(main), "Drawing area");
vbox1 = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox1);
gtk_container_add (GTK_CONTAINER (main), vbox1);
drawing_area = gtk_drawing_area_new();
gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_area), 600, 400);
gtk_box_pack_start (GTK_BOX (vbox1), drawing_area, TRUE, TRUE, 0);
//gtk_container_add(GTK_CONTAINER(main), drawing_area);
buttonSave = gtk_button_new_from_stock ("gtk-save");
gtk_box_pack_start (GTK_BOX (vbox1), buttonSave, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
(GtkSignalFunc) expose_event, NULL);
gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
(GtkSignalFunc) configure_event, NULL);
gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
(GtkSignalFunc) motion_notify_event, NULL);
gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
(GtkSignalFunc) button_press_event, NULL);
gtk_signal_connect (GTK_OBJECT (buttonSave), "clicked",
(GtkSignalFunc) save_image, NULL);
gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
gtk_widget_show(vbox1);
gtk_widget_show(drawing_area);
gtk_widget_show(buttonSave);
gtk_widget_show(main);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]