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