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 entry_on_text_changed (GtkEditable * editable,
313 HildonTouchSelector *selector;
314 HildonTouchSelectorEntryPrivate *priv;
320 gboolean found = FALSE;
321 gint text_column = -1;
323 entry = GTK_ENTRY (editable);
324 selector = HILDON_TOUCH_SELECTOR (userdata);
325 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
328 hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
330 prefix = gtk_entry_get_text (entry);
331 model = hildon_touch_selector_get_model (selector, 0);
333 if (!gtk_tree_model_get_iter_first (model, &iter)) {
338 gtk_tree_model_get (model, &iter, text_column, &text, -1);
339 found = g_str_has_prefix (text, prefix);
341 } while (found != TRUE && gtk_tree_model_iter_next (model, &iter));
343 g_signal_handler_block (selector, priv->signal_id);
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. */
349 hildon_touch_selector_select_iter (selector, 0, &iter, TRUE);
351 g_signal_emit_by_name (selector, "changed", 0);
353 g_signal_handler_unblock (selector, priv->signal_id);
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.
362 hildon_touch_selector_get_text_from_model (HildonTouchSelectorEntry * selector)
364 HildonTouchSelectorEntryPrivate *priv;
368 GList *selected_rows;
370 gint text_column = -1;
372 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
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);
378 if (selected_rows == NULL) {
382 /* We are in single selection mode */
383 g_assert (selected_rows->next == NULL);
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);
389 gtk_tree_path_free (path);
390 g_list_free (selected_rows);
396 hildon_touch_selector_entry_changed (HildonTouchSelector * selector,
397 gint column, gpointer user_data)
399 HildonTouchSelectorEntryPrivate *priv;
402 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
404 text = hildon_touch_selector_get_text_from_model (HILDON_TOUCH_SELECTOR_ENTRY (selector));
406 gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
407 gtk_editable_select_region (GTK_EDITABLE (priv->entry), 0, -1);
413 hildon_touch_selector_entry_set_model (HildonTouchSelector * selector,
414 gint column, GtkTreeModel *model)
416 GtkEntryCompletion *completion;
417 HildonTouchSelectorEntryPrivate *priv;
418 gint text_column = -1;
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));
424 HILDON_TOUCH_SELECTOR_CLASS (hildon_touch_selector_entry_parent_class)->set_model (selector, column, model);
426 priv = HILDON_TOUCH_SELECTOR_ENTRY_GET_PRIVATE (selector);
428 completion = gtk_entry_get_completion (GTK_ENTRY (priv->entry));
429 gtk_entry_completion_set_model (completion, model);
431 text_column = hildon_touch_selector_entry_get_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector));
433 gtk_entry_completion_set_text_column (completion, text_column);
437 hildon_touch_selector_entry_has_multiple_selection (HildonTouchSelector * selector)
439 /* Always TRUE, given the GtkEntry. */