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"
26 #include "gtkstylecontextprivate.h"
29 * SECTION:gtkwidgetpath
30 * @Short_description: Widget path abstraction
31 * @Title: GtkWidgetPath
32 * @See_also: #GtkStyleContext
34 * GtkWidgetPath is a boxed type that represents a widget hierarchy from
35 * the topmost widget, typically a toplevel, to any child. This widget
36 * path abstraction is used in #GtkStyleContext on behalf of the real
37 * widget in order to query style information.
39 * If you are using GTK+ widgets, you probably will not need to use
40 * this API directly, as there is gtk_widget_get_path(), and the style
41 * context returned by gtk_widget_get_style_context() will be automatically
42 * updated on widget hierarchy changes.
44 * The widget path generation is generally simple:
46 * <title>Defining a button within a window</title>
49 * GtkWidgetPath *path;
51 * path = gtk_widget_path_new ();
52 * gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
53 * gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
58 * Although more complex information, such as widget names, or
59 * different classes (property that may be used by other widget
60 * types) and intermediate regions may be included:
63 * <title>Defining the first tab widget in a notebook</title>
66 * GtkWidgetPath *path;
69 * path = gtk_widget_path_new ();
71 * pos = gtk_widget_path_append_type (path, GTK_TYPE_NOTEBOOK);
72 * gtk_widget_path_iter_add_region (path, pos, "tab", GTK_REGION_EVEN | GTK_REGION_FIRST);
74 * pos = gtk_widget_path_append_type (path, GTK_TYPE_LABEL);
75 * gtk_widget_path_iter_set_name (path, pos, "first tab label");
80 * All this information will be used to match the style information
81 * that applies to the described widget.
84 G_DEFINE_BOXED_TYPE (GtkWidgetPath, gtk_widget_path,
85 gtk_widget_path_copy, gtk_widget_path_free)
88 typedef struct GtkPathElement GtkPathElement;
100 GArray *elems; /* First element contains the described widget */
104 * gtk_widget_path_new:
106 * Returns an empty widget path.
108 * Returns: (transfer full): A newly created, empty, #GtkWidgetPath
113 gtk_widget_path_new (void)
117 path = g_slice_new0 (GtkWidgetPath);
118 path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
124 * gtk_widget_path_copy:
125 * @path: a #GtkWidgetPath
127 * Returns a copy of @path
129 * Returns: (transfer full): a copy of @path
134 gtk_widget_path_copy (const GtkWidgetPath *path)
136 GtkWidgetPath *new_path;
139 g_return_val_if_fail (path != NULL, NULL);
141 new_path = gtk_widget_path_new ();
143 for (i = 0; i < path->elems->len; i++)
145 GtkPathElement *elem, new = { 0 };
147 elem = &g_array_index (path->elems, GtkPathElement, i);
149 new.type = elem->type;
150 new.name = elem->name;
157 g_hash_table_iter_init (&iter, elem->regions);
158 new.regions = g_hash_table_new (NULL, NULL);
160 while (g_hash_table_iter_next (&iter, &key, &value))
161 g_hash_table_insert (new.regions, key, value);
166 new.classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
167 g_array_append_vals (new.classes, elem->classes->data, elem->classes->len);
170 g_array_append_val (new_path->elems, new);
177 * gtk_widget_path_free:
178 * @path: a #GtkWidgetPath
180 * Frees a #GtkWidgetPath.
185 gtk_widget_path_free (GtkWidgetPath *path)
189 g_return_if_fail (path != NULL);
191 for (i = 0; i < path->elems->len; i++)
193 GtkPathElement *elem;
195 elem = &g_array_index (path->elems, GtkPathElement, i);
198 g_hash_table_destroy (elem->regions);
201 g_array_free (elem->classes, TRUE);
204 g_array_free (path->elems, TRUE);
205 g_slice_free (GtkWidgetPath, path);
209 * gtk_widget_path_length:
210 * @path: a #GtkWidgetPath
212 * Returns the number of #GtkWidget #GTypes between the represented
213 * widget and its topmost container.
215 * Returns: the number of elements in the path
220 gtk_widget_path_length (const GtkWidgetPath *path)
222 g_return_val_if_fail (path != NULL, 0);
224 return path->elems->len;
228 * gtk_widget_path_prepend_type:
229 * @path: a #GtkWidgetPath
230 * @type: widget type to prepend
232 * Prepends a widget type to the widget hierachy represented by @path.
237 gtk_widget_path_prepend_type (GtkWidgetPath *path,
240 GtkPathElement new = { 0 };
242 g_return_if_fail (path != NULL);
245 g_array_prepend_val (path->elems, new);
249 * gtk_widget_path_append_type:
250 * @path: a #GtkWidgetPath
251 * @type: widget type to append
253 * Appends a widget type to the widget hierachy represented by @path.
255 * Returns: the position where the element was inserted
260 gtk_widget_path_append_type (GtkWidgetPath *path,
263 GtkPathElement new = { 0 };
265 g_return_val_if_fail (path != NULL, 0);
268 g_array_append_val (path->elems, new);
270 return path->elems->len - 1;
274 * gtk_widget_path_iter_get_object_type:
275 * @path: a #GtkWidgetPath
276 * @pos: position to get the object type for, -1 for the path head
278 * Returns the object #GType that is at position @pos in the widget
279 * hierarchy defined in @path.
281 * Returns: a widget type
286 gtk_widget_path_iter_get_object_type (const GtkWidgetPath *path,
289 GtkPathElement *elem;
291 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
292 g_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
294 if (pos < 0 || pos >= path->elems->len)
295 pos = path->elems->len - 1;
297 elem = &g_array_index (path->elems, GtkPathElement, pos);
302 * gtk_widget_path_iter_set_object_type:
303 * @path: a #GtkWidgetPath
304 * @pos: position to modify, -1 for the path head
305 * @type: object type to set
307 * Sets the object type for a given position in the widget hierarchy
313 gtk_widget_path_iter_set_object_type (GtkWidgetPath *path,
317 GtkPathElement *elem;
319 g_return_if_fail (path != NULL);
320 g_return_if_fail (path->elems->len != 0);
322 if (pos < 0 || pos >= path->elems->len)
323 pos = path->elems->len - 1;
325 elem = &g_array_index (path->elems, GtkPathElement, pos);
330 * gtk_widget_path_iter_get_name:
331 * @path: a #GtkWidgetPath
332 * @pos: position to get the widget name for, -1 for the path head
334 * Returns the name corresponding to the widget found at
335 * the position @pos in the widget hierarchy defined by
338 * Returns: The widget name, or %NULL if none was set.
340 G_CONST_RETURN gchar *
341 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
344 GtkPathElement *elem;
346 g_return_val_if_fail (path != NULL, NULL);
347 g_return_val_if_fail (path->elems->len != 0, NULL);
349 if (pos < 0 || pos >= path->elems->len)
350 pos = path->elems->len - 1;
352 elem = &g_array_index (path->elems, GtkPathElement, pos);
353 return g_quark_to_string (elem->name);
357 * gtk_widget_path_iter_set_name:
358 * @path: a #GtkWidgetPath
359 * @pos: position to modify, -1 for the path head
362 * Sets the widget name for the widget found at position @pos
363 * in the widget hierarchy defined by @path.
368 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
372 GtkPathElement *elem;
374 g_return_if_fail (path != NULL);
375 g_return_if_fail (path->elems->len != 0);
376 g_return_if_fail (name != NULL);
378 if (pos < 0 || pos >= path->elems->len)
379 pos = path->elems->len - 1;
381 elem = &g_array_index (path->elems, GtkPathElement, pos);
383 elem->name = g_quark_from_string (name);
387 * gtk_widget_path_iter_has_qname:
388 * @path: a #GtkWidgetPath
389 * @pos: position to query, -1 for the path head
390 * @qname: widget name as a #GQuark
392 * See gtk_widget_path_iter_has_name(). This is a version
393 * that operates on #GQuark<!-- -->s.
395 * Returns: %TRUE if the widget at @pos has this name
400 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
404 GtkPathElement *elem;
406 g_return_val_if_fail (path != NULL, FALSE);
407 g_return_val_if_fail (path->elems->len != 0, FALSE);
408 g_return_val_if_fail (qname != 0, FALSE);
410 if (pos < 0 || pos >= path->elems->len)
411 pos = path->elems->len - 1;
413 elem = &g_array_index (path->elems, GtkPathElement, pos);
415 return (elem->name == qname);
419 * gtk_widget_path_iter_has_name:
420 * @path: a #GtkWidgetPath
421 * @pos: position to query, -1 for the path head
422 * @name: a widget name
424 * Returns %TRUE if the widget at position @pos has the name @name,
427 * Returns: %TRUE if the widget at @pos has this name
432 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
438 g_return_val_if_fail (path != NULL, FALSE);
439 g_return_val_if_fail (path->elems->len != 0, FALSE);
441 if (pos < 0 || pos >= path->elems->len)
442 pos = path->elems->len - 1;
444 qname = g_quark_try_string (name);
449 return gtk_widget_path_iter_has_qname (path, pos, qname);
453 * gtk_widget_path_iter_add_class:
454 * @path: a #GtkWidget
455 * @pos: position to modify, -1 for the path head
456 * @name: a class name
458 * Adds the class @name to the widget at position @pos in
459 * the hierarchy defined in @path. See
460 * gtk_style_context_add_class().
465 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
469 GtkPathElement *elem;
470 gboolean added = FALSE;
474 g_return_if_fail (path != NULL);
475 g_return_if_fail (path->elems->len != 0);
476 g_return_if_fail (name != NULL);
478 if (pos < 0 || pos >= path->elems->len)
479 pos = path->elems->len - 1;
481 elem = &g_array_index (path->elems, GtkPathElement, pos);
482 qname = g_quark_from_string (name);
485 elem->classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
487 for (i = 0; i < elem->classes->len; i++)
491 quark = g_array_index (elem->classes, GQuark, i);
501 g_array_insert_val (elem->classes, i, qname);
508 g_array_append_val (elem->classes, qname);
512 * gtk_widget_path_iter_remove_class:
513 * @path: a #GtkWidgetPath
514 * @pos: position to modify, -1 for the path head
517 * Removes the class @name from the widget at position @pos in
518 * the hierarchy defined in @path.
523 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
527 GtkPathElement *elem;
531 g_return_if_fail (path != NULL);
532 g_return_if_fail (path->elems->len != 0);
533 g_return_if_fail (name != NULL);
535 if (pos < 0 || pos >= path->elems->len)
536 pos = path->elems->len - 1;
538 qname = g_quark_try_string (name);
543 elem = &g_array_index (path->elems, GtkPathElement, pos);
548 for (i = 0; i < elem->classes->len; i++)
552 quark = g_array_index (elem->classes, GQuark, i);
556 else if (quark == qname)
558 g_array_remove_index (elem->classes, i);
565 * gtk_widget_path_iter_clear_classes:
566 * @path: a #GtkWidget
567 * @pos: position to modify, -1 for the path head
569 * Removes all classes from the widget at position @pos in the
570 * hierarchy defined in @path.
575 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
578 GtkPathElement *elem;
580 g_return_if_fail (path != NULL);
581 g_return_if_fail (path->elems->len != 0);
583 if (pos < 0 || pos >= path->elems->len)
584 pos = path->elems->len - 1;
586 elem = &g_array_index (path->elems, GtkPathElement, pos);
591 if (elem->classes->len > 0)
592 g_array_remove_range (elem->classes, 0, elem->classes->len);
596 * gtk_widget_path_iter_list_classes:
597 * @path: a #GtkWidgetPath
598 * @pos: position to query, -1 for the path head
600 * Returns a list with all the class names defined for the widget
601 * at position @pos in the hierarchy defined in @path.
603 * Returns: (transfer container) (element-type utf8): The list of
604 * classes, This is a list of strings, the #GSList contents
605 * are owned by GTK+, but you should use g_slist_free() to
606 * free the list itself.
611 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
614 GtkPathElement *elem;
618 g_return_val_if_fail (path != NULL, NULL);
619 g_return_val_if_fail (path->elems->len != 0, NULL);
621 if (pos < 0 || pos >= path->elems->len)
622 pos = path->elems->len - 1;
624 elem = &g_array_index (path->elems, GtkPathElement, pos);
629 for (i = 0; i < elem->classes->len; i++)
633 quark = g_array_index (elem->classes, GQuark, i);
634 list = g_slist_prepend (list, (gchar *) g_quark_to_string (quark));
637 return g_slist_reverse (list);
641 * gtk_widget_path_iter_has_qclass:
642 * @path: a #GtkWidgetPath
643 * @pos: position to query, -1 for the path head
644 * @qname: class name as a #GQuark
646 * See gtk_widget_path_iter_has_class(). This is a version that operates
647 * with GQuark<!-- -->s.
649 * Returns: %TRUE if the widget at @pos has the class defined.
654 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
658 GtkPathElement *elem;
661 g_return_val_if_fail (path != NULL, FALSE);
662 g_return_val_if_fail (path->elems->len != 0, FALSE);
663 g_return_val_if_fail (qname != 0, FALSE);
665 if (pos < 0 || pos >= path->elems->len)
666 pos = path->elems->len - 1;
668 elem = &g_array_index (path->elems, GtkPathElement, pos);
673 for (i = 0; i < elem->classes->len; i++)
677 quark = g_array_index (elem->classes, GQuark, i);
681 else if (quark > qname)
689 * gtk_widget_path_iter_has_class:
690 * @path: a #GtkWidgetPath
691 * @pos: position to query, -1 for the path head
694 * Returns %TRUE if the widget at position @pos has the class @name
695 * defined, %FALSE otherwise.
697 * Returns: %TRUE if the class @name is defined for the widget at @pos
702 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
708 g_return_val_if_fail (path != NULL, FALSE);
709 g_return_val_if_fail (path->elems->len != 0, FALSE);
710 g_return_val_if_fail (name != NULL, FALSE);
712 if (pos < 0 || pos >= path->elems->len)
713 pos = path->elems->len - 1;
715 qname = g_quark_try_string (name);
720 return gtk_widget_path_iter_has_qclass (path, pos, qname);
724 * gtk_widget_path_iter_add_region:
725 * @path: a #GtkWidgetPath
726 * @pos: position to modify, -1 for the path head
728 * @flags: flags affecting the region
730 * Adds the region @name to the widget at position @pos in
731 * the hierarchy defined in @path. See
732 * gtk_style_context_add_region().
734 * <note><para>Region names must only contain lowercase letters
735 * and '-', starting always with a lowercase letter.</para></note>
740 gtk_widget_path_iter_add_region (GtkWidgetPath *path,
743 GtkRegionFlags flags)
745 GtkPathElement *elem;
748 g_return_if_fail (path != NULL);
749 g_return_if_fail (path->elems->len != 0);
750 g_return_if_fail (name != NULL);
751 g_return_if_fail (_gtk_style_context_check_region_name (name));
753 if (pos < 0 || pos >= path->elems->len)
754 pos = path->elems->len - 1;
756 elem = &g_array_index (path->elems, GtkPathElement, pos);
757 qname = g_quark_from_string (name);
760 elem->regions = g_hash_table_new (NULL, NULL);
762 g_hash_table_insert (elem->regions,
763 GUINT_TO_POINTER (qname),
764 GUINT_TO_POINTER (flags));
768 * gtk_widget_path_iter_remove_region:
769 * @path: a #GtkWidgetPath
770 * @pos: position to modify, -1 for the path head
773 * Removes the region @name from the widget at position @pos in
774 * the hierarchy defined in @path.
779 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
783 GtkPathElement *elem;
786 g_return_if_fail (path != NULL);
787 g_return_if_fail (path->elems->len != 0);
788 g_return_if_fail (name != NULL);
790 if (pos < 0 || pos >= path->elems->len)
791 pos = path->elems->len - 1;
793 qname = g_quark_try_string (name);
798 elem = &g_array_index (path->elems, GtkPathElement, pos);
801 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
805 * gtk_widget_path_iter_clear_regions:
806 * @path: a #GtkWidgetPath
807 * @pos: position to modify, -1 for the path head
809 * Removes all regions from the widget at position @pos in the
810 * hierarchy defined in @path.
815 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
818 GtkPathElement *elem;
820 g_return_if_fail (path != NULL);
821 g_return_if_fail (path->elems->len != 0);
823 if (pos < 0 || pos >= path->elems->len)
824 pos = path->elems->len - 1;
826 elem = &g_array_index (path->elems, GtkPathElement, pos);
829 g_hash_table_remove_all (elem->regions);
833 * gtk_widget_path_iter_list_regions:
834 * @path: a #GtkWidgetPath
835 * @pos: position to query, -1 for the path head
837 * Returns a list with all the region names defined for the widget
838 * at position @pos in the hierarchy defined in @path.
840 * Returns: (transfer container) (element-type utf8): The list of
841 * regions, This is a list of strings, the #GSList contents
842 * are owned by GTK+, but you should use g_slist_free() to
843 * free the list itself.
848 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
851 GtkPathElement *elem;
856 g_return_val_if_fail (path != NULL, NULL);
857 g_return_val_if_fail (path->elems->len != 0, NULL);
859 if (pos < 0 || pos >= path->elems->len)
860 pos = path->elems->len - 1;
862 elem = &g_array_index (path->elems, GtkPathElement, pos);
867 g_hash_table_iter_init (&iter, elem->regions);
869 while (g_hash_table_iter_next (&iter, &key, NULL))
873 qname = GPOINTER_TO_UINT (key);
874 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
881 * gtk_widget_path_iter_has_qregion:
882 * @path: a #GtkWidgetPath
883 * @pos: position to query, -1 for the path head
884 * @qname: region name as a #GQuark
885 * @flags: (out): return location for the region flags
887 * See gtk_widget_path_iter_has_region(). This is a version that operates
888 * with GQuark<!-- -->s.
890 * Returns: %TRUE if the widget at @pos has the region defined.
895 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
898 GtkRegionFlags *flags)
900 GtkPathElement *elem;
903 g_return_val_if_fail (path != NULL, FALSE);
904 g_return_val_if_fail (path->elems->len != 0, FALSE);
905 g_return_val_if_fail (qname != 0, FALSE);
907 if (pos < 0 || pos >= path->elems->len)
908 pos = path->elems->len - 1;
910 elem = &g_array_index (path->elems, GtkPathElement, pos);
915 if (!g_hash_table_lookup_extended (elem->regions,
916 GUINT_TO_POINTER (qname),
921 *flags = GPOINTER_TO_UINT (value);
927 * gtk_widget_path_iter_has_region:
928 * @path: a #GtkWidgetPath
929 * @pos: position to query, -1 for the path head
931 * @flags: (out): return location for the region flags
933 * Returns %TRUE if the widget at position @pos has the class @name
934 * defined, %FALSE otherwise.
936 * Returns: %TRUE if the class @name is defined for the widget at @pos
941 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
944 GtkRegionFlags *flags)
948 g_return_val_if_fail (path != NULL, FALSE);
949 g_return_val_if_fail (path->elems->len != 0, FALSE);
950 g_return_val_if_fail (name != NULL, FALSE);
952 if (pos < 0 || pos >= path->elems->len)
953 pos = path->elems->len - 1;
955 qname = g_quark_try_string (name);
960 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
964 * gtk_widget_path_get_object_type:
965 * @path: a #GtkWidget
967 * Returns the topmost object type, that is, the object type this path
970 * Returns: The object type
975 gtk_widget_path_get_object_type (const GtkWidgetPath *path)
977 GtkPathElement *elem;
979 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
981 elem = &g_array_index (path->elems, GtkPathElement,
982 path->elems->len - 1);
987 * gtk_widget_path_is_type:
988 * @path: a #GtkWidgetPath
989 * @type: widget type to match
991 * Returns %TRUE if the widget type represented by this path
992 * is @type, or a subtype of it.
994 * Returns: %TRUE if the widget represented by @path is of type @type
999 gtk_widget_path_is_type (const GtkWidgetPath *path,
1002 GtkPathElement *elem;
1004 g_return_val_if_fail (path != NULL, FALSE);
1006 elem = &g_array_index (path->elems, GtkPathElement,
1007 path->elems->len - 1);
1009 if (elem->type == type ||
1010 g_type_is_a (elem->type, type))
1017 * gtk_widget_path_has_parent:
1018 * @path: a #GtkWidgetPath
1019 * @type: widget type to check in parents
1021 * Returns %TRUE if any of the parents of the widget represented
1022 * in @path is of type @type, or any subtype of it.
1024 * Returns: %TRUE if any parent is of type @type
1029 gtk_widget_path_has_parent (const GtkWidgetPath *path,
1034 g_return_val_if_fail (path != NULL, FALSE);
1036 for (i = 0; i < path->elems->len - 1; i++)
1038 GtkPathElement *elem;
1040 elem = &g_array_index (path->elems, GtkPathElement, i);
1042 if (elem->type == type ||
1043 g_type_is_a (elem->type, type))