2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * SECTION:hildon-hvolumebar
27 * @short_description: A widget that displays a horizontal volume bar.
28 * @see_also: #HildonVVolumebar, #HildonVolumebar
30 * The #HildonHVolumebar widget displays a horizontal volume bar that allows
31 * increasing or decreasing volume within a pre-defined range, and includes
32 * a mute icon which users can click to mute the sound.
36 * GtkWidget *volbar = hildon_hvolumebar_new ();
37 * g_signal_connect (G_OBJECT(volbar), "mute_toggled", G_CALLBACK(mute_toggle), NULL);
38 * g_signal_connect (G_OBJECT(volbar), "level_changed", G_CALLBACK(level_change), NULL);
44 #undef HILDON_DISABLE_DEPRECATED
46 #include "hildon-hvolumebar.h"
47 #include "hildon-volumebar.h"
48 #include "hildon-volumebar-range.h"
49 #include "hildon-volumebar-private.h"
51 /* Defines for normal version of HVolumebar */
54 #define DEFAULT_TBUTTON_WIDTH 60
56 #define DEFAULT_TBUTTON_HEIGHT 60
59 #define MINIMUM_BAR_WIDTH 147
61 #define DEFAULT_BAR_HEIGHT 60
63 #define DEFAULT_ENDING_SIZE 20
65 /* Gap to leave for mute button */
66 #define VERTICAL_MUTE_GAP 0
68 #define HORIZONTAL_MUTE_GAP 0
70 /* Sizes inside a toolbar */
73 #define TOOL_DEFAULT_TBUTTON_WIDTH 26
75 #define TOOL_DEFAULT_TBUTTON_HEIGHT 26
79 #define TOOL_MINIMUM_BAR_WIDTH 121
81 #define TOOL_DEFAULT_BAR_HEIGHT 40
83 #define TOOL_DEFAULT_ENDING_SIZE 0
85 #define TOOL_VERTICAL_MUTE_GAP \
86 ((TOOL_DEFAULT_BAR_HEIGHT - TOOL_DEFAULT_TBUTTON_HEIGHT) / 2)
88 static HildonVolumebarClass* parent_class;
91 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass);
94 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar);
97 hildon_hvolumebar_expose (GtkWidget *widget,
98 GdkEventExpose *event);
100 hildon_hvolumebar_size_request (GtkWidget *widget,
101 GtkRequisition *requisition);
103 hildon_hvolumebar_size_allocate (GtkWidget *widget,
104 GtkAllocation *allocation);
106 hildon_hvolumebar_map (GtkWidget *widget);
109 * hildon_hvolumebar_get_type:
111 * Returns GType for HildonHVolumebar.
113 * Returns: HildonHVolumebar type
116 hildon_hvolumebar_get_type (void)
118 static GType type = 0;
121 static const GTypeInfo info = {
122 sizeof (HildonHVolumebarClass),
123 NULL, /* base_init */
124 NULL, /* base_finalize */
125 (GClassInitFunc) hildon_hvolumebar_class_init, /* class_init */
126 NULL, /* class_finalize */
127 NULL, /* class_data */
128 sizeof (HildonHVolumebar),
130 (GInstanceInitFunc) hildon_hvolumebar_init,
132 type = g_type_register_static (HILDON_TYPE_VOLUMEBAR,
133 "HildonHVolumebar", &info, 0);
139 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass)
141 GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS (klass);
143 parent_class = g_type_class_peek_parent (klass);
145 volumebar_class->size_request = hildon_hvolumebar_size_request;
146 volumebar_class->size_allocate = hildon_hvolumebar_size_allocate;
147 volumebar_class->map = hildon_hvolumebar_map;
148 volumebar_class->expose_event = hildon_hvolumebar_expose;
152 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar)
154 HildonVolumebarPrivate *priv;
156 priv = HILDON_VOLUMEBAR_GET_PRIVATE (hvolumebar);
159 HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
160 (GTK_ORIENTATION_HORIZONTAL));
162 gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (hvolumebar));
163 gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (hvolumebar));
165 gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
168 g_signal_connect_swapped (G_OBJECT (priv->volumebar), "value-changed",
169 G_CALLBACK(hildon_volumebar_level_change),
172 g_signal_connect_swapped (priv->tbutton, "toggled",
173 G_CALLBACK (hildon_volumebar_mute_toggled), hvolumebar);
175 gtk_widget_show (GTK_WIDGET (priv->volumebar));
179 * hildon_hvolumebar_new:
181 * Creates a new #HildonHVolumebar widget.
183 * Returns: a new #HildonHVolumebar
186 hildon_hvolumebar_new (void)
188 return GTK_WIDGET (g_object_new(HILDON_TYPE_HVOLUMEBAR, NULL));
192 hildon_hvolumebar_map (GtkWidget* widget)
194 HildonVolumebarPrivate *priv;
197 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
200 parent = gtk_widget_get_ancestor (GTK_WIDGET (widget), GTK_TYPE_TOOLBAR);
202 /* Check if the volumebar is in a toolbar */
204 priv->is_toolbar = TRUE;
206 GTK_WIDGET_CLASS (parent_class)->map (widget);
210 hildon_hvolumebar_expose (GtkWidget * widget,
211 GdkEventExpose * event)
213 HildonVolumebarPrivate *priv;
215 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
218 if (GTK_WIDGET_DRAWABLE (widget)) {
219 /* Paint background */
220 gtk_paint_box (widget->style, widget->window,
221 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
222 NULL, widget, "background",
223 widget->allocation.x,
224 widget->allocation.y,
225 widget->allocation.width,
226 widget->allocation.height);
228 /* The contents of the widget can paint themselves */
229 /* FIXME Not sure if this is even needed here */
230 (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
237 hildon_hvolumebar_size_request (GtkWidget * widget,
238 GtkRequisition * requisition)
240 HildonVolumebarPrivate *priv;
242 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
245 /* Volumebar has different dimensions in toolbar */
246 requisition->width = (priv->is_toolbar
247 ? TOOL_MINIMUM_BAR_WIDTH
248 : MINIMUM_BAR_WIDTH);
250 requisition->height = (priv->is_toolbar
251 ? TOOL_DEFAULT_BAR_HEIGHT
252 : DEFAULT_BAR_HEIGHT);
256 hildon_hvolumebar_size_allocate (GtkWidget * widget,
257 GtkAllocation * allocation)
259 HildonVolumebarPrivate *priv;
260 GtkAllocation button_allocation, range_allocation;
262 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
265 button_allocation.x = 0;
266 button_allocation.width = 0;
268 /* Center the widget vertically */
269 if (priv->is_toolbar && allocation->height > TOOL_DEFAULT_BAR_HEIGHT) {
270 allocation->y += (allocation->height - TOOL_DEFAULT_BAR_HEIGHT) / 2;
271 allocation->height = TOOL_DEFAULT_BAR_HEIGHT;
274 if (!priv->is_toolbar && allocation->height > DEFAULT_BAR_HEIGHT) {
275 allocation->y += (allocation->height - DEFAULT_BAR_HEIGHT) / 2;
276 allocation->height = DEFAULT_BAR_HEIGHT;
279 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
281 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
283 /* Allocate space for the mute button */
284 if (priv->is_toolbar) {
285 button_allocation.x = allocation->x;
286 button_allocation.y = allocation->y + TOOL_VERTICAL_MUTE_GAP;
287 button_allocation.width = TOOL_DEFAULT_TBUTTON_WIDTH;
288 button_allocation.height = TOOL_DEFAULT_TBUTTON_HEIGHT;
290 button_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
291 button_allocation.y = allocation->y + VERTICAL_MUTE_GAP;
292 button_allocation.width = DEFAULT_TBUTTON_WIDTH;
293 button_allocation.height = DEFAULT_TBUTTON_HEIGHT;
295 gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
298 if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
300 /* Allocate space for the slider */
301 range_allocation.y = allocation->y;
303 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
305 /* Leave room for the mute button */
306 range_allocation.x = button_allocation.x
307 + button_allocation.width
308 + HORIZONTAL_MUTE_GAP;
310 if (priv->is_toolbar)
312 /* In toolbar with mute button */
313 range_allocation.width = MAX(0,
315 - 2 * TOOL_DEFAULT_ENDING_SIZE
316 - TOOL_DEFAULT_TBUTTON_WIDTH
317 - HORIZONTAL_MUTE_GAP);
319 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
325 /* Standalone with mute button */
326 range_allocation.width = MAX(0,
328 - 2 * DEFAULT_ENDING_SIZE
329 - DEFAULT_TBUTTON_WIDTH
330 - HORIZONTAL_MUTE_GAP);
332 range_allocation.height = DEFAULT_BAR_HEIGHT;
339 if (priv->is_toolbar)
341 /* In toolbar without mute button */
342 range_allocation.x = allocation->x;
344 range_allocation.width = MAX(0,
346 - 2 * TOOL_DEFAULT_ENDING_SIZE );
348 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
354 /* Standalone without mute button */
355 range_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
357 range_allocation.width = MAX(0,
359 - 2 * DEFAULT_ENDING_SIZE );
361 range_allocation.height = DEFAULT_BAR_HEIGHT;
365 gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),