2011-11-30 03:26:19 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* Authors: 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-11-30 03:26:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2014-05-10 19:50:37 +00:00
|
|
|
#include "gtkiconhelperprivate.h"
|
|
|
|
|
2013-06-24 12:13:43 +00:00
|
|
|
#include <math.h>
|
2014-05-10 19:50:37 +00:00
|
|
|
|
2015-03-11 20:24:05 +00:00
|
|
|
#include "gtkcssenumvalueprivate.h"
|
2015-12-02 02:18:26 +00:00
|
|
|
#include "gtkcssiconthemevalueprivate.h"
|
2015-12-09 17:32:05 +00:00
|
|
|
#include "gtkcssnodeprivate.h"
|
2017-11-15 03:29:31 +00:00
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2015-12-09 17:32:05 +00:00
|
|
|
#include "gtkcssstyleprivate.h"
|
2015-12-11 14:12:19 +00:00
|
|
|
#include "gtkcssstylepropertyprivate.h"
|
2015-12-14 04:29:35 +00:00
|
|
|
#include "gtkcsstransientnodeprivate.h"
|
2015-12-07 03:44:39 +00:00
|
|
|
#include "gtkiconthemeprivate.h"
|
2015-12-09 17:32:05 +00:00
|
|
|
#include "gtkrendericonprivate.h"
|
2016-11-19 18:58:23 +00:00
|
|
|
#include "gtksnapshot.h"
|
2017-01-10 23:37:12 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2011-11-30 03:26:19 +00:00
|
|
|
|
2018-03-14 00:56:30 +00:00
|
|
|
struct _GtkIconHelper
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
|
|
|
GtkImageDefinition *def;
|
|
|
|
|
|
|
|
gint pixel_size;
|
|
|
|
|
|
|
|
guint use_fallback : 1;
|
|
|
|
guint force_scale_pixbuf : 1;
|
|
|
|
guint texture_is_symbolic : 1;
|
|
|
|
|
|
|
|
GtkWidget *owner;
|
|
|
|
GtkCssNode *node;
|
|
|
|
GdkPaintable *paintable;
|
|
|
|
int texture_scale;
|
|
|
|
};
|
|
|
|
|
2018-02-27 00:01:26 +00:00
|
|
|
static int
|
|
|
|
get_default_size (GtkIconHelper *self)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2018-02-27 00:01:26 +00:00
|
|
|
GtkCssStyle *style;
|
2011-11-30 03:26:19 +00:00
|
|
|
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->pixel_size != -1)
|
2018-02-27 00:01:26 +00:00
|
|
|
return self->pixel_size;
|
2011-11-30 03:26:19 +00:00
|
|
|
|
2018-02-27 00:01:26 +00:00
|
|
|
style = gtk_css_node_get_style (self->node);
|
|
|
|
return _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_SIZE), 100);
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkIconLookupFlags
|
2015-12-07 03:44:39 +00:00
|
|
|
get_icon_lookup_flags (GtkIconHelper *self,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkTextDirection dir)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2015-03-11 20:24:05 +00:00
|
|
|
GtkIconLookupFlags flags;
|
|
|
|
GtkCssIconStyle icon_style;
|
|
|
|
|
|
|
|
flags = GTK_ICON_LOOKUP_USE_BUILTIN;
|
2011-11-30 03:26:19 +00:00
|
|
|
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->pixel_size != -1 || self->force_scale_pixbuf)
|
2011-11-30 03:26:19 +00:00
|
|
|
flags |= GTK_ICON_LOOKUP_FORCE_SIZE;
|
|
|
|
|
2015-12-07 03:44:39 +00:00
|
|
|
icon_style = _gtk_css_icon_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_STYLE));
|
2015-03-11 20:24:05 +00:00
|
|
|
|
|
|
|
switch (icon_style)
|
|
|
|
{
|
|
|
|
case GTK_CSS_ICON_STYLE_REGULAR:
|
|
|
|
flags |= GTK_ICON_LOOKUP_FORCE_REGULAR;
|
|
|
|
break;
|
|
|
|
case GTK_CSS_ICON_STYLE_SYMBOLIC:
|
|
|
|
flags |= GTK_ICON_LOOKUP_FORCE_SYMBOLIC;
|
|
|
|
break;
|
|
|
|
case GTK_CSS_ICON_STYLE_REQUESTED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:44:39 +00:00
|
|
|
if (dir == GTK_TEXT_DIR_LTR)
|
2015-03-11 20:24:05 +00:00
|
|
|
flags |= GTK_ICON_LOOKUP_DIR_LTR;
|
2015-12-07 03:44:39 +00:00
|
|
|
else if (dir == GTK_TEXT_DIR_RTL)
|
2015-03-11 20:24:05 +00:00
|
|
|
flags |= GTK_ICON_LOOKUP_DIR_RTL;
|
2014-05-10 19:50:37 +00:00
|
|
|
|
2011-11-30 03:26:19 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
static GdkPaintable *
|
|
|
|
ensure_paintable_from_texture (GtkIconHelper *self,
|
|
|
|
GdkTexture *texture,
|
|
|
|
int *scale)
|
2017-11-04 18:05:52 +00:00
|
|
|
{
|
2018-03-14 03:38:18 +00:00
|
|
|
*scale = gtk_image_definition_get_storage_type (self->def);
|
2017-11-04 18:05:52 +00:00
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
return g_object_ref (GDK_PAINTABLE (texture));
|
2017-11-04 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
static GdkPaintable *
|
|
|
|
ensure_paintable_from_paintable (GtkIconHelper *self,
|
|
|
|
GdkPaintable *paintable,
|
|
|
|
int *scale)
|
|
|
|
{
|
|
|
|
*scale = 1;
|
|
|
|
|
|
|
|
return g_object_ref (paintable);
|
|
|
|
}
|
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
static GdkPaintable *
|
|
|
|
ensure_paintable_for_gicon (GtkIconHelper *self,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkTextDirection dir,
|
|
|
|
gint scale,
|
|
|
|
GIcon *gicon,
|
|
|
|
gboolean *symbolic)
|
2013-06-24 12:13:43 +00:00
|
|
|
{
|
2015-12-04 15:06:08 +00:00
|
|
|
GtkIconTheme *icon_theme;
|
|
|
|
gint width, height;
|
|
|
|
GtkIconInfo *info;
|
|
|
|
GtkIconLookupFlags flags;
|
2017-11-05 05:30:45 +00:00
|
|
|
GdkTexture *texture;
|
2013-06-24 12:13:43 +00:00
|
|
|
|
2015-12-04 15:06:08 +00:00
|
|
|
icon_theme = gtk_css_icon_theme_value_get_icon_theme
|
2015-12-07 03:44:39 +00:00
|
|
|
(gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_THEME));
|
|
|
|
flags = get_icon_lookup_flags (self, style, dir);
|
2013-06-24 12:13:43 +00:00
|
|
|
|
2018-02-27 00:01:26 +00:00
|
|
|
width = height = get_default_size (self);
|
2015-12-04 15:06:08 +00:00
|
|
|
|
|
|
|
info = gtk_icon_theme_lookup_by_gicon_for_scale (icon_theme,
|
|
|
|
gicon,
|
|
|
|
MIN (width, height),
|
|
|
|
scale, flags);
|
2017-11-30 22:31:17 +00:00
|
|
|
if (info == NULL)
|
|
|
|
info = gtk_icon_theme_lookup_icon (icon_theme,
|
|
|
|
"image-missing",
|
|
|
|
width,
|
|
|
|
flags | GTK_ICON_LOOKUP_USE_BUILTIN | GTK_ICON_LOOKUP_GENERIC_FALLBACK);
|
|
|
|
|
|
|
|
*symbolic = gtk_icon_info_is_symbolic (info);
|
|
|
|
texture = gtk_icon_info_load_texture (info);
|
2015-11-29 23:45:58 +00:00
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
return GDK_PAINTABLE (texture);
|
2013-06-24 12:13:43 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
static GdkPaintable *
|
|
|
|
gtk_icon_helper_load_paintable (GtkIconHelper *self,
|
|
|
|
int *out_scale,
|
|
|
|
gboolean *out_symbolic)
|
2013-06-24 12:13:43 +00:00
|
|
|
{
|
2018-02-16 08:09:35 +00:00
|
|
|
GdkPaintable *paintable;
|
2015-11-24 18:55:09 +00:00
|
|
|
GIcon *gicon;
|
2017-11-05 05:30:45 +00:00
|
|
|
int scale;
|
|
|
|
gboolean symbolic;
|
2013-06-24 12:13:43 +00:00
|
|
|
|
2017-09-30 15:26:42 +00:00
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
2013-06-24 12:13:43 +00:00
|
|
|
{
|
2017-11-04 18:05:52 +00:00
|
|
|
case GTK_IMAGE_TEXTURE:
|
2018-02-16 08:09:35 +00:00
|
|
|
paintable = ensure_paintable_from_texture (self, gtk_image_definition_get_texture (self->def), &scale);
|
2017-11-05 05:30:45 +00:00
|
|
|
symbolic = FALSE;
|
2017-11-04 18:05:52 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
paintable = ensure_paintable_from_paintable (self, gtk_image_definition_get_paintable (self->def), &scale);
|
|
|
|
symbolic = FALSE;
|
|
|
|
break;
|
|
|
|
|
2013-06-24 12:13:43 +00:00
|
|
|
case GTK_IMAGE_ICON_NAME:
|
2017-11-05 05:30:45 +00:00
|
|
|
scale = gtk_widget_get_scale_factor (self->owner);
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->use_fallback)
|
|
|
|
gicon = g_themed_icon_new_with_default_fallbacks (gtk_image_definition_get_icon_name (self->def));
|
2015-11-24 18:55:09 +00:00
|
|
|
else
|
2017-09-30 15:26:42 +00:00
|
|
|
gicon = g_themed_icon_new (gtk_image_definition_get_icon_name (self->def));
|
2018-02-16 08:09:35 +00:00
|
|
|
paintable = ensure_paintable_for_gicon (self,
|
|
|
|
gtk_css_node_get_style (self->node),
|
|
|
|
gtk_widget_get_direction (self->owner),
|
|
|
|
scale,
|
|
|
|
gicon,
|
|
|
|
&symbolic);
|
2015-11-24 18:55:09 +00:00
|
|
|
g_object_unref (gicon);
|
|
|
|
break;
|
|
|
|
|
2013-06-24 12:13:43 +00:00
|
|
|
case GTK_IMAGE_GICON:
|
2017-11-05 05:30:45 +00:00
|
|
|
scale = gtk_widget_get_scale_factor (self->owner);
|
2018-02-16 08:09:35 +00:00
|
|
|
paintable = ensure_paintable_for_gicon (self,
|
|
|
|
gtk_css_node_get_style (self->node),
|
|
|
|
gtk_widget_get_direction (self->owner),
|
|
|
|
scale,
|
|
|
|
gtk_image_definition_get_gicon (self->def),
|
|
|
|
&symbolic);
|
2013-06-24 12:13:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
default:
|
2018-02-16 08:09:35 +00:00
|
|
|
paintable = NULL;
|
2017-11-05 05:30:45 +00:00
|
|
|
scale = 1;
|
|
|
|
symbolic = FALSE;
|
2017-10-23 05:53:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:30:45 +00:00
|
|
|
*out_scale = scale;
|
|
|
|
*out_symbolic = symbolic;
|
2017-10-23 05:53:09 +00:00
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
return paintable;
|
2017-10-23 05:53:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 17:25:12 +00:00
|
|
|
static void
|
2018-02-16 08:09:35 +00:00
|
|
|
gtk_icon_helper_ensure_paintable (GtkIconHelper *self)
|
2016-11-07 17:25:12 +00:00
|
|
|
{
|
2017-11-05 05:30:45 +00:00
|
|
|
gboolean symbolic;
|
2017-10-23 05:53:09 +00:00
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
if (self->paintable)
|
2016-11-07 17:25:12 +00:00
|
|
|
return;
|
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
self->paintable = gtk_icon_helper_load_paintable (self,
|
|
|
|
&self->texture_scale,
|
|
|
|
&symbolic);
|
2017-11-05 05:30:45 +00:00
|
|
|
self->texture_is_symbolic = symbolic;
|
2016-11-07 17:25:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 08:40:52 +00:00
|
|
|
static void
|
|
|
|
gtk_icon_helper_paintable_snapshot (GdkPaintable *paintable,
|
|
|
|
GdkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (paintable);
|
|
|
|
GtkCssStyle *style;
|
|
|
|
|
|
|
|
style = gtk_css_node_get_style (self->node);
|
|
|
|
|
|
|
|
gtk_icon_helper_ensure_paintable (self);
|
|
|
|
if (self->paintable == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_css_style_snapshot_icon_paintable (style,
|
|
|
|
snapshot,
|
|
|
|
self->paintable,
|
|
|
|
width, height,
|
|
|
|
self->texture_is_symbolic);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkPaintable *
|
|
|
|
gtk_icon_helper_paintable_get_current_image (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (paintable);
|
|
|
|
|
|
|
|
gtk_icon_helper_ensure_paintable (self);
|
|
|
|
if (self->paintable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gtk_icon_helper_paintable_get_current_image (self->paintable);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_icon_helper_paintable_get_intrinsic_width (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (paintable);
|
|
|
|
|
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
|
|
|
{
|
|
|
|
case GTK_IMAGE_TEXTURE:
|
|
|
|
return gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (gtk_image_definition_get_texture (self->def)));
|
|
|
|
|
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
return gdk_paintable_get_intrinsic_width (gtk_image_definition_get_paintable (self->def));
|
|
|
|
|
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
|
|
|
if (self->pixel_size != -1 || self->force_scale_pixbuf)
|
|
|
|
return get_default_size (self);
|
|
|
|
gtk_icon_helper_ensure_paintable (self);
|
|
|
|
if (self->paintable)
|
|
|
|
return gdk_paintable_get_intrinsic_width (self->paintable);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gtk_icon_helper_paintable_get_intrinsic_height (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (paintable);
|
|
|
|
|
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
|
|
|
{
|
|
|
|
case GTK_IMAGE_TEXTURE:
|
|
|
|
return gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (gtk_image_definition_get_texture (self->def)));
|
|
|
|
|
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
return gdk_paintable_get_intrinsic_height (gtk_image_definition_get_paintable (self->def));
|
|
|
|
|
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
|
|
|
if (self->pixel_size != -1 || self->force_scale_pixbuf)
|
|
|
|
return get_default_size (self);
|
|
|
|
gtk_icon_helper_ensure_paintable (self);
|
|
|
|
if (self->paintable)
|
|
|
|
return gdk_paintable_get_intrinsic_height (self->paintable);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static double gtk_icon_helper_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (paintable);
|
|
|
|
|
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
|
|
|
{
|
|
|
|
case GTK_IMAGE_TEXTURE:
|
|
|
|
return gdk_paintable_get_intrinsic_aspect_ratio (GDK_PAINTABLE (gtk_image_definition_get_texture (self->def)));
|
|
|
|
|
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
return gdk_paintable_get_intrinsic_aspect_ratio (gtk_image_definition_get_paintable (self->def));
|
|
|
|
|
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
|
|
|
return 1.0;
|
|
|
|
|
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_icon_helper_paintable_init (GdkPaintableInterface *iface)
|
|
|
|
{
|
|
|
|
iface->snapshot = gtk_icon_helper_paintable_snapshot;
|
|
|
|
iface->get_current_image = gtk_icon_helper_paintable_get_current_image;
|
|
|
|
iface->get_intrinsic_width = gtk_icon_helper_paintable_get_intrinsic_width;
|
|
|
|
iface->get_intrinsic_height = gtk_icon_helper_paintable_get_intrinsic_height;
|
|
|
|
iface->get_intrinsic_aspect_ratio = gtk_icon_helper_paintable_get_intrinsic_aspect_ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkIconHelper, gtk_icon_helper, G_TYPE_OBJECT,
|
|
|
|
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
|
|
|
|
gtk_icon_helper_paintable_init))
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_icon_helper_invalidate (GtkIconHelper *self)
|
|
|
|
{
|
|
|
|
g_clear_object (&self->paintable);
|
|
|
|
self->texture_scale = 1;
|
|
|
|
self->texture_is_symbolic = FALSE;
|
|
|
|
|
|
|
|
if (!GTK_IS_CSS_TRANSIENT_NODE (self->node))
|
|
|
|
gtk_widget_queue_resize (self->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_icon_helper_invalidate_for_change (GtkIconHelper *self,
|
|
|
|
GtkCssStyleChange *change)
|
|
|
|
{
|
|
|
|
if (change == NULL ||
|
|
|
|
((gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_SYMBOLIC_ICON) &&
|
|
|
|
self->texture_is_symbolic) ||
|
|
|
|
(gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_ICON) &&
|
|
|
|
!self->texture_is_symbolic)))
|
|
|
|
{
|
|
|
|
/* Avoid the queue_resize in gtk_icon_helper_invalidate */
|
|
|
|
g_clear_object (&self->paintable);
|
|
|
|
self->texture_scale = 1;
|
|
|
|
self->texture_is_symbolic = FALSE;
|
|
|
|
|
|
|
|
if (change == NULL ||
|
|
|
|
(gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_ICON_SIZE) &&
|
|
|
|
!GTK_IS_CSS_TRANSIENT_NODE (self->node)))
|
|
|
|
gtk_widget_queue_resize (self->owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_icon_helper_take_definition (GtkIconHelper *self,
|
|
|
|
GtkImageDefinition *def)
|
|
|
|
{
|
|
|
|
_gtk_icon_helper_clear (self);
|
|
|
|
|
|
|
|
if (def == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gtk_image_definition_unref (self->def);
|
|
|
|
self->def = def;
|
|
|
|
|
|
|
|
gtk_icon_helper_invalidate (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_icon_helper_clear (GtkIconHelper *self)
|
|
|
|
{
|
|
|
|
g_clear_object (&self->paintable);
|
|
|
|
self->texture_scale = 1;
|
|
|
|
self->texture_is_symbolic = FALSE;
|
|
|
|
|
|
|
|
if (gtk_image_definition_get_storage_type (self->def) != GTK_IMAGE_EMPTY)
|
|
|
|
{
|
|
|
|
gtk_image_definition_unref (self->def);
|
|
|
|
self->def = gtk_image_definition_new_empty ();
|
|
|
|
gtk_icon_helper_invalidate (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_icon_helper_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self = GTK_ICON_HELPER (object);
|
|
|
|
|
|
|
|
_gtk_icon_helper_clear (self);
|
|
|
|
g_signal_handlers_disconnect_by_func (self->owner, G_CALLBACK (gtk_icon_helper_invalidate), self);
|
|
|
|
gtk_image_definition_unref (self->def);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_icon_helper_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_icon_helper_class_init (GtkIconHelperClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = gtk_icon_helper_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_icon_helper_init (GtkIconHelper *self)
|
|
|
|
{
|
|
|
|
self->def = gtk_image_definition_new_empty ();
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIconHelper *
|
|
|
|
gtk_icon_helper_new (GtkCssNode *css_node,
|
|
|
|
GtkWidget *owner)
|
|
|
|
{
|
|
|
|
GtkIconHelper *self;
|
|
|
|
|
|
|
|
self = g_object_new (GTK_TYPE_ICON_HELPER, NULL);
|
|
|
|
|
|
|
|
self->pixel_size = -1;
|
|
|
|
self->texture_is_symbolic = FALSE;
|
|
|
|
|
|
|
|
self->node = css_node;
|
|
|
|
self->owner = owner;
|
|
|
|
g_signal_connect_swapped (owner, "direction-changed", G_CALLBACK (gtk_icon_helper_invalidate), self);
|
|
|
|
g_signal_connect_swapped (owner, "notify::scale-factor", G_CALLBACK (gtk_icon_helper_invalidate), self);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-02-27 00:55:03 +00:00
|
|
|
void
|
|
|
|
gtk_icon_helper_measure (GtkIconHelper *self,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural)
|
|
|
|
{
|
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
|
|
|
{
|
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
{
|
|
|
|
double min_width, min_height, nat_width, nat_height;
|
|
|
|
int default_size = get_default_size (self);
|
|
|
|
|
|
|
|
gdk_paintable_compute_concrete_size (gtk_image_definition_get_paintable (self->def),
|
|
|
|
0, 0,
|
|
|
|
default_size, default_size,
|
|
|
|
&min_width, &min_height);
|
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
|
|
|
gdk_paintable_compute_concrete_size (gtk_image_definition_get_paintable (self->def),
|
|
|
|
0,
|
|
|
|
for_size < 0 ? 0 : for_size,
|
|
|
|
default_size, default_size,
|
|
|
|
&nat_width, &nat_height);
|
|
|
|
*minimum = ceil (min_width);
|
|
|
|
*natural = ceil (nat_width);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gdk_paintable_compute_concrete_size (gtk_image_definition_get_paintable (self->def),
|
|
|
|
for_size < 0 ? 0 : for_size,
|
|
|
|
0,
|
|
|
|
default_size, default_size,
|
|
|
|
&nat_width, &nat_height);
|
|
|
|
*minimum = ceil (min_height);
|
|
|
|
*natural = ceil (nat_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_IMAGE_TEXTURE:
|
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
_gtk_icon_helper_get_size (self, &width, &height);
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
*minimum = *natural = width;
|
|
|
|
else
|
|
|
|
*minimum = *natural = height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
static void
|
|
|
|
get_size_for_paintable (GtkIconHelper *self,
|
|
|
|
GdkPaintable *paintable,
|
|
|
|
int *width_out,
|
|
|
|
int *height_out)
|
|
|
|
{
|
|
|
|
int width = gdk_paintable_get_intrinsic_width (paintable);
|
|
|
|
int height = gdk_paintable_get_intrinsic_height (paintable);
|
|
|
|
|
|
|
|
if (width == 0)
|
|
|
|
{
|
|
|
|
if (height != 0)
|
|
|
|
{
|
|
|
|
double ar = gdk_paintable_get_intrinsic_aspect_ratio (paintable);
|
|
|
|
|
|
|
|
if (ar > 0)
|
|
|
|
width = ceil (height * ar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (height == 0)
|
|
|
|
{
|
|
|
|
double ar = gdk_paintable_get_intrinsic_aspect_ratio (paintable);
|
|
|
|
|
|
|
|
if (ar > 0)
|
|
|
|
height = ceil (width / ar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width == 0 || height == 0)
|
2018-02-27 00:01:26 +00:00
|
|
|
width = height = get_default_size (self);
|
2018-02-16 09:50:49 +00:00
|
|
|
|
|
|
|
*width_out = width;
|
|
|
|
*height_out = height;
|
|
|
|
}
|
|
|
|
|
2011-11-30 03:26:19 +00:00
|
|
|
void
|
|
|
|
_gtk_icon_helper_get_size (GtkIconHelper *self,
|
|
|
|
gint *width_out,
|
|
|
|
gint *height_out)
|
|
|
|
{
|
2017-10-23 13:25:46 +00:00
|
|
|
gint width, height;
|
2011-11-30 03:26:19 +00:00
|
|
|
|
|
|
|
width = height = 0;
|
|
|
|
|
2017-09-30 15:26:42 +00:00
|
|
|
/* Certain kinds of images are easy to calculate the size for,</cosimoc>3 these
|
2014-08-12 10:34:12 +00:00
|
|
|
we do immediately to avoid having to potentially load the image
|
|
|
|
data for something that may not yet be visible */
|
2017-09-30 15:26:42 +00:00
|
|
|
switch (gtk_image_definition_get_storage_type (self->def))
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2014-08-12 10:34:12 +00:00
|
|
|
case GTK_IMAGE_ICON_NAME:
|
|
|
|
case GTK_IMAGE_GICON:
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->pixel_size != -1 || self->force_scale_pixbuf)
|
2018-02-27 00:01:26 +00:00
|
|
|
width = height = get_default_size (self);
|
2017-11-04 18:05:52 +00:00
|
|
|
break;
|
2014-08-12 10:34:12 +00:00
|
|
|
|
2017-11-04 18:05:52 +00:00
|
|
|
case GTK_IMAGE_TEXTURE:
|
|
|
|
{
|
|
|
|
GdkTexture *texture = gtk_image_definition_get_texture (self->def);
|
|
|
|
width = gdk_texture_get_width (texture);
|
|
|
|
height = gdk_texture_get_height (texture);
|
|
|
|
}
|
2014-08-12 10:34:12 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
case GTK_IMAGE_PAINTABLE:
|
|
|
|
{
|
|
|
|
GdkPaintable *paintable = gtk_image_definition_get_paintable (self->def);
|
|
|
|
|
|
|
|
get_size_for_paintable (self, paintable, &width, &height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-08-12 10:34:12 +00:00
|
|
|
case GTK_IMAGE_EMPTY:
|
|
|
|
default:
|
|
|
|
break;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
2014-08-12 10:34:12 +00:00
|
|
|
|
2018-03-15 02:01:58 +00:00
|
|
|
/* Otherwise we load the paintable to guarantee we get a size */
|
2014-08-12 10:34:12 +00:00
|
|
|
if (width == 0)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2018-02-16 08:09:35 +00:00
|
|
|
gtk_icon_helper_ensure_paintable (self);
|
2014-08-12 10:34:12 +00:00
|
|
|
|
2018-02-16 08:09:35 +00:00
|
|
|
if (self->paintable != NULL)
|
2014-08-12 10:34:12 +00:00
|
|
|
{
|
2018-02-16 09:50:49 +00:00
|
|
|
get_size_for_paintable (self, self->paintable, &width, &height);
|
2014-08-12 10:34:12 +00:00
|
|
|
}
|
2017-11-15 03:29:31 +00:00
|
|
|
else
|
2014-08-12 10:34:12 +00:00
|
|
|
{
|
2018-02-27 00:01:26 +00:00
|
|
|
width = height = get_default_size (self);
|
2014-08-12 10:34:12 +00:00
|
|
|
}
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (width_out)
|
|
|
|
*width_out = width;
|
|
|
|
if (height_out)
|
|
|
|
*height_out = height;
|
|
|
|
}
|
|
|
|
|
2015-11-25 14:59:44 +00:00
|
|
|
void
|
|
|
|
_gtk_icon_helper_set_definition (GtkIconHelper *self,
|
|
|
|
GtkImageDefinition *def)
|
|
|
|
{
|
|
|
|
if (def)
|
|
|
|
gtk_icon_helper_take_definition (self, gtk_image_definition_ref (def));
|
|
|
|
else
|
|
|
|
_gtk_icon_helper_clear (self);
|
|
|
|
}
|
|
|
|
|
2017-11-15 00:43:13 +00:00
|
|
|
void
|
2011-11-30 03:26:19 +00:00
|
|
|
_gtk_icon_helper_set_gicon (GtkIconHelper *self,
|
2017-11-15 00:43:13 +00:00
|
|
|
GIcon *gicon)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2015-12-01 03:44:29 +00:00
|
|
|
gtk_icon_helper_take_definition (self, gtk_image_definition_new_gicon (gicon));
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 00:43:13 +00:00
|
|
|
void
|
2011-11-30 03:26:19 +00:00
|
|
|
_gtk_icon_helper_set_icon_name (GtkIconHelper *self,
|
2017-11-15 00:43:13 +00:00
|
|
|
const gchar *icon_name)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2015-12-01 03:44:29 +00:00
|
|
|
gtk_icon_helper_take_definition (self, gtk_image_definition_new_icon_name (icon_name));
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 18:05:52 +00:00
|
|
|
void
|
|
|
|
_gtk_icon_helper_set_texture (GtkIconHelper *self,
|
|
|
|
GdkTexture *texture)
|
|
|
|
{
|
2018-03-14 03:45:25 +00:00
|
|
|
gtk_icon_helper_take_definition (self, gtk_image_definition_new_texture (texture));
|
2017-11-04 18:05:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
void
|
|
|
|
_gtk_icon_helper_set_paintable (GtkIconHelper *self,
|
|
|
|
GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
gtk_icon_helper_take_definition (self, gtk_image_definition_new_paintable (paintable));
|
|
|
|
}
|
|
|
|
|
2014-06-09 12:58:05 +00:00
|
|
|
gboolean
|
2011-11-30 03:26:19 +00:00
|
|
|
_gtk_icon_helper_set_pixel_size (GtkIconHelper *self,
|
2014-06-09 12:58:05 +00:00
|
|
|
gint pixel_size)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->pixel_size != pixel_size)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
self->pixel_size = pixel_size;
|
2015-12-11 14:12:19 +00:00
|
|
|
gtk_icon_helper_invalidate (self);
|
2014-06-09 12:58:05 +00:00
|
|
|
return TRUE;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
2014-06-09 12:58:05 +00:00
|
|
|
return FALSE;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 12:58:05 +00:00
|
|
|
gboolean
|
2011-11-30 03:26:19 +00:00
|
|
|
_gtk_icon_helper_set_use_fallback (GtkIconHelper *self,
|
2014-06-09 12:58:05 +00:00
|
|
|
gboolean use_fallback)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->use_fallback != use_fallback)
|
2011-11-30 03:26:19 +00:00
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
self->use_fallback = use_fallback;
|
2015-12-11 14:12:19 +00:00
|
|
|
gtk_icon_helper_invalidate (self);
|
2014-06-09 12:58:05 +00:00
|
|
|
return TRUE;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
2014-06-09 12:58:05 +00:00
|
|
|
return FALSE;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkImageType
|
|
|
|
_gtk_icon_helper_get_storage_type (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return gtk_image_definition_get_storage_type (self->def);
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gtk_icon_helper_get_use_fallback (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return self->use_fallback;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
_gtk_icon_helper_get_pixel_size (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return self->pixel_size;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 01:25:17 +00:00
|
|
|
GtkImageDefinition *
|
|
|
|
gtk_icon_helper_get_definition (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return self->def;
|
2015-12-01 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 03:26:19 +00:00
|
|
|
GIcon *
|
|
|
|
_gtk_icon_helper_peek_gicon (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return gtk_image_definition_get_gicon (self->def);
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 18:05:52 +00:00
|
|
|
GdkTexture *
|
|
|
|
_gtk_icon_helper_peek_texture (GtkIconHelper *self)
|
|
|
|
{
|
|
|
|
return gtk_image_definition_get_texture (self->def);
|
|
|
|
}
|
|
|
|
|
2018-02-16 09:50:49 +00:00
|
|
|
GdkPaintable *
|
|
|
|
_gtk_icon_helper_peek_paintable (GtkIconHelper *self)
|
|
|
|
{
|
|
|
|
return gtk_image_definition_get_paintable (self->def);
|
|
|
|
}
|
|
|
|
|
2011-11-30 03:26:19 +00:00
|
|
|
const gchar *
|
|
|
|
_gtk_icon_helper_get_icon_name (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return gtk_image_definition_get_icon_name (self->def);
|
2015-12-14 03:00:31 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 03:26:19 +00:00
|
|
|
gboolean
|
|
|
|
_gtk_icon_helper_get_is_empty (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return gtk_image_definition_get_storage_type (self->def) == GTK_IMAGE_EMPTY;
|
2011-11-30 03:26:19 +00:00
|
|
|
}
|
2012-07-13 13:54:29 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
_gtk_icon_helper_get_force_scale_pixbuf (GtkIconHelper *self)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
return self->force_scale_pixbuf;
|
2012-07-13 13:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gtk_icon_helper_set_force_scale_pixbuf (GtkIconHelper *self,
|
|
|
|
gboolean force_scale)
|
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
if (self->force_scale_pixbuf != force_scale)
|
2012-07-13 13:54:29 +00:00
|
|
|
{
|
2017-09-30 15:26:42 +00:00
|
|
|
self->force_scale_pixbuf = force_scale;
|
2015-12-11 14:12:19 +00:00
|
|
|
gtk_icon_helper_invalidate (self);
|
2012-07-13 13:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 03:29:31 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gtk_icon_size_set_style_classes (GtkCssNode *cssnode,
|
|
|
|
GtkIconSize icon_size)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
GtkIconSize icon_size;
|
|
|
|
const char *class_name;
|
|
|
|
} class_names[] = {
|
|
|
|
{ GTK_ICON_SIZE_NORMAL, "normal-icons" },
|
|
|
|
{ GTK_ICON_SIZE_LARGE, "large-icons" }
|
|
|
|
};
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (class_names); i++)
|
|
|
|
{
|
|
|
|
if (icon_size == class_names[i].icon_size)
|
2017-11-18 04:21:24 +00:00
|
|
|
gtk_css_node_add_class (cssnode, g_quark_from_static_string (class_names[i].class_name));
|
2017-11-15 03:29:31 +00:00
|
|
|
else
|
2017-11-18 04:21:24 +00:00
|
|
|
gtk_css_node_remove_class (cssnode, g_quark_from_static_string (class_names[i].class_name));
|
2017-11-15 03:29:31 +00:00
|
|
|
}
|
|
|
|
}
|