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