]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkthemingengine.c
stylecontext: add a sidebar style class
[~andy/gtk] / gtk / gtkthemingengine.c
index da95aeb9ee223db8548ab2539f012c102698525c..df09426abeb52eb8ddb28ba7991b5821c540fada 100644 (file)
@@ -1472,6 +1472,32 @@ _cairo_round_rectangle_sides (cairo_t          *cr,
     }
 }
 
+/* Set the appropriate matrix for
+ * patterns coming from the style context
+ */
+static void
+style_pattern_set_matrix (cairo_pattern_t *pattern,
+                          gdouble          width,
+                          gdouble          height)
+{
+  cairo_matrix_t matrix;
+  gint w, h;
+
+  if (cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SURFACE)
+    {
+      cairo_surface_t *surface;
+
+      cairo_pattern_get_surface (pattern, &surface);
+      w = cairo_image_surface_get_width (surface);
+      h = cairo_image_surface_get_height (surface);
+    }
+  else
+    w = h = 1;
+
+  cairo_matrix_init_scale (&matrix, (gdouble) w / width, (gdouble) h / height);
+  cairo_pattern_set_matrix (pattern, &matrix);
+}
+
 static void
 render_background_internal (GtkThemingEngine *engine,
                             cairo_t          *cr,
@@ -1489,12 +1515,16 @@ render_background_internal (GtkThemingEngine *engine,
   GtkBorder *border;
   gint radius, border_width;
   GtkBorderStyle border_style;
+  gdouble mat_w, mat_h;
+  cairo_matrix_t identity;
 
-  if (width <= 0 || height <= 0)
-    return;
+  /* Use unmodified size for pattern scaling */
+  mat_w = width;
+  mat_h = height;
 
   flags = gtk_theming_engine_get_state (engine);
-  cairo_save (cr);
+
+  cairo_matrix_init_identity (&identity);
 
   gtk_theming_engine_get (engine, flags,
                           "background-image", &pattern,
@@ -1507,9 +1537,31 @@ render_background_internal (GtkThemingEngine *engine,
   border_width = MIN (MIN (border->top, border->bottom),
                       MIN (border->left, border->right));
 
-  running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
+  if (border_width > 1 &&
+      border_style == GTK_BORDER_STYLE_NONE)
+    {
+      cairo_set_line_width (cr, border_width);
+
+      x += (gdouble) border_width / 2;
+      y += (gdouble) border_width / 2;
+      width -= border_width;
+      height -= border_width;
+    }
+  else
+    {
+      x += border->left;
+      y += border->top;
+      width -= border->left + border->right;
+      height -= border->top + border->bottom;
+    }
+
+  if (width <= 0 || height <= 0)
+    return;
+
+  cairo_save (cr);
+  cairo_translate (cr, x, y);
 
-  cairo_push_group (cr);
+  running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
 
   if (gtk_theming_engine_has_class (engine, "background"))
     {
@@ -1518,9 +1570,6 @@ render_background_internal (GtkThemingEngine *engine,
       cairo_paint (cr);
     }
 
-  cairo_translate (cr, x, y);
-  cairo_scale (cr, width, height);
-
   if (running)
     {
       cairo_pattern_t *other_pattern;
@@ -1611,10 +1660,16 @@ render_background_internal (GtkThemingEngine *engine,
               /* Different pattern types, or different color
                * stop counts, alpha blend both patterns.
                */
-              cairo_rectangle (cr, 0, 0, 1, 1);
+              _cairo_round_rectangle_sides (cr, (gdouble) radius,
+                                            0, 0, width, height,
+                                            SIDE_ALL, junction);
+
+              style_pattern_set_matrix (other_pattern, mat_w, mat_h);
               cairo_set_source (cr, other_pattern);
               cairo_fill_preserve (cr);
 
+              cairo_pattern_set_matrix (other_pattern, &identity);
+
               /* Set alpha for posterior drawing
                * of the target pattern
                */
@@ -1698,66 +1753,52 @@ render_background_internal (GtkThemingEngine *engine,
         gdk_rgba_free (other_bg);
     }
 
-  cairo_rectangle (cr, 0, 0, 1, 1);
-
+  _cairo_round_rectangle_sides (cr, (gdouble) radius,
+                                0, 0, width, height,
+                                SIDE_ALL, junction);
   if (pattern)
     {
+      style_pattern_set_matrix (pattern, mat_w, mat_h);
       cairo_set_source (cr, pattern);
-      cairo_pattern_destroy (pattern);
     }
   else
     gdk_cairo_set_source_rgba (cr, bg_color);
 
   if (alpha == 1)
-    cairo_fill (cr);
-  else
     {
-      cairo_pattern_t *mask;
-
-      mask = cairo_pattern_create_rgba (1, 1, 1, alpha);
-      cairo_mask (cr, mask);
-      cairo_pattern_destroy (mask);
-    }
-
-  cairo_pop_group_to_source (cr);
-
-  if (border_width > 1 &&
-      border_style == GTK_BORDER_STYLE_NONE)
-    {
-      cairo_set_line_width (cr, border_width);
-
-      x += (gdouble) border_width / 2;
-      y += (gdouble) border_width / 2;
-      width -= border_width;
-      height -= border_width;
+      if (border_width > 1 &&
+          border_style != GTK_BORDER_STYLE_NONE)
+        {
+          /* stroke with the same source, so the background
+           * has exactly the shape than the frame, this
+           * is important so gtk_render_background() and
+           * gtk_render_frame() fit perfectly with round
+           * borders.
+           */
+          cairo_fill_preserve (cr);
+          cairo_stroke (cr);
+        }
+      else
+        cairo_fill (cr);
     }
   else
     {
-      x += border->left;
-      y += border->top;
-      width -= border->left + border->right;
-      height -= border->top + border->bottom;
-    }
+      cairo_save (cr);
+      _cairo_round_rectangle_sides (cr, (gdouble) radius,
+                                    0, 0, width, height,
+                                    SIDE_ALL, junction);
+      cairo_clip (cr);
 
-  _cairo_round_rectangle_sides (cr, (gdouble) radius,
-                                x, y, width, height,
-                                SIDE_ALL, junction);
-  cairo_close_path (cr);
+      cairo_paint_with_alpha (cr, alpha);
 
-  if (border_width > 1 &&
-      border_style != GTK_BORDER_STYLE_NONE)
+      cairo_restore (cr);
+    }
+
+  if (pattern)
     {
-      /* stroke with the same source, so the background
-       * has exactly the shape than the frame, this
-       * is important so gtk_render_background() and
-       * gtk_render_frame() fit perfectly with round
-       * borders.
-       */
-      cairo_fill_preserve (cr);
-      cairo_stroke (cr);
+      cairo_pattern_set_matrix (pattern, &identity);
+      cairo_pattern_destroy (pattern);
     }
-  else
-    cairo_fill (cr);
 
   cairo_restore (cr);
 
@@ -2621,12 +2662,14 @@ gtk_theming_engine_render_handle (GtkThemingEngine *engine,
   GtkStateFlags flags;
   GdkRGBA *bg_color;
   GdkRGBA lighter, darker;
+  GtkJunctionSides sides;
   gint xx, yy;
 
   cairo_save (cr);
   flags = gtk_theming_engine_get_state (engine);
 
-  cairo_set_line_width (cr, 1);
+  cairo_set_line_width (cr, 1.0);
+  sides = gtk_theming_engine_get_junction_sides (engine);
 
   gtk_theming_engine_get (engine, flags,
                           "background-color", &bg_color,
@@ -2634,19 +2677,10 @@ gtk_theming_engine_render_handle (GtkThemingEngine *engine,
   color_shade (bg_color, 0.7, &darker);
   color_shade (bg_color, 1.3, &lighter);
 
-  gdk_cairo_set_source_rgba (cr, bg_color);
-  cairo_rectangle (cr, x, y, width, height);
-  cairo_fill (cr);
+  render_background_internal (engine, cr, x, y, width, height, sides);
 
   if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
     {
-      GtkJunctionSides sides;
-
-      cairo_save (cr);
-
-      cairo_set_line_width (cr, 1.0);
-      sides = gtk_theming_engine_get_junction_sides (engine);
-
       /* reduce confusing values to a meaningful state */
       if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
         sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
@@ -2891,8 +2925,6 @@ gtk_theming_engine_render_handle (GtkThemingEngine *engine,
               yi += 3;
             }
         }
-
-      cairo_restore (cr);
     }
   else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
     {
@@ -2931,7 +2963,6 @@ gtk_theming_engine_render_activity (GtkThemingEngine *engine,
       GtkStateFlags state;
       guint num_steps, step;
       GdkRGBA *color;
-      gdouble dx, dy;
       gdouble progress;
       gdouble radius;
       gdouble half;
@@ -2951,13 +2982,10 @@ gtk_theming_engine_render_activity (GtkThemingEngine *engine,
 
       cairo_save (cr);
 
-      cairo_translate (cr, x, y);
-
-      /* draw clip region */
       cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+      cairo_set_line_width (cr, 2.0);
+      cairo_translate (cr, x + width / 2, y + height / 2);
 
-      dx = width / 2;
-      dy = height / 2;
       radius = MIN (width / 2, height / 2);
       half = num_steps / 2;
 
@@ -2966,10 +2994,9 @@ gtk_theming_engine_render_activity (GtkThemingEngine *engine,
           gint inset = 0.7 * radius;
 
           /* transparency is a function of time and intial value */
-          gdouble t = (gdouble) ((i + num_steps - step)
-                                 % num_steps) / num_steps;
-
-          cairo_save (cr);
+          gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
+          gdouble xscale = - sin (i * G_PI / half);
+          gdouble yscale = - cos (i * G_PI / half);
 
           cairo_set_source_rgba (cr,
                                  color->red,
@@ -2977,16 +3004,13 @@ gtk_theming_engine_render_activity (GtkThemingEngine *engine,
                                  color->blue,
                                  color->alpha * t);
 
-          cairo_set_line_width (cr, 2.0);
           cairo_move_to (cr,
-                         dx + (radius - inset) * cos (i * G_PI / half),
-                         dy + (radius - inset) * sin (i * G_PI / half));
+                         (radius - inset) * xscale,
+                         (radius - inset) * yscale);
           cairo_line_to (cr,
-                         dx + radius * cos (i * G_PI / half),
-                         dy + radius * sin (i * G_PI / half));
+                         radius * xscale,
+                         radius * yscale);
           cairo_stroke (cr);
-
-          cairo_restore (cr);
         }
 
       cairo_restore (cr);