]> Pileus Git - ~andy/gtk/blobdiff - demos/gtk-demo/drawingarea.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / demos / gtk-demo / drawingarea.c
index 5e80449c7332c1d2015ba1279a2437fbf9f98e99..db5a62d97f4f7e144150d593f0297a6db22160fb 100644 (file)
 
 static GtkWidget *window = NULL;
 /* Pixmap for scribble area, to store current scribbles */
-static GdkPixmap *pixmap = NULL;
+static cairo_surface_t *surface = NULL;
 
-/* Create a new pixmap of the appropriate size to store our scribbles */
+/* Create a new surface of the appropriate size to store our scribbles */
 static gboolean
 scribble_configure_event (GtkWidget         *widget,
                           GdkEventConfigure *event,
                           gpointer           data)
 {
+  GtkAllocation allocation;
   cairo_t *cr;
 
-  if (pixmap)
-    g_object_unref (pixmap);
+  if (surface)
+    cairo_surface_destroy (surface);
 
-  pixmap = gdk_pixmap_new (widget->window,
-                           widget->allocation.width,
-                           widget->allocation.height,
-                           -1);
+  gtk_widget_get_allocation (widget, &allocation);
+  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
+                                               CAIRO_CONTENT_COLOR,
+                                               allocation.width,
+                                               allocation.height);
 
-  /* Initialize the pixmap to white */
-  cr = gdk_cairo_create (pixmap);
+  /* Initialize the surface to white */
+  cr = cairo_create (surface);
 
   cairo_set_source_rgb (cr, 1, 1, 1);
   cairo_paint (cr);
@@ -47,24 +49,14 @@ scribble_configure_event (GtkWidget         *widget,
   return TRUE;
 }
 
-/* Redraw the screen from the pixmap */
+/* Redraw the screen from the surface */
 static gboolean
-scribble_expose_event (GtkWidget      *widget,
-                       GdkEventExpose *event,
-                       gpointer        data)
+scribble_draw (GtkWidget *widget,
+               cairo_t   *cr,
+               gpointer   data)
 {
-  /* We use the "foreground GC" for the widget since it already exists,
-   * but honestly any GC would work. The only thing to worry about
-   * is whether the GC has an inappropriate clip region set.
-   */
-
-  gdk_draw_drawable (widget->window,
-                     widget->style->fg_gc[gtk_widget_get_state (widget)],
-                     pixmap,
-                     /* Only copy the area that was exposed. */
-                     event->area.x, event->area.y,
-                     event->area.x, event->area.y,
-                     event->area.width, event->area.height);
+  cairo_set_source_surface (cr, surface, 0, 0);
+  cairo_paint (cr);
 
   return FALSE;
 }
