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 represent 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);
752 hildon_touch_selector_insert_column (HildonTouchSelector * selector, gint position)
754 g_warning ("Un-implemented!");
761 * hildon_touch_selector_get_num_columns:
762 * @selector: a #HildonTouchSelector
764 * Gets the number of columns in the #HildonTouchSelector.
766 * Returns: the number of columns in @selector.
769 hildon_touch_selector_get_num_columns (HildonTouchSelector * selector)
771 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), -1);
773 return g_slist_length (selector->priv->columns);
777 * hildon_touch_selector_get_column_selection_mode:
778 * @selector: a #HildonTouchSelector
780 * Gets the selection mode of @selector.
782 * Returns: one of #HildonTouchSelectorSelectionMode
784 HildonTouchSelectorSelectionMode
785 hildon_touch_selector_get_column_selection_mode (HildonTouchSelector * selector)
787 HildonTouchSelectorSelectionMode result =
788 HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
789 GtkSelectionMode treeview_mode = GTK_SELECTION_SINGLE;
790 SelectorColumn *column = NULL;
791 GtkTreeSelection *selection = NULL;
793 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), result);
794 g_return_val_if_fail (hildon_touch_selector_get_num_columns (selector) > 0,
797 column = (SelectorColumn *) selector->priv->columns->data;
799 selection = gtk_tree_view_get_selection (column->tree_view);
800 treeview_mode = gtk_tree_selection_get_mode (selection);
803 if (treeview_mode == GTK_SELECTION_MULTIPLE) {
804 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE;
806 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
813 * hildon_touch_selector_set_column_selection_mode:
814 * @selector: a #HildonTouchSelector
815 * @mode: the #HildonTouchSelectorMode for @selector
817 * Sets the selection mode for @selector. See #HildonTouchSelectorSelectionMode.
820 hildon_touch_selector_set_column_selection_mode (HildonTouchSelector * selector,
821 HildonTouchSelectorSelectionMode mode)
823 GtkTreeView *tv = NULL;
824 SelectorColumn *column = NULL;
825 GtkTreeSelection *selection = NULL;
826 GtkSelectionMode treeview_mode = GTK_SELECTION_MULTIPLE;
829 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
830 g_return_if_fail (hildon_touch_selector_get_num_columns (selector) > 0);
832 column = (SelectorColumn *) (g_slist_nth (selector->priv->columns, 0))->data;
833 tv = column->tree_view;
837 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE:
838 treeview_mode = GTK_SELECTION_SINGLE;
840 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE:
841 treeview_mode = GTK_SELECTION_MULTIPLE;
845 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
846 gtk_tree_selection_set_mode (selection, treeview_mode);
848 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
849 gtk_tree_model_get_iter_first (column->model, &iter);
850 gtk_tree_selection_unselect_all (selection);
851 gtk_tree_selection_select_iter (selection, &iter);
857 * hildon_touch_selector_set_print_func:
858 * @selector: a #HildonTouchSelector
859 * @func: a #HildonTouchSelectorPrintFunc function
861 * Sets the function to be used by hildon_touch_selector_get_current_text()
862 * to produce a text representation of the currently selected items in @selector.
863 * The default function will return a concatenation of comma separated items
864 * selected in each column in @selector. Use this to override this method if you
865 * need a particular representation for your application.
869 hildon_touch_selector_set_print_func (HildonTouchSelector * selector,
870 HildonTouchSelectorPrintFunc func)
872 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
874 selector->priv->print_func = func;
878 * hildon_touch_selector_get_print_func:
879 * @selector: a #HildonTouchSelector
881 * Gets the #HildonTouchSelectorPrintFunc currently used. See
882 * hildon_touch_selector_set_print_func().
884 * Returns: a #HildonTouchSelectorPrintFunc or %NULL if the default
885 * one is currently used.
887 HildonTouchSelectorPrintFunc
888 hildon_touch_selector_get_print_func (HildonTouchSelector * selector)
890 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
892 return selector->priv->print_func;
896 * hildon_touch_selector_get_selected:
897 * @selector: a #HildonTouchSelector
898 * @column: the column number we want to get the element
899 * @iter: #GtkTreeIter currently selected
901 * Sets @iter to the currently selected node on the nth-column, if selection is
902 * set to %HILDON_TOUCH_SELECTOR_SINGLE or %HILDON_TOUCH_SELECTOR_MULTIPLE with
903 * a column different that the first one. @iter may be %NULL if you just want to
904 * test if selection has any selected items.
906 * This function will not work if selection is in
907 * %HILDON_TOUCH_SELECTOR_MULTIPLE mode and the column is the first one.
909 * See gtk_tree_selection_get_selected() for more information.
911 * Returns: %TRUE if @iter was correctly set, %FALSE otherwise
914 hildon_touch_selector_get_selected (HildonTouchSelector * selector,
915 gint column, GtkTreeIter * iter)
917 GtkTreeSelection *selection = NULL;
918 SelectorColumn *current_column = NULL;
919 HildonTouchSelectorSelectionMode mode;
921 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
922 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
924 mode = hildon_touch_selector_get_column_selection_mode (selector);
926 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE) ||
927 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)&&(column>0)),
930 current_column = g_slist_nth_data (selector->priv->columns, column);
933 gtk_tree_view_get_selection (GTK_TREE_VIEW (current_column->tree_view));
935 return gtk_tree_selection_get_selected (selection, NULL, iter);
939 * hildon_touch_selector_select_iter
940 * @selector: a #HildonTouchSelector
941 * @column: the column to selects
942 * @iter: the #GtkTreeIter to be selected
943 * @scroll_to: whether to smoothly scroll to the item
945 * Sets the currently selected item in the column @column to the one pointed by @iter,
946 * optionally smoothly scrolling to it.
950 hildon_touch_selector_select_iter (HildonTouchSelector * selector,
951 gint column, GtkTreeIter * iter,
957 SelectorColumn *current_column = NULL;
958 GtkTreeSelection *selection = NULL;
961 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
962 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
964 current_column = g_slist_nth_data (selector->priv->columns, column);
966 selection = gtk_tree_view_get_selection (current_column->tree_view);
967 model = gtk_tree_view_get_model (current_column->tree_view);
968 path = gtk_tree_model_get_path (model, iter);
970 gtk_tree_selection_select_iter (selection, iter);
973 gtk_tree_view_get_background_area (current_column->tree_view,
975 gtk_tree_view_convert_bin_window_to_tree_coords (current_column->tree_view,
976 0, rect.y, NULL, &y);
977 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (current_column->panarea),
980 gtk_tree_path_free (path);
984 * hildon_touch_selector_unselect_iter
985 * @selector: a #HildonTouchSelector
986 * @column: the column to unselects from
987 * @iter: the #GtkTreeIter to be unselected
989 * Unselect the item pointed by @iter in the column @column
993 void hildon_touch_selector_unselect_iter (HildonTouchSelector * selector,
997 SelectorColumn *current_column = NULL;
998 GtkTreeSelection *selection = NULL;
1000 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1001 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1003 current_column = g_slist_nth_data (selector->priv->columns, column);
1004 selection = gtk_tree_view_get_selection (current_column->tree_view);
1005 gtk_tree_selection_unselect_iter (selection, iter);
1009 * hildon_touch_selector_get_selected_rows:
1010 * @selector: a #HildonTouchSelector
1011 * @column: the position of the column to get the selected rows from
1013 * Creates a list of #GtkTreePath<!-- -->s of all selected rows in a column. Additionally,
1014 * if you to plan to modify the model after calling this function, you may
1015 * want to convert the returned list into a list of GtkTreeRowReferences. To do this,
1016 * you can use gtk_tree_row_reference_new().
1018 * See gtk_tree_selection_get_selected_rows() for more information.
1020 * Returns: A new #GList containing a #GtkTreePath for each selected row in the column @column.
1024 hildon_touch_selector_get_selected_rows (HildonTouchSelector * selector,
1027 GList *result = NULL;
1028 SelectorColumn *current_column = NULL;
1029 GtkTreeSelection *selection = NULL;
1031 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1032 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1035 current_column = g_slist_nth_data (selector->priv->columns, column);
1036 selection = gtk_tree_view_get_selection (current_column->tree_view);
1038 result = gtk_tree_selection_get_selected_rows (selection, NULL);
1045 * hildon_touch_selector_get_model:
1046 * @selector: a #HildonTouchSelector
1047 * @column: the position of the column in @selector
1049 * Gets the model of a column of @selector.
1051 * Returns: the #GtkTreeModel for the column @column of @selector.
1054 hildon_touch_selector_get_model (HildonTouchSelector * selector, gint column)
1056 SelectorColumn *current_column = NULL;
1058 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1059 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1062 current_column = g_slist_nth_data (selector->priv->columns, column);
1064 return current_column->model;
1068 _hildon_touch_selector_set_model (HildonTouchSelector * selector,
1069 gint column, GtkTreeModel * model)
1071 SelectorColumn *current_column = NULL;
1072 g_print ("this was actually called\n");
1075 (SelectorColumn *) g_slist_nth_data (selector->priv->columns, column);
1077 current_column->model = model;
1078 gtk_tree_view_set_model (current_column->tree_view, current_column->model);
1082 * hildon_touch_selector_set_model:
1083 * @selector: a #HildonTouchSelector
1084 * @column: the position of the column to set the model to
1085 * @model: a #GtkTreeModel
1087 * Sets the #GtkTreeModel for a particular column in @model.
1090 hildon_touch_selector_set_model (HildonTouchSelector * selector,
1091 gint column, GtkTreeModel * model)
1093 g_return_if_fail (HILDON_TOUCH_SELECTOR (selector));
1094 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1096 HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->set_model (selector, column, model);
1100 * hildon_touch_selector_get_current_text:
1101 * @selector: a #HildonTouchSelector
1103 * Returns a string representing the currently selected items for
1104 * each column of @selector. See hildon_touch_selector_set_print_func().
1106 * Returns: a newly allocated string.
1109 hildon_touch_selector_get_current_text (HildonTouchSelector * selector)
1111 gchar *result = NULL;
1112 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1114 if (selector->priv->print_func) {
1115 result = (*selector->priv->print_func) (selector);
1117 result = _default_print_func (selector);
1124 _hildon_touch_selector_center_on_selected_items (gpointer data)
1126 HildonTouchSelector *selector = NULL;
1127 SelectorColumn *column = NULL;
1128 GSList *iter_column = NULL;
1134 HildonTouchSelectorSelectionMode selection_mode;
1136 /* ensure to center on the initial values */
1137 selector = HILDON_TOUCH_SELECTOR (data);
1139 selection_mode = hildon_touch_selector_get_column_selection_mode (selector);
1141 iter_column = selector->priv->columns;
1143 while (iter_column) {
1144 column = (SelectorColumn *) iter_column->data;
1147 && (selection_mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)) {
1150 if (hildon_touch_selector_get_selected (selector, i, &iter)) {
1151 path = gtk_tree_model_get_path (column->model, &iter);
1152 gtk_tree_view_get_background_area (GTK_TREE_VIEW
1153 (column->tree_view), path, NULL,
1156 gtk_tree_view_convert_bin_window_to_tree_coords (GTK_TREE_VIEW
1157 (column->tree_view), 0,
1160 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA
1161 (column->panarea), -1, y);
1163 gtk_tree_path_free (path);
1165 iter_column = iter_column->next;
1173 _hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
1175 HildonTouchSelectorSelectionMode mode = HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
1178 n_columns = hildon_touch_selector_get_num_columns (selector);
1179 mode = hildon_touch_selector_get_column_selection_mode (selector);
1181 return ((n_columns > 1) || (mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE));
1185 * hildon_touch_selector_has_multiple_selection:
1186 * @selector: A #HildonTouchSelector
1188 * Determines whether @selector is complex enough to actually require an
1189 * extra selection step than only picking an item. This is normally %TRUE
1190 * if @selector has multiple columns, multiple selection, or when it is a
1191 * more complex widget, like %HildonTouchSelectorEntry.
1193 * This information is useful for widgets containing a %HildonTouchSelector,
1194 * like #HildonPickerDialog, that could need a "Done" button, in case that
1195 * its internal #HildonTouchSelector has multiple columns, for instance.
1197 * Returns: %TRUE if @selector requires multiple selection steps.
1200 hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
1202 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
1204 return HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->has_multiple_selection (selector);