2005-05-13 16:47:18 -04:00
|
|
|
/*
|
2006-02-13 11:08:32 -05:00
|
|
|
Copyright (C) 2003-2006 Paul Davis
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <gdkmm/rectangle.h>
|
|
|
|
#include <gtkmm2ext/fastmeter.h>
|
2006-03-14 02:46:59 -05:00
|
|
|
#include <gtkmm2ext/utils.h>
|
2005-05-13 16:47:18 -04:00
|
|
|
#include <gtkmm/style.h>
|
|
|
|
|
|
|
|
using namespace Gtk;
|
2005-12-07 12:15:37 -05:00
|
|
|
using namespace Gdk;
|
|
|
|
using namespace Glib;
|
2005-05-13 16:47:18 -04:00
|
|
|
using namespace Gtkmm2ext;
|
|
|
|
using namespace std;
|
|
|
|
|
2006-03-14 15:51:55 -05:00
|
|
|
string FastMeter::v_image_path;
|
|
|
|
string FastMeter::h_image_path;
|
2005-12-07 12:15:37 -05:00
|
|
|
RefPtr<Pixmap> FastMeter::v_pixmap;
|
|
|
|
RefPtr<Bitmap> FastMeter::v_mask;
|
2005-05-13 16:47:18 -04:00
|
|
|
gint FastMeter::v_pixheight = 0;
|
|
|
|
gint FastMeter::v_pixwidth = 0;
|
|
|
|
|
2005-12-07 12:15:37 -05:00
|
|
|
RefPtr<Pixmap> FastMeter::h_pixmap;
|
|
|
|
RefPtr<Bitmap> FastMeter::h_mask;
|
2005-05-13 16:47:18 -04:00
|
|
|
gint FastMeter::h_pixheight = 0;
|
|
|
|
gint FastMeter::h_pixwidth = 0;
|
|
|
|
|
|
|
|
FastMeter::FastMeter (long hold, unsigned long dimen, Orientation o)
|
|
|
|
{
|
|
|
|
orientation = o;
|
|
|
|
hold_cnt = hold;
|
|
|
|
hold_state = 0;
|
|
|
|
current_peak = 0;
|
|
|
|
current_level = 0;
|
|
|
|
current_user_level = -100.0f;
|
|
|
|
|
2005-12-07 12:15:37 -05:00
|
|
|
set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
|
2005-12-06 11:21:06 -05:00
|
|
|
|
2006-01-03 02:06:57 -05:00
|
|
|
pixrect.x = 0;
|
|
|
|
pixrect.y = 0;
|
2005-05-13 16:47:18 -04:00
|
|
|
|
2006-03-14 15:51:55 -05:00
|
|
|
request_width = dimen;
|
|
|
|
request_height = dimen;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FastMeter::~FastMeter ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-03-14 15:51:55 -05:00
|
|
|
FastMeter::on_realize ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-03-14 15:51:55 -05:00
|
|
|
DrawingArea::on_realize ();
|
2005-05-13 16:47:18 -04:00
|
|
|
|
2006-03-14 15:51:55 -05:00
|
|
|
Glib::RefPtr<Gdk::Drawable> drawable = Glib::RefPtr<Gdk::Window>::cast_dynamic (get_window());
|
|
|
|
Gdk::Color transparent;
|
|
|
|
|
|
|
|
if (!v_image_path.empty() && v_pixmap == 0) {
|
|
|
|
gint w, h;
|
|
|
|
|
|
|
|
v_pixmap = Pixmap::create_from_xpm (drawable, Colormap::get_system(), v_mask, transparent, v_image_path);
|
2005-11-25 19:06:46 -05:00
|
|
|
v_pixmap->get_size(w, h);
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
v_pixheight = h;
|
|
|
|
v_pixwidth = w;
|
|
|
|
}
|
|
|
|
|
2006-03-14 15:51:55 -05:00
|
|
|
if (!h_image_path.empty() && h_pixmap == 0) {
|
2005-05-13 16:47:18 -04:00
|
|
|
gint w, h;
|
|
|
|
|
2006-03-14 15:51:55 -05:00
|
|
|
h_pixmap = Pixmap::create_from_xpm (drawable, Colormap::get_system(), h_mask, transparent, h_image_path);
|
2005-11-25 19:06:46 -05:00
|
|
|
h_pixmap->get_size(w, h);
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
h_pixheight = h;
|
|
|
|
h_pixwidth = w;
|
|
|
|
}
|
2006-03-14 15:51:55 -05:00
|
|
|
|
|
|
|
if (orientation == Vertical) {
|
|
|
|
pixrect.width = min (v_pixwidth, request_width);
|
|
|
|
pixrect.height = v_pixheight;
|
|
|
|
} else {
|
|
|
|
pixrect.width = h_pixwidth;
|
|
|
|
pixrect.height = min (h_pixheight, request_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
request_width = pixrect.width;
|
|
|
|
request_height= pixrect.height;
|
|
|
|
|
|
|
|
set_size_request (request_width, request_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::set_vertical_xpm (std::string path)
|
|
|
|
{
|
|
|
|
v_image_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::set_horizontal_xpm (std::string path)
|
|
|
|
{
|
|
|
|
h_image_path = path;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::set_hold_count (long val)
|
|
|
|
{
|
|
|
|
if (val < 1) {
|
|
|
|
val = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hold_cnt = val;
|
|
|
|
hold_state = 0;
|
|
|
|
current_peak = 0;
|
|
|
|
|
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::on_size_request (GtkRequisition* req)
|
|
|
|
{
|
|
|
|
req->width = request_width;
|
|
|
|
req->height = request_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FastMeter::on_expose_event (GdkEventExpose* ev)
|
|
|
|
{
|
|
|
|
if (orientation == Vertical) {
|
|
|
|
return vertical_expose (ev);
|
|
|
|
} else {
|
|
|
|
return horizontal_expose (ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FastMeter::vertical_expose (GdkEventExpose* ev)
|
|
|
|
{
|
|
|
|
gint top_of_meter;
|
2006-01-03 02:06:57 -05:00
|
|
|
GdkRectangle intersection;
|
2006-02-13 11:08:32 -05:00
|
|
|
GdkRectangle background;
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
top_of_meter = (gint) floor (v_pixheight * current_level);
|
2006-01-03 02:06:57 -05:00
|
|
|
pixrect.height = top_of_meter;
|
2005-05-13 16:47:18 -04:00
|
|
|
|
2006-02-13 11:08:32 -05:00
|
|
|
background.x = 0;
|
|
|
|
background.y = 0;
|
|
|
|
background.width = pixrect.width;
|
|
|
|
background.height = v_pixheight - top_of_meter;
|
|
|
|
|
|
|
|
if (gdk_rectangle_intersect (&background, &ev->area, &intersection)) {
|
|
|
|
get_window()->draw_rectangle (get_style()->get_black_gc(), true,
|
|
|
|
intersection.x, intersection.y,
|
|
|
|
intersection.width, intersection.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
|
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
/* draw the part of the meter image that we need. the area we draw is bounded "in reverse" (top->bottom)
|
|
|
|
*/
|
2006-02-13 11:08:32 -05:00
|
|
|
|
2006-01-03 23:10:08 -05:00
|
|
|
get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap,
|
|
|
|
intersection.x, v_pixheight - top_of_meter,
|
|
|
|
intersection.x, v_pixheight - top_of_meter,
|
|
|
|
intersection.width, intersection.height);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* draw peak bar */
|
|
|
|
|
|
|
|
if (hold_state) {
|
2006-01-03 23:10:08 -05:00
|
|
|
get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), v_pixmap,
|
|
|
|
intersection.x, v_pixheight - (gint) floor (v_pixheight * current_peak),
|
|
|
|
intersection.x, v_pixheight - (gint) floor (v_pixheight * current_peak),
|
|
|
|
intersection.width, 3);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FastMeter::horizontal_expose (GdkEventExpose* ev)
|
|
|
|
{
|
2006-01-03 02:06:57 -05:00
|
|
|
GdkRectangle intersection;
|
2005-05-13 16:47:18 -04:00
|
|
|
gint right_of_meter;
|
|
|
|
|
|
|
|
right_of_meter = (gint) floor (h_pixwidth * current_level);
|
2006-01-03 02:06:57 -05:00
|
|
|
pixrect.width = right_of_meter;
|
|
|
|
|
|
|
|
if (gdk_rectangle_intersect (&pixrect, &ev->area, &intersection)) {
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
/* draw the part of the meter image that we need.
|
|
|
|
*/
|
|
|
|
|
2006-01-03 23:10:08 -05:00
|
|
|
get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
|
|
|
|
intersection.x, intersection.y,
|
|
|
|
intersection.x, intersection.y,
|
|
|
|
intersection.width, intersection.height);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
2006-01-03 23:10:08 -05:00
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
/* draw peak bar */
|
2006-01-03 23:10:08 -05:00
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
if (hold_state) {
|
2006-01-03 23:10:08 -05:00
|
|
|
get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), h_pixmap,
|
|
|
|
right_of_meter, intersection.y,
|
|
|
|
right_of_meter, intersection.y,
|
|
|
|
3, intersection.height);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::set (float lvl, float usrlvl)
|
|
|
|
{
|
|
|
|
current_level = lvl;
|
|
|
|
current_user_level = usrlvl;
|
|
|
|
|
|
|
|
if (lvl > current_peak) {
|
|
|
|
current_peak = lvl;
|
|
|
|
hold_state = hold_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hold_state > 0) {
|
|
|
|
if (--hold_state == 0) {
|
|
|
|
current_peak = lvl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FastMeter::clear ()
|
|
|
|
{
|
|
|
|
current_level = 0;
|
|
|
|
current_peak = 0;
|
|
|
|
hold_state = 0;
|
|
|
|
queue_draw ();
|
|
|
|
}
|