2014-05-01 17:50:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 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: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcssimageiconthemeprivate.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2020-10-03 02:28:17 +00:00
|
|
|
#include "gtk/css/gtkcssserializerprivate.h"
|
2014-05-01 17:50:22 +00:00
|
|
|
#include "gtksettingsprivate.h"
|
2016-12-14 03:55:24 +00:00
|
|
|
#include "gtksnapshot.h"
|
2014-05-01 17:50:22 +00:00
|
|
|
#include "gtkstyleproviderprivate.h"
|
2016-01-13 21:17:44 +00:00
|
|
|
#include "gtkiconthemeprivate.h"
|
2014-05-01 17:50:22 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkCssImageIconTheme, _gtk_css_image_icon_theme, GTK_TYPE_CSS_IMAGE)
|
|
|
|
|
2014-09-06 01:21:49 +00:00
|
|
|
static double
|
2014-05-01 17:50:22 +00:00
|
|
|
gtk_css_image_icon_theme_get_aspect_ratio (GtkCssImage *image)
|
|
|
|
{
|
|
|
|
/* icon theme icons only take a single size when requesting, so we insist on being square */
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-12-14 03:55:24 +00:00
|
|
|
gtk_css_image_icon_theme_snapshot (GtkCssImage *image,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
|
|
|
GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
|
2020-02-04 16:19:22 +00:00
|
|
|
GtkIconPaintable *icon;
|
2020-01-27 15:42:38 +00:00
|
|
|
double icon_width, icon_height;
|
2020-07-24 13:54:49 +00:00
|
|
|
int size;
|
2020-01-27 15:42:38 +00:00
|
|
|
double x, y;
|
2014-05-01 17:50:22 +00:00
|
|
|
|
|
|
|
size = floor (MIN (width, height));
|
|
|
|
if (size <= 0)
|
|
|
|
return;
|
|
|
|
|
2017-03-29 17:26:18 +00:00
|
|
|
if (size == icon_theme->cached_size &&
|
2020-01-27 15:42:38 +00:00
|
|
|
icon_theme->cached_icon != NULL)
|
2014-05-16 02:06:23 +00:00
|
|
|
{
|
2020-01-30 09:52:09 +00:00
|
|
|
icon = icon_theme->cached_icon;
|
2014-05-16 02:06:23 +00:00
|
|
|
}
|
2017-03-29 17:26:18 +00:00
|
|
|
else
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
2020-01-30 09:52:09 +00:00
|
|
|
icon = gtk_icon_theme_lookup_icon (icon_theme->icon_theme,
|
|
|
|
icon_theme->name,
|
2020-02-04 02:53:22 +00:00
|
|
|
NULL,
|
2020-01-30 09:52:09 +00:00
|
|
|
size,
|
|
|
|
icon_theme->scale,
|
2020-02-01 23:27:14 +00:00
|
|
|
GTK_TEXT_DIR_NONE,
|
2020-02-01 23:35:42 +00:00
|
|
|
0);
|
2020-01-30 09:52:09 +00:00
|
|
|
|
|
|
|
g_assert (icon != NULL);
|
2017-11-30 00:44:30 +00:00
|
|
|
|
2020-01-27 15:42:38 +00:00
|
|
|
g_clear_object (&icon_theme->cached_icon);
|
2017-11-30 00:44:30 +00:00
|
|
|
|
2017-03-29 17:26:18 +00:00
|
|
|
icon_theme->cached_size = size;
|
2020-01-30 09:52:09 +00:00
|
|
|
icon_theme->cached_icon = icon;
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 09:52:09 +00:00
|
|
|
icon_width = (double) MIN (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon)), width);
|
|
|
|
icon_height = (double) MIN (gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon)), height);
|
2016-12-14 03:55:24 +00:00
|
|
|
|
2020-01-27 15:42:38 +00:00
|
|
|
x = (width - icon_width) / 2;
|
|
|
|
y = (height - icon_height) / 2;
|
|
|
|
|
|
|
|
if (x != 0 || y != 0)
|
2017-11-08 02:39:47 +00:00
|
|
|
{
|
2020-01-27 15:42:38 +00:00
|
|
|
gtk_snapshot_save (snapshot);
|
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (x, y));
|
2017-11-08 02:39:47 +00:00
|
|
|
}
|
2020-02-04 16:19:22 +00:00
|
|
|
gtk_icon_paintable_snapshot_with_colors (icon, snapshot,
|
|
|
|
icon_width,
|
|
|
|
icon_height,
|
|
|
|
&icon_theme->color,
|
|
|
|
&icon_theme->success,
|
|
|
|
&icon_theme->warning,
|
|
|
|
&icon_theme->error);
|
2020-01-27 15:42:38 +00:00
|
|
|
if (x != 0 || y != 0)
|
|
|
|
gtk_snapshot_restore (snapshot);
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:11:08 +00:00
|
|
|
static guint
|
|
|
|
gtk_css_image_icon_theme_parse_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkCssImageIconTheme *icon_theme = data;
|
|
|
|
|
2019-04-04 21:37:38 +00:00
|
|
|
icon_theme->name = gtk_css_parser_consume_string (parser);
|
2019-03-25 03:11:08 +00:00
|
|
|
if (icon_theme->name == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-01 17:50:22 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_image_icon_theme_parse (GtkCssImage *image,
|
2014-05-16 02:06:23 +00:00
|
|
|
GtkCssParser *parser)
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
2019-03-25 03:11:08 +00:00
|
|
|
if (!gtk_css_parser_has_function (parser, "-gtk-icontheme"))
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected '-gtk-icontheme('");
|
2014-05-01 17:50:22 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:11:08 +00:00
|
|
|
return gtk_css_parser_consume_function (parser, 1, 1, gtk_css_image_icon_theme_parse_arg, image);
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_image_icon_theme_print (GtkCssImage *image,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
|
|
|
|
|
|
|
|
g_string_append (string, "-gtk-icontheme(");
|
2020-10-03 02:31:29 +00:00
|
|
|
gtk_css_print_string (string, icon_theme->name, FALSE);
|
2014-05-01 17:50:22 +00:00
|
|
|
g_string_append (string, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssImage *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_image_icon_theme_compute (GtkCssImage *image,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
|
|
|
GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
|
|
|
|
GtkCssImageIconTheme *copy;
|
2020-04-22 00:41:59 +00:00
|
|
|
GtkSettings *settings;
|
|
|
|
GdkDisplay *display;
|
2014-05-01 17:50:22 +00:00
|
|
|
|
|
|
|
copy = g_object_new (GTK_TYPE_CSS_IMAGE_ICON_THEME, NULL);
|
|
|
|
copy->name = g_strdup (icon_theme->name);
|
2020-04-22 00:41:59 +00:00
|
|
|
settings = gtk_style_provider_get_settings (provider);
|
|
|
|
display = _gtk_settings_get_display (settings);
|
|
|
|
copy->icon_theme = gtk_icon_theme_get_for_display (display);
|
2020-04-22 17:49:18 +00:00
|
|
|
copy->serial = gtk_icon_theme_get_serial (copy->icon_theme);
|
2017-10-31 03:31:46 +00:00
|
|
|
copy->scale = gtk_style_provider_get_scale (provider);
|
2016-01-13 21:17:44 +00:00
|
|
|
gtk_icon_theme_lookup_symbolic_colors (style, ©->color, ©->success, ©->warning, ©->error);
|
2014-05-16 02:06:23 +00:00
|
|
|
|
2014-05-01 17:50:22 +00:00
|
|
|
return GTK_CSS_IMAGE (copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_image_icon_theme_equal (GtkCssImage *image1,
|
2014-06-29 02:39:02 +00:00
|
|
|
GtkCssImage *image2)
|
2014-05-01 17:50:22 +00:00
|
|
|
{
|
2019-08-17 16:55:35 +00:00
|
|
|
GtkCssImageIconTheme *icon_theme1 = (GtkCssImageIconTheme *) image1;
|
|
|
|
GtkCssImageIconTheme *icon_theme2 = (GtkCssImageIconTheme *) image2;
|
2014-05-01 17:50:22 +00:00
|
|
|
|
2020-04-22 17:49:18 +00:00
|
|
|
return icon_theme1->serial == icon_theme2->serial &&
|
|
|
|
icon_theme1->icon_theme == icon_theme2->icon_theme &&
|
|
|
|
g_str_equal (icon_theme1->name, icon_theme2->name);
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 02:39:02 +00:00
|
|
|
static void
|
|
|
|
gtk_css_image_icon_theme_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (object);
|
|
|
|
|
|
|
|
g_free (icon_theme->name);
|
|
|
|
icon_theme->name = NULL;
|
|
|
|
|
2020-01-27 15:42:38 +00:00
|
|
|
g_clear_object (&icon_theme->cached_icon);
|
2017-03-29 17:26:18 +00:00
|
|
|
|
2014-06-29 02:39:02 +00:00
|
|
|
G_OBJECT_CLASS (_gtk_css_image_icon_theme_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2014-05-01 17:50:22 +00:00
|
|
|
static void
|
|
|
|
_gtk_css_image_icon_theme_class_init (GtkCssImageIconThemeClass *klass)
|
|
|
|
{
|
|
|
|
GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
|
2014-06-29 02:39:02 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2014-05-01 17:50:22 +00:00
|
|
|
|
|
|
|
image_class->get_aspect_ratio = gtk_css_image_icon_theme_get_aspect_ratio;
|
2016-12-14 03:55:24 +00:00
|
|
|
image_class->snapshot = gtk_css_image_icon_theme_snapshot;
|
2014-05-01 17:50:22 +00:00
|
|
|
image_class->parse = gtk_css_image_icon_theme_parse;
|
|
|
|
image_class->print = gtk_css_image_icon_theme_print;
|
|
|
|
image_class->compute = gtk_css_image_icon_theme_compute;
|
|
|
|
image_class->equal = gtk_css_image_icon_theme_equal;
|
2014-06-29 02:39:02 +00:00
|
|
|
|
|
|
|
object_class->dispose = gtk_css_image_icon_theme_dispose;
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_css_image_icon_theme_init (GtkCssImageIconTheme *icon_theme)
|
|
|
|
{
|
2020-02-01 22:38:49 +00:00
|
|
|
icon_theme->icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
|
2014-05-01 17:50:22 +00:00
|
|
|
icon_theme->scale = 1;
|
2017-03-29 17:26:18 +00:00
|
|
|
icon_theme->cached_size = -1;
|
2020-01-27 15:42:38 +00:00
|
|
|
icon_theme->cached_icon = NULL;
|
2014-05-01 17:50:22 +00:00
|
|
|
}
|
|
|
|
|