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"
37 #include "hildon-enum-types.h"
39 #define FINGER_BUTTON_HEIGHT 70
40 #define THUMB_BUTTON_HEIGHT 105
41 #define HALFSCREEN_BUTTON_WIDTH 400
42 #define FULLSCREEN_BUTTON_WIDTH 800
44 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
46 #define HILDON_BUTTON_GET_PRIVATE(obj) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
48 HILDON_TYPE_BUTTON, HildonButtonPrivate));
50 typedef struct _HildonButtonPrivate HildonButtonPrivate;
52 struct _HildonButtonPrivate
62 PROP_ARRANGEMENT_FLAGS
66 hildon_button_set_arrangement (HildonButton *button,
67 HildonButtonFlags flags);
70 hildon_button_construct_child (HildonButton *button);
73 hildon_button_set_property (GObject *object,
78 HildonButton *button = HILDON_BUTTON (object);
83 hildon_button_set_title (button, g_value_get_string (value));
86 hildon_button_set_value (button, g_value_get_string (value));
88 case PROP_ARRANGEMENT_FLAGS:
89 hildon_button_set_arrangement (button, g_value_get_enum (value));
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 hildon_button_get_property (GObject *object,
103 HildonButton *button = HILDON_BUTTON (object);
104 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
109 g_value_set_string (value, gtk_label_get_text (priv->title));
112 g_value_set_string (value, gtk_label_get_text (priv->value));
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 hildon_button_class_init (HildonButtonClass *klass)
123 GObjectClass *gobject_class = (GObjectClass *)klass;
124 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
126 gobject_class->set_property = hildon_button_set_property;
127 gobject_class->get_property = hildon_button_get_property;
129 g_object_class_install_property (
132 g_param_spec_string (
135 "Text of the title label inside the button",
137 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
139 g_object_class_install_property (
142 g_param_spec_string (
145 "Text of the value label inside the button",
147 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
149 g_object_class_install_property (
151 PROP_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));
160 gtk_widget_class_install_style_property (
163 "horizontal-spacing",
164 "Horizontal spacing between labels",
165 "Horizontal spacing between the title and value labels, when in horizontal mode",
169 gtk_widget_class_install_style_property (
173 "Vertical spacing between labels",
174 "Vertical spacing between the title and value labels, when in vertical mode",
178 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
182 hildon_button_init (HildonButton *self)
184 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
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);
190 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
191 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
193 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
194 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
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);
201 hildon_button_set_size_groups (HildonButton *button,
202 GtkSizeGroup *title_size_group,
203 GtkSizeGroup *value_size_group)
205 HildonButtonPrivate *priv;
207 g_return_if_fail (HILDON_IS_BUTTON (button));
209 priv = HILDON_BUTTON_GET_PRIVATE (button);
211 if (title_size_group)
212 gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
214 if (value_size_group)
215 gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
220 * @flags: flags to set the size and layout of the button
222 * Creates a new #HildonButton. To add a child widget use gtk_container_add().
224 * Returns: a new #HildonButton
227 hildon_button_new (HildonButtonFlags flags)
229 return hildon_button_new_full (flags, NULL, NULL, NULL, NULL);
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
238 * Creates a new #HildonButton with two labels, @title and @value.
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.
243 * Returns: a new #HildonButton
246 hildon_button_new_with_text (HildonButtonFlags flags,
250 return hildon_button_new_full (flags, title, value, NULL, NULL);
254 hildon_button_new_full (HildonButtonFlags flags,
257 GtkSizeGroup *title_size_group,
258 GtkSizeGroup *value_size_group)
263 button = g_object_new (HILDON_TYPE_BUTTON,
264 "arrangement-flags", flags,
267 "name", "hildon-button",
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);
277 hildon_button_set_arrangement (HildonButton *button,
278 HildonButtonFlags flags)
281 HildonButtonPrivate *priv;
282 guint horizontal_spacing;
283 guint vertical_spacing;
286 const char *widget_name = NULL;
288 priv = HILDON_BUTTON_GET_PRIVATE (button);
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";
300 gtk_widget_set_name (GTK_WIDGET (button), widget_name);
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;
310 g_object_set (button,
311 "width-request", width,
312 "height-request", height,
315 /* Pack everything */
316 gtk_widget_style_get (GTK_WIDGET (button),
317 "horizontal-spacing", &horizontal_spacing,
318 "vertical-spacing", &vertical_spacing,
321 if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
322 box = gtk_vbox_new (FALSE, vertical_spacing);
324 box = gtk_hbox_new (FALSE, horizontal_spacing);
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);
330 gtk_container_add (GTK_CONTAINER (priv->alignment), box);
334 hildon_button_set_title (HildonButton *button,
337 HildonButtonPrivate *priv;
339 g_return_if_fail (HILDON_IS_BUTTON (button));
341 priv = HILDON_BUTTON_GET_PRIVATE (button);
342 gtk_label_set_text (priv->title, title);
345 hildon_button_construct_child (button);
347 g_object_notify (G_OBJECT (button), "title");
351 hildon_button_set_value (HildonButton *button,
354 HildonButtonPrivate *priv;
356 g_return_if_fail (HILDON_IS_BUTTON (button));
358 priv = HILDON_BUTTON_GET_PRIVATE (button);
359 gtk_label_set_text (priv->value, value);
361 /* If the button has no value, hide the label so the title is
362 * properly aligned */
364 gtk_widget_show (GTK_WIDGET (priv->value));
366 gtk_widget_hide (GTK_WIDGET (priv->value));
368 g_object_notify (G_OBJECT (button), "value");
372 hildon_button_get_title (HildonButton *button)
374 HildonButtonPrivate *priv;
376 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
378 priv = HILDON_BUTTON_GET_PRIVATE (button);
380 return gtk_label_get_text (priv->title);
384 hildon_button_get_value (HildonButton *button)
386 HildonButtonPrivate *priv;
388 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
390 priv = HILDON_BUTTON_GET_PRIVATE (button);
392 return gtk_label_get_text (priv->value);
396 hildon_button_set_text (HildonButton *button,
400 hildon_button_set_title (button, title);
401 hildon_button_set_value (button, value);
405 hildon_button_construct_child (HildonButton *button)
407 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
408 GtkBin *bin = GTK_BIN (button);
410 /* Return if there's nothing to do */
411 if (bin->child == priv->alignment)
415 gtk_container_remove (GTK_CONTAINER (button), bin->child);
418 gtk_container_add (GTK_CONTAINER (button), priv->alignment);
419 gtk_widget_show_all (priv->alignment);