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