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);
242 g_return_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET));
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);
266 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 0);
269 g_array_append_val (path->elems, new);
271 return path->elems->len - 1;
275 * gtk_widget_path_iter_get_widget_type:
276 * @path: a #GtkWidgetPath
277 * @pos: position to get the widget type for, -1 for the path head
279 * Returns the widget #GType that is at position @pos in the widget
280 * hierarchy defined in @path.
282 * Returns: a widget type
287 gtk_widget_path_iter_get_widget_type (const GtkWidgetPath *path,
290 GtkPathElement *elem;
292 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
293 g_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
295 if (pos < 0 || pos > path->elems->len)
296 pos = path->elems->len - 1;
298 elem = &g_array_index (path->elems, GtkPathElement, pos);
303 * gtk_widget_path_iter_set_widget_type:
304 * @path: a #GtkWidgetPath
305 * @pos: position to modify, -1 for the path head
306 * @type: widget type to set
308 * Sets the widget type for a given position in the widget hierarchy
309 * defined by @path. @type must be a #GtkWidget derived #GType.
314 gtk_widget_path_iter_set_widget_type (GtkWidgetPath *path,
318 GtkPathElement *elem;
320 g_return_if_fail (path != NULL);
321 g_return_if_fail (path->elems->len != 0);
322 g_return_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET));
324 if (pos < 0 || pos > path->elems->len)
325 pos = path->elems->len - 1;
327 elem = &g_array_index (path->elems, GtkPathElement, pos);
332 * gtk_widget_path_iter_get_name:
333 * @path: a #GtkWidgetPath
334 * @pos: position to get the widget name for, -1 for the path head
336 * Returns the name corresponding to the widget found at
337 * the position @pos in the widget hierarchy defined by
340 * Returns: The widget name, or %NULL if none was set.
342 G_CONST_RETURN gchar *
343 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
346 GtkPathElement *elem;
348 g_return_val_if_fail (path != NULL, NULL);
349 g_return_val_if_fail (path->elems->len != 0, NULL);
351 if (pos < 0 || pos > path->elems->len)
352 pos = path->elems->len - 1;
354 elem = &g_array_index (path->elems, GtkPathElement, pos);
355 return g_quark_to_string (elem->name);
359 * gtk_widget_path_iter_set_name:
360 * @path: a #GtkWidgetPath
361 * @pos: position to modify, -1 for the path head
364 * Sets the widget name for the widget found at position @pos
365 * in the widget hierarchy defined by @path.
370 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
374 GtkPathElement *elem;
376 g_return_if_fail (path != NULL);
377 g_return_if_fail (path->elems->len != 0);
378 g_return_if_fail (name != NULL);
380 if (pos < 0 || pos > path->elems->len)
381 pos = path->elems->len - 1;
383 elem = &g_array_index (path->elems, GtkPathElement, pos);
385 elem->name = g_quark_from_string (name);
389 * gtk_widget_path_iter_has_qname:
390 * @path: a #GtkWidgetPath
391 * @pos: position to query, -1 for the path head
392 * @qname: widget name as a #GQuark
394 * See gtk_widget_path_iter_has_name(). This is a version
395 * that operates on #GQuark<!-- -->s.
397 * Returns: %TRUE if the widget at @pos has this name
402 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
406 GtkPathElement *elem;
408 g_return_val_if_fail (path != NULL, FALSE);
409 g_return_val_if_fail (path->elems->len != 0, FALSE);
410 g_return_val_if_fail (qname != 0, FALSE);
412 if (pos < 0 || pos > path->elems->len)
413 pos = path->elems->len - 1;
415 elem = &g_array_index (path->elems, GtkPathElement, pos);
417 return (elem->name == qname);
421 * gtk_widget_path_iter_has_name:
422 * @path: a #GtkWidgetPath
423 * @pos: position to query, -1 for the path head
424 * @name: a widget name
426 * Returns %TRUE if the widget at position @pos has the name @name,
429 * Returns: %TRUE if the widget at @pos has this name
434 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
440 g_return_val_if_fail (path != NULL, FALSE);
441 g_return_val_if_fail (path->elems->len != 0, FALSE);
443 if (pos < 0 || pos > path->elems->len)
444 pos = path->elems->len - 1;
446 qname = g_quark_try_string (name);
451 return gtk_widget_path_iter_has_qname (path, pos, qname);
455 * gtk_widget_path_iter_add_class:
456 * @path: a #GtkWidget
457 * @pos: position to modify, -1 for the path head
458 * @name: a class name
460 * Adds the class @name to the widget at position @pos in
461 * the hierarchy defined in @path. See
462 * gtk_style_context_add_class().
467 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
471 GtkPathElement *elem;
472 gboolean added = FALSE;
476 g_return_if_fail (path != NULL);
477 g_return_if_fail (path->elems->len != 0);
478 g_return_if_fail (name != NULL);
480 if (pos < 0 || pos > path->elems->len)
481 pos = path->elems->len - 1;
483 elem = &g_array_index (path->elems, GtkPathElement, pos);
484 qname = g_quark_from_string (name);
487 elem->classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
489 for (i = 0; i < elem->classes->len; i++)
493 quark = g_array_index (elem->classes, GQuark, i);
503 g_array_insert_val (elem->classes, i, qname);
510 g_array_append_val (elem->classes, qname);
514 * gtk_widget_path_iter_remove_class:
515 * @path: a #GtkWidgetPath
516 * @pos: position to modify, -1 for the path head
519 * Removes the class @name from the widget at position @pos in
520 * the hierarchy defined in @path.
525 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
529 GtkPathElement *elem;
533 g_return_if_fail (path != NULL);
534 g_return_if_fail (path->elems->len != 0);
535 g_return_if_fail (name != NULL);
537 if (pos < 0 || pos > path->elems->len)
538 pos = path->elems->len - 1;
540 qname = g_quark_try_string (name);
545 elem = &g_array_index (path->elems, GtkPathElement, pos);
550 for (i = 0; i < elem->classes->len; i++)
554 quark = g_array_index (elem->classes, GQuark, i);
558 else if (quark == qname)
560 g_array_remove_index (elem->classes, i);
567 * gtk_widget_path_iter_clear_classes:
568 * @path: a #GtkWidget
569 * @pos: position to modify, -1 for the path head
571 * Removes all classes from the widget at position @pos in the
572 * hierarchy defined in @path.
577 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
580 GtkPathElement *elem;
582 g_return_if_fail (path != NULL);
583 g_return_if_fail (path->elems->len != 0);
585 if (pos < 0 || pos > path->elems->len)
586 pos = path->elems->len - 1;
588 elem = &g_array_index (path->elems, GtkPathElement, pos);
593 if (elem->classes->len > 0)
594 g_array_remove_range (elem->classes, 0, elem->classes->len);
598 * gtk_widget_path_iter_list_classes:
599 * @path: a #GtkWidgetPath
600 * @pos: position to query, -1 for the path head
602 * Returns a list with all the class names defined for the widget
603 * at position @pos in the hierarchy defined in @path.
605 * Returns: (transfer container) (type utf8): The list of classes,
606 * This is a list of strings, the #GSList contents are
607 * owned by GTK+, but you should use g_slist_free() to
608 * free the list itself.
613 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
616 GtkPathElement *elem;
620 g_return_val_if_fail (path != NULL, NULL);
621 g_return_val_if_fail (path->elems->len != 0, NULL);
623 if (pos < 0 || pos > path->elems->len)
624 pos = path->elems->len - 1;
626 elem = &g_array_index (path->elems, GtkPathElement, pos);
631 for (i = 0; i < elem->classes->len; i++)
635 quark = g_array_index (elem->classes, GQuark, i);
636 list = g_slist_prepend (list, (gchar *) g_quark_to_string (quark));
639 return g_slist_reverse (list);
643 * gtk_widget_path_iter_has_qclass:
644 * @path: a #GtkWidgetPath
645 * @pos: position to query, -1 for the path head
646 * @qname: class name as a #GQuark
648 * See gtk_widget_path_iter_has_class(). This is a version that operates
649 * with GQuark<!-- -->s.
651 * Returns: %TRUE if the widget at @pos has the class defined.
656 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
660 GtkPathElement *elem;
663 g_return_val_if_fail (path != NULL, FALSE);
664 g_return_val_if_fail (path->elems->len != 0, FALSE);
665 g_return_val_if_fail (qname != 0, FALSE);
667 if (pos < 0 || pos > path->elems->len)
668 pos = path->elems->len - 1;
670 elem = &g_array_index (path->elems, GtkPathElement, pos);
675 for (i = 0; i < elem->classes->len; i++)
679 quark = g_array_index (elem->classes, GQuark, i);
683 else if (quark > qname)
691 * gtk_widget_path_iter_has_class:
692 * @path: a #GtkWidgetPath
693 * @pos: position to query, -1 for the path head
696 * Returns %TRUE if the widget at position @pos has the class @name
697 * defined, %FALSE otherwise.
699 * Returns: %TRUE if the class @name is defined for the widget at @pos
704 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
710 g_return_val_if_fail (path != NULL, FALSE);
711 g_return_val_if_fail (path->elems->len != 0, FALSE);
712 g_return_val_if_fail (name != NULL, FALSE);
714 if (pos < 0 || pos > path->elems->len)
715 pos = path->elems->len - 1;
717 qname = g_quark_try_string (name);
722 return gtk_widget_path_iter_has_qclass (path, pos, qname);
726 * gtk_widget_path_iter_add_region:
727 * @path: a #GtkWidgetPath
728 * @pos: position to modify, -1 for the path head
730 * @flags: flags affecting the region
732 * Adds the region @name to the widget at position @pos in
733 * the hierarchy defined in @path. See
734 * gtk_style_context_add_region().
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);
751 if (pos < 0 || pos > path->elems->len)
752 pos = path->elems->len - 1;
754 elem = &g_array_index (path->elems, GtkPathElement, pos);
755 qname = g_quark_from_string (name);
758 elem->regions = g_hash_table_new (NULL, NULL);
760 g_hash_table_insert (elem->regions,
761 GUINT_TO_POINTER (qname),
762 GUINT_TO_POINTER (flags));
766 * gtk_widget_path_iter_remove_region:
767 * @path: a #GtkWidgetPath
768 * @pos: position to modify, -1 for the path head
771 * Removes the region @name from the widget at position @pos in
772 * the hierarchy defined in @path.
777 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
781 GtkPathElement *elem;
784 g_return_if_fail (path != NULL);
785 g_return_if_fail (path->elems->len != 0);
786 g_return_if_fail (name != NULL);
788 if (pos < 0 || pos > path->elems->len)
789 pos = path->elems->len - 1;
791 qname = g_quark_try_string (name);
796 elem = &g_array_index (path->elems, GtkPathElement, pos);
799 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
803 * gtk_widget_path_iter_clear_regions:
804 * @path: a #GtkWidgetPath
805 * @pos: position to modify, -1 for the path head
807 * Removes all regions from the widget at position @pos in the
808 * hierarchy defined in @path.
813 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
816 GtkPathElement *elem;
818 g_return_if_fail (path != NULL);
819 g_return_if_fail (path->elems->len != 0);
821 if (pos < 0 || pos > path->elems->len)
822 pos = path->elems->len - 1;
824 elem = &g_array_index (path->elems, GtkPathElement, pos);
827 g_hash_table_remove_all (elem->regions);
831 * gtk_widget_path_iter_list_regions:
832 * @path: a #GtkWidgetPath
833 * @pos: position to query, -1 for the path head
835 * Returns a list with all the region names defined for the widget
836 * at position @pos in the hierarchy defined in @path.
838 * Returns: (transfer container) (type utf8): The list of regions,
839 * This is a list of strings, the #GSList contents are
840 * owned by GTK+, but you should use g_slist_free() to
841 * free the list itself.
846 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
849 GtkPathElement *elem;
854 g_return_val_if_fail (path != NULL, NULL);
855 g_return_val_if_fail (path->elems->len != 0, NULL);
857 if (pos < 0 || pos > path->elems->len)
858 pos = path->elems->len - 1;
860 elem = &g_array_index (path->elems, GtkPathElement, pos);
865 g_hash_table_iter_init (&iter, elem->regions);
867 while (g_hash_table_iter_next (&iter, &key, NULL))
871 qname = GPOINTER_TO_UINT (key);
872 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
879 * gtk_widget_path_iter_has_qregion:
880 * @path: a #GtkWidgetPath
881 * @pos: position to query, -1 for the path head
882 * @qname: region name as a #GQuark
883 * @flags: (out): return location for the region flags
885 * See gtk_widget_path_iter_has_region(). This is a version that operates
886 * with GQuark<!-- -->s.
888 * Returns: %TRUE if the widget at @pos has the region defined.
893 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
896 GtkRegionFlags *flags)
898 GtkPathElement *elem;
901 g_return_val_if_fail (path != NULL, FALSE);
902 g_return_val_if_fail (path->elems->len != 0, FALSE);
903 g_return_val_if_fail (qname != 0, FALSE);
905 if (pos < 0 || pos > path->elems->len)
906 pos = path->elems->len - 1;
908 elem = &g_array_index (path->elems, GtkPathElement, pos);
913 if (!g_hash_table_lookup_extended (elem->regions,
914 GUINT_TO_POINTER (qname),
919 *flags = GPOINTER_TO_UINT (value);
925 * gtk_widget_path_iter_has_region:
926 * @path: a #GtkWidgetPath
927 * @pos: position to query, -1 for the path head
929 * @flags: (out): return location for the region flags
931 * Returns %TRUE if the widget at position @pos has the class @name
932 * defined, %FALSE otherwise.
934 * Returns: %TRUE if the class @name is defined for the widget at @pos
939 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
942 GtkRegionFlags *flags)
946 g_return_val_if_fail (path != NULL, FALSE);
947 g_return_val_if_fail (path->elems->len != 0, FALSE);
948 g_return_val_if_fail (name != NULL, FALSE);
950 if (pos < 0 || pos > path->elems->len)
951 pos = path->elems->len - 1;
953 qname = g_quark_try_string (name);
958 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
962 * gtk_widget_path_get_widget_type:
963 * @path: a #GtkWidget
965 * Returns the topmost widget type, that is, the widget type this path
968 * Returns: The widget type
973 gtk_widget_path_get_widget_type (const GtkWidgetPath *path)
975 GtkPathElement *elem;
977 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
979 elem = &g_array_index (path->elems, GtkPathElement,
980 path->elems->len - 1);
985 * gtk_widget_path_is_type:
986 * @path: a #GtkWidgetPath
987 * @type: widget type to match
989 * Returns %TRUE if the widget type represented by this path
990 * is @type, or a subtype of it.
992 * Returns: %TRUE if the widget represented by @path is of type @type
997 gtk_widget_path_is_type (const GtkWidgetPath *path,
1000 GtkPathElement *elem;
1002 g_return_val_if_fail (path != NULL, FALSE);
1003 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 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);
1034 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 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))