2 Copyright (C) 2003-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gdkmm/rectangle.h>
25 #include <gtkmm2ext/fastmeter.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm/style.h>
30 #define UINT_TO_RGB(u,r,g,b) { (*(r)) = ((u)>>16)&0xff; (*(g)) = ((u)>>8)&0xff; (*(b)) = (u)&0xff; }
31 #define UINT_TO_RGBA(u,r,g,b,a) { UINT_TO_RGB(((u)>>8),r,g,b); (*(a)) = (u)&0xff; }
35 using namespace Gtkmm2ext;
39 int FastMeter::min_v_pixbuf_size = 10;
40 int FastMeter::max_v_pixbuf_size = 1024;
41 Glib::RefPtr<Gdk::Pixbuf>* FastMeter::v_pixbuf_cache = 0;
43 int FastMeter::min_h_pixbuf_size = 10;
44 int FastMeter::max_h_pixbuf_size = 1024;
45 Glib::RefPtr<Gdk::Pixbuf>* FastMeter::h_pixbuf_cache = 0;
47 int FastMeter::_clr0 = 0;
48 int FastMeter::_clr1 = 0;
49 int FastMeter::_clr2 = 0;
50 int FastMeter::_clr3 = 0;
52 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o, int len, int clr0, int clr1, int clr2, int clr3)
64 set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
69 if (orientation == Vertical) {
72 pixbuf = request_vertical_meter(dimen, len);
76 pixbuf = request_horizontal_meter(len, dimen);
79 pixheight = pixbuf->get_height();
80 pixwidth = pixbuf->get_width();
82 if (orientation == Vertical) {
83 pixrect.width = min (pixwidth, (gint) dimen);
84 pixrect.height = pixheight;
86 pixrect.width = pixwidth;
87 pixrect.height = min (pixheight, (gint) dimen);
90 request_width = pixrect.width;
91 request_height= pixrect.height;
94 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
96 if (height < min_v_pixbuf_size)
97 height = min_v_pixbuf_size;
98 if (height > max_v_pixbuf_size)
99 height = max_v_pixbuf_size;
101 //int index = height - 1;
103 //if (v_pixbuf_cache == 0) {
104 // v_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
105 // memset(v_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
107 Glib::RefPtr<Gdk::Pixbuf> ret;// = v_pixbuf_cache[index];
113 data = (guint8*) malloc(width*height * 3);
115 guint8 r,g,b,r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3,a;
117 UINT_TO_RGBA (_clr0, &r0, &g0, &b0, &a);
118 UINT_TO_RGBA (_clr1, &r1, &g1, &b1, &a);
119 UINT_TO_RGBA (_clr2, &r2, &g2, &b2, &a);
120 UINT_TO_RGBA (_clr3, &r3, &g3, &b3, &a);
121 // fake log calculation copied from log_meter.h
122 // actual calculation:
124 // def = (0.0f + 20.0f) * 2.5f + 50f
125 // return def / 115.0f
126 int knee = (int)floor((float)height * 100.0f / 115.0f);
129 for (y = 0; y < knee/2; y++) {
131 r = (guint8)floor((float)abs(r1 - r0) * (float)y / (float)(knee/2));
132 (r0 >= r1) ? r = r0 - r : r += r0;
134 g = (guint8)floor((float)abs(g1 - g0) * (float)y / (float)(knee/2));
135 (g0 >= g1) ? g = g0 - g : g += g0;
137 b = (guint8)floor((float)abs(b1 - b0) * (float)y / (float)(knee/2));
138 (b0 >= b1) ? b = b0 - b : b += b0;
140 for (int x = 0; x < width; x++) {
141 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
142 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
143 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
147 int offset = knee - y;
148 for (int i=0; i < offset; i++,y++) {
150 r = (guint8)floor((float)abs(r2 - r1) * (float)i / (float)offset);
151 (r1 >= r2) ? r = r1 - r : r += r1;
153 g = (guint8)floor((float)abs(g2 - g1) * (float)i / (float)offset);
154 (g1 >= g2) ? g = g1 - g : g += g1;
156 b = (guint8)floor((float)abs(b2 - b1) * (float)i / (float)offset);
157 (b1 >= b2) ? b = b1 - b : b += b1;
159 for (int x = 0; x < width; x++) {
160 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
161 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
162 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
166 for (; y < height; y++) {
167 for (int x = 0; x < width; x++) {
168 data[ (x+(height-y-1)*width) * 3 + 0 ] = r3;
169 data[ (x+(height-y-1)*width) * 3 + 1 ] = g3;
170 data[ (x+(height-y-1)*width) * 3 + 2 ] = b3;
174 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
175 //v_pixbuf_cache[index] = ret;
180 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
182 if (width < min_h_pixbuf_size)
183 width = min_h_pixbuf_size;
184 if (width > max_h_pixbuf_size)
185 width = max_h_pixbuf_size;
187 int index = width - 1;
189 if (h_pixbuf_cache == 0) {
190 h_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
191 memset(h_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
193 Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
199 data = (guint8*) malloc(width*height * 3);
206 // fake log calculation copied from log_meter.h
207 // actual calculation:
209 // def = (0.0f + 20.0f) * 2.5f + 50f
210 // return def / 115.0f
211 int knee = (int)floor((float)width * 100.0f / 115.0f);
215 for (x = 0; x < knee / 2; x++) {
217 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
219 for (int y = 0; y < height; y++) {
220 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
221 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
222 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
226 for (; x < knee; x++) {
228 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
230 for (int y = 0; y < height; y++) {
231 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
232 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
233 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
240 for (; x < width; x++) {
241 for (int y = 0; y < height; y++) {
242 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
243 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
244 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
248 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
249 h_pixbuf_cache[index] = ret;
254 FastMeter::~FastMeter ()
259 FastMeter::set_hold_count (long val)
273 FastMeter::on_size_request (GtkRequisition* req)
275 if (orientation == Vertical) {
277 req->height = request_height;
278 req->height = max(req->height, min_v_pixbuf_size);
279 req->height = min(req->height, max_v_pixbuf_size);
281 req->width = request_width;
285 req->width = request_width;
286 req->width = max(req->width, min_h_pixbuf_size);
287 req->width = min(req->width, max_h_pixbuf_size);
289 req->height = request_height;
295 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
297 if (orientation == Vertical) {
299 if (alloc.get_width() != request_width) {
300 alloc.set_width (request_width);
303 int h = alloc.get_height();
304 h = max(h, min_v_pixbuf_size);
305 h = min(h, max_v_pixbuf_size);
307 if ( h != alloc.get_height())
310 if (pixheight != h) {
311 pixbuf = request_vertical_meter(request_width, h);
316 if (alloc.get_height() != request_height) {
317 alloc.set_height(request_height);
320 int w = alloc.get_width();
321 w = max(w, min_h_pixbuf_size);
322 w = min(w, max_h_pixbuf_size);
324 if ( w != alloc.get_width())
328 pixbuf = request_horizontal_meter(w, request_height);
332 pixheight = pixbuf->get_height();
333 pixwidth = pixbuf->get_width();
335 DrawingArea::on_size_allocate(alloc);
339 FastMeter::on_expose_event (GdkEventExpose* ev)
341 if (orientation == Vertical) {
342 return vertical_expose (ev);
344 return horizontal_expose (ev);
349 FastMeter::vertical_expose (GdkEventExpose* ev)
352 GdkRectangle intersection;
353 GdkRectangle background;
355 top_of_meter = (gint) floor (pixheight * current_level);
357 /* reset the height & origin of the rect that needs to show the pixbuf
360 pixrect.height = top_of_meter;
361 pixrect.y = pixheight - top_of_meter;
365 background.width = pixrect.width;
366 background.height = pixheight - top_of_meter;
368 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
369 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
370 intersection.x, intersection.y,
371 intersection.width, intersection.height);
374 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
375 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
376 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
377 intersection.x, intersection.y,
378 intersection.x, intersection.y,
379 intersection.width, intersection.height,
380 Gdk::RGB_DITHER_NONE, 0, 0);
384 if (hold_state && intersection.width > 0) {
385 gint y = pixheight - (gint) floor (pixheight * current_peak);
386 int h = min(3, pixheight - y);
388 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
391 intersection.width, h,
392 Gdk::RGB_DITHER_NONE, 0, 0);
399 FastMeter::horizontal_expose (GdkEventExpose* ev)
402 GdkRectangle intersection;
403 GdkRectangle background;
405 right_of_meter = (gint) floor (pixwidth * current_level);
406 pixrect.width = right_of_meter;
410 background.width = pixwidth - right_of_meter;
411 background.height = pixrect.height;
413 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
414 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
415 intersection.x + right_of_meter, intersection.y,
416 intersection.width, intersection.height);
419 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
420 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
421 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
422 intersection.x, intersection.y,
423 intersection.x, intersection.y,
424 pixrect.width, intersection.height,
425 Gdk::RGB_DITHER_NONE, 0, 0);
429 // XXX: peaks don't work properly
431 if (hold_state && intersection.height > 0) {
432 gint x = (gint) floor(pixwidth * current_peak);
434 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
437 3, intersection.height,
438 Gdk::RGB_DITHER_NONE, 0, 0);
446 FastMeter::set (float lvl)
450 if (lvl > current_peak) {
452 hold_state = hold_cnt;
455 if (hold_state > 0) {
456 if (--hold_state == 0) {