Support icon theme in volume & netstatus plug-ins.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
1 #include <gtk/gtk.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include <glib/gi18n.h>
7 #include <gdk-pixbuf/gdk-pixbuf.h>
8
9 #include "panel.h"
10 #include "misc.h"
11 #include "plugin.h"
12
13 #include "dbg.h"
14
15 #include "volume-impl.h"
16
17 #include "volume_xpm.h"
18
19 int mixer_fd;
20
21 typedef struct {
22 GtkWidget *mainw;
23 GtkWidget *dlg;
24 GtkTooltips* tooltips;
25 } volume_t;
26
27 static void
28 volume_destructor(plugin *p)
29 {
30 volume_t *vol = (volume_t *) p->priv;
31
32 ENTER;
33 if (vol->dlg)
34 gtk_widget_destroy(vol->dlg);
35 g_object_unref( vol->tooltips );
36 gtk_widget_destroy(vol->mainw);
37 if (mixer_fd)
38 close(mixer_fd);
39 g_free(vol);
40 RET();
41 }
42
43 static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, volume_t *vol)
44 {
45 /* FIXME: use smarter method */
46 gtk_widget_destroy( dlg );
47 vol->dlg = NULL;
48 }
49
50 static void on_button_press (GtkWidget* widget, GdkEventButton* evt, plugin* p)
51 {
52 volume_t *vol = (volume_t*) p->priv;
53
54 if( evt->button == 1 ) { /* Left click*/
55 if( ! vol->dlg ) {
56 vol->dlg = create_volume_window();
57 g_signal_connect( vol->dlg, "focus-out-event", on_volume_focus, vol );
58 }
59 gtk_window_present( GTK_WINDOW(vol->dlg) );
60 }
61 }
62
63 static int
64 volume_constructor(plugin *p)
65 {
66 volume_t *vol;
67 line s;
68 GdkPixbuf *icon;
69 GtkWidget *image;
70 GtkIconTheme* theme;
71 GtkIconInfo* info;
72
73 ENTER;
74 s.len = 256;
75 vol = g_new0(volume_t, 1);
76 g_return_val_if_fail(vol != NULL, 0);
77 p->priv = vol;
78
79 /* check if OSS mixer device could be open */
80 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
81 if (mixer_fd < 0) {
82 RET(0);
83 }
84
85 vol->mainw = gtk_event_box_new();
86
87 theme = gtk_icon_theme_get_default();
88 if( info = gtk_icon_theme_lookup_icon( theme, "stock_volume", 24, 0 ) )
89 {
90 icon = gdk_pixbuf_new_from_file_at_size( gtk_icon_info_get_filename( info ),
91 24, 24, NULL );
92 gtk_icon_info_free( info );
93 }
94 else
95 icon = gdk_pixbuf_new_from_xpm_data(volume_xpm);
96 if(icon) {
97 image = gtk_image_new_from_pixbuf(icon);
98 gtk_container_add (GTK_CONTAINER (vol->mainw), image);
99 }
100
101 gtk_widget_add_events( vol->mainw, GDK_BUTTON_PRESS_MASK );
102 g_signal_connect( vol->mainw, "button-press-event",
103 G_CALLBACK(on_button_press), p );
104 gtk_widget_set_size_request( vol->mainw, 24, 24 );
105
106 vol->dlg = NULL;
107 gtk_widget_show_all(vol->mainw);
108
109 gtk_container_add(GTK_CONTAINER(p->pwid), vol->mainw);
110
111 vol->tooltips = gtk_tooltips_new ();
112 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
113 gtk_tooltips_set_tip (vol->tooltips, vol->mainw, _("Volume control"), NULL);
114
115 RET(1);
116
117 error:
118 volume_destructor(p);
119 RET(0);
120 }
121
122
123 plugin_class volume_plugin_class = {
124 fname: NULL,
125 count: 0,
126
127 type : "volume",
128 name : "volume",
129 version: "1.0",
130 description : "Display and control volume",
131
132 constructor : volume_constructor,
133 destructor : volume_destructor,
134 };