2009-03-12 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-vvolumebar.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-vvolumebar
27  * @short_description: A widget that displays a vertical volume bar.
28  * @see_also: #HildonVolumebar, #HildonHVolumebar
29  *
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.
33  * 
34  * Here is an example that creates a vertical volume bar and connects
35  * both its signals.
36  * <example>
37  * <title>HildonVVolumebar example</title>
38  * <programlisting>
39  * GtkWidget *volbar = hildon_vvolumebar_new ();
40  * g_signal_connect (G_OBJECT (volbar), "mute_toggled", G_CALLBACK (mute_toggle), NULL);
41  * g_signal_connect (G_OBJECT (volbar), "level_changed", G_CALLBACK (level_change), NULL);
42  * </programlisting>
43  * </example>
44  */
45
46 #undef                                          HILDON_DISABLE_DEPRECATED
47
48 #ifdef                                          HAVE_CONFIG_H
49 #include                                        <config.h>
50 #endif
51
52 #include                                        "hildon-vvolumebar.h"
53 #include                                        "hildon-volumebar-range.h"
54 #include                                        "hildon-volumebar-private.h"
55
56 /* Volume bar */
57 #define                                         DEFAULT_BAR_WIDTH 60
58
59 #define                                         MINIMUM_BAR_HEIGHT 165
60
61 /* Toggle button */
62
63 #define                                         DEFAULT_VERTICAL_TBUTTON_WIDTH 60
64
65 #define                                         DEFAULT_VERTICAL_TBUTTON_HEIGHT 60
66
67 #define                                         DEFAULT_ENDING_SIZE 0
68
69 /* Gap to leave for mute button */
70
71 #define                                         HORIZONTAL_MUTE_GAP 0
72
73 #define                                         VERTICAL_MUTE_GAP 0
74
75 static HildonVolumebarClass*                    parent_class;
76
77 static void 
78 hildon_vvolumebar_class_init                    (HildonVVolumebarClass * klass);
79
80 static void 
81 hildon_vvolumebar_init                          (HildonVVolumebar * vvolumebar);
82
83 static gboolean
84 hildon_vvolumebar_expose                        (GtkWidget * widget,
85                                                  GdkEventExpose * event);
86
87 static void 
88 hildon_vvolumebar_size_request                  (GtkWidget * widget,
89                                                  GtkRequisition * requisition);
90
91 static void
92 hildon_vvolumebar_size_allocate                 (GtkWidget * widget,
93                                                  GtkAllocation * allocation);
94
95 /**
96  * hildon_vvolumebar_get_type:
97  *
98  * Initializes and returns the type of a hildon vvolumebar.
99  *
100  * Returns: GType of #HildonVVolumebar
101  */
102 GType G_GNUC_CONST
103 hildon_vvolumebar_get_type                      (void)
104 {
105     static GType type = 0;
106
107     if (!type) {
108         static const GTypeInfo info = {
109             sizeof (HildonVVolumebarClass),
110             NULL,       /* base_init */
111             NULL,       /* base_finalize */
112             (GClassInitFunc) hildon_vvolumebar_class_init,     /* class_init */
113             NULL,       /* class_finalize */
114             NULL,       /* class_data */
115             sizeof (HildonVVolumebar),
116             0,
117             (GInstanceInitFunc) hildon_vvolumebar_init,
118         };
119         type =
120             g_type_register_static (HILDON_TYPE_VOLUMEBAR,
121                     "HildonVVolumebar", &info, 0);
122     }
123     return type;
124 }
125
126 static void 
127 hildon_vvolumebar_class_init                    (HildonVVolumebarClass *klass)
128 {
129     GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS(klass);
130
131     parent_class = g_type_class_peek_parent(klass);
132
133     volumebar_class->size_request   = hildon_vvolumebar_size_request;
134     volumebar_class->size_allocate  = hildon_vvolumebar_size_allocate;
135     volumebar_class->expose_event   = hildon_vvolumebar_expose;
136 }
137
138 static void 
139 hildon_vvolumebar_init                          (HildonVVolumebar *vvolumebar)
140 {
141     HildonVolumebarPrivate *priv;
142
143     priv = HILDON_VOLUMEBAR_GET_PRIVATE (vvolumebar);
144     g_assert (priv);
145
146     priv->volumebar = HILDON_VOLUMEBAR_RANGE (hildon_volumebar_range_new
147             (GTK_ORIENTATION_VERTICAL));
148
149     gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (vvolumebar));
150     gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (vvolumebar));
151
152     gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
153
154     /* Signals */
155     g_signal_connect_swapped(G_OBJECT(priv->volumebar), "value-changed",
156             G_CALLBACK(hildon_volumebar_level_change),
157             vvolumebar);
158
159     g_signal_connect_swapped(priv->tbutton, "toggled",
160             G_CALLBACK(hildon_volumebar_mute_toggled), vvolumebar);
161
162     /* FIXME Not sure why this is here */
163     gtk_widget_show (GTK_WIDGET (priv->volumebar));
164 }
165
166 /**
167  * hildon_vvolumebar_new:
168  *
169  * Creates a new #HildonVVolumebar widget.
170  *
171  * Returns: a new #HildonVVolumebar
172  */
173 GtkWidget*
174 hildon_vvolumebar_new                           (void)
175 {
176     return GTK_WIDGET (g_object_new(HILDON_TYPE_VVOLUMEBAR, NULL));
177 }
178
179 static gboolean 
180 hildon_vvolumebar_expose                        (GtkWidget *widget,
181                                                  GdkEventExpose *event)
182 {
183
184     HildonVolumebarPrivate *priv;
185
186     priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
187     g_assert (priv);
188
189     if (GTK_WIDGET_DRAWABLE (widget)) {
190         /* Paint background */
191         gtk_paint_box (widget->style, widget->window,
192                 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
193                 NULL, widget, "background",
194                 widget->allocation.x,
195                 widget->allocation.y,
196                 widget->allocation.width,
197                 widget->allocation.height);
198
199         /* The contents of the widget can paint themselves */
200         (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
201     }
202
203     return FALSE;
204 }
205
206 static void
207 hildon_vvolumebar_size_request                  (GtkWidget *widget,
208                                                  GtkRequisition *requisition)
209 {
210     requisition->height = MINIMUM_BAR_HEIGHT;
211     requisition->width = DEFAULT_BAR_WIDTH;
212 }
213
214 static void
215 hildon_vvolumebar_size_allocate                 (GtkWidget *widget,
216                                                  GtkAllocation *allocation)
217 {
218     HildonVolumebarPrivate *priv;
219
220     GtkAllocation range_allocation, button_allocation;
221
222     priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
223     g_assert (priv);
224
225     /* Center the widget horizontally */
226     if (allocation->width > DEFAULT_BAR_WIDTH) {
227         allocation->x +=
228             (allocation->width - DEFAULT_BAR_WIDTH) / 2;
229         allocation->width = DEFAULT_BAR_WIDTH;
230     }
231
232     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
233
234     if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
235         /* Allocate space for the slider */
236         range_allocation.x = allocation->x;
237         range_allocation.y = allocation->y + DEFAULT_ENDING_SIZE;
238
239         range_allocation.width = DEFAULT_BAR_WIDTH;
240
241         if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
242         {
243             /* Leave room for the mute button */
244             range_allocation.height = MAX (0,
245                     allocation->height
246                     - 2 * DEFAULT_ENDING_SIZE
247                     - DEFAULT_VERTICAL_TBUTTON_HEIGHT
248                     - VERTICAL_MUTE_GAP);
249         }
250
251         else
252         {
253             range_allocation.height = MAX (0,
254                     allocation->height
255                     - 2 * DEFAULT_ENDING_SIZE);
256         }
257
258         gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),
259                 &range_allocation);
260     }
261
262     if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
263         /* Allocate space for the mute button */
264         button_allocation.x = allocation->x + HORIZONTAL_MUTE_GAP;
265         button_allocation.y = allocation->y + allocation->height -
266             DEFAULT_VERTICAL_TBUTTON_HEIGHT -
267             VERTICAL_MUTE_GAP - 2 * DEFAULT_ENDING_SIZE;
268         button_allocation.width = DEFAULT_VERTICAL_TBUTTON_WIDTH;
269         button_allocation.height = DEFAULT_VERTICAL_TBUTTON_HEIGHT;
270         gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
271                 &button_allocation);
272     }
273 }