2008-12-09 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-touch-selector-entry.c
index 0cd3b0f..2e20011 100644 (file)
  * @short_description: A selector widget with one column and a text entry
  * @see_also: #HildonTouchSelector, #HildonPickerButton
  *
- * #HildonTouchSelectorEntry is a selector widget with a text entry, similar in behaviour
- * to #GtkComboBoxEntry, that allows user to select an item from a predefined list
- * or to enter a different one in a #GtkEntry. Items can also be searched and selected
- * by typing in the entry.
+ * #HildonTouchSelectorEntry is a selector widget with a text entry, similar in
+ * behaviour to #GtkComboBoxEntry, that allows user to select an item from a
+ * predefined list or to enter a different one in a #GtkEntry. Items can also
+ * be searched and selected by typing in the entry.
  *
  * The main difference between the #GtkTreeModel used by #HildonTouchSelector
- * and #HildonTouchSelectorEntry, is that the latter must always include a text column.
- * You should set it with hildon_touch_selector_entry_set_text_column().
+ * and #HildonTouchSelectorEntry, is that the latter must always include a text
+ * column. You should set it with hildon_touch_selector_entry_set_text_column().
  *
  * Normally, you would use #HildonTouchSelectorEntry together with a
  * #HildonPickerDialog activated from a button. For the most common
@@ -61,8 +61,11 @@ static void hildon_touch_selector_entry_set_model (HildonTouchSelector * selecto
                                                    gint column, GtkTreeModel *model);
 static gboolean hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector);
 
+static void
+_text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data);
+
+
 struct _HildonTouchSelectorEntryPrivate {
-  gint text_column;
   gulong signal_id;
   GtkWidget *entry;
 };
@@ -111,6 +114,14 @@ hildon_touch_selector_entry_class_init (HildonTouchSelectorEntryClass *klass)
   object_class->get_property = hildon_touch_selector_entry_get_property;
   object_class->set_property = hildon_touch_selector_entry_set_property;
 
+  /**
+   * HildonTouchSelectorEntry:text-column:
+   *
+   * Deprecated: now this property is in HildonTouchSelectorColumn use
+   * hildon_touch_selector_entry_set_text_column() and
+   * hildon_touch_selector_entry_get_text_column() to manage this.
+   *
+   **/
   g_object_class_install_property (G_OBJECT_CLASS (klass),
                                    PROP_TEXT_COLUMN,
                                    g_param_spec_int ("text-column",
@@ -126,10 +137,24 @@ static gchar *
 hildon_touch_selector_entry_print_func (HildonTouchSelector * selector)
 {
   HildonTouchSelectorEntryPrivate *priv;
+  GtkTreeModel *model;
+  GtkTreeIter iter;
+  gint column;
+  gchar *text = NULL;
 
   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
 
-  return g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
+  if (*(gtk_entry_get_text (GTK_ENTRY (priv->entry))) != '\0') {
+    text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
+  } else {
+    model = hildon_touch_selector_get_model (selector, 0);
+    if (hildon_touch_selector_get_selected (selector, 0, &iter)) {
+      column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
+      gtk_tree_model_get (model, &iter, column, &text, -1);
+    }
+  }
+
+  return text;
 }
 
 static void
@@ -180,6 +205,7 @@ hildon_touch_selector_entry_new_text (void)
   GtkWidget *selector;
   GtkEntryCompletion *completion;
   HildonTouchSelectorEntryPrivate *priv;
+  HildonTouchSelectorColumn *column = NULL;
 
   selector = hildon_touch_selector_entry_new ();
 
@@ -188,41 +214,81 @@ hildon_touch_selector_entry_new_text (void)
   model = gtk_list_store_new (1, G_TYPE_STRING);
   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
+  column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
+                                                     GTK_TREE_MODEL (model), FALSE);
 
-  hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
-                                            GTK_TREE_MODEL (model), FALSE);
+  g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
+                    selector);
   hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
 
   return selector;
 }
 
