2 Copyright (C) 2000-2007 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 "pbd/controllable.h"
26 #include "gtkmm2ext/gui_thread.h"
28 #include "ardour/panner.h"
29 #include "stereo_panner.h"
36 StereoPanner::StereoPanner (boost::shared_ptr<PBD::Controllable> position, boost::shared_ptr<PBD::Controllable> width)
37 : position_control (position)
38 , width_control (width)
40 , dragging_position (false)
44 set_size_request (-1, 15);
46 position_control->Changed.connect (connections, invalidator(*this), boost::bind (&DrawingArea::queue_draw, this), gui_context());
47 width_control->Changed.connect (connections, invalidator(*this), boost::bind (&DrawingArea::queue_draw, this), gui_context());
49 add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK|Gdk::POINTER_MOTION_MASK);
52 StereoPanner::~StereoPanner ()
57 StereoPanner::on_expose_event (GdkEventExpose* ev)
59 Glib::RefPtr<Gdk::Window> win (get_window());
60 Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
62 cairo_t* cr = gdk_cairo_create (win->gobj());
66 double pos = position_control->get_value (); /* 0..1 */
67 double swidth = width_control->get_value (); /* -1..+1 */
68 const int pos_box_size = 5;
71 height = get_height ();
73 /* compute where the central box is */
75 x1 = (int) floor (width * pos);
78 cairo_set_source_rgb (cr, 255, 0, 0);
79 cairo_rectangle (cr, x1, 4, pos_box_size, pos_box_size);
82 /* compute & draw the line through the box */
84 x2 = x1 - (int) floor ((fabs (swidth) * width)/2.0); // center, then back up half the swidth value
86 cairo_set_source_rgb (cr, 0, 255, 0);
87 cairo_move_to (cr, x2, 4+(pos_box_size/2));
88 cairo_line_to (cr, x2 + floor ((fabs (swidth * width))), 4+(pos_box_size/2));
96 StereoPanner::on_button_press_event (GdkEventButton* ev)
101 /* center 8 pixels are for position drag */
104 double pos = position_control->get_value ();
106 if ((ev->x >= (int) floor ((pos * w)-4)) && (ev->x <= (int) floor ((pos * w)+4))) {
107 dragging_position = true;
109 dragging_position = false;
112 if (ev->type == GDK_2BUTTON_PRESS) {
113 if (dragging_position) {
114 cerr << "Reset pos\n";
115 position_control->set_value (0.5); // reset position to center
117 cerr << "Reset width\n";
118 width_control->set_value (1.0); // reset position to full, LR
129 StereoPanner::on_button_release_event (GdkEventButton* ev)
132 dragging_position = false;
137 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
144 float delta = (abs (ev->x - last_drag_x)) / (double) (w/2);
146 if (!dragging_position) {
147 double wv = width_control->get_value();
149 if (((drag_start_x < w/2) && ev->x > last_drag_x) || // start left of center, move towards it
150 ((drag_start_x > w/2) && ev->x < last_drag_x)) { // start right of center, move towards it
151 wv = wv * (1.0 - delta);
153 /* moving out, so increase the width */
154 wv = wv * (1.0 + delta);
157 width_control->set_value (wv);
161 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
163 if (ev->x > last_drag_x) { // increasing
164 pv = pv * (1.0 + delta);
166 pv = pv * (1.0 - delta);
169 position_control->set_value (pv);