2008-10-06 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / examples / hildon-button-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Author: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include                                        <gtk/gtk.h>
26 #include                                        <hildon-button.h>
27 #include                                        <hildon-stackable-window.h>
28
29 static GtkWidget *horizontal_layout;
30 static GtkWidget *images;
31
32 static void
33 button_clicked_cb                               (HildonButton *button,
34                                                  gpointer      data)
35 {
36     g_debug ("Pressed button: %s", hildon_button_get_title (button));
37 }
38
39
40 static GtkWidget *
41 create_image                                    (void)
42 {
43     return gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
44 }
45
46 static void
47 vertical_buttons_window                         (GtkButton *b,
48                                                  gpointer   data)
49 {
50     GtkWidget *win;
51     GtkWidget *button;
52     GtkBox *hbox;
53     GtkBox *vbox1;
54     GtkBox *vbox2;
55     GtkBox *vbox3;
56     int i;
57     HildonButtonArrangement arrangement;
58     gboolean use_images;
59
60     /* Create window */
61     win = hildon_stackable_window_new ();
62     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
63
64     arrangement = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (horizontal_layout)) ?
65             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL :
66             HILDON_BUTTON_ARRANGEMENT_VERTICAL;
67
68     use_images = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (images));
69
70     /* Create and pack boxes */
71     hbox = GTK_BOX (gtk_hbox_new (FALSE, 10));
72     vbox1 = GTK_BOX (gtk_vbox_new (FALSE, 10));
73     vbox2 = GTK_BOX (gtk_vbox_new (FALSE, 10));
74     vbox3 = GTK_BOX (gtk_vbox_new (FALSE, 10));
75
76     gtk_box_pack_start (hbox, GTK_WIDGET (vbox1), TRUE, TRUE, 0);
77     gtk_box_pack_start (hbox, GTK_WIDGET (vbox2), TRUE, TRUE, 0);
78     gtk_box_pack_start (hbox, GTK_WIDGET (vbox3), TRUE, TRUE, 0);
79
80     /* Finger buttons */
81     gtk_box_pack_start (vbox1, gtk_label_new ("Finger height"), FALSE, FALSE, 0);
82     for (i = 0; i < 4; i++) {
83         gchar *title = g_strdup_printf ("Title %d", i);
84         button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT |
85                                               HILDON_SIZE_AUTO_WIDTH, arrangement, title,
86                                               i % 2 ? "Value" : NULL);
87         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
88         gtk_box_pack_start (vbox1, button, FALSE, FALSE, 0);
89         g_free (title);
90         if (use_images)
91             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
92         if (i % 2 == 0)
93             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
94     }
95
96     /* Thumb buttons */
97     gtk_box_pack_start (vbox2, gtk_label_new ("Thumb height"), FALSE, FALSE, 0);
98     for (i = 0; i < 3; i++) {
99         gchar *title = g_strdup_printf ("Title %d", i);
100         button = hildon_button_new_with_text (HILDON_SIZE_THUMB_HEIGHT |
101                                               HILDON_SIZE_AUTO_WIDTH, arrangement, title,
102                                               i % 2 ? "Value" : NULL);
103         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
104         gtk_box_pack_start (vbox2, button, FALSE, FALSE, 0);
105         g_free (title);
106         if (use_images)
107             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
108         if (i % 2 == 0)
109             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
110     }
111
112     /* Auto buttons */
113     gtk_box_pack_start (vbox3, gtk_label_new ("Auto height"), FALSE, FALSE, 0);
114     for (i = 0; i < 6; i++) {
115         gchar *title = g_strdup_printf ("Title %d", i);
116         button = hildon_button_new_with_text (HILDON_SIZE_AUTO, arrangement, title,
117                                               i % 2 ? "Value" : NULL);
118         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
119         gtk_box_pack_start (vbox3, button, FALSE, FALSE, 0);
120         g_free (title);
121         if (use_images)
122             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
123         if (i % 2 == 0)
124             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
125     }
126
127     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (hbox));
128
129     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_widget_destroy), NULL);
130
131     gtk_widget_show_all (win);
132 }
133
134 static void
135 horizontal_buttons_window                       (GtkButton *b,
136                                                  gpointer   data)
137 {
138     GtkWidget *win;
139     GtkWidget *button;
140     GtkBox *vbox;
141     GtkBox *hbox1;
142     GtkBox *hbox2;
143     GtkBox *hbox3;
144     GtkBox *hbox4;
145     HildonButtonArrangement arrangement;
146     gboolean use_images;
147
148     /* Create window */
149     win = hildon_stackable_window_new ();
150     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
151
152     arrangement = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (horizontal_layout)) ?
153             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL :
154             HILDON_BUTTON_ARRANGEMENT_VERTICAL;
155
156     use_images = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (images));
157
158     /* Create and pack boxes */
159     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
160     hbox1 = GTK_BOX (gtk_hbox_new (FALSE, 10));
161     hbox2 = GTK_BOX (gtk_hbox_new (FALSE, 10));
162     hbox3 = GTK_BOX (gtk_hbox_new (FALSE, 10));
163     hbox4 = GTK_BOX (gtk_hbox_new (FALSE, 10));
164
165     gtk_box_pack_start (vbox, gtk_label_new ("(all buttons have finger height)"), FALSE, FALSE, 0);
166     gtk_box_pack_start (vbox, GTK_WIDGET (hbox1), FALSE, FALSE, 0);
167     gtk_box_pack_start (vbox, GTK_WIDGET (hbox2), FALSE, FALSE, 0);
168     gtk_box_pack_start (vbox, GTK_WIDGET (hbox3), FALSE, FALSE, 0);
169     gtk_box_pack_start (vbox, GTK_WIDGET (hbox4), FALSE, FALSE, 0);
170
171     /* Full screen width button */
172     button = hildon_button_new_with_text (HILDON_SIZE_FULLSCREEN_WIDTH |
173                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Full width", "Value");
174     gtk_box_pack_start (hbox1, button, TRUE, TRUE, 0);
175     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
176     if (use_images)
177         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
178
179     /* Half screen width buttons */
180     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
181                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Half width 1", "Value");
182     gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0);
183     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
184     if (use_images)
185         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
186
187     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
188                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
189                                           "Half width 2 with long title",
190                                           "Value");
191     gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0);
192     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
193     if (use_images)
194         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
195     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
196
197     /* Half screen width buttons */
198     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
199                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Half width 3", NULL);
200     gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0);
201     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
202     if (use_images)
203         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
204     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
205
206     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
207                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
208                                           "Half width 4 with very long title (REALLY long)",
209                                           "Value (title is truncated)");
210     gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0);
211     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
212     if (use_images)
213         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
214
215     /* Auto width button */
216     button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH |
217                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
218                                           "Auto width 1", "Value");
219     gtk_box_pack_start (hbox4, button, TRUE, TRUE, 0);
220     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
221     if (use_images)
222         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
223
224     button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH |
225                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
226                                           "Auto width 2 with longer text", NULL);
227     gtk_box_pack_start (hbox4, button, TRUE, TRUE, 0);
228     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
229     if (use_images)
230         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
231     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
232
233     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
234
235     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_widget_destroy), NULL);
236
237     gtk_widget_show_all (win);
238 }
239
240 int
241 main                                            (int    argc,
242                                                  char **argv)
243 {
244     GtkWidget *win;
245     GtkWidget *but1;
246     GtkWidget *but2;
247     GtkWidget *label;
248     GtkBox *vbox;
249     GtkBox *hbox;
250     GtkWidget *align;
251
252     gtk_init (&argc, &argv);
253
254     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
255     hbox = GTK_BOX (gtk_hbox_new (TRUE, 10));
256
257     win = hildon_stackable_window_new ();
258
259     label = gtk_label_new ("HildonButton example");
260     but1 = gtk_button_new_with_label ("Buttons with different heights");
261     but2 = gtk_button_new_with_label ("Buttons with different widths");
262
263     horizontal_layout = gtk_check_button_new_with_label ("Use horizontal layout");
264     images = gtk_check_button_new_with_label ("Use images");
265     gtk_box_pack_start (hbox, horizontal_layout, TRUE, TRUE, 0);
266     gtk_box_pack_start (hbox, images, TRUE, TRUE, 0);
267     align = gtk_alignment_new (0.5, 0.5, 0, 0);
268     gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (hbox));
269
270     gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);
271     gtk_box_pack_start (vbox, but1, TRUE, TRUE, 0);
272     gtk_box_pack_start (vbox, but2, TRUE, TRUE, 0);
273     gtk_box_pack_start (vbox, align, FALSE, FALSE, 0);
274
275     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
276     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
277
278     g_signal_connect (but1, "clicked", G_CALLBACK (vertical_buttons_window), NULL);
279     g_signal_connect (but2, "clicked", G_CALLBACK (horizontal_buttons_window), NULL);
280     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
281
282     gtk_widget_show_all (win);
283
284     gtk_main ();
285
286     return 0;
287 }