]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcomboboxtext.c
GtkEntry: Sanity check the end_pos value in _get_display_text()
[~andy/gtk] / gtk / gtkcomboboxtext.c
index 00d75d2f803c0c4dd296bed60cf3ffb4a5403fff..e09f3ef3c490f825f8ba01b25309015b67d1edcd 100644 (file)
  * The entry itself can be accessed by calling gtk_bin_get_child() on the
  * combo box.
  *
+ * You should not call gtk_combo_box_set_model() or attempt to pack more cells
+ * into this combo box via its GtkCellLayout interface.
+ *
  * <refsect2 id="GtkComboBoxText-BUILDER-UI">
  * <title>GtkComboBoxText as GtkBuildable</title>
  * <para>
  * The GtkComboBoxText implementation of the GtkBuildable interface
- * supports adding items directly using the &lt;items&gt element
- * and specifying &lt;item&gt elements for each item. Each &lt;item&gt
+ * supports adding items directly using the &lt;items&gt; element
+ * and specifying &lt;item&gt; elements for each item. Each &lt;item&gt;
  * element supports the regular translation attributes "translatable",
  * "context" and "comments".
  *
@@ -70,7 +73,8 @@
  * </object>
  * ]]></programlisting>
  * </example>
- *
+ * </para>
+ * </refsect2>
  */
 
 static void     gtk_combo_box_text_buildable_interface_init     (GtkBuildableIface *iface);
@@ -98,11 +102,15 @@ gtk_combo_box_text_constructor (GType                  type,
                                 guint                  n_construct_properties,
                                 GObjectConstructParam *construct_properties)
 {
-  GObject            *object;
+  GObject    *object;
+  const gint text_column = 0;
 
   object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
     (type, n_construct_properties, construct_properties);
 
+  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (object), text_column);
+  gtk_combo_box_set_id_column (GTK_COMBO_BOX (object), 1);
+
   if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
     {
       GtkCellRenderer *cell;
@@ -110,7 +118,7 @@ gtk_combo_box_text_constructor (GType                  type,
       cell = gtk_cell_renderer_text_new ();
       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
-                                      "text", 0,
+                                      "text", text_column,
                                       NULL);
     }
 
@@ -149,9 +157,11 @@ typedef struct {
   GtkBuilder    *builder;
   GObject       *object;
   const gchar   *domain;
+  gchar         *id;
+
+  GString       *string;
 
   gchar         *context;
-  gchar         *string;
   guint          translatable : 1;
 
   guint          is_text : 1;
@@ -190,6 +200,8 @@ item_start_element (GMarkupParseContext *context,
            }
          else if (strcmp (names[i], "context") == 0) 
            data->context = g_strdup (values[i]);
+         else if (strcmp (names[i], "id") == 0)
+           data->id = g_strdup (values[i]);
          else
            g_warning ("Unknown custom combo box item attribute: %s", names[i]);
        }
@@ -204,29 +216,9 @@ item_text (GMarkupParseContext *context,
           GError             **error)
 {
   ItemParserData *data = (ItemParserData*)user_data;
-  gchar *string;
-
-  if (!data->is_text)
-    return;
-
-  string = g_strndup (text, text_len);
-
-  if (data->translatable && text_len)
-    {
-      gchar *translated;
-
-      /* FIXME: This will not use the domain set in the .ui file,
-       * since the parser is not telling the builder about the domain.
-       * However, it will work for gtk_builder_set_translation_domain() calls.
-       */
-      translated = _gtk_builder_parser_translate (data->domain,
-                                                 data->context,
-                                                 string);
-      g_free (string);
-      string = translated;
-    }
 
-  data->string = string;
+  if (data->is_text)
+    g_string_append_len (data->string, text, text_len);
 }
 
 static void
@@ -238,14 +230,32 @@ item_end_element (GMarkupParseContext *context,
   ItemParserData *data = (ItemParserData*)user_data;
 
   /* Append the translated strings */
-  if (data->string)
-    gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (data->object), data->string);
+  if (data->string->len)
+    {
+      if (data->translatable)
+       {
+         gchar *translated;
+
+         /* FIXME: This will not use the domain set in the .ui file,
+          * since the parser is not telling the builder about the domain.
+          * However, it will work for gtk_builder_set_translation_domain() calls.
+          */
+         translated = _gtk_builder_parser_translate (data->domain,
+                                                     data->context,
+                                                     data->string->str);
+         g_string_set_size (data->string, 0);
+         g_string_append (data->string, translated);
+       }
+
+      gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (data->object), data->id, data->string->str);
+    }
 
   data->translatable = FALSE;
