title: GtkDrawingArea - how to make it drawable?
body: <p>I'm going out of my mind a bit here. </p>
  
  <p>I'm trying to draw some simple graphics on my GTK form using cairo. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;gtk/gtk.h&gt;
  #include &lt;cairo.h&gt;
  
  GtkWidget* window;
  GtkWidget* darea; 
  
  
  int main(int argc, char **argv)
  {
      gtk_init(&amp;argc, &amp;argv);     
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_default_size(GTK_WINDOW(window), 390, 240);
  
      darea = gtk_drawing_area_new();
      gtk_container_add(GTK_CONTAINER(window), darea); 
  
      cairo_t *cr; 
      cr = gdk_cairo_create(darea-&gt;window);
      cairo_rectangle(cr, 0, 0, 100, 100); 
      cairo_fill(cr);
  
  
      gtk_widget_show_all(window);
  
      gtk_main(); 
  
      return 0;
  }
  </code></pre>
  
  <p>This compiles, but gives me  </p>
  
  <blockquote>
    <p>Gdk-CRITICAL **: IA__gdk_cairo_create: assertion `GDK_IS_DRAWABLE
    (drawable)' failed</p>
  </blockquote>
  
  <p>followed by a segfault. </p>
  
  <p>I've been looking at the <a href="http://zetcode.com/tutorials/cairographicstutorial/shapesfills/" rel="nofollow">tutorial here</a></p>
  
  <p>So I changed my code as follows, made the cairo calls occur inside the expose event. </p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;gtk/gtk.h&gt;
      #include &lt;cairo.h&gt;
  
      GtkWidget* window;
      GtkWidget* darea; 
  
      static gboolean
      on_expose_event(GtkWidget *widget,
          GdkEventExpose *event,
          gpointer data)
      {
          cairo_t *cr; 
          cr = gdk_cairo_create(darea-&gt;window);
          cairo_rectangle(cr, 0, 0, 100, 100); 
          cairo_fill(cr);
  
      }    
  
      int main(int argc, char **argv)
      {
          gtk_init(&amp;argc, &amp;argv);     
          window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
          gtk_window_set_default_size(GTK_WINDOW(window), 390, 240);
  
          darea = gtk_drawing_area_new();
          gtk_container_add(GTK_CONTAINER(window), darea); 
  
          g_signal_connect(darea, "expose-event",
            G_CALLBACK(on_expose_event), NULL);
  
  
          gtk_widget_show_all(window);
  
          gtk_main(); 
  
          return 0;
      }
  </code></pre>
  
  <p>Why does this fix it? 
  My understanding re: the expose is: 
  the</p>
  
  <pre><code>g_signal_connect(darea, "expose-event", G_GCALLBACK(on_expose_event), NULL); 
  </code></pre>
  
  <p>tells the program, 'when an expose event happens to darea, then call on_expose_event'. The null is where you can pass in a pointer to a struct of additional information for the function to use. </p>
  
  <p>and </p>
  
  <pre><code>    static gboolean
      on_expose_event(GtkWidget *widget,
          GdkEventExpose *event,
          gpointer data)
      {
  </code></pre>
  
  <p>means that on_expose_event is passed a pointer to the widget that the event happened to, andin this case because it's an expose event, a pointer to a struct containing information about the expose event, and a pointer to a struct to any other information you might like to add.</p>