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.
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().
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.
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().
46 #include "hildon-touch-selector.h"
47 #include "hildon-touch-selector-entry.h"
48 #include "hildon-entry.h"
50 G_DEFINE_TYPE (HildonTouchSelectorEntry, hildon_touch_selector_entry, HILDON_TYPE_TOUCH_SELECTOR)
52 #define HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE(o) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((o), HILDON_TYPE_TOUCH_SELECTOR_ENTRY, HildonTouchSelectorEntryPrivate))
55 typedef struct _HildonTouchSelectorEntryPrivate HildonTouchSelectorEntryPrivate;
57 static void entry_on_text_changed (GtkEditable * editable, gpointer userdata);
58 static void hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
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);
66 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data);
69 struct _HildonTouchSelectorEntryPrivate {
79 hildon_touch_selector_entry_get_property (GObject *object, guint property_id,
80 GValue *value, GParamSpec *pspec)
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)));
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
92 hildon_touch_selector_entry_set_property (GObject *object, guint property_id,
93 const GValue *value, GParamSpec *pspec)
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));
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105 hildon_touch_selector_entry_class_init (HildonTouchSelectorEntryClass *klass)
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
108 HildonTouchSelectorClass *selector_class = HILDON_TOUCH_SELECTOR_CLASS (klass);
110 g_type_class_add_private (klass, sizeof (HildonTouchSelectorEntryPrivate));
112 selector_class->set_model = hildon_touch_selector_entry_set_model;
113 selector_class->has_multiple_selection = hildon_touch_selector_entry_has_multiple_selection;
115 object_class->get_property = hildon_touch_selector_entry_get_property;
116 object_class->set_property = hildon_touch_selector_entry_set_property;
119 * HildonTouchSelectorEntry:text-column:
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.
127 g_object_class_install_property (G_OBJECT_CLASS (klass),
129 g_param_spec_int ("text-column",
131 "A column in the data source model to get the strings from.",
139 hildon_touch_selector_entry_print_func (HildonTouchSelector * selector)
141 HildonTouchSelectorEntryPrivate *priv;
147 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
149 if (*(gtk_entry_get_text (GTK_ENTRY (priv->entry))) != '\0') {
150 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
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);
163 hildon_touch_selector_entry_init (HildonTouchSelectorEntry *self)
165 HildonTouchSelectorEntryPrivate *priv;
166 GtkEntryCompletion *completion;
168 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (self);
170 priv->entry = hildon_entry_new (HILDON_SIZE_AUTO);
171 gtk_entry_set_activates_default (GTK_ENTRY (priv->entry), TRUE);
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);
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);
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);
189 * hildon_touch_selector_entry_new:
191 * Creates a #HildonTouchSelectorEntry
193 * Returns: A new #HildonTouchSelectorEntry
198 hildon_touch_selector_entry_new (void)
200 return g_object_new (HILDON_TYPE_TOUCH_SELECTOR_ENTRY, NULL);
204 * hildon_touch_selector_entry_new_text:
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().
210 * Returns: A new #HildonTouchSelectorEntry
215 hildon_touch_selector_entry_new_text (void)
219 GtkEntryCompletion *completion;
220 HildonTouchSelectorEntryPrivate *priv;
221 HildonTouchSelectorColumn *column = NULL;
223 selector = hildon_touch_selector_entry_new ();
225 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
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);
233 g_signal_connect (column, "notify::text-column", G_CALLBACK (_text_column_modified),
235 hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), 0);
241 _text_column_modified (GObject *pspec, GParamSpec *gobject, gpointer data)
243 HildonTouchSelectorEntry *selector;
244 HildonTouchSelectorEntryPrivate *priv;
245 GtkEntryCompletion *completion;
246 gint text_column = -1;
248 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (data));
249 selector = HILDON_TOUCH_SELECTOR_ENTRY (data);
251 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (HILDON_TOUCH_SELECTOR_ENTRY(selector));
252 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
254 text_column = hildon_touch_selector_entry_get_text_column (selector);
256 gtk_entry_completion_set_text_column (completion, text_column);
260 * hildon_touch_selector_entry_set_text_column:
261 * @selector: A #HildonTouchSelectorEntry
262 * @text_column: A column in model to get the strings from
264 * Sets the model column which touch selector box should use to get strings
265 * from to be @text_column.
270 hildon_touch_selector_entry_set_text_column (HildonTouchSelectorEntry *selector,
273 HildonTouchSelectorColumn *column = NULL;
275 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
276 g_return_if_fail (text_column >= -1);
278 column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector), 0);
280 g_object_set (G_OBJECT (column), "text-column", text_column, NULL);
284 * hildon_touch_selector_entry_get_text_column:
285 * @selector: A #HildonTouchSelectorEntry
287 * Gets the text column that @selector is using as a text column.
289 * Returns: the number of the column used as a text column.
294 hildon_touch_selector_entry_get_text_column (HildonTouchSelectorEntry *selector)
296 HildonTouchSelectorColumn *column = NULL;
297 gint text_column = -1;
299 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), -1);
301 column = hildon_touch_selector_get_column (HILDON_TOUCH_SELECTOR (selector),
304 g_object_get (G_OBJECT (column), "text-column", &text_column, NULL);
310 * hildon_touch_selector_entry_set_input_mode:
311 * @selector: a #HildonTouchSelectorEntry
312 * @input_mode: #HildonGtkInputMode mask
314 * Sets the input mode to be used in the #GtkEntry in @selector. See hildon_gtk_entry_set_input_mode()
320 hildon_touch_selector_entry_set_input_mode (HildonTouchSelectorEntry * selector,
321 HildonGtkInputMode input_mode)
323 HildonTouchSelectorEntryPrivate *priv;
325 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
326 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
328 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry), input_mode);
332 * hildon_touch_selector_entry_get_input_mode:
333 * @selector: a #HildonTouchSelectorEntry
335 * Gets the input mode used in the #GtkEntry in @selector. See hildon_gtk_entry_get_input_mode()
338 * Returns: a mask of #HildonGtkInputMode
343 hildon_touch_selector_entry_get_input_mode (HildonTouchSelectorEntry * selector)
345 HildonTouchSelectorEntryPrivate *priv;
347 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector), HILDON_GTK_INPUT_MODE_ALPHA);
349 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
351 return hildon_gtk_entry_get_input_mode (GTK_ENTRY (priv->entry));
355 entry_on_text_changed (GtkEditable * editable,
358 HildonTouchSelector *selector;
359 HildonTouchSelectorEntryPrivate *priv;
365 gboolean found = FALSE;
366 gint text_column = -1;
368 entry = GTK_ENTRY (editable);
369 selector = HILDON_TOUCH_SELECTOR (userdata);
370 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
373 hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
375 prefix = gtk_entry_get_text (entry);
376 model = hildon_touch_selector_get_model (selector, 0);
378 if (!gtk_tree_model_get_iter_first (model, &iter)) {
383 gtk_tree_model_get (model, &iter, text_column, &text, -1);
384 found = g_str_has_prefix (text, prefix);
386 } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
388 g_signal_handler_block (selector, priv->signal_id);
390 /* We emit the HildonTouchSelector::changed signal because a change in the
391 GtkEntry represents a change in current selection, and therefore, users
392 should be notified. */
394 hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
396 g_signal_emit_by_name (selector, "changed", 0);
398 g_signal_handler_unblock (selector, priv->signal_id);
402 /* FIXME: This is actually a very ugly way to retrieve the text. Ideally,
403 we would have API to retrieve it from the base clase (HildonTouchSelector).
404 In the meantime, leaving it here.
407 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
409 HildonTouchSelectorEntryPrivate *priv;
413 GList *selected_rows;
415 gint text_column = -1;
417 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
419 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
420 text_column = hildon_touch_selector_entry_get_text_column (selector);
421 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
423 if (selected_rows == NULL) {
427 /* We are in single selection mode */
428 g_assert (selected_rows->next == NULL);
430 path = (GtkTreePath *)selected_rows->data;
431 gtk_tree_model_get_iter (model, &iter, path);
432 gtk_tree_model_get (model, &iter, text_column, &text, -1);
434 gtk_tree_path_free (path);
435 g_list_free (selected_rows);
441 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
442 gint column, gpointer user_data)
444 HildonTouchSelectorEntryPrivate *priv;
447 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
449 text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
451 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
452 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
458 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
459 gint column, GtkTreeModel *model)
461 GtkEntryCompletion *completion;
462 HildonTouchSelectorEntryPrivate *priv;
463 gint text_column = -1;
465 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_ENTRY (selector));
466 g_return_if_fail (column == 0);
467 g_return_if_fail (GTK_IS_TREE_MODEL (model));
469 HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
471 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
473 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
474 gtk_entry_completion_set_model (completion, model);
476 text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
478 gtk_entry_completion_set_text_column (completion, text_column);
482 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
484 /* Always TRUE, given the GtkEntry. */