1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2007 Red Hat, Inc.
5 * - Bastien Nocera <bnocera@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GTK+ Team and others 2007. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkvolumebutton.h"
32 #include "gtktooltip.h"
37 * SECTION:gtkvolumebutton
38 * @Short_description: A button which pops up a volume control
39 * @Title: GtkVolumeButton
41 * #GtkVolumeButton is a subclass of #GtkScaleButton that has
42 * been tailored for use as a volume control widget with suitable
43 * icons, tooltips and accessible labels.
46 #define EPSILON (1e-10)
48 static const gchar * const icons[] =
53 "audio-volume-medium",
57 static const gchar * const icons_symbolic[] =
59 "audio-volume-muted-symbolic",
60 "audio-volume-high-symbolic",
61 "audio-volume-low-symbolic",
62 "audio-volume-medium-symbolic",
72 static gboolean cb_query_tooltip (GtkWidget *button,
75 gboolean keyboard_mode,
78 static void cb_value_changed (GtkVolumeButton *button,
82 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
85 gtk_volume_button_set_property (GObject *object,
90 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
95 if (g_value_get_boolean (value))
96 gtk_scale_button_set_icons (button, (const char **) icons_symbolic);
98 gtk_scale_button_set_icons (button, (const char **) icons);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 gtk_volume_button_get_property (GObject *object,
114 case PROP_SYMBOLIC: {
117 g_object_get (object, "icons", &icon_list, NULL);
118 if (icon_list != NULL &&
119 icon_list[0] != NULL &&
120 g_str_equal (icon_list[0], icons_symbolic[0]))
121 g_value_set_boolean (value, TRUE);
123 g_value_set_boolean (value, FALSE);
124 g_strfreev (icon_list);
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 gobject_class->set_property = gtk_volume_button_set_property;
139 gobject_class->get_property = gtk_volume_button_get_property;
142 * GtkVolumeButton:use-symbolic:
144 * Whether to use symbolic icons as the icons. Note that
145 * if the symbolic icons are not available in your installed
146 * theme, then the normal (potentially colorful) icons will
151 g_object_class_install_property (gobject_class,
153 g_param_spec_boolean ("use-symbolic",
154 P_("Use symbolic icons"),
155 P_("Whether to use symbolic icons"),
161 gtk_volume_button_init (GtkVolumeButton *button)
163 GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
165 GtkWidget *minus_button, *plus_button;
167 atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
169 atk_object_set_description (gtk_widget_get_accessible (GTK_WIDGET (button)),
170 _("Turns volume down or up"));
171 atk_action_set_description (ATK_ACTION (gtk_widget_get_accessible (GTK_WIDGET (button))),
173 _("Adjusts the volume"));
175 minus_button = gtk_scale_button_get_minus_button (sbutton);
176 plus_button = gtk_scale_button_get_plus_button (sbutton);
178 atk_object_set_name (gtk_widget_get_accessible (minus_button),
180 atk_object_set_description (gtk_widget_get_accessible (minus_button),
181 _("Decreases the volume"));
182 gtk_widget_set_tooltip_text (minus_button, _("Volume Down"));
184 atk_object_set_name (gtk_widget_get_accessible (plus_button),
186 atk_object_set_description (gtk_widget_get_accessible (plus_button),
187 _("Increases the volume"));
188 gtk_widget_set_tooltip_text (plus_button, _("Volume Up"));
190 gtk_scale_button_set_icons (sbutton, (const char **) icons);
192 adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);
193 g_object_set (G_OBJECT (button),
195 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
199 g_signal_connect (G_OBJECT (button), "query-tooltip",
200 G_CALLBACK (cb_query_tooltip), NULL);
201 g_signal_connect (G_OBJECT (button), "value-changed",
202 G_CALLBACK (cb_value_changed), NULL);
206 * gtk_volume_button_new
208 * Creates a #GtkVolumeButton, with a range between 0.0 and 1.0, with
209 * a stepping of 0.02. Volume values can be obtained and modified using
210 * the functions from #GtkScaleButton.
212 * Return value: a new #GtkVolumeButton
217 gtk_volume_button_new (void)
220 button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
221 return GTK_WIDGET (button);
225 cb_query_tooltip (GtkWidget *button,
228 gboolean keyboard_mode,
232 GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
233 GtkAdjustment *adjustment;
238 image = ATK_IMAGE (gtk_widget_get_accessible (button));
240 adjustment = gtk_scale_button_get_adjustment (scale_button);
241 val = gtk_scale_button_get_value (scale_button);
243 if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
245 str = g_strdup (_("Muted"));
247 else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
249 str = g_strdup (_("Full Volume"));
255 percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
257 /* Translators: this is the percentage of the current volume,
258 * as used in the tooltip, eg. "49 %".
259 * Translate the "%d" to "%Id" if you want to use localised digits,
260 * or otherwise translate the "%d" to "%d".
262 str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
265 gtk_tooltip_set_text (tooltip, str);
266 atk_image_set_image_description (image, str);
273 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
275 gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));