Release 2.1.96-6
[hildon] / examples / hildon-touch-selector-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include                                        <glib.h>
25 #include                                        <gtk/gtk.h>
26 #include                                        <hildon/hildon.h>
27
28 static GtkWidget *create_selector ();
29 static GtkWidget *get_visible_content (GtkWidget * window);
30
31 static GtkWindow *parent_window = NULL;
32
33 static GtkWidget *label = NULL;
34
35 static void
36 value_changed (HildonPickerButton * button,
37                gpointer user_data)
38 {
39   HildonTouchSelector *selector;
40   gchar *aux_string = NULL;
41
42   selector = hildon_picker_button_get_selector (button);
43   aux_string = hildon_touch_selector_get_current_text (selector);
44   gtk_label_set_text (GTK_LABEL (label), aux_string);
45   g_free (aux_string);
46 }
47
48 static GtkWidget *
49 create_selector ()
50 {
51   GtkWidget *selector = NULL;
52   GSList *icon_list = NULL;
53   GtkListStore *store_icons = NULL;
54   GSList *item = NULL;
55   GtkCellRenderer *renderer = NULL;
56   HildonTouchSelectorColumn *column = NULL;
57
58   selector = hildon_touch_selector_new ();
59
60   icon_list = gtk_stock_list_ids ();
61
62   store_icons = gtk_list_store_new (1, G_TYPE_STRING);
63   for (item = icon_list; item; item = g_slist_next (item)) {
64     GtkTreeIter iter;
65     gchar *label = item->data;
66
67     gtk_list_store_append (store_icons, &iter);
68     gtk_list_store_set (store_icons, &iter, 0, label, -1);
69     g_free (label);
70   }
71   g_slist_free (icon_list);
72
73   renderer = gtk_cell_renderer_pixbuf_new ();
74   gtk_cell_renderer_set_fixed_size (renderer, -1, 100);
75
76   column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector),
77                                                 GTK_TREE_MODEL (store_icons),
78                                                 renderer, "stock-id", 0, NULL);
79   hildon_touch_selector_column_set_text_column (column, 0);
80
81   hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
82                                                    HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
83
84   return selector;
85 }
86
87 static GtkWidget *
88 get_visible_content (GtkWidget * window)
89 {
90   GtkWidget *result = NULL;
91   GtkWidget *button = NULL;
92   GtkWidget *selector;
93
94   label = gtk_label_new ("Here we are going to put the selection");
95
96   button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
97   hildon_button_set_title (HILDON_BUTTON (button), "Click me..");
98   selector = create_selector ();
99   hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
100                                      HILDON_TOUCH_SELECTOR (selector));
101
102   g_signal_connect (G_OBJECT (button), "value-changed",
103                     G_CALLBACK (value_changed), window);
104
105   result = gtk_vbox_new (FALSE, 6);
106
107   gtk_container_add (GTK_CONTAINER (result), button);
108   gtk_container_add (GTK_CONTAINER (result), label);
109
110   return result;
111 }
112
113 int
114 main (int argc, char **argv)
115 {
116   HildonProgram *program = NULL;
117   GtkWidget *window = NULL;
118
119   hildon_gtk_init (&argc, &argv);
120
121   program = hildon_program_get_instance ();
122   g_set_application_name
123     ("hildon-touch-selector cell renderer example program");
124
125   window = hildon_stackable_window_new ();
126   parent_window = GTK_WINDOW (window);
127   hildon_program_add_window (program, HILDON_WINDOW (window));
128
129   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
130
131   GtkWidget *vbox = get_visible_content (window);
132
133   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (vbox));
134
135   g_signal_connect (G_OBJECT (window), "destroy",
136                     G_CALLBACK (gtk_main_quit), NULL);
137   gtk_widget_show_all (GTK_WIDGET (window));
138
139   gtk_main ();
140
141   return 0;
142 }