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.
28 #include "hildon-button.h"
30 #define FINGER_BUTTON_HEIGHT 70
31 #define THUMB_BUTTON_HEIGHT 105
32 #define HALFSCREEN_BUTTON_WIDTH 400
33 #define FULLSCREEN_BUTTON_WIDTH 800
35 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
37 #define HILDON_BUTTON_GET_PRIVATE(obj) \
38 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
39 HILDON_TYPE_BUTTON, HildonButtonPrivate));
41 typedef struct _HildonButtonPrivate HildonButtonPrivate;
43 struct _HildonButtonPrivate
53 PROP_ARRANGEMENT_FLAGS
57 hildon_button_set_arrangement (HildonButton *button,
58 HildonButtonFlags flags);
61 hildon_button_construct_child (HildonButton *button);
64 hildon_button_set_property (GObject *object,
69 HildonButton *button = HILDON_BUTTON (object);
74 hildon_button_set_title (button, g_value_get_string (value));
77 hildon_button_set_value (button, g_value_get_string (value));
79 case PROP_ARRANGEMENT_FLAGS:
80 hildon_button_set_arrangement (button, g_value_get_int (value));
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 hildon_button_get_property (GObject *object,
94 HildonButton *button = HILDON_BUTTON (object);
95 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
100 g_value_set_string (value, gtk_label_get_text (priv->title));
103 g_value_set_string (value, gtk_label_get_text (priv->value));
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 hildon_button_class_init (HildonButtonClass *klass)
114 GObjectClass *gobject_class = (GObjectClass *)klass;
115 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
117 gobject_class->set_property = hildon_button_set_property;
118 gobject_class->get_property = hildon_button_get_property;
120 g_object_class_install_property (
123 g_param_spec_string (
126 "Text of the title label inside the button",
128 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
130 g_object_class_install_property (
133 g_param_spec_string (
136 "Text of the value label inside the button",
138 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
140 g_object_class_install_property (
142 PROP_ARRANGEMENT_FLAGS,
146 "How the button contents must be arranged",
148 HILDON_BUTTON_WITH_HORIZONTAL_VALUE,
149 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
151 gtk_widget_class_install_style_property (
154 "horizontal-spacing",
155 "Horizontal spacing between labels",
156 "Horizontal spacing between the title and value labels, when in horizontal mode",
160 gtk_widget_class_install_style_property (
164 "Vertical spacing between labels",
165 "Vertical spacing between the title and value labels, when in vertical mode",
169 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
173 hildon_button_init (HildonButton *self)
175 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
177 priv->title = GTK_LABEL (gtk_label_new (NULL));
178 priv->value = GTK_LABEL (gtk_label_new (NULL));
179 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
181 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
182 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
184 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
185 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
187 /* The value label is not shown automatically, see hildon_button_set_value() */
188 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
192 hildon_button_set_size_groups (HildonButton *button,
193 GtkSizeGroup *title_size_group,
194 GtkSizeGroup *value_size_group)
196 HildonButtonPrivate *priv;
198 g_return_if_fail (HILDON_IS_BUTTON (button));
200 priv = HILDON_BUTTON_GET_PRIVATE (button);
202 if (title_size_group)
203 gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
205 if (value_size_group)
206 gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
210 hildon_button_new (HildonButtonFlags flags)
212 return hildon_button_new_full (flags, NULL, NULL, NULL, NULL);
216 hildon_button_new_with_text (HildonButtonFlags flags,
220 return hildon_button_new_full (flags, title, value, NULL, NULL);
224 hildon_button_new_full (HildonButtonFlags flags,
227 GtkSizeGroup *title_size_group,
228 GtkSizeGroup *value_size_group)
233 button = g_object_new (HILDON_TYPE_BUTTON,
234 "arrangement-flags", flags,
237 "name", "hildon-button",
239 /* Set size groups */
240 if (title_size_group || value_size_group)
241 hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group);
247 hildon_button_set_arrangement (HildonButton *button,
248 HildonButtonFlags flags)
251 HildonButtonPrivate *priv;
252 guint horizontal_spacing;
253 guint vertical_spacing;
256 const char *widget_name = NULL;
258 priv = HILDON_BUTTON_GET_PRIVATE (button);
260 /* Requested height */
261 if (flags & HILDON_BUTTON_FINGER_HEIGHT) {
262 height = FINGER_BUTTON_HEIGHT;
263 widget_name = "hildon-finger-button";
264 } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) {
265 height = THUMB_BUTTON_HEIGHT;
266 widget_name = "hildon-thumb-button";
270 gtk_widget_set_name (GTK_WIDGET (button), widget_name);
273 /* Requested width */
274 if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) {
275 width = HALFSCREEN_BUTTON_WIDTH;
276 } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) {
277 width = FULLSCREEN_BUTTON_WIDTH;
280 g_object_set (button,
281 "width-request", width,
282 "height-request", height,
285 /* Pack everything */
286 gtk_widget_style_get (GTK_WIDGET (button),
287 "horizontal-spacing", &horizontal_spacing,
288 "vertical-spacing", &vertical_spacing,
291 if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
292 box = gtk_vbox_new (FALSE, vertical_spacing);
294 box = gtk_hbox_new (FALSE, horizontal_spacing);
297 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
298 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
300 gtk_container_add (GTK_CONTAINER (priv->alignment), box);
304 hildon_button_set_title (HildonButton *button,
307 HildonButtonPrivate *priv;
309 g_return_if_fail (HILDON_IS_BUTTON (button));
311 priv = HILDON_BUTTON_GET_PRIVATE (button);
312 gtk_label_set_text (priv->title, title);
315 hildon_button_construct_child (button);
317 g_object_notify (G_OBJECT (button), "title");
321 hildon_button_set_value (HildonButton *button,
324 HildonButtonPrivate *priv;
326 g_return_if_fail (HILDON_IS_BUTTON (button));
328 priv = HILDON_BUTTON_GET_PRIVATE (button);
329 gtk_label_set_text (priv->value, value);
331 /* If the button has no value, hide the label so the title is
332 * properly aligned */
334 gtk_widget_show (GTK_WIDGET (priv->value));
336 gtk_widget_hide (GTK_WIDGET (priv->value));
338 g_object_notify (G_OBJECT (button), "value");
342 hildon_button_get_title (HildonButton *button)
344 HildonButtonPrivate *priv;
346 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
348 priv = HILDON_BUTTON_GET_PRIVATE (button);
350 return gtk_label_get_text (priv->title);
354 hildon_button_get_value (HildonButton *button)
356 HildonButtonPrivate *priv;
358 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
360 priv = HILDON_BUTTON_GET_PRIVATE (button);
362 return gtk_label_get_text (priv->value);
366 hildon_button_set_text (HildonButton *button,
370 hildon_button_set_title (button, title);
371 hildon_button_set_value (button, value);
375 hildon_button_construct_child (HildonButton *button)
377 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
378 GtkBin *bin = GTK_BIN (button);
380 /* Return if there's nothing to do */
381 if (bin->child == priv->alignment)
385 gtk_container_remove (GTK_CONTAINER (button), bin->child);
388 gtk_container_add (GTK_CONTAINER (button), priv->alignment);
389 gtk_widget_show_all (priv->alignment);