+static void
+_text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
+{
+  HildonTouchSelectorEntry *selector;
+  HildonTouchSelectorEntryPrivate *priv;
+  GtkEntryCompletion *completion;
+  gint text_column = -1;
+
+  g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
+  selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
+
+  priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
+  completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
+
+  text_column = hildon_touch_selector_entry_get_text_column (selector);
+
+  gtk_entry_completion_set_text_column (completion, text_column);
+}
+
+/**
+ * hildon_touch_selector_entry_set_text_column:
+ * @selector: A #HildonTouchSelectorEntry
+ * @text_column: A column in model to get the strings from
+ *
+ * Sets the model column which touch selector box should use to get strings
+ * from to be @text_column.
+ *
+ **/
 void
 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
                                              gint text_column)
 {
-  HildonTouchSelectorEntryPrivate *priv;
-  GtkEntryCompletion *completion;
+  HildonTouchSelectorColumn *column = NULL;
 
   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
   g_return_if_fail (text_column >= -1);
 
-  priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
-  completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
+  column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
 
-  gtk_entry_completion_set_text_column (completion, text_column);
-  priv->text_column = text_column;
+  g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
 }
 
+/**
+ * hildon_touch_selector_entry_get_text_column:
+ * @selector: A #HildonTouchSelectorEntry
+ *
+ * Gets the text column that @selector is using as a text column.
+ *
+ * Returns: the number of the column used as a text column.
+ *
+ **/
 gint
 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
 {
-  HildonTouchSelectorEntryPrivate *priv;
+  HildonTouchSelectorColumn *column = NULL;
+  gint text_column = -1;
 
   g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
 
-  priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
+  column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
+                                             0);
 
-  return priv->text_column;
+  g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
+
+  return text_column;
 }
 
 static void
@@ -237,20 +303,24 @@ entry_on_text_changed (GtkEditable * editable,
   const gchar *prefix;
   gchar *text;
   gboolean found = FALSE;
+  gint text_column = -1;
 
   entry = GTK_ENTRY (editable);
   selector = HILDON_TOUCH_SELECTOR (userdata);
   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
 
+  text_column =
+    hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
+
   prefix = gtk_entry_get_text (entry);
-  model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
+  model = hildon_touch_selector_get_model (selector, 0);
 
   if (!gtk_tree_model_get_iter_first (model, &iter)) {
     return;
   }
 
   do {
-    gtk_tree_model_get (model, &iter, priv->text_column, &text, -1);
+    gtk_tree_model_get (model, &iter, text_column, &text, -1);
     found = g_str_has_prefix (text, prefix);
     g_free (text);
   } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
@@ -261,7 +331,7 @@ entry_on_text_changed (GtkEditable * editable,
        GtkEntry represents a change in current selection, and therefore, users
        should be notified. */
     if (found) {
-      hildon_touch_selector_set_active_iter (selector, 0, &iter, TRUE);
+      hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
     }
     g_signal_emit_by_name (selector, "changed", 0);
   }
@@ -282,10 +352,12 @@ hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
   GtkTreePath *path;
   GList *selected_rows;
   gchar *text;
+  gint text_column = -1;
 
   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
 
   model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
+  text_column = hildon_touch_selector_entry_get_text_column (selector);
   selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
 
   if (selected_rows == NULL) {
@@ -297,7 +369,7 @@ hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
 
   path = (GtkTreePath *)selected_rows->data;
   gtk_tree_model_get_iter (model, &iter, path);
-  gtk_tree_model_get (model, &iter, priv->text_column, &text, -1);
+  gtk_tree_model_get (model, &iter, text_column, &text, -1);
 
   gtk_tree_path_free (path);
   g_list_free (selected_rows);
@@ -315,9 +387,11 @@ hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
 
   text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
-  gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
-  gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
-  g_free (text);
+  if (text != NULL) {
+    gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
+    gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
+    g_free (text);
+  }
 }
 
 static void
@@ -326,6 +400,7 @@ hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
 {
   GtkEntryCompletion *completion;
   HildonTouchSelectorEntryPrivate *priv;
+  gint text_column = -1;
 
   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
   g_return_if_fail (column == 0);
@@ -337,7 +412,10 @@ hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
 
   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
   gtk_entry_completion_set_model (completion, model);
-  gtk_entry_completion_set_text_column (completion, priv->text_column);
+
+  text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
+
+  gtk_entry_completion_set_text_column (completion, text_column);
 }
 
 static gboolean