title: Draw a GtkImage with Cairo
body: <p>I tried the solutions proposed in similary notes (<a href="https://stackoverflow.com/questions/4521441/how-to-create-a-gtkimage-from-a-cairo-context">How to create a gtkimage from a cairo context</a>, <a href="https://stackoverflow.com/questions/5685262/how-do-i-create-a-gtkimage-from-a-cairo-surface">How do I create a gtkimage from a cairo surface</a>)
  but they didn't work for me.</p>
  <p>In my actual program, a background task calculates successive states of a physics simulation, and each iteration should add a point to a diagram. If I used a GtkDrawingArea, I would have to store all these points (not knowing in advance how many there will be), and draw everything upon each draw signal. So my idea is to use a GtkImage, where I have to draw only the one new point in each iteration.</p>
  <p>In the following test program something should be drawn upon a button click, but nothing happens. Can somebody please indicate the error?</p>
  <pre><code>/* Test for drawing an image with Cairo. To be compiled with:
     gcc -Wall -g gtk_img.c -o gtk_img `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;gdk/gdk.h&gt;
  #include &lt;cairo.h&gt;
  #include &lt;gtk/gtk.h&gt;
  
  #define BORDER      6
  #define WIDTH       100
  #define HEIGHT      100
  
  cairo_surface_t *surf;
  cairo_t         *cr;
  cairo_pattern_t *back, *fore;
  
  
  /*  Event Handlers
      ==============
  */
  
  static gboolean Delete_Event
         (GtkWidget *widget, GdkEvent *event, gpointer data)
  {
      return FALSE;
  }
  
  static void Destroy (GtkWidget *widget, gpointer data)
  {
      gtk_main_quit ();
  }
  
  static void Draw_Now (GtkWidget *widget, gpointer data)
  {
      printf(&quot;Draw button clicked\n&quot;);
      cr = cairo_create (surf);
      cairo_set_source (cr, back);
      cairo_paint (cr);
      cairo_set_source (cr, fore);
      cairo_move_to (cr, 0.0, 0.0);
      cairo_line_to (cr, 20.0, 20.0);
      cairo_stroke (cr);
  }
  
  
  /*  Main Program
      ============
  */
  
  int main (int argc, char *argv[])
  {
      GtkWidget   *window;
      GtkImage    *img;
      GtkBox      *vbox;
      GtkButton   *button;
      
      // init GTK, make window with vbox
      gtk_init (&amp;argc, &amp;argv);
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      g_signal_connect (G_OBJECT (window), &quot;delete_event&quot;,
                G_CALLBACK (Delete_Event), NULL);
      g_signal_connect (G_OBJECT (window), &quot;destroy&quot;,
                G_CALLBACK (Destroy), NULL);
      gtk_container_set_border_width (GTK_CONTAINER (window), BORDER);
      vbox = (GtkBox*)gtk_box_new (GTK_ORIENTATION_VERTICAL, BORDER);
      gtk_container_add (GTK_CONTAINER(window), (GtkWidget*)vbox);
  
      // make image
      surf = cairo_image_surface_create (CAIRO_FORMAT_RGB24, WIDTH, HEIGHT);
      img = (GtkImage*)gtk_image_new_from_surface (surf);
      gtk_image_clear (img);
      back = cairo_pattern_create_rgb (0.9, 0.9, 0.9);
      fore = cairo_pattern_create_rgb (0.1, 0.1, 0.1);
      gtk_box_pack_start (vbox, (GtkWidget*)img, TRUE, TRUE, FALSE);
  
      // button
      button = (GtkButton*)gtk_button_new_with_label (&quot;Draw&quot;);
      g_signal_connect
         (G_OBJECT (button), &quot;clicked&quot;, G_CALLBACK (Draw_Now), NULL);
      gtk_box_pack_start (vbox, (GtkWidget*)button, FALSE, FALSE, FALSE);
  
      // start GTK
      gtk_widget_show_all (window);
      gtk_main ();
      
      return 0;
  }
  </code></pre>