]> Pileus Git - ~andy/gtk/blob - gtk/gtkvolumebutton.c
Change FSF Address
[~andy/gtk] / gtk / gtkvolumebutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2007 Red Hat, Inc.
3  *
4  * Authors:
5  * - Bastien Nocera <bnocera@redhat.com>
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 /*
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/.
26  */
27
28 #include "config.h"
29
30 #include "gtkvolumebutton.h"
31 #include "gtkstock.h"
32 #include "gtktooltip.h"
33 #include "gtkintl.h"
34
35
36 /**
37  * SECTION:gtkvolumebutton
38  * @Short_description: A button which pops up a volume control
39  * @Title: GtkVolumeButton
40  *
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.
44  */
45
46 #define EPSILON (1e-10)
47
48 static const gchar * const icons[] =
49 {
50   "audio-volume-muted",
51   "audio-volume-high",
52   "audio-volume-low",
53   "audio-volume-medium",
54   NULL
55 };
56
57 static const gchar * const icons_symbolic[] =
58 {
59   "audio-volume-muted-symbolic",
60   "audio-volume-high-symbolic",
61   "audio-volume-low-symbolic",
62   "audio-volume-medium-symbolic",
63   NULL
64 };
65
66 enum
67 {
68   PROP_0,
69   PROP_SYMBOLIC
70 };
71
72 static gboolean cb_query_tooltip (GtkWidget       *button,
73                                   gint             x,
74                                   gint             y,
75                                   gboolean         keyboard_mode,
76                                   GtkTooltip      *tooltip,
77                                   gpointer         user_data);
78 static void     cb_value_changed (GtkVolumeButton *button,
79                                   gdouble          value,
80                                   gpointer         user_data);
81
82 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
83
84 static void
85 gtk_volume_button_set_property (GObject       *object,
86                                 guint          prop_id,
87                                 const GValue  *value,
88                                 GParamSpec    *pspec)
89 {
90   GtkScaleButton *button = GTK_SCALE_BUTTON (object);
91
92   switch (prop_id)
93     {
94     case PROP_SYMBOLIC:
95       if (g_value_get_boolean (value))
96         gtk_scale_button_set_icons (button, (const char **) icons_symbolic);
97       else
98         gtk_scale_button_set_icons (button, (const char **) icons);
99       break;
100     default:
101       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102       break;
103     }
104 }
105
106 static void
107 gtk_volume_button_get_property (GObject     *object,
108                                 guint        prop_id,
109                                 GValue      *value,
110                                 GParamSpec  *pspec)
111 {
112   switch (prop_id)
113     {
114     case PROP_SYMBOLIC: {
115       char **icon_list;
116
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);
122       else
123         g_value_set_boolean (value, FALSE);
124       g_strfreev (icon_list);
125       break;
126     }
127     default:
128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129       break;
130     }
131 }
132
133 static void
134 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
135 {
136   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137
138   gobject_class->set_property = gtk_volume_button_set_property;
139   gobject_class->get_property = gtk_volume_button_get_property;
140
141   /**
142    * GtkVolumeButton:use-symbolic:
143    *
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
147    * be used.
148    *
149    * Since: 3.0
150    */
151   g_object_class_install_property (gobject_class,
152                                    PROP_SYMBOLIC,
153                                    g_param_spec_boolean ("use-symbolic",
154                                                          P_("Use symbolic icons"),
155                                                          P_("Whether to use symbolic icons"),
156                                                          FALSE,
157                                                          G_PARAM_READWRITE));
158 }
159
160 static void
161 gtk_volume_button_init (GtkVolumeButton *button)
162 {
163   GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
164   GtkAdjustment *adj;
165   GtkWidget *minus_button, *plus_button;
166
167   atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
168                        _("Volume"));
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))),
172                               1,
173                               _("Adjusts the volume"));
174
175   minus_button = gtk_scale_button_get_minus_button (sbutton);
176   plus_button = gtk_scale_button_get_plus_button (sbutton);
177
178   atk_object_set_name (gtk_widget_get_accessible (minus_button),
179                        _("Volume Down"));
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"));
183
184   atk_object_set_name (gtk_widget_get_accessible (plus_button),
185                        _("Volume Up"));
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"));
189
190   gtk_scale_button_set_icons (sbutton, (const char **) icons);
191
192   adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);
193   g_object_set (G_OBJECT (button),
194                 "adjustment", adj,
195                 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
196                 "has-tooltip", TRUE,
197                 NULL);
198
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);
203 }
204
205 /**
206  * gtk_volume_button_new
207  *
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.
211  *
212  * Return value: a new #GtkVolumeButton
213  *
214  * Since: 2.12
215  */
216 GtkWidget *
217 gtk_volume_button_new (void)
218 {
219   GObject *button;
220   button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
221   return GTK_WIDGET (button);
222 }
223
224 static gboolean
225 cb_query_tooltip (GtkWidget  *button,
226                   gint        x,
227                   gint        y,
228                   gboolean    keyboard_mode,
229                   GtkTooltip *tooltip,
230                   gpointer    user_data)
231 {
232   GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
233   GtkAdjustment *adjustment;
234   gdouble val;
235   char *str;
236   AtkImage *image;
237
238   image = ATK_IMAGE (gtk_widget_get_accessible (button));
239
240   adjustment = gtk_scale_button_get_adjustment (scale_button);
241   val = gtk_scale_button_get_value (scale_button);
242
243   if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
244     {
245       str = g_strdup (_("Muted"));
246     }
247   else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
248     {
249       str = g_strdup (_("Full Volume"));
250     }
251   else
252     {
253       int percent;
254
255       percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
256
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".
261        */
262       str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
263     }
264
265   gtk_tooltip_set_text (tooltip, str);
266   atk_image_set_image_description (image, str);
267   g_free (str);
268
269   return TRUE;
270 }
271
272 static void
273 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
274 {
275   gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));
276 }