Reputation: 977
I am trying to paint a point on drawing_area
in Gtk
with cairo
.
I have two functions, do_drawing
function to paint the background on draw
signal, and the second one do_drawPoint
to draw a point.
void painter::do_drawing(cairo_t *cr)
{
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
}
void painter::do_pointDraw(cairo_t *cr)
{
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_arc(cr, 150, 150, 10, 0, 2 * M_PI);
cairo_fill(cr);
}
Now, the drawPoint
function is called whenever user clicks on the drawing_area
.
void drawingArea::drawPoint()
{
g_signal_connect(area, "button-press-event",
G_CALLBACK(clicked), NULL);
std::cout<<"drawPoint"<<std::endl;
}
gboolean clicked(GtkWidget *widget, GdkEventButton *event, cairo_t *cr,
gpointer user_data)
{
if (event->button == 1) {
x = event->x;
y = event->y;
}
std::cout<<x<<" and "<<y<<std::endl;
ptr->do_pointDraw(cr);
gtk_widget_queue_draw(widget);
}
return TRUE;
}
But after printing values of x
and y
, the program terminates with segmentation fault.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff74ac3ae in cairo_set_source_rgb ()
What might be causing this? Am I proceeding correctly?
Upvotes: 0
Views: 445
Reputation: 14577
It's hard to read the code (with broken indentation and with only snippets provided), but some things to note:
button-press-event
handler signature is incorrect. The cairo context is not there.draw
signal handler of GtkDrawingArea. The idea of drawing on button press is just not how Cairo works: user interaction should change the state of your app, draw-handler should draw based on the current app state.button-press-event
handler is connected inside drawPoint ()
. This sounds very wrong.I suggest you try reading the DrawingArea docs and start with the simple example it gives, and then start adding more complex things when that works.
Upvotes: 2