2 * This file is part of hildon-libs
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
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-vvolumebar
27 * @short_description: A widget that displays a vertical volume bar
28 * @see_also: #HildonVolumebar, #HildonHVolumebar
30 * #HildonVVolumebar is a subclass of #HildonVolumebar. It displays a
31 * vertical volume bar that allows increasing or decreasing volume
32 * within a predefined range, and muting when users click the mute icon.
36 #include "hildon-vvolumebar.h"
38 #include "hildon-volumebar-range.h"
39 #include "hildon-volumebar-private.h"
42 #define DEFAULT_BAR_WIDTH 58
43 #define MINIMUM_BAR_HEIGHT 165
45 #define DEFAULT_VERTICAL_TBUTTON_WIDTH 26
46 #define DEFAULT_VERTICAL_TBUTTON_HEIGHT 26
47 #define DEFAULT_ENDING_SIZE 20
48 /* Gap to leave for mute button */
49 #define HORIZONTAL_MUTE_GAP 16
50 #define VERTICAL_MUTE_GAP 6
52 static HildonVolumebarClass *parent_class;
53 static void hildon_vvolumebar_class_init(HildonVVolumebarClass * klass);
54 static void hildon_vvolumebar_init(HildonVVolumebar * vvolumebar);
55 static gboolean hildon_vvolumebar_expose(GtkWidget * widget,
56 GdkEventExpose * event);
57 static void hildon_vvolumebar_size_request(GtkWidget * widget,
58 GtkRequisition * requisition);
59 static void hildon_vvolumebar_size_allocate(GtkWidget * widget,
60 GtkAllocation * allocation);
62 GType hildon_vvolumebar_get_type(void)
64 static GType type = 0;
67 static const GTypeInfo info = {
68 sizeof(HildonVVolumebarClass),
70 NULL, /* base_finalize */
71 (GClassInitFunc) hildon_vvolumebar_class_init, /* class_init */
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof(HildonVVolumebar),
76 (GInstanceInitFunc) hildon_vvolumebar_init,
79 g_type_register_static(HILDON_TYPE_VOLUMEBAR,
80 "HildonVVolumebar", &info, 0);
85 static void hildon_vvolumebar_class_init(HildonVVolumebarClass * klass)
87 GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS(klass);
89 parent_class = g_type_class_peek_parent(klass);
91 volumebar_class->size_request = hildon_vvolumebar_size_request;
92 volumebar_class->size_allocate = hildon_vvolumebar_size_allocate;
93 volumebar_class->expose_event = hildon_vvolumebar_expose;
96 static void hildon_vvolumebar_init(HildonVVolumebar * vvolumebar)
98 HildonVolumebarPrivate *priv;
100 priv = HILDON_VOLUMEBAR_GET_PRIVATE(vvolumebar);
103 HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
104 (GTK_ORIENTATION_VERTICAL));
106 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(vvolumebar), GTK_CAN_FOCUS);
108 gtk_widget_set_parent(GTK_WIDGET(priv->tbutton), GTK_WIDGET(vvolumebar));
109 gtk_widget_set_parent(GTK_WIDGET(priv->volumebar), GTK_WIDGET(vvolumebar));
111 gtk_scale_set_draw_value(GTK_SCALE(priv->volumebar), FALSE);
114 g_signal_connect_swapped(G_OBJECT(priv->volumebar), "value-changed",
115 G_CALLBACK(hildon_volumebar_level_change),
117 g_signal_connect_swapped(priv->tbutton, "toggled",
118 G_CALLBACK(_hildon_volumebar_mute_toggled), vvolumebar);
120 gtk_widget_show(GTK_WIDGET(priv->volumebar));
124 * hildon_vvolumebar_new:
126 * Creates a new #HildonVVolumebar widget.
128 * Returns: a new #HildonVVolumebar
130 GtkWidget *hildon_vvolumebar_new(void)
132 return GTK_WIDGET(g_object_new(HILDON_TYPE_VVOLUMEBAR, NULL));
135 static gboolean hildon_vvolumebar_expose(GtkWidget * widget,
136 GdkEventExpose * event)
138 HildonVolumebarPrivate *priv;
140 g_assert(HILDON_IS_VVOLUMEBAR(widget));
142 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
144 if (GTK_WIDGET_DRAWABLE(widget)) {
145 /* Paint background */
146 gtk_paint_box(widget->style, widget->window,
147 GTK_WIDGET_STATE(priv->volumebar), GTK_SHADOW_OUT,
148 NULL, widget, "background",
149 widget->allocation.x,
150 widget->allocation.y,
151 widget->allocation.width,
152 widget->allocation.height);
154 /* The contents of the widget can paint themselves */
155 (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
162 hildon_vvolumebar_size_request(GtkWidget * widget,
163 GtkRequisition * requisition)
165 g_assert(HILDON_IS_VVOLUMEBAR(widget));
167 requisition->height = MINIMUM_BAR_HEIGHT;
168 requisition->width = DEFAULT_BAR_WIDTH;
172 hildon_vvolumebar_size_allocate(GtkWidget * widget,
173 GtkAllocation * allocation)
175 HildonVolumebarPrivate *priv;
177 GtkAllocation range_allocation, button_allocation;
179 g_assert(HILDON_IS_VVOLUMEBAR(widget));
181 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
183 /* Center the widget horizontally */
184 if (allocation->width > DEFAULT_BAR_WIDTH) {
186 (allocation->width - DEFAULT_BAR_WIDTH) / 2;
187 allocation->width = DEFAULT_BAR_WIDTH;
190 GTK_WIDGET_CLASS(parent_class)->size_allocate(widget, allocation);
192 if (priv->volumebar && GTK_WIDGET_VISIBLE(priv->volumebar)) {
193 /* Allocate space for the slider */
194 range_allocation.x = allocation->x;
195 range_allocation.y = allocation->y + DEFAULT_ENDING_SIZE;
197 range_allocation.width = DEFAULT_BAR_WIDTH;
199 if (priv->tbutton && GTK_WIDGET_VISIBLE(priv->tbutton))
201 /* Leave room for the mute button */
202 range_allocation.height = MAX(0,
204 - 2 * DEFAULT_ENDING_SIZE
205 - DEFAULT_VERTICAL_TBUTTON_HEIGHT
206 - VERTICAL_MUTE_GAP);
211 range_allocation.height = MAX(0,
213 - 2 * DEFAULT_ENDING_SIZE);
216 gtk_widget_size_allocate(GTK_WIDGET(priv->volumebar),
220 if (priv->tbutton && GTK_WIDGET_VISIBLE(priv->tbutton)) {
221 /* Allocate space for the mute button */
222 button_allocation.x = allocation->x + HORIZONTAL_MUTE_GAP;
223 button_allocation.y = allocation->y + allocation->height -
224 VERTICAL_MUTE_GAP - 2 * DEFAULT_ENDING_SIZE;
225 button_allocation.width = DEFAULT_VERTICAL_TBUTTON_WIDTH;
226 button_allocation.height = DEFAULT_VERTICAL_TBUTTON_HEIGHT;
227 gtk_widget_size_allocate(GTK_WIDGET(priv->tbutton),