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