2008-10-01 Alejandro Pinheiro <apinheiro@igalia.com>
[hildon] / examples / hildon-touch-selector-multi-cells-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
27 #include "hildon-program.h"
28 #include "hildon-stackable-window.h"
29
30 #include "hildon-touch-selector.h"
31 #include "hildon-picker-dialog.h"
32 #include "hildon-picker-button.h"
33
34 static GtkWidget *create_selector ();
35 static GtkWidget *get_visible_content (GtkWidget * window);
36
37 static GtkWindow *parent_window = NULL;
38
39 static GtkWidget *label = NULL;
40
41 static void
42 value_changed (HildonPickerButton * button,
43                gpointer user_data)
44 {
45   HildonTouchSelector *selector;
46   gchar *aux_string = NULL;
47
48   selector = hildon_picker_button_get_selector (button);
49   aux_string = hildon_touch_selector_get_current_text (selector);
50   gtk_label_set_text (GTK_LABEL (label), aux_string);
51   g_free (aux_string);
52 }
53
54 static GtkWidget *
55 create_selector ()
56 {
57   GtkWidget *selector = NULL;
58   GSList *icon_list = NULL;
59   GtkListStore *store_icons = NULL;
60   GSList *item = NULL;
61   GtkCellRenderer *renderer = NULL;
62   HildonTouchSelectorColumn *column = NULL;
63
64   selector = hildon_touch_selector_new ();
65
66   icon_list = gtk_stock_list_ids ();
67
68   store_icons = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
69   for (item = icon_list; item; item = g_slist_next (item)) {
70     GtkTreeIter iter;
71     GtkStockItem stock_item;
72     gchar *stock_id;
73
74     stock_id = (gchar *)item->data;
75     gtk_stock_lookup (stock_id, &stock_item);
76     gtk_list_store_append (store_icons, &iter);
77     gtk_list_store_set (store_icons, &iter, 0, stock_id, 1, stock_item.label, -1);
78
79     g_free (stock_id);
80   }
81   g_slist_free (icon_list);
82
83   column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector),
84                                                 GTK_TREE_MODEL (store_icons),
85                                                 NULL, NULL);
86   g_object_set (G_OBJECT(column), "text-column", 1, NULL);
87
88   renderer = gtk_cell_renderer_pixbuf_new ();
89   gtk_cell_renderer_set_fixed_size (renderer, 75, 75);
90   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column), renderer, FALSE);
91   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (column), renderer,
92                                   "stock-id", 0, NULL);
93
94   renderer = gtk_cell_renderer_text_new ();
95   g_object_set (renderer, "xalign", 0.5, NULL);
96   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column), renderer, TRUE);
97   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (column), renderer,
98                                   "text", 1, NULL);
99
100   hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
101                                                    HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
102
103   return selector;
104 }
105
106 static GtkWidget *
107 get_visible_content (GtkWidget * window)
108 {
109   GtkWidget *result = NULL;
110   GtkWidget *button = NULL;
111   GtkWidget *selector;
112
113   label = gtk_label_new ("Here we are going to put the selection");
114
115   button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
116   hildon_button_set_title (HILDON_BUTTON (button), "Click me..");
117   selector = create_selector ();
118   hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
119                                      HILDON_TOUCH_SELECTOR (selector));
120
121   g_signal_connect (G_OBJECT (button), "value-changed",
122                     G_CALLBACK (value_changed), window);
123
124   result = gtk_vbox_new (FALSE, 6);
125
126   gtk_container_add (GTK_CONTAINER (result), button);
127   gtk_container_add (GTK_CONTAINER (result), label);
128
129   return result;
130 }
131
132 int
133 main (int argc, char **args)
134 {
135   HildonProgram *program = NULL;
136   GtkWidget *window = NULL;
137
138   gtk_init (&argc, &args);
139
140   program = hildon_program_get_instance ();
141   g_set_application_name
142     ("hildon-touch-selector cell renderer example program");
143
144   window = hildon_stackable_window_new ();
145   parent_window = GTK_WINDOW (window);
146   hildon_program_add_window (program, HILDON_WINDOW (window));
147
148   gtk_container_set_border_width (GTK_CONTAINER (window), 6);
149
150   gtk_rc_parse_string ("style \"fremantle-widget\" {\n"
151                        " GtkWidget::hildon-mode = 1 \n"
152                        "} widget \"*.fremantle-widget\" style \"fremantle-widget\"\n");
153
154
155   GtkWidget *vbox = get_visible_content (window);
156
157   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (vbox));
158
159   g_signal_connect (G_OBJECT (window), "destroy",
160                     G_CALLBACK (gtk_main_quit), NULL);
161   gtk_widget_show_all (GTK_WIDGET (window));
162
163   gtk_main ();
164
165   return 0;
166 }