]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcomboboxtext.c
Add a way to associate numeric ids with combobox values
[~andy/gtk] / gtk / gtkcomboboxtext.c
index 25400debe52b261a59caa2ed5e68955d67b94973..46767d9f0d827dccec46febf36bdde244498c017 100644 (file)
@@ -72,7 +72,7 @@ gtk_combo_box_text_init (GtkComboBoxText *combo_box)
 {
   GtkListStore *store;
 
-  store = gtk_list_store_new (1, G_TYPE_STRING);
+  store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
   gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
   g_object_unref (store);
 }
@@ -102,6 +102,7 @@ gtk_combo_box_text_new (void)
 {
   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
                        "entry-text-column", 0,
+                       "id-column", 1,
                        NULL);
 }
 
@@ -121,6 +122,7 @@ 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);
 }
 
@@ -137,22 +139,23 @@ void
 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
                                 const gchar     *text)
 {
-  GtkListStore *store;
-  GtkTreeIter iter;
-  gint text_column;
-  gint column_type;
-
-  g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
-  g_return_if_fail (text != NULL);
-
-  store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
-  g_return_if_fail (GTK_IS_LIST_STORE (store));
-  text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
-  column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
-  g_return_if_fail (column_type == G_TYPE_STRING);
+  gtk_combo_box_text_insert_text (combo_box, G_MAXINT, text);
+}
 
-  gtk_list_store_append (store, &iter);
-  gtk_list_store_set (store, &iter, text_column, text, -1);
+/**
+ * gtk_combo_box_text_prepend_text:
+ * @combo_box: A #GtkComboBox
+ * @text: A string
+ *
+ * Prepends @string to the list of strings stored in @combo_box.
+ *
+ * Since: 2.24
+ */
+void
+gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
+                                 const gchar     *text)
+{
+  gtk_combo_box_text_insert_text (combo_box, 0, text);
 }
 
 /**
@@ -170,44 +173,35 @@ gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
                                 gint             position,
                                 const gchar     *text)
 {
-  GtkListStore *store;
-  GtkTreeIter iter;
-  gint text_column;
-  gint column_type;
-
-  g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
-  g_return_if_fail (position >= 0);
-  g_return_if_fail (text != NULL);
-
-  store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
-  g_return_if_fail (GTK_IS_LIST_STORE (store));
-  text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
-  column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
-  g_return_if_fail (column_type == G_TYPE_STRING);
-
-  gtk_list_store_insert (store, &iter, position);
-  gtk_list_store_set (store, &iter, text_column, text, -1);
+  gtk_combo_box_text_insert_text_with_id (combo_box, position, text, 0);
 }
 
 /**
- * gtk_combo_box_text_prepend_text:
- * @combo_box: A #GtkComboBox
+ * gtk_combo_box_text_insert_text_with_id:
+ * @combo_box: A #GtkComboBoxText
+ * @position: An index to insert @text
  * @text: A string
+ * @id: a numeric ID for this value
  *
- * Prepends @string to the list of strings stored in @combo_box.
+ * Inserts @string at @position in the list of strings stored in @combo_box,
+ * and sets its numeric ID to @id. See #GtkComboBox::id-column.
  *
- * Since: 2.24
+ * Since: 3.0
  */
 void
-gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
-                                 const gchar     *text)
+gtk_combo_box_text_insert_text_with_id (GtkComboBoxText *combo_box,
+                                        gint             position,
+                                        const gchar     *text,
+                                        gint             id)
 {
   GtkListStore *store;
   GtkTreeIter iter;
   gint text_column;
+  gint id_column;
   gint column_type;
 
   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
+  g_return_if_fail (position >= 0);
   g_return_if_fail (text != NULL);
 
   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
@@ -215,11 +209,18 @@ gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
   g_return_if_fail (column_type == G_TYPE_STRING);
+  id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
+  if (id_column != -1)
+    {
+      column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
+      g_return_if_fail (column_type == G_TYPE_INT);
+    }
 
-  gtk_list_store_prepend (store, &iter);
-  gtk_list_store_set (store, &iter, text_column, text, -1);
+  gtk_list_store_insert (store, &iter, position);
+  gtk_list_store_set (store, &iter, text_column, text, id_column, id, -1);
 }
 
+
 /**
  * gtk_combo_box_text_remove:
  * @combo_box: A #GtkComboBox