Add more katze_object_get_foo functions

This commit is contained in:
Christian Dywan 2008-11-18 22:59:19 +01:00
parent 79cbcd0886
commit 1e6c22d94a
2 changed files with 171 additions and 29 deletions

View file

@ -139,36 +139,47 @@ katze_property_proxy (gpointer object,
const gchar* property, const gchar* property,
const gchar* hint) const gchar* hint)
{ {
GObjectClass* class;
GParamSpec* pspec;
GType type;
const gchar* nick;
const gchar* _hint;
GtkWidget* widget;
gchar* string;
g_return_val_if_fail (G_IS_OBJECT (object), NULL); g_return_val_if_fail (G_IS_OBJECT (object), NULL);
GObjectClass* class = G_OBJECT_GET_CLASS (object);
GParamSpec* pspec = g_object_class_find_property (class, property); class = G_OBJECT_GET_CLASS (object);
pspec = g_object_class_find_property (class, property);
if (!pspec) if (!pspec)
{ {
g_warning (_("Property '%s' is invalid for %s"), g_warning (_("Property '%s' is invalid for %s"),
property, G_OBJECT_CLASS_NAME (class)); property, G_OBJECT_CLASS_NAME (class));
return gtk_label_new (property); return gtk_label_new (property);
} }
GType type = G_PARAM_SPEC_TYPE (pspec);
const gchar* nick = g_param_spec_get_nick (pspec); type = G_PARAM_SPEC_TYPE (pspec);
const gchar* _hint = g_intern_string (hint); nick = g_param_spec_get_nick (pspec);
_hint = g_intern_string (hint);
if (_hint == g_intern_string ("blurb")) if (_hint == g_intern_string ("blurb"))
nick = g_param_spec_get_blurb (pspec); nick = g_param_spec_get_blurb (pspec);
GtkWidget* widget; string = NULL;
gchar* string = NULL;
if (type == G_TYPE_PARAM_BOOLEAN) if (type == G_TYPE_PARAM_BOOLEAN)
{ {
gboolean toggled = katze_object_get_boolean (object, property);
widget = gtk_check_button_new_with_label (gettext (nick)); widget = gtk_check_button_new_with_label (gettext (nick));
gboolean toggled;
g_object_get (object, property, &toggled, NULL);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), toggled); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), toggled);
g_signal_connect (widget, "toggled", g_signal_connect (widget, "toggled",
G_CALLBACK (proxy_toggle_button_toggled_cb), object); G_CALLBACK (proxy_toggle_button_toggled_cb), object);
} }
else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("file")) else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("file"))
{ {
string = katze_object_get_string (object, property);
widget = gtk_file_chooser_button_new (_("Choose file"), widget = gtk_file_chooser_button_new (_("Choose file"),
GTK_FILE_CHOOSER_ACTION_OPEN); GTK_FILE_CHOOSER_ACTION_OPEN);
g_object_get (object, property, &string, NULL);
if (!string) if (!string)
string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value); string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value);
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (widget), gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (widget),
@ -184,9 +195,10 @@ katze_property_proxy (gpointer object,
} }
else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("folder")) else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("folder"))
{ {
string = katze_object_get_string (object, property);
widget = gtk_file_chooser_button_new (_("Choose folder"), widget = gtk_file_chooser_button_new (_("Choose folder"),
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER); GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
g_object_get (object, property, &string, NULL);
if (!string) if (!string)
string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value); string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
@ -202,9 +214,10 @@ katze_property_proxy (gpointer object,
} }
else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("uri")) else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("uri"))
{ {
string = katze_object_get_string (object, property);
widget = gtk_file_chooser_button_new (_("Choose file"), widget = gtk_file_chooser_button_new (_("Choose file"),
GTK_FILE_CHOOSER_ACTION_OPEN); GTK_FILE_CHOOSER_ACTION_OPEN);
g_object_get (object, property, &string, NULL);
if (!string) if (!string)
string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value); string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value);
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (widget), gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (widget),
@ -220,22 +233,22 @@ katze_property_proxy (gpointer object,
} }
else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("font")) else if (type == G_TYPE_PARAM_STRING && _hint == g_intern_string ("font"))
{ {
widget = gtk_combo_box_new_text (); int n_families, i;
PangoContext* context = gtk_widget_get_pango_context (widget); PangoContext* context;
PangoFontFamily** families; PangoFontFamily** families;
int n_families; string = katze_object_get_string (object, property);
widget = gtk_combo_box_new_text ();
context = gtk_widget_get_pango_context (widget);
pango_context_list_families (context, &families, &n_families); pango_context_list_families (context, &families, &n_families);
g_object_get (object, property, &string, NULL);
if (!string) if (!string)
string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value); string = g_strdup (G_PARAM_SPEC_STRING (pspec)->default_value);
gint i = 0; for (i = 0; i < n_families; i++)
while (i < n_families)
{ {
const gchar* font = pango_font_family_get_name (families[i]); const gchar* font = pango_font_family_get_name (families[i]);
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), font); gtk_combo_box_append_text (GTK_COMBO_BOX (widget), font);
if (string && !strcmp (font, string)) if (string && !strcmp (font, string))
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i); gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
i++;
} }
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE ( gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (
gtk_combo_box_get_model (GTK_COMBO_BOX (widget))), gtk_combo_box_get_model (GTK_COMBO_BOX (widget))),
@ -258,45 +271,44 @@ katze_property_proxy (gpointer object,
} }
else if (type == G_TYPE_PARAM_FLOAT) else if (type == G_TYPE_PARAM_FLOAT)
{ {
gfloat value = katze_object_get_float (object, property);
widget = gtk_spin_button_new_with_range ( widget = gtk_spin_button_new_with_range (
G_PARAM_SPEC_FLOAT (pspec)->minimum, G_PARAM_SPEC_FLOAT (pspec)->minimum,
G_PARAM_SPEC_FLOAT (pspec)->maximum, 1); G_PARAM_SPEC_FLOAT (pspec)->maximum, 1);
/* Keep it narrow, 5 + 2 digits are usually fine */ /* Keep it narrow, 5 + 2 digits are usually fine */
gtk_entry_set_width_chars (GTK_ENTRY (widget), 5 + 2); gtk_entry_set_width_chars (GTK_ENTRY (widget), 5 + 2);
gtk_spin_button_set_digits (GTK_SPIN_BUTTON (widget), 2); gtk_spin_button_set_digits (GTK_SPIN_BUTTON (widget), 2);
gfloat value;
g_object_get (object, property, &value, NULL);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value); gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
g_signal_connect (widget, "value-changed", g_signal_connect (widget, "value-changed",
G_CALLBACK (proxy_spin_button_changed_cb), object); G_CALLBACK (proxy_spin_button_changed_cb), object);
} }
else if (type == G_TYPE_PARAM_INT) else if (type == G_TYPE_PARAM_INT)
{ {
gint value = katze_object_get_int (object, property);
widget = gtk_spin_button_new_with_range ( widget = gtk_spin_button_new_with_range (
G_PARAM_SPEC_INT (pspec)->minimum, G_PARAM_SPEC_INT (pspec)->minimum,
G_PARAM_SPEC_INT (pspec)->maximum, 1); G_PARAM_SPEC_INT (pspec)->maximum, 1);
/* Keep it narrow, 5 digits are usually fine */ /* Keep it narrow, 5 digits are usually fine */
gtk_entry_set_width_chars (GTK_ENTRY (widget), 5); gtk_entry_set_width_chars (GTK_ENTRY (widget), 5);
gint value;
g_object_get (object, property, &value, NULL);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value); gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
g_signal_connect (widget, "value-changed", g_signal_connect (widget, "value-changed",
G_CALLBACK (proxy_spin_button_changed_cb), object); G_CALLBACK (proxy_spin_button_changed_cb), object);
} }
else if (type == G_TYPE_PARAM_ENUM) else if (type == G_TYPE_PARAM_ENUM)
{ {
gint i;
GEnumClass* enum_class = G_ENUM_CLASS ( GEnumClass* enum_class = G_ENUM_CLASS (
g_type_class_ref (pspec->value_type)); g_type_class_ref (pspec->value_type));
gint value = katze_object_get_enum (object, property);
widget = gtk_combo_box_new_text (); widget = gtk_combo_box_new_text ();
gint i = 0; for (i = 0; i < enum_class->n_values; i++)
while (i < enum_class->n_values)
{ {
const gchar* label = gettext (enum_class->values[i].value_nick); const gchar* label = gettext (enum_class->values[i].value_nick);
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), label); gtk_combo_box_append_text (GTK_COMBO_BOX (widget), label);
i++;
} }
gint value;
g_object_get (object, property, &value, NULL);
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), value); gtk_combo_box_set_active (GTK_COMBO_BOX (widget), value);
g_signal_connect (widget, "changed", g_signal_connect (widget, "changed",
G_CALLBACK (proxy_combo_box_changed_cb), object); G_CALLBACK (proxy_combo_box_changed_cb), object);
@ -518,6 +530,116 @@ katze_pixbuf_new_from_buffer (const guchar* buffer,
return pixbuf; return pixbuf;
} }
/**
* katze_object_get_boolean:
* @object: a #GObject
* @property_name: the name of the property to get
*
* Retrieve the boolean value of the specified property.
*
* Return value: a boolean
**/
gboolean
katze_object_get_boolean (gpointer object,
const gchar* property)
{
gboolean value = FALSE;
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
/* FIXME: Check value type */
g_object_get (object, property, &value, NULL);
return value;
}
/**
* katze_object_get_int:
* @object: a #GObject
* @property_name: the name of the property to get
*
* Retrieve the integer value of the specified property.
*
* Return value: an integer
**/
gint
katze_object_get_int (gpointer object,
const gchar* property)
{
gint value = -1;
g_return_val_if_fail (G_IS_OBJECT (object), -1);
/* FIXME: Check value type */
g_object_get (object, property, &value, NULL);
return value;
}
/**
* katze_object_get_float:
* @object: a #GObject
* @property_name: the name of the property to get
*
* Retrieve the float value of the specified property.
*
* Return value: a float
**/
gfloat
katze_object_get_float (gpointer object,
const gchar* property)
{
gfloat value = -1.0f;
g_return_val_if_fail (G_IS_OBJECT (object), -1.0f);
/* FIXME: Check value type */
g_object_get (object, property, &value, NULL);
return value;
}
/**
* katze_object_get_enum:
* @object: a #GObject
* @property_name: the name of the property to get
*
* Retrieve the enum value of the specified property.
*
* Return value: an enum
**/
gint
katze_object_get_enum (gpointer object,
const gchar* property)
{
gint value = -1;
g_return_val_if_fail (G_IS_OBJECT (object), -1);
/* FIXME: Check value type */
g_object_get (object, property, &value, NULL);
return value;
}
/**
* katze_object_get_string:
* @object: a #GObject
* @property_name: the name of the property to get
*
* Retrieve the string value of the specified property.
*
* Return value: a float
**/
gchar*
katze_object_get_string (gpointer object,
const gchar* property)
{
gchar* value = NULL;
g_return_val_if_fail (G_IS_OBJECT (object), NULL);
/* FIXME: Check value type */
g_object_get (object, property, &value, NULL);
return value;
}
/** /**
* katze_object_get_object: * katze_object_get_object:
* @object: a #GObject * @object: a #GObject
@ -525,7 +647,7 @@ katze_pixbuf_new_from_buffer (const guchar* buffer,
* *
* Retrieve the object value of the specified property. * Retrieve the object value of the specified property.
* *
* Return value: Return value: an object * Return value: an object
**/ **/
gpointer gpointer
katze_object_get_object (gpointer object, katze_object_get_object (gpointer object,

View file

@ -84,6 +84,26 @@ katze_pixbuf_new_from_buffer (const guchar* buffer,
const gchar* mime_type, const gchar* mime_type,
GError** error); GError** error);
gint
katze_object_get_boolean (gpointer object,
const gchar* property);
gint
katze_object_get_int (gpointer object,
const gchar* property);
gfloat
katze_object_get_float (gpointer object,
const gchar* property);
gint
katze_object_get_enum (gpointer object,
const gchar* property);
gchar*
katze_object_get_string (gpointer object,
const gchar* property);
gpointer gpointer
katze_object_get_object (gpointer object, katze_object_get_object (gpointer object,
const gchar* property); const gchar* property);