1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2014 Benjamin Otte <otte@gnome.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gtkcsspathnodeprivate.h"
21 #include "gtkcssstylepropertyprivate.h"
22 #include "gtkprivate.h"
23 #include "gtkstylecontextprivate.h"
24
G_DEFINE_TYPE(GtkCssPathNode,gtk_css_path_node,GTK_TYPE_CSS_NODE)25 G_DEFINE_TYPE (GtkCssPathNode, gtk_css_path_node, GTK_TYPE_CSS_NODE)
26
27 static void
28 gtk_css_path_node_finalize (GObject *object)
29 {
30 GtkCssPathNode *node = GTK_CSS_PATH_NODE (object);
31
32 if (node->path)
33 gtk_widget_path_unref (node->path);
34
35 G_OBJECT_CLASS (gtk_css_path_node_parent_class)->finalize (object);
36 }
37
38 static void
gtk_css_path_node_invalidate(GtkCssNode * node)39 gtk_css_path_node_invalidate (GtkCssNode *node)
40 {
41 GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
42
43 if (path_node->context)
44 gtk_style_context_validate (path_node->context, NULL);
45 }
46
47 gboolean
gtk_css_path_node_real_init_matcher(GtkCssNode * node,GtkCssMatcher * matcher)48 gtk_css_path_node_real_init_matcher (GtkCssNode *node,
49 GtkCssMatcher *matcher)
50 {
51 GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
52
53 if (path_node->path == NULL ||
54 gtk_widget_path_length (path_node->path) == 0)
55 return FALSE;
56
57 return _gtk_css_matcher_init (matcher,
58 path_node->path,
59 gtk_css_node_get_declaration (node));
60 }
61
62 static GtkWidgetPath *
gtk_css_path_node_real_create_widget_path(GtkCssNode * node)63 gtk_css_path_node_real_create_widget_path (GtkCssNode *node)
64 {
65 GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
66 GtkWidgetPath *path;
67 guint length;
68
69 if (path_node->path == NULL)
70 path = gtk_widget_path_new ();
71 else
72 path = gtk_widget_path_copy (path_node->path);
73
74 length = gtk_widget_path_length (path);
75 if (length > 0)
76 {
77 gtk_css_node_declaration_add_to_widget_path (gtk_css_node_get_declaration (node),
78 path,
79 length - 1);
80 }
81
82 return path;
83 }
84
85 static const GtkWidgetPath *
gtk_css_path_node_real_get_widget_path(GtkCssNode * node)86 gtk_css_path_node_real_get_widget_path (GtkCssNode *node)
87 {
88 GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
89
90 return path_node->path;
91 }
92
93 static GtkCssStyle *
gtk_css_path_node_update_style(GtkCssNode * cssnode,GtkCssChange change,gint64 timestamp,GtkCssStyle * style)94 gtk_css_path_node_update_style (GtkCssNode *cssnode,
95 GtkCssChange change,
96 gint64 timestamp,
97 GtkCssStyle *style)
98 {
99 /* This should get rid of animations */
100 return GTK_CSS_NODE_CLASS (gtk_css_path_node_parent_class)->update_style (cssnode, change, 0, style);
101 }
102
103 static GtkStyleProviderPrivate *
gtk_css_path_node_get_style_provider(GtkCssNode * node)104 gtk_css_path_node_get_style_provider (GtkCssNode *node)
105 {
106 GtkCssPathNode *path_node = GTK_CSS_PATH_NODE (node);
107
108 if (path_node->context == NULL)
109 return NULL;
110
111 return gtk_style_context_get_style_provider (path_node->context);
112 }
113
114 static void
gtk_css_path_node_class_init(GtkCssPathNodeClass * klass)115 gtk_css_path_node_class_init (GtkCssPathNodeClass *klass)
116 {
117 GtkCssNodeClass *node_class = GTK_CSS_NODE_CLASS (klass);
118 GObjectClass *object_class = G_OBJECT_CLASS (klass);
119
120 object_class->finalize = gtk_css_path_node_finalize;
121
122 node_class->invalidate = gtk_css_path_node_invalidate;
123 node_class->update_style = gtk_css_path_node_update_style;
124 node_class->init_matcher = gtk_css_path_node_real_init_matcher;
125 node_class->create_widget_path = gtk_css_path_node_real_create_widget_path;
126 node_class->get_widget_path = gtk_css_path_node_real_get_widget_path;
127 node_class->get_style_provider = gtk_css_path_node_get_style_provider;
128 }
129
130 static void
gtk_css_path_node_init(GtkCssPathNode * cssnode)131 gtk_css_path_node_init (GtkCssPathNode *cssnode)
132 {
133 }
134
135 GtkCssNode *
gtk_css_path_node_new(GtkStyleContext * context)136 gtk_css_path_node_new (GtkStyleContext *context)
137 {
138 GtkCssPathNode *node;
139
140 g_return_val_if_fail (context == NULL || GTK_IS_STYLE_CONTEXT (context), NULL);
141
142 node = g_object_new (GTK_TYPE_CSS_PATH_NODE, NULL);
143 node->context = context;
144
145 return GTK_CSS_NODE (node);
146 }
147
148 void
gtk_css_path_node_unset_context(GtkCssPathNode * node)149 gtk_css_path_node_unset_context (GtkCssPathNode *node)
150 {
151 gtk_internal_return_if_fail (GTK_IS_CSS_PATH_NODE (node));
152 gtk_internal_return_if_fail (node->context != NULL);
153
154 node->context = NULL;
155
156 gtk_css_node_invalidate_style_provider (GTK_CSS_NODE (node));
157 }
158
159 void
gtk_css_path_node_set_widget_path(GtkCssPathNode * node,GtkWidgetPath * path)160 gtk_css_path_node_set_widget_path (GtkCssPathNode *node,
161 GtkWidgetPath *path)
162 {
163 gtk_internal_return_if_fail (GTK_IS_CSS_PATH_NODE (node));
164
165 if (node->path == path)
166 return;
167
168 if (node->path)
169 gtk_widget_path_unref (node->path);
170
171 if (path)
172 gtk_widget_path_ref (path);
173
174 node->path = path;
175
176 gtk_css_node_invalidate (GTK_CSS_NODE (node), GTK_CSS_CHANGE_ANY);
177 }
178
179 GtkWidgetPath *
gtk_css_path_node_get_widget_path(GtkCssPathNode * node)180 gtk_css_path_node_get_widget_path (GtkCssPathNode *node)
181 {
182 gtk_internal_return_val_if_fail (GTK_IS_CSS_PATH_NODE (node), NULL);
183
184 return node->path;
185 }
186
187