summaryrefslogtreecommitdiffstats
path: root/bin/gui/egg-property-tree-view.c
blob: 52d1e10c0ae745179d28bf36cb9d2bd55ced60f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (C) 2011, 2012 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
   (Karlsruhe Institute of Technology)

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public License along
   with this library; if not, write to the Free Software Foundation, Inc., 51
   Franklin St, Fifth Floor, Boston, MA 02110, USA */


#include "egg-property-tree-view.h"
#include "egg-property-cell-renderer.h"

G_DEFINE_TYPE (EggPropertyTreeView, egg_property_tree_view, GTK_TYPE_TREE_VIEW)

#define EGG_PROPERTY_TREE_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), EGG_TYPE_PROPERTY_TREE_VIEW, EggPropertyTreeViewPrivate))

struct _EggPropertyTreeViewPrivate
{
    GtkListStore *list_store;
};

enum
{
    COLUMN_PROP_NAME,
    COLUMN_PROP_ROW,
    COLUMN_PROP_ADJUSTMENT,
    N_COLUMNS
};

static void
egg_property_tree_view_populate_model_with_properties (GtkListStore *model, GObject *object)
{
    GParamSpec **pspecs;
    GObjectClass *oclass;
    guint n_properties;
    GtkTreeIter iter;

    oclass = G_OBJECT_GET_CLASS (object);
    pspecs = g_object_class_list_properties (oclass, &n_properties);

    for (guint i = 0; i < n_properties; i++) {
        if (pspecs[i]->flags & G_PARAM_READABLE) {
            GtkObject *adjustment;

            adjustment = gtk_adjustment_new (5, 0, 1000, 1, 10, 0);

            gtk_list_store_append (model, &iter);
            gtk_list_store_set (model, &iter,
                    COLUMN_PROP_NAME, pspecs[i]->name,
                    COLUMN_PROP_ROW, FALSE,
                    COLUMN_PROP_ADJUSTMENT, adjustment,
                    -1);
        }
    }

    g_free (pspecs);
}

GtkWidget *
egg_property_tree_view_new (GObject *object)
{
    EggPropertyTreeView *property_tree_view;
    GtkTreeView *tree_view;
    GtkTreeViewColumn *prop_column, *value_column;
    GtkCellRenderer *prop_renderer, *value_renderer;
    GtkListStore *list_store;

    property_tree_view = EGG_PROPERTY_TREE_VIEW (g_object_new (EGG_TYPE_PROPERTY_TREE_VIEW, NULL));
    list_store = property_tree_view->priv->list_store;
    tree_view = GTK_TREE_VIEW (property_tree_view);

    egg_property_tree_view_populate_model_with_properties (list_store, object);
    gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (list_store));

    prop_renderer = gtk_cell_renderer_text_new ();
    prop_column = gtk_tree_view_column_new_with_attributes ("Property", prop_renderer,
            "text", COLUMN_PROP_NAME,
            NULL);

    value_renderer = egg_property_cell_renderer_new (object, list_store);
    value_column = gtk_tree_view_column_new_with_attributes ("Value", value_renderer,
            "prop-name", COLUMN_PROP_NAME,
            NULL);

    gtk_tree_view_append_column (tree_view, prop_column);
    gtk_tree_view_append_column (tree_view, value_column);

    return GTK_WIDGET (tree_view);
}

static void
egg_property_tree_view_class_init (EggPropertyTreeViewClass *klass)
{
    g_type_class_add_private (klass, sizeof (EggPropertyTreeViewPrivate));
}

static void
egg_property_tree_view_init (EggPropertyTreeView *tree_view)
{
    EggPropertyTreeViewPrivate *priv = EGG_PROPERTY_TREE_VIEW_GET_PRIVATE (tree_view);

    tree_view->priv = priv = EGG_PROPERTY_TREE_VIEW_GET_PRIVATE (tree_view);
    priv->list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_BOOLEAN, GTK_TYPE_ADJUSTMENT);
}