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"
28 * SECTION:gtkwidgetpath
29 * @Short_description: Widget path abstraction
30 * @Title: GtkWidgetPath
31 * @See_also: #GtkStyleContext
33 * GtkWidgetPath is a boxed type that represents a widget hierarchy from
34 * the topmost widget, typically a toplevel, to any child. This widget
35 * path abstraction is used in #GtkStyleContext on behalf of the real
36 * widget in order to query style information.
38 * If you are using GTK+ widgets, you probably will not need to use
39 * this API directly, as there is gtk_widget_get_path(), and the style
40 * context returned by gtk_widget_get_style_context() will be automatically
41 * updated on widget hierarchy changes.
43 * The widget path generation is generally simple:
45 * <title>Defining a button within a window</title>
48 * GtkWidgetPath *path;
50 * path = gtk_widget_path_new ();
51 * gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
52 * gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
57 * Although more complex information, such as widget names, or
58 * different classes (property that may be used by other widget
59 * types) and intermediate regions may be included:
62 * <title>Defining the first tab widget in a notebook</title>
65 * GtkWidgetPath *path;
68 * path = gtk_widget_path_new ();
70 * pos = gtk_widget_path_append_type (path, GTK_TYPE_NOTEBOOK);
71 * gtk_widget_path_iter_add_region (path, pos, "tab", GTK_REGION_EVEN | GTK_REGION_FIRST);
73 * pos = gtk_widget_path_append_type (path, GTK_TYPE_LABEL);
74 * gtk_widget_path_iter_set_name (path, pos, "first tab label");
79 * All this information will be used to match the style information
80 * that applies to the described widget.
83 G_DEFINE_BOXED_TYPE (GtkWidgetPath, gtk_widget_path,
84 gtk_widget_path_copy, gtk_widget_path_free)
87 typedef struct GtkPathElement GtkPathElement;
99 GArray *elems; /* First element contains the described widget */
103 * gtk_widget_path_new:
105 * Returns an empty widget path.
107 * Returns: (transfer full): A newly created, empty, #GtkWidgetPath
112 gtk_widget_path_new (void)
116 path = g_slice_new0 (GtkWidgetPath);
117 path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
123 * gtk_widget_path_copy:
124 * @path: a #GtkWidgetPath
126 * Returns a copy of @path
128 * Returns: (transfer full): a copy of @path
133 gtk_widget_path_copy (const GtkWidgetPath *path)
135 GtkWidgetPath *new_path;
138 g_return_val_if_fail (path != NULL, NULL);
140 new_path = gtk_widget_path_new ();
142 for (i = 0; i < path->elems->len; i++)
144 GtkPathElement *elem, new = { 0 };
146 elem = &g_array_index (path->elems, GtkPathElement, i);
148 new.type = elem->type;
149 new.name = elem->name;
156 g_hash_table_iter_init (&iter, elem->regions);
157 new.regions = g_hash_table_new (NULL, NULL);
159 while (g_hash_table_iter_next (&iter, &key, &value))
160 g_hash_table_insert (new.regions, key, value);
165 new.classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
166 g_array_append_vals (new.classes, elem->classes->data, elem->classes->len);
169 g_array_append_val (new_path->elems, new);
176 * gtk_widget_path_free:
177 * @path: a #GtkWidgetPath
179 * Frees a #GtkWidgetPath.
184 gtk_widget_path_free (GtkWidgetPath *path)
188 g_return_if_fail (path != NULL);
190 for (i = 0; i < path->elems->len; i++)
192 GtkPathElement *elem;
194 elem = &g_array_index (path->elems, GtkPathElement, i);
197 g_hash_table_destroy (elem->regions);
200 g_array_free (elem->classes, TRUE);
203 g_array_free (path->elems, TRUE);
204 g_slice_free (GtkWidgetPath, path);
208 * gtk_widget_path_length:
209 * @path: a #GtkWidgetPath
211 * Returns the number of #GtkWidget #GTypes between the represented
212 * widget and its topmost container.
214 * Returns: the number of elements in the path
219 gtk_widget_path_length (const GtkWidgetPath *path)
221 g_return_val_if_fail (path != NULL, 0);
223 return path->elems->len;
227 * gtk_widget_path_prepend_type:
228 * @path: a #GtkWidgetPath
229 * @type: widget type to prepend
231 * Prepends a widget type to the widget hierachy represented by @path.
236 gtk_widget_path_prepend_type (GtkWidgetPath *path,
239 GtkPathElement new = { 0 };
241 g_return_if_fail (path != NULL);
244 g_array_prepend_val (path->elems, new);
248 * gtk_widget_path_append_type:
249 * @path: a #GtkWidgetPath
250 * @type: widget type to append
252 * Appends a widget type to the widget hierachy represented by @path.
254 * Returns: the position where the element was inserted
259 gtk_widget_path_append_type (GtkWidgetPath *path,
262 GtkPathElement new = { 0 };
264 g_return_val_if_fail (path != NULL, 0);
267 g_array_append_val (path->elems, new);
269 return path->elems->len - 1;
273 * gtk_widget_path_iter_get_object_type:
274 * @path: a #GtkWidgetPath
275 * @pos: position to get the object type for, -1 for the path head
277 * Returns the object #GType that is at position @pos in the widget
278 * hierarchy defined in @path.
280 * Returns: a widget type
285 gtk_widget_path_iter_get_object_type (const GtkWidgetPath *path,
288 GtkPathElement *elem;
290 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
291 g_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
293 if (pos < 0 || pos > path->elems->len)
294 pos = path->elems->len - 1;
296 elem = &g_array_index (path->elems, GtkPathElement, pos);
301 * gtk_widget_path_iter_set_object_type:
302 * @path: a #GtkWidgetPath
303 * @pos: position to modify, -1 for the path head
304 * @type: object type to set
306 * Sets the object type for a given position in the widget hierarchy
312 gtk_widget_path_iter_set_object_type (GtkWidgetPath *path,
316 GtkPathElement *elem;
318 g_return_if_fail (path != NULL);
319 g_return_if_fail (path->elems->len != 0);
321 if (pos < 0 || pos > path->elems->len)
322 pos = path->elems->len - 1;
324 elem = &g_array_index (path->elems, GtkPathElement, pos);
329 * gtk_widget_path_iter_get_name:
330 * @path: a #GtkWidgetPath
331 * @pos: position to get the widget name for, -1 for the path head
333 * Returns the name corresponding to the widget found at
334 * the position @pos in the widget hierarchy defined by
337 * Returns: The widget name, or %NULL if none was set.
339 G_CONST_RETURN gchar *
340 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
343 GtkPathElement *elem;
345 g_return_val_if_fail (path != NULL, NULL);
346 g_return_val_if_fail (path->elems->len != 0, NULL);
348 if (pos < 0 || pos > path->elems->len)
349 pos = path->elems->len - 1;
351 elem = &g_array_index (path->elems, GtkPathElement, pos);
352 return g_quark_to_string (elem->name);
356 * gtk_widget_path_iter_set_name:
357 * @path: a #GtkWidgetPath
358 * @pos: position to modify, -1 for the path head
361 * Sets the widget name for the widget found at position @pos
362 * in the widget hierarchy defined by @path.
367 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
371 GtkPathElement *elem;
373 g_return_if_fail (path != NULL);
374 g_return_if_fail (path->elems->len != 0);
375 g_return_if_fail (name != NULL);
377 if (pos < 0 || pos > path->elems->len)
378 pos = path->elems->len - 1;
380 elem = &g_array_index (path->elems, GtkPathElement, pos);
382 elem->name = g_quark_from_string (name);
386 * gtk_widget_path_iter_has_qname:
387 * @path: a #GtkWidgetPath
388 * @pos: position to query, -1 for the path head
389 * @qname: widget name as a #GQuark
391 * See gtk_widget_path_iter_has_name(). This is a version
392 * that operates on #GQuark<!-- -->s.
394 * Returns: %TRUE if the widget at @pos has this name
399 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
403 GtkPathElement *elem;
405 g_return_val_if_fail (path != NULL, FALSE);
406 g_return_val_if_fail (path->elems->len != 0, FALSE);
407 g_return_val_if_fail (qname != 0, FALSE);
409 if (pos < 0 || pos > path->elems->len)
410 pos = path->elems->len - 1;
412 elem = &g_array_index (path->elems, GtkPathElement, pos);
414 return (elem->name == qname);
418 * gtk_widget_path_iter_has_name:
419 * @path: a #GtkWidgetPath
420 * @pos: position to query, -1 for the path head
421 * @name: a widget name
423 * Returns %TRUE if the widget at position @pos has the name @name,
426 * Returns: %TRUE if the widget at @pos has this name
431 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
437 g_return_val_if_fail (path != NULL, FALSE);
438 g_return_val_if_fail (path->elems->len != 0, FALSE);
440 if (pos < 0 || pos > path->elems->len)
441 pos = path->elems->len - 1;
443 qname = g_quark_try_string (name);
448 return gtk_widget_path_iter_has_qname (path, pos, qname);
452 * gtk_widget_path_iter_add_class:
453 * @path: a #GtkWidget
454 * @pos: position to modify, -1 for the path head
455 * @name: a class name
457 * Adds the class @name to the widget at position @pos in
458 * the hierarchy defined in @path. See
459 * gtk_style_context_add_class().
464 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
468 GtkPathElement *elem;
469 gboolean added = FALSE;
473 g_return_if_fail (path != NULL);
474 g_return_if_fail (path->elems->len != 0);
475 g_return_if_fail (name != NULL);
477 if (pos < 0 || pos > path->elems->len)
478 pos = path->elems->len - 1;
480 elem = &g_array_index (path->elems, GtkPathElement, pos);
481 qname = g_quark_from_string (name);
484 elem->classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
486 for (i = 0; i < elem->classes->len; i++)
490 quark = g_array_index (elem->classes, GQuark, i);
500 g_array_insert_val (elem->classes, i, qname);
507 g_array_append_val (elem->classes, qname);
511 * gtk_widget_path_iter_remove_class:
512 * @path: a #GtkWidgetPath
513 * @pos: position to modify, -1 for the path head
516 * Removes the class @name from the widget at position @pos in
517 * the hierarchy defined in @path.
522 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
526 GtkPathElement *elem;
530 g_return_if_fail (path != NULL);
531 g_return_if_fail (path->elems->len != 0);
532 g_return_if_fail (name != NULL);
534 if (pos < 0 || pos > path->elems->len)
535 pos = path->elems->len - 1;
537 qname = g_quark_try_string (name);
542 elem = &g_array_index (path->elems, GtkPathElement, pos);
547 for (i = 0; i < elem->classes->len; i++)
551 quark = g_array_index (elem->classes, GQuark, i);
555 else if (quark == qname)
557 g_array_remove_index (elem->classes, i);
564 * gtk_widget_path_iter_clear_classes:
565 * @path: a #GtkWidget
566 * @pos: position to modify, -1 for the path head
568 * Removes all classes from the widget at position @pos in the
569 * hierarchy defined in @path.
574 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
577 GtkPathElement *elem;
579 g_return_if_fail (path != NULL);
580 g_return_if_fail (path->elems->len != 0);
582 if (pos < 0 || pos > path->elems->len)
583 pos = path->elems->len - 1;
585 elem = &g_array_index (path->elems, GtkPathElement, pos);
590 if (elem->classes->len > 0)
591 g_array_remove_range (elem->classes, 0, elem->classes->len);
595 * gtk_widget_path_iter_list_classes:
596 * @path: a #GtkWidgetPath
597 * @pos: position to query, -1 for the path head
599 * Returns a list with all the class names defined for the widget
600 * at position @pos in the hierarchy defined in @path.
602 * Returns: (transfer container) (type utf8): The list of classes,
603 * This is a list of strings, the #GSList contents are
604 * owned by GTK+, but you should use g_slist_free() to
605 * free the list itself.
610 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
613 GtkPathElement *elem;
617 g_return_val_if_fail (path != NULL, NULL);
618 g_return_val_if_fail (path->elems->len != 0, NULL);
620 if (pos < 0 || pos > path->elems->len)
621 pos = path->elems->len - 1;
623 elem = &g_array_index (path->elems, GtkPathElement, pos);
628 for (i = 0; i < elem->classes->len; i++)
632 quark = g_array_index (elem->classes, GQuark, i);
633 list = g_slist_prepend (list, (gchar *) g_quark_to_string (quark));
636 return g_slist_reverse (list);
640 * gtk_widget_path_iter_has_qclass:
641 * @path: a #GtkWidgetPath
642 * @pos: position to query, -1 for the path head
643 * @qname: class name as a #GQuark
645 * See gtk_widget_path_iter_has_class(). This is a version that operates
646 * with GQuark<!-- -->s.
648 * Returns: %TRUE if the widget at @pos has the class defined.
653 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
657 GtkPathElement *elem;
660 g_return_val_if_fail (path != NULL, FALSE);
661 g_return_val_if_fail (path->elems->len != 0, FALSE);
662 g_return_val_if_fail (qname != 0, FALSE);
664 if (pos < 0 || pos > path->elems->len)
665 pos = path->elems->len - 1;
667 elem = &g_array_index (path->elems, GtkPathElement, pos);
672 for (i = 0; i < elem->classes->len; i++)
676 quark = g_array_index (elem->classes, GQuark, i);
680 else if (quark > qname)
688 * gtk_widget_path_iter_has_class:
689 * @path: a #GtkWidgetPath
690 * @pos: position to query, -1 for the path head
693 * Returns %TRUE if the widget at position @pos has the class @name
694 * defined, %FALSE otherwise.
696 * Returns: %TRUE if the class @name is defined for the widget at @pos
701 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
707 g_return_val_if_fail (path != NULL, FALSE);
708 g_return_val_if_fail (path->elems->len != 0, FALSE);
709 g_return_val_if_fail (name != NULL, FALSE);
711 if (pos < 0 || pos > path->elems->len)
712 pos = path->elems->len - 1;
714 qname = g_quark_try_string (name);
719 return gtk_widget_path_iter_has_qclass (path, pos, qname);
723 * gtk_widget_path_iter_add_region:
724 * @path: a #GtkWidgetPath
725 * @pos: position to modify, -1 for the path head
727 * @flags: flags affecting the region
729 * Adds the region @name to the widget at position @pos in
730 * the hierarchy defined in @path. See
731 * gtk_style_context_add_region().
733 * <note><para>Region names must only contain lowercase letters
734 * and '-', starting always with a lowercase letter.</para></note>
739 gtk_widget_path_iter_add_region (GtkWidgetPath *path,
742 GtkRegionFlags flags)
744 GtkPathElement *elem;
747 g_return_if_fail (path != NULL);
748 g_return_if_fail (path->elems->len != 0);
749 g_return_if_fail (name != NULL);
750 g_return_if_fail (_gtk_style_context_check_region_name (name));
752 if (pos < 0 || pos > path->elems->len)
753 pos = path->elems->len - 1;
755 elem = &g_array_index (path->elems, GtkPathElement, pos);
756 qname = g_quark_from_string (name);
759 elem->regions = g_hash_table_new (NULL, NULL);
761 g_hash_table_insert (elem->regions,
762 GUINT_TO_POINTER (qname),
763 GUINT_TO_POINTER (flags));
767 * gtk_widget_path_iter_remove_region:
768 * @path: a #GtkWidgetPath
769 * @pos: position to modify, -1 for the path head
772 * Removes the region @name from the widget at position @pos in
773 * the hierarchy defined in @path.
778 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
782 GtkPathElement *elem;
785 g_return_if_fail (path != NULL);
786 g_return_if_fail (path->elems->len != 0);
787 g_return_if_fail (name != NULL);
789 if (pos < 0 || pos > path->elems->len)
790 pos = path->elems->len - 1;
792 qname = g_quark_try_string (name);
797 elem = &g_array_index (path->elems, GtkPathElement, pos);
800 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
804 * gtk_widget_path_iter_clear_regions:
805 * @path: a #GtkWidgetPath
806 * @pos: position to modify, -1 for the path head
808 * Removes all regions from the widget at position @pos in the
809 * hierarchy defined in @path.
814 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
817 GtkPathElement *elem;
819 g_return_if_fail (path != NULL);
820 g_return_if_fail (path->elems->len != 0);
822 if (pos < 0 || pos > path->elems->len)
823 pos = path->elems->len - 1;
825 elem = &g_array_index (path->elems, GtkPathElement, pos);
828 g_hash_table_remove_all (elem->regions);
832 * gtk_widget_path_iter_list_regions:
833 * @path: a #GtkWidgetPath
834 * @pos: position to query, -1 for the path head
836 * Returns a list with all the region names defined for the widget
837 * at position @pos in the hierarchy defined in @path.
839 * Returns: (transfer container) (type utf8): The list of regions,
840 * This is a list of strings, the #GSList contents are
841 * owned by GTK+, but you should use g_slist_free() to
842 * free the list itself.
847 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
850 GtkPathElement *elem;
855 g_return_val_if_fail (path != NULL, NULL);
856 g_return_val_if_fail (path->elems->len != 0, NULL);
858 if (pos < 0 || pos > path->elems->len)
859 pos = path->elems->len - 1;
861 elem = &g_array_index (path->elems, GtkPathElement, pos);
866 g_hash_table_iter_init (&iter, elem->regions);
868 while (g_hash_table_iter_next (&iter, &key, NULL))
872 qname = GPOINTER_TO_UINT (key);
873 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
880 * gtk_widget_path_iter_has_qregion:
881 * @path: a #GtkWidgetPath
882 * @pos: position to query, -1 for the path head
883 * @qname: region name as a #GQuark
884 * @flags: (out): return location for the region flags
886 * See gtk_widget_path_iter_has_region(). This is a version that operates
887 * with GQuark<!-- -->s.
889 * Returns: %TRUE if the widget at @pos has the region defined.
894 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
897 GtkRegionFlags *flags)
899 GtkPathElement *elem;
902 g_return_val_if_fail (path != NULL, FALSE);
903 g_return_val_if_fail (path->elems->len != 0, FALSE);
904 g_return_val_if_fail (qname != 0, FALSE);
906 if (pos < 0 || pos > path->elems->len)
907 pos = path->elems->len - 1;
909 elem = &g_array_index (path->elems, GtkPathElement, pos);
914 if (!g_hash_table_lookup_extended (elem->regions,
915 GUINT_TO_POINTER (qname),
920 *flags = GPOINTER_TO_UINT (value);
926 * gtk_widget_path_iter_has_region:
927 * @path: a #GtkWidgetPath
928 * @pos: position to query, -1 for the path head
930 * @flags: (out): return location for the region flags
932 * Returns %TRUE if the widget at position @pos has the class @name
933 * defined, %FALSE otherwise.
935 * Returns: %TRUE if the class @name is defined for the widget at @pos
940 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
943 GtkRegionFlags *flags)
947 g_return_val_if_fail (path != NULL, FALSE);
948 g_return_val_if_fail (path->elems->len != 0, FALSE);
949 g_return_val_if_fail (name != NULL, FALSE);
951 if (pos < 0 || pos > path->elems->len)
952 pos = path->elems->len - 1;
954 qname = g_quark_try_string (name);
959 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
963 * gtk_widget_path_get_object_type:
964 * @path: a #GtkWidget
966 * Returns the topmost object type, that is, the object type this path
969 * Returns: The object type
974 gtk_widget_path_get_object_type (const GtkWidgetPath *path)
976 GtkPathElement *elem;
978 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
980 elem = &g_array_index (path->elems, GtkPathElement,
981 path->elems->len - 1);
986 * gtk_widget_path_is_type:
987 * @path: a #GtkWidgetPath
988 * @type: widget type to match
990 * Returns %TRUE if the widget type represented by this path
991 * is @type, or a subtype of it.
993 * Returns: %TRUE if the widget represented by @path is of type @type
998 gtk_widget_path_is_type (const GtkWidgetPath *path,
1001 GtkPathElement *elem;
1003 g_return_val_if_fail (path != NULL, FALSE);
1005 elem = &g_array_index (path->elems, GtkPathElement,
1006 path->elems->len - 1);
1008 if (elem->type == type ||
1009 g_type_is_a (elem->type, type))
1016 * gtk_widget_path_has_parent:
1017 * @path: a #GtkWidgetPath
1018 * @type: widget type to check in parents
1020 * Returns %TRUE if any of the parents of the widget represented
1021 * in @path is of type @type, or any subtype of it.
1023 * Returns: %TRUE if any parent is of type @type
1028 gtk_widget_path_has_parent (const GtkWidgetPath *path,
1033 g_return_val_if_fail (path != NULL, FALSE);
1035 for (i = 0; i < path->elems->len - 1; i++)
1037 GtkPathElement *elem;
1039 elem = &g_array_index (path->elems, GtkPathElement, i);
1041 if (elem->type == type ||
1042 g_type_is_a (elem->type, type))