2006-11-21 12:30:47 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2006 Paul Davis
|
|
|
|
|
|
|
|
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: fastmeter.h 570 2006-06-07 21:21:21Z sampo $
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <gtkmm2ext/pixfader.h>
|
|
|
|
|
|
|
|
using namespace Gtkmm2ext;
|
|
|
|
using namespace Gtk;
|
|
|
|
using namespace Gdk;
|
|
|
|
using namespace std;
|
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
PixFader::PixFader (Glib::RefPtr<Pixbuf> belt, Gtk::Adjustment& adj)
|
2006-11-21 12:30:47 -05:00
|
|
|
: adjustment (adj),
|
2006-11-21 16:40:38 -05:00
|
|
|
pixbuf (belt)
|
2006-11-21 12:30:47 -05:00
|
|
|
{
|
|
|
|
dragging = false;
|
|
|
|
default_value = adjustment.get_value();
|
|
|
|
last_drawn = -1;
|
2006-11-21 16:40:38 -05:00
|
|
|
pixheight = pixbuf->get_height();
|
2006-11-21 12:30:47 -05:00
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
view.x = 0;
|
|
|
|
view.y = 0;
|
|
|
|
view.width = pixbuf->get_width();
|
|
|
|
view.height = pixheight / 2;
|
|
|
|
|
|
|
|
unity_y = (int) rint (view.height - (default_value * view.height));
|
2006-11-21 12:30:47 -05:00
|
|
|
|
|
|
|
add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK);
|
|
|
|
|
|
|
|
adjustment.signal_value_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
|
|
|
|
adjustment.signal_changed().connect (mem_fun (*this, &PixFader::adjustment_changed));
|
|
|
|
}
|
|
|
|
|
|
|
|
PixFader::~PixFader ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PixFader::on_expose_event (GdkEventExpose* ev)
|
|
|
|
{
|
|
|
|
GdkRectangle intersection;
|
2006-11-21 16:40:38 -05:00
|
|
|
int dh = display_height ();
|
|
|
|
int offset_into_pixbuf = (int) floor (view.height / ((float) view.height / dh));
|
2006-11-21 12:30:47 -05:00
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
if (gdk_rectangle_intersect (&view, &ev->area, &intersection)) {
|
|
|
|
get_window()->draw_pixbuf(get_style()->get_fg_gc(get_state()), pixbuf,
|
|
|
|
intersection.x, offset_into_pixbuf + intersection.y,
|
|
|
|
0, 0,
|
2006-11-21 12:30:47 -05:00
|
|
|
intersection.width, intersection.height,
|
|
|
|
Gdk::RGB_DITHER_NONE, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* always draw the line */
|
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
get_window()->draw_line (get_style()->get_fg_gc(get_state()), 0, unity_y, view.width - 2, unity_y);
|
2006-11-21 12:30:47 -05:00
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
last_drawn = dh;
|
2006-11-21 12:30:47 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PixFader::on_size_request (GtkRequisition* req)
|
|
|
|
{
|
2006-11-21 16:40:38 -05:00
|
|
|
req->width = view.width;
|
|
|
|
req->height = view.height;
|
2006-11-21 12:30:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PixFader::on_button_press_event (GdkEventButton* ev)
|
|
|
|
{
|
|
|
|
switch (ev->button) {
|
|
|
|
case 1:
|
2006-11-21 16:40:38 -05:00
|
|
|
case 2:
|
2006-11-21 12:30:47 -05:00
|
|
|
if (!(ev->state & Gdk::SHIFT_MASK)) {
|
|
|
|
add_modal_grab();
|
|
|
|
grab_y = ev->y;
|
|
|
|
grab_start = ev->y;
|
|
|
|
grab_window = ev->window;
|
|
|
|
dragging = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PixFader::on_button_release_event (GdkEventButton* ev)
|
|
|
|
{
|
|
|
|
double scale;
|
2006-11-21 16:40:38 -05:00
|
|
|
double fract;
|
2006-11-21 12:30:47 -05:00
|
|
|
|
|
|
|
if (ev->state & GDK_CONTROL_MASK) {
|
|
|
|
if (ev->state & GDK_MOD1_MASK) {
|
|
|
|
scale = 0.05;
|
|
|
|
} else {
|
|
|
|
scale = 0.1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scale = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ev->button) {
|
|
|
|
case 1:
|
|
|
|
if (dragging) {
|
|
|
|
remove_modal_grab();
|
|
|
|
dragging = false;
|
|
|
|
|
|
|
|
if (ev->y == grab_start) {
|
|
|
|
/* no motion - just a click */
|
2006-11-21 16:40:38 -05:00
|
|
|
|
|
|
|
if (ev->y < view.height - display_height()) {
|
|
|
|
/* above the current display height, remember X Window coords */
|
|
|
|
adjustment.set_value (adjustment.get_value() + adjustment.get_step_increment());
|
2006-11-21 12:30:47 -05:00
|
|
|
} else {
|
2006-11-21 16:40:38 -05:00
|
|
|
adjustment.set_value (adjustment.get_value() - adjustment.get_step_increment());
|
2006-11-21 12:30:47 -05:00
|
|
|
}
|
|
|
|
}
|
2006-11-21 16:40:38 -05:00
|
|
|
|
2006-11-21 12:30:47 -05:00
|
|
|
} else {
|
2006-11-21 16:40:38 -05:00
|
|
|
|
2006-11-21 12:30:47 -05:00
|
|
|
if (ev->state & Gdk::SHIFT_MASK) {
|
|
|
|
adjustment.set_value (default_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-11-21 16:40:38 -05:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (dragging) {
|
|
|
|
remove_modal_grab();
|
|
|
|
dragging = false;
|
|
|
|
|
|
|
|
fract = 1.0 - (ev->y / view.height); // inverted X Window coordinates, grrr
|
|
|
|
|
|
|
|
fract = min (1.0, fract);
|
|
|
|
fract = max (0.0, fract);
|
|
|
|
|
|
|
|
adjustment.set_value (scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-11-21 12:30:47 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-11-21 16:40:38 -05:00
|
|
|
|
2006-11-21 12:30:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PixFader::on_scroll_event (GdkEventScroll* ev)
|
|
|
|
{
|
|
|
|
double scale;
|
|
|
|
|
|
|
|
if (ev->state & GDK_CONTROL_MASK) {
|
|
|
|
if (ev->state & GDK_MOD1_MASK) {
|
|
|
|
scale = 0.05;
|
|
|
|
} else {
|
|
|
|
scale = 0.1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scale = 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ev->direction) {
|
|
|
|
|
|
|
|
case GDK_SCROLL_UP:
|
|
|
|
/* wheel up */
|
|
|
|
adjustment.set_value (adjustment.get_value() + (adjustment.get_page_increment() * scale));
|
|
|
|
break;
|
|
|
|
case GDK_SCROLL_DOWN:
|
|
|
|
/* wheel down */
|
|
|
|
adjustment.set_value (adjustment.get_value() - (adjustment.get_page_increment() * scale));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PixFader::on_motion_notify_event (GdkEventMotion* ev)
|
|
|
|
{
|
|
|
|
if (dragging) {
|
|
|
|
double fract;
|
|
|
|
double delta;
|
|
|
|
double scale;
|
|
|
|
|
|
|
|
if (ev->window != grab_window) {
|
|
|
|
grab_y = ev->y;
|
|
|
|
grab_window = ev->window;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->state & GDK_CONTROL_MASK) {
|
|
|
|
if (ev->state & GDK_MOD1_MASK) {
|
|
|
|
scale = 0.05;
|
|
|
|
} else {
|
|
|
|
scale = 0.1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scale = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
delta = ev->y - grab_y;
|
|
|
|
grab_y = ev->y;
|
|
|
|
|
2006-11-21 16:40:38 -05:00
|
|
|
fract = (delta / view.height);
|
2006-11-21 12:30:47 -05:00
|
|
|
|
|
|
|
fract = min (1.0, fract);
|
|
|
|
fract = max (-1.0, fract);
|
|
|
|
|
|
|
|
// X Window is top->bottom for 0..Y
|
|
|
|
|
|
|
|
fract = -fract;
|
|
|
|
|
|
|
|
adjustment.set_value (adjustment.get_value() + scale * fract * (adjustment.get_upper() - adjustment.get_lower()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PixFader::adjustment_changed ()
|
|
|
|
{
|
|
|
|
if (display_height() != last_drawn) {
|
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
}
|
2006-11-21 16:40:38 -05:00
|
|
|
|
|
|
|
int
|
|
|
|
PixFader::display_height ()
|
|
|
|
{
|
|
|
|
float fract = (adjustment.get_upper() - adjustment.get_value ()) / ((adjustment.get_upper() - adjustment.get_lower()));
|
|
|
|
return (int) floor (view.height * (1.0 - fract));
|
|
|
|
}
|