2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation.
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.
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.
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.
22 * SECTION:hildon-touch-selector-entry
23 * @short_description: A selector widget with one column and a text entry
24 * @see_also: #HildonTouchSelector, #HildonPickerButton
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().
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().
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.
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().
47 #include "hildon-touch-selector.h"
48 #include "hildon-touch-selector-entry.h"
49 #include "hildon-entry.h"
51 G_DEFINE_TYPE (HildonTouchSelectorEntry, hildon_touch_selector_entry, HILDON_TYPE_TOUCH_SELECTOR)
53 #define HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE(o) \
54 (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_TOUCH_SELECTOR_ENTRY, HildonTouchSelectorEntryPrivate))
56 typedef struct _HildonTouchSelectorEntryPrivate HildonTouchSelectorEntryPrivate;
58 static void entry_on_text_changed (GtkEditable * editable, gpointer userdata);
59 static void hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
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);
67 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data);
70 struct _HildonTouchSelectorEntryPrivate {
80 hildon_touch_selector_entry_get_property (GObject *object, guint property_id,
81 GValue *value, GParamSpec *pspec)
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)));
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 hildon_touch_selector_entry_set_property (GObject *object, guint property_id,
95 const GValue *value, GParamSpec *pspec)
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));
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
108 hildon_touch_selector_entry_class_init (HildonTouchSelectorEntryClass *klass)
110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 HildonTouchSelectorClass *selector_class = HILDON_TOUCH_SELECTOR_CLASS (klass);
113 g_type_class_add_private (klass, sizeof (HildonTouchSelectorEntryPrivate));
115 selector_class->set_model = hildon_touch_selector_entry_set_model;
116 selector_class->has_multiple_selection = hildon_touch_selector_entry_has_multiple_selection;
118 object_class->get_property = hildon_touch_selector_entry_get_property;
119 object_class->set_property = hildon_touch_selector_entry_set_property;
122 * HildonTouchSelectorEntry:text-column:
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.
130 g_object_class_install_property (G_OBJECT_CLASS (klass),
132 g_param_spec_int ("text-column",
134 "A column in the data source model to get the strings from.",
142 hildon_touch_selector_entry_print_func (HildonTouchSelector * selector, gpointer user_data)
144 HildonTouchSelectorEntryPrivate *priv;
150 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
152 if (*(gtk_entry_get_text (GTK_ENTRY (priv->entry))) != '\0') {
153 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
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);
166 hildon_touch_selector_entry_init (HildonTouchSelectorEntry *self)
168 HildonTouchSelectorEntryPrivate *priv;
169 GtkEntryCompletion *completion;
170 HildonGtkInputMode input_mode;
172 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (self);
174 priv->entry = hildon_entry_new (HILDON_SIZE_AUTO);
175 gtk_entry_set_activates_default (GTK_ENTRY (priv->entry), TRUE);
176 input_mode = hildon_gtk_entry_get_input_mode (GTK_ENTRY (priv->entry));
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;
183 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry), input_mode);
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);
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);
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);
201 * hildon_touch_selector_entry_new:
203 * Creates a #HildonTouchSelectorEntry
205 * Returns: A new #HildonTouchSelectorEntry
210 hildon_touch_selector_entry_new (void)
212 return g_object_new (HILDON_TYPE_TOUCH_SELECTOR_ENTRY, NULL);
216 * hildon_touch_selector_entry_new_text:
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().
222 * Returns: A new #HildonTouchSelectorEntry
227 hildon_touch_selector_entry_new_text (void)
231 GtkEntryCompletion *completion;
232 HildonTouchSelectorEntryPrivate *priv;
233 HildonTouchSelectorColumn *column = NULL;
235 selector = hildon_touch_selector_entry_new ();
237 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
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);
245 g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
247 hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
253 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
255 HildonTouchSelectorEntry *selector;
256 HildonTouchSelectorEntryPrivate *priv;
257 GtkEntryCompletion *completion;
258 gint text_column = -1;
260 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
261 selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
263 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
264 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
266 text_column = hildon_touch_selector_entry_get_text_column (selector);
268 gtk_entry_completion_set_text_column (completion, text_column);
272 * hildon_touch_selector_entry_set_text_column:
273 * @selector: A #HildonTouchSelectorEntry
274 * @text_column: A column in model to get the strings from
276 * Sets the model column which touch selector box should use to get strings
277 * from to be @text_column.
282 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
285 HildonTouchSelectorColumn *column = NULL;
287 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
288 g_return_if_fail (text_column >= -1);
290 column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
292 g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
296 * hildon_touch_selector_entry_get_text_column:
297 * @selector: A #HildonTouchSelectorEntry
299 * Gets the text column that @selector is using as a text column.
301 * Returns: the number of the column used as a text column.
306 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
308 HildonTouchSelectorColumn *column = NULL;
309 gint text_column = -1;
311 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
313 column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
316 g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
322 * hildon_touch_selector_entry_set_input_mode:
323 * @selector: a #HildonTouchSelectorEntry
324 * @input_mode: #HildonGtkInputMode mask
326 * Sets the input mode to be used in the #GtkEntry in @selector. See hildon_gtk_entry_set_input_mode()
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.
338 hildon_touch_selector_entry_set_input_mode (HildonTouchSelectorEntry * selector,
339 HildonGtkInputMode input_mode)
341 HildonTouchSelectorEntryPrivate *priv;
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)));
348 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
350 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry), input_mode);
354 * hildon_touch_selector_entry_get_input_mode:
355 * @selector: a #HildonTouchSelectorEntry
357 * Gets the input mode used in the #GtkEntry in @selector. See hildon_gtk_entry_get_input_mode()
360 * Returns: a mask of #HildonGtkInputMode
365 hildon_touch_selector_entry_get_input_mode (HildonTouchSelectorEntry * selector)
367 HildonTouchSelectorEntryPrivate *priv;
369 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), HILDON_GTK_INPUT_MODE_ALPHA);
371 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
373 return hildon_gtk_entry_get_input_mode (GTK_ENTRY (priv->entry));
377 entry_on_text_changed (GtkEditable * editable,
380 HildonTouchSelector *selector;
381 HildonTouchSelectorEntryPrivate *priv;
387 gboolean found = FALSE;
388 gint text_column = -1;
390 entry = GTK_ENTRY (editable);
391 selector = HILDON_TOUCH_SELECTOR (userdata);
392 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
395 hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
397 prefix = gtk_entry_get_text (entry);
399 if (prefix[0] == '\0') {
403 model = hildon_touch_selector_get_model (selector, 0);
405 if (!gtk_tree_model_get_iter_first (model, &iter)) {
410 gtk_tree_model_get (model, &iter, text_column, &text, -1);
411 found = g_str_has_prefix (text, prefix);
413 } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
415 g_signal_handler_block (selector, priv->signal_id);
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. */
421 hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
423 g_signal_emit_by_name (selector, "changed", 0);
425 g_signal_handler_unblock (selector, priv->signal_id);
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.
434 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
439 GList *selected_rows;
441 gint text_column = -1;
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);
447 if (selected_rows == NULL) {
451 /* We are in single selection mode */
452 g_assert (selected_rows->next == NULL);
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);
458 gtk_tree_path_free (path);
459 g_list_free (selected_rows);
465 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
466 gint column, gpointer user_data)
468 HildonTouchSelectorEntryPrivate *priv;
471 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
473 text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
475 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
476 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
482 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
483 gint column, GtkTreeModel *model)
485 GtkEntryCompletion *completion;
486 HildonTouchSelectorEntryPrivate *priv;
487 gint text_column = -1;
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));
493 HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
495 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
497 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
498 gtk_entry_completion_set_model (completion, model);
500 text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
502 gtk_entry_completion_set_text_column (completion, text_column);
506 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
508 /* Always TRUE, given the GtkEntry. */
513 * hildon_touch_selector_entry_get_entry:
514 * @selector: a #HildonTouchSelectorEntry.
516 * Provides access to the #HildonEntry in @selector. Use to programmatically
517 * change the contents in entry or modify its behavior.
519 * Returns: a #HildonEntry.
524 hildon_touch_selector_entry_get_entry (HildonTouchSelectorEntry * selector)
526 HildonTouchSelectorEntryPrivate *priv;
528 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), NULL);
530 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
532 return HILDON_ENTRY (priv->entry);