2008-08-13 Alberto Garcia <agarcia@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
28 static GtkWidget *horizontal_layout;
29 static GtkWidget *images;
30
31 static void
32 button_clicked_cb                               (HildonButton *button,
33                                                  gpointer      data)
34 {
35     g_debug ("Pressed button: %s", hildon_button_get_title (button));
36 }
37
38
39 static GtkWidget *
40 create_image                                    (void)
41 {
42     return gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
43 }
44
45 static void
46 vertical_buttons_window                         (GtkButton *b,
47                                                  gpointer   data)
48 {
49     GtkWidget *win;
50     GtkWidget *button;
51     GtkBox *hbox;
52     GtkBox *vbox1;
53     GtkBox *vbox2;
54     GtkBox *vbox3;
55     int i;
56     HildonButtonArrangement arrangement;
57     gboolean use_images;
58
59     /* Create window */
60     win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
61     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
62
63     arrangement = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (horizontal_layout)) ?
64             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL :
65             HILDON_BUTTON_ARRANGEMENT_VERTICAL;
66
67     use_images = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (images));
68
69     /* Create and pack boxes */
70     hbox = GTK_BOX (gtk_hbox_new (FALSE, 10));
71     vbox1 = GTK_BOX (gtk_vbox_new (FALSE, 10));
72     vbox2 = GTK_BOX (gtk_vbox_new (FALSE, 10));
73     vbox3 = GTK_BOX (gtk_vbox_new (FALSE, 10));
74
75     gtk_box_pack_start (hbox, GTK_WIDGET (vbox1), TRUE, TRUE, 0);
76     gtk_box_pack_start (hbox, GTK_WIDGET (vbox2), TRUE, TRUE, 0);
77     gtk_box_pack_start (hbox, GTK_WIDGET (vbox3), TRUE, TRUE, 0);
78
79     /* Finger buttons */
80     gtk_box_pack_start (vbox1, gtk_label_new ("Finger height"), FALSE, FALSE, 0);
81     for (i = 0; i < 4; i++) {
82         gchar *title = g_strdup_printf ("Title %d", i);
83         button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT |
84                                               HILDON_SIZE_AUTO_WIDTH, arrangement, title,
85                                               i % 2 ? "Value" : NULL);
86         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
87         gtk_box_pack_start (vbox1, button, FALSE, FALSE, 0);
88         g_free (title);
89         if (use_images)
90             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
91         if (i % 2 == 0)
92             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
93     }
94
95     /* Thumb buttons */
96     gtk_box_pack_start (vbox2, gtk_label_new ("Thumb height"), FALSE, FALSE, 0);
97     for (i = 0; i < 3; i++) {
98         gchar *title = g_strdup_printf ("Title %d", i);
99         button = hildon_button_new_with_text (HILDON_SIZE_THUMB_HEIGHT |
100                                               HILDON_SIZE_AUTO_WIDTH, arrangement, title,
101                                               i % 2 ? "Value" : NULL);
102         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
103         gtk_box_pack_start (vbox2, button, FALSE, FALSE, 0);
104         g_free (title);
105         if (use_images)
106             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
107         if (i % 2 == 0)
108             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
109     }
110
111     /* Auto buttons */
112     gtk_box_pack_start (vbox3, gtk_label_new ("Auto height"), FALSE, FALSE, 0);
113     for (i = 0; i < 6; i++) {
114         gchar *title = g_strdup_printf ("Title %d", i);
115         button = hildon_button_new_with_text (HILDON_SIZE_AUTO, arrangement, title,
116                                               i % 2 ? "Value" : NULL);
117         g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
118         gtk_box_pack_start (vbox3, button, FALSE, FALSE, 0);
119         g_free (title);
120         if (use_images)
121             hildon_button_set_image (HILDON_BUTTON (button), create_image ());
122         if (i % 2 == 0)
123             hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
124     }
125
126     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (hbox));
127
128     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_widget_destroy), NULL);
129
130     gtk_widget_show_all (win);
131 }
132
133 static void
134 horizontal_buttons_window                       (GtkButton *b,
135                                                  gpointer   data)
136 {
137     GtkWidget *win;
138     GtkWidget *button;
139     GtkBox *vbox;
140     GtkBox *hbox1;
141     GtkBox *hbox2;
142     GtkBox *hbox3;
143     GtkBox *hbox4;
144     HildonButtonArrangement arrangement;
145     gboolean use_images;
146
147     /* Create window */
148     win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
149     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
150
151     arrangement = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (horizontal_layout)) ?
152             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL :
153             HILDON_BUTTON_ARRANGEMENT_VERTICAL;
154
155     use_images = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (images));
156
157     /* Create and pack boxes */
158     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
159     hbox1 = GTK_BOX (gtk_hbox_new (FALSE, 10));
160     hbox2 = GTK_BOX (gtk_hbox_new (FALSE, 10));
161     hbox3 = GTK_BOX (gtk_hbox_new (FALSE, 10));
162     hbox4 = GTK_BOX (gtk_hbox_new (FALSE, 10));
163
164     gtk_box_pack_start (vbox, gtk_label_new ("(all buttons have finger height)"), FALSE, FALSE, 0);
165     gtk_box_pack_start (vbox, GTK_WIDGET (hbox1), FALSE, FALSE, 0);
166     gtk_box_pack_start (vbox, GTK_WIDGET (hbox2), FALSE, FALSE, 0);
167     gtk_box_pack_start (vbox, GTK_WIDGET (hbox3), FALSE, FALSE, 0);
168     gtk_box_pack_start (vbox, GTK_WIDGET (hbox4), FALSE, FALSE, 0);
169
170     /* Full screen width button */
171     button = hildon_button_new_with_text (HILDON_SIZE_FULLSCREEN_WIDTH |
172                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Full width", "Value");
173     gtk_box_pack_start (hbox1, button, TRUE, TRUE, 0);
174     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
175     if (use_images)
176         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
177
178     /* Half screen width buttons */
179     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
180                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Half width 1", "Value");
181     gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0);
182     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
183     if (use_images)
184         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
185
186     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
187                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
188                                           "Half width 2 with long title",
189                                           "Value");
190     gtk_box_pack_start (hbox2, button, TRUE, TRUE, 0);
191     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
192     if (use_images)
193         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
194     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
195
196     /* Half screen width buttons */
197     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
198                                           HILDON_SIZE_FINGER_HEIGHT, arrangement, "Half width 3", NULL);
199     gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0);
200     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
201     if (use_images)
202         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
203     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
204
205     button = hildon_button_new_with_text (HILDON_SIZE_HALFSCREEN_WIDTH |
206                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
207                                           "Half width 4 with very long title (REALLY long)",
208                                           "Value (title is truncated)");
209     gtk_box_pack_start (hbox3, button, TRUE, TRUE, 0);
210     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
211     if (use_images)
212         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
213
214     /* Auto width button */
215     button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH |
216                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
217                                           "Auto width 1", "Value");
218     gtk_box_pack_start (hbox4, button, TRUE, TRUE, 0);
219     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
220     if (use_images)
221         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
222
223     button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH |
224                                           HILDON_SIZE_FINGER_HEIGHT, arrangement,
225                                           "Auto width 2 with longer text", NULL);
226     gtk_box_pack_start (hbox4, button, TRUE, TRUE, 0);
227     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked_cb), NULL);
228     if (use_images)
229         hildon_button_set_image (HILDON_BUTTON (button), create_image ());
230     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
231
232     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
233
234     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_widget_destroy), NULL);
235
236     gtk_widget_show_all (win);
237 }
238
239 int
240 main                                            (int    argc,
241                                                  char **argv)
242 {
243     GtkWidget *win;
244     GtkWidget *but1;
245     GtkWidget *but2;
246     GtkWidget *label;
247     GtkBox *vbox;
248     GtkBox *hbox;
249     GtkWidget *align;
250
251     gtk_init (&argc, &argv);
252
253     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
254     hbox = GTK_BOX (gtk_hbox_new (TRUE, 10));
255
256     win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
257
258     label = gtk_label_new ("HildonButton example");
259     but1 = gtk_button_new_with_label ("Buttons with different heights");
260     but2 = gtk_button_new_with_label ("Buttons with different widths");
261
262     horizontal_layout = gtk_check_button_new_with_label ("Use horizontal layout");
263     images = gtk_check_button_new_with_label ("Use images");
264     gtk_box_pack_start (hbox, horizontal_layout, TRUE, TRUE, 0);
265     gtk_box_pack_start (hbox, images, TRUE, TRUE, 0);
266     align = gtk_alignment_new (0.5, 0.5, 0, 0);
267     gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (hbox));
268
269     gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);
270     gtk_box_pack_start (vbox, but1, TRUE, TRUE, 0);
271     gtk_box_pack_start (vbox, but2, TRUE, TRUE, 0);
272     gtk_box_pack_start (vbox, align, FALSE, FALSE, 0);
273
274     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
275     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
276
277     g_signal_connect (but1, "clicked", G_CALLBACK (vertical_buttons_window), NULL);
278     g_signal_connect (but2, "clicked", G_CALLBACK (horizontal_buttons_window), NULL);
279     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
280
281     gtk_widget_show_all (win);
282
283     gtk_main ();
284
285     return 0;
286 }