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