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)
54 set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
59 if (orientation == Vertical) {
60 pixbuf = request_vertical_meter(dimen, 250);
62 pixbuf = request_horizontal_meter(186, dimen);
65 pixheight = pixbuf->get_height();
66 pixwidth = pixbuf->get_width();
68 if (orientation == Vertical) {
69 pixrect.width = min (pixwidth, (gint) dimen);
70 pixrect.height = pixheight;
72 pixrect.width = pixwidth;
73 pixrect.height = min (pixheight, (gint) dimen);
76 request_width = pixrect.width;
77 request_height= pixrect.height;
80 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_vertical_meter(int width, int height)
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;
87 int index = height - 1;
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);
93 Glib::RefPtr<Gdk::Pixbuf> ret = v_pixbuf_cache[index];
99 data = (guint8*) malloc(width*height * 3);
106 // fake log calculation copied from log_meter.h
107 // actual calculation:
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);
115 for (y = 0; y < knee / 2; y++) {
117 r = (guint8)floor(255.0 * (float)y/(float)(knee / 2));
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;
126 for (; y < knee; y++) {
128 g = 255 - (guint8)floor(170.0 * (float)(y - knee/ 2)/(float)(knee / 2));
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;
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;
148 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
149 v_pixbuf_cache[index] = ret;
154 Glib::RefPtr<Gdk::Pixbuf> FastMeter::request_horizontal_meter(int width, int height)
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;
161 int index = width - 1;
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);
167 Glib::RefPtr<Gdk::Pixbuf> ret = h_pixbuf_cache[index];
173 data = (guint8*) malloc(width*height * 3);
180 // fake log calculation copied from log_meter.h
181 // actual calculation:
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);
189 for (x = 0; x < knee / 2; x++) {
191 r = (guint8)floor(255.0 * (float)x/(float)(knee / 2));
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;
200 for (; x < knee; x++) {
202 g = 255 - (guint8)floor(170.0 * (float)(x - knee/ 2)/(float)(knee / 2));
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;
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;
222 ret = Pixbuf::create_from_data(data, COLORSPACE_RGB, false, 8, width, height, width * 3);
223 h_pixbuf_cache[index] = ret;
228 FastMeter::~FastMeter ()
233 FastMeter::set_hold_count (long val)
247 FastMeter::on_size_request (GtkRequisition* req)
249 if (orientation == Vertical) {
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);
255 req->width = request_width;
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);
263 req->height = request_height;
269 FastMeter::on_size_allocate (Gtk::Allocation &alloc)
271 if (orientation == Vertical) {
273 if (alloc.get_width() != request_width) {
274 alloc.set_width (request_width);
277 int h = alloc.get_height();
278 h = max(h, min_v_pixbuf_size);
279 h = min(h, max_v_pixbuf_size);
281 if ( h != alloc.get_height())
284 if (pixheight != h) {
285 pixbuf = request_vertical_meter(request_width, h);
290 if (alloc.get_height() != request_height) {
291 alloc.set_height(request_height);
294 int w = alloc.get_width();
295 w = max(w, min_h_pixbuf_size);
296 w = min(w, max_h_pixbuf_size);
298 if ( w != alloc.get_width())
302 pixbuf = request_horizontal_meter(w, request_height);
306 pixheight = pixbuf->get_height();
307 pixwidth = pixbuf->get_width();
309 DrawingArea::on_size_allocate(alloc);
313 FastMeter::on_expose_event (GdkEventExpose* ev)
315 if (orientation == Vertical) {
316 return vertical_expose (ev);
318 return horizontal_expose (ev);
323 FastMeter::vertical_expose (GdkEventExpose* ev)
326 GdkRectangle intersection;
327 GdkRectangle background;
329 top_of_meter = (gint) floor (pixheight * current_level);
330 pixrect.height = top_of_meter;
334 background.width = pixrect.width;
335 background.height = pixheight - top_of_meter;
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);
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);
353 if (hold_state && intersection.width > 0) {
354 gint y = pixheight - (gint) floor (pixheight * current_peak);
355 int h = min(3, pixheight - y);
357 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
360 intersection.width, h,
361 Gdk::RGB_DITHER_NONE, 0, 0);
368 FastMeter::horizontal_expose (GdkEventExpose* ev)
371 GdkRectangle intersection;
372 GdkRectangle background;
374 right_of_meter = (gint) floor (pixwidth * current_level);
375 pixrect.width = right_of_meter;
379 background.width = pixwidth - right_of_meter;
380 background.height = pixrect.height;
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);
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);
398 // XXX: peaks don't work properly
400 if (hold_state && intersection.height > 0) {
401 gint x = (gint) floor(pixwidth * current_peak);
403 get_window()->draw_pixbuf (get_style()->get_fg_gc(get_state()), pixbuf,
406 3, intersection.height,
407 Gdk::RGB_DITHER_NONE, 0, 0);
415 FastMeter::set (float lvl)
419 if (lvl > current_peak) {
421 hold_state = hold_cnt;
424 if (hold_state > 0) {
425 if (--hold_state == 0) {