2012-04-03 13:15:24 +00:00
|
|
|
/* 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"
|
|
|
|
|
2014-08-20 21:19:59 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2012-04-03 13:15:24 +00:00
|
|
|
#include "gtkcssshadowvalueprivate.h"
|
2017-10-28 20:10:46 +00:00
|
|
|
#include "gtksnapshot.h"
|
2012-04-03 13:15:24 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct _GtkCssValue {
|
|
|
|
GTK_CSS_VALUE_BASE
|
|
|
|
guint len;
|
|
|
|
GtkCssValue *values[1];
|
|
|
|
};
|
|
|
|
|
2012-04-03 13:33:00 +00:00
|
|
|
static GtkCssValue * gtk_css_shadows_value_new (GtkCssValue **values,
|
|
|
|
guint len);
|
|
|
|
|
2012-04-03 13:15:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-07-11 04:56:07 +00:00
|
|
|
static GtkCssValue *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_value_shadows_compute (GtkCssValue *value,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2012-07-11 04:56:07 +00:00
|
|
|
{
|
2016-04-21 20:28:58 +00:00
|
|
|
GtkCssValue *result, *tmp;
|
|
|
|
guint i, j;
|
2012-07-11 04:56:07 +00:00
|
|
|
|
|
|
|
if (value->len == 0)
|
|
|
|
return _gtk_css_value_ref (value);
|
|
|
|
|
2016-04-21 20:28:58 +00:00
|
|
|
result = NULL;
|
2012-07-11 04:56:07 +00:00
|
|
|
for (i = 0; i < value->len; i++)
|
|
|
|
{
|
2016-04-21 20:28:58 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-07-11 04:56:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 20:28:58 +00:00
|
|
|
if (result != NULL)
|
|
|
|
return result;
|
|
|
|
else
|
|
|
|
return _gtk_css_value_ref (value);
|
2012-07-11 04:56:07 +00:00
|
|
|
}
|
|
|
|
|
2012-04-03 13:15:24 +00:00
|
|
|
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,
|
2012-08-30 13:51:29 +00:00
|
|
|
guint property_id,
|
2012-04-03 13:15:24 +00:00
|
|
|
double progress)
|
|
|
|
{
|
2012-10-16 18:22:24 +00:00
|
|
|
guint i, len;
|
|
|
|
GtkCssValue **values;
|
2012-04-03 13:33:00 +00:00
|
|
|
|
|
|
|
/* catches the important case of 2 none values */
|
|
|
|
if (start == end)
|
|
|
|
return _gtk_css_value_ref (start);
|
|
|
|
|
|
|
|
if (start->len > end->len)
|
2012-10-16 18:22:24 +00:00
|
|
|
len = start->len;
|
2012-04-03 13:33:00 +00:00
|
|
|
else
|
2012-10-16 18:22:24 +00:00
|
|
|
len = end->len;
|
|
|
|
|
|
|
|
values = g_newa (GtkCssValue *, len);
|
2012-04-03 13:33:00 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MIN (start->len, end->len); i++)
|
|
|
|
{
|
2012-10-16 18:22:24 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-04-03 13:33:00 +00:00
|
|
|
}
|
|
|
|
if (start->len > end->len)
|
|
|
|
{
|
2012-10-16 18:22:24 +00:00
|
|
|
for (; i < len; i++)
|
2012-04-03 13:33:00 +00:00
|
|
|
{
|
|
|
|
GtkCssValue *fill = _gtk_css_shadow_value_new_for_transition (start->values[i]);
|
2012-10-16 18:22:24 +00:00
|
|
|
values[i] = _gtk_css_value_transition (start->values[i], fill, property_id, progress);
|
2012-04-03 13:33:00 +00:00
|
|
|
_gtk_css_value_unref (fill);
|
2012-10-16 18:22:24 +00:00
|
|
|
|
|
|
|
if (values[i] == NULL)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
_gtk_css_value_unref (values[i]);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-03 13:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-16 18:22:24 +00:00
|
|
|
for (; i < len; i++)
|
2012-04-03 13:33:00 +00:00
|
|
|
{
|
|
|
|
GtkCssValue *fill = _gtk_css_shadow_value_new_for_transition (end->values[i]);
|
2012-10-16 18:22:24 +00:00
|
|
|
values[i] = _gtk_css_value_transition (fill, end->values[i], property_id, progress);
|
2012-04-03 13:33:00 +00:00
|
|
|
_gtk_css_value_unref (fill);
|
2012-10-16 18:22:24 +00:00
|
|
|
|
|
|
|
if (values[i] == NULL)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
_gtk_css_value_unref (values[i]);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-03 13:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 18:22:24 +00:00
|
|
|
return gtk_css_shadows_value_new (values, len);
|
2012-04-03 13:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-07-11 04:56:07 +00:00
|
|
|
gtk_css_value_shadows_compute,
|
2012-04-03 13:15:24 +00:00
|
|
|
gtk_css_value_shadows_equal,
|
|
|
|
gtk_css_value_shadows_transition,
|
2018-02-13 04:55:33 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2012-04-03 13:15:24 +00:00
|
|
|
gtk_css_value_shadows_print
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkCssValue none_singleton = { >K_CSS_VALUE_SHADOWS, 1, 0, { NULL } };
|
|
|
|
|
|
|
|
GtkCssValue *
|
|
|
|
_gtk_css_shadows_value_new_none (void)
|
|
|
|
{
|
|
|
|
return _gtk_css_value_ref (&none_singleton);
|
|
|
|
}
|
|
|
|
|
2012-04-03 13:33:00 +00:00
|
|
|
static GtkCssValue *
|
2012-04-03 13:15:24 +00:00
|
|
|
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 (>K_CSS_VALUE_SHADOWS, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (len - 1));
|
|
|
|
result->len = len;
|
|
|
|
memcpy (&result->values[0], values, sizeof (GtkCssValue *) * len);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssValue *
|
2014-06-09 23:04:55 +00:00
|
|
|
_gtk_css_shadows_value_parse (GtkCssParser *parser,
|
|
|
|
gboolean box_shadow_mode)
|
2012-04-03 13:15:24 +00:00
|
|
|
{
|
|
|
|
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 {
|
2014-06-09 23:04:55 +00:00
|
|
|
value = _gtk_css_shadow_value_parse (parser, box_shadow_mode);
|
2012-04-03 13:15:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-12 01:16:07 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_css_shadows_value_is_none (const GtkCssValue *shadows)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (shadows->class == >K_CSS_VALUE_SHADOWS, TRUE);
|
|
|
|
|
|
|
|
return shadows->len == 0;
|
|
|
|
}
|
|
|
|
|
2017-09-30 11:11:51 +00:00
|
|
|
gsize
|
|
|
|
gtk_css_shadows_value_get_n_shadows (const GtkCssValue *shadows)
|
2016-12-18 23:45:35 +00:00
|
|
|
{
|
2017-09-30 11:11:51 +00:00
|
|
|
return shadows->len;
|
|
|
|
}
|
2016-12-18 23:45:35 +00:00
|
|
|
|
2017-09-30 11:11:51 +00:00
|
|
|
void
|
|
|
|
gtk_css_shadows_value_get_shadows (const GtkCssValue *shadows,
|
|
|
|
GskShadow *out_shadows)
|
|
|
|
{
|
|
|
|
guint i;
|
2016-12-18 23:45:35 +00:00
|
|
|
|
|
|
|
for (i = 0; i < shadows->len; i++)
|
2017-09-30 11:11:51 +00:00
|
|
|
gtk_css_shadow_value_get_shadow (shadows->values[i], &out_shadows[i]);
|
2016-12-18 23:45:35 +00:00
|
|
|
}
|
|
|
|
|
2012-04-03 13:15:24 +00:00
|
|
|
void
|
|
|
|
_gtk_css_shadows_value_paint_icon (const GtkCssValue *shadows,
|
|
|
|
cairo_t *cr)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_return_if_fail (shadows->class == >K_CSS_VALUE_SHADOWS);
|
|
|
|
|
|
|
|
for (i = 0; i < shadows->len; i++)
|
|
|
|
{
|
|
|
|
_gtk_css_shadow_value_paint_icon (shadows->values[i], cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:38:22 +00:00
|
|
|
void
|
|
|
|
gtk_css_shadows_value_snapshot_outset (const GtkCssValue *shadows,
|
|
|
|
GtkSnapshot *snapshot,
|
2016-12-13 19:48:58 +00:00
|
|
|
const GskRoundedRect*border_box)
|
2016-11-19 19:38:22 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_return_if_fail (shadows->class == >K_CSS_VALUE_SHADOWS);
|
|
|
|
|
|
|
|
for (i = 0; i < shadows->len; i++)
|
|
|
|
{
|
|
|
|
if (_gtk_css_shadow_value_get_inset (shadows->values[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gtk_css_shadow_value_snapshot_outset (shadows->values[i], snapshot, border_box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_css_shadows_value_snapshot_inset (const GtkCssValue *shadows,
|
|
|
|
GtkSnapshot *snapshot,
|
2016-12-13 19:48:58 +00:00
|
|
|
const GskRoundedRect*padding_box)
|
2016-11-19 19:38:22 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_return_if_fail (shadows->class == >K_CSS_VALUE_SHADOWS);
|
|
|
|
|
|
|
|
for (i = 0; i < shadows->len; i++)
|
|
|
|
{
|
|
|
|
if (!_gtk_css_shadow_value_get_inset (shadows->values[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gtk_css_shadow_value_snapshot_inset (shadows->values[i], snapshot, padding_box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 20:50:39 +00:00
|
|
|
void
|
|
|
|
_gtk_css_shadows_value_get_extents (const GtkCssValue *shadows,
|
|
|
|
GtkBorder *border)
|
|
|
|
{
|
|
|
|
guint i;
|
2016-11-19 19:13:54 +00:00
|
|
|
GtkBorder b = { 0 }, sb;
|
2013-04-22 20:50:39 +00:00
|
|
|
const GtkCssValue *shadow;
|
|
|
|
|
|
|
|
g_return_if_fail (shadows->class == >K_CSS_VALUE_SHADOWS);
|
|
|
|
|
|
|
|
for (i = 0; i < shadows->len; i++)
|
|
|
|
{
|
|
|
|
shadow = shadows->values[i];
|
|
|
|
|
|
|
|
if (_gtk_css_shadow_value_get_inset (shadow))
|
|
|
|
continue;
|
|
|
|
|
2016-11-19 19:13:54 +00:00
|
|
|
gtk_css_shadow_value_get_extents (shadow, &sb);
|
2013-04-22 20:50:39 +00:00
|
|
|
|
2016-11-19 19:13:54 +00:00
|
|
|
b.top = MAX (b.top, sb.top);
|
2016-11-30 22:52:54 +00:00
|
|
|
b.right = MAX (b.right, sb.right);
|
|
|
|
b.bottom = MAX (b.bottom, sb.bottom);
|
|
|
|
b.left = MAX (b.left, sb.left);
|
2013-04-22 20:50:39 +00:00
|
|
|
}
|
2016-11-30 22:52:54 +00:00
|
|
|
|
|
|
|
*border = b;
|
2013-04-22 20:50:39 +00:00
|
|
|
}
|
2017-10-28 20:10:46 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_css_shadows_value_push_snapshot (const GtkCssValue *value,
|
|
|
|
GtkSnapshot *snapshot)
|
|
|
|
{
|
|
|
|
gboolean need_shadow = FALSE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < value->len; i++)
|
|
|
|
{
|
|
|
|
if (!gtk_css_shadow_value_is_clear (value->values[i]))
|
|
|
|
{
|
|
|
|
need_shadow = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_shadow)
|
|
|
|
{
|
|
|
|
GskShadow *shadows = g_newa (GskShadow, value->len);
|
|
|
|
gtk_css_shadows_value_get_shadows (value, shadows);
|
2018-04-24 01:17:23 +00:00
|
|
|
gtk_snapshot_push_shadow (snapshot, shadows, value->len);
|
2017-10-28 20:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return need_shadow;
|
|
|
|
}
|