d2e3183a0a92717e959645a248b7039ba48965d4
[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 GtkWidget *
189 hildon_touch_selector_entry_new (void)
190 {
191   return g_object_new (HILDON_TYPE_TOUCH_SELECTOR_ENTRY, NULL);
192 }
193
194 /**
195  * hildon_touch_selector_entry_new_text:
196  *
197  * Creates a #HildonTouchSelectorEntry with a single text column that
198  * can be populated conveniently through hildon_touch_selector_append_text(),
199  * hildon_touch_selector_prepend_text(), hildon_touch_selector_insert_text().
200  *
201  * Returns: A new #HildonTouchSelectorEntry
202  *
203  * Since: 2.2
204  **/
205 GtkWidget *
206 hildon_touch_selector_entry_new_text (void)
207 {
208   GtkListStore *model;
209   GtkWidget *selector;
210   GtkEntryCompletion *completion;
211   HildonTouchSelectorEntryPrivate *priv;
212   HildonTouchSelectorColumn *column = NULL;
213
214   selector = hildon_touch_selector_entry_new ();
215
216   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
217
218   model = gtk_list_store_new (1, G_TYPE_STRING);
219   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
220   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
221   column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
222                                                      GTK_TREE_MODEL (model), FALSE);
223
224   g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
225                     selector);
226   hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
227
228   return selector;
229 }
230
231 static void
232 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
233 {
234   HildonTouchSelectorEntry *selector;
235   HildonTouchSelectorEntryPrivate *priv;
236   GtkEntryCompletion *completion;
237   gint text_column = -1;
238
239   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
240   selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
241
242   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
243   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
244
245   text_column = hildon_touch_selector_entry_get_text_column (selector);
246
247   gtk_entry_completion_set_text_column (completion, text_column);
248 }
249
250 /**
251  * hildon_touch_selector_entry_set_text_column:
252  * @selector: A #HildonTouchSelectorEntry
253  * @text_column: A column in model to get the strings from
254  *
255  * Sets the model column which touch selector box should use to get strings
256  * from to be @text_column.
257  *
258  * Since: 2.2
259  **/
260 void
261 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
262                                              gint text_column)
263 {
264   HildonTouchSelectorColumn *column = NULL;
265
266   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
267   g_return_if_fail (text_column >= -1);
268
269   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
270
271   g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
272 }
273
274 /**
275  * hildon_touch_selector_entry_get_text_column:
276  * @selector: A #HildonTouchSelectorEntry
277  *
278  * Gets the text column that @selector is using as a text column.
279  *
280  * Returns: the number of the column used as a text column.
281  *
282  * Since: 2.2
283  **/
284 gint
285 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
286 {
287   HildonTouchSelectorColumn *column = NULL;
288   gint text_column = -1;
289
290   g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
291
292   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
293                                              0);
294
295   g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
296
297   return text_column;
298 }
299
300 static void
301 entry_on_text_changed (GtkEditable * editable,
302                        gpointer userdata)
303 {
304   HildonTouchSelector *selector;
305   HildonTouchSelectorEntryPrivate *priv;
306   GtkTreeModel *model;
307   GtkTreeIter iter;
308   GtkEntry *entry;
309   const gchar *prefix;
310   gchar *text;
311   gboolean found = FALSE;
312   gint text_column = -1;
313
314   entry = GTK_ENTRY (editable);
315   selector = HILDON_TOUCH_SELECTOR (userdata);
316   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
317
318   text_column =
319     hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
320
321   prefix = gtk_entry_get_text (entry);
322   model = hildon_touch_selector_get_model (selector, 0);
323
324   if (!gtk_tree_model_get_iter_first (model, &iter)) {
325     return;
326   }
327
328   do {
329     gtk_tree_model_get (model, &iter, text_column, &text, -1);
330     found = g_str_has_prefix (text, prefix);
331     g_free (text);
332   } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
333
334   g_signal_handler_block (selector, priv->signal_id);
335   {
336     /* We emit the HildonTouchSelector::changed signal because a change in the
337        GtkEntry represents a change in current selection, and therefore, users
338        should be notified. */
339     if (found) {
340       hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
341     }
342     g_signal_emit_by_name (selector, "changed", 0);
343   }
344   g_signal_handler_unblock (selector, priv->signal_id);
345
346 }
347
348 /* FIXME: This is actually a very ugly way to retrieve the text. Ideally,
349    we would have API to retrieve it from the base clase (HildonTouchSelector).
350    In the meantime, leaving it here.
351  */
352 static gchar *
353 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
354 {
355   HildonTouchSelectorEntryPrivate *priv;
356   GtkTreeModel *model;
357   GtkTreeIter iter;
358   GtkTreePath *path;
359   GList *selected_rows;
360   gchar *text;
361   gint text_column = -1;
362
363   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
364
365   model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
366   text_column = hildon_touch_selector_entry_get_text_column (selector);
367   selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
368
369   if (selected_rows == NULL) {
370     return NULL;
371   }
372
373   /* We are in single selection mode */
374   g_assert (selected_rows->next == NULL);
375
376   path = (GtkTreePath *)selected_rows->data;
377   gtk_tree_model_get_iter (model, &iter, path);
378   gtk_tree_model_get (model, &iter, text_column, &text, -1);
379
380   gtk_tree_path_free (path);
381   g_list_free (selected_rows);
382
383   return text;
384 }
385
386 static void
387 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
388                                      gint column, gpointer user_data)
389 {
390   HildonTouchSelectorEntryPrivate *priv;
391   gchar *text;
392
393   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
394
395   text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
396   if (text != NULL) {
397     gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
398     gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
399     g_free (text);
400   }
401 }
402
403 static void
404 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
405                                        gint column, GtkTreeModel *model)
406 {
407   GtkEntryCompletion *completion;
408   HildonTouchSelectorEntryPrivate *priv;
409   gint text_column = -1;
410
411   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
412   g_return_if_fail (column == 0);
413   g_return_if_fail (GTK_IS_TREE_MODEL (model));
414
415   HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
416
417   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
418
419   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
420   gtk_entry_completion_set_model (completion, model);
421
422   text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
423
424   gtk_entry_completion_set_text_column (completion, text_column);
425 }
426
427 static gboolean
428 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
429 {
430   /* Always TRUE, given the GtkEntry. */
431   return TRUE;
432 }