2014-05-03 01:48:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008-2009 Christian Hammond
|
|
|
|
* Copyright (c) 2008-2009 David Trowbridge
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2014-05-05 03:47:11 +00:00
|
|
|
|
2014-05-03 01:48:33 +00:00
|
|
|
#include "prop-list.h"
|
|
|
|
#include "property-cell-renderer.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLUMN_NAME,
|
|
|
|
COLUMN_VALUE,
|
|
|
|
COLUMN_DEFINED_AT,
|
|
|
|
COLUMN_OBJECT,
|
|
|
|
COLUMN_TOOLTIP,
|
2014-05-08 14:16:58 +00:00
|
|
|
COLUMN_WRITABLE,
|
|
|
|
COLUMN_ATTRIBUTE
|
2014-05-03 01:48:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2014-05-03 19:25:43 +00:00
|
|
|
PROP_WIDGET_TREE,
|
|
|
|
PROP_CHILD_PROPERTIES
|
2014-05-03 01:48:33 +00:00
|
|
|
};
|
|
|
|
|
2014-05-07 03:24:20 +00:00
|
|
|
struct _GtkInspectorPropListPrivate
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
GtkListStore *model;
|
|
|
|
GHashTable *prop_iters;
|
|
|
|
GList *signal_cnxs;
|
|
|
|
GtkWidget *widget_tree;
|
2014-05-05 01:08:36 +00:00
|
|
|
GtkCellRenderer *value_renderer;
|
2014-05-03 19:25:43 +00:00
|
|
|
gboolean child_properties;
|
2014-05-08 14:16:58 +00:00
|
|
|
GtkTreeViewColumn *attribute_column;
|
2014-05-03 01:48:33 +00:00
|
|
|
};
|
|
|
|
|
2014-05-07 03:24:20 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorPropList, gtk_inspector_prop_list, GTK_TYPE_TREE_VIEW)
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
static void
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_init (GtkInspectorPropList *pl)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
2014-05-07 03:24:20 +00:00
|
|
|
pl->priv = gtk_inspector_prop_list_get_instance_private (pl);
|
2014-05-05 01:08:36 +00:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (pl));
|
|
|
|
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (pl->priv->model),
|
|
|
|
COLUMN_NAME,
|
|
|
|
GTK_SORT_ASCENDING);
|
2014-05-03 01:48:33 +00:00
|
|
|
pl->priv->prop_iters = g_hash_table_new_full (g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
NULL,
|
|
|
|
(GDestroyNotify) gtk_tree_iter_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_property (GObject *object,
|
2014-05-05 01:08:36 +00:00
|
|
|
guint param_id,
|
2014-05-03 01:48:33 +00:00
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2014-05-07 03:24:20 +00:00
|
|
|
GtkInspectorPropList *pl = GTK_INSPECTOR_PROP_LIST (object);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDGET_TREE:
|
|
|
|
g_value_take_object (value, pl->priv->widget_tree);
|
|
|
|
break;
|
|
|
|
|
2014-05-03 19:25:43 +00:00
|
|
|
case PROP_CHILD_PROPERTIES:
|
|
|
|
g_value_set_boolean (value, pl->priv->child_properties);
|
|
|
|
break;
|
|
|
|
|
2014-05-03 01:48:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_property (GObject *object,
|
2014-05-05 01:08:36 +00:00
|
|
|
guint param_id,
|
2014-05-03 01:48:33 +00:00
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2014-05-07 03:24:20 +00:00
|
|
|
GtkInspectorPropList *pl = GTK_INSPECTOR_PROP_LIST (object);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDGET_TREE:
|
|
|
|
pl->priv->widget_tree = g_value_get_object (value);
|
2014-05-08 05:17:35 +00:00
|
|
|
g_object_set_data (G_OBJECT (pl->priv->value_renderer), "gtk-inspector-widget-tree", pl->priv->widget_tree);
|
2014-05-03 01:48:33 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-03 19:25:43 +00:00
|
|
|
case PROP_CHILD_PROPERTIES:
|
|
|
|
pl->priv->child_properties = g_value_get_boolean (value);
|
2014-05-08 14:16:58 +00:00
|
|
|
g_object_set (pl->priv->value_renderer,
|
|
|
|
"is-child-property", pl->priv->child_properties,
|
|
|
|
NULL);
|
2014-05-03 19:25:43 +00:00
|
|
|
break;
|
|
|
|
|
2014-05-03 01:48:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_class_init (GtkInspectorPropListClass *klass)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
2014-05-05 01:08:36 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
object_class->get_property = get_property;
|
|
|
|
object_class->set_property = set_property;
|
2014-05-05 01:08:36 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_WIDGET_TREE,
|
|
|
|
g_param_spec_object ("widget-tree", "Widget Tree", "Widget tree",
|
|
|
|
GTK_TYPE_WIDGET, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2014-05-03 19:25:43 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_CHILD_PROPERTIES,
|
|
|
|
g_param_spec_boolean ("child-properties", "Child properties", "Child properties",
|
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2014-05-05 01:08:36 +00:00
|
|
|
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/prop-list.ui");
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorPropList, model);
|
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorPropList, value_renderer);
|
2014-05-08 14:16:58 +00:00
|
|
|
gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorPropList, attribute_column);
|
2014-05-03 01:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_update_prop (GtkInspectorPropList *pl,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
GParamSpec *prop)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
|
|
|
GValue gvalue = {0};
|
2014-05-08 14:16:58 +00:00
|
|
|
gchar *value = NULL;
|
|
|
|
gchar *attribute = NULL;
|
2014-05-03 01:48:33 +00:00
|
|
|
|
2014-05-08 14:16:58 +00:00
|
|
|
g_value_init (&gvalue, prop->value_type);
|
2014-05-03 19:25:43 +00:00
|
|
|
if (pl->priv->child_properties)
|
|
|
|
{
|
|
|
|
GtkWidget *parent;
|
|
|
|
|
|
|
|
parent = gtk_widget_get_parent (GTK_WIDGET (pl->priv->object));
|
|
|
|
gtk_container_child_get_property (GTK_CONTAINER (parent),
|
|
|
|
GTK_WIDGET (pl->priv->object),
|
|
|
|
prop->name, &gvalue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_object_get_property (pl->priv->object, prop->name, &gvalue);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
if (G_VALUE_HOLDS_ENUM (&gvalue))
|
|
|
|
{
|
2014-05-05 01:08:36 +00:00
|
|
|
GEnumClass *enum_class = G_PARAM_SPEC_ENUM (prop)->enum_class;
|
|
|
|
GEnumValue *enum_value = g_enum_get_value (enum_class, g_value_get_enum (&gvalue));
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
value = g_strdup (enum_value->value_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-08 14:16:58 +00:00
|
|
|
value = g_strdup_value_contents (&gvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GTK_IS_CELL_RENDERER (pl->priv->object))
|
|
|
|
{
|
|
|
|
gpointer *area;
|
|
|
|
gint column = -1;
|
|
|
|
|
|
|
|
area = g_object_get_data (pl->priv->object, "gtk-inspector-cell-area");
|
|
|
|
if (area)
|
|
|
|
column = gtk_cell_area_attribute_get_column (GTK_CELL_AREA (area),
|
|
|
|
GTK_CELL_RENDERER (pl->priv->object),
|
|
|
|
prop->name);
|
|
|
|
|
|
|
|
if (column != -1)
|
|
|
|
attribute = g_strdup_printf ("%d", column);
|
2014-05-03 01:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_list_store_set (pl->priv->model, iter,
|
|
|
|
COLUMN_NAME, prop->name,
|
2014-05-08 14:16:58 +00:00
|
|
|
COLUMN_VALUE, value ? value : "",
|
2014-05-03 01:48:33 +00:00
|
|
|
COLUMN_DEFINED_AT, g_type_name (prop->owner_type),
|
|
|
|
COLUMN_OBJECT, pl->priv->object,
|
|
|
|
COLUMN_TOOLTIP, g_param_spec_get_blurb (prop),
|
2014-05-05 01:08:36 +00:00
|
|
|
COLUMN_WRITABLE, (prop->flags & G_PARAM_WRITABLE) != 0,
|
2014-05-08 14:16:58 +00:00
|
|
|
COLUMN_ATTRIBUTE, attribute ? attribute : "",
|
2014-05-03 01:48:33 +00:00
|
|
|
-1);
|
|
|
|
|
|
|
|
g_free (value);
|
2014-05-08 14:16:58 +00:00
|
|
|
g_free (attribute);
|
2014-05-03 01:48:33 +00:00
|
|
|
g_value_unset (&gvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_prop_changed_cb (GObject *pspec,
|
|
|
|
GParamSpec *prop,
|
|
|
|
GtkInspectorPropList *pl)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter *iter = g_hash_table_lookup(pl->priv->prop_iters, prop->name);
|
|
|
|
|
|
|
|
if (iter != NULL)
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_update_prop (pl, iter, prop);
|
2014-05-03 01:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_new (GtkWidget *widget_tree,
|
|
|
|
gboolean child_properties)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
2014-05-07 03:24:20 +00:00
|
|
|
g_type_ensure (GTK_TYPE_INSPECTOR_PROPERTY_CELL_RENDERER);
|
2014-05-05 01:08:36 +00:00
|
|
|
|
2014-05-07 03:24:20 +00:00
|
|
|
return g_object_new (GTK_TYPE_INSPECTOR_PROP_LIST,
|
2014-05-05 01:08:36 +00:00
|
|
|
"widget-tree", widget_tree,
|
|
|
|
"child-properties", child_properties,
|
|
|
|
NULL);
|
2014-05-03 01:48:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:43:04 +00:00
|
|
|
gboolean
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_set_object (GtkInspectorPropList *pl,
|
|
|
|
GObject *object)
|
2014-05-03 01:48:33 +00:00
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GParamSpec **props;
|
|
|
|
guint num_properties;
|
|
|
|
guint i;
|
|
|
|
GList *l;
|
|
|
|
|
2014-05-03 20:43:04 +00:00
|
|
|
if (pl->priv->object == object)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-05-03 01:48:33 +00:00
|
|
|
pl->priv->object = object;
|
|
|
|
|
|
|
|
for (l = pl->priv->signal_cnxs; l != NULL; l = l->next)
|
|
|
|
{
|
|
|
|
gulong id = GPOINTER_TO_UINT (l->data);
|
|
|
|
|
|
|
|
if (g_signal_handler_is_connected (object, id))
|
|
|
|
g_signal_handler_disconnect (object, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (pl->priv->signal_cnxs);
|
|
|
|
pl->priv->signal_cnxs = NULL;
|
|
|
|
|
|
|
|
g_hash_table_remove_all (pl->priv->prop_iters);
|
|
|
|
gtk_list_store_clear (pl->priv->model);
|
2014-05-03 19:57:16 +00:00
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (pl), FALSE);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
2014-05-03 19:25:43 +00:00
|
|
|
if (pl->priv->child_properties)
|
|
|
|
{
|
|
|
|
GtkWidget *parent;
|
|
|
|
|
|
|
|
if (!GTK_IS_WIDGET (object))
|
2014-05-03 20:43:04 +00:00
|
|
|
return TRUE;
|
2014-05-03 19:25:43 +00:00
|
|
|
|
|
|
|
parent = gtk_widget_get_parent (GTK_WIDGET (object));
|
|
|
|
if (!parent)
|
2014-05-03 20:43:04 +00:00
|
|
|
return TRUE;
|
2014-05-03 19:25:43 +00:00
|
|
|
|
|
|
|
props = gtk_container_class_list_child_properties (G_OBJECT_GET_CLASS (parent), &num_properties);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
props = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &num_properties);
|
|
|
|
|
2014-05-03 19:57:16 +00:00
|
|
|
gtk_widget_set_sensitive (GTK_WIDGET (pl), TRUE);
|
|
|
|
|
2014-05-03 01:48:33 +00:00
|
|
|
for (i = 0; i < num_properties; i++)
|
|
|
|
{
|
|
|
|
GParamSpec *prop = props[i];
|
2014-05-05 01:08:36 +00:00
|
|
|
gchar *signal_name;
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
if (! (prop->flags & G_PARAM_READABLE))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gtk_list_store_append (pl->priv->model, &iter);
|
2014-05-07 03:24:20 +00:00
|
|
|
gtk_inspector_prop_list_update_prop (pl, &iter, prop);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
g_hash_table_insert (pl->priv->prop_iters, (gpointer) prop->name, gtk_tree_iter_copy (&iter));
|
|
|
|
|
|
|
|
/* Listen for updates */
|
2014-05-03 19:25:43 +00:00
|
|
|
if (pl->priv->child_properties)
|
|
|
|
signal_name = g_strdup_printf ("child-notify::%s", prop->name);
|
|
|
|
else
|
|
|
|
signal_name = g_strdup_printf ("notify::%s", prop->name);
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
pl->priv->signal_cnxs =
|
2014-05-05 01:08:36 +00:00
|
|
|
g_list_prepend (pl->priv->signal_cnxs,
|
2014-05-08 14:16:58 +00:00
|
|
|
GINT_TO_POINTER (g_signal_connect (object, signal_name,
|
|
|
|
G_CALLBACK (gtk_inspector_prop_list_prop_changed_cb), pl)));
|
2014-05-03 01:48:33 +00:00
|
|
|
|
|
|
|
g_free (signal_name);
|
|
|
|
}
|
2014-05-03 20:43:04 +00:00
|
|
|
|
2014-05-08 14:16:58 +00:00
|
|
|
g_object_set (pl->priv->attribute_column,
|
|
|
|
"visible", !pl->priv->child_properties && GTK_IS_CELL_RENDERER (object),
|
|
|
|
NULL);
|
2014-05-03 20:43:04 +00:00
|
|
|
return TRUE;
|
2014-05-03 01:48:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 01:08:36 +00:00
|
|
|
// vim: set et sw=2 ts=2:
|