cgtkpixbuf

How to load GdkPixbuf from file in a callback function?


I'm using gtk+ 3.14 and I want to load a picture that the user chooses. The code here correctly displays the picture but doest not seem to update the GdkPixbuf *pixbuf. In fact, GdkPixbuf is not NULL inside the function but it is NULL outside it. What can I do to correctly load and use pixbuf in other functions ?

Here is the callback structure :

struct callback_struct
{
  GdkPixbuf *pix;
  GtkWidget *img;
  int height;
  int width;
  float scale;
};

Here is my code :

void callback_load(gpointer data)
{
  struct callback_struct *passed = data;
  GdkPixbuf *pixbuf = passed->pix;
  GtkWidget *image = passed->img;
  int h_scr = passed ->height;
  int w_scr = passed->width;
  float scale = passed->scale;
  GtkWidget *dial_box = gtk_file_chooser_dialog_new("Choose the image to load"
          ,GTK_WINDOW(gtk_widget_get_toplevel(image)),
          GTK_FILE_CHOOSER_ACTION_OPEN,
          "Cancel",GTK_RESPONSE_CANCEL,"Open",GTK_RESPONSE_ACCEPT,NULL);
  switch (gtk_dialog_run (GTK_DIALOG (dial_box)))
  {
    case GTK_RESPONSE_ACCEPT:
    {
      gchar *filename =gtk_file_chooser_get_filename
          (GTK_FILE_CHOOSER (dial_box));
      pixbuf = gdk_pixbuf_new_from_file(filename,NULL);
      printf("%d\n",(pixbuf == 0));
      GdkPixbuf *scaled_buffer = gdk_pixbuf_scale_simple
           (pixbuf,h_scr*scale,w_scr*scale,GDK_INTERP_BILINEAR);
      gtk_image_set_from_pixbuf(GTK_IMAGE(image),scaled_buffer);
      printf("%d\n",(pixbuf == 0));
      gtk_widget_destroy(dial_box);
      break;
    }
    case GTK_RESPONSE_CANCEL:
    {
        gtk_widget_destroy(dial_box);
        break;
    }
  }
}

Solution

  • You only modify the local pointer pixbuf: in fact passed->pix is NULL throughout the code.

    You should either not use a local pointer at all (and just refer to passed->pix) , or alternatively set the structs pointer equal to the local pointer at some point after initializing it.