1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "gtkwidget.h"
25 #include "gtkwidgetpath.h"
27 typedef struct GtkPathElement GtkPathElement;
38 GArray *elems; /* First element contains the described widget */
42 gtk_widget_path_new (void)
46 path = g_slice_new0 (GtkWidgetPath);
47 path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
53 gtk_widget_path_copy (const GtkWidgetPath *path)
55 GtkWidgetPath *new_path;
58 g_return_val_if_fail (path != NULL, NULL);
60 new_path = gtk_widget_path_new ();
62 for (i = 0; i < path->elems->len; i++)
64 GtkPathElement *elem, new = { 0 };
66 elem = &g_array_index (path->elems, GtkPathElement, i);
68 new.type = elem->type;
69 new.name = elem->name;
76 g_hash_table_iter_init (&iter, elem->regions);
77 new.regions = g_hash_table_new (NULL, NULL);
79 while (g_hash_table_iter_next (&iter, &key, &value))
80 g_hash_table_insert (new.regions, key, value);
83 g_array_append_val (new_path->elems, new);
90 gtk_widget_path_free (GtkWidgetPath *path)
94 g_return_if_fail (path != NULL);
96 for (i = 0; i < path->elems->len; i++)
100 elem = &g_array_index (path->elems, GtkPathElement, i);
103 g_hash_table_destroy (elem->regions);
106 g_array_free (path->elems, TRUE);
107 g_slice_free (GtkWidgetPath, path);
111 gtk_widget_path_length (const GtkWidgetPath *path)
113 g_return_val_if_fail (path != NULL, 0);
115 return path->elems->len;
119 gtk_widget_path_prepend_type (GtkWidgetPath *path,
122 GtkPathElement new = { 0 };
124 g_return_val_if_fail (path != NULL, 0);
125 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 0);
128 g_array_append_val (path->elems, new);
130 return path->elems->len - 1;
134 gtk_widget_path_iter_get_widget_type (const GtkWidgetPath *path,
137 GtkPathElement *elem;
139 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
140 g_return_val_if_fail (pos < path->elems->len, G_TYPE_INVALID);
142 elem = &g_array_index (path->elems, GtkPathElement, pos);
147 gtk_widget_path_iter_set_widget_type (GtkWidgetPath *path,
151 GtkPathElement *elem;
153 g_return_if_fail (path != NULL);
154 g_return_if_fail (pos < path->elems->len);
155 g_return_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET));
157 elem = &g_array_index (path->elems, GtkPathElement, pos);
161 G_CONST_RETURN gchar *
162 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
165 GtkPathElement *elem;
167 g_return_val_if_fail (path != NULL, NULL);
168 g_return_val_if_fail (pos < path->elems->len, NULL);
170 elem = &g_array_index (path->elems, GtkPathElement, pos);
171 return g_quark_to_string (elem->name);
175 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
179 GtkPathElement *elem;
181 g_return_if_fail (path != NULL);
182 g_return_if_fail (pos < path->elems->len);
183 g_return_if_fail (name != NULL);
185 elem = &g_array_index (path->elems, GtkPathElement, pos);
187 elem->name = g_quark_from_string (name);
191 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
195 GtkPathElement *elem;
197 g_return_val_if_fail (path != NULL, FALSE);
198 g_return_val_if_fail (qname != 0, FALSE);
199 g_return_val_if_fail (pos < path->elems->len, FALSE);
201 elem = &g_array_index (path->elems, GtkPathElement, pos);
203 return (elem->name == qname);
207 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
213 g_return_val_if_fail (path != NULL, FALSE);
214 g_return_val_if_fail (name != NULL, FALSE);
215 g_return_val_if_fail (pos < path->elems->len, FALSE);
217 qname = g_quark_try_string (name);
222 return gtk_widget_path_iter_has_qname (path, pos, qname);
226 gtk_widget_path_iter_add_region (GtkWidgetPath *path,
229 GtkRegionFlags flags)
231 GtkPathElement *elem;
234 g_return_if_fail (path != NULL);
235 g_return_if_fail (pos < path->elems->len);
236 g_return_if_fail (name != NULL);
238 elem = &g_array_index (path->elems, GtkPathElement, pos);
239 qname = g_quark_from_string (name);
242 elem->regions = g_hash_table_new (NULL, NULL);
244 g_hash_table_insert (elem->regions,
245 GUINT_TO_POINTER (qname),
246 GUINT_TO_POINTER (flags));
250 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
254 GtkPathElement *elem;
257 g_return_if_fail (path != NULL);
258 g_return_if_fail (pos < path->elems->len);
259 g_return_if_fail (name != NULL);
261 qname = g_quark_try_string (name);
266 elem = &g_array_index (path->elems, GtkPathElement, pos);
269 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
273 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
276 GtkPathElement *elem;
278 g_return_if_fail (path != NULL);
279 g_return_if_fail (pos < path->elems->len);
281 elem = &g_array_index (path->elems, GtkPathElement, pos);
284 g_hash_table_remove_all (elem->regions);
288 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
291 GtkPathElement *elem;
296 g_return_val_if_fail (path != NULL, NULL);
297 g_return_val_if_fail (pos < path->elems->len, NULL);
299 elem = &g_array_index (path->elems, GtkPathElement, pos);
304 g_hash_table_iter_init (&iter, elem->regions);
306 while (g_hash_table_iter_next (&iter, &key, NULL))
310 qname = GPOINTER_TO_UINT (key);
311 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
318 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
321 GtkRegionFlags *flags)
323 GtkPathElement *elem;
326 g_return_val_if_fail (path != NULL, FALSE);
327 g_return_val_if_fail (pos < path->elems->len, FALSE);
328 g_return_val_if_fail (qname != 0, FALSE);
330 elem = &g_array_index (path->elems, GtkPathElement, pos);
335 if (!g_hash_table_lookup_extended (elem->regions,
336 GUINT_TO_POINTER (qname),
341 *flags = GPOINTER_TO_UINT (value);
347 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
350 GtkRegionFlags *flags)
354 g_return_val_if_fail (path != NULL, FALSE);
355 g_return_val_if_fail (pos < path->elems->len, FALSE);
356 g_return_val_if_fail (name != NULL, FALSE);
358 qname = g_quark_try_string (name);
363 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
367 gtk_widget_path_get_widget_type (const GtkWidgetPath *path)
369 GtkPathElement *elem;
371 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
373 elem = &g_array_index (path->elems, GtkPathElement, 0);
378 gtk_widget_path_is_type (const GtkWidgetPath *path,
381 GtkPathElement *elem;
383 g_return_val_if_fail (path != NULL, FALSE);
384 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
386 elem = &g_array_index (path->elems, GtkPathElement, 0);
388 if (elem->type == type ||
389 g_type_is_a (elem->type, type))
396 gtk_widget_path_has_parent (const GtkWidgetPath *path,
401 g_return_val_if_fail (path != NULL, FALSE);
402 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
404 for (i = 1; i < path->elems->len; i++)
406 GtkPathElement *elem;
408 elem = &g_array_index (path->elems, GtkPathElement, i);
410 if (elem->type == type ||
411 g_type_is_a (elem->type, type))
418 #define __GTK_WIDGET_PATH_C__
419 #include "gtkaliasdef.c"