1 /*
2 * Copyright (c) 2014 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19 #include <glib/gi18n-lib.h>
20
21 #include "selector.h"
22
23 #include "gtktreeselection.h"
24 #include "gtktreestore.h"
25 #include "gtktreeview.h"
26 #include "gtkwidgetpath.h"
27 #include "gtklabel.h"
28
29
30 enum
31 {
32 COLUMN_SELECTOR
33 };
34
35 struct _GtkInspectorSelectorPrivate
36 {
37 GtkTreeStore *model;
38 GtkTreeView *tree;
39 };
40
G_DEFINE_TYPE_WITH_PRIVATE(GtkInspectorSelector,gtk_inspector_selector,GTK_TYPE_BOX)41 G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorSelector, gtk_inspector_selector, GTK_TYPE_BOX)
42
43 static void
44 gtk_inspector_selector_init (GtkInspectorSelector *oh)
45 {
46 oh->priv = gtk_inspector_selector_get_instance_private (oh);
47 gtk_widget_init_template (GTK_WIDGET (oh));
48 }
49
50 static void
gtk_inspector_selector_class_init(GtkInspectorSelectorClass * klass)51 gtk_inspector_selector_class_init (GtkInspectorSelectorClass *klass)
52 {
53 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
54
55 gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/inspector/selector.ui");
56 gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorSelector, model);
57 gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorSelector, tree);
58 }
59
60 void
gtk_inspector_selector_set_object(GtkInspectorSelector * oh,GObject * object)61 gtk_inspector_selector_set_object (GtkInspectorSelector *oh,
62 GObject *object)
63 {
64 GtkTreeIter iter, parent;
65 gint i;
66 GtkWidget *widget;
67 gchar *path, **words;
68
69 gtk_tree_store_clear (oh->priv->model);
70
71 if (!GTK_IS_WIDGET (object))
72 {
73 gtk_widget_hide (GTK_WIDGET (oh));
74 return;
75 }
76
77 widget = GTK_WIDGET (object);
78
79 path = gtk_widget_path_to_string (gtk_widget_get_path (widget));
80 words = g_strsplit (path, " ", 0);
81
82 for (i = 0; words[i]; i++)
83 {
84 gtk_tree_store_append (oh->priv->model, &iter, i ? &parent : NULL);
85 gtk_tree_store_set (oh->priv->model, &iter,
86 COLUMN_SELECTOR, words[i],
87 -1);
88 parent = iter;
89 }
90
91 g_strfreev (words);
92 g_free (path);
93
94 gtk_tree_view_expand_all (oh->priv->tree);
95 gtk_tree_selection_select_iter (gtk_tree_view_get_selection (oh->priv->tree), &iter);
96
97 gtk_widget_show (GTK_WIDGET (oh));
98 }
99
100 // vim: set et sw=2 ts=2:
101