[gtk+/wip/ebassi/combo: 9/16] Allow changing the 'Custom Entry' label



commit fe94b58dc4c535a8fc4e45c5c354af4f7e74de68
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 5 17:50:36 2015 -0500

    Allow changing the 'Custom Entry' label

 docs/reference/gtk/gtk3-sections.txt |    2 +
 gtk/gtkcombo.c                       |   69 ++++++++++++++++++++++++++++++++-
 gtk/gtkcombo.h                       |    6 +++
 gtk/ui/gtkcombo.ui                   |    5 +-
 tests/testnewcombo.c                 |    1 +
 5 files changed, 78 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 0558f6c..c79ebf5 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -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
diff --git a/gtk/gtkcombo.c b/gtk/gtkcombo.c
index 9d9dd6c..8215397 100644
--- a/gtk/gtkcombo.c
+++ b/gtk/gtkcombo.c
@@ -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");
@@ -1767,6 +1790,45 @@ gtk_combo_get_allow_custom (GtkCombo *combo)
 }
 
 /**
+ * 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
  * @group: a group ID
@@ -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));
 }
+
diff --git a/gtk/gtkcombo.h b/gtk/gtkcombo.h
index 2e88180..417f228 100644
--- a/gtk/gtkcombo.h
+++ b/gtk/gtkcombo.h
@@ -80,6 +80,12 @@ 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,
                                                  const gchar    *text,
diff --git a/gtk/ui/gtkcombo.ui b/gtk/ui/gtkcombo.ui
index 23811d2..b13485e 100644
--- a/gtk/ui/gtkcombo.ui
+++ b/gtk/ui/gtkcombo.ui
@@ -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>
diff --git a/tests/testnewcombo.c b/tests/testnewcombo.c
index 86d9f11..434befc 100644
--- a/tests/testnewcombo.c
+++ b/tests/testnewcombo.c
@@ -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>"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]