@@ -83,14 +75,16 @@ draw_brush (GtkWidget *widget,
   update_rect.width = 6;
   update_rect.height = 6;
 
-  /* Paint to the pixmap, where we store our state */
-  cr = gdk_cairo_create (pixmap);
+  /* Paint to the surface, where we store our state */
+  cr = cairo_create (surface);
 
   gdk_cairo_rectangle (cr, &update_rect);
   cairo_fill (cr);
 
+  cairo_destroy (cr);
+
   /* Now invalidate the affected region of the drawing area. */
-  gdk_window_invalidate_rect (widget->window,
+  gdk_window_invalidate_rect (gtk_widget_get_window (widget),
                               &update_rect,
                               FALSE);
 }
@@ -100,7 +94,7 @@ scribble_button_press_event (GtkWidget      *widget,
                              GdkEventButton *event,
                              gpointer        data)
 {
-  if (pixmap == NULL)
+  if (surface == NULL)
     return FALSE; /* paranoia check, in case we haven't gotten a configure event */
 
   if (event->button == 1)
@@ -118,7 +112,7 @@ scribble_motion_notify_event (GtkWidget      *widget,
   int x, y;
   GdkModifierType state;
 
-  if (pixmap == NULL)
+  if (surface == NULL)
     return FALSE; /* paranoia check, in case we haven't gotten a configure event */
 
   /* This call is very important; it requests the next motion event.
@@ -143,45 +137,38 @@ scribble_motion_notify_event (GtkWidget      *widget,
 
 
 static gboolean
-checkerboard_expose (GtkWidget      *da,
-                     GdkEventExpose *event,
-                     gpointer        data)
+checkerboard_draw (GtkWidget *da,
+                   cairo_t   *cr,
+                   gpointer   data)
 {
-  gint i, j, xcount, ycount;
-  cairo_t *cr;
+  gint i, j, xcount, ycount, width, height;
 
 #define CHECK_SIZE 10
 #define SPACING 2
 
-  /* At the start of an expose handler, a clip region of event->area
-   * is set on the window, and event->area has been cleared to the
+  /* At the start of a draw handler, a clip region has been set on
+   * the Cairo context, and the contents have been cleared to the
    * widget's background color. The docs for
    * gdk_window_begin_paint_region() give more details on how this
    * works.
    */
 
-  cr = gdk_cairo_create (da->window);
-  gdk_cairo_rectangle (cr, &event->area);
-  cairo_clip (cr);
-
   xcount = 0;
+  width = gtk_widget_get_allocated_width (da);
+  height = gtk_widget_get_allocated_height (da);
   i = SPACING;
-  while (i < da->allocation.width)
+  while (i < width)
     {
       j = SPACING;
       ycount = xcount % 2; /* start with even/odd depending on row */
-      while (j < da->allocation.height)
+      while (j < height)
         {
-          GdkGC *gc;
-
           if (ycount % 2)
             cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
           else
             cairo_set_source_rgb (cr, 1, 1, 1);
 
-          /* If we're outside event->area, this will do nothing.
-           * It might be mildly more efficient if we handled
-           * the clipping ourselves, but again we're feeling lazy.
+          /* If we're outside the clip, this will do nothing.
            */
           cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
           cairo_fill (cr);
@@ -194,8 +181,6 @@ checkerboard_expose (GtkWidget      *da,
       ++xcount;
     }
 
-  cairo_destroy (cr);
-
   /* return TRUE because we've handled this event, so no
    * further processing is required.
    */
@@ -207,9 +192,9 @@ close_window (void)
 {
   window = NULL;
 
-  if (pixmap)
-    g_object_unref (pixmap);
-  pixmap = NULL;
+  if (surface)
+    cairo_surface_destroy (surface);
+  surface = NULL;
 }
 
 GtkWidget *
@@ -231,7 +216,7 @@ do_drawingarea (GtkWidget *do_widget)
 
       gtk_container_set_border_width (GTK_CONTAINER (window), 8);
 
-      vbox = gtk_vbox_new (FALSE, 8);
+      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 8);
       gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
       gtk_container_add (GTK_CONTAINER (window), vbox);
 
@@ -254,8 +239,8 @@ do_drawingarea (GtkWidget *do_widget)
 
       gtk_container_add (GTK_CONTAINER (frame), da);
 
-      g_signal_connect (da, "expose-event",
-                        G_CALLBACK (checkerboard_expose), NULL);
+      g_signal_connect (da, "draw",
+                        G_CALLBACK (checkerboard_draw), NULL);
 
       /*
        * Create the scribble area
@@ -276,10 +261,10 @@ do_drawingarea (GtkWidget *do_widget)
 
       gtk_container_add (GTK_CONTAINER (frame), da);
 
-      /* Signals used to handle backing pixmap */
+      /* Signals used to handle backing surface */
 
-      g_signal_connect (da, "expose-event",
-                        G_CALLBACK (scribble_expose_event), NULL);
+      g_signal_connect (da, "draw",
+                        G_CALLBACK (scribble_draw), NULL);
       g_signal_connect (da,"configure-event",
                         G_CALLBACK (scribble_configure_event), NULL);