Merge with 2.0-ongoing R2885.
[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 <string.h>
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         _clr0 = clr0;
60         _clr1 = clr1;
61         _clr2 = clr2;
62         _clr3 = clr3;
63         
64         set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
65
66         pixrect.x = 0;
67         pixrect.y = 0;
68
69         if (orientation == Vertical) {
70                 if (!len)
71                         len = 250;
72                 pixbuf = request_vertical_meter(dimen, len);
73         } else {
74                 if (!len)
75                         len = 186;
76                 pixbuf = request_horizontal_meter(len, dimen);
77         }
78
79         pixheight = pixbuf->get_height();
80         pixwidth  = pixbuf->get_width();
81
82         if (orientation == Vertical) {
83                 pixrect.width = min (pixwidth, (gint) dimen);
84                 pixrect.height = pixheight;
85         } else {
86                 pixrect.width = pixwidth;
87                 pixrect.height = min (pixheight, (gint) dimen);
88         }
89
90         request_width = pixrect.width;
91         request_height= pixrect.height;
92 }
93
94 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
95 {
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;
100         
101         //int index = height - 1;
102
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);
106         //}
107         Glib::RefPtr<Gdk::Pixbuf> ret;// = v_pixbuf_cache[index];
108         //if (ret)
109         //      return ret;
110
111         guint8* data;
112
113         data = (guint8*) malloc(width*height * 3);
114
115         guint8 r,g,b,r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3,a;
116
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:
123         // log_meter(0.0f) =
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);
127         int y;
128
129         for (y = 0; y < knee/2; y++) {
130
131                 r = (guint8)floor((float)abs(r1 - r0) * (float)y / (float)(knee/2));
132                 (r0 >= r1) ? r = r0 - r : r += r0;
133                         
134                 g = (guint8)floor((float)abs(g1 - g0) * (float)y / (float)(knee/2));
135                 (g0 >= g1) ? g = g0 - g : g += g0;
136
137                 b = (guint8)floor((float)abs(b1 - b0) * (float)y / (float)(knee/2));
138                 (b0 >= b1) ? b = b0 - b : b += b0;
139
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;
144                 }
145         }
146
147         int offset = knee - y;
148         for (int i=0; i < offset; i++,y++) {
149
150                 r = (guint8)floor((float)abs(r2 - r1) * (float)i / (float)offset);
151                 (r1 >= r2) ? r = r1 - r : r += r1;
152
153                 g = (guint8)floor((float)abs(g2 - g1) * (float)i / (float)offset);
154                 (g1 >= g2) ? g = g1 - g : g += g1;
155
156                 b = (guint8)floor((float)abs(b2 - b1) * (float)i / (float)offset);
157                 (b1 >= b2) ? b = b1 - b : b += b1;
158
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;
163                 }
164         }
165         y--;
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;
171                 }
172         }
173         
174         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
175         //v_pixbuf_cache[index] = ret;
176
177         return ret;
178 }
179
180 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
181 {
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;
186         
187         int index = width - 1;
188         
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);
192         }
193         Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
194         if (ret)
195                 return ret;
196
197         guint8* data;
198
199         data = (guint8*) malloc(width*height * 3);
200         
201         guint8 r,g,b;
202         r=0;
203         g=255;
204         b=0;
205
206         // fake log calculation copied from log_meter.h
207         // actual calculation:
208         // log_meter(0.0f) =
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);
212         
213         int x;
214         
215         for (x = 0; x < knee / 2; x++) {
216
217                 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
218                 
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;
223                 }
224         }
225         
226         for (; x < knee; x++) {
227
228                 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
229                 
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;
234                 }
235         }
236
237         r=255;
238         g=0;
239         b=0;
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;
245                 }
246         }
247         
248         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
249         h_pixbuf_cache[index] = ret;
250
251         return ret;
252 }
253
254 FastMeter::~FastMeter ()
255 {
256 }
257
258 void
259 FastMeter::set_hold_count (long val)
260 {
261         if (val < 1) {
262                 val = 1;
263         }
264         
265         hold_cnt = val;
266         hold_state = 0;
267         current_peak = 0;
268         
269         queue_draw ();
270 }
271
272 void
273 FastMeter::on_size_request (GtkRequisition* req)
274 {
275         if (orientation == Vertical) {
276
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);
280
281                 req->width  = request_width;
282
283         } else {
284
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);
288
289                 req->height = request_height;
290         }
291
292 }
293
294 void
295 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
296 {
297         if (orientation == Vertical) {
298
299                 if (alloc.get_width() != request_width) {
300                         alloc.set_width (request_width);
301                 }
302
303                 int h = alloc.get_height();
304                 h = max(h, min_v_pixbuf_size);
305                 h = min(h, max_v_pixbuf_size);
306
307                 if ( h != alloc.get_height())
308                         alloc.set_height(h);
309
310                 if (pixheight != h) {
311                         pixbuf = request_vertical_meter(request_width, h);
312                 }
313
314         } else {
315
316                 if (alloc.get_height() != request_height) {
317                         alloc.set_height(request_height);
318                 }
319
320                 int w = alloc.get_width();
321                 w = max(w, min_h_pixbuf_size);
322                 w = min(w, max_h_pixbuf_size);
323
324                 if ( w != alloc.get_width())
325                         alloc.set_width(w);
326
327                 if (pixwidth != w) {
328                         pixbuf = request_horizontal_meter(w, request_height);
329                 }
330         }
331
332         pixheight = pixbuf->get_height();
333         pixwidth  = pixbuf->get_width();
334
335         DrawingArea::on_size_allocate(alloc);
336 }
337
338 bool
339 FastMeter::on_expose_event (GdkEventExpose* ev)
340 {
341         if (orientation == Vertical) {
342                 return vertical_expose (ev);
343         } else {
344                 return horizontal_expose (ev);
345         }
346 }
347
348 bool
349 FastMeter::vertical_expose (GdkEventExpose* ev)
350 {
351         gint top_of_meter;
352         GdkRectangle intersection;
353         GdkRectangle background;
354
355         top_of_meter = (gint) floor (pixheight * current_level);
356         
357         /* reset the height & origin of the rect that needs to show the pixbuf
358          */
359
360         pixrect.height = top_of_meter;
361         pixrect.y = pixheight - top_of_meter;
362
363         background.x = 0;
364         background.y = 0;
365         background.width = pixrect.width;
366         background.height = pixheight - top_of_meter;
367
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);
372         }
373
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);
381         }
382
383         // draw peak bar 
384         if (hold_state && intersection.width > 0) {
385                 gint y = pixheight - (gint) floor (pixheight * current_peak);
386                 int h = min(3, pixheight - y);
387
388                 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
389                                            intersection.x, y,
390                                            intersection.x, y,
391                                            intersection.width, h,
392                                            Gdk::RGB_DITHER_NONE, 0, 0);
393         }
394
395         return TRUE;
396 }
397
398 bool
399 FastMeter::horizontal_expose (GdkEventExpose* ev)
400 {
401         gint right_of_meter;
402         GdkRectangle intersection;
403         GdkRectangle background;
404
405         right_of_meter = (gint) floor (pixwidth * current_level);
406         pixrect.width = right_of_meter;
407
408         background.x = 0;
409         background.y = 0;
410         background.width  = pixwidth - right_of_meter;
411         background.height = pixrect.height;
412
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);
417         }
418         
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);
426         }
427
428         // draw peak bar 
429         // XXX: peaks don't work properly
430         /*
431         if (hold_state && intersection.height > 0) {
432                 gint x = (gint) floor(pixwidth * current_peak);
433
434                 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
435                                            x, intersection.y,
436                                            x, intersection.y,
437                                            3, intersection.height,
438                                            Gdk::RGB_DITHER_NONE, 0, 0);
439         }
440         */
441         
442         return true;
443 }
444
445 void
446 FastMeter::set (float lvl)
447 {
448         current_level = lvl;
449         
450         if (lvl > current_peak) {
451                 current_peak = lvl;
452                 hold_state = hold_cnt;
453         }
454         
455         if (hold_state > 0) {
456                 if (--hold_state == 0) {
457                         current_peak = lvl;
458                 }
459         }
460
461         queue_draw ();
462 }
463
464 void
465 FastMeter::clear ()
466 {
467         current_level = 0;
468         current_peak = 0;
469         hold_state = 0;
470         queue_draw ();
471 }