FranciscoS
FranciscoS

Reputation: 11

gtk_adjustment_get_value segmentation fault

I'm trying to code some simple stuff in C, with GTK+ and Cairo, but I'm having some troubles using the gtk_adjustment_get_value function.

This is my code:

#include <cairo.h>
#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>
#include <gtk/gtkhscale.h>
#include <math.h>

typedef struct
{
  GtkAdjustment *adj[2];    //Adjustments para as escalas
  GtkWidget *scale[2];      //Escalas
} ProgInfo;

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

  cr = gdk_cairo_create(widget->window);

  pinfo = (ProgInfo *) data;

  pos1x = gtk_adjustment_get_value (pinfo->adj[0]);

  //Objecto

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

  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 1.0);
  cairo_rectangle (cr, (gint) pos3x, (gint) pos3y, 5, 80);
  cairo_stroke_preserve (cr);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_fill (cr);

  cairo_stroke(cr);
  cairo_destroy(cr);

  return FALSE;
}


int main (int argc, char *argv[])
{

  GtkWidget *window, *vbox, *vbox1, *frame, *frame1, *frame2, *label, *label1, *label2, *hbox, *hbox1, *hscale;
  char string[40] = "Posição Objecto", string1[30] = "Posição Espelho";
  ProgInfo *pinfo;
  gdouble pos1x;
  pinfo = (ProgInfo *) calloc (1, sizeof (ProgInfo));

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);
  g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title (GTK_WINDOW(window), "Projecto Óptica");
  gtk_window_set_default_size (GTK_WINDOW(window), 800, 600); 
  gtk_widget_set_app_paintable (window, TRUE);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (vbox), hbox1);

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_end (GTK_BOX (hbox1), vbox1, FALSE, FALSE, 20);

  //Posição Objecto
  label2 = gtk_label_new (string);
  gtk_box_pack_start (GTK_BOX (vbox1), label2, FALSE, FALSE, 20);

  pinfo->adj[0] = (GtkAdjustment *) gtk_adjustment_new (0.7, 0.0, 1.0, 0.001, 1.0, 0.0);
  //Os parâmetros são, por ordem: valor inicial, valor mínimo e valor máximo
  pinfo->scale[0] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[0]));
  gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[0], FALSE, TRUE, 5);

  //Posição Espelho
  label1 = gtk_label_new (string1);
  gtk_box_pack_start (GTK_BOX (vbox1), label1, FALSE, FALSE, 20);

  pinfo->adj[1] = (GtkAdjustment *)gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
  pinfo->scale[1] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[1]));
  gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[1], FALSE, FALSE, 0);

  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}

I created a couple of sliders with the hscale widget, and they show up fine in the window, I can move them, everything's alright. But I want to make my variable, pos1x, change when I move the slider. I tried using the gtk_adjustment_get_value function to do it, but I'm getting a segmentation fault. I've noticed that if I comment out this line:

  pos1x = gtk_adjustment_get_value (pinfo->adj[0]);

The segmentation fault does not happen anymore, but the app doesn't do what I want either. I realize that this is probably very simple, and my doubts derive from scarce understanding of how GTK+ works, but I've been trying for the last few hours and I can't figure out what's wrong. I'd appreciate any help you have to offer.

Oh, and sorry for putting such a big block of code here, but I wasn't sure which parts you'd need.

Upvotes: 0

Views: 196

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14577

You connect to the expose-event here with the userdata pointer set to NULL:

g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);

but in the handler you expect it to be a pointer to ProgInfo. You should replace NULL with pinfo.

Upvotes: 4

Related Questions