2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@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 * #HildonHVolumeBar has been deprecated since Hildon 2.2
37 * See <link linkend="hildon-migrating-volume-bar">Migrating Volume Bars</link>
38 * section to know how to migrate this deprecated widget.
44 * GtkWidget *volbar = hildon_hvolumebar_new ();
45 * g_signal_connect (G_OBJECT(volbar), "mute_toggled", G_CALLBACK(mute_toggle), NULL);
46 * g_signal_connect (G_OBJECT(volbar), "level_changed", G_CALLBACK(level_change), NULL);
52 #undef HILDON_DISABLE_DEPRECATED
54 #include "hildon-hvolumebar.h"
55 #include "hildon-volumebar.h"
56 #include "hildon-volumebar-range.h"
57 #include "hildon-volumebar-private.h"
59 /* Defines for normal version of HVolumebar */
62 #define DEFAULT_TBUTTON_WIDTH 60
64 #define DEFAULT_TBUTTON_HEIGHT 60
67 #define MINIMUM_BAR_WIDTH 147
69 #define DEFAULT_BAR_HEIGHT 60
71 #define DEFAULT_ENDING_SIZE 20
73 /* Gap to leave for mute button */
74 #define VERTICAL_MUTE_GAP 0
76 #define HORIZONTAL_MUTE_GAP 0
78 /* Sizes inside a toolbar */
81 #define TOOL_DEFAULT_TBUTTON_WIDTH 26
83 #define TOOL_DEFAULT_TBUTTON_HEIGHT 26
87 #define TOOL_MINIMUM_BAR_WIDTH 121
89 #define TOOL_DEFAULT_BAR_HEIGHT 40
91 #define TOOL_DEFAULT_ENDING_SIZE 0
93 #define TOOL_VERTICAL_MUTE_GAP \
94 ((TOOL_DEFAULT_BAR_HEIGHT - TOOL_DEFAULT_TBUTTON_HEIGHT) / 2)
96 static HildonVolumebarClass* parent_class;
99 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass);
102 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar);
105 hildon_hvolumebar_expose (GtkWidget *widget,
106 GdkEventExpose *event);
108 hildon_hvolumebar_size_request (GtkWidget *widget,
109 GtkRequisition *requisition);
111 hildon_hvolumebar_size_allocate (GtkWidget *widget,
112 GtkAllocation *allocation);
114 hildon_hvolumebar_map (GtkWidget *widget);
117 * hildon_hvolumebar_get_type:
119 * Returns GType for HildonHVolumebar.
121 * Returns: HildonHVolumebar type
124 hildon_hvolumebar_get_type (void)
126 static GType type = 0;
129 static const GTypeInfo info = {
130 sizeof (HildonHVolumebarClass),
131 NULL, /* base_init */
132 NULL, /* base_finalize */
133 (GClassInitFunc) hildon_hvolumebar_class_init, /* class_init */
134 NULL, /* class_finalize */
135 NULL, /* class_data */
136 sizeof (HildonHVolumebar),
138 (GInstanceInitFunc) hildon_hvolumebar_init,
140 type = g_type_register_static (HILDON_TYPE_VOLUMEBAR,
141 "HildonHVolumebar", &info, 0);
147 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass)
149 GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS (klass);
151 parent_class = g_type_class_peek_parent (klass);
153 volumebar_class->size_request = hildon_hvolumebar_size_request;
154 volumebar_class->size_allocate = hildon_hvolumebar_size_allocate;
155 volumebar_class->map = hildon_hvolumebar_map;
156 volumebar_class->expose_event = hildon_hvolumebar_expose;
160 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar)
162 HildonVolumebarPrivate *priv;
164 priv = HILDON_VOLUMEBAR_GET_PRIVATE (hvolumebar);
167 HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
168 (GTK_ORIENTATION_HORIZONTAL));
170 gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (hvolumebar));
171 gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (hvolumebar));
173 gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
176 g_signal_connect_swapped (G_OBJECT (priv->volumebar), "value-changed",
177 G_CALLBACK(hildon_volumebar_level_change),
180 g_signal_connect_swapped (priv->tbutton, "toggled",
181 G_CALLBACK (hildon_volumebar_mute_toggled), hvolumebar);
183 gtk_widget_show (GTK_WIDGET (priv->volumebar));
187 * hildon_hvolumebar_new:
189 * Creates a new #HildonHVolumebar widget.
191 * Returns: a new #HildonHVolumebar
194 hildon_hvolumebar_new (void)
196 return GTK_WIDGET (g_object_new(HILDON_TYPE_HVOLUMEBAR, NULL));
200 hildon_hvolumebar_map (GtkWidget* widget)
202 HildonVolumebarPrivate *priv;
205 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
208 parent = gtk_widget_get_ancestor (GTK_WIDGET (widget), GTK_TYPE_TOOLBAR);
210 /* Check if the volumebar is in a toolbar */
212 priv->is_toolbar = TRUE;
214 GTK_WIDGET_CLASS (parent_class)->map (widget);
218 hildon_hvolumebar_expose (GtkWidget * widget,
219 GdkEventExpose * event)
221 HildonVolumebarPrivate *priv;
223 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
226 if (GTK_WIDGET_DRAWABLE (widget)) {
227 /* Paint background */
228 gtk_paint_box (widget->style, widget->window,
229 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
230 NULL, widget, "background",
231 widget->allocation.x,
232 widget->allocation.y,
233 widget->allocation.width,
234 widget->allocation.height);
236 /* The contents of the widget can paint themselves */
237 /* FIXME Not sure if this is even needed here */
238 (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
245 hildon_hvolumebar_size_request (GtkWidget * widget,
246 GtkRequisition * requisition)
248 HildonVolumebarPrivate *priv;
250 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
253 /* Volumebar has different dimensions in toolbar */
254 requisition->width = (priv->is_toolbar
255 ? TOOL_MINIMUM_BAR_WIDTH
256 : MINIMUM_BAR_WIDTH);
258 requisition->height = (priv->is_toolbar
259 ? TOOL_DEFAULT_BAR_HEIGHT
260 : DEFAULT_BAR_HEIGHT);
264 hildon_hvolumebar_size_allocate (GtkWidget * widget,
265 GtkAllocation * allocation)
267 HildonVolumebarPrivate *priv;
268 GtkAllocation button_allocation, range_allocation;
270 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
273 button_allocation.x = 0;
274 button_allocation.width = 0;
276 /* Center the widget vertically */
277 if (priv->is_toolbar && allocation->height > TOOL_DEFAULT_BAR_HEIGHT) {
278 allocation->y += (allocation->height - TOOL_DEFAULT_BAR_HEIGHT) / 2;
279 allocation->height = TOOL_DEFAULT_BAR_HEIGHT;
282 if (!priv->is_toolbar && allocation->height > DEFAULT_BAR_HEIGHT) {
283 allocation->y += (allocation->height - DEFAULT_BAR_HEIGHT) / 2;
284 allocation->height = DEFAULT_BAR_HEIGHT;
287 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
289 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
291 /* Allocate space for the mute button */
292 if (priv->is_toolbar) {
293 button_allocation.x = allocation->x;
294 button_allocation.y = allocation->y + TOOL_VERTICAL_MUTE_GAP;
295 button_allocation.width = TOOL_DEFAULT_TBUTTON_WIDTH;
296 button_allocation.height = TOOL_DEFAULT_TBUTTON_HEIGHT;
298 button_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
299 button_allocation.y = allocation->y + VERTICAL_MUTE_GAP;
300 button_allocation.width = DEFAULT_TBUTTON_WIDTH;
301 button_allocation.height = DEFAULT_TBUTTON_HEIGHT;
303 gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
306 if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
308 /* Allocate space for the slider */
309 range_allocation.y = allocation->y;
311 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
313 /* Leave room for the mute button */
314 range_allocation.x = button_allocation.x
315 + button_allocation.width
316 + HORIZONTAL_MUTE_GAP;
318 if (priv->is_toolbar)
320 /* In toolbar with mute button */
321 range_allocation.width = MAX(0,
323 - 2 * TOOL_DEFAULT_ENDING_SIZE
324 - TOOL_DEFAULT_TBUTTON_WIDTH
325 - HORIZONTAL_MUTE_GAP);
327 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
333 /* Standalone with mute button */
334 range_allocation.width = MAX(0,
336 - 2 * DEFAULT_ENDING_SIZE
337 - DEFAULT_TBUTTON_WIDTH
338 - HORIZONTAL_MUTE_GAP);
340 range_allocation.height = DEFAULT_BAR_HEIGHT;
347 if (priv->is_toolbar)
349 /* In toolbar without mute button */
350 range_allocation.x = allocation->x;
352 range_allocation.width = MAX(0,
354 - 2 * TOOL_DEFAULT_ENDING_SIZE );
356 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
362 /* Standalone without mute button */
363 range_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
365 range_allocation.width = MAX(0,
367 - 2 * DEFAULT_ENDING_SIZE );
369 range_allocation.height = DEFAULT_BAR_HEIGHT;
373 gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),