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.
49 * The current selection has a string representation. In the most common cases,
50 * each column model will contain a text column. You can configure
51 * which column in particular using the #HildonTouchSelectorColumn property
52 * #HildonTouchSelectorColumn:text-column
54 * You can get this string representation using
55 * hildon_touch_selector_get_current_text().
56 * You can configure how the selection is printed with
57 * hildon_touch_selector_set_print_func(), that sets the current hildon touch
58 * selector print function. The widget has a default print function, that
59 * uses the #HildonTouchSelectorColumn:text-column property on each
60 * #HildonTouchSelectorColumn to compose the final representation.
62 * If you create the selector using hildon_touch_selector_new_text() you
63 * don't need to take care of this property, as the model is created internally.
64 * If you create the selector using hildon_touch_selector_new(), you
65 * need to specify properly the property for your custom model in order to get a
66 * non-empty string representation, or define your custom print function.
69 * <title>Creating a HildonTouchSelector</title>
72 * selection_changed (HildonTouchSelector * selector,
73 * gpointer *user_data)
75 * gchar *current_selection = NULL;
77 * current_selection = hildon_touch_selector_get_current_text (selector);
78 * g_debug ("Current selection : %s", current_selection);
82 * create_customized_selector ()
84 * GtkWidget *selector = NULL;
85 * GSList *icon_list = NULL;
86 * GtkListStore *store_icons = NULL;
87 * GSList *item = NULL;
88 * GtkCellRenderer *renderer = NULL;
89 * HildonTouchSelectorColumn *column = NULL;
91 * selector = hildon_touch_selector_new ();
93 * icon_list = gtk_stock_list_ids ();
95 * store_icons = gtk_list_store_new (1, G_TYPE_STRING);
96 * for (item = icon_list; item; item = g_slist_next (item)) {
98 * gchar *label = item->data;
100 * gtk_list_store_append (store_icons, &iter);
101 * gtk_list_store_set (store_icons, &iter, 0, label, -1);
104 * g_slist_free (icon_list);
106 * renderer = gtk_cell_renderer_pixbuf_new ();
107 * gtk_cell_renderer_set_fixed_size (renderer, -1, 100);
109 * column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector),
110 * GTK_TREE_MODEL (store_icons),
111 * renderer, "stock-id", 0, NULL);
113 * g_object_set (G_OBJECT (column), "text-column", 0, NULL);
115 * hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
116 * HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
118 * g_signal_connect (G_OBJECT (selector), "changed",
119 * G_CALLBACK (selection_changed), NULL);
125 * create_simple_selector ()
127 * GtkWidget *selector = NULL;
130 * selector = hildon_touch_selector_new_text ();
131 * hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
132 * HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
134 * g_signal_connect (G_OBJECT (selector), "changed",
135 * G_CALLBACK (selection_changed), NULL);
137 * for (i = 1; i <= 10 ; i++) {
138 * gchar *label = g_strdup_printf ("Item &percnt;d", i);
140 * hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
153 * SECTION:hildon-touch-selector-column
154 * @short_description: A visible column in a #HildonTouchSelector
156 * #HildonTouchSelectorColumn object represents a visible column in
157 * #HildonTouchSelector. It allows to manage the cell renderers related to each
161 #undef HILDON_DISABLE_DEPRECATED
170 #include "hildon-gtk.h"
172 #include "hildon-pannable-area.h"
173 #include "hildon-touch-selector.h"
174 #include "hildon-touch-selector-private.h"
176 #define HILDON_TOUCH_SELECTOR_GET_PRIVATE(obj) \
177 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_TOUCH_SELECTOR, HildonTouchSelectorPrivate))
179 G_DEFINE_TYPE (HildonTouchSelector, hildon_touch_selector, GTK_TYPE_VBOX)
182 * IMPLEMENTATION NOTES:
183 * Struct to maintain the data of each column. The columns are the elements
184 * of the widget that belongs properly to the selection behaviour. Although
185 * internally the columns are arranged in a private #GtkHBox, as the selector
186 * itself is a #GtkVBox, you can add more widgets, like buttons etc., so
187 * you finally could have a widget with more elements that the columns, but
188 * this doesn't belongs to the selection logic
190 struct _HildonTouchSelectorColumnPrivate
192 HildonTouchSelector *parent; /* the selector that contains this column */
195 GtkTreeView *tree_view;
196 gulong realize_handler;
197 GtkTreePath *initial_path;
199 GtkWidget *panarea; /* the pannable widget */
202 struct _HildonTouchSelectorPrivate
204 GSList *columns; /* the selection columns */
205 GtkWidget *hbox; /* the container for the selector's columns */
206 gboolean initial_scroll; /* whether initial fancy scrolling to selection */
208 gboolean changed_blocked;
210 HildonTouchSelectorPrintFunc print_func;
211 gpointer print_user_data;
212 GDestroyNotify print_destroy_func;
217 PROP_HAS_MULTIPLE_SELECTION = 1,
228 static gint hildon_touch_selector_signals[LAST_SIGNAL] = { 0 };
231 hildon_touch_selector_dispose (GObject * object);
234 hildon_touch_selector_get_property (GObject * object,
239 hildon_touch_selector_set_property (GObject *object,
246 static void hildon_touch_selector_remove (GtkContainer * container,
248 /* private functions */
249 static void _row_tapped_cb (GtkTreeView * tree_view,
252 static gchar *_default_print_func (HildonTouchSelector * selector,
255 static HildonTouchSelectorColumn *_create_new_column (HildonTouchSelector * selector,
256 GtkTreeModel * model,
257 gboolean *emit_changed,
258 GtkCellRenderer * renderer,
261 on_realize_cb (GtkWidget *widget,
264 on_row_changed (GtkTreeModel *model,
270 hildon_touch_selector_scroll_to (HildonTouchSelectorColumn *column,
274 _hildon_touch_selector_center_on_selected_items (HildonTouchSelector *selector,
275 HildonTouchSelectorColumn *column);
277 _hildon_touch_selector_set_model (HildonTouchSelector * selector,
279 GtkTreeModel * model);
281 _hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector);
284 hildon_touch_selector_emit_value_changed (HildonTouchSelector *selector,
287 /* GtkCellLayout implementation (HildonTouchSelectorColumn)*/
288 static void hildon_touch_selector_column_cell_layout_init (GtkCellLayoutIface *iface);
290 static void hildon_touch_selector_column_cell_layout_pack_start (GtkCellLayout *cell_layout,
291 GtkCellRenderer *cell,
293 static void hildon_touch_selector_column_cell_layout_pack_end (GtkCellLayout *cell_layout,
294 GtkCellRenderer *cell,
296 static void hildon_touch_selector_column_cell_layout_clear (GtkCellLayout *cell_layout);
297 static void hildon_touch_selector_column_cell_layout_add_attribute(GtkCellLayout *cell_layout,
298 GtkCellRenderer *cell,
299 const gchar *attribute,
301 static void hildon_touch_selector_column_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
302 GtkCellRenderer *cell,
303 GtkCellLayoutDataFunc func,
305 GDestroyNotify destroy);
306 static void hildon_touch_selector_column_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
307 GtkCellRenderer *cell);
308 static void hildon_touch_selector_column_cell_layout_reorder (GtkCellLayout *cell_layout,
309 GtkCellRenderer *cell,
311 static GList *hildon_touch_selector_column_cell_layout_get_cells (GtkCellLayout *cell_layout);
315 hildon_touch_selector_class_init (HildonTouchSelectorClass * class)
317 GObjectClass *gobject_class;
318 GtkObjectClass *object_class;
319 GtkContainerClass *container_class;
321 gobject_class = G_OBJECT_CLASS (class);
322 object_class = GTK_OBJECT_CLASS (class);
323 container_class = GTK_CONTAINER_CLASS (class);
326 gobject_class->dispose = hildon_touch_selector_dispose;
327 gobject_class->get_property = hildon_touch_selector_get_property;
328 gobject_class->set_property = hildon_touch_selector_set_property;
333 container_class->remove = hildon_touch_selector_remove;
335 /* HildonTouchSelector */
336 class->changed = NULL;
337 class->set_model = _hildon_touch_selector_set_model;
339 class->has_multiple_selection = _hildon_touch_selector_has_multiple_selection;
343 * HildonTouchSelector::changed:
344 * @widget: the object which received the signal
345 * @column: the number of the column that has changed
347 * The "changed" signal is emitted when the active item on any column is changed.
348 * This can be due to the user selecting a different item from the list, or
349 * due to a call to hildon_touch_selector_select_iter() on one of the columns.
353 hildon_touch_selector_signals[CHANGED] =
354 g_signal_new ("changed",
355 G_OBJECT_CLASS_TYPE (class),
357 G_STRUCT_OFFSET (HildonTouchSelectorClass, changed),
359 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
362 * HildonTouchSelector::columns-changed:
363 * @selector: the object which received the signal
365 * The "columns-changed" signal is emitted when the number
366 * of columns in the #HildonTouchSelector change.
370 hildon_touch_selector_signals[COLUMNS_CHANGED] =
371 g_signal_new ("columns-changed",
372 G_OBJECT_CLASS_TYPE (class),
373 G_SIGNAL_RUN_LAST, 0,
375 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
379 g_object_class_install_property (gobject_class, PROP_HAS_MULTIPLE_SELECTION,
380 g_param_spec_boolean ("has-multiple-selection",
381 "has multiple selection",
382 "Whether the widget has multiple "
383 "selection (like multiple columns, "
384 "multiselection mode, or multiple "
385 "internal widgets) and therefore "
386 "it may need a confirmation button, "
391 g_object_class_install_property (G_OBJECT_CLASS (gobject_class),
393 g_param_spec_boolean ("initial-scroll",
395 "Whether to scroll to the"
396 "current selection when"
397 "the selector is first"
400 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
402 /* style properties */
403 /* We need to ensure fremantle mode for the treeview in order to work
404 properly. This is not about the appearance, this is about behaviour */
405 gtk_rc_parse_string ("style \"fremantle-htst\" {\n"
406 " GtkWidget::hildon-mode = 1\n"
407 "} widget \"*.fremantle-htst\" style \"fremantle-htst\""
408 "widget_class \"*<HildonPannableArea>.GtkTreeView\" style :highest \"fremantle-htst\"");
410 g_type_class_add_private (object_class, sizeof (HildonTouchSelectorPrivate));
414 hildon_touch_selector_get_property (GObject * object,
416 GValue * value, GParamSpec * pspec)
418 HildonTouchSelectorPrivate *priv = HILDON_TOUCH_SELECTOR (object)->priv;
421 case PROP_HAS_MULTIPLE_SELECTION:
422 g_value_set_boolean (value,
423 hildon_touch_selector_has_multiple_selection (HILDON_TOUCH_SELECTOR (object)));
425 case PROP_INITIAL_SCROLL:
426 g_value_set_boolean (value, priv->initial_scroll);
429 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 hildon_touch_selector_set_property (GObject *object, guint prop_id,
436 const GValue *value, GParamSpec *pspec)
438 HildonTouchSelectorPrivate *priv = HILDON_TOUCH_SELECTOR (object)->priv;
441 case PROP_INITIAL_SCROLL:
442 priv->initial_scroll = g_value_get_boolean (value);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 hildon_touch_selector_init (HildonTouchSelector * selector)
454 selector->priv = HILDON_TOUCH_SELECTOR_GET_PRIVATE (selector);
456 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (selector), GTK_NO_WINDOW);
457 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (selector), FALSE);
459 selector->priv->columns = NULL;
461 selector->priv->print_func = NULL;
462 selector->priv->print_user_data = NULL;
463 selector->priv->print_destroy_func = NULL;
464 selector->priv->initial_scroll = TRUE;
465 selector->priv->hbox = gtk_hbox_new (FALSE, 0);
467 selector->priv->changed_blocked = FALSE;
469 gtk_box_pack_end (GTK_BOX (selector), selector->priv->hbox,
471 gtk_widget_show (selector->priv->hbox);
475 hildon_touch_selector_dispose (GObject * object)
477 GObjectClass *gobject_class;
479 hildon_touch_selector_set_print_func_full (HILDON_TOUCH_SELECTOR (object),
482 gobject_class = G_OBJECT_CLASS (hildon_touch_selector_parent_class);
484 if (gobject_class->dispose)
485 (* gobject_class->dispose) (object);
489 disconnect_model_handlers (HildonTouchSelectorColumn *col, HildonTouchSelector *selector)
491 g_signal_handlers_disconnect_by_func (col->priv->model,
492 on_row_changed, selector);
496 * IMPLEMENTATION NOTES:
497 * Some people sent questions regarding a missing dispose/finalize function on
498 * this widget that could lead to leak memory, so we will clarify this topic.
500 * This is not required as #HildonTouchSelector extends #GtkContainer. When the
501 * widget is freed, the #GtkContainer freeing memory functions are called. This
502 * process includes remove each widget individually, so all the widgets are
505 * In the same way, this widget redefines gtk_container->remove function, in
506 * order to free the column related information if it is required.
508 * Please take a look to hildon_touch_selector_remove for more information.
511 /*------------------------------ GtkContainer ------------------------------ */
514 * Required in order to free the column at the columns list
517 hildon_touch_selector_remove (GtkContainer * container, GtkWidget * widget)
519 HildonTouchSelector *selector = NULL;
521 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (container));
522 selector = HILDON_TOUCH_SELECTOR (container);
524 /* Remove the extra data related to the columns, if required. */
525 if (widget == selector->priv->hbox) {
526 g_slist_foreach (selector->priv->columns, (GFunc) disconnect_model_handlers, selector);
527 g_slist_foreach (selector->priv->columns, (GFunc) g_object_unref, NULL);
529 g_slist_free (selector->priv->columns);
531 selector->priv->columns = NULL;
533 g_debug ("Freeing a widget outside the columns logic");
536 /* Now remove the widget itself from the container */
537 GTK_CONTAINER_CLASS (hildon_touch_selector_parent_class)->remove (container, widget);
540 /* ------------------------------ PRIVATE METHODS ---------------------------- */
542 hildon_touch_selector_block_changed (HildonTouchSelector *selector)
544 selector->priv->changed_blocked = TRUE;
548 hildon_touch_selector_unblock_changed (HildonTouchSelector *selector)
550 selector->priv->changed_blocked = FALSE;
554 hildon_touch_selector_emit_value_changed (HildonTouchSelector *selector,
557 if (!selector->priv->changed_blocked) {
558 g_signal_emit (selector, hildon_touch_selector_signals[CHANGED], 0, column);
563 * default_print_func:
564 * @selector: a #HildonTouchSelector
566 * Default print function
568 * Returns: a new string that represents the selected items
573 _default_print_func (HildonTouchSelector * selector, gpointer user_data)
575 gchar *result = NULL;
577 gint num_columns = 0;
579 GtkTreeModel *model = NULL;
580 gchar *current_string = NULL;
582 HildonTouchSelectorSelectionMode mode;
584 GtkTreePath *current_path = NULL;
585 GList *selected_rows = NULL;
586 gint initial_value = 0;
587 gint text_column = -1;
588 HildonTouchSelectorColumn *column = NULL;
590 num_columns = hildon_touch_selector_get_num_columns (selector);
592 mode = hildon_touch_selector_get_column_selection_mode (selector);
594 if ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)
595 && (num_columns > 0)) {
596 /* In this case we get the first column first */
597 selected_rows = hildon_touch_selector_get_selected_rows (selector, 0);
598 model = hildon_touch_selector_get_model (selector, 0);
599 column = hildon_touch_selector_get_column (selector, 0);
600 g_object_get (G_OBJECT(column), "text-column", &text_column, NULL);
602 result = g_strdup_printf ("(");
604 for (item = selected_rows; item; item = g_list_next (item)) {
605 current_path = item->data;
606 gtk_tree_model_get_iter (model, &iter, current_path);
608 if (text_column != -1) {
609 gtk_tree_model_get (model, &iter, text_column, ¤t_string, -1);
612 if (i < g_list_length (selected_rows) - 1) {
613 aux = g_strconcat (result, current_string, ",", NULL);
617 aux = g_strconcat (result, current_string, NULL);
622 if (current_string) {
623 g_free (current_string);
624 current_string = NULL;
630 aux = g_strconcat (result, ")", NULL);
634 g_list_foreach (selected_rows, (GFunc) (gtk_tree_path_free), NULL);
635 g_list_free (selected_rows);
641 for (i = initial_value; i < num_columns; i++) {
642 model = hildon_touch_selector_get_model (selector, i);
643 column = hildon_touch_selector_get_column (selector, i);
644 g_object_get (G_OBJECT(column), "text-column", &text_column, NULL);
646 if (hildon_touch_selector_get_selected (selector, i, &iter)) {
647 if (text_column == -1 ) {
648 g_warning ("Trying to use the default print function in HildonTouchSelector, but "
649 "\"text-column\" property is not set for HildonTouchSelectorColumn %p.", column);
650 current_string = NULL;
652 gtk_tree_model_get (model, &iter, text_column, ¤t_string, -1);
656 result = current_string;
658 aux = g_strconcat (result, ":", current_string, NULL);
660 g_free (current_string);
661 current_string = NULL;
671 _row_tapped_cb (GtkTreeView * tree_view, GtkTreePath * path, gpointer user_data)
673 HildonTouchSelector *selector = NULL;
674 HildonTouchSelectorColumn *column = NULL;
675 gint num_column = -1;
677 column = HILDON_TOUCH_SELECTOR_COLUMN (user_data);
678 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (column->priv->parent));
680 selector = column->priv->parent;
682 num_column = g_slist_index (selector->priv->columns, column);
684 hildon_touch_selector_emit_value_changed (selector, num_column);
688 static HildonTouchSelectorColumn *
689 _create_new_column (HildonTouchSelector * selector,
690 GtkTreeModel * model,
691 gboolean *emit_changed,
692 GtkCellRenderer * renderer, va_list args)
694 HildonTouchSelectorColumn *new_column = NULL;
695 GtkTreeViewColumn *tree_column = NULL;
696 GtkTreeView *tv = NULL;
697 GtkWidget *panarea = NULL;
698 GtkTreeSelection *selection = NULL;
703 tree_column = gtk_tree_view_column_new ();
705 if (renderer != NULL) {
706 gtk_tree_view_column_pack_start (tree_column, renderer, TRUE);
708 attribute = va_arg (args, gchar *);
709 while (attribute != NULL) {
710 value = va_arg (args, gint);
711 gtk_tree_view_column_add_attribute (tree_column, renderer, attribute,
713 attribute = va_arg (args, gchar *);
718 tv = GTK_TREE_VIEW (hildon_gtk_tree_view_new (HILDON_UI_MODE_EDIT));
720 tv = GTK_TREE_VIEW (gtk_tree_view_new ());
721 #endif /* MAEMO_GTK */
723 gtk_tree_view_set_enable_search (tv, FALSE);
724 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (tv), GTK_CAN_FOCUS);
726 gtk_tree_view_set_model (tv, model);
727 g_signal_connect (model, "row-changed",
728 G_CALLBACK (on_row_changed), selector);
729 gtk_tree_view_set_rules_hint (tv, TRUE);
731 gtk_tree_view_append_column (GTK_TREE_VIEW (tv), tree_column);
733 new_column = g_object_new (HILDON_TYPE_TOUCH_SELECTOR_COLUMN, NULL);
734 new_column->priv->parent = selector;
736 panarea = hildon_pannable_area_new ();
738 g_object_set (G_OBJECT (panarea),
739 "initial-hint", FALSE, NULL);
741 gtk_container_add (GTK_CONTAINER (panarea), GTK_WIDGET (tv));
743 new_column->priv->model = model;
744 new_column->priv->tree_view = tv;
745 new_column->priv->panarea = panarea;
746 new_column->priv->realize_handler = 0;
747 new_column->priv->initial_path = NULL;
749 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
750 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
752 /* select the first item */
753 *emit_changed = FALSE;
754 if (gtk_tree_model_get_iter_first (model, &iter)) {
755 gtk_tree_selection_select_iter (selection, &iter);
756 *emit_changed = TRUE;
759 gtk_widget_grab_focus (GTK_WIDGET (tv));
761 /* connect to the hildon-row-tapped signal connection */
762 g_signal_connect (G_OBJECT (tv), "hildon-row-tapped",
763 G_CALLBACK (_row_tapped_cb), new_column);
769 /* ------------------------ HildonTouchSelectorColumn implementation ---------------------- */
770 G_DEFINE_TYPE_WITH_CODE (HildonTouchSelectorColumn, hildon_touch_selector_column, G_TYPE_OBJECT,
771 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
772 hildon_touch_selector_column_cell_layout_init))
780 hildon_touch_selector_column_class_init (HildonTouchSelectorColumnClass *klass);
783 hildon_touch_selector_column_get_property (GObject *object, guint property_id,
784 GValue *value, GParamSpec *pspec);
787 hildon_touch_selector_column_set_property (GObject *object, guint property_id,
788 const GValue *value, GParamSpec *pspec);
791 hildon_touch_selector_column_set_text_column (HildonTouchSelectorColumn *column,
794 hildon_touch_selector_column_get_text_column (HildonTouchSelectorColumn *column);
798 hildon_touch_selector_column_class_init (HildonTouchSelectorColumnClass *klass)
800 GObjectClass *gobject_class = NULL;
802 gobject_class = G_OBJECT_CLASS (klass);
804 g_type_class_add_private (gobject_class, sizeof (HildonTouchSelectorColumnPrivate));
807 gobject_class->get_property = hildon_touch_selector_column_get_property;
808 gobject_class->set_property = hildon_touch_selector_column_set_property;
810 g_object_class_install_property (G_OBJECT_CLASS(klass),
812 g_param_spec_int ("text-column",
814 "A column in the data source model to get the strings from.",
822 hildon_touch_selector_column_init (HildonTouchSelectorColumn *column)
824 column->priv = G_TYPE_INSTANCE_GET_PRIVATE (column, HILDON_TYPE_TOUCH_SELECTOR_COLUMN,
825 HildonTouchSelectorColumnPrivate);
826 column->priv->text_column = -1;
830 hildon_touch_selector_column_set_text_column (HildonTouchSelectorColumn *column,
833 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (column));
834 g_return_if_fail (text_column >= -1);
836 column->priv->text_column = text_column;
838 g_object_notify (G_OBJECT (column), "text-column");
842 hildon_touch_selector_column_get_text_column (HildonTouchSelectorColumn *column)
844 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (column), -1);
846 return column->priv->text_column;
850 hildon_touch_selector_column_get_property (GObject *object, guint property_id,
851 GValue *value, GParamSpec *pspec)
853 switch (property_id) {
854 case PROP_TEXT_COLUMN:
855 g_value_set_int (value,
856 hildon_touch_selector_column_get_text_column (HILDON_TOUCH_SELECTOR_COLUMN (object)));
859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
864 hildon_touch_selector_column_set_property (GObject *object, guint property_id,
865 const GValue *value, GParamSpec *pspec)
867 switch (property_id) {
868 case PROP_TEXT_COLUMN:
869 hildon_touch_selector_column_set_text_column (HILDON_TOUCH_SELECTOR_COLUMN (object),
870 g_value_get_int (value));
873 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
877 /* ------------------------ GtkCellLayout implementation -------------------- */
879 hildon_touch_selector_column_cell_layout_init (GtkCellLayoutIface *iface)
881 iface->pack_start = hildon_touch_selector_column_cell_layout_pack_start;
882 iface->pack_end = hildon_touch_selector_column_cell_layout_pack_end;
883 iface->clear = hildon_touch_selector_column_cell_layout_clear;
884 iface->add_attribute = hildon_touch_selector_column_cell_layout_add_attribute;
885 iface->set_cell_data_func = hildon_touch_selector_column_cell_layout_set_cell_data_func;
886 iface->clear_attributes = hildon_touch_selector_column_cell_layout_clear_attributes;
887 iface->reorder = hildon_touch_selector_column_cell_layout_reorder;
888 iface->get_cells = hildon_touch_selector_column_cell_layout_get_cells;
892 hildon_touch_selector_column_cell_layout_pack_start (GtkCellLayout *cell_layout,
893 GtkCellRenderer *cell,
896 HildonTouchSelectorColumn *sel_column = NULL;
897 GtkTreeViewColumn *view_column = NULL;
899 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
900 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
902 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
904 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(view_column), cell, expand);
909 hildon_touch_selector_column_cell_layout_pack_end (GtkCellLayout *cell_layout,
910 GtkCellRenderer *cell,
913 HildonTouchSelectorColumn *sel_column = NULL;
914 GtkTreeViewColumn *view_column = NULL;
916 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
917 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
919 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
921 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT(view_column), cell, expand);
925 hildon_touch_selector_column_cell_layout_clear (GtkCellLayout *cell_layout)
927 HildonTouchSelectorColumn *sel_column = NULL;
928 GtkTreeViewColumn *view_column = NULL;
930 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
931 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
933 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
935 gtk_cell_layout_clear (GTK_CELL_LAYOUT(view_column));
939 hildon_touch_selector_column_cell_layout_add_attribute (GtkCellLayout *cell_layout,
940 GtkCellRenderer *cell,
941 const gchar *attribute,
944 HildonTouchSelectorColumn *sel_column = NULL;
945 GtkTreeViewColumn *view_column = NULL;
947 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
948 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
950 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
952 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT(view_column), cell, attribute, column);
957 hildon_touch_selector_column_cell_layout_set_cell_data_func (GtkCellLayout *cell_layout,
958 GtkCellRenderer *cell,
959 GtkCellLayoutDataFunc func,
961 GDestroyNotify destroy)
963 HildonTouchSelectorColumn *sel_column = NULL;
964 GtkTreeViewColumn *view_column = NULL;
966 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
967 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
969 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
971 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT(view_column), cell, func,
976 hildon_touch_selector_column_cell_layout_clear_attributes (GtkCellLayout *cell_layout,
977 GtkCellRenderer *cell)
979 HildonTouchSelectorColumn *sel_column = NULL;
980 GtkTreeViewColumn *view_column = NULL;
982 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
983 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
985 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
987 gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (view_column), cell);
991 hildon_touch_selector_column_cell_layout_reorder (GtkCellLayout *cell_layout,
992 GtkCellRenderer *cell,
995 HildonTouchSelectorColumn *sel_column = NULL;
996 GtkTreeViewColumn *view_column = NULL;
998 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout));
999 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
1001 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
1003 gtk_cell_layout_reorder (GTK_CELL_LAYOUT(view_column), cell, position);
1007 hildon_touch_selector_column_cell_layout_get_cells (GtkCellLayout *cell_layout)
1009 HildonTouchSelectorColumn *sel_column = NULL;
1010 GtkTreeViewColumn *view_column = NULL;
1012 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR_COLUMN (cell_layout), NULL);
1013 sel_column = HILDON_TOUCH_SELECTOR_COLUMN (cell_layout);
1015 view_column = gtk_tree_view_get_column (sel_column->priv->tree_view, 0);
1017 return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT(view_column));
1020 /* ------------------------------ PUBLIC METHODS ---------------------------- */
1023 * hildon_touch_selector_new:
1025 * Creates a new empty #HildonTouchSelector.
1027 * Returns: a new #HildonTouchSelector.
1032 hildon_touch_selector_new (void)
1034 return g_object_new (HILDON_TYPE_TOUCH_SELECTOR, NULL);
1038 * hildon_touch_selector_new_text:
1040 * Creates a #HildonTouchSelector with a single text column that
1041 * can be populated conveniently through hildon_touch_selector_append_text(),
1042 * hildon_touch_selector_prepend_text(), hildon_touch_selector_insert_text().
1044 * Returns: A new #HildonTouchSelector
1049 hildon_touch_selector_new_text (void)
1051 GtkWidget *selector;
1052 GtkListStore *store;
1053 HildonTouchSelectorColumn *column = NULL;
1055 selector = hildon_touch_selector_new ();
1056 store = gtk_list_store_new (1, G_TYPE_STRING);
1058 column = hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
1059 GTK_TREE_MODEL (store), TRUE);
1061 g_object_set (G_OBJECT (column), "text-column", 0, NULL);
1067 * hildon_touch_selector_append_text:
1068 * @selector: A #HildonTouchSelector.
1069 * @text: a non %NULL text string.
1071 * Appends a new entry in a #HildonTouchSelector created with
1072 * hildon_touch_selector_new_text().
1077 hildon_touch_selector_append_text (HildonTouchSelector * selector,
1081 GtkTreeModel *model;
1083 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1084 g_return_if_fail (text != NULL);
1086 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
1088 g_return_if_fail (GTK_IS_LIST_STORE (model));
1090 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
1091 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
1095 * hildon_touch_selector_prepend_text:
1096 * @selector: A #HildonTouchSelector.
1097 * @text: a non %NULL text string.
1099 * Prepends a new entry in a #HildonTouchSelector created with
1100 * hildon_touch_selector_new_text().
1105 hildon_touch_selector_prepend_text (HildonTouchSelector * selector,
1109 GtkTreeModel *model;
1111 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1112 g_return_if_fail (text != NULL);
1114 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
1116 g_return_if_fail (GTK_IS_LIST_STORE (model));
1118 gtk_list_store_prepend (GTK_LIST_STORE (model), &iter);
1119 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
1123 * hildon_touch_selector_insert_text:
1124 * @selector: a #HildonTouchSelector.
1125 * @position: the position to insert @text.
1126 * @text: A non %NULL text string.
1128 * Inserts a new entry in a particular position of a
1129 * #HildonTouchSelector created with hildon_touch_selector_new_text().
1134 hildon_touch_selector_insert_text (HildonTouchSelector * selector,
1135 gint position, const gchar * text)
1138 GtkTreeModel *model;
1140 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1141 g_return_if_fail (text != NULL);
1142 g_return_if_fail (position >= 0);
1144 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
1146 g_return_if_fail (GTK_IS_LIST_STORE (model));
1148 gtk_list_store_insert (GTK_LIST_STORE (model), &iter, position);
1149 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, text, -1);
1153 * hildon_touch_selector_append_column
1154 * @selector: a #HildonTouchSelector
1155 * @model: the #GtkTreeModel with the data of the column
1156 * @cell_renderer: The #GtkCellRenderer where to draw each row contents.
1157 * @Varargs: a %NULL-terminated pair of attributes and column numbers.
1159 * This functions adds a new column to the widget, whose data will
1160 * be obtained from the model. Only widgets added this way should used on
1161 * the selection logic, i.e., the print function, the #HildonTouchPicker::changed
1164 * You can optionally pass a #GtkCellRenderer in @cell_renderer,
1165 * together with a %NULL-terminated list of pairs property/value, in
1166 * the same way you would use gtk_tree_view_column_set_attributes().
1167 * This will pack @cell_renderer at the start of the column, expanded by default.
1168 * If you prefer not to add it this way, you can simply pass %NULL to @cell_renderer
1169 * and use the #GtkCellLayout interface on the returned #HildonTouchSelectorColumn
1170 * to set your renderers.
1172 * There is a prerequisite to be considered on models used: text data must
1173 * be in the first column.
1175 * This method basically adds a #GtkTreeView to the widget, using the model and
1176 * the data received.
1178 * Returns: the new column added added, %NULL otherwise.
1183 HildonTouchSelectorColumn*
1184 hildon_touch_selector_append_column (HildonTouchSelector * selector,
1185 GtkTreeModel * model,
1186 GtkCellRenderer * cell_renderer, ...)
1189 HildonTouchSelectorColumn *new_column = NULL;
1190 gboolean emit_changed = FALSE;
1193 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1194 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1196 if (model != NULL) {
1198 va_start (args, cell_renderer);
1199 new_column = _create_new_column (selector, model, &emit_changed, cell_renderer, args);
1202 selector->priv->columns = g_slist_append (selector->priv->columns,
1204 gtk_box_pack_start (GTK_BOX (selector->priv->hbox),
1205 new_column->priv->panarea,
1208 gtk_widget_show_all (new_column->priv->panarea);
1210 if (selector->priv->initial_scroll) {
1211 _hildon_touch_selector_center_on_selected_items (selector, new_column);
1218 g_signal_emit (selector, hildon_touch_selector_signals[COLUMNS_CHANGED], 0);
1220 colnum = g_slist_length (selector->priv->columns);
1221 hildon_touch_selector_emit_value_changed (selector, colnum);
1228 * hildon_touch_selector_append_text_column
1229 * @selector: a #HildonTouchSelector
1230 * @model: a #GtkTreeModel with data for the column
1231 * @center: whether to center the text on the column
1233 * Equivalent to hildon_touch_selector_append_column(), but using a
1234 * default text cell renderer. This is the most common use case of the
1237 * Returns: the new column added, NULL otherwise.
1241 HildonTouchSelectorColumn*
1242 hildon_touch_selector_append_text_column (HildonTouchSelector * selector,
1243 GtkTreeModel * model, gboolean center)
1245 gfloat xalign = center ? 0.5 : 0.0;
1246 GtkCellRenderer *renderer;
1248 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1249 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
1251 renderer = gtk_cell_renderer_text_new ();
1253 g_object_set (renderer,
1258 return hildon_touch_selector_append_column (selector, model, renderer,
1263 * hildon_touch_selector_remove_column:
1264 * @selector: a #HildonTouchSelector
1265 * @column: the position of the column to be removed
1267 * Removes a column from @selector.
1269 * Returns: %TRUE if the column was removed, %FALSE otherwise
1274 hildon_touch_selector_remove_column (HildonTouchSelector * selector, gint column)
1276 HildonTouchSelectorColumn *current_column = NULL;
1277 HildonTouchSelectorPrivate *priv;
1279 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
1280 g_return_val_if_fail (column <
1281 hildon_touch_selector_get_num_columns (selector), FALSE);
1283 priv = HILDON_TOUCH_SELECTOR_GET_PRIVATE (selector);
1284 current_column = g_slist_nth_data (priv->columns, column);
1286 gtk_container_remove (GTK_CONTAINER (priv->hbox), current_column->priv->panarea);
1287 priv->columns = g_slist_remove (priv->columns, current_column);
1288 g_object_unref (current_column);
1290 g_signal_emit (selector, hildon_touch_selector_signals[COLUMNS_CHANGED], 0);
1296 * hildon_touch_selector_set_column_attributes:
1297 * @selector: a #HildonTouchSelector
1298 * @num_column: the number of the column whose attributes we're setting
1299 * @cell_renderer: the #GtkCellRendere we're setting the attributes of
1300 * @Varargs: A %NULL-terminated list of attributes.
1302 * Sets the attributes for the given column. The attributes must be given
1303 * in attribute/column pairs, just like in gtk_tree_view_column_set_attributes().
1304 * All existing attributes are removed and replaced with the new ones.
1306 * Deprecated: #HildonTouchSelectorColumn implements #GtkCellLayout, use this
1307 * interface instead. See
1308 * hildon_touch_selector_get_column().
1313 hildon_touch_selector_set_column_attributes (HildonTouchSelector * selector,
1315 GtkCellRenderer * cell_renderer,
1319 GtkTreeViewColumn *tree_column = NULL;
1320 HildonTouchSelectorColumn *current_column = NULL;
1321 gchar *attribute = NULL;
1324 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1325 g_return_if_fail (num_column <
1326 hildon_touch_selector_get_num_columns (selector));
1328 current_column = g_slist_nth_data (selector->priv->columns, num_column);
1330 tree_column = gtk_tree_view_get_column (current_column->priv->tree_view, 0);
1331 gtk_tree_view_remove_column (current_column->priv->tree_view, tree_column);
1333 tree_column = gtk_tree_view_column_new ();
1334 gtk_tree_view_column_pack_start (tree_column, cell_renderer, TRUE);
1336 va_start (args, cell_renderer);
1337 attribute = va_arg (args, gchar *);
1339 gtk_tree_view_column_clear_attributes (tree_column, cell_renderer);
1341 while (attribute != NULL) {
1342 value = va_arg (args, gint);
1343 gtk_tree_view_column_add_attribute (tree_column, cell_renderer,
1345 attribute = va_arg (args, gchar *);
1350 gtk_tree_view_append_column (current_column->priv->tree_view, tree_column);
1354 * hildon_touch_selector_get_num_columns:
1355 * @selector: a #HildonTouchSelector
1357 * Gets the number of columns in the #HildonTouchSelector.
1359 * Returns: the number of columns in @selector.
1364 hildon_touch_selector_get_num_columns (HildonTouchSelector * selector)
1366 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), -1);
1368 return g_slist_length (selector->priv->columns);
1372 * hildon_touch_selector_get_column_selection_mode:
1373 * @selector: a #HildonTouchSelector
1375 * Gets the selection mode of @selector.
1377 * Returns: one of #HildonTouchSelectorSelectionMode
1381 HildonTouchSelectorSelectionMode
1382 hildon_touch_selector_get_column_selection_mode (HildonTouchSelector * selector)
1384 HildonTouchSelectorSelectionMode result =
1385 HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
1386 GtkSelectionMode treeview_mode = GTK_SELECTION_BROWSE;
1387 HildonTouchSelectorColumn *column = NULL;
1388 GtkTreeSelection *selection = NULL;
1390 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), result);
1391 g_return_val_if_fail (hildon_touch_selector_get_num_columns (selector) > 0,
1394 column = HILDON_TOUCH_SELECTOR_COLUMN (selector->priv->columns->data);
1396 selection = gtk_tree_view_get_selection (column->priv->tree_view);
1397 treeview_mode = gtk_tree_selection_get_mode (selection);
1400 if (treeview_mode == GTK_SELECTION_MULTIPLE) {
1401 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE;
1403 result = HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE;
1410 * hildon_touch_selector_set_column_selection_mode:
1411 * @selector: a #HildonTouchSelector
1412 * @mode: the #HildonTouchSelectorMode for @selector
1414 * Sets the selection mode for @selector. See #HildonTouchSelectorSelectionMode.
1419 hildon_touch_selector_set_column_selection_mode (HildonTouchSelector * selector,
1420 HildonTouchSelectorSelectionMode mode)
1422 GtkTreeView *tv = NULL;
1423 HildonTouchSelectorColumn *column = NULL;
1424 GtkTreeSelection *selection = NULL;
1425 GtkSelectionMode treeview_mode = GTK_SELECTION_MULTIPLE;
1427 HildonTouchSelectorSelectionMode current_mode;
1429 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1430 g_return_if_fail (hildon_touch_selector_get_num_columns (selector) > 0);
1432 current_mode = hildon_touch_selector_get_column_selection_mode (selector);
1434 if (current_mode == mode) {
1438 column = HILDON_TOUCH_SELECTOR_COLUMN ((g_slist_nth (selector->priv->columns, 0))->data);
1439 tv = column->priv->tree_view;
1443 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE:
1444 treeview_mode = GTK_SELECTION_BROWSE;
1446 case HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE:
1447 treeview_mode = GTK_SELECTION_MULTIPLE;
1451 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
1452 gtk_tree_selection_set_mode (selection, treeview_mode);
1454 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tv));
1455 gtk_tree_model_get_iter_first (column->priv->model, &iter);
1456 gtk_tree_selection_unselect_all (selection);
1457 gtk_tree_selection_select_iter (selection, &iter);
1459 /* the column changed was the first one */
1460 hildon_touch_selector_emit_value_changed (selector, 0);
1466 * hildon_touch_selector_set_print_func:
1467 * @selector: a #HildonTouchSelector
1468 * @func: a #HildonTouchSelectorPrintFunc function
1470 * Sets the function to be used by hildon_touch_selector_get_current_text()
1471 * to produce a text representation of the currently selected items in @selector.
1472 * The default function will return a concatenation of comma separated items
1473 * selected in each column in @selector. Use this to override this method if you
1474 * need a particular representation for your application.
1479 hildon_touch_selector_set_print_func (HildonTouchSelector * selector,
1480 HildonTouchSelectorPrintFunc func)
1482 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1484 hildon_touch_selector_set_print_func_full (selector, func, NULL, NULL);
1488 * hildon_touch_selector_set_print_func_full:
1489 * @selector: a #HildonTouchSelector
1490 * @func: a #HildonTouchSelectorPrintFunc function
1491 * @user_data: a pointer to user data or %NULL
1492 * @destroy_func: a callback for freeing the user data or %NULL
1494 * Sets the function to be used by hildon_touch_selector_get_current_text()
1495 * to produce a text representation of the currently selected items in @selector.
1496 * The default function will return a concatenation of comma separated items
1497 * selected in each column in @selector. Use this to override this method if you
1498 * need a particular representation for your application.
1503 hildon_touch_selector_set_print_func_full (HildonTouchSelector *selector,
1504 HildonTouchSelectorPrintFunc func,
1506 GDestroyNotify destroy_func)
1508 gpointer old_user_data;
1509 GDestroyNotify old_destroy_func;
1511 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1513 old_user_data = selector->priv->print_user_data;
1514 old_destroy_func = selector->priv->print_destroy_func;
1516 selector->priv->print_func = func;
1517 selector->priv->print_user_data = user_data;
1518 selector->priv->print_destroy_func = destroy_func;
1520 if (old_destroy_func && old_user_data != user_data)
1521 (*old_destroy_func) (old_user_data);
1525 * hildon_touch_selector_get_print_func:
1526 * @selector: a #HildonTouchSelector
1528 * Gets the #HildonTouchSelectorPrintFunc currently used. See
1529 * hildon_touch_selector_set_print_func().
1531 * Returns: a #HildonTouchSelectorPrintFunc or %NULL if the default
1532 * one is currently used.
1534 HildonTouchSelectorPrintFunc
1535 hildon_touch_selector_get_print_func (HildonTouchSelector * selector)
1537 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1539 return selector->priv->print_func;
1543 * hildon_touch_selector_set_active:
1544 * @selector: a #HildonTouchSelector
1545 * @column: column number
1546 * @index: the index of the item to select, or -1 to have no active item
1548 * Sets the active item of the #HildonTouchSelector to @index. The
1549 * column number is taken from @column.
1551 * @selector must be in %HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE
1556 hildon_touch_selector_set_active (HildonTouchSelector *selector,
1560 GtkTreeSelection *selection = NULL;
1561 HildonTouchSelectorColumn *current_column = NULL;
1562 HildonTouchSelectorSelectionMode mode;
1565 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1566 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1567 mode = hildon_touch_selector_get_column_selection_mode (selector);
1568 g_return_if_fail (mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE);
1570 current_column = g_slist_nth_data (selector->priv->columns, column);
1572 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (current_column->priv->tree_view));
1573 path = gtk_tree_path_new_from_indices (index, -1);
1574 gtk_tree_selection_unselect_all (selection);
1576 gtk_tree_selection_select_path (selection, path);
1578 hildon_touch_selector_emit_value_changed (selector, column);
1580 gtk_tree_path_free (path);
1584 * hildon_touch_selector_get_active:
1585 * @selector: a #HildonTouchSelector
1586 * @column: column number
1588 * Returns the index of the currently active item in column number
1589 * @column, or -1 if there's no active item.
1591 * @selector must be in %HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE
1593 * Returns: an integer which is the index of the currently active
1594 * item, or -1 if there's no active item.
1599 hildon_touch_selector_get_active (HildonTouchSelector *selector,
1602 GtkTreeSelection *selection = NULL;
1603 HildonTouchSelectorColumn *current_column = NULL;
1604 HildonTouchSelectorSelectionMode mode;
1605 GtkTreeModel *model;
1610 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), -1);
1611 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector), -1);
1612 mode = hildon_touch_selector_get_column_selection_mode (selector);
1613 g_return_val_if_fail (mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE, -1);
1615 current_column = g_slist_nth_data (selector->priv->columns, column);
1617 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (current_column->priv->tree_view));
1618 g_return_val_if_fail (gtk_tree_selection_get_selected (selection, NULL, &iter), -1);
1620 model = gtk_tree_view_get_model (GTK_TREE_VIEW (current_column->priv->tree_view));
1621 path = gtk_tree_model_get_path (model, &iter);
1622 index = (gtk_tree_path_get_indices (path))[0];
1624 gtk_tree_path_free (path);
1630 * hildon_touch_selector_get_selected:
1631 * @selector: a #HildonTouchSelector
1632 * @column: the column number we want to get the element
1633 * @iter: #GtkTreeIter currently selected
1635 * Sets @iter to the currently selected node on the nth-column, if selection is
1636 * set to %HILDON_TOUCH_SELECTOR_SINGLE or %HILDON_TOUCH_SELECTOR_MULTIPLE with
1637 * a column different that the first one. @iter may be %NULL if you just want to
1638 * test if selection has any selected items.
1640 * This function will not work if selection is in
1641 * %HILDON_TOUCH_SELECTOR_MULTIPLE mode and the column is the first one.
1643 * See gtk_tree_selection_get_selected() for more information.
1645 * Returns: %TRUE if @iter was correctly set, %FALSE otherwise
1650 hildon_touch_selector_get_selected (HildonTouchSelector * selector,
1651 gint column, GtkTreeIter * iter)
1653 GtkTreeSelection *selection = NULL;
1654 HildonTouchSelectorColumn *current_column = NULL;
1655 HildonTouchSelectorSelectionMode mode;
1657 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
1658 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1660 mode = hildon_touch_selector_get_column_selection_mode (selector);
1661 g_return_val_if_fail
1662 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE) ||
1663 ((mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE)&&(column>0)),
1666 current_column = g_slist_nth_data (selector->priv->columns, column);
1669 gtk_tree_view_get_selection (GTK_TREE_VIEW (current_column->priv->tree_view));
1671 return gtk_tree_selection_get_selected (selection, NULL, iter);
1675 * hildon_touch_selector_select_iter
1676 * @selector: a #HildonTouchSelector
1677 * @column: the column to selects
1678 * @iter: the #GtkTreeIter to be selected
1679 * @scroll_to: whether to smoothly scroll to the item
1681 * Sets the currently selected item in the column @column to the one pointed by @iter,
1682 * optionally smoothly scrolling to it.
1687 hildon_touch_selector_select_iter (HildonTouchSelector * selector,
1688 gint column, GtkTreeIter * iter,
1692 GtkTreeModel *model;
1693 HildonTouchSelectorColumn *current_column = NULL;
1694 GtkTreeView *tv = NULL;
1695 GtkTreeSelection *selection = NULL;
1697 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1698 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1700 current_column = g_slist_nth_data (selector->priv->columns, column);
1702 tv = current_column->priv->tree_view;
1703 selection = gtk_tree_view_get_selection (tv);
1704 model = gtk_tree_view_get_model (tv);
1705 path = gtk_tree_model_get_path (model, iter);
1707 gtk_tree_selection_select_iter (selection, iter);
1710 hildon_touch_selector_scroll_to (current_column, tv, path);
1713 hildon_touch_selector_emit_value_changed (selector, column);
1715 gtk_tree_path_free (path);
1719 * hildon_touch_selector_unselect_iter
1720 * @selector: a #HildonTouchSelector
1721 * @column: the column to unselects from
1722 * @iter: the #GtkTreeIter to be unselected
1724 * Unselect the item pointed by @iter in the column @column
1729 void hildon_touch_selector_unselect_iter (HildonTouchSelector * selector,
1733 HildonTouchSelectorColumn *current_column = NULL;
1734 GtkTreeSelection *selection = NULL;
1736 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1737 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1739 current_column = g_slist_nth_data (selector->priv->columns, column);
1740 selection = gtk_tree_view_get_selection (current_column->priv->tree_view);
1741 gtk_tree_selection_unselect_iter (selection, iter);
1743 hildon_touch_selector_emit_value_changed (selector, column);
1747 * hildon_touch_selector_unselect_all:
1748 * @selector: a #HildonTouchSelector
1749 * @column: the position of the column to get the selected rows from
1751 * Unselects all the selected items in the column @column.
1756 hildon_touch_selector_unselect_all (HildonTouchSelector * selector,
1759 HildonTouchSelectorColumn *current_column = NULL;
1760 GtkTreeSelection *selection = NULL;
1762 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
1763 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1765 current_column = g_slist_nth_data (selector->priv->columns, column);
1766 selection = gtk_tree_view_get_selection (current_column->priv->tree_view);
1767 gtk_tree_selection_unselect_all (selection);
1769 hildon_touch_selector_emit_value_changed (selector, column);
1773 * hildon_touch_selector_get_selected_rows:
1774 * @selector: a #HildonTouchSelector
1775 * @column: the position of the column to get the selected rows from
1777 * Creates a list of #GtkTreePath<!-- -->s of all selected rows in a column. Additionally,
1778 * if you to plan to modify the model after calling this function, you may
1779 * want to convert the returned list into a list of GtkTreeRowReferences. To do this,
1780 * you can use gtk_tree_row_reference_new().
1782 * See gtk_tree_selection_get_selected_rows() for more information.
1784 * Returns: A new #GList containing a #GtkTreePath for each selected row in the column @column.
1789 hildon_touch_selector_get_selected_rows (HildonTouchSelector * selector,
1792 GList *result = NULL;
1793 HildonTouchSelectorColumn *current_column = NULL;
1794 GtkTreeSelection *selection = NULL;
1796 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1797 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1800 current_column = g_slist_nth_data (selector->priv->columns, column);
1801 selection = gtk_tree_view_get_selection (current_column->priv->tree_view);
1803 result = gtk_tree_selection_get_selected_rows (selection, NULL);
1809 * hildon_touch_selector_get_model:
1810 * @selector: a #HildonTouchSelector
1811 * @column: the position of the column in @selector
1813 * Gets the model of a column of @selector.
1815 * Returns: the #GtkTreeModel for the column @column of @selector.
1820 hildon_touch_selector_get_model (HildonTouchSelector * selector, gint column)
1822 HildonTouchSelectorColumn *current_column = NULL;
1824 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1825 g_return_val_if_fail (column < hildon_touch_selector_get_num_columns (selector),
1828 current_column = g_slist_nth_data (selector->priv->columns, column);
1830 return current_column->priv->model;
1834 on_row_changed (GtkTreeModel *model,
1839 HildonTouchSelector *selector;
1840 HildonTouchSelectorColumn *current_column;
1845 selector = HILDON_TOUCH_SELECTOR (userdata);
1847 for (col = selector->priv->columns; col != NULL; col = col->next) {
1848 current_column = HILDON_TOUCH_SELECTOR_COLUMN (col->data);
1849 if (current_column->priv->model == model &&
1850 gtk_tree_selection_path_is_selected (gtk_tree_view_get_selection (current_column->priv->tree_view),
1852 hildon_touch_selector_emit_value_changed (selector, column);
1859 _hildon_touch_selector_set_model (HildonTouchSelector * selector,
1860 gint column, GtkTreeModel * model)
1862 HildonTouchSelectorColumn *current_column = NULL;
1865 HILDON_TOUCH_SELECTOR_COLUMN (g_slist_nth_data (selector->priv->columns, column));
1867 if (current_column->priv->model) {
1868 g_signal_handlers_disconnect_by_func (current_column->priv->model,
1869 on_row_changed, selector);
1871 current_column->priv->model = model;
1872 gtk_tree_view_set_model (current_column->priv->tree_view,
1873 current_column->priv->model);
1874 g_signal_connect (model, "row-changed",
1875 G_CALLBACK (on_row_changed), selector);
1879 * hildon_touch_selector_set_model:
1880 * @selector: a #HildonTouchSelector
1881 * @column: the position of the column to set the model to
1882 * @model: a #GtkTreeModel
1884 * Sets the #GtkTreeModel for a particular column in @model.
1889 hildon_touch_selector_set_model (HildonTouchSelector * selector,
1890 gint column, GtkTreeModel * model)
1892 g_return_if_fail (HILDON_TOUCH_SELECTOR (selector));
1893 g_return_if_fail (column < hildon_touch_selector_get_num_columns (selector));
1895 HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->set_model (selector, column, model);
1899 * hildon_touch_selector_get_current_text:
1900 * @selector: a #HildonTouchSelector
1902 * Returns a string representing the currently selected items for
1903 * each column of @selector. See hildon_touch_selector_set_print_func().
1905 * Returns: a newly allocated string.
1910 hildon_touch_selector_get_current_text (HildonTouchSelector * selector)
1912 gchar *result = NULL;
1913 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
1915 if (selector->priv->print_func) {
1916 result = (*selector->priv->print_func) (selector, selector->priv->print_user_data);
1918 result = _default_print_func (selector, NULL);
1925 search_nearest_element (HildonPannableArea *panarea,
1927 GList *selected_rows,
1928 GtkTreePath **nearest_path)
1930 GtkAdjustment *adj = NULL;
1931 gdouble target_value = 0;
1934 GtkTreePath *path = NULL;
1936 gdouble nearest_distance = -1;
1937 gdouble current_distance = -1;
1938 GtkTreePath *result_path = NULL;
1940 g_assert (nearest_path != NULL);
1942 if (selected_rows == NULL) {
1943 *nearest_path = NULL;
1947 adj = hildon_pannable_area_get_vadjustment (panarea);
1948 g_return_if_fail (adj != NULL);
1950 /* we add this in order to check the nearest to the center of
1952 target_value = gtk_adjustment_get_value (adj) + adj->page_size/2;
1954 path = result_path = selected_rows->data;
1955 gtk_tree_view_get_background_area (tv, path, NULL, &rect);
1956 gtk_tree_view_convert_bin_window_to_tree_coords (tv, 0, rect.y, NULL, &y);
1957 nearest_distance = abs (target_value - y);
1959 for (iter = selected_rows->next; iter; iter = g_list_next (iter)) {
1960 gtk_tree_view_get_background_area (tv, path, NULL, &rect);
1961 gtk_tree_view_convert_bin_window_to_tree_coords (tv, 0, rect.y, NULL, &y);
1962 current_distance = abs (target_value - y);
1963 if (current_distance < nearest_distance) {
1964 nearest_distance = current_distance;
1969 *nearest_path = result_path;
1973 on_realize_cb (GtkWidget *widget,
1976 HildonTouchSelectorColumn *column = NULL;
1980 column = HILDON_TOUCH_SELECTOR_COLUMN (data);
1982 if (column->priv->initial_path) {
1983 gtk_tree_view_get_background_area (GTK_TREE_VIEW (column->priv->tree_view),
1984 column->priv->initial_path, NULL, &rect);
1985 gtk_tree_view_convert_bin_window_to_tree_coords
1986 (GTK_TREE_VIEW (column->priv->tree_view),
1987 0, rect.y, NULL, &y);
1989 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (column->priv->panarea),
1992 gtk_tree_path_free (column->priv->initial_path);
1994 column->priv->initial_path = NULL;
1998 g_signal_handler_disconnect (column->priv->panarea,
1999 column->priv->realize_handler);
2005 hildon_touch_selector_scroll_to (HildonTouchSelectorColumn *column,
2009 if (GTK_WIDGET_REALIZED (column->priv->panarea)) {
2013 gtk_tree_view_get_background_area (tv,
2015 gtk_tree_view_convert_bin_window_to_tree_coords (tv,
2016 0, rect.y, NULL, &y);
2018 hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA
2019 (column->priv->panarea), -1, y);
2021 if (column->priv->realize_handler != 0) {
2023 if (column->priv->initial_path) {
2024 gtk_tree_path_free (column->priv->initial_path);
2025 column->priv->initial_path = NULL;
2028 g_signal_handler_disconnect (column->priv->panarea,
2029 column->priv->realize_handler);
2030 column->priv->realize_handler = 0;
2033 column->priv->initial_path = gtk_tree_path_copy (path);
2034 column->priv->realize_handler =
2035 g_signal_connect_after (G_OBJECT (column->priv->panarea), "realize",
2036 G_CALLBACK (on_realize_cb),
2043 * Center on the selected item of a concrete column
2045 * Returns: TRUE if was able to do that
2049 _hildon_touch_selector_center_on_selected_items (HildonTouchSelector *selector,
2050 HildonTouchSelectorColumn *column)
2052 GtkTreePath *path = NULL;
2053 GList *selected_rows = NULL;
2054 gint num_column = -1;
2056 num_column = g_slist_index (selector->priv->columns, column);
2058 selected_rows = hildon_touch_selector_get_selected_rows (selector, num_column);
2059 if (selected_rows) {
2060 search_nearest_element (HILDON_PANNABLE_AREA (column->priv->panarea),
2061 GTK_TREE_VIEW (column->priv->tree_view),
2066 hildon_touch_selector_scroll_to (column,
2067 GTK_TREE_VIEW (column->priv->tree_view),
2073 g_list_foreach (selected_rows, (GFunc) (gtk_tree_path_free), NULL);
2074 g_list_free (selected_rows);
2081 _hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
2083 HildonTouchSelectorSelectionMode mode;
2086 n_columns = hildon_touch_selector_get_num_columns (selector);
2087 mode = hildon_touch_selector_get_column_selection_mode (selector);
2089 return ((n_columns > 1) || (mode == HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE));
2093 * hildon_touch_selector_has_multiple_selection:
2094 * @selector: A #HildonTouchSelector
2096 * Determines whether @selector is complex enough to actually require an
2097 * extra selection step than only picking an item. This is normally %TRUE
2098 * if @selector has multiple columns, multiple selection, or when it is a
2099 * more complex widget, like #HildonTouchSelectorEntry.
2101 * This information is useful for widgets containing a #HildonTouchSelector,
2102 * like #HildonPickerDialog, that could need a "Done" button, in case that
2103 * its internal #HildonTouchSelector has multiple columns, for instance.
2105 * Returns: %TRUE if @selector requires multiple selection steps.
2110 hildon_touch_selector_has_multiple_selection (HildonTouchSelector * selector)
2112 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), FALSE);
2114 return HILDON_TOUCH_SELECTOR_GET_CLASS (selector)->has_multiple_selection (selector);
2119 * hildon_touch_selector_get_column:
2120 * @selector: A #HildonTouchSelector
2121 * @column: a column number
2123 * Use this method to retrieve a #HildonTouchSelectorColumn. Then, you can use
2124 * the #GtkCellLayout interface to set up the layout of the column.
2126 * Returns: the @column<!-- -->-th #HildonTouchSelectorColumn in @selector
2130 HildonTouchSelectorColumn *
2131 hildon_touch_selector_get_column (HildonTouchSelector * selector,
2134 gint num_columns = -1;
2136 g_return_val_if_fail (HILDON_IS_TOUCH_SELECTOR (selector), NULL);
2137 num_columns = hildon_touch_selector_get_num_columns (selector);
2138 g_return_val_if_fail (column < num_columns && column >= 0, NULL);
2140 return g_slist_nth_data (selector->priv->columns, column);
2145 * hildon_touch_selector_center_on_selected:
2146 * @selector: a #HildonTouchSelector
2148 * Ensures all the columns in a #HildonTouchSelector show a selected
2149 * item. If one of the columns is in
2150 * %HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE mode, that column
2151 * will be scrolled to ensure the selected item that is closest to the
2152 * currently visible area is shown.
2157 hildon_touch_selector_center_on_selected (HildonTouchSelector *selector)
2159 GSList *iter = NULL;
2161 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
2163 for (iter = selector->priv->columns; iter; iter = g_slist_next (iter)) {
2164 _hildon_touch_selector_center_on_selected_items (selector,
2165 HILDON_TOUCH_SELECTOR_COLUMN (iter->data));
2170 * hildon_touch_selector_optimal_size_request
2171 * @selector: a #HildonTouchSelector
2172 * @requisition: a #GtkRequisition
2174 * Gets the optimal size request of the touch selector. This function is mostly
2175 * intended for dialog implementations that include a #HildonTouchSelector and
2176 * want to optimize the screen real state, for example, when you want a dialog
2177 * to show as much of the selector, avoiding any extra empty space below the
2180 * See #HildonPickerDialog implementation for an example.
2182 * This function is oriented to be used in the size_request of a dialog or window,
2183 * if you are not sure do not use it.
2185 * There is a precondition to this function: Since this function does not
2186 * call the "size_request" method, it can only be used when you know that
2187 * gtk_widget_size_request() has been called since the last time a resize was
2193 hildon_touch_selector_optimal_size_request (HildonTouchSelector *selector,
2194 GtkRequisition *requisition)
2196 GSList *iter = NULL;
2198 gint base_height = 0;
2200 g_return_if_fail (HILDON_IS_TOUCH_SELECTOR (selector));
2202 iter = selector->priv->columns;
2204 /* Default optimal values are the current ones */
2205 gtk_widget_get_child_requisition (GTK_WIDGET (selector),
2209 height = requisition->height;
2211 /* we use the normal requisition as base, as the touch selector can has
2212 extra widgets, not only the columns (ie: HildonTouchSelectorEntry) */
2213 base_height = requisition->height;
2216 /* Compute optimal height for the columns */
2218 HildonTouchSelectorColumn *column;
2220 GtkRequisition child_requisition = {0};
2222 column = HILDON_TOUCH_SELECTOR_COLUMN (iter->data);
2223 child = GTK_WIDGET (column->priv->tree_view);
2225 gtk_widget_get_child_requisition (child, &child_requisition);
2227 height = MAX (height, child_requisition.height);
2229 iter = g_slist_next (iter);
2232 requisition->height = base_height + height;