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_prepend_val (path->elems, new);
138 gtk_widget_path_append_type (GtkWidgetPath *path,
141 GtkPathElement new = { 0 };
143 g_return_val_if_fail (path != NULL, 0);
144 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), 0);
147 g_array_append_val (path->elems, new);
149 return path->elems->len - 1;
153 gtk_widget_path_iter_get_widget_type (const GtkWidgetPath *path,
156 GtkPathElement *elem;
158 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
159 g_return_val_if_fail (pos < path->elems->len, G_TYPE_INVALID);
161 elem = &g_array_index (path->elems, GtkPathElement, pos);
166 gtk_widget_path_iter_set_widget_type (GtkWidgetPath *path,
170 GtkPathElement *elem;
172 g_return_if_fail (path != NULL);
173 g_return_if_fail (pos < path->elems->len);
174 g_return_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET));
176 elem = &g_array_index (path->elems, GtkPathElement, pos);
180 G_CONST_RETURN gchar *
181 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
184 GtkPathElement *elem;
186 g_return_val_if_fail (path != NULL, NULL);
187 g_return_val_if_fail (pos < path->elems->len, NULL);
189 elem = &g_array_index (path->elems, GtkPathElement, pos);
190 return g_quark_to_string (elem->name);
194 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
198 GtkPathElement *elem;
200 g_return_if_fail (path != NULL);
201 g_return_if_fail (pos < path->elems->len);
202 g_return_if_fail (name != NULL);
204 elem = &g_array_index (path->elems, GtkPathElement, pos);
206 elem->name = g_quark_from_string (name);
210 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
214 GtkPathElement *elem;
216 g_return_val_if_fail (path != NULL, FALSE);
217 g_return_val_if_fail (qname != 0, FALSE);
218 g_return_val_if_fail (pos < path->elems->len, FALSE);
220 elem = &g_array_index (path->elems, GtkPathElement, pos);
222 return (elem->name == qname);
226 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
232 g_return_val_if_fail (path != NULL, FALSE);
233 g_return_val_if_fail (name != NULL, FALSE);
234 g_return_val_if_fail (pos < path->elems->len, FALSE);
236 qname = g_quark_try_string (name);
241 return gtk_widget_path_iter_has_qname (path, pos, qname);
245 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
249 GtkPathElement *elem;
250 gboolean added = FALSE;
254 g_return_if_fail (path != NULL);
255 g_return_if_fail (pos < path->elems->len);
256 g_return_if_fail (name != NULL);
258 elem = &g_array_index (path->elems, GtkPathElement, pos);
259 qname = g_quark_from_string (name);
262 elem->classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
264 for (i = 0; i < elem->classes->len; i++)
268 quark = g_array_index (elem->classes, GQuark, i);
278 g_array_insert_val (elem->classes, i, qname);
285 g_array_append_val (elem->classes, qname);
289 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
293 GtkPathElement *elem;
297 g_return_if_fail (path != NULL);
298 g_return_if_fail (pos < path->elems->len);
299 g_return_if_fail (name != NULL);
301 qname = g_quark_try_string (name);
306 elem = &g_array_index (path->elems, GtkPathElement, pos);
311 for (i = 0; i < elem->classes->len; i++)
315 quark = g_array_index (elem->classes, GQuark, i);
319 else if (quark == qname)
321 g_array_remove_index (elem->classes, i);
328 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
331 GtkPathElement *elem;
333 g_return_if_fail (path != NULL);
334 g_return_if_fail (pos < path->elems->len);
336 elem = &g_array_index (path->elems, GtkPathElement, pos);
341 if (elem->classes->len > 0)
342 g_array_remove_range (elem->classes, 0, elem->classes->len);
346 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
349 GtkPathElement *elem;
353 g_return_val_if_fail (path != NULL, NULL);
354 g_return_val_if_fail (pos < path->elems->len, NULL);
356 elem = &g_array_index (path->elems, GtkPathElement, pos);
361 for (i = 0; i < elem->classes->len; i++)
365 quark = g_array_index (elem->classes, GQuark, i);
366 list = g_slist_prepend (list, (gchar *) g_quark_to_string (quark));
369 return g_slist_reverse (list);
373 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
377 GtkPathElement *elem;
380 g_return_val_if_fail (path != NULL, FALSE);
381 g_return_val_if_fail (pos < path->elems->len, FALSE);
382 g_return_val_if_fail (qname != 0, FALSE);
384 elem = &g_array_index (path->elems, GtkPathElement, pos);
389 for (i = 0; i < elem->classes->len; i++)
393 quark = g_array_index (elem->classes, GQuark, i);
397 else if (quark > qname)
405 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
411 g_return_val_if_fail (path != NULL, FALSE);
412 g_return_val_if_fail (pos < path->elems->len, FALSE);
413 g_return_val_if_fail (name != NULL, FALSE);
415 qname = g_quark_try_string (name);
420 return gtk_widget_path_iter_has_qclass (path, pos, qname);
424 gtk_widget_path_iter_add_region (GtkWidgetPath *path,
427 GtkRegionFlags flags)
429 GtkPathElement *elem;
432 g_return_if_fail (path != NULL);
433 g_return_if_fail (pos < path->elems->len);
434 g_return_if_fail (name != NULL);
436 elem = &g_array_index (path->elems, GtkPathElement, pos);
437 qname = g_quark_from_string (name);
440 elem->regions = g_hash_table_new (NULL, NULL);
442 g_hash_table_insert (elem->regions,
443 GUINT_TO_POINTER (qname),
444 GUINT_TO_POINTER (flags));
448 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
452 GtkPathElement *elem;
455 g_return_if_fail (path != NULL);
456 g_return_if_fail (pos < path->elems->len);
457 g_return_if_fail (name != NULL);
459 qname = g_quark_try_string (name);
464 elem = &g_array_index (path->elems, GtkPathElement, pos);
467 g_hash_table_remove (elem->regions, GUINT_TO_POINTER (qname));
471 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
474 GtkPathElement *elem;
476 g_return_if_fail (path != NULL);
477 g_return_if_fail (pos < path->elems->len);
479 elem = &g_array_index (path->elems, GtkPathElement, pos);
482 g_hash_table_remove_all (elem->regions);
486 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
489 GtkPathElement *elem;
494 g_return_val_if_fail (path != NULL, NULL);
495 g_return_val_if_fail (pos < path->elems->len, NULL);
497 elem = &g_array_index (path->elems, GtkPathElement, pos);
502 g_hash_table_iter_init (&iter, elem->regions);
504 while (g_hash_table_iter_next (&iter, &key, NULL))
508 qname = GPOINTER_TO_UINT (key);
509 list = g_slist_prepend (list, (gchar *) g_quark_to_string (qname));
516 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
519 GtkRegionFlags *flags)
521 GtkPathElement *elem;
524 g_return_val_if_fail (path != NULL, FALSE);
525 g_return_val_if_fail (pos < path->elems->len, FALSE);
526 g_return_val_if_fail (qname != 0, FALSE);
528 elem = &g_array_index (path->elems, GtkPathElement, pos);
533 if (!g_hash_table_lookup_extended (elem->regions,
534 GUINT_TO_POINTER (qname),
539 *flags = GPOINTER_TO_UINT (value);
545 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
548 GtkRegionFlags *flags)
552 g_return_val_if_fail (path != NULL, FALSE);
553 g_return_val_if_fail (pos < path->elems->len, FALSE);
554 g_return_val_if_fail (name != NULL, FALSE);
556 qname = g_quark_try_string (name);
561 return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
565 gtk_widget_path_get_widget_type (const GtkWidgetPath *path)
567 GtkPathElement *elem;
569 g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
571 elem = &g_array_index (path->elems, GtkPathElement, 0);
576 gtk_widget_path_is_type (const GtkWidgetPath *path,
579 GtkPathElement *elem;
581 g_return_val_if_fail (path != NULL, FALSE);
582 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
584 elem = &g_array_index (path->elems, GtkPathElement, 0);
586 if (elem->type == type ||
587 g_type_is_a (elem->type, type))
594 gtk_widget_path_has_parent (const GtkWidgetPath *path,
599 g_return_val_if_fail (path != NULL, FALSE);
600 g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), FALSE);
602 for (i = 1; i < path->elems->len; i++)
604 GtkPathElement *elem;
606 elem = &g_array_index (path->elems, GtkPathElement, i);
608 if (elem->type == type ||
609 g_type_is_a (elem->type, type))