2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * Contact: Karl Lattimer <karl.lattimer@nokia.com>
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.
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.
20 * SECTION:hildon-button
21 * @short_description: Widget representing a button in the Hildon framework.
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.
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.
32 * The #HildonButton can hold any valid child widget, but it usually
33 * contains two labels: title and value (the latter being optional).
36 #include "hildon-button.h"
38 #define FINGER_BUTTON_HEIGHT 70
39 #define THUMB_BUTTON_HEIGHT 105
40 #define HALFSCREEN_BUTTON_WIDTH 400
41 #define FULLSCREEN_BUTTON_WIDTH 800
43 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
45 #define HILDON_BUTTON_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
47 HILDON_TYPE_BUTTON, HildonButtonPrivate));
49 typedef struct _HildonButtonPrivate HildonButtonPrivate;
51 struct _HildonButtonPrivate
61 PROP_ARRANGEMENT_FLAGS
65 hildon_button_set_arrangement (HildonButton *button,
66 HildonButtonFlags flags);
69 hildon_button_construct_child (HildonButton *button);
72 hildon_button_set_property (GObject *object,
77 HildonButton *button = HILDON_BUTTON (object);
82 hildon_button_set_title (button, g_value_get_string (value));
85 hildon_button_set_value (button, g_value_get_string (value));
87 case PROP_ARRANGEMENT_FLAGS:
88 hildon_button_set_arrangement (button, g_value_get_int (value));
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 hildon_button_get_property (GObject *object,
102 HildonButton *button = HILDON_BUTTON (object);
103 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
108 g_value_set_string (value, gtk_label_get_text (priv->title));
111 g_value_set_string (value, gtk_label_get_text (priv->value));
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 hildon_button_class_init (HildonButtonClass *klass)
122 GObjectClass *gobject_class = (GObjectClass *)klass;
123 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
125 gobject_class->set_property = hildon_button_set_property;
126 gobject_class->get_property = hildon_button_get_property;
128 g_object_class_install_property (
131 g_param_spec_string (
134 "Text of the title label inside the button",
136 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
138 g_object_class_install_property (
141 g_param_spec_string (
144 "Text of the value label inside the button",
146 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
148 g_object_class_install_property (
150 PROP_ARRANGEMENT_FLAGS,
154 "How the button contents must be arranged",
156 HILDON_BUTTON_WITH_HORIZONTAL_VALUE,
157 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
159 gtk_widget_class_install_style_property (
162 "horizontal-spacing",
163 "Horizontal spacing between labels",
164 "Horizontal spacing between the title and value labels, when in horizontal mode",
168 gtk_widget_class_install_style_property (
172 "Vertical spacing between labels",
173 "Vertical spacing between the title and value labels, when in vertical mode",
177 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
181 hildon_button_init (HildonButton *self)
183 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
185 priv->title = GTK_LABEL (gtk_label_new (NULL));
186 priv->value = GTK_LABEL (gtk_label_new (NULL));
187 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
189 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
190 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
192 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
193 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
195 /* The value label is not shown automatically, see hildon_button_set_value() */
196 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
200 hildon_button_set_size_groups (HildonButton *button,
201 GtkSizeGroup *title_size_group,
202 GtkSizeGroup *value_size_group)
204 HildonButtonPrivate *priv;
206 g_return_if_fail (HILDON_IS_BUTTON (button));
208 priv = HILDON_BUTTON_GET_PRIVATE (button);
210 if (title_size_group)
211 gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
213 if (value_size_group)
214 gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
219 * @flags: flags to set the size and layout of the button
221 * Creates a new #HildonButton. To add a child widget use gtk_container_add().
223 * Returns: a new #HildonButton
226 hildon_button_new (HildonButtonFlags flags)
228 return hildon_button_new_full (flags, NULL, NULL, NULL, NULL);
232 * hildon_button_new_with_text:
233 * @flags: flags to set the size and layout of the button
234 * @title: Title of the button (main label)
235 * @value: Value of the button (secondary label), or %NULL
237 * Creates a new #HildonButton with two labels, @title and @value.
239 * If you just want to use the main label, set @value to %NULL. You
240 * can set it to a non-%NULL value at any time later.
242 * Returns: a new #HildonButton
245 hildon_button_new_with_text (HildonButtonFlags flags,
249 return hildon_button_new_full (flags, title, value, NULL, NULL);
253 hildon_button_new_full (HildonButtonFlags flags,
256 GtkSizeGroup *title_size_group,
257 GtkSizeGroup *value_size_group)
262 button = g_object_new (HILDON_TYPE_BUTTON,
263 "arrangement-flags", flags,
266 "name", "hildon-button",
268 /* Set size groups */
269 if (title_size_group || value_size_group)
270 hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group);
276 hildon_button_set_arrangement (HildonButton *button,
277 HildonButtonFlags flags)
280 HildonButtonPrivate *priv;
281 guint horizontal_spacing;
282 guint vertical_spacing;
285 const char *widget_name = NULL;
287 priv = HILDON_BUTTON_GET_PRIVATE (button);
289 /* Requested height */
290 if (flags & HILDON_BUTTON_FINGER_HEIGHT) {
291 height = FINGER_BUTTON_HEIGHT;
292 widget_name = "hildon-finger-button";
293 } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) {
294 height = THUMB_BUTTON_HEIGHT;
295 widget_name = "hildon-thumb-button";
299 gtk_widget_set_name (GTK_WIDGET (button), widget_name);
302 /* Requested width */
303 if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) {
304 width = HALFSCREEN_BUTTON_WIDTH;
305 } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) {
306 width = FULLSCREEN_BUTTON_WIDTH;
309 g_object_set (button,
310 "width-request", width,
311 "height-request", height,
314 /* Pack everything */
315 gtk_widget_style_get (GTK_WIDGET (button),
316 "horizontal-spacing", &horizontal_spacing,
317 "vertical-spacing", &vertical_spacing,
320 if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
321 box = gtk_vbox_new (FALSE, vertical_spacing);
323 box = gtk_hbox_new (FALSE, horizontal_spacing);
326 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
327 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
329 gtk_container_add (GTK_CONTAINER (priv->alignment), box);
333 hildon_button_set_title (HildonButton *button,
336 HildonButtonPrivate *priv;
338 g_return_if_fail (HILDON_IS_BUTTON (button));
340 priv = HILDON_BUTTON_GET_PRIVATE (button);
341 gtk_label_set_text (priv->title, title);
344 hildon_button_construct_child (button);
346 g_object_notify (G_OBJECT (button), "title");
350 hildon_button_set_value (HildonButton *button,
353 HildonButtonPrivate *priv;
355 g_return_if_fail (HILDON_IS_BUTTON (button));
357 priv = HILDON_BUTTON_GET_PRIVATE (button);
358 gtk_label_set_text (priv->value, value);
360 /* If the button has no value, hide the label so the title is
361 * properly aligned */
363 gtk_widget_show (GTK_WIDGET (priv->value));
365 gtk_widget_hide (GTK_WIDGET (priv->value));
367 g_object_notify (G_OBJECT (button), "value");
371 hildon_button_get_title (HildonButton *button)
373 HildonButtonPrivate *priv;
375 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
377 priv = HILDON_BUTTON_GET_PRIVATE (button);
379 return gtk_label_get_text (priv->title);
383 hildon_button_get_value (HildonButton *button)
385 HildonButtonPrivate *priv;
387 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
389 priv = HILDON_BUTTON_GET_PRIVATE (button);
391 return gtk_label_get_text (priv->value);
395 hildon_button_set_text (HildonButton *button,
399 hildon_button_set_title (button, title);
400 hildon_button_set_value (button, value);
404 hildon_button_construct_child (HildonButton *button)
406 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
407 GtkBin *bin = GTK_BIN (button);
409 /* Return if there's nothing to do */
410 if (bin->child == priv->alignment)
414 gtk_container_remove (GTK_CONTAINER (button), bin->child);
417 gtk_container_add (GTK_CONTAINER (button), priv->alignment);
418 gtk_widget_show_all (priv->alignment);