gtk/modules/inspector/property-cell-renderer.c

467 lines
15 KiB
C
Raw Normal View History

/*
* 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
#include "property-cell-renderer.h"
#include "widget-tree.h"
struct _GtkInspectorPropertyCellRendererPrivate
{
GObject *object;
char *name;
2014-05-03 19:25:43 +00:00
gboolean is_child_property;
};
enum
{
PROP_0,
PROP_OBJECT,
2014-05-03 19:25:43 +00:00
PROP_NAME,
PROP_IS_CHILD_PROPERTY
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorPropertyCellRenderer, gtk_inspector_property_cell_renderer, GTK_TYPE_CELL_RENDERER_TEXT);
static void
gtk_inspector_property_cell_renderer_init(GtkInspectorPropertyCellRenderer *renderer)
{
renderer->priv = gtk_inspector_property_cell_renderer_get_instance_private (renderer);
}
static void
2014-05-05 04:06:07 +00:00
get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (object);
switch (param_id)
{
2014-05-05 04:06:07 +00:00
case PROP_OBJECT:
g_value_set_object(value, r->priv->object);
break;
2014-05-05 04:06:07 +00:00
case PROP_NAME:
g_value_set_string(value, r->priv->name);
break;
2014-05-05 04:06:07 +00:00
case PROP_IS_CHILD_PROPERTY:
g_value_set_boolean (value, r->priv->is_child_property);
break;
2014-05-03 19:25:43 +00:00
2014-05-05 04:06:07 +00:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
break;
}
}
static void
2014-05-05 04:06:07 +00:00
set_property (GObject *object,
guint param_id,
const GValue *value,
2014-05-05 04:06:07 +00:00
GParamSpec *pspec)
{
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (object);
switch (param_id)
{
2014-05-05 04:06:07 +00:00
case PROP_OBJECT:
r->priv->object = g_value_get_object (value);
break;
case PROP_NAME:
g_free (r->priv->name);
r->priv->name = g_value_dup_string (value);
break;
case PROP_IS_CHILD_PROPERTY:
r->priv->is_child_property = g_value_get_boolean (value);
g_object_notify (object, "is-child-property");
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
break;
}
}
2014-05-03 19:25:43 +00:00
static GParamSpec *
find_property (GtkCellRenderer *renderer)
{
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (renderer);
2014-05-03 19:25:43 +00:00
if (r->priv->is_child_property)
{
GtkWidget *parent;
parent = gtk_widget_get_parent (GTK_WIDGET (r->priv->object));
return gtk_container_class_find_child_property (G_OBJECT_GET_CLASS (parent), r->priv->name);
}
return g_object_class_find_property (G_OBJECT_GET_CLASS (r->priv->object), r->priv->name);
}
static void
get_value (GtkCellRenderer *renderer,
GValue *gvalue)
{
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (renderer);
2014-05-03 19:25:43 +00:00
if (r->priv->is_child_property)
{
GtkWidget *widget;
GtkWidget *parent;
widget = GTK_WIDGET (r->priv->object);
parent = gtk_widget_get_parent (widget);
gtk_container_child_get_property (GTK_CONTAINER (parent), widget, r->priv->name, gvalue);
}
else
g_object_get_property (r->priv->object, r->priv->name, gvalue);
}
static void
set_value (GtkCellRenderer *renderer,
GValue *gvalue)
{
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (renderer);
2014-05-03 19:25:43 +00:00
if (r->priv->is_child_property)
{
GtkWidget *widget;
GtkWidget *parent;
widget = GTK_WIDGET (r->priv->object);
parent = gtk_widget_get_parent (widget);
gtk_container_child_set_property (GTK_CONTAINER (parent), widget, r->priv->name, gvalue);
}
else
g_object_set_property (r->priv->object, r->priv->name, gvalue);
}
static void
2014-05-05 04:06:07 +00:00
stop_editing (GtkCellEditable *editable,
GtkCellRenderer *renderer)
{
2014-05-05 04:06:07 +00:00
GValue gvalue = {0};
GParamSpec *prop;
2014-05-05 04:06:07 +00:00
prop = find_property (renderer);
g_value_init(&gvalue, prop->value_type);
2014-05-05 04:06:07 +00:00
if (GTK_IS_ENTRY(editable))
{
2014-05-05 04:06:07 +00:00
gboolean canceled;
g_object_get (editable, "editing-canceled", &canceled, NULL);
2014-05-05 04:06:07 +00:00
gtk_cell_renderer_stop_editing (renderer, canceled);
2014-05-05 04:06:07 +00:00
if (canceled)
return;
if (GTK_IS_SPIN_BUTTON(editable))
{
2014-05-05 04:06:07 +00:00
gdouble value = g_ascii_strtod (gtk_entry_get_text (GTK_ENTRY (editable)), NULL);
if (G_IS_PARAM_SPEC_INT (prop))
g_value_set_int (&gvalue, (gint)value);
else if G_IS_PARAM_SPEC_UINT (prop)
g_value_set_uint (&gvalue, (guint)value);
else if G_IS_PARAM_SPEC_INT64 (prop)
g_value_set_int64 (&gvalue, (gint64)value);
else if G_IS_PARAM_SPEC_UINT64 (prop)
g_value_set_uint64 (&gvalue, (guint64)value);
else if G_IS_PARAM_SPEC_LONG (prop)
g_value_set_long (&gvalue, (glong)value);
else if G_IS_PARAM_SPEC_ULONG (prop)
g_value_set_ulong (&gvalue, (gulong)value);
else if G_IS_PARAM_SPEC_DOUBLE (prop)
g_value_set_double (&gvalue, (gdouble)value);
else
return;
}
2014-05-05 04:06:07 +00:00
else
{
2014-05-05 04:06:07 +00:00
g_value_set_string (&gvalue, gtk_entry_get_text (GTK_ENTRY (editable)));
}
}
2014-05-05 04:06:07 +00:00
else if (GTK_IS_COMBO_BOX (editable))
{
2014-05-05 04:06:07 +00:00
/* We have no way of getting the canceled state for a GtkComboBox */
gtk_cell_renderer_stop_editing (renderer, FALSE);
2014-05-05 04:06:07 +00:00
if (G_IS_PARAM_SPEC_BOOLEAN (prop))
{
2014-05-05 04:06:07 +00:00
g_value_set_boolean (&gvalue, gtk_combo_box_get_active (GTK_COMBO_BOX (editable)) == 1);
}
2014-05-05 04:06:07 +00:00
else if (G_IS_PARAM_SPEC_ENUM (prop))
{
2014-05-05 04:06:07 +00:00
gchar *enum_name = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (editable));
GEnumClass *enum_class;
GEnumValue *enum_value;
2014-05-05 04:06:07 +00:00
if (enum_name == NULL)
return;
2014-05-05 04:06:07 +00:00
enum_class = G_PARAM_SPEC_ENUM (prop)->enum_class;
enum_value = g_enum_get_value_by_name (enum_class, enum_name);
g_value_set_enum (&gvalue, enum_value->value);
2014-05-05 04:06:07 +00:00
g_free (enum_name);
}
}
2014-05-05 04:06:07 +00:00
set_value (renderer, &gvalue);
g_value_unset (&gvalue);
}
static GtkCellEditable *
2014-05-05 04:06:07 +00:00
start_editing (GtkCellRenderer *renderer,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
const GdkRectangle *background_area,
const GdkRectangle *cell_area,
GtkCellRendererState flags)
{
2014-05-05 04:06:07 +00:00
PangoFontDescription *font_desc;
GtkCellEditable *editable = NULL;
GValue gvalue = {0};
GParamSpec *prop;
GtkInspectorPropertyCellRenderer *r = GTK_INSPECTOR_PROPERTY_CELL_RENDERER (renderer);
2014-05-05 04:06:07 +00:00
prop = find_property (renderer);
2014-05-05 04:06:07 +00:00
g_value_init (&gvalue, prop->value_type);
get_value (renderer, &gvalue);
if (G_VALUE_HOLDS_OBJECT (&gvalue))
{
GtkInspectorWidgetTree *widget_tree = g_object_get_data (G_OBJECT (renderer), "gtk-inspector-widget-tree");
2014-05-05 04:06:07 +00:00
GObject *prop_object = g_value_get_object (&gvalue);
GtkTreeIter iter;
2014-05-05 04:06:07 +00:00
if (prop_object)
{
2014-05-05 04:06:07 +00:00
/* First check if the value is already in the tree (happens with 'parent' for instance) */
if (gtk_inspector_widget_tree_find_object (widget_tree, prop_object, &iter))
2014-05-05 04:06:07 +00:00
{
gtk_inspector_widget_tree_select_object (widget_tree, prop_object);
2014-05-05 04:06:07 +00:00
}
else if (gtk_inspector_widget_tree_find_object (widget_tree, r->priv->object, &iter))
2014-05-05 04:06:07 +00:00
{
gtk_inspector_widget_tree_append_object (widget_tree, prop_object, &iter, prop->name);
gtk_inspector_widget_tree_select_object (widget_tree, prop_object);
2014-05-05 04:06:07 +00:00
}
else
{
g_warning ("GtkInspector: couldn't find the widget in the tree");
2014-05-05 04:06:07 +00:00
}
}
2014-05-05 04:06:07 +00:00
g_value_unset (&gvalue);
return NULL;
}
else
{
if (!(prop->flags & G_PARAM_WRITABLE))
return NULL;
if (G_VALUE_HOLDS_ENUM (&gvalue) || G_VALUE_HOLDS_BOOLEAN (&gvalue))
{
2014-05-05 04:06:07 +00:00
GtkWidget *combobox;
GList *renderers;
2014-05-05 04:06:07 +00:00
combobox = gtk_combo_box_text_new ();
gtk_widget_show (combobox);
g_object_set (G_OBJECT (combobox), "has-frame", FALSE, NULL);
2014-05-05 04:06:07 +00:00
if (G_VALUE_HOLDS_BOOLEAN (&gvalue))
{
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combobox), "FALSE");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combobox), "TRUE");
2014-05-05 04:06:07 +00:00
gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), g_value_get_boolean (&gvalue) ? 1 : 0);
}
2014-05-05 04:06:07 +00:00
else if (G_VALUE_HOLDS_ENUM(&gvalue))
{
gint value = g_value_get_enum (&gvalue);
GEnumClass *enum_class = G_PARAM_SPEC_ENUM (prop)->enum_class;
guint i;
2014-05-05 04:06:07 +00:00
for (i = 0; i < enum_class->n_values; i++)
{
GEnumValue *enum_value = &enum_class->values[i];
2014-05-05 04:06:07 +00:00
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combobox),
enum_value->value_name);
2014-05-05 04:06:07 +00:00
if (enum_value->value == value)
gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), i);
}
}
2014-05-05 04:06:07 +00:00
renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (combobox));
g_object_set (G_OBJECT (renderers->data), "scale", 0.8, NULL);
g_list_free (renderers);
2014-05-05 04:06:07 +00:00
editable = GTK_CELL_EDITABLE (combobox);
}
2014-05-05 04:06:07 +00:00
else if (G_VALUE_HOLDS_STRING (&gvalue))
{
2014-05-05 04:06:07 +00:00
GtkWidget *entry;
entry = gtk_entry_new ();
gtk_widget_show (entry);
gtk_entry_set_text (GTK_ENTRY (entry), g_value_get_string (&gvalue));
editable = GTK_CELL_EDITABLE (entry);
}
2014-05-05 04:06:07 +00:00
else if (G_VALUE_HOLDS_INT (&gvalue) ||
G_VALUE_HOLDS_UINT (&gvalue) ||
G_VALUE_HOLDS_INT64 (&gvalue) ||
G_VALUE_HOLDS_UINT64 (&gvalue) ||
G_VALUE_HOLDS_LONG (&gvalue) ||
G_VALUE_HOLDS_ULONG (&gvalue) ||
G_VALUE_HOLDS_DOUBLE (&gvalue))
{
2014-05-05 04:06:07 +00:00
gdouble min, max, value;
GtkWidget *spinbutton;
guint digits = 0;
2014-05-05 04:06:07 +00:00
if (G_VALUE_HOLDS_INT (&gvalue))
{
GParamSpecInt *paramspec = G_PARAM_SPEC_INT (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_int (&gvalue);
}
else if (G_VALUE_HOLDS_UINT (&gvalue))
{
GParamSpecUInt *paramspec = G_PARAM_SPEC_UINT (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_uint (&gvalue);
}
else if (G_VALUE_HOLDS_INT64 (&gvalue))
{
GParamSpecInt64 *paramspec = G_PARAM_SPEC_INT64 (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_int64 (&gvalue);
}
else if (G_VALUE_HOLDS_UINT64 (&gvalue))
{
GParamSpecUInt64 *paramspec = G_PARAM_SPEC_UINT64 (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_uint64 (&gvalue);
}
else if (G_VALUE_HOLDS_LONG (&gvalue))
{
GParamSpecLong *paramspec = G_PARAM_SPEC_LONG (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_long (&gvalue);
}
else if (G_VALUE_HOLDS_ULONG (&gvalue))
{
GParamSpecULong *paramspec = G_PARAM_SPEC_ULONG (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_ulong (&gvalue);
}
else if (G_VALUE_HOLDS_DOUBLE (&gvalue))
{
GParamSpecDouble *paramspec = G_PARAM_SPEC_DOUBLE (prop);
min = paramspec->minimum;
max = paramspec->maximum;
value = g_value_get_double (&gvalue);
digits = 2;
}
else
{
/* Shouldn't really be able to happen. */
return NULL;
}
spinbutton = gtk_spin_button_new_with_range (min, max, 1);
gtk_widget_show (spinbutton);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), value);
gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), digits);
2014-05-05 04:06:07 +00:00
editable = GTK_CELL_EDITABLE(spinbutton);
}
}
2014-05-05 04:06:07 +00:00
g_value_unset (&gvalue);
2014-05-05 04:06:07 +00:00
if (!editable)
return NULL;
2014-05-05 04:06:07 +00:00
font_desc = pango_font_description_new ();
pango_font_description_set_size (font_desc, 8 * PANGO_SCALE);
gtk_widget_override_font (GTK_WIDGET (editable), font_desc);
pango_font_description_free (font_desc);
g_signal_connect (editable, "editing-done", G_CALLBACK (stop_editing), renderer);
2014-05-05 04:06:07 +00:00
return editable;
}
static void
gtk_inspector_property_cell_renderer_class_init (GtkInspectorPropertyCellRendererClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
object_class->get_property = get_property;
object_class->set_property = set_property;
cell_class->start_editing = start_editing;
2014-05-05 04:06:07 +00:00
g_object_class_install_property (object_class, PROP_OBJECT,
g_param_spec_object ("object", "Object", "The object owning the property",
G_TYPE_OBJECT, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string ("name", "Name", "The property name",
NULL, G_PARAM_READWRITE));
2014-05-03 19:25:43 +00:00
g_object_class_install_property (object_class, PROP_IS_CHILD_PROPERTY,
g_param_spec_boolean ("is-child-property", "Child property", "Child property",
FALSE, G_PARAM_READWRITE));
}
GtkCellRenderer *
gtk_inspector_property_cell_renderer_new (void)
{
return g_object_new (GTK_TYPE_INSPECTOR_PROPERTY_CELL_RENDERER, NULL);
}
2014-05-05 04:06:07 +00:00
// vim: set et ts=2: