1
2/*
3 * Copyright © 2015 Endless Mobile, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authors: Cosimo Cecchi <cosimoc@gnome.org>
19 */
20
21#include "config.h"
22
23#include "gtkcssnodeprivate.h"
24#include "gtkcssnumbervalueprivate.h"
25#include "gtkbuiltiniconprivate.h"
26#include "gtkwidgetprivate.h"
27#include "gtkrendericonprivate.h"
28#include "gtksnapshot.h"
29
30/* GtkBuiltinIcon was a minimal widget wrapped around a GtkBuiltinIcon gadget,
31 * It should be used whenever builtin-icon functionality is desired
32 * but a widget is needed for other reasons.
33 */
34
35struct _GtkBuiltinIcon
36{
37 GtkWidget parent;
38};
39
40G_DEFINE_TYPE (GtkBuiltinIcon, gtk_builtin_icon, GTK_TYPE_WIDGET)
41
42static void
43gtk_builtin_icon_snapshot (GtkWidget *widget,
44 GtkSnapshot *snapshot)
45{
46 GtkCssStyle *style = gtk_css_node_get_style (cssnode: gtk_widget_get_css_node (widget));
47 int width, height;
48
49 width = gtk_widget_get_width (widget);
50 height = gtk_widget_get_height (widget);
51
52 if (width > 0 && height > 0)
53 gtk_css_style_snapshot_icon (style, snapshot, width, height);
54}
55
56static void
57gtk_builtin_icon_css_changed (GtkWidget *widget,
58 GtkCssStyleChange *change)
59{
60 GTK_WIDGET_CLASS (gtk_builtin_icon_parent_class)->css_changed (widget, change);
61
62 if (change == NULL ||
63 gtk_css_style_change_affects (change, affects: GTK_CSS_AFFECTS_ICON_SIZE))
64 {
65 gtk_widget_queue_resize (widget);
66 }
67 else if (gtk_css_style_change_affects (change, affects: GTK_CSS_AFFECTS_ICON_TEXTURE |
68 GTK_CSS_AFFECTS_ICON_REDRAW))
69 {
70 gtk_widget_queue_draw (widget);
71 }
72}
73
74static void
75gtk_builtin_icon_measure (GtkWidget *widget,
76 GtkOrientation orientation,
77 int for_size,
78 int *minimum,
79 int *natural,
80 int *minimum_baseline,
81 int *natural_baseline)
82{
83 GtkCssStyle *style;
84
85 style = gtk_css_node_get_style (cssnode: gtk_widget_get_css_node (widget));
86
87 *minimum = *natural = _gtk_css_number_value_get (number: style->icon->icon_size, one_hundred_percent: 100);
88}
89
90static void
91gtk_builtin_icon_class_init (GtkBuiltinIconClass *klass)
92{
93 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
94
95 wclass->snapshot = gtk_builtin_icon_snapshot;
96 wclass->measure = gtk_builtin_icon_measure;
97 wclass->css_changed = gtk_builtin_icon_css_changed;
98}
99
100static void
101gtk_builtin_icon_init (GtkBuiltinIcon *self)
102{
103}
104
105GtkWidget *
106gtk_builtin_icon_new (const char *css_name)
107{
108 return g_object_new (GTK_TYPE_BUILTIN_ICON,
109 first_property_name: "css-name", css_name,
110 NULL);
111}
112
113void
114gtk_builtin_icon_set_css_name (GtkBuiltinIcon *self,
115 const char *css_name)
116{
117 gtk_css_node_set_name (cssnode: gtk_widget_get_css_node (GTK_WIDGET (self)),
118 name: g_quark_from_string (string: css_name));
119}
120

source code of gtk/gtk/gtkbuiltinicon.c