2008-08-08 Alberto Garcia <agarcia@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 represents 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, named title and value. To change the alignment
34  * of the labels, use gtk_button_set_alignment()
35  */
36
37 #include                                        "hildon-button.h"
38 #include                                        "hildon-enum-types.h"
39
40 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
41
42 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
43                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
44                                                 HILDON_TYPE_BUTTON, HildonButtonPrivate));
45
46 typedef struct                                  _HildonButtonPrivate HildonButtonPrivate;
47
48 struct                                          _HildonButtonPrivate
49 {
50     GtkLabel *title;
51     GtkLabel *value;
52     GtkBox *hbox;
53     GtkWidget *label_box;
54     GtkWidget *alignment;
55     GtkWidget *image;
56     GtkPositionType image_position;
57 };
58
59 enum {
60     PROP_TITLE = 1,
61     PROP_VALUE,
62     PROP_SIZE,
63     PROP_ARRANGEMENT
64 };
65
66 static void
67 hildon_button_set_arrangement                   (HildonButton            *button,
68                                                  HildonButtonArrangement  arrangement);
69
70 static void
71 hildon_button_construct_child                   (HildonButton *button);
72
73 static void
74 hildon_button_set_property                      (GObject      *object,
75                                                  guint         prop_id,
76                                                  const GValue *value,
77                                                  GParamSpec   *pspec)
78 {
79     HildonButton *button = HILDON_BUTTON (object);
80
81     switch (prop_id)
82     {
83     case PROP_TITLE:
84         hildon_button_set_title (button, g_value_get_string (value));
85         break;
86     case PROP_VALUE:
87         hildon_button_set_value (button, g_value_get_string (value));
88         break;
89     case PROP_SIZE:
90         hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
91         break;
92     case PROP_ARRANGEMENT:
93         hildon_button_set_arrangement (button, g_value_get_enum (value));
94         break;
95     default:
96         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97         break;
98     }
99 }
100
101 static void
102 hildon_button_get_property                      (GObject    *object,
103                                                  guint       prop_id,
104                                                  GValue     *value,
105                                                  GParamSpec *pspec)
106 {
107     HildonButton *button = HILDON_BUTTON (object);
108     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
109
110     switch (prop_id)
111     {
112     case PROP_TITLE:
113         g_value_set_string (value, gtk_label_get_text (priv->title));
114         break;
115     case PROP_VALUE:
116         g_value_set_string (value, gtk_label_get_text (priv->value));
117         break;
118     default:
119         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120         break;
121     }
122 }
123
124 static void
125 hildon_button_class_init                        (HildonButtonClass *klass)
126 {
127     GObjectClass *gobject_class = (GObjectClass *)klass;
128     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
129
130     gobject_class->set_property = hildon_button_set_property;
131     gobject_class->get_property = hildon_button_get_property;
132
133     g_object_class_install_property (
134         gobject_class,
135         PROP_TITLE,
136         g_param_spec_string (
137             "title",
138             "Title",
139             "Text of the title label inside the button",
140             NULL,
141             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
142
143     g_object_class_install_property (
144         gobject_class,
145         PROP_VALUE,
146         g_param_spec_string (
147             "value",
148             "Value",
149             "Text of the value label inside the button",
150             NULL,
151             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
152
153     g_object_class_install_property (
154         gobject_class,
155         PROP_SIZE,
156         g_param_spec_flags (
157             "size",
158             "Size",
159             "Size request for the button",
160             HILDON_TYPE_SIZE_TYPE,
161             HILDON_SIZE_AUTO,
162             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
163
164     g_object_class_install_property (
165         gobject_class,
166         PROP_ARRANGEMENT,
167         g_param_spec_enum (
168             "arrangement",
169             "Arrangement",
170             "How the button contents must be arranged",
171             HILDON_TYPE_BUTTON_ARRANGEMENT,
172             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
173             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
174
175     gtk_widget_class_install_style_property (
176         widget_class,
177         g_param_spec_uint (
178             "horizontal-spacing",
179             "Horizontal spacing between labels",
180             "Horizontal spacing between the title and value labels, when in horizontal mode",
181             0, G_MAXUINT, 25,
182             G_PARAM_READABLE));
183
184     gtk_widget_class_install_style_property (
185         widget_class,
186         g_param_spec_uint (
187             "vertical-spacing",
188             "Vertical spacing between labels",
189             "Vertical spacing between the title and value labels, when in vertical mode",
190             0, G_MAXUINT, 5,
191             G_PARAM_READABLE));
192
193     g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
194 }
195
196 static void
197 hildon_button_init                              (HildonButton *self)
198 {
199     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
200
201     priv->title = GTK_LABEL (gtk_label_new (NULL));
202     priv->value = GTK_LABEL (gtk_label_new (NULL));
203     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
204     priv->image = NULL;
205     priv->image_position = GTK_POS_LEFT;
206     priv->hbox = NULL;
207     priv->label_box = NULL;
208
209     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
210     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
211
212     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
213     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
214
215     /* The labels are not shown automatically, see hildon_button_set_(title|value) */
216     gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
217     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
218 }
219
220 /**
221  * hildon_button_set_size_groups:
222  * @button: a #HildonButton
223  * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
224  * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
225  *
226  * Adds the title and value labels of @button to @title_size_group and
227  * @value_size_group respectively. %NULL size groups will be ignored.
228  **/
229 void
230 hildon_button_set_size_groups                   (HildonButton *button,
231                                                  GtkSizeGroup *title_size_group,
232                                                  GtkSizeGroup *value_size_group)
233 {
234     HildonButtonPrivate *priv;
235
236     g_return_if_fail (HILDON_IS_BUTTON (button));
237     g_return_if_fail (!title_size_group || GTK_IS_SIZE_GROUP (title_size_group));
238     g_return_if_fail (!value_size_group || GTK_IS_SIZE_GROUP (value_size_group));
239
240     priv = HILDON_BUTTON_GET_PRIVATE (button);
241
242     if (title_size_group)
243         gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
244
245     if (value_size_group)
246         gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
247 }
248
249 /**
250  * hildon_button_new:
251  * @size: Flags to set the size of the button.
252  * @arrangement: How the labels must be arranged.
253  *
254  * Creates a new #HildonButton. To add a child widget use gtk_container_add().
255  *
256  * Returns: a new #HildonButton
257  **/
258 GtkWidget *
259 hildon_button_new                               (HildonSizeType          size,
260                                                  HildonButtonArrangement arrangement)
261 {
262     return hildon_button_new_full (size, arrangement, NULL, NULL, NULL, NULL);
263 }
264
265 /**
266  * hildon_button_new_with_text:
267  * @size: Flags to set the size of the button.
268  * @arrangement: How the labels must be arranged.
269  * @title: Title of the button (main label), or %NULL
270  * @value: Value of the button (secondary label), or %NULL
271  *
272  * Creates a new #HildonButton with two labels, @title and @value.
273  *
274  * If you just don't want to use one of the labels, set it to
275  * %NULL. You can set it to a non-%NULL value at any time later.
276  *
277  * Returns: a new #HildonButton
278  **/
279 GtkWidget *
280 hildon_button_new_with_text                     (HildonSizeType           size,
281                                                  HildonButtonArrangement  arrangement,
282                                                  const gchar             *title,
283                                                  const gchar             *value)
284 {
285     return hildon_button_new_full (size, arrangement, title, value, NULL, NULL);
286 }
287
288 /**
289  * hildon_button_new_full:
290  * @size: Flags to set the size of the button.
291  * @arrangement: How the labels must be arranged.
292  * @title: Title of the button (main label)
293  * @value: Value of the button (secondary label), or %NULL
294  * @title_size_group: a #GtkSizeGroup for the @title label, or %NULL
295  * @value_size_group: a #GtkSizeGroup for the @value label, or %NULL
296  *
297  * Creates a new #HildonButton with two labels, @title and @value, and
298  * their respective size groups.
299  *
300  * If you just don't want to use one of the labels, set it to
301  * %NULL. You can set it to a non-%NULL value at any time later.
302  *
303  * @title and @value will be added to @title_size_group and
304  * @value_size_group, respectively, if present.
305  *
306  * Returns: a new #HildonButton
307  **/
308 GtkWidget *
309 hildon_button_new_full                          (HildonSizeType           size,
310                                                  HildonButtonArrangement  arrangement,
311                                                  const gchar             *title,
312                                                  const gchar             *value,
313                                                  GtkSizeGroup            *title_size_group,
314                                                  GtkSizeGroup            *value_size_group)
315 {
316     GtkWidget *button;
317
318     /* Create widget */
319     button = g_object_new (HILDON_TYPE_BUTTON,
320                            "size", size,
321                            "arrangement", arrangement,
322                            "title", title,
323                            "value", value,
324                            "name", "hildon-button",
325                            NULL);
326     /* Set size groups */
327     if (title_size_group || value_size_group)
328         hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group);
329
330     return button;
331 }
332
333 static void
334 hildon_button_set_arrangement                   (HildonButton            *button,
335                                                  HildonButtonArrangement  arrangement)
336 {
337     HildonButtonPrivate *priv;
338     guint horizontal_spacing;
339     guint vertical_spacing;
340
341     priv = HILDON_BUTTON_GET_PRIVATE (button);
342
343     /* Pack everything */
344     gtk_widget_style_get (GTK_WIDGET (button),
345                           "horizontal-spacing", &horizontal_spacing,
346                           "vertical-spacing", &vertical_spacing,
347                           NULL);
348
349     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
350         priv->label_box = gtk_vbox_new (FALSE, vertical_spacing);
351     } else {
352         priv->label_box = gtk_hbox_new (FALSE, horizontal_spacing);
353     }
354
355     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), FALSE, FALSE, 0);
356     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
357 }
358
359 /**
360  * hildon_button_set_title:
361  * @button: a #HildonButton
362  * @title: a new title (main label) for the button, or %NULL
363  *
364  * Sets the title (main label) of @button to @title.
365  *
366  * This will clear the previously set title.
367  *
368  * If @title is set to %NULL, the title label will be hidden and the
369  * value label will be realigned.
370  **/
371 void
372 hildon_button_set_title                         (HildonButton *button,
373                                                  const gchar  *title)
374 {
375     HildonButtonPrivate *priv;
376
377     g_return_if_fail (HILDON_IS_BUTTON (button));
378
379     priv = HILDON_BUTTON_GET_PRIVATE (button);
380     gtk_label_set_text (priv->title, title);
381
382     /* If the button has no title, hide the label so the value is
383      * properly aligned */
384     if (title) {
385         hildon_button_construct_child (button);
386         gtk_widget_show (GTK_WIDGET (priv->title));
387     } else {
388         gtk_widget_hide (GTK_WIDGET (priv->title));
389     }
390
391     g_object_notify (G_OBJECT (button), "title");
392 }
393
394 /**
395  * hildon_button_set_value:
396  * @button: a #HildonButton
397  * @value: a new value (secondary label) for the button, or %NULL
398  *
399  * Sets the value (secondary label) of @button to @value.
400  *
401  * This will clear the previously set value.
402  *
403  * If @value is set to %NULL, the value label will be hidden and the
404  * title label will be realigned.
405  *
406  **/
407 void
408 hildon_button_set_value                         (HildonButton *button,
409                                                  const gchar  *value)
410 {
411     HildonButtonPrivate *priv;
412
413     g_return_if_fail (HILDON_IS_BUTTON (button));
414
415     priv = HILDON_BUTTON_GET_PRIVATE (button);
416     gtk_label_set_text (priv->value, value);
417
418     /* If the button has no value, hide the label so the title is
419      * properly aligned */
420     if (value) {
421         hildon_button_construct_child (button);
422         gtk_widget_show (GTK_WIDGET (priv->value));
423     } else {
424         gtk_widget_hide (GTK_WIDGET (priv->value));
425     }
426
427     g_object_notify (G_OBJECT (button), "value");
428 }
429
430 /**
431  * hildon_button_get_title:
432  * @button: a #HildonButton
433  *
434  * Gets the text from the main label (title) of @button, or %NULL if
435  * none has been set.
436  *
437  * Returns: The text of the title label. This string is owned by the
438  * widget and must not be modified or freed.
439  **/
440 const gchar *
441 hildon_button_get_title                         (HildonButton *button)
442 {
443     HildonButtonPrivate *priv;
444
445     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
446
447     priv = HILDON_BUTTON_GET_PRIVATE (button);
448
449     return gtk_label_get_text (priv->title);
450 }
451
452 /**
453  * hildon_button_get_value:
454  * @button: a #HildonButton
455  *
456  * Gets the text from the secondary label (value) of @button, or %NULL
457  * if none has been set.
458  *
459  * Returns: The text of the value label. This string is owned by the
460  * widget and must not be modified or freed.
461  **/
462 const gchar *
463 hildon_button_get_value                         (HildonButton *button)
464 {
465     HildonButtonPrivate *priv;
466
467     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
468
469     priv = HILDON_BUTTON_GET_PRIVATE (button);
470
471     return gtk_label_get_text (priv->value);
472 }
473
474 /**
475  * hildon_button_set_text:
476  * @button: a #HildonButton
477  * @title: new text for the button title (main label)
478  * @value: new text for the button value (secondary label)
479  *
480  * Convenience function to change both labels of a #HildonButton
481  **/
482 void
483 hildon_button_set_text                          (HildonButton *button,
484                                                  const gchar  *title,
485                                                  const gchar  *value)
486 {
487     hildon_button_set_title (button, title);
488     hildon_button_set_value (button, value);
489 }
490
491 /**
492  * hildon_button_set_image:
493  * @button: a #HildonButton
494  * @image: a widget to set as the button image
495  *
496  * Sets the image of @button to the given widget. The previous image
497  * (if any) will be removed.
498  **/
499 void
500 hildon_button_set_image                         (HildonButton *button,
501                                                  GtkWidget    *image)
502 {
503     HildonButtonPrivate *priv;
504
505     g_return_if_fail (HILDON_IS_BUTTON (button));
506     g_return_if_fail (!image || GTK_IS_WIDGET (image));
507
508     priv = HILDON_BUTTON_GET_PRIVATE (button);
509
510     /* Return if there's nothing to do */
511     if (image == priv->image)
512         return;
513
514     if (priv->image && priv->image->parent)
515         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
516
517     priv->image = image;
518
519     hildon_button_construct_child (button);
520 }
521
522 /**
523  * hildon_button_set_image_position:
524  * @button: a #HildonButton
525  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
526  *
527  * Sets the position of the image inside @button. Only left and right
528  * are supported.
529  **/
530 void
531 hildon_button_set_image_position                (HildonButton    *button,
532                                                  GtkPositionType  position)
533 {
534     HildonButtonPrivate *priv;
535
536     g_return_if_fail (HILDON_IS_BUTTON (button));
537     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
538
539     priv = HILDON_BUTTON_GET_PRIVATE (button);
540
541     /* Return if there's nothing to do */
542     if (priv->image_position == position)
543         return;
544
545     priv->image_position = position;
546
547     hildon_button_construct_child (button);
548 }
549
550 static void
551 hildon_button_construct_child                   (HildonButton *button)
552 {
553     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
554     GtkWidget *child;
555
556     /* Save a ref to the image if necessary */
557     if (priv->image && priv->image->parent != NULL) {
558         g_object_ref (priv->image);
559         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
560     }
561
562     /* Save a ref to the label box if necessary */
563     if (priv->label_box->parent != NULL) {
564         g_object_ref (priv->label_box);
565         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
566     }
567
568     /* Remove the child from the container and add priv->alignment */
569     child = gtk_bin_get_child (GTK_BIN (button));
570     if (child != NULL && child != priv->alignment) {
571         gtk_container_remove (GTK_CONTAINER (button), child);
572         child = NULL;
573     }
574
575     if (child == NULL) {
576         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
577     }
578
579     /* Create a new hbox */
580     if (priv->hbox) {
581         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
582     }
583     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, 10));
584     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
585
586     /* Pack the image and the alignment in the new hbox */
587     if (priv->image && priv->image_position == GTK_POS_LEFT)
588         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
589
590     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
591
592     if (priv->image && priv->image_position == GTK_POS_RIGHT)
593         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
594
595     /* Show everything */
596     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
597 }