gtkthemingbackground.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 * Copyright (C) 2011 Red Hat, Inc.
 * 
 * Authors: Carlos Garnacho <carlosg@gnome.org>
 *          Cosimo Cecchi <cosimoc@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21
 */

22 23
#include "config.h"

24
#include "gtkthemingbackgroundprivate.h"
25

26
#include "gtkcssarrayvalueprivate.h"
27
#include "gtkcssbgsizevalueprivate.h"
28
#include "gtkcssenumvalueprivate.h"
29
#include "gtkcssimagevalueprivate.h"
30
#include "gtkcssshadowsvalueprivate.h"
31
#include "gtkcsspositionvalueprivate.h"
32
#include "gtkcssrepeatvalueprivate.h"
33
#include "gtkcssrgbavalueprivate.h"
34
#include "gtkcsstypesprivate.h"
35
#include "gtkstylecontextprivate.h"
36

37 38
#include <math.h>

39 40
#include <gdk/gdk.h>

41 42 43 44 45
/* this is in case round() is not provided by the compiler, 
 * such as in the case of C89 compilers, like MSVC
 */
#include "fallback-c89.c"

46 47
typedef struct _GtkThemingBackground GtkThemingBackground;

48 49
#define N_BOXES (3)

50 51 52
struct _GtkThemingBackground {
  GtkStyleContext *context;

53
  GtkRoundedBox boxes[N_BOXES];
54 55
};

56 57 58
static void
_gtk_theming_background_paint_color (GtkThemingBackground *bg,
                                     cairo_t              *cr,
59
                                     const GdkRGBA        *bg_color,
60 61 62 63 64 65 66 67 68
                                     GtkCssValue          *background_image)
{
  gint n_values = _gtk_css_array_value_get_n_values (background_image);
  GtkCssArea clip = _gtk_css_area_value_get 
    (_gtk_css_array_value_get_nth 
     (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP), 
      n_values - 1));

  cairo_save (cr);
69
  _gtk_rounded_box_path (&bg->boxes[clip], cr);
70 71
  cairo_clip (cr);

72
  gdk_cairo_set_source_rgba (cr, bg_color);
73
  cairo_paint (cr);
74

75 76 77 78 79
  cairo_restore (cr);
}

static void
_gtk_theming_background_paint_layer (GtkThemingBackground *bg,
80
                                     guint                 idx,
81 82
                                     cairo_t              *cr)
{
83 84
  GtkCssRepeatStyle hrepeat, vrepeat;
  const GtkCssValue *pos, *repeat;
85
  GtkCssImage *image;
86
  const GtkRoundedBox *origin;
87 88 89
  double image_width, image_height;
  double width, height;

90 91
  pos = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION), idx);
  repeat = _gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT), idx);
92 93
  hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
  vrepeat = _gtk_css_background_repeat_value_get_y (repeat);
94 95 96
  image = _gtk_css_image_value_get_image (
              _gtk_css_array_value_get_nth (
                  _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE),
97
                  idx));
98
  origin = &bg->boxes[
99 100 101
               _gtk_css_area_value_get (
                   _gtk_css_array_value_get_nth (
                       _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_ORIGIN),
102
                       idx))];
103 104
  width = origin->box.width;
  height = origin->box.height;
105

106
  if (image == NULL || width <= 0 || height <= 0)
107 108
    return;

109
  _gtk_css_bg_size_value_compute_size (_gtk_css_array_value_get_nth (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE), idx),
110
                                       image,
111 112 113 114 115
                                       width,
                                       height,
                                       &image_width,
                                       &image_height);

116 117 118
  if (image_width <= 0 || image_height <= 0)
    return;

119 120 121 122 123 124
  /* optimization */
  if (image_width == width)
    hrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;
  if (image_height == height)
    vrepeat = GTK_CSS_REPEAT_STYLE_NO_REPEAT;

125 126 127

  cairo_save (cr);

