]> Pileus Git - ~andy/gtk/blob - gtk/gtkvolumebutton.c
Add the GtkVolumeButton widget, a button that pops up a scale when clicked
[~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 <glib/gi18n.h>
33 #include <atk/atk.h>
34
35 #include "gtkvolumebutton.h"
36 #include "gtktooltips.h"
37 #include "gtkstock.h"
38
39 struct _GtkVolumeButton
40 {
41   GtkScaleButton  parent;
42   GtkTooltips    *tooltips;
43 };
44
45 static void     gtk_volume_button_class_init    (GtkVolumeButtonClass *klass);
46 static void     gtk_volume_button_init          (GtkVolumeButton      *button);
47 static void     gtk_volume_button_dispose       (GObject *object);
48 static void     gtk_volume_button_update_tooltip(GtkVolumeButton *button);
49 static void     cb_value_changed                (GtkVolumeButton      *button,
50                                                  gdouble               value,
51                                                  gpointer              user_data);
52
53 static GtkScaleButtonClass *parent_class = NULL;
54
55 G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
56
57 static void
58 gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
59 {
60   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
61
62   parent_class = g_type_class_peek_parent (klass);
63   gobject_class->dispose = gtk_volume_button_dispose;
64 }
65
66 static void
67 gtk_volume_button_init (GtkVolumeButton *button)
68 {
69   GtkScaleButton *sbutton = GTK_SCALE_BUTTON (button);
70   GtkObject *adj;
71   const char *icons[] = {
72         "audio-volume-muted",
73         "audio-volume-high",
74         "audio-volume-low",
75         "audio-volume-medium",
76         NULL
77   };
78
79   atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (button)),
80                        _("Volume"));
81   atk_object_set_name (gtk_widget_get_accessible (sbutton->minus_button),
82                        _("Volume Down"));
83   atk_object_set_name (gtk_widget_get_accessible (sbutton->plus_button),
84                        _("Volume Up"));
85
86   gtk_scale_button_set_icons (sbutton, icons);
87
88   adj = gtk_adjustment_new (0, 0, 100, 2, 10 * 2, 0);
89   g_object_set (G_OBJECT (button),
90                 "adjustment", adj,
91                 "size", GTK_ICON_SIZE_SMALL_TOOLBAR,
92                 NULL);
93
94   button->tooltips = gtk_tooltips_new ();
95   g_object_ref_sink (button->tooltips);
96   gtk_volume_button_update_tooltip (button);
97
98   g_signal_connect (G_OBJECT (button), "value-changed",
99                     G_CALLBACK (cb_value_changed), NULL);
100 }
101
102 static void
103 gtk_volume_button_dispose (GObject *object)
104 {
105   GtkVolumeButton *button;
106
107   button = GTK_VOLUME_BUTTON (object);
108
109   if (button->tooltips)
110     g_object_unref (button->tooltips);
111   button->tooltips = NULL;
112
113   G_OBJECT_CLASS (parent_class)->dispose (object);
114 }
115
116 /**
117  * gtk_volume_button_new
118  *
119  * Creates a #GtkVolumeButton, with a range between 0 and 100, with
120  * a stepping of 2. Volume values can be obtained and modified using
121  * the functions from #GtkScaleButton.
122  *
123  * Return value: a new #GtkVolumeButton
124  *
125  * Since: 2.12
126  */
127 GtkWidget *
128 gtk_volume_button_new (void)
129 {
130   GObject *button;
131   button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
132   return GTK_WIDGET (button);
133 }
134
135 static void
136 gtk_volume_button_update_tooltip (GtkVolumeButton *button)
137 {
138   gdouble val;
139   char *str;
140
141   val = gtk_scale_button_get_value (GTK_SCALE_BUTTON (button));
142
143   if (val == 0.0)
144     {
145       str = g_strdup (_("Muted"));
146     }
147   else if (val == 100.0)
148     {
149       str = g_strdup (_("Full Volume"));
150     }
151   else
152     {
153       int percent;
154
155       percent = (int) val;
156       /* translators, this is the percentage of the current volume,
157        * as used in the tooltip, eg. "49 %"
158        * do not translate the part before the | */
159       str = g_strdup_printf (Q_("volume percentage|%d %%"), percent);
160     }
161
162   gtk_tooltips_set_tip (button->tooltips,
163                         GTK_WIDGET (button),
164                         str, NULL);
165   g_free (str);
166 }
167
168 static void
169 cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
170 {
171   gtk_volume_button_update_tooltip (button);
172 }
173
174 /*
175  * vim: sw=2 ts=8 cindent noai bs=2
176  */