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 "gtkadjustment.h"
34 #include "gtktooltip.h"
38 * SECTION:gtkvolumebutton
39 * @Short_description: A button which pops up a volume control
40 * @Title: GtkVolumeButton
42 * #GtkVolumeButton is a subclass of #GtkScaleButton that has
43 * been tailored for use as a volume control widget with suitable
44 * icons, tooltips and accessible labels.
47 #define EPSILON (1e-10)
49 static const gchar * const icons[] =
54 "audio-volume-medium",
58 static const gchar * const icons_symbolic[] =
60 "audio-volume-muted-symbolic",
61 "audio-volume-high-symbolic",
62 "audio-volume-low-symbolic",
63 "audio-volume-medium-symbolic",
73 static gboolean cb_query_tooltip (GtkWidget *button,
76 gboolean keyboard_mode,
79 static void cb_value_changed (GtkVolumeButton *button,
83 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
86 gtk_volume_button_set_property (GObject *object,
91 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
96 if (g_value_get_boolean (value))
97 gtk_scale_button_set_icons (button, (const char **) icons_symbolic);
99 gtk_scale_button_set_icons (button, (const char **) icons);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 gtk_volume_button_get_property (GObject *object,
115 case PROP_SYMBOLIC: {
118 g_object_get (object, "icons", &icon_list, NULL);
119 if (icon_list != NULL &&
120 icon_list[0] != NULL &&
121 g_str_equal (icon_list[0], icons_symbolic[0]))
122 g_value_set_boolean (value, TRUE);
124 g_value_set_boolean (value, FALSE);
125 g_strfreev (icon_list);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 gobject_class->set_property = gtk_volume_button_set_property;
140 gobject_class->get_property = gtk_volume_button_get_property;
143 * GtkVolumeButton:use-symbolic:
145 * Whether to use symbolic icons as the icons. Note that
146 * if the symbolic icons are not available in your installed
147 * theme, then the normal (potentially colorful) icons will
152 g_object_class_install_property (gobject_class,
154 g_param_spec_boolean ("use-symbolic",
155 P_("Use symbolic icons"),
156 P_("Whether to use symbolic icons"),
162 gtk_volume_button_init (GtkVolumeButton *button)
164 GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
166 GtkWidget *minus_button, *plus_button;
168 atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
170 atk_object_set_description (gtk_widget_get_accessible (GTK_WIDGET (button)),
171 _("Turns volume down or up"));
172 atk_action_set_description (ATK_ACTION (gtk_widget_get_accessible (GTK_WIDGET (button))),
174 _("Adjusts the volume"));
176 minus_button = gtk_scale_button_get_minus_button (sbutton);
177 plus_button = gtk_scale_button_get_plus_button (sbutton);
179 atk_object_set_name (gtk_widget_get_accessible (minus_button),
181 atk_object_set_description (gtk_widget_get_accessible (minus_button),
182 _("Decreases the volume"));
183 gtk_widget_set_tooltip_text (minus_button, _("Volume Down"));
185 atk_object_set_name (gtk_widget_get_accessible (plus_button),
187 atk_object_set_description (gtk_widget_get_accessible (plus_button),
188 _("Increases the volume"));
189 gtk_widget_set_tooltip_text (plus_button, _("Volume Up"));
191 gtk_scale_button_set_icons (sbutton, (const char **) icons);
193 adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);
194 g_object_set (G_OBJECT (button),
196 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
200 g_signal_connect (G_OBJECT (button), "query-tooltip",
201 G_CALLBACK (cb_query_tooltip), NULL);
202 g_signal_connect (G_OBJECT (button), "value-changed",
203 G_CALLBACK (cb_value_changed), NULL);
207 * gtk_volume_button_new:
209 * Creates a #GtkVolumeButton, with a range between 0.0 and 1.0, with
210 * a stepping of 0.02. Volume values can be obtained and modified using
211 * the functions from #GtkScaleButton.
213 * Return value: a new #GtkVolumeButton
218 gtk_volume_button_new (void)
221 button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
222 return GTK_WIDGET (button);
226 cb_query_tooltip (GtkWidget *button,
229 gboolean keyboard_mode,
233 GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
234 GtkAdjustment *adjustment;
239 image = ATK_IMAGE (gtk_widget_get_accessible (button));
241 adjustment = gtk_scale_button_get_adjustment (scale_button);
242 val = gtk_scale_button_get_value (scale_button);
244 if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
246 str = g_strdup (_("Muted"));
248 else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
250 str = g_strdup (_("Full Volume"));
256 percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
258 /* Translators: this is the percentage of the current volume,
259 * as used in the tooltip, eg. "49 %".
260 * Translate the "%d" to "%Id" if you want to use localised digits,
261 * or otherwise translate the "%d" to "%d".
263 str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
266 gtk_tooltip_set_text (tooltip, str);
267 atk_image_set_image_description (image, str);
274 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
276 gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));