]> Pileus Git - ~andy/gtk/blob - gtk/gtkvolumebutton.c
Use accessor functions to access GtkScaleButton
[~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
51 static gboolean cb_query_tooltip (GtkWidget       *button,
52                                   gint             x,
53                                   gint             y,
54                                   gboolean         keyboard_mode,
55                                   GtkTooltip      *tooltip,
56                                   gpointer         user_data);
57 static void     cb_value_changed (GtkVolumeButton *button,
58                                   gdouble          value,
59                                   gpointer         user_data);
60
61 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
62
63 static void
64 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
65 {
66 }
67
68 static void
69 gtk_volume_button_init (GtkVolumeButton *button)
70 {
71   GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
72   GtkObject *adj;
73   GtkWidget *minus_button, *plus_button;
74   const char *icons[] = {
75         "audio-volume-muted",
76         "audio-volume-high",
77         "audio-volume-low",
78         "audio-volume-medium",
79         NULL
80   };
81
82   atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
83                        _("Volume"));
84   atk_object_set_description (gtk_widget_get_accessible (GTK_WIDGET (button)),
85                        _("Turns volume down or up"));
86   atk_action_set_description (ATK_ACTION (gtk_widget_get_accessible (GTK_WIDGET (button))),
87                               1,
88                               _("Adjusts the volume"));
89
90   minus_button = gtk_scale_button_get_minus_button (sbutton);
91   plus_button = gtk_scale_button_get_plus_button (sbutton);
92
93   atk_object_set_name (gtk_widget_get_accessible (minus_button),
94                        _("Volume Down"));
95   atk_object_set_description (gtk_widget_get_accessible (minus_button),
96                        _("Decreases the volume"));
97   gtk_widget_set_tooltip_text (minus_button, _("Volume Down"));
98
99   atk_object_set_name (gtk_widget_get_accessible (plus_button),
100                        _("Volume Up"));
101   atk_object_set_description (gtk_widget_get_accessible (plus_button),
102                        _("Increases the volume"));
103   gtk_widget_set_tooltip_text (plus_button, _("Volume Up"));
104
105   gtk_scale_button_set_icons (sbutton, icons);
106
107   adj = gtk_adjustment_new (0., 0., 1., 0.02, 0.2, 0.);
108   g_object_set (G_OBJECT (button),
109                 "adjustment", adj,
110                 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
111                 "has-tooltip", TRUE,
112                 NULL);
113
114   g_signal_connect (G_OBJECT (button), "query-tooltip",
115                     G_CALLBACK (cb_query_tooltip), NULL);
116   g_signal_connect (G_OBJECT (button), "value-changed",
117                     G_CALLBACK (cb_value_changed), NULL);
118 }
119
120 /**
121  * gtk_volume_button_new
122  *
123  * Creates a #GtkVolumeButton, with a range between 0.0 and 1.0, with
124  * a stepping of 0.02. Volume values can be obtained and modified using
125  * the functions from #GtkScaleButton.
126  *
127  * Return value: a new #GtkVolumeButton
128  *
129  * Since: 2.12
130  */
131 GtkWidget *
132 gtk_volume_button_new (void)
133 {
134   GObject *button;
135   button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
136   return GTK_WIDGET (button);
137 }
138
139 static gboolean
140 cb_query_tooltip (GtkWidget  *button,
141                   gint        x,
142                   gint        y,
143                   gboolean    keyboard_mode,
144                   GtkTooltip *tooltip,
145                   gpointer    user_data)
146 {
147   GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
148   GtkAdjustment *adj;
149   gdouble val;
150   char *str;
151   AtkImage *image;
152
153   image = ATK_IMAGE (gtk_widget_get_accessible (button));
154
155   adj = gtk_scale_button_get_adjustment (scale_button);
156   val = gtk_scale_button_get_value (scale_button);
157
158   if (val < (adj->lower + EPSILON))
159     {
160       str = g_strdup (_("Muted"));
161     }
162   else if (val >= (adj->upper - EPSILON))
163     {
164       str = g_strdup (_("Full Volume"));
165     }
166   else
167     {
168       int percent;
169
170       percent = (int) (100. * val / (adj->upper - adj->lower) + .5);
171
172       /* Translators: this is the percentage of the current volume,
173        * as used in the tooltip, eg. "49 %".
174        * Translate the "%d" to "%Id" if you want to use localised digits,
175        * or otherwise translate the "%d" to "%d".
176        */
177       str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
178     }
179
180   gtk_tooltip_set_text (tooltip, str);
181   atk_image_set_image_description (image, str);
182   g_free (str);
183
184   return TRUE;
185 }
186
187 static void
188 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
189 {
190   gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));
191 }