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
52 PROP_ARRANGEMENT_FLAGS
55 static void hildon_button_set_arrangement (HildonButton *button, HildonButtonFlags flags);
58 hildon_button_set_property (GObject *object,
63 HildonButton *button = HILDON_BUTTON (object);
68 hildon_button_set_title (button, g_value_get_string (value));
71 hildon_button_set_value (button, g_value_get_string (value));
73 case PROP_ARRANGEMENT_FLAGS:
74 hildon_button_set_arrangement (button, g_value_get_int (value));
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83 hildon_button_get_property (GObject *object,
88 HildonButton *button = HILDON_BUTTON (object);
89 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
94 g_value_set_string (value, gtk_label_get_text (priv->title));
97 g_value_set_string (value, gtk_label_get_text (priv->value));
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 hildon_button_class_init (HildonButtonClass *klass)
108 GObjectClass *gobject_class = (GObjectClass *)klass;
109 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
111 gobject_class->set_property = hildon_button_set_property;
112 gobject_class->get_property = hildon_button_get_property;
114 g_object_class_install_property (
117 g_param_spec_string (
120 "Text of the title label inside the button",
122 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
124 g_object_class_install_property (
127 g_param_spec_string (
130 "Text of the value label inside the button",
132 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
134 g_object_class_install_property (
136 PROP_ARRANGEMENT_FLAGS,
137 g_param_spec_int ("arrangement-flags",
139 "How the button contents must be arranged",
141 HILDON_BUTTON_WITH_HORIZONTAL_VALUE,
142 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
144 gtk_widget_class_install_style_property (
147 "horizontal-spacing",
148 "Horizontal spacing between labels",
149 "Horizontal spacing between the title and value labels, when in horizontal mode",
153 gtk_widget_class_install_style_property (
157 "Vertical spacing between labels",
158 "Vertical spacing between the title and value labels, when in vertical mode",
162 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
166 hildon_button_init (HildonButton *self)
168 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
170 priv->title = GTK_LABEL (gtk_label_new (NULL));
171 priv->value = GTK_LABEL (gtk_label_new (NULL));
173 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
174 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
176 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
177 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
181 hildon_button_set_size_groups (HildonButton *button,
182 GtkSizeGroup *title_size_group,
183 GtkSizeGroup *value_size_group)
185 HildonButtonPrivate *priv;
187 g_return_if_fail (HILDON_IS_BUTTON (button));
189 priv = HILDON_BUTTON_GET_PRIVATE (button);
191 if (title_size_group)
192 gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title));
194 if (value_size_group)
195 gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value));
199 hildon_button_new (HildonButtonFlags flags,
203 return hildon_button_new_full (flags, title, value, NULL, NULL);
207 hildon_button_new_full (HildonButtonFlags flags,
210 GtkSizeGroup *title_size_group,
211 GtkSizeGroup *value_size_group)
216 button = g_object_new (HILDON_TYPE_BUTTON,
217 "arrangement-flags", flags,
220 "name", "hildon-button",
222 /* Set size groups */
223 if (title_size_group || value_size_group)
224 hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group);
230 hildon_button_set_arrangement (HildonButton *button,
231 HildonButtonFlags flags)
234 GtkWidget *alignment;
235 HildonButtonPrivate *priv;
236 guint horizontal_spacing;
237 guint vertical_spacing;
240 const char *widget_name = NULL;
242 priv = HILDON_BUTTON_GET_PRIVATE (button);
244 /* Requested height */
245 if (flags & HILDON_BUTTON_FINGER_HEIGHT) {
246 height = FINGER_BUTTON_HEIGHT;
247 widget_name = "hildon-finger-button";
248 } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) {
249 height = THUMB_BUTTON_HEIGHT;
250 widget_name = "hildon-thumb-button";
254 gtk_widget_set_name (GTK_WIDGET (button), widget_name);
257 /* Requested width */
258 if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) {
259 width = HALFSCREEN_BUTTON_WIDTH;
260 } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) {
261 width = FULLSCREEN_BUTTON_WIDTH;
264 g_object_set (button,
265 "width-request", width,
266 "height-request", height,
269 /* Pack everything */
270 gtk_widget_style_get (GTK_WIDGET (button),
271 "horizontal-spacing", &horizontal_spacing,
272 "vertical-spacing", &vertical_spacing,
275 if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) {
276 box = gtk_vbox_new (FALSE, vertical_spacing);
278 box = gtk_hbox_new (FALSE, horizontal_spacing);
281 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
283 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
284 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
286 gtk_container_add (GTK_CONTAINER (alignment), box);
287 gtk_container_add (GTK_CONTAINER (button), alignment);
289 gtk_widget_show_all (alignment);
293 hildon_button_set_title (HildonButton *button,
296 HildonButtonPrivate *priv;
298 g_return_if_fail (HILDON_IS_BUTTON (button));
300 priv = HILDON_BUTTON_GET_PRIVATE (button);
301 gtk_label_set_text (priv->title, title);
303 g_object_notify (G_OBJECT (button), "title");
307 hildon_button_set_value (HildonButton *button,
310 HildonButtonPrivate *priv;
312 g_return_if_fail (HILDON_IS_BUTTON (button));
314 priv = HILDON_BUTTON_GET_PRIVATE (button);
315 gtk_label_set_text (priv->value, value);
317 g_object_notify (G_OBJECT (button), "value");
321 hildon_button_get_title (HildonButton *button)
323 HildonButtonPrivate *priv;
325 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
327 priv = HILDON_BUTTON_GET_PRIVATE (button);
329 return gtk_label_get_text (priv->title);
333 hildon_button_get_value (HildonButton *button)
335 HildonButtonPrivate *priv;
337 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
339 priv = HILDON_BUTTON_GET_PRIVATE (button);
341 return gtk_label_get_text (priv->value);
345 hildon_button_set_title_and_value (HildonButton *button,
349 hildon_button_set_title (button, title);
350 hildon_button_set_value (button, value);