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;
39 GArray *elems; /* First element contains the described widget */
43 gtk_widget_path_new (void)
47 path = g_slice_new0 (GtkWidgetPath);
48 path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
54 gtk_widget_path_copy (const GtkWidgetPath *path)
56 GtkWidgetPath *new_path;
59 g_return_val_if_fail (path != NULL, NULL);
61 new_path = gtk_widget_path_new ();
63 for (i = 0; i < path->elems->len; i++)
65 GtkPathElement *elem, new = { 0 };
67 elem = &g_array_index (path->elems, GtkPathElement, i);
69 new.type = elem->type;
70 new.name = elem->name;
77 g_hash_table_iter_init (&iter, elem->regions);
78 new.regions = g_hash_table_new (NULL, NULL);
80 while (g_hash_table_iter_next (&iter, &key, &value))
81 g_hash_table_insert (new.regions, key, value);
84 g_array_append_val (new_path->elems, new);
91 gtk_widget_path_free (GtkWidgetPath *path)
95 g_return_if_fail (path != NULL);
97 for (i = 0; i < path->elems->len; i++)
101 elem = &g_array_index (path->elems, GtkPathElement, i);
104 g_hash_table_destroy (elem->regions);
107 g_array_free (elem->classes, TRUE);
110 g_array_free (path->elems, TRUE);
111 g_slice_free (GtkWidgetPath, path);
115 gtk_widget_path_length (const GtkWidgetPath *path)
117 g_return_val_if_fail (path != NULL, 0);
119 return path->elems->len;
123 gtk_widget_path_prepend_type (GtkWidgetPath *path,
126 GtkPathElement new = { 0 };
128 g_return_val_if_fail (path != NULL, 0);
129 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 0);
132 g_array_append_val (path->elems, new);
134 return path->elems->len - 1;
138 gtk_widget_path_iter_get_widget_type (const GtkWidgetPath *path,
141 GtkPathElement *elem;
143 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
144 g_return_val_if_fail (pos < path->elems->len, G_TYPE_INVALID);
146 elem = &g_array_index (path->elems, GtkPathElement, pos);
151 gtk_widget_path_iter_set_widget_type (GtkWidgetPath *path,
155 GtkPathElement *elem;
157 g_return_if_fail (path != NULL);
158 g_return_if_fail (pos < path->elems->len);
159 g_return_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET));
161 elem = &g_array_index (path->elems, GtkPathElement, pos);
165 G_CONST_RETURN gchar *
166 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
169 GtkPathElement *elem;
171 g_return_val_if_fail (path != NULL, NULL);
172 g_return_val_if_fail (pos < path->elems->len, NULL);
174 elem = &g_array_index (path->elems, GtkPathElement, pos);
175 return g_quark_to_string (elem->name);
179 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
183 GtkPathElement *elem;
185 g_return_if_fail (path != NULL);
186 g_return_if_fail (pos < path->elems->len);
187 g_return_if_fail (name != NULL);
189 elem = &g_array_index (path->elems, GtkPathElement, pos);
191 elem->name = g_quark_from_string (name);
195 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
199 GtkPathElement *elem;
201 g_return_val_if_fail (path != NULL, FALSE);
202 g_return_val_if_fail (qname != 0, FALSE);
203 g_return_val_if_fail (pos < path->elems->len, FALSE);
205 elem = &g_array_index (path->elems, GtkPathElement, pos);
207 return (elem->name == qname);
211 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
217 g_return_val_if_fail (path != NULL, FALSE);
218 g_return_val_if_fail (name != NULL, FALSE);
219 g_return_val_if_fail (pos < path->elems->len, FALSE);
221 qname = g_quark_try_string (name);
226 return gtk_widget_path_iter_has_qname (path, pos, qname);
230 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
234 GtkPathElement *elem;
235 gboolean added = FALSE;
239 g_return_if_fail (path != NULL);
240 g_return_if_fail (pos < path->elems->len);
241 g_return_if_fail (name != NULL);
243 elem = &g_array_index (path->elems, GtkPathElement, pos);
244 qname = g_quark_from_string (name);
247 elem->classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
249 for (i = 0; i < elem->classes->len; i++)
253 quark = g_array_index (elem->classes, GQuark, i);
263 g_array_insert_val (elem->classes, i, qname);
270 g_array_append_val (elem->classes, qname);
274 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
278 GtkPathElement *elem;
282 g_return_if_fail (path != NULL);
283 g_return_if_fail (pos < path->elems->len);
284 g_return_if_fail (name != NULL);
286 qname = g_quark_try_string (name);
291 elem = &g_array_index (path->elems, GtkPathElement, pos);
296 for (i = 0; i < elem->classes->len; i++)
300 quark = g_array_index (elem->classes, GQuark, i);
304 else if (quark == qname)
306 g_array_remove_index (elem->classes, i);
313 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
316 GtkPathElement *elem;
318 g_return_if_fail (path != NULL);
319 g_return_if_fail (pos < path->elems->len);
321 elem = &g_array_index (path->elems, GtkPathElement, pos);
326 if (elem->classes->len > 0)
327 g_array_remove_range (elem->classes, 0, elem->classes->len);
331 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
334 GtkPathElement *elem;
338 g_return_val_if_fail (path != NULL, NULL);
339 g_return_val_if_fail (pos < path->elems->len, NULL);
341 elem = &g_array_index (path->elems, GtkPathElement, pos);
346 for (i = 0; i < elem->classes->len; i++)
350 quark = g_array_index (elem->classes, GQuark, i);
351 list = g_slist_prepend (list, (gchar *) g_quark_to_string (quark));
354 return g_slist_reverse (list);
358 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
362 GtkPathElement *elem;
365 g_return_val_if_fail (path != NULL, FALSE);
366 g_return_val_if_fail (pos < path->elems->len, FALSE);
367 g_return_val_if_fail (qname != 0, FALSE);
369 elem = &g_array_index (path->elems, GtkPathElement, pos);
374 for (i = 0; i < elem->classes->len; i++)
378 quark = g_array_index (elem->classes, GQuark, i);
382 else if (quark > qname)
390 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
396 g_return_val_if_fail (path != NULL, FALSE);
397 g_return_val_if_fail (pos < path->elems->len, FALSE);
398 g_return_val_if_fail (name != NULL, FALSE);
400 qname = g_quark_try_string (name);
405 return gtk_widget_path_iter_has_qclass (path, pos, qname);
409 gtk_widget_path_iter_add_region (GtkWidgetPath *path,
412 GtkRegionFlags flags)
414 GtkPathElement *elem;
417 g_return_if_fail (path != NULL);
418 g_return_if_fail (pos < path->elems->len);
419 g_return_if_fail (name != NULL);
421 elem = &g_array_index (path->elems, GtkPathElement, pos);
422 qname = g_quark_from_string (name);
425 elem->regions = g_hash_table_new (NULL, NULL);
427 g_hash_table_insert (elem->regions,
428 GUINT_TO_POINTER (qname),
429 GUINT_TO_POINTER (flags));
433 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
437 GtkPathElement *elem;
440 g_return_if_fail (path != NULL);
441 g_return_if_fail (pos < path->elems->len);
442 g_return_if_fail (name != NULL);
444 qname = g_quark_try_string (name);
449 elem = &g_array_index (path->elems, GtkPathElement, pos);
452 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
456 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
459 GtkPathElement *elem;
461 g_return_if_fail (path != NULL);
462 g_return_if_fail (pos < path->elems->len);
464 elem = &g_array_index (path->elems, GtkPathElement, pos);
467 g_hash_table_remove_all (elem->regions);
471 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
474 GtkPathElement *elem;
479 g_return_val_if_fail (path != NULL, NULL);
480 g_return_val_if_fail (pos < path->elems->len, NULL);
482 elem = &g_array_index (path->elems, GtkPathElement, pos);
487 g_hash_table_iter_init (&iter, elem->regions);
489 while (g_hash_table_iter_next (&iter, &key, NULL))
493 qname = GPOINTER_TO_UINT (key);
494 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
501 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
504 GtkRegionFlags *flags)
506 GtkPathElement *elem;
509 g_return_val_if_fail (path != NULL, FALSE);
510 g_return_val_if_fail (pos < path->elems->len, FALSE);
511 g_return_val_if_fail (qname != 0, FALSE);
513 elem = &g_array_index (path->elems, GtkPathElement, pos);
518 if (!g_hash_table_lookup_extended (elem->regions,
519 GUINT_TO_POINTER (qname),
524 *flags = GPOINTER_TO_UINT (value);
530 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
533 GtkRegionFlags *flags)
537 g_return_val_if_fail (path != NULL, FALSE);
538 g_return_val_if_fail (pos < path->elems->len, FALSE);
539 g_return_val_if_fail (name != NULL, FALSE);
541 qname = g_quark_try_string (name);
546 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
550 gtk_widget_path_get_widget_type (const GtkWidgetPath *path)
552 GtkPathElement *elem;
554 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
556 elem = &g_array_index (path->elems, GtkPathElement, 0);
561 gtk_widget_path_is_type (const GtkWidgetPath *path,
564 GtkPathElement *elem;
566 g_return_val_if_fail (path != NULL, FALSE);
567 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
569 elem = &g_array_index (path->elems, GtkPathElement, 0);
571 if (elem->type == type ||
572 g_type_is_a (elem->type, type))
579 gtk_widget_path_has_parent (const GtkWidgetPath *path,
584 g_return_val_if_fail (path != NULL, FALSE);
585 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
587 for (i = 1; i < path->elems->len; i++)
589 GtkPathElement *elem;
591 elem = &g_array_index (path->elems, GtkPathElement, i);
593 if (elem->type == type ||
594 g_type_is_a (elem->type, type))