2 * This file is a part of hildon
4 * Copyright (C) 2005, 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
23 * @short_description: A selector widget with several columns.
25 * #HildonTouchSelector is a selector widget, that allows users to
26 * select items from one to many predefined lists. It is very similar
27 * to #GtkComboBox, but with several individual pannable columns.
29 * Normally, you would use #HildonTouchSelector together with a
30 * #HildonPickerDialog activated from a button. For the most common
31 * cases, you should use #HildonPickerButton.
33 * The composition of each column in the selector is represented by a
34 * #GtkTreeModel. To add a new column to a #HildonTouchSelector, use
35 * hildon_touch_selector_append_column(). If you want to add a
36 * text-only column, without special attributes, use
37 * hildon_touch_selector_append_text_column().
39 * It is highly recommended that you use only one column
40 * #HildonTouchSelector<!-- -->s.
41 * If you only need a text only, one column selector, you can create it with
42 * hildon_touch_selector_new_text() and populate with
43 * hildon_touch_selector_append_text(), hildon_touch_selector_prepend_text(),
44 * and hildon_touch_selector_insert_text().
46 * If you need a selector widget that also accepts user inputs, you
47 * can use #HildonTouchSelectorEntry.
57 #include "hildon-gtk.h"
59 #include "hildon-pannable-area.h"
60 #include "hildon-touch-selector.h"
62 #define HILDON_TOUCH_SELECTOR_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_TOUCH_SELECTOR, HildonTouchSelectorPrivate))
65 G_DEFINE_TYPE (HildonTouchSelector, hildon_touch_selector, GTK_TYPE_VBOX)
67 #define CENTER_ON_SELECTED_ITEM_DELAY 50
70 * Struct to maintain the data of each column. The columns are the elements
71 * of the widget that belongs properly to the selection behaviour. As
72 * the selector contents are arranged in a #GtkHBox, you can add more widgets, like buttons etc.
73 * between the columns, but this doesn't belongs to the selection
76 typedef struct _SelectorColumn SelectorColumn;
77 struct _SelectorColumn
79 HildonTouchSelector *parent; /* the selector that contains this column */
81 GtkTreeView *tree_view;
83 GtkWidget *panarea; /* the pannable widget */
86 struct _HildonTouchSelectorPrivate
88 GSList *columns; /* the selection columns */
89 GtkWidget *hbox; /* the container for the selector's columns */
91 HildonTouchSelectorPrintFunc print_func;
96 PROP_HAS_MULTIPLE_SELECTION = 1
105 static gint hildon_touch_selector_signals[LAST_SIGNAL] = { 0 };
107 static void hildon_touch_selector_get_property (GObject * object,
109 GValue * value, GParamSpec * pspec);
112 static void hildon_touch_selector_map (GtkWidget * widget);
115 static void hildon_touch_selector_remove (GtkContainer * container,
117 /* private functions */
118 static void _selection_changed_cb (GtkTreeSelection * selection,
120 static gchar *_default_print_func (HildonTouchSelector * selector);
122 static SelectorColumn *_create_new_column (HildonTouchSelector * selector,
123 GtkTreeModel * model,
124 GtkCellRenderer * renderer,
126 static gboolean _hildon_touch_selector_center_on_selected_items (gpointer data);
129 _hildon_touch_selector_set_model (HildonTouchSelector * selector,
130 gint num_column, GtkTreeModel * model);
132 _hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector);
135 hildon_touch_selector_class_init (HildonTouchSelectorClass * class)
137 GObjectClass *gobject_class;
138 GtkObjectClass *object_class;
139 GtkWidgetClass *widget_class;
140 GtkContainerClass *container_class;
142 gobject_class = (GObjectClass *) class;
143 object_class = (GtkObjectClass *) class;
144 widget_class = (GtkWidgetClass *) class;
145 container_class = (GtkContainerClass *) class;
149 gobject_class->get_property = hildon_touch_selector_get_property;
152 widget_class->map = hildon_touch_selector_map;
155 container_class->remove = hildon_touch_selector_remove;
157 /* HildonTouchSelector */
158 class->set_model = _hildon_touch_selector_set_model;
160 class->has_multiple_selection = _hildon_touch_selector_has_multiple_selection;
164 * HildonTouchSelector::changed:
165 * @widget: the object which received the signal
167 * The changed signal is emitted when the active
168 * item is changed. This can be due to the user selecting
169 * a different item from the list, or due to a
170 * call to hildon_touch_selector_select_iter() on
171 * one of the columns.
174 hildon_touch_selector_signals[CHANGED] =
175 g_signal_new ("changed",
176 G_OBJECT_CLASS_TYPE (class),
178 G_STRUCT_OFFSET (HildonTouchSelectorClass, changed),
180 gtk_marshal_NONE__INT, G_TYPE_NONE, 1, G_TYPE_INT);
183 g_object_class_install_property (gobject_class, PROP_HAS_MULTIPLE_SELECTION,
184 g_param_spec_boolean ("has-multiple-selection",
185 "has multiple selection",
186 "Whether the widget has multiple "
187 "selection (like multiple columns, "
188 "multiselection mode, or multiple "
189 "internal widgets) and therefore "
190 "it may need a confirmation button, "
195 /* style properties */
196 g_type_class_add_private (object_class, sizeof (HildonTouchSelectorPrivate));
200 hildon_touch_selector_get_property (GObject * object,
202 GValue * value, GParamSpec * pspec)
205 case PROP_HAS_MULTIPLE_SELECTION:
206 g_value_set_boolean (value,
207 hildon_touch_selector_has_multiple_selection (HILDON_TOUCH_SELECTOR (object)));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 hildon_touch_selector_init (HildonTouchSelector * selector)
218 selector->priv = HILDON_TOUCH_SELECTOR_GET_PRIVATE (selector);
220 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (selector), GTK_NO_WINDOW);
221 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (selector), FALSE);
223 selector->priv->columns = NULL;
225 selector->priv->print_func = NULL;
226 selector->priv->hbox = gtk_hbox_new (FALSE, 0);
228 gtk_box_pack_end (GTK_BOX (selector), selector->priv->hbox,
230 gtk_widget_show (selector->priv->hbox);
232 /* FIXME: this is the correct height? A fixed height is the correct
234 gtk_widget_set_size_request (GTK_WIDGET (selector), -1, 320);
238 hildon_touch_selector_map (GtkWidget * widget)
240 GTK_WIDGET_CLASS (hildon_touch_selector_parent_class)->map (widget);
242 g_timeout_add (CENTER_ON_SELECTED_ITEM_DELAY,
243 _hildon_touch_selector_center_on_selected_items, widget);
246 /*------------------------------ GtkContainer ------------------------------ */
249 * Required in order to free the column at the columns list
252 hildon_touch_selector_remove (GtkContainer * container, GtkWidget * widget)
254 HildonTouchSelector *selector = NULL;
257 SelectorColumn *current_column = NULL;
258 gint num_columns = 0;
260 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (container));
262 selector = HILDON_TOUCH_SELECTOR (container);
263 num_columns = hildon_touch_selector_get_num_columns (selector);
265 /* Check if the widget is inside a column and remove
267 iter = selector->priv->columns;
270 current_column = (SelectorColumn *) iter->data;
271 if (widget == current_column->panarea) {
272 current_column = g_slist_nth_data (selector->priv->columns, position);
274 selector->priv->columns = g_slist_remove (selector->priv->columns,
276 g_free (current_column);
282 iter = g_slist_next (iter);
284 if (position >= num_columns) {
285 g_debug ("This widget was not inside the selector column");
288 GTK_CONTAINER_CLASS (hildon_touch_selector_parent_class)->remove (container, widget);
291 /* ------------------------------ PRIVATE METHODS ---------------------------- */
293 * default_print_func:
294 * @selector: a #HildonTouchSelector
296 * Default print function
298 * Returns: a new string that represents the selected items
301 _default_print_func (HildonTouchSelector * selector)
303 gchar *result = NULL;
305 gint num_columns = 0;
307 GtkTreeModel *model = NULL;
308 gchar *current_string = NULL;
310 HildonTouchSelectorSelectionMode mode;
312 GtkTreePath *current_path = NULL;
313 GList *selected_rows = NULL;
314 gint initial_value = 0;
316 num_columns = hildon_touch_selector_get_num_columns (selector);
318 mode = hildon_touch_selector_get_column_selection_mode (selector);
320 if ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
321 && (num_columns > 0)) {
322 /* In this case we get the first column first */
323 selected_rows = hildon_touch_selector_get_selected_rows (selector, 0);
324 model = hildon_touch_selector_get_model (selector, 0);
326 result = g_strdup_printf ("(");
328 for (item = selected_rows; item; item = g_list_next (item)) {
329 current_path = item->data;
330 gtk_tree_model_get_iter (model, &iter, current_path);
332 gtk_tree_model_get (model, &iter, 0, ¤t_string, -1);
334 if (i < g_list_length (selected_rows) - 1) {
335 aux = g_strconcat (result, current_string, ",", NULL);
339 aux = g_strconcat (result, current_string, NULL);
346 aux = g_strconcat (result, ")", NULL);
350 g_list_foreach (selected_rows, (GFunc) (gtk_tree_path_free), NULL);
351 g_list_free (selected_rows);
357 for (i = initial_value; i < num_columns; i++) {
358 model = hildon_touch_selector_get_model (selector, i);
359 if (hildon_touch_selector_get_selected (selector, i, &iter)) {
361 gtk_tree_model_get (model, &iter, 0, ¤t_string, -1);
363 aux = g_strconcat (result, ":", current_string, NULL);
367 result = g_strdup_printf ("%s", current_string);
376 _selection_changed_cb (GtkTreeSelection * selection, gpointer user_data)
378 HildonTouchSelector *selector = NULL;
379 SelectorColumn *column = NULL;
380 gint num_column = -1;
382 column = (SelectorColumn *) user_data;
383 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (column->parent));
385 selector = column->parent;
387 num_column = g_slist_index (selector->priv->columns, column);
389 g_signal_emit (selector, hildon_touch_selector_signals[CHANGED], 0, num_column);
393 static SelectorColumn *
394 _create_new_column (HildonTouchSelector * selector,
395 GtkTreeModel * model,
396 GtkCellRenderer * renderer, va_list args)
398 SelectorColumn *new_column = NULL;
399 GtkTreeViewColumn *tree_column = NULL;
400 GtkTreeView *tv = NULL;
401 GtkWidget *panarea = NULL;
402 GtkTreeSelection *selection = NULL;
407 tree_column = gtk_tree_view_column_new ();
408 gtk_tree_view_column_pack_start (tree_column, renderer, TRUE);
410 attribute = va_arg (args, gchar *);
411 while (attribute != NULL) {
412 value = va_arg (args, gint);
413 gtk_tree_view_column_add_attribute (tree_column, renderer, attribute,
415 attribute = va_arg (args, gchar *);
418 tv = GTK_TREE_VIEW (hildon_gtk_tree_view_new (HILDON_UI_MODE_EDIT));
419 gtk_tree_view_set_model (tv, model);
420 gtk_tree_view_set_rules_hint (tv, TRUE);
422 gtk_tree_view_append_column (GTK_TREE_VIEW (tv), tree_column);
424 new_column = (SelectorColumn *) g_malloc0 (sizeof (SelectorColumn));
425 new_column->parent = selector;
427 panarea = hildon_pannable_area_new ();
429 g_object_set (G_OBJECT (panarea), "vscrollbar-policy", GTK_POLICY_NEVER,
430 "initial-hint", FALSE, NULL);
432 gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
434 new_column->model = model;
435 new_column->tree_view = tv;
436 new_column->panarea = panarea;
438 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
439 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
441 /* select the first item */
442 if (gtk_tree_model_get_iter_first (model, &iter)) {
443 gtk_tree_selection_select_iter (selection, &iter);
446 gtk_widget_grab_focus (GTK_WIDGET (tv));
448 /* connect to the changed signal connection */
449 g_signal_connect (G_OBJECT (selection), "changed",
450 G_CALLBACK (_selection_changed_cb), new_column);
455 /* ------------------------------ PUBLIC METHODS ---------------------------- */
458 * hildon_touch_selector_new:
460 * Creates a new empty #HildonTouchSelector.
462 * Returns: a new #HildonTouchSelector.
465 hildon_touch_selector_new (void)
467 return g_object_new (HILDON_TYPE_TOUCH_SELECTOR, NULL);
471 * hildon_touch_selector_new_text:
473 * Creates a #HildonTouchSelector with a single text column that
474 * can be populated conveniently through hildon_touch_selector_append_text(),
475 * hildon_touch_selector_prepend_text(), hildon_touch_selector_insert_text().
477 * Returns: A new #HildonTouchSelector
480 hildon_touch_selector_new_text (void)
485 selector = hildon_touch_selector_new ();
486 store = gtk_list_store_new (1, G_TYPE_STRING);
488 hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
489 GTK_TREE_MODEL (store), TRUE);
495 * hildon_touch_selector_append_text:
496 * @selector: A #HildonTouchSelector.
497 * @text: a non %NULL text string.
499 * Appends a new entry in a #HildonTouchSelector created with
500 * hildon_touch_selector_new_text().
503 hildon_touch_selector_append_text (HildonTouchSelector * selector,
509 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
510 g_return_if_fail (text != NULL);
512 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
514 g_return_if_fail (GTK_IS_LIST_STORE (model));
516 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
517 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
521 * hildon_touch_selector_prepend_text:
522 * @selector: A #HildonTouchSelector.
523 * @text: a non %NULL text string.
525 * Prepends a new entry in a #HildonTouchSelector created with
526 * hildon_touch_selector_new_text().
529 hildon_touch_selector_prepend_text (HildonTouchSelector * selector,
535 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
536 g_return_if_fail (text != NULL);
538 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
540 g_return_if_fail (GTK_IS_LIST_STORE (model));
542 gtk_list_store_prepend (GTK_LIST_STORE (model), &iter);
543 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
547 * hildon_touch_selector_insert_text:
548 * @selector: a #HildonTouchSelector.
549 * @position: the position to insert @text.
550 * @text: A non %NULL text string.
552 * Inserts a new entry in particular position of a #HildoTouchSelector created
553 * with hildon_touch_selector_new_text().
557 hildon_touch_selector_insert_text (HildonTouchSelector * selector,
558 gint position, const gchar * text)
563 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
564 g_return_if_fail (text != NULL);
565 g_return_if_fail (position >= 0);
567 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
569 g_return_if_fail (GTK_IS_LIST_STORE (model));
571 gtk_list_store_insert (GTK_LIST_STORE (model), &iter, position);
572 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
576 * hildon_touch_selector_append_column
577 * @selector: a #HildonTouchSelector
578 * @model: the #GtkTreeModel with the data of the column
579 * @cell_renderer: The #GtkCellRenderer where to draw each row contents.
580 * @Varargs: a %NULL-terminated pair of attributes and column numbers.
582 * This functions adds a new column to the widget, whose data will
583 * be obtained from the model. Only widgets added this way should used on
584 * the selection logic, i.e., the print function, the #HildonTouchPicker::changed
587 * Contents will be represented in @cell_renderer. You can pass a %NULL-terminated
588 * list of pairs property/value, in the same way you would use
589 * gtk_tree_view_column_set_attributes().
591 * There is a prerequisite to be considered on models used: text data must
592 * be in the first column.
594 * This method basically adds a #GtkTreeView to the widget, using the model and
597 * Returns: %TRUE if a new column was added, %FALSE otherwise
601 hildon_touch_selector_append_column (HildonTouchSelector * selector,
602 GtkTreeModel * model,
603 GtkCellRenderer * cell_renderer, ...)
606 SelectorColumn *new_column = NULL;
608 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
609 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
613 va_start (args, cell_renderer);
614 new_column = _create_new_column (selector, model, cell_renderer, args);
617 selector->priv->columns = g_slist_append (selector->priv->columns,
619 gtk_box_pack_start (GTK_BOX (selector->priv->hbox), new_column->panarea, TRUE, TRUE, 6);
621 gtk_widget_show_all (new_column->panarea);
630 * hildon_touch_selector_append_text_column
631 * @selector: a #HildonTouchSelector
632 * @model: a #GtkTreeModel with data for the column
633 * @center: whether to center the text on the column
635 * Equivalent to hildon_touch_selector_append_column(), but using a
636 * default text cell renderer. This is the most common use case of the
639 * Returns: %TRUE if a new column was added, %FALSE otherwise.
642 hildon_touch_selector_append_text_column (HildonTouchSelector * selector,
643 GtkTreeModel * model, gboolean center)
645 GtkCellRenderer *renderer = NULL;
648 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
649 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
652 renderer = gtk_cell_renderer_text_new ();
655 g_value_init (&val, G_TYPE_FLOAT);
656 g_value_set_float (&val, 0.5);
657 /* FIXME: center the text, this should be configurable */
658 g_object_set_property (G_OBJECT (renderer), "xalign", &val);
661 return hildon_touch_selector_append_column (selector, model, renderer,
669 * hildon_touch_selector_remove_column:
670 * @selector: a #HildonTouchSelector
671 * @column: the position of the column to be removed
673 * Removes a column from @selector.
675 * Returns: %TRUE if the column was removed, %FALSE otherwise
678 hildon_touch_selector_remove_column (HildonTouchSelector * selector, gint column)
680 SelectorColumn *current_column = NULL;
681 HildonTouchSelectorPrivate *priv;
683 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
684 g_return_val_if_fail (column <
685 hildon_touch_selector_get_num_columns (selector), FALSE);
687 priv = HILDON_TOUCH_SELECTOR_GET_PRIVATE (selector);
688 current_column = g_slist_nth_data (priv->columns, column);
690 gtk_container_remove (GTK_CONTAINER (priv->hbox), current_column->panarea);
691 priv->columns = g_slist_remove (priv->columns, current_column);
692 g_free (current_column);
698 * hildon_touch_selector_set_column_attributes:
699 * @selector: a #HildonTouchSelector
700 * @num_column: the number of the column whose attributes we're setting
701 * @cell_renderer: the #GtkCellRendere we're setting the attributes of
702 * @Varargs: A %NULL-terminated list of attributes.
704 * Sets the attributes for the given column. The attributes must be given
705 * in attribute/column pairs, just like in gtk_tree_view_column_set_attributes().
706 * All existing attributes are removed and replaced with the new ones.
710 hildon_touch_selector_set_column_attributes (HildonTouchSelector * selector,
712 GtkCellRenderer * cell_renderer,
716 GtkTreeViewColumn *tree_column = NULL;
717 SelectorColumn *current_column = NULL;
718 gchar *attribute = NULL;
721 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
722 g_return_if_fail (num_column <
723 hildon_touch_selector_get_num_columns (selector));
725 current_column = g_slist_nth_data (selector->priv->columns, num_column);
727 tree_column = gtk_tree_view_get_column (current_column->tree_view, 0);
728 gtk_tree_view_remove_column (current_column->tree_view, tree_column);
730 tree_column = gtk_tree_view_column_new ();
731 gtk_tree_view_column_pack_start (tree_column, cell_renderer, TRUE);
733 va_start (args, cell_renderer);
734 attribute = va_arg (args, gchar *);
736 gtk_tree_view_column_clear_attributes (tree_column, cell_renderer);
738 while (attribute != NULL) {
739 value = va_arg (args, gint);
740 gtk_tree_view_column_add_attribute (tree_column, cell_renderer,
742 attribute = va_arg (args, gchar *);
747 gtk_tree_view_append_column (current_column->tree_view, tree_column);
751 * hildon_touch_selector_get_num_columns:
752 * @selector: a #HildonTouchSelector
754 * Gets the number of columns in the #HildonTouchSelector.
756 * Returns: the number of columns in @selector.
759 hildon_touch_selector_get_num_columns (HildonTouchSelector * selector)
761 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), -1);
763 return g_slist_length (selector->priv->columns);
767 * hildon_touch_selector_get_column_selection_mode:
768 * @selector: a #HildonTouchSelector
770 * Gets the selection mode of @selector.
772 * Returns: one of #HildonTouchSelectorSelectionMode
774 HildonTouchSelectorSelectionMode
775 hildon_touch_selector_get_column_selection_mode (HildonTouchSelector * selector)
777 HildonTouchSelectorSelectionMode result =
778 HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
779 GtkSelectionMode treeview_mode = GTK_SELECTION_SINGLE;
780 SelectorColumn *column = NULL;
781 GtkTreeSelection *selection = NULL;
783 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), result);
784 g_return_val_if_fail (hildon_touch_selector_get_num_columns (selector) > 0,
787 column = (SelectorColumn *) selector->priv->columns->data;
789 selection = gtk_tree_view_get_selection (column->tree_view);
790 treeview_mode = gtk_tree_selection_get_mode (selection);
793 if (treeview_mode == GTK_SELECTION_MULTIPLE) {
794 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE;
796 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
803 * hildon_touch_selector_set_column_selection_mode:
804 * @selector: a #HildonTouchSelector
805 * @mode: the #HildonTouchSelectorMode for @selector
807 * Sets the selection mode for @selector. See #HildonTouchSelectorSelectionMode.
810 hildon_touch_selector_set_column_selection_mode (HildonTouchSelector * selector,
811 HildonTouchSelectorSelectionMode mode)
813 GtkTreeView *tv = NULL;
814 SelectorColumn *column = NULL;
815 GtkTreeSelection *selection = NULL;
816 GtkSelectionMode treeview_mode = GTK_SELECTION_MULTIPLE;
819 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
820 g_return_if_fail (hildon_touch_selector_get_num_columns (selector) > 0);
822 column = (SelectorColumn *) (g_slist_nth (selector->priv->columns, 0))->data;
823 tv = column->tree_view;
827 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE:
828 treeview_mode = GTK_SELECTION_SINGLE;
830 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE:
831 treeview_mode = GTK_SELECTION_MULTIPLE;
835 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
836 gtk_tree_selection_set_mode (selection, treeview_mode);
838 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
839 gtk_tree_model_get_iter_first (column->model, &iter);
840 gtk_tree_selection_unselect_all (selection);
841 gtk_tree_selection_select_iter (selection, &iter);
847 * hildon_touch_selector_set_print_func:
848 * @selector: a #HildonTouchSelector
849 * @func: a #HildonTouchSelectorPrintFunc function
851 * Sets the function to be used by hildon_touch_selector_get_current_text()
852 * to produce a text representation of the currently selected items in @selector.
853 * The default function will return a concatenation of comma separated items
854 * selected in each column in @selector. Use this to override this method if you
855 * need a particular representation for your application.
859 hildon_touch_selector_set_print_func (HildonTouchSelector * selector,
860 HildonTouchSelectorPrintFunc func)
862 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
864 selector->priv->print_func = func;
868 * hildon_touch_selector_get_print_func:
869 * @selector: a #HildonTouchSelector
871 * Gets the #HildonTouchSelectorPrintFunc currently used. See
872 * hildon_touch_selector_set_print_func().
874 * Returns: a #HildonTouchSelectorPrintFunc or %NULL if the default
875 * one is currently used.
877 HildonTouchSelectorPrintFunc
878 hildon_touch_selector_get_print_func (HildonTouchSelector * selector)
880 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
882 return selector->priv->print_func;
886 * hildon_touch_selector_get_selected:
887 * @selector: a #HildonTouchSelector
888 * @column: the column number we want to get the element
889 * @iter: #GtkTreeIter currently selected
891 * Sets @iter to the currently selected node on the nth-column, if selection is
892 * set to %HILDON_TOUCH_SELECTOR_SINGLE or %HILDON_TOUCH_SELECTOR_MULTIPLE with
893 * a column different that the first one. @iter may be %NULL if you just want to
894 * test if selection has any selected items.
896 * This function will not work if selection is in
897 * %HILDON_TOUCH_SELECTOR_MULTIPLE mode and the column is the first one.
899 * See gtk_tree_selection_get_selected() for more information.
901 * Returns: %TRUE if @iter was correctly set, %FALSE otherwise
904 hildon_touch_selector_get_selected (HildonTouchSelector * selector,
905 gint column, GtkTreeIter * iter)
907 GtkTreeSelection *selection = NULL;
908 SelectorColumn *current_column = NULL;
909 HildonTouchSelectorSelectionMode mode;
911 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
912 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
914 mode = hildon_touch_selector_get_column_selection_mode (selector);
916 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE) ||
917 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)&&(column>0)),
920 current_column = g_slist_nth_data (selector->priv->columns, column);
923 gtk_tree_view_get_selection (GTK_TREE_VIEW (current_column->tree_view));
925 return gtk_tree_selection_get_selected (selection, NULL, iter);
929 * hildon_touch_selector_select_iter
930 * @selector: a #HildonTouchSelector
931 * @column: the column to selects
932 * @iter: the #GtkTreeIter to be selected
933 * @scroll_to: whether to smoothly scroll to the item
935 * Sets the currently selected item in the column @column to the one pointed by @iter,
936 * optionally smoothly scrolling to it.
940 hildon_touch_selector_select_iter (HildonTouchSelector * selector,
941 gint column, GtkTreeIter * iter,
947 SelectorColumn *current_column = NULL;
948 GtkTreeSelection *selection = NULL;
951 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
952 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
954 current_column = g_slist_nth_data (selector->priv->columns, column);
956 selection = gtk_tree_view_get_selection (current_column->tree_view);
957 model = gtk_tree_view_get_model (current_column->tree_view);
958 path = gtk_tree_model_get_path (model, iter);
960 gtk_tree_selection_select_iter (selection, iter);
963 gtk_tree_view_get_background_area (current_column->tree_view,
965 gtk_tree_view_convert_bin_window_to_tree_coords (current_column->tree_view,
966 0, rect.y, NULL, &y);
967 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (current_column->panarea),
970 gtk_tree_path_free (path);
974 * hildon_touch_selector_unselect_iter
975 * @selector: a #HildonTouchSelector
976 * @column: the column to unselects from
977 * @iter: the #GtkTreeIter to be unselected
979 * Unselect the item pointed by @iter in the column @column
983 void hildon_touch_selector_unselect_iter (HildonTouchSelector * selector,
987 SelectorColumn *current_column = NULL;
988 GtkTreeSelection *selection = NULL;
990 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
991 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
993 current_column = g_slist_nth_data (selector->priv->columns, column);
994 selection = gtk_tree_view_get_selection (current_column->tree_view);
995 gtk_tree_selection_unselect_iter (selection, iter);
999 * hildon_touch_selector_get_selected_rows:
1000 * @selector: a #HildonTouchSelector
1001 * @column: the position of the column to get the selected rows from
1003 * Creates a list of #GtkTreePath<!-- -->s of all selected rows in a column. Additionally,
1004 * if you to plan to modify the model after calling this function, you may
1005 * want to convert the returned list into a list of GtkTreeRowReferences. To do this,
1006 * you can use gtk_tree_row_reference_new().
1008 * See gtk_tree_selection_get_selected_rows() for more information.
1010 * Returns: A new #GList containing a #GtkTreePath for each selected row in the column @column.
1014 hildon_touch_selector_get_selected_rows (HildonTouchSelector * selector,
1017 GList *result = NULL;
1018 SelectorColumn *current_column = NULL;
1019 GtkTreeSelection *selection = NULL;
1021 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1022 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1025 current_column = g_slist_nth_data (selector->priv->columns, column);
1026 selection = gtk_tree_view_get_selection (current_column->tree_view);
1028 result = gtk_tree_selection_get_selected_rows (selection, NULL);
1035 * hildon_touch_selector_get_model:
1036 * @selector: a #HildonTouchSelector
1037 * @column: the position of the column in @selector
1039 * Gets the model of a column of @selector.
1041 * Returns: the #GtkTreeModel for the column @column of @selector.
1044 hildon_touch_selector_get_model (HildonTouchSelector * selector, gint column)
1046 SelectorColumn *current_column = NULL;
1048 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1049 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1052 current_column = g_slist_nth_data (selector->priv->columns, column);
1054 return current_column->model;
1058 _hildon_touch_selector_set_model (HildonTouchSelector * selector,
1059 gint column, GtkTreeModel * model)
1061 SelectorColumn *current_column = NULL;
1064 (SelectorColumn *) g_slist_nth_data (selector->priv->columns, column);
1066 current_column->model = model;
1067 gtk_tree_view_set_model (current_column->tree_view, current_column->model);
1071 * hildon_touch_selector_set_model:
1072 * @selector: a #HildonTouchSelector
1073 * @column: the position of the column to set the model to
1074 * @model: a #GtkTreeModel
1076 * Sets the #GtkTreeModel for a particular column in @model.
1079 hildon_touch_selector_set_model (HildonTouchSelector * selector,
1080 gint column, GtkTreeModel * model)
1082 g_return_if_fail (HILDON_TOUCH_SELECTOR (selector));
1083 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1085 HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->set_model (selector, column, model);
1089 * hildon_touch_selector_get_current_text:
1090 * @selector: a #HildonTouchSelector
1092 * Returns a string representing the currently selected items for
1093 * each column of @selector. See hildon_touch_selector_set_print_func().
1095 * Returns: a newly allocated string.
1098 hildon_touch_selector_get_current_text (HildonTouchSelector * selector)
1100 gchar *result = NULL;
1101 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1103 if (selector->priv->print_func) {
1104 result = (*selector->priv->print_func) (selector);
1106 result = _default_print_func (selector);
1113 _hildon_touch_selector_center_on_selected_items (gpointer data)
1115 HildonTouchSelector *selector = NULL;
1116 SelectorColumn *column = NULL;
1117 GSList *iter_column = NULL;
1123 HildonTouchSelectorSelectionMode selection_mode;
1125 /* ensure to center on the initial values */
1126 selector = HILDON_TOUCH_SELECTOR (data);
1128 selection_mode = hildon_touch_selector_get_column_selection_mode (selector);
1130 iter_column = selector->priv->columns;
1132 while (iter_column) {
1133 column = (SelectorColumn *) iter_column->data;
1136 && (selection_mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)) {
1139 if (hildon_touch_selector_get_selected (selector, i, &iter)) {
1140 path = gtk_tree_model_get_path (column->model, &iter);
1141 gtk_tree_view_get_background_area (GTK_TREE_VIEW
1142 (column->tree_view), path, NULL,
1145 gtk_tree_view_convert_bin_window_to_tree_coords (GTK_TREE_VIEW
1146 (column->tree_view), 0,
1149 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA
1150 (column->panarea), -1, y);
1152 gtk_tree_path_free (path);
1154 iter_column = iter_column->next;
1162 _hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
1164 HildonTouchSelectorSelectionMode mode = HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
1167 n_columns = hildon_touch_selector_get_num_columns (selector);
1168 mode = hildon_touch_selector_get_column_selection_mode (selector);
1170 return ((n_columns > 1) || (mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE));
1174 * hildon_touch_selector_has_multiple_selection:
1175 * @selector: A #HildonTouchSelector
1177 * Determines whether @selector is complex enough to actually require an
1178 * extra selection step than only picking an item. This is normally %TRUE
1179 * if @selector has multiple columns, multiple selection, or when it is a
1180 * more complex widget, like %HildonTouchSelectorEntry.
1182 * This information is useful for widgets containing a %HildonTouchSelector,
1183 * like #HildonPickerDialog, that could need a "Done" button, in case that
1184 * its internal #HildonTouchSelector has multiple columns, for instance.
1186 * Returns: %TRUE if @selector requires multiple selection steps.
1189 hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
1191 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
1193 return HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->has_multiple_selection (selector);