gtkcssshadowsvalue.c 9.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Author: 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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkcssshadowsvalueprivate.h"

24 25
#include <math.h>

26
#include "gtkcairoblurprivate.h"
27 28 29 30 31 32 33 34 35 36
#include "gtkcssshadowvalueprivate.h"

#include <string.h>

struct _GtkCssValue {
  GTK_CSS_VALUE_BASE
  guint         len;
  GtkCssValue  *values[1];
};

37 38 39
static GtkCssValue *    gtk_css_shadows_value_new       (GtkCssValue **values,
                                                         guint         len);

40 41 42 43 44 45 46 47 48 49 50 51 52
static void
gtk_css_value_shadows_free (GtkCssValue *value)
{
  guint i;

  for (i = 0; i < value->len; i++)
    {
      _gtk_css_value_unref (value->values[i]);
    }

  g_slice_free1 (sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (value->len - 1), value);
}

53
static GtkCssValue *
54 55 56
gtk_css_value_shadows_compute (GtkCssValue             *value,
                               guint                    property_id,
                               GtkStyleProviderPrivate *provider,
57
                               GtkCssStyle             *style,
58
                               GtkCssStyle             *parent_style)
59
{
60 61
  GtkCssValue *result, *tmp;
  guint i, j;
62 63 64 65

  if (value->len == 0)
    return _gtk_css_value_ref (value);

66
  result = NULL;
67 68
  for (i = 0; i < value->len; i++)
    {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      tmp = _gtk_css_value_compute (value->values[i], property_id, provider, style, parent_style);

      if (result)
        {
          result->values[i] = tmp;
        }
      else if (tmp != value->values[i])
        {
          result = gtk_css_shadows_value_new (value->values, value->len);
          for (j = 0; j < i; j++)
            {
              _gtk_css_value_ref (result->values[j]);
            }
          result->values[i] = tmp;
        }
      else
        {
          _gtk_css_value_unref (tmp);
        }
88 89
    }

90 91 92 93
  if (result != NULL)
    return result;
  else
    return _gtk_css_value_ref (value);
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static gboolean
gtk_css_value_shadows_equal (const GtkCssValue *value1,
                             const GtkCssValue *value2)
{
  guint i;

  /* XXX: Should we fill up here? */
  if (value1->len != value2->len)
    return FALSE;

  for (i = 0; i < value1->len; i++)
    {
      if (!_gtk_css_value_equal (value1->values[i],
                                 value2->values[i]))
        return FALSE;
    }

  return TRUE;
}

static GtkCssValue *
gtk_css_value_shadows_transition (GtkCssValue *start,
                                  GtkCssValue *end,
119
                                  guint        property_id,
120 121
                                  double       progress)
{
122 123
  guint i, len;
  GtkCssValue **values;
124 125 126 127 128 129

  /* catches the important case of 2 none values */
  if (start == end)
    return _gtk_css_value_ref (start);

  if (start->len > end->len)
130
    len = start->len;
131
  else
132 133 134
    len = end->len;

  values = g_newa (GtkCssValue *, len);
135 136 137

  for (i = 0; i < MIN (start->len, end->len); i++)
    {
138 139 140 141 142 143 144
      values[i] = _gtk_css_value_transition (start->values[i], end->values[i], property_id, progress);
      if (values[i] == NULL)
        {
          while (i--)
            _gtk_css_value_unref (values[i]);
          return NULL;
        }
145 146 147
    }
  if (start->len > end->len)
    {
148
      for (; i < len; i++)
149 150
        {
          GtkCssValue *fill = _gtk_css_shadow_value_new_for_transition (start->values[i]);
151
          values[i] = _gtk_css_value_transition (start->values[i], fill, property_id, progress);
152
          _gtk_css_value_unref (fill);
153 154 155 156 157 158 159

          if (values[i] == NULL)
            {
              while (i--)
                _gtk_css_value_unref (values[i]);
              return NULL;
            }
160 161 162 163
        }
    }
  else
    {
164
      for (; i < len; i++)
165 166
        {
          GtkCssValue *fill = _gtk_css_shadow_value_new_for_transition (end->values[i]);
167
          values[i] = _gtk_css_value_transition (fill, end->values[i], property_id, progress);
168
          _gtk_css_value_unref (fill);
169 170 171 172 173 174 175

          if (values[i] == NULL)
            {
              while (i--)
                _gtk_css_value_unref (values[i]);
              return NULL;
            }
176 177 178
        }
    }

179
  return gtk_css_shadows_value_new (values, len);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}

static void
gtk_css_value_shadows_print (const GtkCssValue *value,
                             GString           *string)
{
  guint i;

  if (value->len == 0)
    {
      g_string_append (string, "none");
      return;
    }

  for (i = 0; i < value->len; i++)
    {
      if (i > 0)
        g_string_append (string, ", ");
      _gtk_css_value_print (value->values[i], string);
    }
}

static const GtkCssValueClass GTK_CSS_VALUE_SHADOWS = {
  gtk_css_value_shadows_free,
204
  gtk_css_value_shadows_compute,
205 206 207 208 209 210 211 212 213 214 215 216 217
  gtk_css_value_shadows_equal,
  gtk_css_value_shadows_transition,
  gtk_css_value_shadows_print
};

static GtkCssValue none_singleton = { &GTK_CSS_VALUE_SHADOWS, 1, 0, { NULL } };

GtkCssValue *
_gtk_css_shadows_value_new_none (void)
{
  return _gtk_css_value_ref (&none_singleton);
}

218
static GtkCssValue *
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
gtk_css_shadows_value_new (GtkCssValue **values,
                           guint         len)
{
  GtkCssValue *result;
           
  g_return_val_if_fail (values != NULL, NULL);
  g_return_val_if_fail (len > 0, NULL);
         
  result = _gtk_css_value_alloc (&GTK_CSS_VALUE_SHADOWS, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (len - 1));
  result->len = len;
  memcpy (&result->values[0], values, sizeof (GtkCssValue *) * len);
            
  return result;
}

GtkCssValue *
235 236
_gtk_css_shadows_value_parse (GtkCssParser *parser,
                              gboolean      box_shadow_mode)
237 238 239 240 241 242 243 244 245 246
{
  GtkCssValue *value, *result;
  GPtrArray *values;

  if (_gtk_css_parser_try (parser, "none", TRUE))
    return _gtk_css_shadows_value_new_none ();

  values = g_ptr_array_new ();

  do {
247
    value = _gtk_css_shadow_value_parse (parser, box_shadow_mode);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

    if (value == NULL)
      {
        g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref);
        g_ptr_array_free (values, TRUE);
        return NULL;
      }

    g_ptr_array_add (values, value);
  } while (_gtk_css_parser_try (parser, ",", TRUE));

  result = gtk_css_shadows_value_new ((GtkCssValue **) values->pdata, values->len);
  g_ptr_array_free (values, TRUE);
  return result;
}

