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