2008-09-24 Claudio Saavedra <csaavedra@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: Michael Dominic Kostrzewa <michael.kostrzewa@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 #ifdef                                          HAVE_CONFIG_H
47 #include                                        <config.h>
48 #endif
49
50 #include                                        "hildon-vvolumebar.h"
51 #include                                        "hildon-volumebar-range.h"
52 #include                                        "hildon-volumebar-private.h"
53
54 /* Volume bar */
55 #define                                         DEFAULT_BAR_WIDTH 58
56
57 #define                                         MINIMUM_BAR_HEIGHT 165
58
59 /* Toggle button */
60
61 #define                                         DEFAULT_VERTICAL_TBUTTON_WIDTH 26
62
63 #define                                         DEFAULT_VERTICAL_TBUTTON_HEIGHT 26
64
65 #define                                         DEFAULT_ENDING_SIZE 20
66
67 /* Gap to leave for mute button */
68
69 #define                                         HORIZONTAL_MUTE_GAP 16
70
71 #define                                         VERTICAL_MUTE_GAP 6
72
73 static HildonVolumebarClass*                    parent_class;
74
75 static void 
76 hildon_vvolumebar_class_init                    (HildonVVolumebarClass * klass);
77
78 static void 
79 hildon_vvolumebar_init                          (HildonVVolumebar * vvolumebar);
80
81 static gboolean
82 hildon_vvolumebar_expose                        (GtkWidget * widget,
83                                                  GdkEventExpose * event);
84
85 static void 
86 hildon_vvolumebar_size_request                  (GtkWidget * widget,
87                                                  GtkRequisition * requisition);
88
89 static void
90 hildon_vvolumebar_size_allocate                 (GtkWidget * widget,
91                                                  GtkAllocation * allocation);
92
93 /**
94  * hildon_vvolumebar_get_type:
95  *
96  * Initializes and returns the type of a hildon vvolumebar.
97  *
98  * @Returns: GType of #HildonVVolumebar
99  */
100 GType G_GNUC_CONST
101 hildon_vvolumebar_get_type                      (void)
102 {
103     static GType type = 0;
104
105     if (!type) {
106         static const GTypeInfo info = {
107             sizeof (HildonVVolumebarClass),
108             NULL,       /* base_init */
109             NULL,       /* base_finalize */
110             (GClassInitFunc) hildon_vvolumebar_class_init,     /* class_init */
111             NULL,       /* class_finalize */
112             NULL,       /* class_data */
113             sizeof (HildonVVolumebar),
114             0,
115             (GInstanceInitFunc) hildon_vvolumebar_init,
116         };
117         type =
118             g_type_register_static (HILDON_TYPE_VOLUMEBAR,
119                     "HildonVVolumebar", &info, 0);
120     }
121     return type;
122 }
123
124 static void 
125 hildon_vvolumebar_class_init                    (HildonVVolumebarClass *klass)
126 {
127     GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS(klass);
128
129     parent_class = g_type_class_peek_parent(klass);
130
131     volumebar_class->size_request   = hildon_vvolumebar_size_request;
132     volumebar_class->size_allocate  = hildon_vvolumebar_size_allocate;
133     volumebar_class->expose_event   = hildon_vvolumebar_expose;
134 }
135
136 static void 
137 hildon_vvolumebar_init                          (HildonVVolumebar *vvolumebar)
138 {
139     HildonVolumebarPrivate *priv;
140
141     priv = HILDON_VOLUMEBAR_GET_PRIVATE (vvolumebar);
142     g_assert (priv);
143
144     priv->volumebar = HILDON_VOLUMEBAR_RANGE (hildon_volumebar_range_new
145             (GTK_ORIENTATION_VERTICAL));
146
147     gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (vvolumebar));
148     gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (vvolumebar));
149
150     gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
151
152     /* Signals */
153     g_signal_connect_swapped(G_OBJECT(priv->volumebar), "value-changed",
154             G_CALLBACK(hildon_volumebar_level_change),
155             vvolumebar);
156
157     g_signal_connect_swapped(priv->tbutton, "toggled",
158             G_CALLBACK(hildon_volumebar_mute_toggled), vvolumebar);
159
160     /* FIXME Not sure why this is here */
161     gtk_widget_show (GTK_WIDGET (priv->volumebar));
162 }
163
164 /**
165  * hildon_vvolumebar_new:
166  *
167  * Creates a new #HildonVVolumebar widget.
168  *
169  * Returns: a new #HildonVVolumebar
170  */
171 GtkWidget*
172 hildon_vvolumebar_new                           (void)
173 {
174     return GTK_WIDGET (g_object_new(HILDON_TYPE_VVOLUMEBAR, NULL));
175 }
176
177 static gboolean 
178 hildon_vvolumebar_expose                        (GtkWidget *widget,
179                                                  GdkEventExpose *event)
180 {
181
182     HildonVolumebarPrivate *priv;
183
184     priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
185     g_assert (priv);
186
187     if (GTK_WIDGET_DRAWABLE (widget)) {
188         /* Paint background */
189         gtk_paint_box (widget->style, widget->window,
190                 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
191                 NULL, widget, "background",
192                 widget->allocation.x,
193                 widget->allocation.y,
194                 widget->allocation.width,
195                 widget->allocation.height);
196
197         /* The contents of the widget can paint themselves */
198         (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
199     }
200
201     return FALSE;
202 }
203
204 static void
205 hildon_vvolumebar_size_request                  (GtkWidget *widget,
206                                                  GtkRequisition *requisition)
207 {
208     requisition->height = MINIMUM_BAR_HEIGHT;
209     requisition->width = DEFAULT_BAR_WIDTH;
210 }
211
212 static void
213 hildon_vvolumebar_size_allocate                 (GtkWidget *widget,
214                                                  GtkAllocation *allocation)
215 {
216     HildonVolumebarPrivate *priv;
217
218     GtkAllocation range_allocation, button_allocation;
219
220     priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
221     g_assert (priv);
222
223     /* Center the widget horizontally */
224     if (allocation->width > DEFAULT_BAR_WIDTH) {
225         allocation->x +=
226             (allocation->width - DEFAULT_BAR_WIDTH) / 2;
227         allocation->width = DEFAULT_BAR_WIDTH;
228     }
229
230     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
231
232     if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
233         /* Allocate space for the slider */
234         range_allocation.x = allocation->x;
235         range_allocation.y = allocation->y + DEFAULT_ENDING_SIZE;
236
237         range_allocation.width = DEFAULT_BAR_WIDTH;
238
239         if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
240         {
241             /* Leave room for the mute button */
242             range_allocation.height = MAX (0,
243                     allocation->height
244                     - 2 * DEFAULT_ENDING_SIZE
245                     - DEFAULT_VERTICAL_TBUTTON_HEIGHT
246                     - VERTICAL_MUTE_GAP);
247         }
248
249         else
250         {
251             range_allocation.height = MAX (0,
252                     allocation->height
253                     - 2 * DEFAULT_ENDING_SIZE);
254         }
255
256         gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),
257                 &range_allocation);
258     }
259
260     if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
261         /* Allocate space for the mute button */
262         button_allocation.x = allocation->x + HORIZONTAL_MUTE_GAP;
263         button_allocation.y = allocation->y + allocation->height -
264             VERTICAL_MUTE_GAP - 2 * DEFAULT_ENDING_SIZE;
265         button_allocation.width = DEFAULT_VERTICAL_TBUTTON_WIDTH;
266         button_allocation.height = DEFAULT_VERTICAL_TBUTTON_HEIGHT;
267         gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
268                 &button_allocation);
269     }
270 }