264 265 266 267 268 269 270 271
gboolean
_gtk_css_shadows_value_is_none (const GtkCssValue *shadows)
{
  g_return_val_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS, TRUE);

  return shadows->len == 0;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
void
_gtk_css_shadows_value_paint_layout (const GtkCssValue *shadows,
                                     cairo_t           *cr,
                                     PangoLayout       *layout)
{
  guint i;

  g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);

  for (i = 0; i < shadows->len; i++)
    {
      _gtk_css_shadow_value_paint_layout (shadows->values[i], cr, layout);
    }
}

void
_gtk_css_shadows_value_paint_icon (const GtkCssValue *shadows,
                                   cairo_t           *cr)
{
  guint i;

  g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);

  for (i = 0; i < shadows->len; i++)
    {
      _gtk_css_shadow_value_paint_icon (shadows->values[i], cr);
    }
}

void
_gtk_css_shadows_value_paint_box (const GtkCssValue   *shadows,
                                  cairo_t             *cr,
304 305
                                  const GtkRoundedBox *padding_box,
                                  gboolean             inset)
306 307 308 309 310 311 312
{
  guint i;

  g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);

  for (i = 0; i < shadows->len; i++)
    {
313 314
      if (inset == _gtk_css_shadow_value_get_inset (shadows->values[i]))
        _gtk_css_shadow_value_paint_box (shadows->values[i], cr, padding_box);
315 316
    }
}
317 318 319 320 321 322 323 324

void
_gtk_css_shadows_value_get_extents (const GtkCssValue *shadows,
                                    GtkBorder         *border)
{
  guint i;
  GtkBorder b = { 0 };
  const GtkCssValue *shadow;
325
  gdouble hoffset, voffset, spread, radius, clip_radius;
326 327 328

  g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);

329 330
  *border = b;

331 332 333 334 335 336 337 338 339 340
  for (i = 0; i < shadows->len; i++)
    {
      shadow = shadows->values[i];

      if (_gtk_css_shadow_value_get_inset (shadow))
        continue;

      _gtk_css_shadow_value_get_geometry (shadow,
                                          &hoffset, &voffset,
                                          &radius, &spread);
341
      clip_radius = _gtk_cairo_blur_compute_pixels (radius);
342

343 344 345 346
      b.top = MAX (0, ceil (clip_radius + spread - voffset));
      b.right = MAX (0, ceil (clip_radius + spread + hoffset));
      b.bottom = MAX (0, ceil (clip_radius + spread + voffset));
      b.left = MAX (0, ceil (clip_radius + spread - hoffset));
347 348 349 350 351 352 353

      border->top = MAX (border->top, b.top);
      border->right = MAX (border->right, b.right);
      border->bottom = MAX (border->bottom, b.bottom);
      border->left = MAX (border->left, b.left);
    }
}