merge from 2.0-ongoing @ 3581
[ardour.git] / libs / gtkmm2ext / fastmeter.cc
1 /*
2     Copyright (C) 2003-2006 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
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>
29
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;
37
38
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;
42
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;
46
47 int FastMeter::_clr0 = 0;
48 int FastMeter::_clr1 = 0;
49 int FastMeter::_clr2 = 0;
50 int FastMeter::_clr3 = 0;
51
52 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o, int len, int clr0, int clr1, int clr2, int clr3)
53 {
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;
65
66         set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
67
68         pixrect.x = 0;
69         pixrect.y = 0;
70
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);
79         }
80
81         pixheight = pixbuf->get_height();
82         pixwidth  = pixbuf->get_width();
83
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);
90         }
91
92         request_width = pixrect.width;
93         request_height= pixrect.height;
94 }
95
96 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
97 {
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;
102         
103         //int index = height - 1;
104
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);
108         //}
109         Glib::RefPtr<Gdk::Pixbuf> ret;// = v_pixbuf_cache[index];
110         //if (ret)
111         //      return ret;
112
113         guint8* data;
114
115         data = (guint8*) malloc(width*height * 3);
116
117         guint8 r,g,b,r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3,a;
118
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;
130
131         for (y = 0; y < knee/2; y++) {
132
133                 r = (guint8)floor((float)abs(r1 - r0) * (float)y / (float)(knee/2));
134                 (r0 >= r1) ? r = r0 - r : r += r0;
135                         
136                 g = (guint8)floor((float)abs(g1 - g0) * (float)y / (float)(knee/2));
137                 (g0 >= g1) ? g = g0 - g : g += g0;
138
139                 b = (guint8)floor((float)abs(b1 - b0) * (float)y / (float)(knee/2));
140                 (b0 >= b1) ? b = b0 - b : b += b0;
141
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;
146                 }
147         }
148
149         int offset = knee - y;
150         for (int i=0; i < offset; i++,y++) {
151
152                 r = (guint8)floor((float)abs(r2 - r1) * (float)i / (float)offset);
153                 (r1 >= r2) ? r = r1 - r : r += r1;
154
155                 g = (guint8)floor((float)abs(g2 - g1) * (float)i / (float)offset);
156                 (g1 >= g2) ? g = g1 - g : g += g1;
157
158                 b = (guint8)floor((float)abs(b2 - b1) * (float)i / (float)offset);
159                 (b1 >= b2) ? b = b1 - b : b += b1;
160
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;
165                 }
166         }
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;
173                 }
174         }
175         
176         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
177         //v_pixbuf_cache[index] = ret;
178
179         return ret;
180 }
181
182 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
183 {
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;
188         
189         int index = width - 1;
190         
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);
194         }
195         Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
196         if (ret)
197                 return ret;
198
199         guint8* data;
200
201         data = (guint8*) malloc(width*height * 3);
202         
203         guint8 r,g,b;
204         r=0;
205         g=255;
206         b=0;
207
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);
214         
215         int x;
216         
217         for (x = 0; x < knee / 2; x++) {
218
219                 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
220                 
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;
225                 }
226         }
227         
228         for (; x < knee; x++) {
229
230                 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
231                 
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;
236                 }
237         }
238
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;
247                 }
248         }
249         
250         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
251         h_pixbuf_cache[index] = ret;
252
253         return ret;
254 }
255
256 FastMeter::~FastMeter ()
257 {
258 }
259
260 void
261 FastMeter::set_hold_count (long val)
262 {
263         if (val < 1) {
264                 val = 1;
265         }
266         
267         hold_cnt = val;
268         hold_state = 0;
269         current_peak = 0;
270         
271         queue_draw ();
272 }
273
274 void
275 FastMeter::on_size_request (GtkRequisition* req)
276 {
277         if (orientation == Vertical) {
278
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);
282
283                 req->width  = request_width;
284
285         } else {
286
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);
290
291                 req->height = request_height;
292         }
293
294 }
295
296 void
297 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
298 {
299         if (orientation == Vertical) {
300
301                 if (alloc.get_width() != request_width) {
302                         alloc.set_width (request_width);
303                 }
304
305                 int h = alloc.get_height();
306                 h = max(h, min_v_pixbuf_size);
307                 h = min(h, max_v_pixbuf_size);
308
309                 if ( h != alloc.get_height())
310                         alloc.set_height(h);
311
312                 if (pixheight != h) {
313                         pixbuf = request_vertical_meter(request_width, h);
314                 }
315
316         } else {
317
318                 if (alloc.get_height() != request_height) {
319                         alloc.set_height(request_height);
320                 }
321
322                 int w = alloc.get_width();
323                 w = max(w, min_h_pixbuf_size);
324                 w = min(w, max_h_pixbuf_size);
325
326                 if ( w != alloc.get_width())
327                         alloc.set_width(w);
328
329                 if (pixwidth != w) {
330                         pixbuf = request_horizontal_meter(w, request_height);
331                 }
332         }
333
334         pixheight = pixbuf->get_height();
335         pixwidth  = pixbuf->get_width();
336
337         DrawingArea::on_size_allocate(alloc);
338 }
339
340 bool
341 FastMeter::on_expose_event (GdkEventExpose* ev)
342 {
343         if (orientation == Vertical) {
344                 return vertical_expose (ev);
345         } else {
346                 return horizontal_expose (ev);
347         }
348 }
349
350 bool
351 FastMeter::vertical_expose (GdkEventExpose* ev)
352 {
353         gint top_of_meter;
354         GdkRectangle intersection;
355         GdkRectangle background;
356
357         top_of_meter = (gint) floor (pixheight * current_level);
358         
359         /* reset the height & origin of the rect that needs to show the pixbuf
360          */
361
362         pixrect.height = top_of_meter;
363         pixrect.y = pixheight - top_of_meter;
364
365         background.x = 0;
366         background.y = 0;
367         background.width = pixrect.width;
368         background.height = pixheight - top_of_meter;
369
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);
374         }
375
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);
383         }
384
385         // draw peak bar 
386
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);
392
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;
401         }
402
403         return TRUE;
404 }
405
406 bool
407 FastMeter::horizontal_expose (GdkEventExpose* ev)
408 {
409         gint right_of_meter;
410         GdkRectangle intersection;
411         GdkRectangle background;
412
413         right_of_meter = (gint) floor (pixwidth * current_level);
414         pixrect.width = right_of_meter;
415
416         background.x = 0;
417         background.y = 0;
418         background.width  = pixwidth - right_of_meter;
419         background.height = pixrect.height;
420
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);
425         }
426         
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);
434         }
435
436         // draw peak bar 
437         // XXX: peaks don't work properly
438         /*
439         if (hold_state && intersection.height > 0) {
440                 gint x = (gint) floor(pixwidth * current_peak);
441
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);
447         }
448         */
449         
450         return true;
451 }
452
453 void
454 FastMeter::set (float lvl)
455 {
456         float old_level = current_level;
457         float old_peak = current_peak;
458
459         current_level = lvl;
460         
461         if (lvl > current_peak) {
462                 current_peak = lvl;
463                 hold_state = hold_cnt;
464         }
465         
466         if (hold_state > 0) {
467                 if (--hold_state == 0) {
468                         current_peak = lvl;
469                 }
470         }
471
472         if (current_level == old_level && current_peak == old_peak && hold_state == 0) {
473                 return;
474         }
475
476
477         Glib::RefPtr<Gdk::Window> win;
478
479         if ((win = get_window()) == 0) {
480                 queue_draw ();
481                 return;
482         }
483
484         if (orientation == Vertical) {
485                 queue_vertical_redraw (win, old_level);
486         } else {
487                 queue_horizontal_redraw (win, old_level);
488         }
489 }
490
491 void
492 FastMeter::queue_vertical_redraw (const Glib::RefPtr<Gdk::Window>& win, float old_level)
493 {
494         GdkRectangle rect;
495         
496         gint new_top = (gint) floor (pixheight * current_level);
497         
498         rect.x = 0;
499         rect.width = pixwidth;
500         rect.height = new_top;
501         rect.y = pixheight - new_top;
502
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.
509                 */
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)
516                 */
517                 rect.height = pixrect.height - rect.height;
518         }
519
520         GdkRegion* region = 0;
521         bool queue = false;
522
523         if (rect.height != 0) {
524
525                 /* ok, first region to draw ... */
526                 
527                 region = gdk_region_rectangle (&rect);
528                 queue = true;
529         }
530
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.
534         */
535         
536         if (last_peak_rect.width * last_peak_rect.height != 0) {
537                 if (!queue) {
538                         region = gdk_region_new ();
539                         queue = true; 
540                 }
541                 gdk_region_union_with_rect (region, &last_peak_rect);
542         } 
543
544         if (queue) {
545                 gdk_window_invalidate_region (win->gobj(), region, true);
546         }
547         if (region) {
548                 gdk_region_destroy(region);
549                 region = 0;
550         }
551 }
552
553 void
554 FastMeter::queue_horizontal_redraw (const Glib::RefPtr<Gdk::Window>& win, float old_level)
555 {
556         /* XXX OPTIMIZE (when we have some horizontal meters) */
557         queue_draw ();
558 }
559
560 void
561 FastMeter::clear ()
562 {
563         current_level = 0;
564         current_peak = 0;
565         hold_state = 0;
566         queue_draw ();
567 }