2008-04-15 13:21:13 <timj@imendio.com>
[hildon] / src / hildon-hvolumebar.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-hvolumebar
27  * @short_description: A widget that displays a horizontal volume bar.
28  * @see_also: #HildonVVolumebar, #HildonVolumebar
29  * 
30  * The #HildonHVolumebar widget displays a horizontal volume bar that allows
31  * increasing or decreasing volume within a pre-defined range, and includes 
32  * a mute icon which users can click to mute the sound.
33  * 
34  * <example>
35  * <programlisting>
36  * GtkWidget *volbar = hildon_hvolumebar_new ();
37  * g_signal_connect (G_OBJECT(volbar), "mute_toggled", G_CALLBACK(mute_toggle), NULL);
38  * g_signal_connect (G_OBJECT(volbar), "level_changed", G_CALLBACK(level_change), NULL);
39  * </programlisting>
40  * </example>
41  *
42  */
43
44 #include                                        "hildon-hvolumebar.h"
45 #include                                        "hildon-volumebar.h"
46 #include                                        "hildon-volumebar-range.h"
47 #include                                        "hildon-volumebar-private.h"
48 #include                                        <gtk/gtktoolbar.h>
49
50 /* Defines for normal version of HVolumebar */
51 /* Toggle button */
52
53 #define                                         DEFAULT_TBUTTON_WIDTH 26
54
55 #define                                         DEFAULT_TBUTTON_HEIGHT 26
56
57 /* Volume bar */
58 #define                                         MINIMUM_BAR_WIDTH 147
59
60 #define                                         DEFAULT_BAR_HEIGHT 58
61
62 #define                                         DEFAULT_ENDING_SIZE 20
63
64 /* Gap to leave for mute button */
65 #define                                         VERTICAL_MUTE_GAP 16
66
67 #define                                         HORIZONTAL_MUTE_GAP 6
68
69 /* Sizes inside a toolbar */
70 /* Toggle button */
71
72 #define                                         TOOL_DEFAULT_TBUTTON_WIDTH 26
73
74 #define                                         TOOL_DEFAULT_TBUTTON_HEIGHT 26
75
76 /* Volumebar */
77
78 #define                                         TOOL_MINIMUM_BAR_WIDTH 121
79
80 #define                                         TOOL_DEFAULT_BAR_HEIGHT 40
81
82 #define                                         TOOL_DEFAULT_ENDING_SIZE 0
83
84 #define                                         TOOL_VERTICAL_MUTE_GAP \
85                                                 ((TOOL_DEFAULT_BAR_HEIGHT - TOOL_DEFAULT_TBUTTON_HEIGHT) / 2)
86
87 static HildonVolumebarClass*                    parent_class;
88
89 static void 
90 hildon_hvolumebar_class_init                    (HildonHVolumebarClass *klass);
91
92 static void 
93 hildon_hvolumebar_init                          (HildonHVolumebar *hvolumebar);
94
95 static gboolean
96 hildon_hvolumebar_expose                        (GtkWidget *widget,
97                                                  GdkEventExpose *event);
98 static void 
99 hildon_hvolumebar_size_request                  (GtkWidget *widget,
100                                                  GtkRequisition *requisition);
101 static void 
102 hildon_hvolumebar_size_allocate                 (GtkWidget *widget,
103                                                  GtkAllocation *allocation);
104 static void 
105 hildon_hvolumebar_map                           (GtkWidget *widget);
106
107 /**
108  * hildon_hvolumebar_get_type:
109  *
110  * Returns GType for HildonHVolumebar.
111  *
112  * Returns: HildonHVolumebar type
113  */
114 GType G_GNUC_CONST
115 hildon_hvolumebar_get_type                      (void)
116 {
117     static GType type = 0;
118
119     if (!type) {
120         static const GTypeInfo info = {
121             sizeof (HildonHVolumebarClass),
122             NULL,       /* base_init */
123             NULL,       /* base_finalize */
124             (GClassInitFunc) hildon_hvolumebar_class_init,     /* class_init */
125             NULL,       /* class_finalize */
126             NULL,       /* class_data */
127             sizeof (HildonHVolumebar),
128             0,
129             (GInstanceInitFunc) hildon_hvolumebar_init,
130         };
131         type = g_type_register_static (HILDON_TYPE_VOLUMEBAR,
132                 "HildonHVolumebar", &info, 0);
133     }
134     return type;
135 }
136
137 static void
138 hildon_hvolumebar_class_init                    (HildonHVolumebarClass *klass)
139 {
140     GtkWidgetClass *volumebar_class = GTK_WIDGET_CLASS (klass);
141
142     parent_class = g_type_class_peek_parent (klass);
143
144     volumebar_class->size_request   = hildon_hvolumebar_size_request;
145     volumebar_class->size_allocate  = hildon_hvolumebar_size_allocate;
146     volumebar_class->map            = hildon_hvolumebar_map;
147     volumebar_class->expose_event   = hildon_hvolumebar_expose;
148 }
149
150 static void 
151 hildon_hvolumebar_init                          (HildonHVolumebar *hvolumebar)
152 {
153     HildonVolumebarPrivate *priv;
154
155     priv = HILDON_VOLUMEBAR_GET_PRIVATE (hvolumebar);
156
157     priv->volumebar =
158         HILDON_VOLUMEBAR_RANGE(hildon_volumebar_range_new
159                 (GTK_ORIENTATION_HORIZONTAL));
160
161     gtk_widget_set_parent (GTK_WIDGET (priv->tbutton), GTK_WIDGET (hvolumebar));
162     gtk_widget_set_parent (GTK_WIDGET (priv->volumebar), GTK_WIDGET (hvolumebar));
163
164     gtk_scale_set_draw_value (GTK_SCALE (priv->volumebar), FALSE);
165
166     /* Signals */
167     g_signal_connect_swapped (G_OBJECT (priv->volumebar), "value-changed",
168             G_CALLBACK(hildon_volumebar_level_change),
169             hvolumebar);
170
171     g_signal_connect_swapped (priv->tbutton, "toggled",
172             G_CALLBACK (hildon_volumebar_mute_toggled), hvolumebar);
173
174     gtk_widget_show (GTK_WIDGET (priv->volumebar));
175 }
176
177 /**
178  * hildon_hvolumebar_new:
179  *
180  * Creates a new #HildonHVolumebar widget.
181  *
182  * Returns: a new #HildonHVolumebar
183  */
184 GtkWidget*
185 hildon_hvolumebar_new                           (void)
186 {
187     return GTK_WIDGET (g_object_new(HILDON_TYPE_HVOLUMEBAR, NULL));
188 }
189
190 static void 
191 hildon_hvolumebar_map                           (GtkWidget* widget)
192 {
193     HildonVolumebarPrivate *priv;
194     GtkWidget *parent;
195
196     priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
197     g_assert (priv);
198
199     parent = gtk_widget_get_ancestor (GTK_WIDGET (widget), GTK_TYPE_TOOLBAR);
200
201     /* Check if the volumebar is in a toolbar */
202     if (parent)
203         priv->is_toolbar = TRUE;
204
205     GTK_WIDGET_CLASS (parent_class)->map (widget);
206 }
207
208 static gboolean 
209 hildon_hvolumebar_expose                        (GtkWidget * widget,
210                                                  GdkEventExpose * event)
211 {
212     HildonVolumebarPrivate *priv;
213
214     priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
215     g_assert (priv);
216
217     if (GTK_WIDGET_DRAWABLE (widget)) {
218         /* Paint background */
219         gtk_paint_box (widget->style, widget->window,
220                 GTK_WIDGET_STATE (priv->volumebar), GTK_SHADOW_OUT,
221                 NULL, widget, "background",
222                 widget->allocation.x,
223                 widget->allocation.y,
224                 widget->allocation.width,
225                 widget->allocation.height);
226
227         /* The contents of the widget can paint themselves */
228         /* FIXME Not sure if this is even needed here */
229         (*GTK_WIDGET_CLASS(parent_class)->expose_event) (widget, event);
230     }
231
232     return FALSE;
233 }
234
235 static void
236 hildon_hvolumebar_size_request                  (GtkWidget * widget,
237                                                  GtkRequisition * requisition)
238 {
239     HildonVolumebarPrivate *priv;
240
241     priv = HILDON_VOLUMEBAR_GET_PRIVATE(HILDON_VOLUMEBAR(widget));
242     g_assert (priv);
243
244     /* Volumebar has different dimensions in toolbar */
245     requisition->width = (priv->is_toolbar
246             ? TOOL_MINIMUM_BAR_WIDTH
247             : MINIMUM_BAR_WIDTH);
248
249     requisition->height = (priv->is_toolbar
250             ? TOOL_DEFAULT_BAR_HEIGHT
251             : DEFAULT_BAR_HEIGHT);
252 }
253
254 static void
255 hildon_hvolumebar_size_allocate                 (GtkWidget * widget,
256                                                  GtkAllocation * allocation)
257 {
258     HildonVolumebarPrivate *priv;
259     GtkAllocation button_allocation, range_allocation;
260
261     priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
262     g_assert (priv);
263
264     button_allocation.x = 0;
265     button_allocation.width = 0;
266
267     /* Center the widget vertically */
268     if (priv->is_toolbar && allocation->height > TOOL_DEFAULT_BAR_HEIGHT) {
269         allocation->y += (allocation->height - TOOL_DEFAULT_BAR_HEIGHT) / 2;
270         allocation->height = TOOL_DEFAULT_BAR_HEIGHT;
271     }
272
273     if (!priv->is_toolbar && allocation->height > DEFAULT_BAR_HEIGHT) {
274         allocation->y += (allocation->height - DEFAULT_BAR_HEIGHT) / 2;
275         allocation->height = DEFAULT_BAR_HEIGHT;
276     }
277
278     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
279
280     if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton)) {
281
282         /* Allocate space for the mute button */
283         if (priv->is_toolbar) {
284             button_allocation.x = allocation->x;
285             button_allocation.y = allocation->y + TOOL_VERTICAL_MUTE_GAP;
286             button_allocation.width = TOOL_DEFAULT_TBUTTON_WIDTH;
287             button_allocation.height = TOOL_DEFAULT_TBUTTON_HEIGHT;
288         } else {
289             button_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
290             button_allocation.y = allocation->y + VERTICAL_MUTE_GAP;
291             button_allocation.width = DEFAULT_TBUTTON_WIDTH;
292             button_allocation.height = DEFAULT_TBUTTON_HEIGHT;
293         }
294         gtk_widget_size_allocate (GTK_WIDGET (priv->tbutton),
295                 &button_allocation);
296     }
297     if (priv->volumebar && GTK_WIDGET_VISIBLE (priv->volumebar)) {
298
299         /* Allocate space for the slider */
300         range_allocation.y = allocation->y;
301
302         if (priv->tbutton && GTK_WIDGET_VISIBLE (priv->tbutton))
303         {
304             /* Leave room for the mute button */
305             range_allocation.x = button_allocation.x
306                 + button_allocation.width
307                 + HORIZONTAL_MUTE_GAP;
308
309             if (priv->is_toolbar) 
310             {
311                 /* In toolbar with mute button */
312                 range_allocation.width = MAX(0,
313                         allocation->width
314                         - 2 * TOOL_DEFAULT_ENDING_SIZE
315                         - TOOL_DEFAULT_TBUTTON_WIDTH
316                         - HORIZONTAL_MUTE_GAP);
317
318                 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
319
320             } 
321
322             else 
323             {
324                 /* Standalone with mute button */
325                 range_allocation.width = MAX(0,
326                         allocation->width
327                         - 2 * DEFAULT_ENDING_SIZE
328                         - DEFAULT_TBUTTON_WIDTH
329                         - HORIZONTAL_MUTE_GAP);
330
331                 range_allocation.height = DEFAULT_BAR_HEIGHT;
332             }
333
334         }
335
336         else
337         {
338             if (priv->is_toolbar) 
339             {
340                 /* In toolbar without mute button */
341                 range_allocation.x = allocation->x;
342
343                 range_allocation.width = MAX(0,
344                         allocation->width
345                         - 2 * TOOL_DEFAULT_ENDING_SIZE );
346
347                 range_allocation.height = TOOL_DEFAULT_BAR_HEIGHT;
348
349             } 
350
351             else 
352             {
353                 /* Standalone without mute button */
354                 range_allocation.x = allocation->x + DEFAULT_ENDING_SIZE;
355
356                 range_allocation.width = MAX(0,
357                         allocation->width
358                         - 2 * DEFAULT_ENDING_SIZE );
359
360                 range_allocation.height = DEFAULT_BAR_HEIGHT;
361             }
362         }
363
364         gtk_widget_size_allocate (GTK_WIDGET (priv->volumebar),
365                 &range_allocation);
366     }
367 }
368