+  g_string_set_size (data->string, 0);
   g_free (data->context);
-  g_free (data->string);
   data->context = NULL;
-  data->string = NULL;
+  g_free (data->id);
+  data->id = NULL;
   data->is_text = FALSE;
 }
 
@@ -276,6 +286,7 @@ gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable     *buildable,
       parser_data->builder = g_object_ref (builder);
       parser_data->object = g_object_ref (buildable);
       parser_data->domain = gtk_builder_get_translation_domain (builder);
+      parser_data->string = g_string_new ("");
       *parser = item_parser;
       *data = parser_data;
       return TRUE;
@@ -301,6 +312,7 @@ gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
 
       g_object_unref (data->object);
       g_object_unref (data->builder);
+      g_string_free (data->string, TRUE);
       g_slice_free (ItemParserData, data);
     }
 }
@@ -309,7 +321,7 @@ gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
  * gtk_combo_box_text_new:
  *
  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
- * strings. See gtk_combo_box_entry_new_with_text().
+ * strings.
  *
  * Return value: A new #GtkComboBoxText
  *
@@ -319,8 +331,6 @@ GtkWidget *
 gtk_combo_box_text_new (void)
 {
   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
-                       "entry-text-column", 0,
-                       "id-column", 1,
                        NULL);
 }
 
@@ -339,8 +349,6 @@ gtk_combo_box_text_new_with_entry (void)
 {
   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
                        "has-entry", TRUE,
-                       "entry-text-column", 0,
-                       "id-column", 1,
                        NULL);
 }
 
@@ -408,10 +416,11 @@ gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
 /**
  * gtk_combo_box_text_append:
  * @combo_box: A #GtkComboBoxText
+ * @id: (allow-none): a string ID for this value, or %NULL
  * @text: A string
  *
- * Appends @text to the list of strings stored in @combo_box.  If @id is
- * non-%NULL then it is used as the ID of the row.
+ * Appends @text to the list of strings stored in @combo_box.
+ * If @id is non-%NULL then it is used as the ID of the row.
  *
  * This is the same as calling gtk_combo_box_text_insert() with a
  * position of -1.
@@ -432,8 +441,8 @@ gtk_combo_box_text_append (GtkComboBoxText *combo_box,
  * @id: (allow-none): a string ID for this value, or %NULL
  * @text: a string
  *
- * Prepends @text to the list of strings stored in @combo_box.  If @id
- * is non-%NULL then it is used as the ID of the row.
+ * Prepends @text to the list of strings stored in @combo_box.
+ * If @id is non-%NULL then it is used as the ID of the row.
  *
  * This is the same as calling gtk_combo_box_text_insert() with a
  * position of 0.
@@ -458,7 +467,7 @@ gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
  *
  * Inserts @text at @position in the list of strings stored in @combo_box.
  * If @id is non-%NULL then it is used as the ID of the row.  See
- * #GtkComboBox::id-column.
+ * #GtkComboBox:id-column.
  *
  * If @position is negative then @text is appended.
  *
@@ -562,12 +571,13 @@ gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
  * gtk_combo_box_text_get_active_text:
  * @combo_box: A #GtkComboBoxText
  *
- * Returns the currently active string in @combo_box, or %NULL if none
- * is selected. If @combo_box contains an entry, this function will return
- * its contents (which will not necessarily be an item from the list).
+ * Returns the currently active string in @combo_box, or %NULL
+ * if none is selected. If @combo_box contains an entry, this
+ * function will return its contents (which will not necessarily
+ * be an item from the list).
  *
- * Returns: a newly allocated string containing the currently active text.
- *     Must be freed with g_free().
+ * Returns: (transfer full): a newly allocated string containing the
+ *     currently active text. Must be freed with g_free().
  *
  * Since: 2.24
  */
@@ -579,7 +589,14 @@ gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
 
   g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
 
-  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
+ if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo_box)))
+   {
+     GtkWidget *entry;
+
+     entry = gtk_bin_get_child (GTK_BIN (combo_box));
+     text = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
+   }
+  else if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
     {
       GtkTreeModel *model;
       gint text_column;