2016-01-14 18:48:43 +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 "gtkcssimagerecolorprivate.h"
|
2017-11-08 15:04:38 +00:00
|
|
|
#include "gtkcssimageprivate.h"
|
2016-01-14 18:48:43 +00:00
|
|
|
#include "gtkcsspalettevalueprivate.h"
|
|
|
|
#include "gtkcssrgbavalueprivate.h"
|
|
|
|
#include "gtkiconthemeprivate.h"
|
2017-11-08 15:04:38 +00:00
|
|
|
#include "gdkpixbufutilsprivate.h"
|
2016-01-14 18:48:43 +00:00
|
|
|
|
|
|
|
#include "gtkstyleproviderprivate.h"
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
G_DEFINE_TYPE (GtkCssImageRecolor, _gtk_css_image_recolor, GTK_TYPE_CSS_IMAGE)
|
2016-01-14 18:48:43 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_recolor_print (GtkCssImage *image,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
|
|
|
|
char *uri;
|
|
|
|
|
|
|
|
g_string_append (string, "-gtk-recolor(url(");
|
2017-11-08 15:04:38 +00:00
|
|
|
uri = g_file_get_uri (recolor->file);
|
2016-01-14 18:48:43 +00:00
|
|
|
g_string_append (string, uri);
|
|
|
|
g_free (uri);
|
|
|
|
g_string_append (string, ")");
|
|
|
|
if (recolor->palette)
|
|
|
|
{
|
|
|
|
g_string_append (string, ",");
|
|
|
|
_gtk_css_value_print (recolor->palette, string);
|
|
|
|
}
|
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_recolor_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (object);
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
g_clear_pointer (&recolor->palette, _gtk_css_value_unref);
|
|
|
|
g_clear_object (&recolor->file);
|
|
|
|
g_clear_object (&recolor->texture);
|
2016-01-14 18:48:43 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (_gtk_css_image_recolor_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lookup_symbolic_colors (GtkCssStyle *style,
|
|
|
|
GtkCssValue *palette,
|
|
|
|
GdkRGBA *color_out,
|
|
|
|
GdkRGBA *success_out,
|
|
|
|
GdkRGBA *warning_out,
|
|
|
|
GdkRGBA *error_out)
|
|
|
|
{
|
|
|
|
GtkCssValue *color;
|
|
|
|
const GdkRGBA *lookup;
|
|
|
|
|
|
|
|
color = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_COLOR);
|
|
|
|
*color_out = *_gtk_css_rgba_value_get_rgba (color);
|
|
|
|
|
|
|
|
lookup = gtk_css_palette_value_get_color (palette, "success");
|
|
|
|
if (lookup)
|
|
|
|
*success_out = *lookup;
|
|
|
|
else
|
|
|
|
*success_out = *color_out;
|
|
|
|
|
|
|
|
lookup = gtk_css_palette_value_get_color (palette, "warning");
|
|
|
|
if (lookup)
|
|
|
|
*warning_out = *lookup;
|
|
|
|
else
|
|
|
|
*warning_out = *color_out;
|
|
|
|
|
|
|
|
lookup = gtk_css_palette_value_get_color (palette, "error");
|
|
|
|
if (lookup)
|
|
|
|
*error_out = *lookup;
|
|
|
|
else
|
|
|
|
*error_out = *color_out;
|
|
|
|
}
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
static void
|
|
|
|
gtk_css_image_recolor_load_texture (GtkCssImageRecolor *recolor,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
char *uri;
|
|
|
|
|
|
|
|
if (recolor->texture)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uri = g_file_get_uri (recolor->file);
|
|
|
|
|
2017-11-30 12:54:52 +00:00
|
|
|
if (g_file_has_uri_scheme (recolor->file, "resource"))
|
2017-11-08 15:04:38 +00:00
|
|
|
{
|
2017-11-30 12:54:52 +00:00
|
|
|
char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
|
2017-11-08 15:04:38 +00:00
|
|
|
|
2017-11-30 12:54:52 +00:00
|
|
|
if (g_str_has_suffix (uri, ".symbolic.png"))
|
|
|
|
recolor->texture = gdk_texture_new_from_resource (resource_path);
|
2017-11-08 15:04:38 +00:00
|
|
|
else
|
2017-11-30 12:54:52 +00:00
|
|
|
recolor->texture = gtk_make_symbolic_texture_from_resource (resource_path, 0, 0, 1.0, NULL);
|
|
|
|
|
|
|
|
g_free (resource_path);
|
2017-11-08 15:04:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-30 12:54:52 +00:00
|
|
|
if (g_str_has_suffix (uri, ".symbolic.png"))
|
|
|
|
recolor->texture = gdk_texture_new_from_file (recolor->file, NULL);
|
2017-11-08 15:04:38 +00:00
|
|
|
else
|
2017-11-30 12:54:52 +00:00
|
|
|
recolor->texture = gtk_make_symbolic_texture_from_file (recolor->file, 0, 0, 1.0, NULL);
|
2017-11-08 15:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:48:43 +00:00
|
|
|
static GtkCssImage *
|
2016-01-30 03:47:38 +00:00
|
|
|
gtk_css_image_recolor_load (GtkCssImageRecolor *recolor,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssValue *palette,
|
|
|
|
gint scale,
|
|
|
|
GError **gerror)
|
2016-01-14 18:48:43 +00:00
|
|
|
{
|
2016-01-30 03:47:38 +00:00
|
|
|
GError *local_error = NULL;
|
2017-11-08 15:04:38 +00:00
|
|
|
GtkCssImageRecolor *image;
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
image = g_object_new (GTK_TYPE_CSS_IMAGE_RECOLOR, NULL);
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
lookup_symbolic_colors (style, palette, &image->color, &image->success, &image->warning, &image->error);
|
|
|
|
gtk_css_image_recolor_load_texture (recolor, &local_error);
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
image->file = g_object_ref (recolor->file);
|
|
|
|
|
|
|
|
if (recolor->texture)
|
|
|
|
image->texture = g_object_ref (recolor->texture);
|
|
|
|
else
|
2016-01-14 18:48:43 +00:00
|
|
|
{
|
2016-01-30 03:47:38 +00:00
|
|
|
if (gerror)
|
|
|
|
{
|
|
|
|
char *uri;
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
uri = g_file_get_uri (recolor->file);
|
2016-01-30 03:47:38 +00:00
|
|
|
g_set_error (gerror,
|
2019-03-22 03:20:17 +00:00
|
|
|
GTK_CSS_PARSER_ERROR,
|
|
|
|
GTK_CSS_PARSER_ERROR_FAILED,
|
2019-04-18 00:08:16 +00:00
|
|
|
"Error loading image '%s': %s", uri, local_error ? local_error->message : "");
|
2016-01-30 03:47:38 +00:00
|
|
|
g_free (uri);
|
|
|
|
}
|
2016-01-14 18:48:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
g_clear_error (&local_error);
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2017-11-09 03:09:34 +00:00
|
|
|
return GTK_CSS_IMAGE (image);
|
2016-01-14 18:48:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
static void
|
|
|
|
gtk_css_image_recolor_snapshot (GtkCssImage *image,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
|
|
|
|
graphene_matrix_t matrix;
|
|
|
|
graphene_vec4_t offset;
|
|
|
|
GdkRGBA fg = recolor->color;
|
|
|
|
GdkRGBA sc = recolor->success;
|
|
|
|
GdkRGBA wc = recolor->warning;
|
|
|
|
GdkRGBA ec = recolor->error;
|
|
|
|
|
|
|
|
if (recolor->texture == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
graphene_matrix_init_from_float (&matrix,
|
|
|
|
(float[16]) {
|
|
|
|
sc.red - fg.red, sc.green - fg.green, sc.blue - fg.blue, 0,
|
|
|
|
wc.red - fg.red, wc.green - fg.green, wc.blue - fg.blue, 0,
|
|
|
|
ec.red - fg.red, ec.green - fg.green, ec.blue - fg.blue, 0,
|
|
|
|
0, 0, 0, fg.alpha
|
|
|
|
});
|
|
|
|
graphene_vec4_init (&offset, fg.red, fg.green, fg.blue, 0);
|
2018-04-24 01:17:23 +00:00
|
|
|
gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
|
2017-11-08 15:04:38 +00:00
|
|
|
|
|
|
|
gtk_snapshot_append_texture (snapshot,
|
|
|
|
recolor->texture,
|
2018-04-24 01:17:23 +00:00
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height));
|
2017-11-08 15:04:38 +00:00
|
|
|
|
|
|
|
gtk_snapshot_pop (snapshot);
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:48:43 +00:00
|
|
|
static GtkCssImage *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_image_recolor_compute (GtkCssImage *image,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2016-01-14 18:48:43 +00:00
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
|
|
|
|
GtkCssValue *palette;
|
|
|
|
GtkCssImage *img;
|
2016-01-29 22:30:10 +00:00
|
|
|
int scale;
|
2016-01-30 03:47:38 +00:00
|
|
|
GError *error = NULL;
|
2016-01-29 22:30:10 +00:00
|
|
|
|
2017-10-31 03:31:46 +00:00
|
|
|
scale = gtk_style_provider_get_scale (provider);
|
2016-01-14 18:48:43 +00:00
|
|
|
|
|
|
|
if (recolor->palette)
|
|
|
|
palette = _gtk_css_value_compute (recolor->palette, property_id, provider, style, parent_style);
|
|
|
|
else
|
2016-01-21 13:42:55 +00:00
|
|
|
palette = _gtk_css_value_ref (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_PALETTE));
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2016-01-30 03:47:38 +00:00
|
|
|
img = gtk_css_image_recolor_load (recolor, style, palette, scale, &error);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
GtkCssSection *section = gtk_css_style_get_section (style, property_id);
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_style_provider_emit_error (provider, section, error);
|
2016-01-30 03:47:38 +00:00
|
|
|
g_error_free (error);
|
|
|
|
}
|
2016-01-14 18:48:43 +00:00
|
|
|
|
|
|
|
_gtk_css_value_unref (palette);
|
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2019-03-30 19:20:11 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_image_recolor_parse_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data)
|
2016-01-14 18:48:43 +00:00
|
|
|
{
|
2019-03-30 19:20:11 +00:00
|
|
|
GtkCssImageRecolor *self = data;
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2019-03-30 19:20:11 +00:00
|
|
|
switch (arg)
|
|
|
|
{
|
|
|
|
case 0:
|
2019-04-09 00:29:27 +00:00
|
|
|
self->file = gtk_css_parser_consume_url (parser);
|
2019-03-30 19:20:11 +00:00
|
|
|
if (self->file == NULL)
|
2019-04-09 00:29:27 +00:00
|
|
|
return 0;
|
2019-03-30 19:20:11 +00:00
|
|
|
return 1;
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2019-03-30 19:20:11 +00:00
|
|
|
case 1:
|
|
|
|
self->palette = gtk_css_palette_value_parse (parser);
|
|
|
|
if (self->palette == NULL)
|
2019-04-09 03:40:50 +00:00
|
|
|
return 0;
|
2019-03-30 19:20:11 +00:00
|
|
|
return 1;
|
2016-01-14 18:48:43 +00:00
|
|
|
|
2019-03-30 19:20:11 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_image_recolor_parse (GtkCssImage *image,
|
|
|
|
GtkCssParser *parser)
|
|
|
|
{
|
|
|
|
if (!gtk_css_parser_has_function (parser, "-gtk-recolor"))
|
2016-01-14 18:48:43 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected '-gtk-recolor('");
|
2016-01-14 18:48:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-03-30 19:20:11 +00:00
|
|
|
return gtk_css_parser_consume_function (parser, 1, 2, gtk_css_image_recolor_parse_arg, image);
|
2016-01-14 18:48:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
static int
|
|
|
|
gtk_css_image_recolor_get_width (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
|
|
|
|
|
|
|
|
gtk_css_image_recolor_load_texture (recolor, NULL);
|
|
|
|
|
|
|
|
if (recolor->texture == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return gdk_texture_get_width (recolor->texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_css_image_recolor_get_height (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
|
|
|
|
|
|
|
|
gtk_css_image_recolor_load_texture (recolor, NULL);
|
|
|
|
|
|
|
|
if (recolor->texture == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return gdk_texture_get_height (recolor->texture);
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:48:43 +00:00
|
|
|
static void
|
|
|
|
_gtk_css_image_recolor_class_init (GtkCssImageRecolorClass *klass)
|
|
|
|
{
|
|
|
|
GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2017-11-08 15:04:38 +00:00
|
|
|
image_class->get_width = gtk_css_image_recolor_get_width;
|
|
|
|
image_class->get_height = gtk_css_image_recolor_get_height;
|
2016-01-14 18:48:43 +00:00
|
|
|
image_class->compute = gtk_css_image_recolor_compute;
|
2017-11-08 15:04:38 +00:00
|
|
|
image_class->snapshot = gtk_css_image_recolor_snapshot;
|
|
|
|
image_class->parse = gtk_css_image_recolor_parse;
|
2016-01-14 18:48:43 +00:00
|
|
|
image_class->print = gtk_css_image_recolor_print;
|
|
|
|
|
|
|
|
object_class->dispose = gtk_css_image_recolor_dispose;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_css_image_recolor_init (GtkCssImageRecolor *image_recolor)
|
|
|
|
{
|
|
|
|
}
|