Codebase list xapp / 1432186
Add a class to programmatically set styling on a particular widget. Usage ----- c: ``` GtkWidget *widget = gtk_label_new ("blah"); XAppStyleManager *style_manager = xapp_style_manager_new (); xapp_style_manager_set_widget (style_manager, widget); xapp_style_manager_set_style_property (style_manager, "color", "red"); ``` Python: ``` widget = Gtk.Label(label="blah") style_manager = XApp.StyleManager(widget=widget) style_manager.set_style_property("color", "red") ``` Stephen Collins authored 3 years ago Michael Webster committed 3 years ago
3 changed file(s) with 282 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
3232 'xapp-stack-sidebar.h',
3333 'xapp-status-icon.h',
3434 'xapp-status-icon-monitor.h',
35 'xapp-style-manager.h',
3536 'xapp-util.h'
3637 ]
3738
4647 'xapp-stack-sidebar.c',
4748 'xapp-status-icon.c',
4849 'xapp-status-icon-monitor.c',
50 'xapp-style-manager.c',
4951 'xapp-util.c'
5052 ]
5153
0 #include "xapp-style-manager.h"
1
2 /**
3 * SECTION:xapp-style-manager
4 * @Short_description: Convenience class for adding on the fly styling to gtk widgets.
5 * @Title: XAppStyleManager
6 *
7 * #XAppStyleManager is a convenience class designed to make it easier to programmatically add style information to a widget.
8 */
9 struct _XAppStyleManager
10 {
11 GObject parent_instance;
12
13 GHashTable *properties;
14 GtkWidget *widget;
15 GtkCssProvider *provider;
16 gchar *class_name;
17 };
18
19 G_DEFINE_TYPE (XAppStyleManager, xapp_style_manager, G_TYPE_OBJECT)
20
21 enum
22 {
23 PROP_0,
24 PROP_WIDGET,
25 N_PROPERTIES
26 };
27
28 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
29
30 static gint next_id = 0;
31
32 static void
33 process_property_list_update (XAppStyleManager *style_manager)
34 {
35 GString *css_text = g_string_new ("");
36 GHashTableIter iter;
37 gpointer key, value;
38 gchar *raw_text;
39
40 g_string_append (css_text, ".");
41 g_string_append (css_text, style_manager->class_name);
42 g_string_append (css_text, "{");
43
44 g_hash_table_iter_init (&iter, style_manager->properties);
45 while (g_hash_table_iter_next (&iter, &key, &value))
46 {
47 g_string_append (css_text, (const gchar*) key);
48 g_string_append (css_text, ":");
49 g_string_append (css_text, (const gchar*) value);
50 g_string_append (css_text, ";");
51 }
52 g_string_append (css_text, "}");
53
54 raw_text = g_string_free (css_text, FALSE);
55 gtk_css_provider_load_from_data (style_manager->provider, raw_text, -1, NULL);
56 g_free (raw_text);
57 }
58
59 static void
60 xapp_style_manager_set_property (GObject *object,
61 guint prop_id,
62 const GValue *value,
63 GParamSpec *pspec)
64 {
65 XAppStyleManager *style_manager = XAPP_STYLE_MANAGER (object);
66
67 switch (prop_id)
68 {
69 case PROP_WIDGET:
70 xapp_style_manager_set_widget (style_manager, g_value_get_object (value));
71 break;
72 default:
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74 break;
75 }
76 }
77
78 static void
79 xapp_style_manager_get_property (GObject *object,
80 guint prop_id,
81 GValue *value,
82 GParamSpec *pspec)
83 {
84 XAppStyleManager *style_manager = XAPP_STYLE_MANAGER (object);
85
86 switch (prop_id)
87 {
88 case PROP_WIDGET:
89 g_value_set_object (value, xapp_style_manager_get_widget (style_manager));
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 break;
94 }
95 }
96
97 static void
98 xapp_style_manager_init (XAppStyleManager *style_manager)
99 {
100 style_manager->provider = gtk_css_provider_new ();
101 style_manager->class_name = g_strdup_printf ("xapp-%d", next_id);
102 next_id++;
103
104 style_manager->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
105 style_manager->widget = NULL;
106 }
107
108 static void
109 xapp_style_manager_dispose (GObject *object)
110 {
111 XAppStyleManager *style_manager = XAPP_STYLE_MANAGER (object);
112
113 xapp_style_manager_set_widget (style_manager, NULL);
114 g_hash_table_unref (style_manager->properties);
115 g_object_unref (style_manager->provider);
116 g_free (style_manager->class_name);
117
118 G_OBJECT_CLASS (xapp_style_manager_parent_class)->dispose (object);
119 }
120
121 static void
122 xapp_style_manager_class_init (XAppStyleManagerClass *klass)
123 {
124 GObjectClass *object_class = G_OBJECT_CLASS (klass);
125
126 object_class->dispose = xapp_style_manager_dispose;
127 object_class->set_property = xapp_style_manager_set_property;
128 object_class->get_property = xapp_style_manager_get_property;
129
130 /**
131 * XAppStyleManager:widget:
132 *
133 * The widget to be styled.
134 */
135 obj_properties[PROP_WIDGET] =
136 g_param_spec_object ("widget",
137 "Widget",
138 "The widget to be styled.",
139 GTK_TYPE_WIDGET,
140 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
141
142 g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
143 }
144
145 /**
146 * xapp_style_manager_new:
147 *
148 * Creates a new #XAppStyleManager instance
149 *
150 * Returns: (transfer full): a new #XAppStyleManager. Use g_object_unref when finished.
151 *
152 * Since: 2.2
153 */
154 XAppStyleManager *
155 xapp_style_manager_new (void)
156 {
157 XAppStyleManager *style_manager;
158 style_manager = g_object_new (XAPP_TYPE_STYLE_MANAGER, NULL);
159
160 return style_manager;
161 }
162
163 /**
164 * xapp_style_manager_get_widget:
165 * @style_manager: a #XAppStyleManager
166 *
167 * Gets the #GtkWidget the style manager currently applies styles to.
168 *
169 * Returns: (transfer none): the #GtkWidget previously set on the style manager, or %NULL.
170 *
171 * Since: 2.2
172 */
173 GtkWidget *
174 xapp_style_manager_get_widget (XAppStyleManager *style_manager)
175 {
176 return style_manager->widget;
177 }
178
179 /**
180 * xapp_style_manager_set_widget:
181 * @style_manager: a #XAppStyleManager
182 * @widget: the #GtkWidget that the style manager will apply styles to, or
183 * %NULL to unset the current widget and remove all styles currently set by
184 * this #XAppStyleManager instance.
185 *
186 * Sets the #GtkWidget the style manager will apply styles to.
187 *
188 * Since: 2.2
189 */
190 void
191 xapp_style_manager_set_widget (XAppStyleManager *style_manager,
192 GtkWidget *widget)
193 {
194 GtkStyleContext *context;
195
196 if (style_manager->widget)
197 {
198 context = gtk_widget_get_style_context (style_manager->widget);
199 gtk_style_context_remove_provider (context, GTK_STYLE_PROVIDER (style_manager->provider));
200 gtk_style_context_remove_class (context, style_manager->class_name);
201 }
202
203 if (!widget)
204 {
205 style_manager->widget = NULL;
206 return;
207 }
208
209 style_manager->widget = widget;
210 context = gtk_widget_get_style_context (widget);
211
212 gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (style_manager->provider), 700);
213 gtk_style_context_add_class (context, style_manager->class_name);
214 }
215
216 /**
217 * xapp_style_manager_set_style_property:
218 * @style_manager: a #XAppStyleManager
219 * @name: the property name
220 * @value: the value to set the property to
221 *
222 * Adds the given style property to the widget. If the property has already been set, the value will be replaced.
223 *
224 * Since: 2.2
225 */
226 void
227 xapp_style_manager_set_style_property (XAppStyleManager *style_manager,
228 const gchar *name,
229 const gchar *value)
230 {
231 g_hash_table_insert (style_manager->properties, g_strdup (name), g_strdup (value));
232
233 process_property_list_update (style_manager);
234 }
235
236 /**
237 * xapp_style_manager_remove_style_property:
238 * @style_manager: a #XAppStyleManager
239 * @name: the property name
240 *
241 * Removes the given style property from the widget if set.
242 *
243 * Since: 2.2
244 */
245 void
246 xapp_style_manager_remove_style_property (XAppStyleManager *style_manager,
247 const gchar *name)
248 {
249 if (!g_hash_table_lookup (style_manager->properties, name))
250 return;
251
252 g_hash_table_remove (style_manager->properties, name);
253
254 process_property_list_update (style_manager);
255 }
0 #ifndef __XAPP_STYLE_MANAGER_H__
1 #define __XAPP_STYLE_MANAGER_H__
2
3 #include <gtk/gtk.h>
4
5 G_BEGIN_DECLS
6
7 #define XAPP_TYPE_STYLE_MANAGER xapp_style_manager_get_type ()
8 G_DECLARE_FINAL_TYPE (XAppStyleManager, xapp_style_manager, XAPP, STYLE_MANAGER, GObject)
9
10 XAppStyleManager *xapp_style_manager_new (void);
11
12 GtkWidget *xapp_style_manager_get_widget (XAppStyleManager *style_manager);
13 void xapp_style_manager_set_widget (XAppStyleManager *style_manager,
14 GtkWidget *widget);
15 void xapp_style_manager_set_style_property (XAppStyleManager *style_manager,
16 const gchar *name,
17 const gchar *value);
18 void xapp_style_manager_remove_style_property (XAppStyleManager *style_manager,
19 const gchar *name);
20
21 G_END_DECLS
22
23 #endif /* __XAPP_STYLE_MANAGER_H__ */