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>
33 using namespace Gtkmm2ext;
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;
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;
46 FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o)
53 current_user_level = -100.0f;
55 set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
61 if (orientation == Vertical) {
62 pixbuf = request_vertical_meter(250);
64 pixbuf = request_horizontal_meter(186);
67 pixheight = pixbuf->get_height();
68 pixwidth = pixbuf->get_width();
70 if (orientation == Vertical) {
71 pixrect.width = min (pixwidth, (gint) dimen);
72 pixrect.height = pixheight;
74 pixrect.width = pixwidth;
75 pixrect.height = min (pixheight, (gint) dimen);
78 request_width = pixrect.width;
79 request_height= pixrect.height;
82 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int length)
84 if (length < min_v_pixbuf_size)
85 length = min_v_pixbuf_size;
86 if (length > max_v_pixbuf_size)
87 length = max_v_pixbuf_size;
89 int index = length - 1;
91 if (v_pixbuf_cache == 0) {
92 v_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
93 memset(v_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_v_pixbuf_size);
95 Glib::RefPtr<Gdk::Pixbuf> ret = v_pixbuf_cache[index];
103 data = (guint8*) malloc(width*height * 3);
110 // fake log calculation copied from log_meter.h
111 // actual calculation:
113 // def = (0.0f + 20.0f) * 2.5f + 50f
114 // return def / 115.0f
115 int knee = (int)floor((float)height * 100.0f / 115.0f);
119 for (y = 0; y < knee / 2; y++) {
121 r = (guint8)floor(255.0 * (float)y/(float)(knee / 2));
123 for (int x = 0; x < width; x++) {
124 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
125 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
126 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
130 for (; y < knee; y++) {
132 g = 255 - (guint8)floor(170.0 * (float)(y - knee/ 2)/(float)(knee / 2));
134 for (int x = 0; x < width; x++) {
135 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
136 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
137 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
144 for (; y < height; y++) {
145 for (int x = 0; x < width; x++) {
146 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
147 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
148 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
152 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
153 v_pixbuf_cache[index] = ret;
158 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int length)
160 if (length < min_h_pixbuf_size)
161 length = min_h_pixbuf_size;
162 if (length > max_h_pixbuf_size)
163 length = max_h_pixbuf_size;
165 int index = length - 1;
167 if (h_pixbuf_cache == 0) {
168 h_pixbuf_cache = (Glib::RefPtr<Gdk::Pixbuf>*) malloc(sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
169 memset(h_pixbuf_cache,0,sizeof(Glib::RefPtr<Gdk::Pixbuf>) * max_h_pixbuf_size);
171 Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
179 data = (guint8*) malloc(width*height * 3);
186 // fake log calculation copied from log_meter.h
187 // actual calculation:
189 // def = (0.0f + 20.0f) * 2.5f + 50f
190 // return def / 115.0f
191 int knee = (int)floor((float)width * 100.0f / 115.0f);
195 for (x = 0; x < knee / 2; x++) {
197 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
199 for (int y = 0; y < height; y++) {
200 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
201 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
202 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
206 for (; x < knee; x++) {
208 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
210 for (int y = 0; y < height; y++) {
211 data[ (x+(height-y-1)*width) * 3 + 0 ] = r;
212 data[ (x+(height-y-1)*width) * 3 + 1 ] = g;
213 data[ (x+(height-y-1)*width) * 3 + 2 ] = b;
220 for (; x < width; x++) {
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 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
229 h_pixbuf_cache[index] = ret;
234 FastMeter::~FastMeter ()
239 FastMeter::set_hold_count (long val)
253 FastMeter::on_size_request (GtkRequisition* req)
255 if (orientation == Vertical) {
256 req->height = request_height;
258 req->height = max(req->height, min_v_pixbuf_size);
259 req->height = min(req->height, max_v_pixbuf_size);
263 req->width = request_width;
265 req->width = max(req->width, min_h_pixbuf_size);
266 req->width = min(req->width, max_h_pixbuf_size);
274 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
276 if (orientation == Vertical) {
277 if (alloc.get_width() != 5) {
281 int h = alloc.get_height();
282 h = max(h, min_v_pixbuf_size);
283 h = min(h, max_v_pixbuf_size);
285 if ( h != alloc.get_height())
288 if (pixheight != h) {
289 pixbuf = request_vertical_meter(h);
292 if (alloc.get_height() != 5) {
296 int w = alloc.get_width();
297 w = max(w, min_h_pixbuf_size);
298 w = min(w, max_h_pixbuf_size);
300 if ( w != alloc.get_width())
304 pixbuf = request_horizontal_meter(w);
308 pixheight = pixbuf->get_height();
309 pixwidth = pixbuf->get_width();
311 DrawingArea::on_size_allocate(alloc);
315 FastMeter::on_expose_event (GdkEventExpose* ev)
317 if (orientation == Vertical) {
318 return vertical_expose (ev);
320 return horizontal_expose (ev);
325 FastMeter::vertical_expose (GdkEventExpose* ev)
328 GdkRectangle intersection;
329 GdkRectangle background;
331 top_of_meter = (gint) floor (pixheight * current_level);
332 pixrect.height = top_of_meter;
336 background.width = pixrect.width;
337 background.height = pixheight - top_of_meter;
339 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
340 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
341 intersection.x, intersection.y,
342 intersection.width, intersection.height);
345 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
346 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
347 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
348 intersection.x, pixheight - top_of_meter,
349 intersection.x, pixheight - top_of_meter,
350 intersection.width, intersection.height,
351 Gdk::RGB_DITHER_NONE, 0, 0);
355 if (hold_state && intersection.width > 0) {
356 gint y = pixheight - (gint) floor (pixheight * current_peak);
357 int h = min(3, pixheight - y);
359 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
362 intersection.width, h,
363 Gdk::RGB_DITHER_NONE, 0, 0);
370 FastMeter::horizontal_expose (GdkEventExpose* ev)
373 GdkRectangle intersection;
374 GdkRectangle background;
376 right_of_meter = (gint) floor (pixwidth * current_level);
377 pixrect.width = right_of_meter;
381 background.width = pixwidth - right_of_meter;
382 background.height = pixrect.height;
384 if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
385 get_window()->draw_rectangle (get_style()->get_black_gc(), true,
386 intersection.x + right_of_meter, intersection.y,
387 intersection.width, intersection.height);
390 if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
391 // draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
392 get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
393 intersection.x, intersection.y,
394 intersection.x, intersection.y,
395 intersection.width, intersection.height,
396 Gdk::RGB_DITHER_NONE, 0, 0);
400 // XXX: peaks don't work properly
402 if (hold_state && intersection.height > 0) {
403 gint x = (gint) floor(pixwidth * current_peak);
405 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
408 3, intersection.height,
409 Gdk::RGB_DITHER_NONE, 0, 0);
417 FastMeter::set (float lvl, float usrlvl)
420 current_user_level = usrlvl;
422 if (lvl > current_peak) {
424 hold_state = hold_cnt;
427 if (hold_state > 0) {
428 if (--hold_state == 0) {