merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[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 using namespace Gtk;
31 using namespace Gdk;
32 using namespace Glib;
33 using namespace Gtkmm2ext;
34 using namespace std;
35
36
37 int FastMeter::min_v_pixbuf_size = 50;
38 int FastMeter::max_v_pixbuf_size = 1024;
39 Glib::RefPtr<Gdk::Pixbuf>* FastMeter::v_pixbuf_cache = 0;
40
41 int FastMeter::min_h_pixbuf_size = 50;
42 int FastMeter::max_h_pixbuf_size = 1024;
43 Glib::RefPtr<Gdk::Pixbuf>* FastMeter::h_pixbuf_cache = 0;
44
45
46 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o)
47 {
48         orientation = o;
49         hold_cnt = hold;
50         hold_state = 0;
51         current_peak = 0;
52         current_level = 0;
53         
54         set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
55
56         pixrect.x = 0;
57         pixrect.y = 0;
58
59         if (orientation == Vertical) {
60                 pixbuf = request_vertical_meter(dimen, 250);
61         } else {
62                 pixbuf = request_horizontal_meter(186, dimen);
63         }
64
65         pixheight = pixbuf->get_height();
66         pixwidth  = pixbuf->get_width();
67
68         if (orientation == Vertical) {
69                 pixrect.width = min (pixwidth, (gint) dimen);
70                 pixrect.height = pixheight;
71         } else {
72                 pixrect.width = pixwidth;
73                 pixrect.height = min (pixheight, (gint) dimen);
74         }
75
76         request_width = pixrect.width;
77         request_height= pixrect.height;
78 }
79
80 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
81 {
82         if (height < min_v_pixbuf_size)
83                 height = min_v_pixbuf_size;
84         if (height > max_v_pixbuf_size)
85                 height = max_v_pixbuf_size;
86         
87         int index = height - 1;
88
89         if (v_pixbuf_cache == 0) {
90                 v_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
91                 memset(v_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
92         }
93         Glib::RefPtr<Gdk::Pixbuf> ret = v_pixbuf_cache[index];
94         if (ret)
95                 return ret;
96
97         guint8* data;
98
99         data = (guint8*) malloc(width*height * 3);
100         
101         guint8 r,g,b;
102         r=0;
103         g=255;
104         b=0;
105
106         // fake log calculation copied from log_meter.h
107         // actual calculation:
108         // log_meter(0.0f) =
109         //  def = (0.0f + 20.0f) * 2.5f + 50f
110         //  return def / 115.0f
111         int knee = (int)floor((float)height * 100.0f / 115.0f);
112         
113         int y;
114         
115         for (y = 0; y < knee / 2; y++) {
116
117                 r = (guint8)floor(255.0 * (float)y/(float)(knee / 2));
118                 
119                 for (int x = 0; x < width; x++) {
120                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
121                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
122                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
123                 }
124         }
125         
126         for (; y < knee; y++) {
127
128                 g = 255 - (guint8)floor(170.0 * (float)(y - knee/ 2)/(float)(knee / 2));
129                 
130                 for (int x = 0; x < width; x++) {
131                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
132                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
133                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
134                 }
135         }
136
137         r=255;
138         g=0;
139         b=0;
140         for (; y < height; y++) {
141                 for (int x = 0; x < width; x++) {
142                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
143                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
144                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
145                 }
146         }
147         
148         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
149         v_pixbuf_cache[index] = ret;
150
151         return ret;
152 }
153
154 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
155 {
156         if (width < min_h_pixbuf_size)
157                 width = min_h_pixbuf_size;
158         if (width > max_h_pixbuf_size)
159                 width = max_h_pixbuf_size;
160         
161         int index = width - 1;
162         
163         if (h_pixbuf_cache == 0) {
164                 h_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
165                 memset(h_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
166         }
167         Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
168         if (ret)
169                 return ret;
170
171         guint8* data;
172
173         data = (guint8*) malloc(width*height * 3);
174         
175         guint8 r,g,b;
176         r=0;
177         g=255;
178         b=0;
179
180         // fake log calculation copied from log_meter.h
181         // actual calculation:
182         // log_meter(0.0f) =
183         //  def = (0.0f + 20.0f) * 2.5f + 50f
184         //  return def / 115.0f
185         int knee = (int)floor((float)width * 100.0f / 115.0f);
186         
187         int x;
188         
189         for (x = 0; x < knee / 2; x++) {
190
191                 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
192                 
193                 for (int y = 0; y < height; y++) {
194                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
195                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
196                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
197                 }
198         }
199         
200         for (; x < knee; x++) {
201
202                 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
203                 
204                 for (int y = 0; y < height; y++) {
205                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
206                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
207                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
208                 }
209         }
210
211         r=255;
212         g=0;
213         b=0;
214         for (; x < width; x++) {
215                 for (int y = 0; y < height; y++) {
216                         data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
217                         data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
218                         data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
219                 }
220         }
221         
222         ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
223         h_pixbuf_cache[index] = ret;
224
225         return ret;
226 }
227
228 FastMeter::~FastMeter ()
229 {
230 }
231
232 void
233 FastMeter::set_hold_count (long val)
234 {
235         if (val < 1) {
236                 val = 1;
237         }
238         
239         hold_cnt = val;
240         hold_state = 0;
241         current_peak = 0;
242         
243         queue_draw ();
244 }
245
246 void
247 FastMeter::on_size_request (GtkRequisition* req)
248 {
249         if (orientation == Vertical) {
250
251                 req->height = request_height;
252                 req->height = max(req->height, min_v_pixbuf_size);
253                 req->height = min(req->height, max_v_pixbuf_size);
254
255                 req->width  = request_width;
256
257         } else {
258
259                 req->width  = request_width;
260                 req->width  = max(req->width,  min_h_pixbuf_size);
261                 req->width  = min(req->width,  max_h_pixbuf_size);
262
263                 req->height = request_height;
264         }
265
266 }
267
268 void
269 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
270 {
271         if (orientation == Vertical) {
272
273                 if (alloc.get_width() != request_width) {
274                         alloc.set_width (request_width);
275                 }
276
277                 int h = alloc.get_height();
278                 h = max(h, min_v_pixbuf_size);
279                 h = min(h, max_v_pixbuf_size);
280
281                 if ( h != alloc.get_height())
282                         alloc.set_height(h);
283
284                 if (pixheight != h) {
285                         pixbuf = request_vertical_meter(request_width, h);
286                 }
287
288         } else {
289
290                 if (alloc.get_height() != request_height) {
291                         alloc.set_height(request_height);
292                 }
293
294                 int w = alloc.get_width();
295                 w = max(w, min_h_pixbuf_size);
296                 w = min(w, max_h_pixbuf_size);
297
298                 if ( w != alloc.get_width())
299                         alloc.set_width(w);
300
301                 if (pixwidth != w) {
302                         pixbuf = request_horizontal_meter(w, request_height);
303                 }
304         }
305
306         pixheight = pixbuf->get_height();
307         pixwidth  = pixbuf->get_width();
308
309         DrawingArea::on_size_allocate(alloc);
310 }
311
312 bool
313 FastMeter::on_expose_event (GdkEventExpose* ev)
314 {
315         if (orientation == Vertical) {
316                 return vertical_expose (ev);
317         } else {
318                 return horizontal_expose (ev);
319         }
320 }
321
322 bool
323 FastMeter::vertical_expose (GdkEventExpose* ev)
324 {
325         gint top_of_meter;
326         GdkRectangle intersection;
327         GdkRectangle background;
328
329         top_of_meter = (gint) floor (pixheight * current_level);
330         pixrect.height = top_of_meter;
331
332         background.x = 0;
333         background.y = 0;
334         background.width = pixrect.width;
335         background.height = pixheight - top_of_meter;
336
337     if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
338                 get_window()->draw_rectangle (get_style()->get_black_gc(), true, 
339                                               intersection.x, intersection.y,
340                                               intersection.width, intersection.height);
341         }
342         
343         if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
344                 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
345                 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf, 
346                                           intersection.x, pixheight - top_of_meter,
347                                           intersection.x, pixheight - top_of_meter,
348                                           intersection.width, intersection.height,
349                                           Gdk::RGB_DITHER_NONE, 0, 0);
350         }
351
352         // draw peak bar 
353         if (hold_state && intersection.width > 0) {
354                 gint y = pixheight - (gint) floor (pixheight * current_peak);
355                 int h = min(3, pixheight - y);
356
357                 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
358                                            intersection.x, y,
359                                            intersection.x, y,
360                                            intersection.width, h,
361                                            Gdk::RGB_DITHER_NONE, 0, 0);
362         }
363
364         return TRUE;
365 }
366
367 bool
368 FastMeter::horizontal_expose (GdkEventExpose* ev)
369 {
370         gint right_of_meter;
371         GdkRectangle intersection;
372         GdkRectangle background;
373
374         right_of_meter = (gint) floor (pixwidth * current_level);
375         pixrect.width = right_of_meter;
376
377         background.x = 0;
378         background.y = 0;
379         background.width  = pixwidth - right_of_meter;
380         background.height = pixrect.height;
381
382     if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
383                 get_window()->draw_rectangle (get_style()->get_black_gc(), true, 
384                                               intersection.x + right_of_meter, intersection.y,
385                                               intersection.width, intersection.height);
386         }
387         
388         if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
389                 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
390                 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf, 
391                                           intersection.x, intersection.y,
392                                           intersection.x, intersection.y,
393                                           intersection.width, intersection.height,
394                                           Gdk::RGB_DITHER_NONE, 0, 0);
395         }
396
397         // draw peak bar 
398         // XXX: peaks don't work properly
399         /*
400         if (hold_state && intersection.height > 0) {
401                 gint x = (gint) floor(pixwidth * current_peak);
402
403                 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
404                                            x, intersection.y,
405                                            x, intersection.y,
406                                            3, intersection.height,
407                                            Gdk::RGB_DITHER_NONE, 0, 0);
408         }
409         */
410         
411         return true;
412 }
413
414 void
415 FastMeter::set (float lvl)
416 {
417         current_level = lvl;
418         
419         if (lvl > current_peak) {
420                 current_peak = lvl;
421                 hold_state = hold_cnt;
422         }
423         
424         if (hold_state > 0) {
425                 if (--hold_state == 0) {
426                         current_peak = lvl;
427                 }
428         }
429
430         queue_draw ();
431 }
432
433 void
434 FastMeter::clear ()
435 {
436         current_level = 0;
437         current_peak = 0;
438         hold_state = 0;
439         queue_draw ();
440 }