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