Use gtk_widget_destroy() instead of gtk_container_remove() if possible.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
1 /**
2 * Copyright (c) 2006, 2008 LxDE Developers,
3 * see the file AUTHORS for details.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <gtk/gtk.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include <glib/gi18n.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 #include "plugin.h"
29
30 #include "dbg.h"
31
32 #include "volume-impl.h"
33
34 #include "volume_xpm.h"
35
36 int mixer_fd;
37
38 GtkSpinButton *vol_spin;
39 static gdouble vol_before_mute;
40 static gdouble curr_volume;
41 static GtkWidget *curr_image;
42 static gboolean skip_botton1_event;
43
44 typedef struct {
45 Panel *panel;
46 GtkWidget *dlg;
47 } volume_t;
48
49 static void
50 volume_destructor(gpointer user_data)
51 {
52 volume_t *vol = (volume_t *) user_data;
53
54 ENTER;
55 if (vol->dlg)
56 gtk_widget_destroy(vol->dlg);
57 if (mixer_fd)
58 close(mixer_fd);
59 g_free(vol);
60 RET();
61 }
62
63 static void update_icon (GtkWidget *p, volume_t *vol)
64 {
65 GdkPixbuf *icon;
66 GtkWidget *image;
67 GtkIconTheme* theme;
68 GtkIconInfo* info;
69 int icon_size;
70
71 theme = panel_get_icon_theme(vol->panel);
72 icon_size = panel_get_icon_size(vol->panel);
73
74 if (curr_volume <= 0) {
75 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-mute", icon_size, 0 );
76 }
77 else if (curr_volume > 0 && curr_volume <= 50) {
78 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-min", icon_size, 0 );
79 }
80 else if (curr_volume > 50 && curr_volume <= 75) {
81 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-med", icon_size, 0 );
82 }
83 else /* curr_volume > 75 */ {
84 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-max", icon_size, 0 );
85 }
86
87 if (info ) {
88 icon = gdk_pixbuf_new_from_file_at_size(
89 gtk_icon_info_get_filename( info ),
90 icon_size, icon_size, NULL );
91 gtk_icon_info_free( info );
92 }
93 else {
94 icon = gdk_pixbuf_new_from_xpm_data((const char **) volume_xpm);
95 }
96
97 if (icon) {
98 if (curr_image) {
99 gtk_widget_destroy(curr_image);
100 curr_image = NULL;
101 }
102 image = gtk_image_new_from_pixbuf(icon);
103 gtk_container_add(GTK_CONTAINER(p), image);
104
105 curr_image = image;
106 }
107 gtk_widget_show_all(p);
108 return;
109 }
110
111 static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, GtkWidget *p)
112 {
113 volume_t *vol = lxpanel_plugin_get_data(p);
114
115 if (! vol_spin) return;
116 GtkAdjustment *vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
117 if (! vol_adjustment) return;
118 curr_volume = gtk_adjustment_get_value (vol_adjustment);
119
120 update_icon(p, vol);
121
122 /* FIXME: use smarter method */
123 gtk_widget_destroy( dlg );
124 vol->dlg = NULL;
125 }
126
127 static void on_mouse_scroll (GtkWidget* widget, GdkEventScroll* evt, volume_t *vol)
128 {
129 if ( ! vol->dlg ) {
130
131 vol->dlg = create_volume_window();
132 g_signal_connect( vol->dlg, "delete-event",
133 G_CALLBACK(on_volume_focus), widget );
134
135 }
136 else {
137 if (! vol_spin) return;
138 GtkAdjustment *vol_adjustment =
139 gtk_spin_button_get_adjustment (vol_spin);
140 if (! vol_adjustment) return;
141
142 curr_volume = gtk_adjustment_get_value (vol_adjustment);
143
144 if (evt->direction == GDK_SCROLL_UP) {
145 curr_volume += 2;
146 }
147 else /*if (evt->direction == GDK_SCROLL_DOWN)*/ {
148 curr_volume -= 2;
149 }
150 update_icon(widget, vol);
151 gtk_adjustment_set_value (vol_adjustment, curr_volume);
152 gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
153 skip_botton1_event = TRUE;
154 }
155 }
156
157 static gboolean on_button_press (GtkWidget* widget, GdkEventButton* evt, Panel* p)
158 {
159 volume_t *vol = lxpanel_plugin_get_data(widget);
160
161 /* for scroll correction */
162 if (skip_botton1_event) {
163 gtk_widget_destroy(vol->dlg);
164 vol->dlg = NULL;
165 skip_botton1_event = FALSE;
166 }
167
168 switch ( evt->button ) {
169 case 1: { /* Left click */
170 if ( ! vol->dlg ) {
171 vol->dlg = create_volume_window();
172
173 /* setting background to default */
174 gtk_widget_set_style(vol->dlg, panel_get_defstyle(p));
175
176 g_signal_connect( vol->dlg, "focus-out-event",
177 G_CALLBACK(on_volume_focus), widget );
178
179 gtk_window_present( GTK_WINDOW(vol->dlg) );
180 }
181 else {
182 /* update icon */
183 if (! vol_spin) return FALSE;
184 GtkAdjustment *vol_adjustment =
185 gtk_spin_button_get_adjustment (vol_spin);
186 if (! vol_adjustment) return FALSE;
187 curr_volume = gtk_adjustment_get_value (vol_adjustment);
188 update_icon(widget, vol);
189
190 gtk_widget_destroy(vol->dlg);
191 vol->dlg = NULL;
192 }
193 break;
194 }
195
196 case 3: { /* right button */
197 lxpanel_plugin_button_press_event(widget, evt, p);
198 return TRUE;
199 }
200
201 case 2: { /* middle mouse button */
202 if ( ! vol->dlg ) {
203 vol->dlg = create_volume_window();
204 }
205
206 if (! vol_spin) return FALSE;
207 GtkAdjustment *vol_adjustment =
208 gtk_spin_button_get_adjustment (vol_spin);
209 if (! vol_adjustment) return FALSE;
210
211 curr_volume = gtk_adjustment_get_value (vol_adjustment);
212
213 if (curr_volume > 0) {
214 /* turning to mute */
215 vol_before_mute = curr_volume;
216 curr_volume = 0;
217 }
218 else {
219 curr_volume = vol_before_mute;
220 }
221
222 gtk_adjustment_set_value (vol_adjustment, curr_volume);
223 gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
224
225 update_icon(widget, vol);
226
227 gtk_widget_destroy( vol->dlg );
228 vol->dlg = NULL;
229 break;
230 }
231 default: /* never here */
232 break;
233 }
234 return FALSE;
235 }
236
237 static GtkWidget *volume_constructor(Panel *panel, config_setting_t *settings)
238 {
239 volume_t *vol;
240 GtkWidget *p;
241 GtkAdjustment *vol_adjustment;
242
243 vol_before_mute = 1;
244 curr_volume = 0;
245 curr_image = NULL;
246 skip_botton1_event = FALSE;
247
248 ENTER;
249 /* check if OSS mixer device could be open */
250 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
251 if (mixer_fd < 0) {
252 RET(NULL);
253 }
254 /* try to obtain current volume */
255 p = create_volume_window(); /* use pointer */
256 if (! vol_spin)
257 goto _error;
258 vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
259 if (! vol_adjustment)
260 {
261 _error:
262 gtk_widget_destroy(p);
263 RET(NULL);
264 }
265 curr_volume = gtk_adjustment_get_value (vol_adjustment);
266
267 vol = g_new0(volume_t, 1);
268 vol->dlg = p; /* it was reused */
269
270 p = gtk_event_box_new();
271 lxpanel_plugin_set_data(p, vol, volume_destructor);
272 vol->panel = panel;
273
274 gtk_widget_add_events(p, GDK_BUTTON_PRESS_MASK);
275 g_signal_connect(p, "scroll-event", G_CALLBACK(on_mouse_scroll), vol);
276 gtk_widget_set_size_request(p, panel_get_icon_size(panel), panel_get_icon_size(panel));
277
278 update_icon(p, vol);
279 gtk_widget_destroy( vol->dlg );
280 vol->dlg = NULL;
281
282 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
283 gtk_widget_set_tooltip_text(p, _("Volume control"));
284
285 RET(p);
286 }
287
288 FM_DEFINE_MODULE(lxpanel_gtk, volume)
289
290 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
291 .name = N_("Volume Control"),
292 .description = "Display and control volume",
293
294 .new_instance = volume_constructor,
295 .button_press_event = on_button_press
296 };