1
votes

In zetcode "Lines" code, I am trying to display the lines in real time as the mouse button is clicked. I then changed the clicked function to

static gboolean clicked(GtkWidget *widget, GdkEventButton *event,
    gpointer user_data)
{
    if (event->button == 1) {
        glob.coordx[glob.count] = event->x;
        glob.coordy[glob.count++] = event->y;
        gtk_widget_queue_draw(widget);
    }

    return TRUE;
}

I was thinking it would display the lines every time button 1 was clicked, but they are not being drawn on the window at all. What am i missing here?

2

2 Answers

2
votes

The function that runs on draw, do_drawing(), contains this line at its end:

glob.count = 0;

so it clears the array after drawing them all, thus making it very hard to accumulate a large number of lines like you're trying to.

0
votes

If you are following the Zetcode example, you need to change the do_drawing method. This works by drawing a point in real time with each click.

static void do_drawing(cairo_t *cr)
{
  cairo_set_source_rgb(cr, 0, 0, 0);//Line colour
  cairo_set_line_width(cr, 0.5);//Line width
  cairo_translate(cr, -170, -170);//Shift where line 

  //i is starting point, i+1 is next mouse coordinate 
  int i;
  for (i = 0; i < glob.count - 1; i++ ) {
    cairo_move_to(cr, glob.coordx[i], glob.coordy[i]);
    cairo_line_to(cr, glob.coordx[i+1], glob.coordy[i+1]);
    printf("from x:%f, y:%f\t to: x:%f, y:%f\n",glob.coordx[i],glob.coordy[i], glob.coordx[i+1], glob.coordy[i+1]);
    cairo_stroke(cr);    
  }
}