2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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; either version 2.1 of
11 * the License, or (at your option) any later version.
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.
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
26 * @file hildon-volumebar.c
28 * This file contains the implementation of the Hildon Volumebar.
29 * It is a base class for Hildon Vertical Volumebar and for
30 * Hildon Horizontal Volumebar classes.
32 #include <gtk/gtksignal.h>
33 #include <gdk/gdkkeysyms.h>
35 #include "hildon-volumebar.h"
36 #include "hildon-volumebar-range.h"
37 #include "hildon-volumebar-private.h"
39 static GtkContainerClass *parent_class;
42 hildon_volumebar_class_init(HildonVolumebarClass * volumebar_class);
44 hildon_volumebar_init(HildonVolumebar * volumebar);
47 hildon_child_forall(GtkContainer * container,
48 gboolean include_internals,
50 gpointer callback_data);
52 hildon_volumebar_destroy(GtkObject * self);
54 static void hildon_volumebar_set_property(GObject * object,
58 static void hildon_volumebar_get_property(GObject * object,
63 static void mute_toggled (HildonVolumebar *self, gpointer data);
66 hildon_volumebar_key_press(GtkWidget * widget,
84 static guint signals[LAST_SIGNAL] = { 0 };
87 hildon_volumebar_get_type(void)
89 static GType volumebar_type = 0;
91 if (!volumebar_type) {
92 static const GTypeInfo volumebar_info = {
93 sizeof(HildonVolumebarClass),
95 NULL, /* base_finalize */
96 (GClassInitFunc) hildon_volumebar_class_init,
97 NULL, /* class_finalize */
98 NULL, /* class_data */
99 sizeof(HildonVolumebar),
101 (GInstanceInitFunc) hildon_volumebar_init,
103 volumebar_type = g_type_register_static(GTK_TYPE_CONTAINER,
107 return volumebar_type;
111 hildon_volumebar_class_init(HildonVolumebarClass *volumebar_class)
113 GtkContainerClass *container_class =
114 GTK_CONTAINER_CLASS(volumebar_class);
115 GObjectClass *object_class = G_OBJECT_CLASS(volumebar_class);
117 parent_class = g_type_class_peek_parent(volumebar_class);
119 g_type_class_add_private(volumebar_class,
120 sizeof(HildonVolumebarPrivate));
122 /* Because we derived our widget from GtkContainer, we should also
123 override forall method */
124 container_class->forall = hildon_child_forall;
125 GTK_OBJECT_CLASS(volumebar_class)->destroy = hildon_volumebar_destroy;
127 signals[MUTE_TOGGLED_SIGNAL] = g_signal_new("mute_toggled",
133 (HildonVolumebarClass,
134 mute_toggled), NULL, NULL,
135 gtk_marshal_VOID__VOID,
138 signals[LEVEL_CHANGED_SIGNAL] = g_signal_new("level_changed",
144 (HildonVolumebarClass,
145 level_changed), NULL,
147 gtk_marshal_VOID__VOID,
150 object_class->set_property = hildon_volumebar_set_property;
151 object_class->get_property = hildon_volumebar_get_property;
153 /*This kind of property could be usefull in the gtkcontainer*/
154 g_object_class_install_property(object_class,
155 PROP_HILDON_FOCUSABLE,
156 g_param_spec_boolean("can-focus",
157 "The widget focusablility",
158 "The widget focusablility. TRUE is focusable",
160 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
162 g_object_class_install_property(object_class,
163 PROP_HILDON_HAS_MUTE,
164 g_param_spec_boolean("has_mute",
165 "Show/Hide the mute button",
166 "Whether the mute button is visible. Default value: TRUE",
168 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
173 hildon_volumebar_init(HildonVolumebar * volumebar)
175 HildonVolumebarPrivate *priv;
177 priv = HILDON_VOLUMEBAR_GET_PRIVATE(volumebar);
179 /* Should set GTK_NO_WINDOW flag, because widget is derived from
181 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(volumebar), GTK_NO_WINDOW);
182 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(volumebar), GTK_CAN_FOCUS);
184 priv->tbutton = GTK_TOGGLE_BUTTON(gtk_toggle_button_new());
185 g_object_set (G_OBJECT (priv->tbutton), "can-focus", FALSE, NULL);
186 g_signal_connect (G_OBJECT (volumebar), "mute_toggled",
187 G_CALLBACK (mute_toggled), NULL);
189 /* set keypress handler (select hardkey) */
190 g_signal_connect(G_OBJECT(volumebar), "key-press-event",
191 G_CALLBACK(hildon_volumebar_key_press),
197 hildon_child_forall(GtkContainer * container,
198 gboolean include_internals,
199 GtkCallback callback, gpointer callback_data)
201 HildonVolumebar *vbar;
202 HildonVolumebarPrivate *priv;
204 vbar = HILDON_VOLUMEBAR(container);
205 priv = HILDON_VOLUMEBAR_GET_PRIVATE(vbar);
207 g_return_if_fail(callback != NULL);
209 if (!include_internals)
212 (*callback) (GTK_WIDGET(priv->tbutton), callback_data);
213 (*callback) (GTK_WIDGET(priv->volumebar), callback_data);
217 hildon_volumebar_destroy(GtkObject * self)
219 HildonVolumebarPrivate *priv;
221 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
224 gtk_widget_unparent(GTK_WIDGET(priv->tbutton));
225 priv->tbutton = NULL;
227 if (priv->volumebar) {
228 gtk_widget_unparent(GTK_WIDGET(priv->volumebar));
229 priv->volumebar = NULL;
232 if (GTK_OBJECT_CLASS(parent_class)->destroy)
233 GTK_OBJECT_CLASS(parent_class)->destroy(self);
237 hildon_volumebar_set_property(GObject * object,
239 const GValue * value,
242 HildonVolumebar *vbar = HILDON_VOLUMEBAR(object);
243 HildonVolumebarPrivate *priv;
244 gboolean has_mute = TRUE;
246 priv = HILDON_VOLUMEBAR_GET_PRIVATE(vbar)
249 case PROP_HILDON_HAS_MUTE:
250 has_mute = g_value_get_boolean(value);
254 gtk_widget_show(GTK_WIDGET(priv->tbutton));
259 gtk_widget_hide(GTK_WIDGET(priv->tbutton));
263 case PROP_HILDON_FOCUSABLE:
264 g_object_set( G_OBJECT(priv->volumebar), "can-focus",
265 g_value_get_boolean(value), NULL );
268 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
275 hildon_volumebar_get_property(GObject * object,
276 guint prop_id, GValue * value,
279 HildonVolumebarPrivate *priv;
280 priv = HILDON_VOLUMEBAR_GET_PRIVATE(object)
282 case PROP_HILDON_HAS_MUTE:
283 g_value_set_boolean(value, g_value_get_boolean(value));
285 case PROP_HILDON_FOCUSABLE:
286 g_value_set_boolean(value, g_value_get_boolean(value));
287 g_object_set( G_OBJECT(priv->volumebar), "can-focus",
288 g_value_get_boolean(value), NULL );
290 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
296 * hildon_volumebar_level_change:
297 * @self: a HildonVolumebar widget.
299 * Emits "level_changed" signal to the given volumebar. This function
300 * is mainly used by derived classes.
303 hildon_volumebar_level_change(HildonVolumebar * self)
305 g_return_if_fail(HILDON_IS_VOLUMEBAR(self));
306 g_signal_emit_by_name(GTK_WIDGET(self), "level_changed");
310 * hildon_volumebar_set_level:
311 * @self: volumebar to change level on
314 * Sets new volumelevel for this #HildonVolumebar.
317 hildon_volumebar_set_level(HildonVolumebar * self, gdouble level)
319 HildonVolumebarPrivate *priv;
321 g_return_if_fail(self);
323 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
325 /* No need to clamp the level to legal values here as volumebarrange
326 * will do it anyway. And here we don't know the correct values anyway.
328 hildon_volumebar_range_set_level(priv->volumebar, level);
332 * hildon_volumebar_get_level:
333 * @self: volumebar to query level on
335 * Gets the volumelevel of this #HildonVolumebar.
337 * Return value: Volume level.
340 hildon_volumebar_get_level(HildonVolumebar * self)
342 HildonVolumebarPrivate *priv;
344 g_return_val_if_fail(self, -1);
346 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
348 return hildon_volumebar_range_get_level(priv->volumebar);
352 * hildon_volumebar_set_mute:
353 * @self: volumebar to work on
356 * Sets mute status for this #HildonVolumebar.
360 hildon_volumebar_set_mute(HildonVolumebar * self, gboolean mute)
362 HildonVolumebarPrivate *priv;
364 g_return_if_fail(self);
366 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
367 gtk_widget_set_sensitive(GTK_WIDGET(priv->volumebar), !mute);
370 g_object_set (G_OBJECT (priv->tbutton), "can-focus", TRUE, NULL);
371 gtk_widget_grab_focus (GTK_WIDGET(priv->tbutton));
375 g_object_set (G_OBJECT (priv->tbutton), "can-focus", FALSE, NULL);
376 gtk_widget_grab_focus (GTK_WIDGET (self));
381 * hildon_volumebar_get_mute:
382 * @self: volumebar to query mute status
384 * Gets mute status of this #HildonVolumebar (ON/OFF).
386 * Return value: Mute status as #gboolean value.
389 hildon_volumebar_get_mute(HildonVolumebar * self)
391 HildonVolumebarPrivate *priv;
393 g_return_val_if_fail(self, TRUE);
395 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
397 return gtk_toggle_button_get_active(priv->tbutton);
401 * hildon_volumebar_get_adjustment
402 * @self : A #HildonVolumebar
404 * Gets the GtkAdjustment used in volumebar. This can be handy
405 * to give to hildon_appview_set_connected_adjustment which
406 * will allow changing the volume with increase / decrease
409 * This is a temporary solution until volumebar is restructured to
410 * be a child class of GtkRange.
412 * Return value: A @GtkAdjustment used by volumebar.
415 hildon_volumebar_get_adjustment (HildonVolumebar * self)
417 HildonVolumebarPrivate *priv;
419 g_return_val_if_fail(self, NULL);
421 priv = HILDON_VOLUMEBAR_GET_PRIVATE(self);
423 return gtk_range_get_adjustment (GTK_RANGE (priv->volumebar));
427 mute_toggled (HildonVolumebar *self, gpointer data)
429 /* only call hildon_volumebar_set_mute. everything is done there */
430 hildon_volumebar_set_mute (self, hildon_volumebar_get_mute(self));
434 hildon_volumebar_key_press (GtkWidget * widget,
438 HildonVolumebarPrivate *priv;
440 g_return_val_if_fail(widget, FALSE);
441 g_return_val_if_fail(event, FALSE);
443 priv = HILDON_VOLUMEBAR_GET_PRIVATE(widget);
445 if (event->keyval == GDK_Return) {
446 gtk_toggle_button_set_active(priv->tbutton,
447 !hildon_volumebar_get_mute(HILDON_VOLUMEBAR(widget)));