128
  _gtk_rounded_box_path (
129
      &bg->boxes[
130 131 132
          _gtk_css_area_value_get (
              _gtk_css_array_value_get_nth (
                  _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_CLIP),
133
                  idx))],
134
      cr);
135 136 137
  cairo_clip (cr);


138
  cairo_translate (cr, origin->box.x, origin->box.y);
139 140 141 142 143 144 145

  if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT && vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
    {
      cairo_translate (cr,
                       _gtk_css_position_value_get_x (pos, width - image_width),
                       _gtk_css_position_value_get_y (pos, height - image_height));
      /* shortcut for normal case */
146
      _gtk_css_image_draw (image, cr, image_width, image_height);
147 148
    }
  else
149
    {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      int surface_width, surface_height;
      cairo_rectangle_t fill_rect;
      cairo_surface_t *surface;
      cairo_t *cr2;

      /* If ‘background-repeat’ is ‘round’ for one (or both) dimensions,
       * there is a second step. The UA must scale the image in that
       * dimension (or both dimensions) so that it fits a whole number of
       * times in the background positioning area. In the case of the width
       * (height is analogous):
       *
       * If X ≠ 0 is the width of the image after step one and W is the width
       * of the background positioning area, then the rounded width
       * X' = W / round(W / X) where round() is a function that returns the
       * nearest natural number (integer greater than zero). 
       *
       * If ‘background-repeat’ is ‘round’ for one dimension only and if
       * ‘background-size’ is ‘auto’ for the other dimension, then there is
       * a third step: that other dimension is scaled so that the original
       * aspect ratio is restored. 
       */
      if (hrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
        {
          double n = round (width / image_width);

          n = MAX (1, n);

          if (vrepeat != GTK_CSS_REPEAT_STYLE_ROUND
              /* && vsize == auto (it is by default) */)
            image_height *= width / (image_width * n);
          image_width = width / n;
        }
      if (vrepeat == GTK_CSS_REPEAT_STYLE_ROUND)
183
        {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
          double n = round (height / image_height);

          n = MAX (1, n);

          if (hrepeat != GTK_CSS_REPEAT_STYLE_ROUND
              /* && hsize == auto (it is by default) */)
            image_width *= height / (image_height * n);
          image_height = height / n;
        }

      /* if hrepeat or vrepeat is 'space', we create a somewhat larger surface
       * to store the extra space. */
      if (hrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
        {
          double n = floor (width / image_width);
          surface_width = n ? round (width / n) : 0;
200 201
        }
      else
202 203 204
        surface_width = round (image_width);

      if (vrepeat == GTK_CSS_REPEAT_STYLE_SPACE)
205
        {
206 207
          double n = floor (height / image_height);
          surface_height = n ? round (height / n) : 0;
208
        }
209 210 211 212 213 214 215 216 217 218
      else
        surface_height = round (image_height);

      surface = cairo_surface_create_similar (cairo_get_target (cr),
                                              CAIRO_CONTENT_COLOR_ALPHA,
                                              surface_width, surface_height);
      cr2 = cairo_create (surface);
      cairo_translate (cr2,
                       0.5 * (surface_width - image_width),
                       0.5 * (surface_height - image_height));
219
      _gtk_css_image_draw (image, cr2, image_width, image_height);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      cairo_destroy (cr2);

      cairo_set_source_surface (cr, surface,
                                _gtk_css_position_value_get_x (pos, width - image_width),
                                _gtk_css_position_value_get_y (pos, height - image_height));
      cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
      cairo_surface_destroy (surface);

      if (hrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
        {
          fill_rect.x = _gtk_css_position_value_get_x (pos, width - image_width);
          fill_rect.width = image_width;
        }
      else
        {
          fill_rect.x = 0;
          fill_rect.width = width;
        }

      if (vrepeat == GTK_CSS_REPEAT_STYLE_NO_REPEAT)
        {
          fill_rect.y = _gtk_css_position_value_get_y (pos, height - image_height);
          fill_rect.height = image_height;
        }
      else
        {
          fill_rect.y = 0;
          fill_rect.height = height;
        }

      cairo_rectangle (cr, fill_rect.x, fill_rect.y,
                       fill_rect.width, fill_rect.height);
      cairo_fill (cr);
253 254
    }

255

256
  cairo_restore (cr);
257 258 259
}

static void
260 261 262 263
_gtk_theming_background_init_context (GtkThemingBackground *bg,
                                      double                width,
                                      double                height,
                                      GtkJunctionSides      junction)
264
{
265
  GtkStateFlags flags = gtk_style_context_get_state (bg->context);
266
  GtkBorder border, padding;
267

268 269
  gtk_style_context_get_border (bg->context, flags, &border);
  gtk_style_context_get_padding (bg->context, flags, &padding);
270 271 272 273 274 275 276 277 278

  /* In the CSS box model, by default the background positioning area is
   * the padding-box, i.e. all the border-box minus the borders themselves,
   * which determines also its default size, see
   * http://dev.w3.org/csswg/css3-background/#background-origin
   *
   * In the future we might want to support different origins or clips, but
   * right now we just shrink to the default.
   */
279 280
  _gtk_rounded_box_init_rect (&bg->boxes[GTK_CSS_AREA_BORDER_BOX], 0, 0, width, height);
  _gtk_rounded_box_apply_border_radius_for_context (&bg->boxes[GTK_CSS_AREA_BORDER_BOX], bg->context, junction);
281

282 283
  bg->boxes[GTK_CSS_AREA_PADDING_BOX] = bg->boxes[GTK_CSS_AREA_BORDER_BOX];
  _gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_PADDING_BOX],
284 285
			   border.top, border.right,
			   border.bottom, border.left);
286

287 288
  bg->boxes[GTK_CSS_AREA_CONTENT_BOX] = bg->boxes[GTK_CSS_AREA_PADDING_BOX];
  _gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_CONTENT_BOX],
