2016-01-30 01:23:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2016 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* 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.1 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/>.
|
|
|
|
*
|
|
|
|
* Authors: Matthias Clasen <mclasen@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcssimagefallbackprivate.h"
|
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2020-01-10 12:02:33 +00:00
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2016-01-30 01:23:27 +00:00
|
|
|
|
|
|
|
#include "gtkstyleproviderprivate.h"
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkCssImageFallback, _gtk_css_image_fallback, GTK_TYPE_CSS_IMAGE)
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_css_image_fallback_get_width (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _gtk_css_image_get_width (fallback->images[fallback->used]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_css_image_fallback_get_height (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _gtk_css_image_get_height (fallback->images[fallback->used]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
gtk_css_image_fallback_get_aspect_ratio (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
2017-01-13 00:43:07 +00:00
|
|
|
return 0;
|
2016-01-30 01:23:27 +00:00
|
|
|
|
|
|
|
return _gtk_css_image_get_aspect_ratio (fallback->images[fallback->used]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-12-13 04:12:41 +00:00
|
|
|
gtk_css_image_fallback_snapshot (GtkCssImage *image,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
|
|
|
{
|
|
|
|
if (fallback->color)
|
2020-01-16 12:41:55 +00:00
|
|
|
{
|
|
|
|
const GdkRGBA *color = gtk_css_color_value_get_rgba (fallback->color);
|
|
|
|
if (!gdk_rgba_is_clear (color))
|
|
|
|
gtk_snapshot_append_color (snapshot, color,
|
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height));
|
|
|
|
}
|
2016-01-30 01:23:27 +00:00
|
|
|
else
|
2020-01-16 12:41:55 +00:00
|
|
|
{
|
|
|
|
gtk_snapshot_append_color (snapshot, &(GdkRGBA) {1, 0, 0, 1},
|
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height));
|
|
|
|
}
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
|
|
|
else
|
2016-12-13 04:12:41 +00:00
|
|
|
gtk_css_image_snapshot (fallback->images[fallback->used], snapshot, width, height);
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_fallback_print (GtkCssImage *image,
|
2016-09-06 10:21:06 +00:00
|
|
|
GString *string)
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_string_append (string, "image(");
|
|
|
|
for (i = 0; i < fallback->n_images; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
g_string_append (string, ",");
|
|
|
|
_gtk_css_image_print (fallback->images[i], string);
|
|
|
|
}
|
|
|
|
if (fallback->color)
|
|
|
|
{
|
2016-04-26 04:01:59 +00:00
|
|
|
if (fallback->n_images > 0)
|
|
|
|
g_string_append (string, ",");
|
2016-01-30 01:23:27 +00:00
|
|
|
_gtk_css_value_print (fallback->color, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_fallback_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (object);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < fallback->n_images; i++)
|
|
|
|
g_object_unref (fallback->images[i]);
|
|
|
|
g_free (fallback->images);
|
|
|
|
fallback->images = NULL;
|
|
|
|
|
|
|
|
if (fallback->color)
|
|
|
|
{
|
|
|
|
_gtk_css_value_unref (fallback->color);
|
|
|
|
fallback->color = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (_gtk_css_image_fallback_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GtkCssImage *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_image_fallback_compute (GtkCssImage *image,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
GtkCssImageFallback *copy;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
|
|
|
{
|
2020-01-11 07:25:52 +00:00
|
|
|
GtkCssValue *computed_color = NULL;
|
|
|
|
|
|
|
|
if (fallback->color)
|
2021-01-27 02:10:11 +00:00
|
|
|
computed_color = _gtk_css_value_compute (fallback->color,
|
|
|
|
property_id,
|
|
|
|
provider,
|
|
|
|
style,
|
|
|
|
parent_style);
|
2020-01-11 07:25:52 +00:00
|
|
|
|
|
|
|
/* image($color) that didn't change */
|
|
|
|
if (computed_color && !fallback->images &&
|
|
|
|
computed_color == fallback->color)
|
|
|
|
return g_object_ref (image);
|
|
|
|
|
2016-01-30 01:23:27 +00:00
|
|
|
copy = g_object_new (_gtk_css_image_fallback_get_type (), NULL);
|
|
|
|
copy->n_images = fallback->n_images;
|
|
|
|
copy->images = g_new (GtkCssImage *, fallback->n_images);
|
|
|
|
for (i = 0; i < fallback->n_images; i++)
|
|
|
|
{
|
|
|
|
copy->images[i] = _gtk_css_image_compute (fallback->images[i],
|
|
|
|
property_id,
|
|
|
|
provider,
|
|
|
|
style,
|
|
|
|
parent_style);
|
|
|
|
|
2018-02-17 19:31:14 +00:00
|
|
|
if (gtk_css_image_is_invalid (copy->images[i]))
|
2016-01-30 01:23:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (copy->used < 0)
|
|
|
|
copy->used = i;
|
|
|
|
}
|
|
|
|
|
2020-01-11 07:25:52 +00:00
|
|
|
copy->color = computed_color;
|
2016-01-30 01:23:27 +00:00
|
|
|
|
|
|
|
return GTK_CSS_IMAGE (copy);
|
|
|
|
}
|
|
|
|
else
|
2017-12-08 22:48:47 +00:00
|
|
|
return GTK_CSS_IMAGE (g_object_ref (fallback));
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
typedef struct
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
2019-03-30 23:52:22 +00:00
|
|
|
GtkCssValue *color;
|
2016-01-30 01:23:27 +00:00
|
|
|
GPtrArray *images;
|
2019-03-30 23:52:22 +00:00
|
|
|
} ParseData;
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_css_image_fallback_parse_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer _data)
|
|
|
|
{
|
|
|
|
ParseData *data = _data;
|
2016-01-30 01:23:27 +00:00
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
if (data->color != NULL)
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "The color must be the last parameter");
|
2019-03-30 23:52:22 +00:00
|
|
|
return 0;
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
2019-03-30 23:52:22 +00:00
|
|
|
else if (_gtk_css_image_can_parse (parser))
|
|
|
|
{
|
|
|
|
GtkCssImage *image = _gtk_css_image_new_parse (parser);
|
|
|
|
if (image == NULL)
|
|
|
|
return 0;
|
2016-01-30 01:23:27 +00:00
|
|
|
|
2020-09-23 04:06:42 +00:00
|
|
|
if (!data->images)
|
|
|
|
data->images = g_ptr_array_new_with_free_func (g_object_unref);
|
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
g_ptr_array_add (data->images, image);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
2019-03-30 23:52:22 +00:00
|
|
|
data->color = _gtk_css_color_value_parse (parser);
|
|
|
|
if (data->color == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
2019-03-30 23:52:22 +00:00
|
|
|
}
|
2016-01-30 01:23:27 +00:00
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_fallback_parse (GtkCssImage *image,
|
|
|
|
GtkCssParser *parser)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *self = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
ParseData data = { NULL, NULL };
|
2016-01-30 01:23:27 +00:00
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
if (!gtk_css_parser_has_function (parser, "image"))
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'image('");
|
2019-03-30 23:52:22 +00:00
|
|
|
return FALSE;
|
2016-01-30 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, G_MAXUINT, gtk_css_image_fallback_parse_arg, &data))
|
2016-01-30 01:23:27 +00:00
|
|
|
{
|
2019-03-30 23:52:22 +00:00
|
|
|
g_clear_pointer (&data.color, _gtk_css_value_unref);
|
2020-09-23 04:06:42 +00:00
|
|
|
if (data.images)
|
|
|
|
g_ptr_array_free (data.images, TRUE);
|
2016-01-30 01:23:27 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-03-30 23:52:22 +00:00
|
|
|
self->color = data.color;
|
2020-09-23 04:06:42 +00:00
|
|
|
if (data.images)
|
|
|
|
{
|
|
|
|
self->n_images = data.images->len;
|
|
|
|
self->images = (GtkCssImage **) g_ptr_array_free (data.images, FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->n_images = 0;
|
|
|
|
self->images = NULL;
|
|
|
|
}
|
2016-01-30 01:23:27 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-10-14 10:00:19 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_fallback_equal (GtkCssImage *image1,
|
|
|
|
GtkCssImage *image2)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback1 = GTK_CSS_IMAGE_FALLBACK (image1);
|
|
|
|
GtkCssImageFallback *fallback2 = GTK_CSS_IMAGE_FALLBACK (image2);
|
|
|
|
|
|
|
|
if (fallback1->used < 0)
|
|
|
|
{
|
|
|
|
if (fallback2->used >= 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _gtk_css_value_equal (fallback1->color, fallback2->color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fallback2->used < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _gtk_css_image_equal (fallback1->images[fallback1->used],
|
|
|
|
fallback2->images[fallback2->used]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:38:49 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_css_image_fallback_is_computed (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);
|
|
|
|
|
|
|
|
if (fallback->used < 0)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (fallback->color && !fallback->images)
|
|
|
|
return gtk_css_value_is_computed (fallback->color);
|
|
|
|
|
|
|
|
for (i = 0; i < fallback->n_images; i++)
|
|
|
|
{
|
|
|
|
if (!gtk_css_image_is_computed (fallback->images[i]))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:23:27 +00:00
|
|
|
static void
|
|
|
|
_gtk_css_image_fallback_class_init (GtkCssImageFallbackClass *klass)
|
|
|
|
{
|
|
|
|
GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
image_class->get_width = gtk_css_image_fallback_get_width;
|
|
|
|
image_class->get_height = gtk_css_image_fallback_get_height;
|
|
|
|
image_class->get_aspect_ratio = gtk_css_image_fallback_get_aspect_ratio;
|
2016-12-13 04:12:41 +00:00
|
|
|
image_class->snapshot = gtk_css_image_fallback_snapshot;
|
2016-01-30 01:23:27 +00:00
|
|
|
image_class->parse = gtk_css_image_fallback_parse;
|
|
|
|
image_class->compute = gtk_css_image_fallback_compute;
|
|
|
|
image_class->print = gtk_css_image_fallback_print;
|
2017-10-14 10:00:19 +00:00
|
|
|
image_class->equal = gtk_css_image_fallback_equal;
|
2020-01-11 13:38:49 +00:00
|
|
|
image_class->is_computed = gtk_css_image_fallback_is_computed;
|
2016-01-30 01:23:27 +00:00
|
|
|
|
|
|
|
object_class->dispose = gtk_css_image_fallback_dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_css_image_fallback_init (GtkCssImageFallback *image_fallback)
|
|
|
|
{
|
|
|
|
image_fallback->used = -1;
|
|
|
|
}
|
2021-01-27 02:10:11 +00:00
|
|
|
|
|
|
|
GtkCssImage *
|
|
|
|
_gtk_css_image_fallback_new_for_color (GtkCssValue *color)
|
|
|
|
{
|
|
|
|
GtkCssImageFallback *image;
|
|
|
|
|
|
|
|
image = g_object_new (GTK_TYPE_CSS_IMAGE_FALLBACK, NULL);
|
|
|
|
image->color = gtk_css_value_ref (color);
|
|
|
|
|
|
|
|
return (GtkCssImage *)image;
|
|
|
|
}
|