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 #include "hildon-hvolumebar.h"
45 #include "hildon-volumebar.h"
46 #include "hildon-volumebar-range.h"
47 #include "hildon-volumebar-private.h"
48 #include <gtk/gtktoolbar.h>
50 /* Defines for normal version of HVolumebar */
53 #define DEFAULT_TBUTTON_WIDTH 26
55 #define DEFAULT_TBUTTON_HEIGHT 26
58 #define MINIMUM_BAR_WIDTH 147
60 #define DEFAULT_BAR_HEIGHT 58
62 #define DEFAULT_ENDING_SIZE 20
64 /* Gap to leave for mute button */
65 #define VERTICAL_MUTE_GAP 16
67 #define HORIZONTAL_MUTE_GAP 6
69 /* Sizes inside a toolbar */
72 #define TOOL_DEFAULT_TBUTTON_WIDTH 26
74 #define TOOL_DEFAULT_TBUTTON_HEIGHT 26
78 #define TOOL_MINIMUM_BAR_WIDTH 121
80 #define TOOL_DEFAULT_BAR_HEIGHT 40
82 #define TOOL_DEFAULT_ENDING_SIZE 0
84 #define TOOL_VERTICAL_MUTE_GAP \
85 ((TOOL_DEFAULT_BAR_HEIGHT - TOOL_DEFAULT_TBUTTON_HEIGHT) / 2)
87 static HildonVolumebarClass* parent_class;
90 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass);
93 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar);
96 hildon_hvolumebar_expose (GtkWidget *widget,
97 GdkEventExpose *event);
99 hildon_hvolumebar_size_request (GtkWidget *widget,
100 GtkRequisition *requisition);
102 hildon_hvolumebar_size_allocate (GtkWidget *widget,
103 GtkAllocation *allocation);
105 hildon_hvolumebar_map (GtkWidget *widget);
108 * hildon_hvolumebar_get_type:
110 * Returns GType for HildonHVolumebar.
112 * Returns: HildonHVolumebar type
115 hildon_hvolumebar_get_type (void)
117 static GType type = 0;
120 static const GTypeInfo info = {
121 sizeof (HildonHVolumebarClass),
122 NULL, /* base_init */
123 NULL, /* base_finalize */
124 (GClassInitFunc) hildon_hvolumebar_class_init, /* class_init */
125 NULL, /* class_finalize */
126 NULL, /* class_data */
127 sizeof (HildonHVolumebar),
129 (GInstanceInitFunc) hildon_hvolumebar_init,
131 type = g_type_register_static (HILDON_TYPE_VOLUMEBAR,
132 "HildonHVolumebar", &info, 0);
138 hildon_hvolumebar_class_init (HildonHVolumebarClass *klass)
140 GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS (klass);
142 parent_class = g_type_class_peek_parent (klass);
144 volumebar_class->size_request = hildon_hvolumebar_size_request;
145 volumebar_class->size_allocate = hildon_hvolumebar_size_allocate;
146 volumebar_class->map = hildon_hvolumebar_map;
147 volumebar_class->expose_event = hildon_hvolumebar_expose;
151 hildon_hvolumebar_init (HildonHVolumebar *hvolumebar)
153 HildonVolumebarPrivate *priv;
155 priv = HILDON_VOLUMEBAR_GET_PRIVATE (hvolumebar);
158 HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
159 (GTK_ORIENTATION_HORIZONTAL));
161 gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (hvolumebar));
162 gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (hvolumebar));
164 gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
167 g_signal_connect_swapped (G_OBJECT (priv->volumebar), "value-changed",
168 G_CALLBACK(hildon_volumebar_level_change),
171 g_signal_connect_swapped (priv->tbutton, "toggled",
172 G_CALLBACK (hildon_volumebar_mute_toggled), hvolumebar);
174 gtk_widget_show (GTK_WIDGET (priv->volumebar));
178 * hildon_hvolumebar_new:
180 * Creates a new #HildonHVolumebar widget.
182 * Returns: a new #HildonHVolumebar
185 hildon_hvolumebar_new (void)
187 return GTK_WIDGET (g_object_new(HILDON_TYPE_HVOLUMEBAR, NULL));
191 hildon_hvolumebar_map (GtkWidget* widget)
193 HildonVolumebarPrivate *priv;
196 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
199 parent = gtk_widget_get_ancestor (GTK_WIDGET (widget), GTK_TYPE_TOOLBAR);
201 /* Check if the volumebar is in a toolbar */
203 priv->is_toolbar = TRUE;
205 GTK_WIDGET_CLASS (parent_class)->map (widget);
209 hildon_hvolumebar_expose (GtkWidget * widget,
210 GdkEventExpose * event)
212 HildonVolumebarPrivate *priv;
214 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
217 if (GTK_WIDGET_DRAWABLE (widget)) {
218 /* Paint background */
219 gtk_paint_box (widget->style, widget->window,
220 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
221 NULL, widget, "background",
222 widget->allocation.x,
223 widget->allocation.y,
224 widget->allocation.width,
225 widget->allocation.height);
227 /* The contents of the widget can paint themselves */
228 /* FIXME Not sure if this is even needed here */
229 (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
236 hildon_hvolumebar_size_request (GtkWidget * widget,
237 GtkRequisition * requisition)
239 HildonVolumebarPrivate *priv;
241 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
244 /* Volumebar has different dimensions in toolbar */
245 requisition->width = (priv->is_toolbar
246 ? TOOL_MINIMUM_BAR_WIDTH
247 : MINIMUM_BAR_WIDTH);
249 requisition->height = (priv->is_toolbar
250 ? TOOL_DEFAULT_BAR_HEIGHT
251 : DEFAULT_BAR_HEIGHT);
255 hildon_hvolumebar_size_allocate (GtkWidget * widget,
256 GtkAllocation * allocation)
258 HildonVolumebarPrivate *priv;
259 GtkAllocation button_allocation, range_allocation;
261 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
264 button_allocation.x = 0;
265 button_allocation.width = 0;
267 /* Center the widget vertically */
268 if (priv->is_toolbar && allocation->height > TOOL_DEFAULT_BAR_HEIGHT) {
269 allocation->y += (allocation->height - TOOL_DEFAULT_BAR_HEIGHT) / 2;
270 allocation->height = TOOL_DEFAULT_BAR_HEIGHT;
273 if (!priv->is_toolbar && allocation->height > DEFAULT_BAR_HEIGHT) {
274 allocation->y += (allocation->height - DEFAULT_BAR_HEIGHT) / 2;
275 allocation->height = DEFAULT_BAR_HEIGHT;
278 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
280 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
282 /* Allocate space for the mute button */
283 if (priv->is_toolbar) {
284 button_allocation.x = allocation->x;
285 button_allocation.y = allocation->y + TOOL_VERTICAL_MUTE_GAP;
286 button_allocation.width = TOOL_DEFAULT_TBUTTON_WIDTH;
287 button_allocation.height = TOOL_DEFAULT_TBUTTON_HEIGHT;
289 button_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
290 button_allocation.y = allocation->y + VERTICAL_MUTE_GAP;
291 button_allocation.width = DEFAULT_TBUTTON_WIDTH;
292 button_allocation.height = DEFAULT_TBUTTON_HEIGHT;
294 gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
297 if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
299 /* Allocate space for the slider */
300 range_allocation.y = allocation->y;
302 if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
304 /* Leave room for the mute button */
305 range_allocation.x = button_allocation.x
306 + button_allocation.width
307 + HORIZONTAL_MUTE_GAP;
309 if (priv->is_toolbar)
311 /* In toolbar with mute button */
312 range_allocation.width = MAX(0,
314 - 2 * TOOL_DEFAULT_ENDING_SIZE
315 - TOOL_DEFAULT_TBUTTON_WIDTH
316 - HORIZONTAL_MUTE_GAP);
318 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
324 /* Standalone with mute button */
325 range_allocation.width = MAX(0,
327 - 2 * DEFAULT_ENDING_SIZE
328 - DEFAULT_TBUTTON_WIDTH
329 - HORIZONTAL_MUTE_GAP);
331 range_allocation.height = DEFAULT_BAR_HEIGHT;
338 if (priv->is_toolbar)
340 /* In toolbar without mute button */
341 range_allocation.x = allocation->x;
343 range_allocation.width = MAX(0,
345 - 2 * TOOL_DEFAULT_ENDING_SIZE );
347 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
353 /* Standalone without mute button */
354 range_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
356 range_allocation.width = MAX(0,
358 - 2 * DEFAULT_ENDING_SIZE );
360 range_allocation.height = DEFAULT_BAR_HEIGHT;
364 gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),