289 290
			   padding.top, padding.right,
			   padding.bottom, padding.left);
291 292 293
}

void
294 295 296 297 298 299 300
gtk_theming_background_render (GtkStyleContext      *context,
                               cairo_t              *cr,
                               gdouble               x,
                               gdouble               y,
                               gdouble               width,
                               gdouble               height,
                               GtkJunctionSides      junction)
301
{
302 303 304
  GtkThemingBackground bg;
  gint idx;
  GtkCssValue *background_image;
305
  GtkCssValue *box_shadow;
306
  const GdkRGBA *bg_color;
307

308 309 310 311 312 313 314 315 316 317
  background_image = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
  bg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
  box_shadow = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BOX_SHADOW);

  /* This is the common default case of no background */
  if (gtk_rgba_is_clear (bg_color) &&
      _gtk_css_array_value_get_n_values (background_image) == 1 &&
      _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (background_image, 0)) == NULL &&
      _gtk_css_shadows_value_is_none (box_shadow))
    return;
318

319
  bg.context = context;
320
  _gtk_theming_background_init_context (&bg, width, height, junction);
321

322
  cairo_save (cr);
323
  cairo_translate (cr, x, y);
324

325 326 327 328 329
  /* Outset shadows */
  _gtk_css_shadows_value_paint_box (box_shadow,
                                    cr,
                                    &bg.boxes[GTK_CSS_AREA_BORDER_BOX],
                                    FALSE);
330

331
  _gtk_theming_background_paint_color (&bg, cr, bg_color, background_image);
332 333 334

  for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
    {
335
      _gtk_theming_background_paint_layer (&bg, idx, cr);
336 337
    }

338 339 340 341 342
  /* Inset shadows */
  _gtk_css_shadows_value_paint_box (box_shadow,
                                    cr,
                                    &bg.boxes[GTK_CSS_AREA_PADDING_BOX],
                                    TRUE);
343 344 345

  cairo_restore (cr);
}