wmshutdown: Destroy dialog window before shutting down. This is especially useful...
[dockapps.git] / wmradio / gnome_applet_envelope.c
blob8b47b3c34dd8bc9ff78d8a9b10d945755fd555c6
1 /*
2 * Copyright (C) 12 Jun 2003 Tomas Cermak
4 * This file is part of wmradio program.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <gnome.h>
22 #include <panel-applet.h>
23 #include <gtk/gtk.h>
24 #include <gdk/gdk.h>
25 #include <gdk/gdkprivate.h>
26 #include <gdk/gdkx.h>
28 #include "gnome_applet_envelope.h"
29 #include "skin.h"
30 #include "wmradio.h"
31 #include "rc.h"
33 GtkWidget *radio;
34 Window buffer;
35 GC NormalGC;
36 unsigned long gcm;
37 XGCValues gcv;
38 guint timer;
40 void logt(char *s)
42 FILE *f;
44 f = fopen("/tmp/wmradio.log","a");
45 fputs(s,f);
46 fputs("\n",f);
47 fclose(f);
50 Pixel GetColor(char *ColorName, Display * disp, Window win)
52 XColor Color;
53 XWindowAttributes Attributes;
55 XGetWindowAttributes(disp, win, &Attributes);
56 Color.pixel = 0;
58 if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
59 printf("wmradio: can't parse %s\n", ColorName);
60 else if (!XAllocColor(disp, Attributes.colormap, &Color))
61 printf("wmradio: can't allocate %s\n", ColorName);
63 return Color.pixel;
67 void gtk_drawing_area_copy_from_X11_drawable(GtkDrawingArea *DA,
68 Window X11,
69 GC gc,
70 int srcx,int srcy,
71 unsigned int width, unsigned int height,
72 int destx, int desty)
74 GdkWindow *gdkwindow;
76 g_return_if_fail( DA != NULL );
77 g_return_if_fail( X11 != 0 );
79 gdkwindow = GDK_WINDOW(GTK_WIDGET(DA)->window);
80 if(!GDK_WINDOW_DESTROYED( gdkwindow ) ) {
81 XCopyArea(GDK_DISPLAY(),
82 X11,
83 GDK_WINDOW_XWINDOW(gdkwindow),
84 gc,
85 srcx,srcy,width,height,destx,desty);
89 gint on_expose(GtkWidget *w, GdkEventExpose *e)
91 RadioEvent re;
93 re.type = REVENT_EXPOSE;
94 wmradio_handle_event(&re);
95 return TRUE;
98 gint on_button_press(GtkWidget *w, GdkEventButton *e)
100 RadioEvent re;
102 re.type = REVENT_BUTTON_PRESS;
103 re.x = e->x;
104 re.y = e->y;
105 re.control = e->state & GDK_CONTROL_MASK ?
106 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
107 re.shift = e->state & GDK_SHIFT_MASK ?
108 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
109 wmradio_handle_event(&re);
110 if( e->button & 2 ) return FALSE;
111 return TRUE;
114 gint on_button_release(GtkWidget *w, GdkEventButton *e)
116 RadioEvent re;
118 re.type = REVENT_BUTTON_RELEASE;
119 re.x = e->x;
120 re.y = e->y;
121 re.control = e->state & GDK_CONTROL_MASK ?
122 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
123 re.shift = e->state & GDK_SHIFT_MASK ?
124 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
125 wmradio_handle_event(&re);
126 if( e->button & 2 ) return FALSE;
127 return TRUE;
130 gint on_scroll(GtkWidget *w, GdkEventScroll *e)
132 RadioEvent re;
134 switch(e->direction) {
135 case GDK_SCROLL_UP:
136 re.type = REVENT_SCROLL_UP;
137 break;
138 case GDK_SCROLL_DOWN:
139 re.type = REVENT_SCROLL_DOWN;
140 break;
141 default:
142 return FALSE;
144 re.x = e->x;
145 re.y = e->y;
146 re.control = e->state & GDK_CONTROL_MASK ?
147 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
148 re.shift = e->state & GDK_SHIFT_MASK ?
149 CONTROL_STATE_PRESSED : CONTROL_STATE_NOT_PRESSED;
150 wmradio_handle_event(&re);
151 return TRUE;
154 gint on_timer(gpointer data)
156 RadioEvent re;
158 re.type = REVENT_TIMER;
159 wmradio_handle_event(&re);
160 return TRUE;
163 void on_realize(GtkWidget *widget, gpointer not_used)
165 Pixel foreground,background;
166 int screen;
168 wmradio_init_radio_info();
169 rc_read_config();
170 create_skin(rc_get_variable(SECTION_CONFIG,"skin","gdefault.skin"),
171 GDK_DISPLAY(),
172 GDK_ROOT_WINDOW());
173 gtk_drawing_area_size(GTK_DRAWING_AREA(radio), skin_width(),skin_height());
174 wmradio_radio_info()->dont_quit_mode = 1;
175 rc_set_variable_as_int(SECTION_CONFIG,"start-muted",1);
176 wmradio_init();
178 background = GetColor("black", GDK_DISPLAY(), GDK_ROOT_WINDOW());
179 foreground = GetColor("white", GDK_DISPLAY(), GDK_ROOT_WINDOW());
180 gcm = GCForeground | GCBackground | GCGraphicsExposures;
181 gcv.foreground = foreground;
182 gcv.background = background;
183 gcv.graphics_exposures = 0;
184 NormalGC = XCreateGC(GDK_DISPLAY(), GDK_ROOT_WINDOW(), gcm, &gcv);
185 screen = DefaultScreen(GDK_DISPLAY());
186 buffer = XCreatePixmap(GDK_DISPLAY(),
187 GDK_ROOT_WINDOW(),
188 skin_width(),skin_height(),
189 DefaultDepth(GDK_DISPLAY(),screen)/*16 color_depth */);
190 timer = gtk_timeout_add(1000,on_timer,NULL);
193 GtkWidget *video_init(void)
195 radio = gtk_drawing_area_new();
196 gtk_signal_connect(GTK_OBJECT(radio),"expose-event",
197 GTK_SIGNAL_FUNC(on_expose),NULL);
198 gtk_signal_connect(GTK_OBJECT(radio),"button-press-event",
199 GTK_SIGNAL_FUNC(on_button_press),NULL);
200 gtk_signal_connect(GTK_OBJECT(radio),"button-release-event",
201 GTK_SIGNAL_FUNC(on_button_release),NULL);
202 gtk_signal_connect(GTK_OBJECT(radio),"realize",
203 GTK_SIGNAL_FUNC(on_realize),NULL);
204 gtk_signal_connect(GTK_OBJECT(radio),"scroll-event",
205 GTK_SIGNAL_FUNC(on_scroll),NULL);
206 gtk_widget_set_events(radio,
207 GDK_EXPOSURE_MASK
208 | GDK_BUTTON_PRESS_MASK
209 | GDK_POINTER_MOTION_MASK
210 | GDK_BUTTON_RELEASE_MASK
211 | GDK_SCROLL_MASK
213 return radio;
216 void video_mainloop(void)
218 /*applet_widget_gtk_main();*/
219 gtk_timeout_remove(timer);
222 void video_close(void)
224 /*applet_widget_remove(APPLET_WIDGET(applet));*/
225 /*applet_widget_gtk_main_quit();*/
228 void video_draw(float freq,int stereo)
230 skin_to_window(GDK_DISPLAY(),buffer, NormalGC,freq,stereo);
231 gtk_drawing_area_copy_from_X11_drawable(
232 GTK_DRAWING_AREA(radio),
233 buffer,
234 NormalGC,
235 0,0,skin_width(),skin_height(),0,0);
238 static gboolean wmradio_applet_fill(PanelApplet *applet,
239 const gchar *iid,
240 gpointer data)
242 if (strcmp (iid, "OAFIID:WMRadioApplet") != 0) return FALSE;
244 video_init();
245 gtk_container_add (GTK_CONTAINER (applet), radio);
246 gtk_widget_show_all (GTK_WIDGET (applet));
247 return TRUE;
251 PANEL_APPLET_BONOBO_FACTORY ("OAFIID:WMRadioApplet_Factory",
252 PANEL_TYPE_APPLET,
253 "The WMRadio Applet",
254 "0",
255 wmradio_applet_fill,
256 NULL);
258 /* int main(int argc, char *argv[]) */
259 /* { */
260 /* GtkWidget *win; */
262 /* gtk_set_locale(); */
263 /* gtk_init(&argc,&argv); */
265 /* win = gtk_window_new(GTK_WINDOW_TOPLEVEL); */
266 /* radio = video_init(); */
267 /* gtk_container_add (GTK_CONTAINER (win), radio); */
268 /* gtk_widget_show_all(win); */
269 /* gtk_main(); */
270 /* return 0; */
271 /* } */