Zoom session when the mouse pointer is moved up and down during a playhead drag.
[ardour2.git] / libs / gtkmm2ext / fastmeter.cc
blob4bb6e81d00b94bd3e945bc08fda567f5a7545069
1 /*
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.
18 $Id$
21 #include <iostream>
22 #include <cmath>
23 #include <algorithm>
24 #include <gdkmm/rectangle.h>
25 #include <gtkmm2ext/fastmeter.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm/style.h>
28 #include <cstring>
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; }
32 using namespace Gtk;
33 using namespace Gdk;
34 using namespace Glib;
35 using namespace Gtkmm2ext;
36 using namespace std;
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)
54 orientation = o;
55 hold_cnt = hold;
56 hold_state = 0;
57 current_peak = 0;
58 current_level = 0;
59 last_peak_rect.width = 0;
60 last_peak_rect.height = 0;
61 _clr0 = clr0;
62 _clr1 = clr1;
63 _clr2 = clr2;
64 _clr3 = clr3;
66 set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
68 pixrect.x = 0;
69 pixrect.y = 0;
71 if (orientation == Vertical) {
72 if (!len)
73 len = 250;
74 pixbuf = request_vertical_meter(dimen, len);
75 } else {
76 if (!len)
77 len = 186;
78 pixbuf = request_horizontal_meter(len, dimen);
81 pixheight = pixbuf->get_height();
82 pixwidth = pixbuf->get_width();
84 if (orientation == Vertical) {
85 pixrect.width = min (pixwidth, (gint) dimen);
86 pixrect.height = pixheight;
87 } else {
88 pixrect.width = pixwidth;
89 pixrect.height = min (pixheight, (gint) dimen);
92 request_width = pixrect.width;
93 request_height= pixrect.height;
96 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
98 if (height < min_v_pixbuf_size)
99 height = min_v_pixbuf_size;
100 if (height > max_v_pixbuf_size)
101 height = max_v_pixbuf_size;
103 //int index = height - 1;
105 //if (v_pixbuf_cache == 0) {
106 // v_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
107 // memset(v_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
109 Glib::RefPtr<Gdk::Pixbuf> ret;// = v_pixbuf_cache[index];
110 //if (ret)
111 // return ret;
113 guint8* data;
115 data = (guint8*) malloc(width*height * 3);
117 guint8 r,g,b,r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3,a;
119 UINT_TO_RGBA (_clr0, &r0, &g0, &b0, &a);
120 UINT_TO_RGBA (_clr1, &r1, &g1, &b1, &a);
121 UINT_TO_RGBA (_clr2, &r2, &g2, &b2, &a);
122 UINT_TO_RGBA (_clr3, &r3, &g3, &b3, &a);
123 // fake log calculation copied from log_meter.h
124 // actual calculation:
125 // log_meter(0.0f) =
126 // def = (0.0f + 20.0f) * 2.5f + 50f
127 // return def / 115.0f
128 int knee = (int)floor((float)height * 100.0f / 115.0f);
129 int y;
131 for (y = 0; y < knee/2; y++) {
133 r = (guint8)floor((float)abs(r1 - r0) * (float)y / (float)(knee/2));
134 (r0 >= r1) ? r = r0 - r : r += r0;
136 g = (guint8)floor((float)abs(g1 - g0) * (float)y / (float)(knee/2));
137 (g0 >= g1) ? g = g0 - g : g += g0;
139 b = (guint8)floor((float)abs(b1 - b0) * (float)y / (float)(knee/2));
140 (b0 >= b1) ? b = b0 - b : b += b0;
142 for (int x = 0; x < width; x++) {
143 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
144 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
145 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
149 int offset = knee - y;
150 for (int i=0; i < offset; i++,y++) {
152 r = (guint8)floor((float)abs(r2 - r1) * (float)i / (float)offset);
153 (r1 >= r2) ? r = r1 - r : r += r1;
155 g = (guint8)floor((float)abs(g2 - g1) * (float)i / (float)offset);
156 (g1 >= g2) ? g = g1 - g : g += g1;
158 b = (guint8)floor((float)abs(b2 - b1) * (float)i / (float)offset);
159 (b1 >= b2) ? b = b1 - b : b += b1;
161 for (int x = 0; x < width; x++) {
162 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
163 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
164 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
167 y--;
168 for (; y < height; y++) {
169 for (int x = 0; x < width; x++) {
170 data[ (x+(height-y-1)*width) * 3 + 0 ] = r3;
171 data[ (x+(height-y-1)*width) * 3 + 1 ] = g3;
172 data[ (x+(height-y-1)*width) * 3 + 2 ] = b3;
176 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
177 //v_pixbuf_cache[index] = ret;
179 return ret;
182 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
184 if (width < min_h_pixbuf_size)
185 width = min_h_pixbuf_size;
186 if (width > max_h_pixbuf_size)
187 width = max_h_pixbuf_size;
189 int index = width - 1;
191 if (h_pixbuf_cache == 0) {
192 h_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
193 memset(h_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
195 Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
196 if (ret)
197 return ret;
199 guint8* data;
201 data = (guint8*) malloc(width*height * 3);
203 guint8 r,g,b;
204 r=0;
205 g=255;
206 b=0;
208 // fake log calculation copied from log_meter.h
209 // actual calculation:
210 // log_meter(0.0f) =
211 // def = (0.0f + 20.0f) * 2.5f + 50f
212 // return def / 115.0f
213 int knee = (int)floor((float)width * 100.0f / 115.0f);
215 int x;
217 for (x = 0; x < knee / 2; x++) {
219 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
221 for (int y = 0; y < height; y++) {
222 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
223 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
224 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
228 for (; x < knee; x++) {
230 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
232 for (int y = 0; y < height; y++) {
233 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
234 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
235 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
239 r=255;
240 g=0;
241 b=0;
242 for (; x < width; x++) {
243 for (int y = 0; y < height; y++) {
244 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
245 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
246 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
250 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
251 h_pixbuf_cache[index] = ret;
253 return ret;
256 FastMeter::~FastMeter ()
260 void
261 FastMeter::set_hold_count (long val)
263 if (val < 1) {
264 val = 1;
267 hold_cnt = val;
268 hold_state = 0;
269 current_peak = 0;
271 queue_draw ();
274 void
275 FastMeter::on_size_request (GtkRequisition* req)
277 if (orientation == Vertical) {
279 req->height = request_height;
280 req->height = max(req->height, min_v_pixbuf_size);
281 req->height = min(req->height, max_v_pixbuf_size);
283 req->width = request_width;
285 } else {
287 req->width = request_width;
288 req->width = max(req->width, min_h_pixbuf_size);
289 req->width = min(req->width, max_h_pixbuf_size);
291 req->height = request_height;
296 void
297 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
299 if (orientation == Vertical) {
301 if (alloc.get_width() != request_width) {
302 alloc.set_width (request_width);
305 int h = alloc.get_height();
306 h = max(h, min_v_pixbuf_size);
307 h = min(h, max_v_pixbuf_size);
309 if ( h != alloc.get_height())
310 alloc.set_height(h);
312 if (pixheight != h) {
313 pixbuf = request_vertical_meter(request_width, h);
316 } else {
318 if (alloc.get_height() != request_height) {
319 alloc.set_height(request_height);
322 int w = alloc.get_width();
323 w = max(w, min_h_pixbuf_size);
324 w = min(w, max_h_pixbuf_size);
326 if ( w != alloc.get_width())
327 alloc.set_width(w);
329 if (pixwidth != w) {
330 pixbuf = request_horizontal_meter(w, request_height);
334 pixheight = pixbuf->get_height();
335 pixwidth = pixbuf->get_width();
337 DrawingArea::on_size_allocate(alloc);
340 bool
341 FastMeter::on_expose_event (GdkEventExpose* ev)
343 if (orientation == Vertical) {
344 return vertical_expose (ev);
345 } else {
346 return horizontal_expose (ev);
350 bool
351 FastMeter::vertical_expose (GdkEventExpose* ev)
353 gint top_of_meter;
354 GdkRectangle intersection;
355 GdkRectangle background;
357 top_of_meter = (gint) floor (pixheight * current_level);
359 /* reset the height & origin of the rect that needs to show the pixbuf
362 pixrect.height = top_of_meter;
363 pixrect.y = pixheight - top_of_meter;
365 background.x = 0;
366 background.y = 0;
367 background.width = pixrect.width;
368 background.height = pixheight - top_of_meter;
370 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
371 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
372 intersection.x, intersection.y,
373 intersection.width, intersection.height);
376 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
377 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
378 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
379 intersection.x, intersection.y,
380 intersection.x, intersection.y,
381 intersection.width, intersection.height,
382 Gdk::RGB_DITHER_NONE, 0, 0);
385 // draw peak bar
387 if (hold_state) {
388 last_peak_rect.x = 0;
389 last_peak_rect.width = pixwidth;
390 last_peak_rect.y = pixheight - (gint) floor (pixheight * current_peak);
391 last_peak_rect.height = min(3, pixheight - last_peak_rect.y);
393 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
394 0, last_peak_rect.y,
395 0, last_peak_rect.y,
396 pixwidth, last_peak_rect.height,
397 Gdk::RGB_DITHER_NONE, 0, 0);
398 } else {
399 last_peak_rect.width = 0;
400 last_peak_rect.height = 0;
403 return TRUE;
406 bool
407 FastMeter::horizontal_expose (GdkEventExpose* ev)
409 gint right_of_meter;
410 GdkRectangle intersection;
411 GdkRectangle background;
413 right_of_meter = (gint) floor (pixwidth * current_level);
414 pixrect.width = right_of_meter;
416 background.x = 0;
417 background.y = 0;
418 background.width = pixwidth - right_of_meter;
419 background.height = pixrect.height;
421 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
422 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
423 intersection.x + right_of_meter, intersection.y,
424 intersection.width, intersection.height);
427 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
428 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
429 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
430 intersection.x, intersection.y,
431 intersection.x, intersection.y,
432 pixrect.width, intersection.height,
433 Gdk::RGB_DITHER_NONE, 0, 0);
436 // draw peak bar
437 // XXX: peaks don't work properly
439 if (hold_state && intersection.height > 0) {
440 gint x = (gint) floor(pixwidth * current_peak);
442 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
443 x, intersection.y,
444 x, intersection.y,
445 3, intersection.height,
446 Gdk::RGB_DITHER_NONE, 0, 0);
450 return true;
453 void
454 FastMeter::set (float lvl)
456 float old_level = current_level;
457 float old_peak = current_peak;
459 current_level = lvl;
461 if (lvl > current_peak) {
462 current_peak = lvl;
463 hold_state = hold_cnt;
466 if (hold_state > 0) {
467 if (--hold_state == 0) {
468 current_peak = lvl;
472 if (current_level == old_level && current_peak == old_peak && hold_state == 0) {
473 return;
477 Glib::RefPtr<Gdk::Window> win;
479 if ((win = get_window()) == 0) {
480 queue_draw ();
481 return;
484 if (orientation == Vertical) {
485 queue_vertical_redraw (win, old_level);
486 } else {
487 queue_horizontal_redraw (win, old_level);
491 void
492 FastMeter::queue_vertical_redraw (const Glib::RefPtr<Gdk::Window>& win, float old_level)
494 GdkRectangle rect;
496 gint new_top = (gint) floor (pixheight * current_level);
498 rect.x = 0;
499 rect.width = pixwidth;
500 rect.height = new_top;
501 rect.y = pixheight - new_top;
503 if (current_level > old_level) {
504 /* colored/pixbuf got larger, just draw the new section */
505 /* rect.y stays where it is because of X coordinates */
506 /* height of invalidated area is between new.y (smaller) and old.y
507 (larger).
508 X coordinates just make my brain hurt.
510 rect.height = pixrect.y - rect.y;
511 } else {
512 /* it got smaller, compute the difference */
513 /* rect.y becomes old.y (the smaller value) */
514 rect.y = pixrect.y;
515 /* rect.height is the old.y (smaller) minus the new.y (larger)
517 rect.height = pixrect.height - rect.height;
520 GdkRegion* region = 0;
521 bool queue = false;
523 if (rect.height != 0) {
525 /* ok, first region to draw ... */
527 region = gdk_region_rectangle (&rect);
528 queue = true;
531 /* redraw the last place where the last peak hold bar was;
532 the next expose will draw the new one whether its part of
533 expose region or not.
536 if (last_peak_rect.width * last_peak_rect.height != 0) {
537 if (!queue) {
538 region = gdk_region_new ();
539 queue = true;
541 gdk_region_union_with_rect (region, &last_peak_rect);
544 if (queue) {
545 gdk_window_invalidate_region (win->gobj(), region, true);
547 if (region) {
548 gdk_region_destroy(region);
549 region = 0;
553 void
554 FastMeter::queue_horizontal_redraw (const Glib::RefPtr<Gdk::Window>& /*win*/, float /*old_level*/)
556 /* XXX OPTIMIZE (when we have some horizontal meters) */
557 queue_draw ();
560 void
561 FastMeter::clear ()
563 current_level = 0;
564 current_peak = 0;
565 hold_state = 0;
566 queue_draw ();