2008-12-12 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-touch-selector-entry.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version. or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /**
22  * SECTION:hildon-touch-selector-entry
23  * @short_description: A selector widget with one column and a text entry
24  * @see_also: #HildonTouchSelector, #HildonPickerButton
25  *
26  * #HildonTouchSelectorEntry is a selector widget with a text entry, similar in
27  * behaviour to #GtkComboBoxEntry, that allows user to select an item from a
28  * predefined list or to enter a different one in a #HildonEntry. Items can also
29  * be searched and selected by typing in the entry.
30  *
31  * The main difference between the #GtkTreeModel used by #HildonTouchSelector
32  * and #HildonTouchSelectorEntry, is that the latter must always include a text
33  * column. You should set it with hildon_touch_selector_entry_set_text_column().
34  *
35  * Normally, you would use #HildonTouchSelectorEntry together with a
36  * #HildonPickerDialog activated from a button. For the most common
37  * cases, you should use #HildonPickerButton.
38  *
39  * If you only need a text only, one column selector, you can create it with
40  * hildon_touch_selector_entry_new_text() and populate it with
41  * hildon_touch_selector_append_text(), hildon_touch_selector_prepend_text(),
42  * and hildon_touch_selector_insert_text().
43  *
44  */
45
46 #include "hildon-touch-selector.h"
47 #include "hildon-touch-selector-entry.h"
48 #include "hildon-entry.h"
49
50 G_DEFINE_TYPE (HildonTouchSelectorEntry, hildon_touch_selector_entry, HILDON_TYPE_TOUCH_SELECTOR)
51
52 #define HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE(o) \
53   (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_TOUCH_SELECTOR_ENTRY, HildonTouchSelectorEntryPrivate))
54
55 typedef struct _HildonTouchSelectorEntryPrivate HildonTouchSelectorEntryPrivate;
56
57 static void entry_on_text_changed (GtkEditable * editable, gpointer userdata);
58 static void hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
59                                                  gint column,
60                                                  gpointer user_data);
61 static void hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
62                                                    gint column, GtkTreeModel *model);
63 static gboolean hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector);
64
65 static void
66 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data);
67
68
69 struct _HildonTouchSelectorEntryPrivate {
70   gulong signal_id;
71   GtkWidget *entry;
72 };
73
74 enum {
75   PROP_TEXT_COLUMN = 1
76 };
77
78 static void
79 hildon_touch_selector_entry_get_property (GObject *object, guint property_id,
80                                           GValue *value, GParamSpec *pspec)
81 {
82   switch (property_id) {
83   case PROP_TEXT_COLUMN:
84     g_value_set_int (value,
85                      hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (object)));
86   default:
87     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88   }
89 }
90
91 static void
92 hildon_touch_selector_entry_set_property (GObject *object, guint property_id,
93                                           const GValue *value, GParamSpec *pspec)
94 {
95   switch (property_id) {
96   case PROP_TEXT_COLUMN:
97     hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (object),
98                                                  g_value_get_int (value));
99   default:
100     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
101   }
102 }
103
104 static void
105 hildon_touch_selector_entry_class_init (HildonTouchSelectorEntryClass *klass)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (klass);
108   HildonTouchSelectorClass *selector_class = HILDON_TOUCH_SELECTOR_CLASS (klass);
109
110   g_type_class_add_private (klass, sizeof (HildonTouchSelectorEntryPrivate));
111
112   selector_class->set_model = hildon_touch_selector_entry_set_model;
113   selector_class->has_multiple_selection = hildon_touch_selector_entry_has_multiple_selection;
114
115   object_class->get_property = hildon_touch_selector_entry_get_property;
116   object_class->set_property = hildon_touch_selector_entry_set_property;
117
118   /**
119    * HildonTouchSelectorEntry:text-column:
120    *
121    * Deprecated: now this property is in HildonTouchSelectorColumn use
122    * hildon_touch_selector_entry_set_text_column() and
123    * hildon_touch_selector_entry_get_text_column() to manage this.
124    *
125    * Since: 2.2
126    **/
127   g_object_class_install_property (G_OBJECT_CLASS (klass),
128                                    PROP_TEXT_COLUMN,
129                                    g_param_spec_int ("text-column",
130                                                      "Text Column",
131                                                      "A column in the data source model to get the strings from.",
132                                                      -1,
133                                                      G_MAXINT,
134                                                      -1,
135                                                      G_PARAM_READWRITE));
136 }
137
138 static gchar *
139 hildon_touch_selector_entry_print_func (HildonTouchSelector * selector)
140 {
141   HildonTouchSelectorEntryPrivate *priv;
142   GtkTreeModel *model;
143   GtkTreeIter iter;
144   gint column;
145   gchar *text = NULL;
146
147   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
148
149   if (*(gtk_entry_get_text (GTK_ENTRY (priv->entry))) != '\0') {
150     text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
151   } else {
152     model = hildon_touch_selector_get_model (selector, 0);
153     if (hildon_touch_selector_get_selected (selector, 0, &iter)) {
154       column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
155       gtk_tree_model_get (model, &iter, column, &text, -1);
156     }
157   }
158
159   return text;
160 }
161
162 static void
163 hildon_touch_selector_entry_init (HildonTouchSelectorEntry *self)
164 {
165   HildonTouchSelectorEntryPrivate *priv;
166   GtkEntryCompletion *completion;
167
168   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (self);
169
170   priv->entry = hildon_entry_new (HILDON_SIZE_AUTO);
171   gtk_entry_set_activates_default (GTK_ENTRY (priv->entry), TRUE);
172
173   completion = gtk_entry_completion_new ();
174   gtk_entry_completion_set_inline_completion (completion, TRUE);
175   gtk_entry_completion_set_popup_completion (completion, FALSE);
176   gtk_entry_set_completion (GTK_ENTRY (priv->entry), completion);
177
178   gtk_widget_show (priv->entry);
179   g_signal_connect (G_OBJECT (priv->entry), "changed",
180                     G_CALLBACK (entry_on_text_changed), self);
181   priv->signal_id = g_signal_connect (G_OBJECT (self), "changed",
182                                       G_CALLBACK (hildon_touch_selector_entry_changed), NULL);
183
184   hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (self), hildon_touch_selector_entry_print_func);
185   gtk_box_pack_start (GTK_BOX (self), priv->entry, FALSE, FALSE, 0);
186 }
187
188 /**
189  * hildon_touch_selector_entry_new:
190  *
191  * Creates a #HildonTouchSelectorEntry
192  *
193  * Returns: A new #HildonTouchSelectorEntry
194  *
195  * Since: 2.2
196  **/
197 GtkWidget *
198 hildon_touch_selector_entry_new (void)
199 {
200   return g_object_new (HILDON_TYPE_TOUCH_SELECTOR_ENTRY, NULL);
201 }
202
203 /**
204  * hildon_touch_selector_entry_new_text:
205  *
206  * Creates a #HildonTouchSelectorEntry with a single text column that
207  * can be populated conveniently through hildon_touch_selector_append_text(),
208  * hildon_touch_selector_prepend_text(), hildon_touch_selector_insert_text().
209  *
210  * Returns: A new #HildonTouchSelectorEntry
211  *
212  * Since: 2.2
213  **/
214 GtkWidget *
215 hildon_touch_selector_entry_new_text (void)
216 {
217   GtkListStore *model;
218   GtkWidget *selector;
219   GtkEntryCompletion *completion;
220   HildonTouchSelectorEntryPrivate *priv;
221   HildonTouchSelectorColumn *column = NULL;
222
223   selector = hildon_touch_selector_entry_new ();
224
225   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
226
227   model = gtk_list_store_new (1, G_TYPE_STRING);
228   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
229   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
230   column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
231                                                      GTK_TREE_MODEL (model), FALSE);
232
233   g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
234                     selector);
235   hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
236
237   return selector;
238 }
239
240 static void
241 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
242 {
243   HildonTouchSelectorEntry *selector;
244   HildonTouchSelectorEntryPrivate *priv;
245   GtkEntryCompletion *completion;
246   gint text_column = -1;
247
248   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
249   selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
250
251   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
252   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
253
254   text_column = hildon_touch_selector_entry_get_text_column (selector);
255
256   gtk_entry_completion_set_text_column (completion, text_column);
257 }
258
259 /**
260  * hildon_touch_selector_entry_set_text_column:
261  * @selector: A #HildonTouchSelectorEntry
262  * @text_column: A column in model to get the strings from
263  *
264  * Sets the model column which touch selector box should use to get strings
265  * from to be @text_column.
266  *
267  * Since: 2.2
268  **/
269 void
270 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
271                                              gint text_column)
272 {
273   HildonTouchSelectorColumn *column = NULL;
274
275   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
276   g_return_if_fail (text_column >= -1);
277
278   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
279
280   g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
281 }
282
283 /**
284  * hildon_touch_selector_entry_get_text_column:
285  * @selector: A #HildonTouchSelectorEntry
286  *
287  * Gets the text column that @selector is using as a text column.
288  *
289  * Returns: the number of the column used as a text column.
290  *
291  * Since: 2.2
292  **/
293 gint
294 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
295 {
296   HildonTouchSelectorColumn *column = NULL;
297   gint text_column = -1;
298
299   g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
300
301   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
302                                              0);
303
304   g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
305
306   return text_column;
307 }
308
309 static void
310 entry_on_text_changed (GtkEditable * editable,
311                        gpointer userdata)
312 {
313   HildonTouchSelector *selector;
314   HildonTouchSelectorEntryPrivate *priv;
315   GtkTreeModel *model;
316   GtkTreeIter iter;
317   GtkEntry *entry;
318   const gchar *prefix;
319   gchar *text;
320   gboolean found = FALSE;
321   gint text_column = -1;
322
323   entry = GTK_ENTRY (editable);
324   selector = HILDON_TOUCH_SELECTOR (userdata);
325   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
326
327   text_column =
328     hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
329
330   prefix = gtk_entry_get_text (entry);
331   model = hildon_touch_selector_get_model (selector, 0);
332
333   if (!gtk_tree_model_get_iter_first (model, &iter)) {
334     return;
335   }
336
337   do {
338     gtk_tree_model_get (model, &iter, text_column, &text, -1);
339     found = g_str_has_prefix (text, prefix);
340     g_free (text);
341   } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
342
343   g_signal_handler_block (selector, priv->signal_id);
344   {
345     /* We emit the HildonTouchSelector::changed signal because a change in the
346        GtkEntry represents a change in current selection, and therefore, users
347        should be notified. */
348     if (found) {
349       hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
350     }
351     g_signal_emit_by_name (selector, "changed", 0);
352   }
353   g_signal_handler_unblock (selector, priv->signal_id);
354
355 }
356
357 /* FIXME: This is actually a very ugly way to retrieve the text. Ideally,
358    we would have API to retrieve it from the base clase (HildonTouchSelector).
359    In the meantime, leaving it here.
360  */
361 static gchar *
362 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
363 {
364   HildonTouchSelectorEntryPrivate *priv;
365   GtkTreeModel *model;
366   GtkTreeIter iter;
367   GtkTreePath *path;
368   GList *selected_rows;
369   gchar *text;
370   gint text_column = -1;
371
372   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
373
374   model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
375   text_column = hildon_touch_selector_entry_get_text_column (selector);
376   selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
377
378   if (selected_rows == NULL) {
379     return NULL;
380   }
381
382   /* We are in single selection mode */
383   g_assert (selected_rows->next == NULL);
384
385   path = (GtkTreePath *)selected_rows->data;
386   gtk_tree_model_get_iter (model, &iter, path);
387   gtk_tree_model_get (model, &iter, text_column, &text, -1);
388
389   gtk_tree_path_free (path);
390   g_list_free (selected_rows);
391
392   return text;
393 }
394
395 static void
396 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
397                                      gint column, gpointer user_data)
398 {
399   HildonTouchSelectorEntryPrivate *priv;
400   gchar *text;
401
402   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
403
404   text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
405   if (text != NULL) {
406     gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
407     gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
408     g_free (text);
409   }
410 }
411
412 static void
413 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
414                                        gint column, GtkTreeModel *model)
415 {
416   GtkEntryCompletion *completion;
417   HildonTouchSelectorEntryPrivate *priv;
418   gint text_column = -1;
419
420   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
421   g_return_if_fail (column == 0);
422   g_return_if_fail (GTK_IS_TREE_MODEL (model));
423
424   HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
425
426   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
427
428   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
429   gtk_entry_completion_set_model (completion, model);
430
431   text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
432
433   gtk_entry_completion_set_text_column (completion, text_column);
434 }
435
436 static gboolean
437 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
438 {
439   /* Always TRUE, given the GtkEntry. */
440   return TRUE;
441 }