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