18a1bbfb761907d76a1798b49c5c5adec0342ebe
[hildon] / hildon / 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. For more specific use cases,
30  * the #HildonEntry can be accessed directly with hildon_touch_selector_get_entry().
31  *
32  * The main difference between the #GtkTreeModel used by #HildonTouchSelector
33  * and #HildonTouchSelectorEntry, is that the latter must always include a text
34  * column. You should set it with hildon_touch_selector_entry_set_text_column().
35  *
36  * Normally, you would use #HildonTouchSelectorEntry together with a
37  * #HildonPickerDialog activated from a button. For the most common
38  * cases, you should use #HildonPickerButton.
39  *
40  * If you only need a text only, one column selector, you can create it with
41  * hildon_touch_selector_entry_new_text() and populate it with
42  * hildon_touch_selector_append_text(), hildon_touch_selector_prepend_text(),
43  * and hildon_touch_selector_insert_text().
44  *
45  */
46
47 #include "hildon-touch-selector.h"
48 #include "hildon-touch-selector-entry.h"
49 #include "hildon-entry.h"
50
51 G_DEFINE_TYPE (HildonTouchSelectorEntry, hildon_touch_selector_entry, HILDON_TYPE_TOUCH_SELECTOR)
52
53 #define HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE(o) \
54   (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_TOUCH_SELECTOR_ENTRY, HildonTouchSelectorEntryPrivate))
55
56 typedef struct _HildonTouchSelectorEntryPrivate HildonTouchSelectorEntryPrivate;
57
58 static void entry_on_text_changed (GtkEditable * editable, gpointer userdata);
59 static void hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
60                                                  gint column,
61                                                  gpointer user_data);
62 static void hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
63                                                    gint column, GtkTreeModel *model);
64 static gboolean hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector);
65
66 static void
67 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data);
68
69
70 struct _HildonTouchSelectorEntryPrivate {
71   gulong signal_id;
72   GtkWidget *entry;
73 };
74
75 enum {
76   PROP_TEXT_COLUMN = 1
77 };
78
79 static void
80 hildon_touch_selector_entry_get_property (GObject *object, guint property_id,
81                                           GValue *value, GParamSpec *pspec)
82 {
83   switch (property_id) {
84   case PROP_TEXT_COLUMN:
85     g_value_set_int (value,
86                      hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (object)));
87     break;
88   default:
89     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90   }
91 }
92
93 static void
94 hildon_touch_selector_entry_set_property (GObject *object, guint property_id,
95                                           const GValue *value, GParamSpec *pspec)
96 {
97   switch (property_id) {
98   case PROP_TEXT_COLUMN:
99     hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (object),
100                                                  g_value_get_int (value));
101     break;
102   default:
103     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104   }
105 }
106
107 static void
108 hildon_touch_selector_entry_class_init (HildonTouchSelectorEntryClass *klass)
109 {
110   GObjectClass *object_class = G_OBJECT_CLASS (klass);
111   HildonTouchSelectorClass *selector_class = HILDON_TOUCH_SELECTOR_CLASS (klass);
112
113   g_type_class_add_private (klass, sizeof (HildonTouchSelectorEntryPrivate));
114
115   selector_class->set_model = hildon_touch_selector_entry_set_model;
116   selector_class->has_multiple_selection = hildon_touch_selector_entry_has_multiple_selection;
117
118   object_class->get_property = hildon_touch_selector_entry_get_property;
119   object_class->set_property = hildon_touch_selector_entry_set_property;
120
121   /**
122    * HildonTouchSelectorEntry:text-column:
123    *
124    * Deprecated: now this property is in HildonTouchSelectorColumn use
125    * hildon_touch_selector_entry_set_text_column() and
126    * hildon_touch_selector_entry_get_text_column() to manage this.
127    *
128    * Since: 2.2
129    **/
130   g_object_class_install_property (G_OBJECT_CLASS (klass),
131                                    PROP_TEXT_COLUMN,
132                                    g_param_spec_int ("text-column",
133                                                      "Text Column",
134                                                      "A column in the data source model to get the strings from.",
135                                                      -1,
136                                                      G_MAXINT,
137                                                      -1,
138                                                      G_PARAM_READWRITE));
139 }
140
141 static gchar *
142 hildon_touch_selector_entry_print_func (HildonTouchSelector * selector, gpointer user_data)
143 {
144   HildonTouchSelectorEntryPrivate *priv;
145   GtkTreeModel *model;
146   GtkTreeIter iter;
147   gint column;
148   gchar *text = NULL;
149
150   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
151
152   if (*(hildon_entry_get_text (HILDON_ENTRY (priv->entry))) != '\0') {
153     text = g_strdup (hildon_entry_get_text (HILDON_ENTRY (priv->entry)));
154   } else {
155     model = hildon_touch_selector_get_model (selector, 0);
156     if (hildon_touch_selector_get_selected (selector, 0, &iter)) {
157       column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
158       gtk_tree_model_get (model, &iter, column, &text, -1);
159     }
160   }
161
162   return text;
163 }
164
165 static void
166 hildon_touch_selector_entry_init (HildonTouchSelectorEntry *self)
167 {
168   HildonTouchSelectorEntryPrivate *priv;
169   GtkEntryCompletion *completion;
170   HildonGtkInputMode input_mode;
171
172   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (self);
173
174   priv->entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
175   gtk_entry_set_activates_default (GTK_ENTRY (priv->entry), TRUE);
176   input_mode = hildon_gtk_entry_get_input_mode (GTK_ENTRY (priv->entry));
177
178   /* Disable unsupported input modes. */
179   input_mode &= ~HILDON_GTK_INPUT_MODE_MULTILINE;
180   input_mode &= ~HILDON_GTK_INPUT_MODE_INVISIBLE;
181   input_mode &= ~HILDON_GTK_INPUT_MODE_DICTIONARY;
182
183   hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry), input_mode);
184
185   completion = gtk_entry_completion_new ();
186   gtk_entry_completion_set_inline_completion (completion, TRUE);
187   gtk_entry_completion_set_popup_completion (completion, FALSE);
188   gtk_entry_set_completion (GTK_ENTRY (priv->entry), completion);
189
190   gtk_widget_show (priv->entry);
191   g_signal_connect (G_OBJECT (priv->entry), "changed",
192                     G_CALLBACK (entry_on_text_changed), self);
193   priv->signal_id = g_signal_connect (G_OBJECT (self), "changed",
194                                       G_CALLBACK (hildon_touch_selector_entry_changed), NULL);
195
196   hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (self), hildon_touch_selector_entry_print_func);
197   gtk_box_pack_start (GTK_BOX (self), priv->entry, FALSE, FALSE, 0);
198 }
199
200 /**
201  * hildon_touch_selector_entry_new:
202  *
203  * Creates a #HildonTouchSelectorEntry
204  *
205  * Returns: A new #HildonTouchSelectorEntry
206  *
207  * Since: 2.2
208  **/
209 GtkWidget *
210 hildon_touch_selector_entry_new (void)
211 {
212   return g_object_new (HILDON_TYPE_TOUCH_SELECTOR_ENTRY, NULL);
213 }
214
215 /**
216  * hildon_touch_selector_entry_new_text:
217  *
218  * Creates a #HildonTouchSelectorEntry with a single text column that
219  * can be populated conveniently through hildon_touch_selector_append_text(),
220  * hildon_touch_selector_prepend_text(), hildon_touch_selector_insert_text().
221  *
222  * Returns: A new #HildonTouchSelectorEntry
223  *
224  * Since: 2.2
225  **/
226 GtkWidget *
227 hildon_touch_selector_entry_new_text (void)
228 {
229   GtkListStore *model;
230   GtkWidget *selector;
231   GtkEntryCompletion *completion;
232   HildonTouchSelectorEntryPrivate *priv;
233   HildonTouchSelectorColumn *column = NULL;
234
235   selector = hildon_touch_selector_entry_new ();
236
237   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
238
239   model = gtk_list_store_new (1, G_TYPE_STRING);
240   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
241   gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (model));
242   column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
243                                                      GTK_TREE_MODEL (model), FALSE);
244
245   g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
246                     selector);
247   hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
248
249   return selector;
250 }
251
252 static void
253 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
254 {
255   HildonTouchSelectorEntry *selector;
256   HildonTouchSelectorEntryPrivate *priv;
257   GtkEntryCompletion *completion;
258   gint text_column = -1;
259
260   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
261   selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
262
263   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
264   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
265
266   text_column = hildon_touch_selector_entry_get_text_column (selector);
267
268   gtk_entry_completion_set_text_column (completion, text_column);
269 }
270
271 /**
272  * hildon_touch_selector_entry_set_text_column:
273  * @selector: A #HildonTouchSelectorEntry
274  * @text_column: A column in model to get the strings from
275  *
276  * Sets the model column which touch selector box should use to get strings
277  * from to be @text_column.
278  *
279  * Since: 2.2
280  **/
281 void
282 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
283                                              gint text_column)
284 {
285   HildonTouchSelectorColumn *column = NULL;
286
287   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
288   g_return_if_fail (text_column >= -1);
289
290   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
291
292   g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
293 }
294
295 /**
296  * hildon_touch_selector_entry_get_text_column:
297  * @selector: A #HildonTouchSelectorEntry
298  *
299  * Gets the text column that @selector is using as a text column.
300  *
301  * Returns: the number of the column used as a text column.
302  *
303  * Since: 2.2
304  **/
305 gint
306 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
307 {
308   HildonTouchSelectorColumn *column = NULL;
309   gint text_column = -1;
310
311   g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
312
313   column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
314                                              0);
315
316   g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
317
318   return text_column;
319 }
320
321 /**
322  * hildon_touch_selector_entry_set_input_mode:
323  * @selector: a #HildonTouchSelectorEntry
324  * @input_mode: #HildonGtkInputMode mask
325  *
326  * Sets the input mode to be used in the #GtkEntry in @selector. See hildon_gtk_entry_set_input_mode()
327  * for details.
328  *
329  * It must be noted that not all input modes are available for the
330  * entry in @selector. In particular,
331  * %HILDON_GTK_INPUT_MODE_MULTILINE, %HILDON_GTK_INPUT_MODE_INVISIBLE,
332  * %HILDON_GTK_INPUT_MODE_DICTIONARY are disabled, since these are irrelevant
333  * for #HildonTouchSelectorEntry.
334  *
335  * Since: 2.2
336  **/
337 void
338 hildon_touch_selector_entry_set_input_mode (HildonTouchSelectorEntry * selector,
339                                             HildonGtkInputMode input_mode)
340 {
341   HildonTouchSelectorEntryPrivate *priv;
342
343   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
344   g_return_if_fail (!(input_mode & (HILDON_GTK_INPUT_MODE_MULTILINE |
345                                     HILDON_GTK_INPUT_MODE_INVISIBLE |
346                                     HILDON_GTK_INPUT_MODE_DICTIONARY)));
347
348   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
349
350   hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry), input_mode);
351 }
352
353 /**
354  * hildon_touch_selector_entry_get_input_mode:
355  * @selector: a #HildonTouchSelectorEntry
356  *
357  * Gets the input mode used in the #GtkEntry in @selector. See hildon_gtk_entry_get_input_mode()
358  * for details.
359  *
360  * Returns: a mask of #HildonGtkInputMode
361  *
362  * Since: 2.2
363  **/
364 HildonGtkInputMode
365 hildon_touch_selector_entry_get_input_mode (HildonTouchSelectorEntry * selector)
366 {
367   HildonTouchSelectorEntryPrivate *priv;
368
369   g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), HILDON_GTK_INPUT_MODE_ALPHA);
370
371   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
372
373   return hildon_gtk_entry_get_input_mode (GTK_ENTRY (priv->entry));
374 }
375
376 static void
377 entry_on_text_changed (GtkEditable * editable,
378                        gpointer userdata)
379 {
380   HildonTouchSelector *selector;
381   HildonTouchSelectorEntryPrivate *priv;
382   GtkTreeModel *model;
383   GtkTreeIter iter;
384   HildonEntry *entry;
385   const gchar *prefix;
386   gchar *text;
387   gboolean found = FALSE;
388   gint text_column = -1;
389
390   entry = HILDON_ENTRY (editable);
391   selector = HILDON_TOUCH_SELECTOR (userdata);
392   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
393
394   text_column =
395     hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
396
397   prefix = hildon_entry_get_text (entry);
398
399   if (prefix[0] == '\0') {
400           return;
401   }
402
403   model = hildon_touch_selector_get_model (selector, 0);
404
405   if (!gtk_tree_model_get_iter_first (model, &iter)) {
406     return;
407   }
408
409   do {
410     gtk_tree_model_get (model, &iter, text_column, &text, -1);
411     found = g_str_has_prefix (text, prefix);
412     g_free (text);
413   } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
414
415   g_signal_handler_block (selector, priv->signal_id);
416   {
417     /* We emit the HildonTouchSelector::changed signal because a change in the
418        GtkEntry represents a change in current selection, and therefore, users
419        should be notified. */
420     if (found) {
421       hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
422     }
423     g_signal_emit_by_name (selector, "changed", 0);
424   }
425   g_signal_handler_unblock (selector, priv->signal_id);
426
427 }
428
429 /* FIXME: This is actually a very ugly way to retrieve the text. Ideally,
430    we would have API to retrieve it from the base clase (HildonTouchSelector).
431    In the meantime, leaving it here.
432  */
433 static gchar *
434 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
435 {
436   GtkTreeModel *model;
437   GtkTreeIter iter;
438   GtkTreePath *path;
439   GList *selected_rows;
440   gchar *text;
441   gint text_column = -1;
442
443   model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
444   text_column = hildon_touch_selector_entry_get_text_column (selector);
445   selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
446
447   if (selected_rows == NULL) {
448     return NULL;
449   }
450
451   /* We are in single selection mode */
452   g_assert (selected_rows->next == NULL);
453
454   path = (GtkTreePath *)selected_rows->data;
455   gtk_tree_model_get_iter (model, &iter, path);
456   gtk_tree_model_get (model, &iter, text_column, &text, -1);
457
458   gtk_tree_path_free (path);
459   g_list_free (selected_rows);
460
461   return text;
462 }
463
464 static void
465 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
466                                      gint column, gpointer user_data)
467 {
468   HildonTouchSelectorEntryPrivate *priv;
469   gchar *text;
470
471   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
472
473   text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
474   if (text != NULL) {
475     hildon_entry_set_text (HILDON_ENTRY (priv->entry), text);
476     gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
477     g_free (text);
478   }
479 }
480
481 static void
482 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
483                                        gint column, GtkTreeModel *model)
484 {
485   GtkEntryCompletion *completion;
486   HildonTouchSelectorEntryPrivate *priv;
487   gint text_column = -1;
488
489   g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
490   g_return_if_fail (column == 0);
491   g_return_if_fail (GTK_IS_TREE_MODEL (model));
492
493   HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
494
495   priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
496
497   completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
498   gtk_entry_completion_set_model (completion, model);
499
500   text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
501
502   gtk_entry_completion_set_text_column (completion, text_column);
503 }
504
505 static gboolean
506 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
507 {
508   /* Always TRUE, given the GtkEntry. */
509   return TRUE;
510 }
511
512 /**
513  * hildon_touch_selector_entry_get_entry:
514  * @selector: a #HildonTouchSelectorEntry.
515  *
516  * Provides access to the #HildonEntry in @selector. Use to programmatically
517  * change the contents in entry or modify its behavior.
518  *
519  * Returns: a #HildonEntry.
520  *
521  * Since: 2.2
522  **/
523 HildonEntry *
524 hildon_touch_selector_entry_get_entry (HildonTouchSelectorEntry * selector)
525 {
526         HildonTouchSelectorEntryPrivate *priv;
527
528         g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), NULL);
529
530         priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
531
532         return HILDON_ENTRY (priv->entry);
533 }