Mercurial > audlegacy
comparison src/audacious/ui_skinned_equalizer_graph.c @ 3101:b25db04ee2a1
replace EqGraph with UiSkinnedEqualizerGraph
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Thu, 19 Jul 2007 13:06:41 +0200 |
parents | |
children | 1faf842dea49 |
comparison
equal
deleted
inserted
replaced
3100:38ca234df0d9 | 3101:b25db04ee2a1 |
---|---|
1 /* | |
2 * Audacious - a cross-platform multimedia player | |
3 * Copyright (c) 2007 Audacious development team. | |
4 * | |
5 * Based on: | |
6 * BMP - Cross-platform multimedia player | |
7 * Copyright (C) 2003-2004 BMP development team. | |
8 * XMMS: | |
9 * Copyright (C) 1998-2003 XMMS development team. | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; under version 2 of the License. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
23 */ | |
24 | |
25 #include "widgets/widgetcore.h" | |
26 #include "ui_skinned_equalizer_graph.h" | |
27 #include "main.h" | |
28 #include "util.h" | |
29 #include <gtk/gtkmain.h> | |
30 #include <gtk/gtkmarshal.h> | |
31 #include <gtk/gtkimage.h> | |
32 | |
33 #define UI_TYPE_SKINNED_EQUALIZER_GRAPH (ui_skinned_equalizer_graph_get_type()) | |
34 | |
35 enum { | |
36 DOUBLED, | |
37 LAST_SIGNAL | |
38 }; | |
39 | |
40 static void ui_skinned_equalizer_graph_class_init (UiSkinnedEqualizerGraphClass *klass); | |
41 static void ui_skinned_equalizer_graph_init (UiSkinnedEqualizerGraph *equalizer_graph); | |
42 static void ui_skinned_equalizer_graph_destroy (GtkObject *object); | |
43 static void ui_skinned_equalizer_graph_realize (GtkWidget *widget); | |
44 static void ui_skinned_equalizer_graph_size_request (GtkWidget *widget, GtkRequisition *requisition); | |
45 static void ui_skinned_equalizer_graph_size_allocate (GtkWidget *widget, GtkAllocation *allocation); | |
46 static gboolean ui_skinned_equalizer_graph_expose (GtkWidget *widget, GdkEventExpose *event); | |
47 static void ui_skinned_equalizer_graph_toggle_doublesize (UiSkinnedEqualizerGraph *equalizer_graph); | |
48 | |
49 static GtkWidgetClass *parent_class = NULL; | |
50 static guint equalizer_graph_signals[LAST_SIGNAL] = { 0 }; | |
51 | |
52 GType ui_skinned_equalizer_graph_get_type() { | |
53 static GType equalizer_graph_type = 0; | |
54 if (!equalizer_graph_type) { | |
55 static const GTypeInfo equalizer_graph_info = { | |
56 sizeof (UiSkinnedEqualizerGraphClass), | |
57 NULL, | |
58 NULL, | |
59 (GClassInitFunc) ui_skinned_equalizer_graph_class_init, | |
60 NULL, | |
61 NULL, | |
62 sizeof (UiSkinnedEqualizerGraph), | |
63 0, | |
64 (GInstanceInitFunc) ui_skinned_equalizer_graph_init, | |
65 }; | |
66 equalizer_graph_type = g_type_register_static (GTK_TYPE_WIDGET, "UiSkinnedEqualizerGraph", &equalizer_graph_info, 0); | |
67 } | |
68 | |
69 return equalizer_graph_type; | |
70 } | |
71 | |
72 static void ui_skinned_equalizer_graph_class_init(UiSkinnedEqualizerGraphClass *klass) { | |
73 GObjectClass *gobject_class; | |
74 GtkObjectClass *object_class; | |
75 GtkWidgetClass *widget_class; | |
76 | |
77 gobject_class = G_OBJECT_CLASS(klass); | |
78 object_class = (GtkObjectClass*) klass; | |
79 widget_class = (GtkWidgetClass*) klass; | |
80 parent_class = gtk_type_class (gtk_widget_get_type ()); | |
81 | |
82 object_class->destroy = ui_skinned_equalizer_graph_destroy; | |
83 | |
84 widget_class->realize = ui_skinned_equalizer_graph_realize; | |
85 widget_class->expose_event = ui_skinned_equalizer_graph_expose; | |
86 widget_class->size_request = ui_skinned_equalizer_graph_size_request; | |
87 widget_class->size_allocate = ui_skinned_equalizer_graph_size_allocate; | |
88 | |
89 klass->doubled = ui_skinned_equalizer_graph_toggle_doublesize; | |
90 | |
91 equalizer_graph_signals[DOUBLED] = | |
92 g_signal_new ("toggle-double-size", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
93 G_STRUCT_OFFSET (UiSkinnedEqualizerGraphClass, doubled), NULL, NULL, | |
94 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
95 } | |
96 | |
97 static void ui_skinned_equalizer_graph_init(UiSkinnedEqualizerGraph *equalizer_graph) { | |
98 equalizer_graph->width = 113; | |
99 equalizer_graph->height = 19; | |
100 } | |
101 | |
102 GtkWidget* ui_skinned_equalizer_graph_new(GtkWidget *fixed, gint x, gint y) { | |
103 UiSkinnedEqualizerGraph *equalizer_graph = g_object_new (ui_skinned_equalizer_graph_get_type (), NULL); | |
104 | |
105 equalizer_graph->x = x; | |
106 equalizer_graph->y = y; | |
107 equalizer_graph->skin_index = SKIN_EQMAIN; | |
108 equalizer_graph->fixed = fixed; | |
109 equalizer_graph->double_size = FALSE; | |
110 | |
111 gtk_fixed_put(GTK_FIXED(equalizer_graph->fixed), GTK_WIDGET(equalizer_graph), equalizer_graph->x, equalizer_graph->y); | |
112 | |
113 return GTK_WIDGET(equalizer_graph); | |
114 } | |
115 | |
116 static void ui_skinned_equalizer_graph_destroy(GtkObject *object) { | |
117 UiSkinnedEqualizerGraph *equalizer_graph; | |
118 | |
119 g_return_if_fail (object != NULL); | |
120 g_return_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH (object)); | |
121 | |
122 equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (object); | |
123 | |
124 if (GTK_OBJECT_CLASS (parent_class)->destroy) | |
125 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); | |
126 } | |
127 | |
128 static void ui_skinned_equalizer_graph_realize(GtkWidget *widget) { | |
129 UiSkinnedEqualizerGraph *equalizer_graph; | |
130 GdkWindowAttr attributes; | |
131 gint attributes_mask; | |
132 | |
133 g_return_if_fail (widget != NULL); | |
134 g_return_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH(widget)); | |
135 | |
136 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED); | |
137 equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH(widget); | |
138 | |
139 attributes.x = widget->allocation.x; | |
140 attributes.y = widget->allocation.y; | |
141 attributes.width = widget->allocation.width; | |
142 attributes.height = widget->allocation.height; | |
143 attributes.wclass = GDK_INPUT_OUTPUT; | |
144 attributes.window_type = GDK_WINDOW_CHILD; | |
145 attributes.event_mask = gtk_widget_get_events(widget); | |
146 attributes.event_mask |= GDK_EXPOSURE_MASK; | |
147 attributes.visual = gtk_widget_get_visual(widget); | |
148 attributes.colormap = gtk_widget_get_colormap(widget); | |
149 | |
150 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; | |
151 widget->window = gdk_window_new(widget->parent->window, &attributes, attributes_mask); | |
152 | |
153 widget->style = gtk_style_attach(widget->style, widget->window); | |
154 | |
155 gdk_window_set_user_data(widget->window, widget); | |
156 } | |
157 | |
158 static void ui_skinned_equalizer_graph_size_request(GtkWidget *widget, GtkRequisition *requisition) { | |
159 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH(widget); | |
160 | |
161 requisition->width = equalizer_graph->width*(1+equalizer_graph->double_size); | |
162 requisition->height = equalizer_graph->height*(1+equalizer_graph->double_size); | |
163 } | |
164 | |
165 static void ui_skinned_equalizer_graph_size_allocate(GtkWidget *widget, GtkAllocation *allocation) { | |
166 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (widget); | |
167 | |
168 widget->allocation = *allocation; | |
169 widget->allocation.x *= (1+equalizer_graph->double_size); | |
170 widget->allocation.y *= (1+equalizer_graph->double_size); | |
171 if (GTK_WIDGET_REALIZED (widget)) | |
172 gdk_window_move_resize(widget->window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height); | |
173 | |
174 equalizer_graph->x = widget->allocation.x/(equalizer_graph->double_size ? 2 : 1); | |
175 equalizer_graph->y = widget->allocation.y/(equalizer_graph->double_size ? 2 : 1); | |
176 } | |
177 | |
178 void | |
179 init_spline(gfloat * x, gfloat * y, gint n, gfloat * y2) | |
180 { | |
181 gint i, k; | |
182 gfloat p, qn, sig, un, *u; | |
183 | |
184 u = (gfloat *) g_malloc(n * sizeof(gfloat)); | |
185 | |
186 y2[0] = u[0] = 0.0; | |
187 | |
188 for (i = 1; i < n - 1; i++) { | |
189 sig = ((gfloat) x[i] - x[i - 1]) / ((gfloat) x[i + 1] - x[i - 1]); | |
190 p = sig * y2[i - 1] + 2.0; | |
191 y2[i] = (sig - 1.0) / p; | |
192 u[i] = | |
193 (((gfloat) y[i + 1] - y[i]) / (x[i + 1] - x[i])) - | |
194 (((gfloat) y[i] - y[i - 1]) / (x[i] - x[i - 1])); | |
195 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p; | |
196 } | |
197 qn = un = 0.0; | |
198 | |
199 y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0); | |
200 for (k = n - 2; k >= 0; k--) | |
201 y2[k] = y2[k] * y2[k + 1] + u[k]; | |
202 g_free(u); | |
203 } | |
204 | |
205 gfloat | |
206 eval_spline(gfloat xa[], gfloat ya[], gfloat y2a[], gint n, gfloat x) | |
207 { | |
208 gint klo, khi, k; | |
209 gfloat h, b, a; | |
210 | |
211 klo = 0; | |
212 khi = n - 1; | |
213 while (khi - klo > 1) { | |
214 k = (khi + klo) >> 1; | |
215 if (xa[k] > x) | |
216 khi = k; | |
217 else | |
218 klo = k; | |
219 } | |
220 h = xa[khi] - xa[klo]; | |
221 a = (xa[khi] - x) / h; | |
222 b = (x - xa[klo]) / h; | |
223 return (a * ya[klo] + b * ya[khi] + | |
224 ((a * a * a - a) * y2a[klo] + | |
225 (b * b * b - b) * y2a[khi]) * (h * h) / 6.0); | |
226 } | |
227 | |
228 static gboolean ui_skinned_equalizer_graph_expose(GtkWidget *widget, GdkEventExpose *event) { | |
229 g_return_val_if_fail (widget != NULL, FALSE); | |
230 g_return_val_if_fail (UI_SKINNED_IS_EQUALIZER_GRAPH (widget), FALSE); | |
231 g_return_val_if_fail (event != NULL, FALSE); | |
232 | |
233 UiSkinnedEqualizerGraph *equalizer_graph = UI_SKINNED_EQUALIZER_GRAPH (widget); | |
234 | |
235 GdkPixmap *obj = NULL; | |
236 GdkGC *gc; | |
237 | |
238 obj = gdk_pixmap_new(NULL, equalizer_graph->width, equalizer_graph->height, gdk_rgb_get_visual()->depth); | |
239 gc = gdk_gc_new(obj); | |
240 | |
241 GdkColor col; | |
242 guint32 cols[19]; | |
243 gint i, y, ymin, ymax, py = 0; | |
244 gfloat x[] = { 0, 11, 23, 35, 47, 59, 71, 83, 97, 109 }, yf[10]; | |
245 | |
246 /* | |
247 * This avoids the init_spline() function to be inlined. | |
248 * Inlining the function caused troubles when compiling with | |
249 * `-O' (at least on FreeBSD). | |
250 */ | |
251 void (*__init_spline) (gfloat *, gfloat *, gint, gfloat *) = init_spline; | |
252 | |
253 skin_draw_pixmap(bmp_active_skin, obj, gc, equalizer_graph->skin_index, 0, 294, 0, 0, | |
254 equalizer_graph->width, equalizer_graph->height); | |
255 skin_draw_pixmap(bmp_active_skin, obj, gc, equalizer_graph->skin_index, 0, 314, | |
256 0, 9 + ((cfg.equalizer_preamp * 9) / 20), | |
257 equalizer_graph->width, 1); | |
258 | |
259 skin_get_eq_spline_colors(bmp_active_skin, cols); | |
260 | |
261 __init_spline(x, cfg.equalizer_bands, 10, yf); | |
262 for (i = 0; i < 109; i++) { | |
263 y = 9 - | |
264 (gint) ((eval_spline(x, cfg.equalizer_bands, yf, 10, i) * | |
265 9.0) / 20.0); | |
266 if (y < 0) | |
267 y = 0; | |
268 if (y > 18) | |
269 y = 18; | |
270 if (!i) | |
271 py = y; | |
272 if (y < py) { | |
273 ymin = y; | |
274 ymax = py; | |
275 } | |
276 else { | |
277 ymin = py; | |
278 ymax = y; | |
279 } | |
280 py = y; | |
281 for (y = ymin; y <= ymax; y++) { | |
282 col.pixel = cols[y]; | |
283 gdk_gc_set_foreground(gc, &col); | |
284 gdk_draw_point(obj, gc, i + 2, y); | |
285 } | |
286 } | |
287 | |
288 GdkPixmap *image = NULL; | |
289 | |
290 if (equalizer_graph->double_size) { | |
291 image = create_dblsize_pixmap(obj); | |
292 } else { | |
293 image = gdk_pixmap_new(NULL, equalizer_graph->width, equalizer_graph->height, gdk_rgb_get_visual()->depth); | |
294 gdk_draw_drawable (image, gc, obj, 0, 0, 0, 0, equalizer_graph->width, equalizer_graph->height); | |
295 } | |
296 | |
297 g_object_unref(obj); | |
298 | |
299 gdk_draw_drawable (widget->window, gc, image, 0, 0, 0, 0, | |
300 equalizer_graph->width*(1+equalizer_graph->double_size), equalizer_graph->height*(1+equalizer_graph->double_size)); | |
301 g_object_unref(gc); | |
302 g_object_unref(image); | |
303 | |
304 return FALSE; | |
305 } | |
306 | |
307 static void ui_skinned_equalizer_graph_toggle_doublesize(UiSkinnedEqualizerGraph *equalizer_graph) { | |
308 GtkWidget *widget = GTK_WIDGET (equalizer_graph); | |
309 | |
310 equalizer_graph->double_size = !equalizer_graph->double_size; | |
311 gtk_widget_set_size_request(widget, equalizer_graph->width*(1+equalizer_graph->double_size), | |
312 equalizer_graph->height*(1+equalizer_graph->double_size)); | |
313 | |
314 gtk_widget_queue_draw(GTK_WIDGET(equalizer_graph)); | |
315 } |