0

I'm coding with C, GTK+ and cairo. I'm trying to use a slider to change the value of a variable. This is what I have so far:

adj = (GtkAdjustment *) gtk_adjustment_new (300.0, -50.0, 500.0, 1.0, 1.0, 1.0);
scale = gtk_hscale_new (GTK_ADJUSTMENT (adj));
gtk_box_pack_start (GTK_BOX (vbox1), scale, FALSE, TRUE, 5);
gtk_signal_connect(GTK_OBJECT(adj), "value_changed", GTK_SIGNAL_FUNC(value_changed), NULL); 

This is where I create the adjustment and send the signal whenever the user moves the slider.

double 
value_changed (GtkAdjustment *adj)
{
  pos1x = gtk_adjustment_get_value(adj);
  printf ("\n%lf", pos1x);
  return pos1x;
}

Here I change the pos1x value to the value the slider has.

gboolean
on_expose_event (GtkWidget *widget, 
         GdkEventExpose *event, 
         gpointer data)
{
  cairo_t *cr;
  double  posy;
  static gdouble pos2x = 450., pos2y = 290.; //Coordenadas Espelho
  static gdouble pos3x = 450., pos3y = 250.;

  cr = gdk_cairo_create(widget->window);
  pos1x = gtk_adjustment_get_value(adj);
  posy = 250.;

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 1.0);
  cairo_rectangle (cr, (double) pos1x, (double) posy, 20, 80);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_fill (cr);

Now I want to use the pos1x variable to change the coordinates of the rectangle I created with cairo as the user moves the slider. However, I can only get the initial value of the adjustment, not the changes. I'm having trouble thinking of a way to do this, and I'd appreciate it if you could lend me a hand.

Thanks in advance.

liberforce
  • 11,189
  • 37
  • 48
FranciscoS
  • 11
  • 3

1 Answers1

1

You need to call gdk_window_invalidate_rect() or something similar from your value_changed() function to have GTK+ re-emit expose-event.