]> Pileus Git - ~andy/gtk/blobdiff - tests/testcairo.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testcairo.c
index 141bb8517f98c4e08ae374626d22f2a292fcf038..8c45bc1c9b78074458d6a21b40bb56c491c574b5 100644 (file)
@@ -15,9 +15,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <math.h>
@@ -29,10 +27,7 @@ oval_path (cairo_t *cr,
            double xc, double yc,
            double xr, double yr)
 {
-  cairo_matrix_t *matrix;
-
-  matrix = cairo_matrix_create ();
-  cairo_current_matrix (cr, matrix);
+  cairo_save (cr);
 
   cairo_translate (cr, xc, yc);
   cairo_scale (cr, 1.0, yr / xr);
@@ -43,8 +38,7 @@ oval_path (cairo_t *cr,
             0, 2 * G_PI);
   cairo_close_path (cr);
 
-  cairo_set_matrix (cr, matrix);
-  cairo_matrix_destroy (matrix);
+  cairo_restore (cr);
 }
 
 /* Create a path that is a circular oval with radii xr, yr at xc,
@@ -119,79 +113,67 @@ draw_3circles (cairo_t *cr,
   cairo_fill (cr);
 }
 
-static void
-draw (cairo_t *cr,
-      int      width,
-      int      height)
+static gboolean
+on_draw (GtkWidget *widget,
+         cairo_t   *cr)
 {
   cairo_surface_t *overlay, *punch, *circles;
+  cairo_t *overlay_cr, *punch_cr, *circles_cr;
 
   /* Fill the background */
+  int width = gtk_widget_get_allocated_width (widget);
+  int height = gtk_widget_get_allocated_height (widget);
   double radius = 0.5 * (width < height ? width : height) - 10;
   double xc = width / 2.;
   double yc = height / 2.;
 
-  overlay = cairo_surface_create_similar (cairo_current_target_surface (cr),
-                                         CAIRO_FORMAT_ARGB32,
+  overlay = cairo_surface_create_similar (cairo_get_target (cr),
+                                         CAIRO_CONTENT_COLOR_ALPHA,
                                          width, height);
-  if (overlay == NULL)
-    return;
 
-  punch = cairo_surface_create_similar (cairo_current_target_surface (cr),
-                                       CAIRO_FORMAT_A8,
+  punch = cairo_surface_create_similar (cairo_get_target (cr),
+                                       CAIRO_CONTENT_ALPHA,
                                        width, height);
-  if (punch == NULL)
-    return;
 
-  circles = cairo_surface_create_similar (cairo_current_target_surface (cr),
-                                         CAIRO_FORMAT_ARGB32,
+  circles = cairo_surface_create_similar (cairo_get_target (cr),
+                                         CAIRO_CONTENT_COLOR_ALPHA,
                                          width, height);
-  if (circles == NULL)
-    return;
     
   fill_checks (cr, 0, 0, width, height);
 
-  cairo_save (cr);
-  cairo_set_target_surface (cr, overlay);
-  cairo_identity_matrix (cr);
-
   /* Draw a black circle on the overlay
    */
-  cairo_set_source_rgb (cr, 0., 0., 0.);
-  oval_path (cr, xc, yc, radius, radius);
-  cairo_fill (cr);
-
-  cairo_save (cr);
-  cairo_set_target_surface (cr, punch);
+  overlay_cr = cairo_create (overlay);
+  cairo_set_source_rgb (overlay_cr, 0., 0., 0.);
+  oval_path (overlay_cr, xc, yc, radius, radius);
+  cairo_fill (overlay_cr);
 
   /* Draw 3 circles to the punch surface, then cut
    * that out of the main circle in the overlay
    */
-  draw_3circles (cr, xc, yc, radius, 1.0);
+  punch_cr = cairo_create (punch);
+  draw_3circles (punch_cr, xc, yc, radius, 1.0);
+  cairo_destroy (punch_cr);
 
-  cairo_restore (cr);
-
-  cairo_set_operator (cr, CAIRO_OPERATOR_OUT_REVERSE);
-  cairo_set_source_surface (cr, punch, 0, 0);
-  cairo_paint (cr);
+  cairo_set_operator (overlay_cr, CAIRO_OPERATOR_DEST_OUT);
+  cairo_set_source_surface (overlay_cr, punch, 0, 0);
+  cairo_paint (overlay_cr);
 
   /* Now draw the 3 circles in a subgroup again
    * at half intensity, and use OperatorAdd to join up
    * without seams.
    */
-  cairo_save (cr);
-  cairo_set_target_surface (cr, circles);
-
-  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
-  draw_3circles (cr, xc, yc, radius, 0.5);
+  circles_cr = cairo_create (circles);
+  
+  cairo_set_operator (circles_cr, CAIRO_OPERATOR_OVER);
+  draw_3circles (circles_cr, xc, yc, radius, 0.5);
+  cairo_destroy (circles_cr);
 
-  cairo_restore (cr);
+  cairo_set_operator (overlay_cr, CAIRO_OPERATOR_ADD);
+  cairo_set_source_surface (overlay_cr, circles, 0, 0);
+  cairo_paint (overlay_cr);
 
-  cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
-  cairo_set_source_surface (cr, circles, 0, 0);
-  cairo_paint (cr);
-
-  cairo_restore (cr);
+  cairo_destroy (overlay_cr);
 
   cairo_set_source_surface (cr, overlay, 0, 0);
   cairo_paint (cr);
@@ -199,20 +181,6 @@ draw (cairo_t *cr,
   cairo_surface_destroy (overlay);
   cairo_surface_destroy (punch);
   cairo_surface_destroy (circles);
-}
-
-static gboolean
-on_expose_event (GtkWidget      *widget,
-                GdkEventExpose *event,
-                gpointer        data)
-{
-  cairo_t *cr;
-
-  cr = gdk_drawable_create_cairo_context (widget->window);
-
-  draw (cr, widget->allocation.width, widget->allocation.height);
-
-  cairo_destroy (cr);
 
   return FALSE;
 }
@@ -232,8 +200,8 @@ main (int argc, char **argv)
   darea = gtk_drawing_area_new ();
   gtk_container_add (GTK_CONTAINER (window), darea);
 
-  g_signal_connect (darea, "expose-event",
-                   G_CALLBACK (on_expose_event), NULL);
+  g_signal_connect (darea, "draw",
+                   G_CALLBACK (on_draw), NULL);
   g_signal_connect (window, "destroy-event",
                    G_CALLBACK (gtk_main_quit), NULL);