2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@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; either 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 * @file hildon-vvolumebar.c
28 * This file contains the implementation of HildonVVolumebar.
29 * This widget is a subclass of HildonVolumebar and implements
30 * horizontal version of Volumebar.
38 #include "hildon-vvolumebar.h"
39 #include "hildon-volumebar.h"
40 #include "hildon-volumebar-range.h"
41 #include "hildon-volumebar-private.h"
44 #define DEFAULT_BAR_WIDTH 58
45 #define MINIMUM_BAR_HEIGHT 165
47 #define DEFAULT_VERTICAL_TBUTTON_WIDTH 26
48 #define DEFAULT_VERTICAL_TBUTTON_HEIGHT 26
49 #define DEFAULT_ENDING_SIZE 20
50 /* Gap to leave for mute button */
51 #define HORIZONTAL_MUTE_GAP 16
52 #define VERTICAL_MUTE_GAP 6
54 static HildonVolumebarClass *parent_class;
55 static void vvolumebar_class_init(HildonVVolumebarClass * klass);
56 static void vvolumebar_init(HildonVVolumebar * vvolumebar);
57 static void hildon_vvolumebar_mute(GtkWidget * widget,
58 HildonVVolumebar * self);
59 static gboolean hildon_vvolumebar_expose(GtkWidget * widget,
60 GdkEventExpose * event);
61 static void hildon_vvolumebar_size_request(GtkWidget * widget,
62 GtkRequisition * requisition);
63 static void hildon_vvolumebar_size_allocate(GtkWidget * widget,
64 GtkAllocation * allocation);
66 GType hildon_vvolumebar_get_type(void)
68 static GType type = 0;
71 static const GTypeInfo info = {
72 sizeof(HildonVVolumebarClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) vvolumebar_class_init, /* class_init */
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof(HildonVVolumebar),
80 (GInstanceInitFunc) vvolumebar_init,
83 g_type_register_static(HILDON_TYPE_VOLUMEBAR,
84 "HildonVVolumebar", &info, 0);
89 static void vvolumebar_class_init(HildonVVolumebarClass * klass)
91 GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS(klass);
93 parent_class = g_type_class_peek_parent(klass);
95 volumebar_class->size_request = hildon_vvolumebar_size_request;
96 volumebar_class->size_allocate = hildon_vvolumebar_size_allocate;
97 volumebar_class->expose_event = hildon_vvolumebar_expose;
100 static void vvolumebar_init(HildonVVolumebar * vvolumebar)
102 HildonVolumebarPrivate *priv;
104 priv = HILDON_VOLUMEBAR_GET_PRIVATE(vvolumebar);
106 priv->orientation = GTK_ORIENTATION_VERTICAL;
108 HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
109 (GTK_ORIENTATION_VERTICAL));
111 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(vvolumebar), GTK_CAN_FOCUS);
113 gtk_widget_set_parent(GTK_WIDGET(priv->tbutton), GTK_WIDGET(vvolumebar));
114 gtk_widget_set_parent(GTK_WIDGET(priv->volumebar), GTK_WIDGET(vvolumebar));
116 gtk_scale_set_draw_value(GTK_SCALE(priv->volumebar), FALSE);
119 g_signal_connect_swapped(G_OBJECT(priv->volumebar), "value-changed",
120 G_CALLBACK(hildon_volumebar_level_change),
122 g_signal_connect(G_OBJECT(priv->tbutton), "toggled",
123 G_CALLBACK(hildon_vvolumebar_mute), vvolumebar);
125 gtk_widget_show(GTK_WIDGET(priv->volumebar));
129 * hildon_vvolumebar_new:
131 * Creates a new #HildonVVolumebar widget.
133 * Returns: a new #HildonVVolumebar.
135 GtkWidget *hildon_vvolumebar_new(void)
137 return GTK_WIDGET(g_object_new(HILDON_TYPE_VVOLUMEBAR, NULL));
140 static gboolean hildon_vvolumebar_expose(GtkWidget * widget,
141 GdkEventExpose * event)
143 HildonVolumebarPrivate *priv;
145 g_return_val_if_fail(HILDON_IS_VVOLUMEBAR(widget), FALSE);
147 priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
149 if (GTK_WIDGET_DRAWABLE(widget)) {
150 /* Paint background */
151 gtk_paint_box(widget->style, widget->window,
152 GTK_WIDGET_STATE(priv->volumebar), GTK_SHADOW_OUT,
153 NULL, widget, "background",
154 widget->allocation.x,
155 widget->allocation.y,
156 widget->allocation.width,
157 widget->allocation.height);
159 /* The contents of the widget can paint themselves */
160 (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
167 hildon_vvolumebar_size_request(GtkWidget * widget,
168 GtkRequisition * requisition)
170 g_return_if_fail(HILDON_IS_VVOLUMEBAR(widget));
172 requisition->height = MINIMUM_BAR_HEIGHT;
173 requisition->width = DEFAULT_BAR_WIDTH;
177 hildon_vvolumebar_size_allocate(GtkWidget * widget,
178 GtkAllocation * allocation)
180 HildonVolumebarPrivate *priv;
182 GtkAllocation range_allocation, button_allocation;
184 g_return_if_fail(HILDON_IS_VVOLUMEBAR(widget));
186 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
188 /* Center the widget horizontally */
189 if (allocation->width > DEFAULT_BAR_WIDTH) {
191 (allocation->width - DEFAULT_BAR_WIDTH) / 2;
192 allocation->width = DEFAULT_BAR_WIDTH;
195 widget->allocation = *allocation;
197 if (priv->volumebar && GTK_WIDGET_VISIBLE(priv->volumebar)) {
198 /* Allocate space for the slider */
199 range_allocation.x = allocation->x;
200 range_allocation.y = allocation->y + DEFAULT_ENDING_SIZE;
202 range_allocation.width = DEFAULT_BAR_WIDTH;
204 if (priv->tbutton && GTK_WIDGET_VISIBLE(priv->tbutton))
206 /* Leave room for the mute button */
207 range_allocation.height = MAX(0,
209 - 2 * DEFAULT_ENDING_SIZE
210 - DEFAULT_VERTICAL_TBUTTON_HEIGHT
211 - VERTICAL_MUTE_GAP);
216 range_allocation.height = MAX(0,
218 - 2 * DEFAULT_ENDING_SIZE);
221 gtk_widget_size_allocate(GTK_WIDGET(priv->volumebar),
225 if (priv->tbutton && GTK_WIDGET_VISIBLE(priv->tbutton)) {
226 /* Allocate space for the mute button */
227 button_allocation.x = allocation->x + HORIZONTAL_MUTE_GAP;
228 button_allocation.y = allocation->y + allocation->height -
229 VERTICAL_MUTE_GAP - 2 * DEFAULT_ENDING_SIZE;
230 button_allocation.width = DEFAULT_VERTICAL_TBUTTON_WIDTH;
231 button_allocation.height = DEFAULT_VERTICAL_TBUTTON_HEIGHT;
232 gtk_widget_size_allocate(GTK_WIDGET(priv->tbutton),
238 hildon_vvolumebar_mute(GtkWidget * widget, HildonVVolumebar * self)
240 g_signal_emit_by_name(GTK_WIDGET(self), "mute_toggled");