2008-07-29 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-button.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser Public License as published by
10  * the Free Software Foundation; version 2 of the license.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser Public License for more details.
16  *
17  */
18
19 /**
20  * SECTION:hildon-button
21  * @short_description: Widget representing a button in the Hildon framework.
22  *
23  * The #HildonButton is a GTK widget which represent a clickable
24  * button. It is derived from the GtkButton widget and provides
25  * additional commodities specific to the Hildon framework.
26  *
27  * The height of a #HildonButton can be set to either "finger" height
28  * or "thumb" height. It can also be configured to use halfscreen or
29  * fullscreen width. Alternatively, either dimension can be set to
30  * "auto" so it behaves like a standard GtkButton.
31  *
32  * The #HildonButton can hold any valid child widget, but it usually
33  * contains two labels: title and value (the latter being optional).
34  */
35
36 #include                                        "hildon-button.h"
37 #include                                        "hildon-enum-types.h"
38
39 #define FINGER_BUTTON_HEIGHT                    70
40 #define THUMB_BUTTON_HEIGHT                     105
41 #define HALFSCREEN_BUTTON_WIDTH                 400
42 #define FULLSCREEN_BUTTON_WIDTH                 800
43
44 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
45
46 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
47                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
48                                                 HILDON_TYPE_BUTTON, HildonButtonPrivate));
49
50 typedef struct                                  _HildonButtonPrivate HildonButtonPrivate;
51
52 struct                                          _HildonButtonPrivate
53 {
54     GtkLabel *title;
55     GtkLabel *value;
56     GtkWidget *alignment;
57 };
58
59 enum {
60   PROP_TITLE = 1,
61   PROP_VALUE,
62   PROP_ARRANGEMENT_FLAGS
63 };
64
65 static void
66 hildon_button_set_arrangement                   (HildonButton      *button,
67                                                  HildonButtonFlags  flags);
68
69 static void
70 hildon_button_construct_child                   (HildonButton *button);
71
72 static void
73 hildon_button_set_property                      (GObject      *object,
74                                                  guint         prop_id,
75                                                  const GValue *value,
76                                                  GParamSpec   *pspec)
77 {
78     HildonButton *button = HILDON_BUTTON (object);
79
80     switch (prop_id)
81     {
82     case PROP_TITLE:
83         hildon_button_set_title (button, g_value_get_string (value));
84         break;
85     case PROP_VALUE:
86         hildon_button_set_value (button, g_value_get_string (value));
87         break;
88     case PROP_ARRANGEMENT_FLAGS:
89         hildon_button_set_arrangement (button, g_value_get_enum (value));
90         break;
91     default:
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93         break;
94     }
95 }
96
97 static void
98 hildon_button_get_property                      (GObject    *object,
99                                                  guint       prop_id,
100                                                  GValue     *value,
101                                                  GParamSpec *pspec)
102 {
103     HildonButton *button = HILDON_BUTTON (object);
104     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
105
106     switch (prop_id)
107     {
108     case PROP_TITLE:
109         g_value_set_string (value, gtk_label_get_text (priv->title));
110         break;
111     case PROP_VALUE:
112         g_value_set_string (value, gtk_label_get_text (priv->value));
113         break;
114     default:
115         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116         break;
117     }
118 }
119
120 static void
121 hildon_button_class_init                        (HildonButtonClass *klass)
122 {
123     GObjectClass *gobject_class = (GObjectClass *)klass;
124     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
125
126     gobject_class->set_property = hildon_button_set_property;
127     gobject_class->get_property = hildon_button_get_property;
128
129     g_object_class_install_property (
130         gobject_class,
131         PROP_TITLE,
132         g_param_spec_string (
133             "title",
134             "Title",
135             "Text of the title label inside the button",
136             NULL,
137             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
138
139     g_object_class_install_property (
140         gobject_class,
141         PROP_VALUE,
142         g_param_spec_string (
143             "value",
144             "Value",
145             "Text of the value label inside the button",
146             NULL,
147             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
148
149     g_object_class_install_property (
150         gobject_class,
151         PROP_ARRANGEMENT_FLAGS,
152         g_param_spec_flags (
153             "arrangement-flags",
154             "Arrangement flags",
155             "How the button contents must be arranged",
156             HILDON_TYPE_BUTTON_FLAGS,
157             HILDON_BUTTON_WITH_HORIZONTAL_VALUE,
158             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
159
160     gtk_widget_class_install_style_property (
161         widget_class,
162         g_param_spec_uint (
163             "horizontal-spacing",
164             "Horizontal spacing between labels",
165             "Horizontal spacing between the title and value labels, when in horizontal mode",
166             0, G_MAXUINT, 25,
167             G_PARAM_READABLE));
168
169     gtk_widget_class_install_style_property (
170         widget_class,
171         g_param_spec_uint (
172             "vertical-spacing",
173             "Vertical spacing between labels",
174             "Vertical spacing between the title and value labels, when in vertical mode",
175             0, G_MAXUINT, 5,
176             G_PARAM_READABLE));
177
178     g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
179 }
180
181 static void
182 hildon_button_init                              (HildonButton *self)
183 {
184     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
185
186     priv->title = GTK_LABEL (gtk_label_new (NULL));
187     priv->value = GTK_LABEL (gtk_label_new (NULL));
188     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
189
190     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
191     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
192
193     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
194     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
195
196     /* The value label is not shown automatically, see hildon_button_set_value() */
197     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
198 }
199
200 void
201 hildon_button_set_size_groups                   (HildonButton *button,
202                                                  GtkSizeGroup *title_size_group,
203                                                  GtkSizeGroup *value_size_group)
204 {
205     HildonButtonPrivate *priv;
206
207     g_return_if_fail (HILDON_IS_BUTTON (button));
208
209     priv = HILDON_BUTTON_GET_PRIVATE (button);
210
211     if (title_size_group)
212         gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
213
214     if (value_size_group)
215         gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
216 }
217
218 /**
219  * hildon_button_new:
220  * @flags: flags to set the size and layout of the button
221  *
222  * Creates a new #HildonButton. To add a child widget use gtk_container_add().
223  *
224  * Returns: a new #HildonButton
225  **/
226 GtkWidget *
227 hildon_button_new                               (HildonButtonFlags  flags)
228 {
229     return hildon_button_new_full (flags, NULL, NULL, NULL, NULL);
230 }
231
232 /**
233  * hildon_button_new_with_text:
234  * @flags: flags to set the size and layout of the button
235  * @title: Title of the button (main label)
236  * @value: Value of the button (secondary label), or %NULL
237  *
238  * Creates a new #HildonButton with two labels, @title and @value.
239  *
240  * If you just want to use the main label, set @value to %NULL. You
241  * can set it to a non-%NULL value at any time later.
242  *
243  * Returns: a new #HildonButton
244  **/
245 GtkWidget *
246 hildon_button_new_with_text                     (HildonButtonFlags  flags,
247                                                  const char        *title,
248                                                  const char        *value)
249 {
250     return hildon_button_new_full (flags, title, value, NULL, NULL);
251 }
252
253 GtkWidget *
254 hildon_button_new_full                          (HildonButtonFlags  flags,
255                                                  const char        *title,
256                                                  const char        *value,
257                                                  GtkSizeGroup      *title_size_group,
258                                                  GtkSizeGroup      *value_size_group)
259 {
260     GtkWidget *button;
261
262     /* Create widget */
263     button = g_object_new (HILDON_TYPE_BUTTON,
264                            "arrangement-flags", flags,
265                            "title", title,
266                            "value", value,
267                            "name", "hildon-button",
268                            NULL);
269     /* Set size groups */
270     if (title_size_group || value_size_group)
271         hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group);
272
273     return button;
274 }
275
276 static void
277 hildon_button_set_arrangement (HildonButton *button,
278                                HildonButtonFlags flags)
279 {
280     GtkWidget *box;
281     HildonButtonPrivate *priv;
282     guint horizontal_spacing;
283     guint vertical_spacing;
284     gint width = -1;
285     gint height = -1;
286     const char *widget_name = NULL;
287
288     priv = HILDON_BUTTON_GET_PRIVATE (button);
289
290     /* Requested height */
291     if (flags & HILDON_BUTTON_FINGER_HEIGHT) {
292         height = FINGER_BUTTON_HEIGHT;
293         widget_name = "hildon-finger-button";
294     } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) {
295         height = THUMB_BUTTON_HEIGHT;
296         widget_name = "hildon-thumb-button";
297     }
298
299     if (widget_name) {
300         gtk_widget_set_name (GTK_WIDGET (button), widget_name);
301     }
302
303     /* Requested width */
304     if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) {
305         width = HALFSCREEN_BUTTON_WIDTH;
306     } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) {
307         width = FULLSCREEN_BUTTON_WIDTH;
308     }
309
310     g_object_set (button,
311                   "width-request", width,
312                   "height-request", height,
313                   NULL);
314
315     /* Pack everything */
316     gtk_widget_style_get (GTK_WIDGET (button),
317                           "horizontal-spacing", &horizontal_spacing,
318                           "vertical-spacing", &vertical_spacing,
319                           NULL);
320
321     if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
322         box = gtk_vbox_new (FALSE, vertical_spacing);
323     } else {
324         box = gtk_hbox_new (FALSE, horizontal_spacing);
325     }
326
327     gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
328     gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
329
330     gtk_container_add (GTK_CONTAINER (priv->alignment), box);
331 }
332
333 void
334 hildon_button_set_title                         (HildonButton *button,
335                                                  const char   *title)
336 {
337     HildonButtonPrivate *priv;
338
339     g_return_if_fail (HILDON_IS_BUTTON (button));
340
341     priv = HILDON_BUTTON_GET_PRIVATE (button);
342     gtk_label_set_text (priv->title, title);
343
344     if (title)
345         hildon_button_construct_child (button);
346
347     g_object_notify (G_OBJECT (button), "title");
348 }
349
350 void
351 hildon_button_set_value                         (HildonButton *button,
352                                                  const char   *value)
353 {
354     HildonButtonPrivate *priv;
355
356     g_return_if_fail (HILDON_IS_BUTTON (button));
357
358     priv = HILDON_BUTTON_GET_PRIVATE (button);
359     gtk_label_set_text (priv->value, value);
360
361     /* If the button has no value, hide the label so the title is
362      * properly aligned */
363     if (value)
364         gtk_widget_show (GTK_WIDGET (priv->value));
365     else
366         gtk_widget_hide (GTK_WIDGET (priv->value));
367
368     g_object_notify (G_OBJECT (button), "value");
369 }
370
371 const char *
372 hildon_button_get_title                         (HildonButton *button)
373 {
374     HildonButtonPrivate *priv;
375
376     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
377
378     priv = HILDON_BUTTON_GET_PRIVATE (button);
379
380     return gtk_label_get_text (priv->title);
381 }
382
383 const char *
384 hildon_button_get_value                         (HildonButton *button)
385 {
386     HildonButtonPrivate *priv;
387
388     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
389
390     priv = HILDON_BUTTON_GET_PRIVATE (button);
391
392     return gtk_label_get_text (priv->value);
393 }
394
395 void
396 hildon_button_set_text                          (HildonButton *button,
397                                                  const char   *title,
398                                                  const char   *value)
399 {
400     hildon_button_set_title (button, title);
401     hildon_button_set_value (button, value);
402 }
403
404 static void
405 hildon_button_construct_child                   (HildonButton *button)
406 {
407     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
408     GtkBin *bin = GTK_BIN (button);
409
410     /* Return if there's nothing to do */
411     if (bin->child == priv->alignment)
412         return;
413
414     if (bin->child) {
415         gtk_container_remove (GTK_CONTAINER (button), bin->child);
416     }
417
418     gtk_container_add (GTK_CONTAINER (button), priv->alignment);
419     gtk_widget_show_all (priv->alignment);
420 }