Allow changing the 'Custom Entry' label

This commit is contained in:
Matthias Clasen
2015-01-05 17:50:36 -05:00
committed by Emmanuele Bassi
parent 7d39962359
commit fe94b58dc4
5 changed files with 78 additions and 5 deletions

View File

@@ -1003,6 +1003,8 @@ gtk_combo_get_placeholder_text
gtk_combo_set_placeholder_text
gtk_combo_get_allow_custom
gtk_combo_set_allow_custom
gtk_combo_get_custom_text
gtk_combo_set_custom_text
<SUBSECTION Standard>
GTK_TYPE_COMBO

View File

@@ -447,6 +447,7 @@ struct _GtkCombo
const gchar *active;
gchar *placeholder;
gchar *custom_text;
gboolean allow_custom;
GtkWidget *active_label;
@@ -472,7 +473,8 @@ struct _GtkComboClass
enum {
PROP_ACTIVE = 1,
PROP_PLACEHOLDER_TEXT,
PROP_ALLOW_CUSTOM
PROP_ALLOW_CUSTOM,
PROP_CUSTOM_TEXT
};
static GtkBuildableIface *buildable_parent_iface = NULL;
@@ -505,6 +507,7 @@ gtk_combo_finalize (GObject *object)
GtkCombo *combo = GTK_COMBO (object);
g_free (combo->placeholder);
g_free (combo->custom_text);
G_OBJECT_CLASS (gtk_combo_parent_class)->finalize (object);
}
@@ -528,6 +531,9 @@ gtk_combo_set_property (GObject *object,
case PROP_ALLOW_CUSTOM:
gtk_combo_set_allow_custom (combo, g_value_get_boolean (value));
break;
case PROP_CUSTOM_TEXT:
gtk_combo_set_custom_text (combo, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -552,6 +558,9 @@ gtk_combo_get_property (GObject *object,
case PROP_ALLOW_CUSTOM:
g_value_set_boolean (value, gtk_combo_get_allow_custom (combo));
break;
case PROP_CUSTOM_TEXT:
g_value_set_string (value, gtk_combo_get_custom_text (combo));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -608,6 +617,19 @@ gtk_combo_class_init (GtkComboClass *class)
FALSE,
GTK_PARAM_READWRITE));
/**
* GtkCombo:custom-text:
*
* The text that is displayed for the custom entry.
*/
g_object_class_install_property (object_class,
PROP_CUSTOM_TEXT,
g_param_spec_string ("custom-text",
P_("Custom text"),
P_("Text to show for the custom entry"),
NULL,
GTK_PARAM_READWRITE));
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkcombo.ui");
gtk_widget_class_bind_template_child (widget_class, GtkCombo, active_label);
@@ -1680,7 +1702,7 @@ gtk_combo_remove_item (GtkCombo *combo,
/**
* gtk_combo_set_placeholder_text:
* @combo: a #GtkCombo
* @text: (allow-none): the placeholder text to use, or %NULL
* @text: the placeholder text to use
*
* Sets the placeholder text that is displayed in the combo
* if no item is currently selected.
@@ -1692,11 +1714,12 @@ gtk_combo_set_placeholder_text (GtkCombo *combo,
const gchar *text)
{
g_return_if_fail (GTK_IS_COMBO (combo));
g_return_if_fail (text != NULL);
g_free (combo->placeholder);
combo->placeholder = g_strdup (text);
if (combo->active == NULL)
if (combo->active_label != NULL && combo->active == NULL)
gtk_label_set_text (GTK_LABEL (combo->active_label), combo->placeholder);
g_object_notify (G_OBJECT (combo), "placeholder-text");
@@ -1766,6 +1789,45 @@ gtk_combo_get_allow_custom (GtkCombo *combo)
return combo->allow_custom;
}
/**
* gtk_combo_set_custom_text:
* @combo: a #GtkCombo
* @text: the text to show for the custom entry
*
* Sets the text that is displayed for the custom entry.
*
* Since: 3.16
*/
void
gtk_combo_set_custom_text (GtkCombo *combo,
const gchar *text)
{
g_return_if_fail (GTK_IS_COMBO (combo));
g_free (combo->custom_text);
combo->custom_text = g_strdup (text);
g_object_notify (G_OBJECT (combo), "custom-text");
}
/**
* gtk_combo_get_custom_text:
* @combo: a #GtkCombo
*
* Gets the text that is displayed for the custom entry.
*
* Returns: (transfer none): the custom text
*
* Since: 3.16
*/
const gchar *
gtk_combo_get_custom_text (GtkCombo *combo)
{
g_return_val_if_fail (GTK_IS_COMBO (combo), NULL);
return combo->custom_text;
}
/**
* gtk_combo_add_group:
* @combo: a #GtkCombo
@@ -1801,3 +1863,4 @@ gtk_combo_add_group (GtkCombo *combo,
gtk_list_box_invalidate_filter (GTK_LIST_BOX (combo->list));
gtk_list_box_invalidate_sort (GTK_LIST_BOX (combo->list));
}

View File

@@ -79,6 +79,12 @@ void gtk_combo_set_allow_custom (GtkCombo *combo,
GDK_AVAILABLE_IN_3_16
gboolean gtk_combo_get_allow_custom (GtkCombo *combo);
GDK_AVAILABLE_IN_3_16
void gtk_combo_set_custom_text (GtkCombo *combo,
const gchar *text);
GDK_AVAILABLE_IN_3_16
const gchar * gtk_combo_get_custom_text (GtkCombo *combo);
GDK_AVAILABLE_IN_3_16
void gtk_combo_add_group (GtkCombo *combo,
const gchar *group,

View File

@@ -2,6 +2,7 @@
<interface domain="gtk30">
<!-- interface-requires gtk+ 3.10 -->
<template class="GtkCombo" parent="GtkBin">
<property name="custom-text" translatable="yes">Custom Entry</property>
<child>
<object class="GtkToggleButton" id="button">
<property name="visible">True</property>
@@ -101,7 +102,7 @@
<object class="GtkComboRow" id="add_custom">
<property name="visible">False</property>
<property name="group">custom</property>
<property name="text" translatable="yes">Custom Entry</property>
<property name="text" bind-source="GtkCombo" bind-property="custom-text" bind-flags="sync-create"/>
</object>
</child>
</object>
@@ -126,7 +127,7 @@
<child>
<object class="GtkComboRow" id="back_to_list">
<property name="group">list</property>
<property name="text" translatable="yes">Custom Entry</property>
<property name="text" bind-source="GtkCombo" bind-property="custom-text" bind-flags="sync-create"/>
<property name="inverted">True</property>
</object>
</child>

View File

@@ -43,6 +43,7 @@ const gchar data[] =
" <property name='visible'>True</property>"
" <property name='halign'>center</property>"
" <property name='placeholder-text'>None</property>"
" <property name='custom-text'>Other</property>"
" <property name='active'>1</property>"
" <items>"
" <item translatable='yes' id='1' sort='Value 001'>Value 1</item>"