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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 2007. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkvolumebutton.h"
34 #include "gtktooltip.h"
39 * SECTION:gtkvolumebutton
40 * @Short_description: A button which pops up a volume control
41 * @Title: GtkVolumeButton
43 * #GtkVolumeButton is a subclass of #GtkScaleButton that has
44 * been tailored for use as a volume control widget with suitable
45 * icons, tooltips and accessible labels.
48 #define EPSILON (1e-10)
50 static const gchar * const icons[] =
55 "audio-volume-medium",
59 static const gchar * const icons_symbolic[] =
61 "audio-volume-muted-symbolic",
62 "audio-volume-high-symbolic",
63 "audio-volume-low-symbolic",
64 "audio-volume-medium-symbolic",
74 static gboolean cb_query_tooltip (GtkWidget *button,
77 gboolean keyboard_mode,
80 static void cb_value_changed (GtkVolumeButton *button,
84 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
87 gtk_volume_button_set_property (GObject *object,
92 GtkScaleButton *button = GTK_SCALE_BUTTON (object);
97 if (g_value_get_boolean (value))
98 gtk_scale_button_set_icons (button, (const char **) icons_symbolic);
100 gtk_scale_button_set_icons (button, (const char **) icons);
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 gtk_volume_button_get_property (GObject *object,
116 case PROP_SYMBOLIC: {
119 g_object_get (object, "icons", &icon_list, NULL);
120 if (icon_list != NULL &&
121 icon_list[0] != NULL &&
122 g_str_equal (icon_list[0], icons_symbolic[0]))
123 g_value_set_boolean (value, TRUE);
125 g_value_set_boolean (value, FALSE);
126 g_strfreev (icon_list);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 gobject_class->set_property = gtk_volume_button_set_property;
141 gobject_class->get_property = gtk_volume_button_get_property;
144 * GtkVolumeButton:use-symbolic:
146 * Whether to use symbolic icons as the icons. Note that
147 * if the symbolic icons are not available in your installed
148 * theme, then the normal (potentially colorful) icons will
153 g_object_class_install_property (gobject_class,
155 g_param_spec_boolean ("use-symbolic",
156 P_("Use symbolic icons"),
157 P_("Whether to use symbolic icons"),
163 gtk_volume_button_init (GtkVolumeButton *button)
165 GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
167 GtkWidget *minus_button, *plus_button;
169 atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
171 atk_object_set_description (gtk_widget_get_accessible (GTK_WIDGET (button)),
172 _("Turns volume down or up"));
173 atk_action_set_description (ATK_ACTION (gtk_widget_get_accessible (GTK_WIDGET (button))),
175 _("Adjusts the volume"));
177 minus_button = gtk_scale_button_get_minus_button (sbutton);
178 plus_button = gtk_scale_button_get_plus_button (sbutton);
180 atk_object_set_name (gtk_widget_get_accessible (minus_button),
182 atk_object_set_description (gtk_widget_get_accessible (minus_button),
183 _("Decreases the volume"));
184 gtk_widget_set_tooltip_text (minus_button, _("Volume Down"));
186 atk_object_set_name (gtk_widget_get_accessible (plus_button),
188 atk_object_set_description (gtk_widget_get_accessible (plus_button),
189 _("Increases the volume"));
190 gtk_widget_set_tooltip_text (plus_button, _("Volume Up"));
192 gtk_scale_button_set_icons (sbutton, (const char **) icons);
194 adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);
195 g_object_set (G_OBJECT (button),
197 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
201 g_signal_connect (G_OBJECT (button), "query-tooltip",
202 G_CALLBACK (cb_query_tooltip), NULL);
203 g_signal_connect (G_OBJECT (button), "value-changed",
204 G_CALLBACK (cb_value_changed), NULL);
208 * gtk_volume_button_new
210 * Creates a #GtkVolumeButton, with a range between 0.0 and 1.0, with
211 * a stepping of 0.02. Volume values can be obtained and modified using
212 * the functions from #GtkScaleButton.
214 * Return value: a new #GtkVolumeButton
219 gtk_volume_button_new (void)
222 button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
223 return GTK_WIDGET (button);
227 cb_query_tooltip (GtkWidget *button,
230 gboolean keyboard_mode,
234 GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
235 GtkAdjustment *adjustment;
240 image = ATK_IMAGE (gtk_widget_get_accessible (button));
242 adjustment = gtk_scale_button_get_adjustment (scale_button);
243 val = gtk_scale_button_get_value (scale_button);
245 if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
247 str = g_strdup (_("Muted"));
249 else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
251 str = g_strdup (_("Full Volume"));
257 percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
259 /* Translators: this is the percentage of the current volume,
260 * as used in the tooltip, eg. "49 %".
261 * Translate the "%d" to "%Id" if you want to use localised digits,
262 * or otherwise translate the "%d" to "%d".
264 str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
267 gtk_tooltip_set_text (tooltip, str);
268 atk_image_set_image_description (image, str);
275 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
277 gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));