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"
25 #include "pbd/compose.h"
27 #include "gtkmm2ext/gui_thread.h"
28 #include "gtkmm2ext/gtk_ui.h"
30 #include "ardour/panner.h"
31 #include "stereo_panner.h"
38 static const int pos_box_size = 10;
39 static const int lr_box_size = 18;
40 static const int step_down = 10;
42 StereoPanner::StereoPanner (boost::shared_ptr<PBD::Controllable> position, boost::shared_ptr<PBD::Controllable> width)
43 : position_control (position)
44 , width_control (width)
46 , dragging_position (false)
50 position_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
51 width_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
54 add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK|Gdk::POINTER_MOTION_MASK);
57 StereoPanner::~StereoPanner ()
62 StereoPanner::set_tooltip ()
64 Gtkmm2ext::UI::instance()->set_tip (this, string_compose (_("L:%1 R:%2 Width: %3%%"),
65 (int) floor (position_control->get_value() * 100.0),
66 (int) floor ((1.0 - position_control->get_value() * 100)),
67 (int) floor (width_control->get_value() * 100.0)).c_str());
71 StereoPanner::value_change ()
78 StereoPanner::on_expose_event (GdkEventExpose* ev)
80 Glib::RefPtr<Gdk::Window> win (get_window());
81 Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
83 cairo_t* cr = gdk_cairo_create (win->gobj());
86 double pos = position_control->get_value (); /* 0..1 */
87 double swidth = width_control->get_value (); /* -1..+1 */
88 double fswidth = fabs (swidth);
91 height = get_height ();
95 cairo_set_source_rgb (cr, 0.184, 0.172, 0.172);
96 cairo_rectangle (cr, 0, 0, width, height);
99 /* compute the outer edges of the L/R boxes based on the current stereo width */
101 int usable_width = width - lr_box_size;
102 int center = lr_box_size/2 + (int) floor (usable_width * pos);
103 int left = center - (int) floor (fswidth * usable_width / 2.0); // center of leftmost box
104 int right = center + (int) floor (fswidth * usable_width / 2.0); // center of rightmost box
106 // cerr << "pos " << pos << " width = " << width << " swidth = " << swidth << " center @ " << center << " L = " << left << " R = " << right << endl;
108 /* compute & draw the line through the box */
110 cairo_set_line_width (cr, 2);
111 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
112 cairo_move_to (cr, left, 4+(pos_box_size/2)+step_down);
113 cairo_line_to (cr, left, 4+(pos_box_size/2));
114 cairo_line_to (cr, right, 4+(pos_box_size/2));
115 cairo_line_to (cr, right, 4+(pos_box_size/2) + step_down);
119 /* flip where the L/R boxes are drawn */
125 left -= lr_box_size/2;
126 right -= lr_box_size/2;
130 (lr_box_size/2)+step_down,
131 lr_box_size, lr_box_size);
132 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
133 cairo_stroke_preserve (cr);
134 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
141 (lr_box_size/2) + step_down + 13);
142 cairo_set_source_rgba (cr, 0.129, 0.054, 0.588, 1.0);
143 cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
144 cairo_show_text (cr, "L");
150 (lr_box_size/2)+step_down,
151 lr_box_size, lr_box_size);
152 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
153 cairo_stroke_preserve (cr);
154 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
161 (lr_box_size/2)+step_down + 13);
162 cairo_set_source_rgba (cr, 0.129, 0.054, 0.588, 1.0);
163 cairo_show_text (cr, "R");
165 /* draw the central box */
167 cairo_set_line_width (cr, 1);
168 cairo_rectangle (cr, center - (pos_box_size/2), 4, pos_box_size, pos_box_size);
169 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
170 cairo_stroke_preserve (cr);
171 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
181 StereoPanner::on_button_press_event (GdkEventButton* ev)
183 drag_start_x = ev->x;
187 /* top section of widget is for position drags */
188 dragging_position = true;
190 dragging_position = false;
193 if (ev->type == GDK_2BUTTON_PRESS) {
194 if (dragging_position) {
195 cerr << "Reset pos\n";
196 position_control->set_value (0.5); // reset position to center
198 cerr << "Reset width\n";
199 width_control->set_value (1.0); // reset position to full, LR
210 StereoPanner::on_button_release_event (GdkEventButton* ev)
213 dragging_position = false;
218 StereoPanner::on_scroll_event (GdkEventScroll* ev)
224 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
231 double delta = (abs (ev->x - last_drag_x)) / (w/2.0);
234 if (!dragging_position) {
235 double wv = width_control->get_value();
238 double opx; // compute the operational x-coordinate given the current pos+width
241 /* positive value: increasing width means adding */
244 /* positive value: increasing width means subtracting */
248 if (drag_start_x < w/2) {
249 /* started left of center */
251 opx = position_control->get_value() - (wv/2.0);
255 if (ev->x > last_drag_x) {
263 if (ev->x > last_drag_x) {
271 /* started right of center */
273 opx = position_control->get_value() + (wv/2.0);
277 if (ev->x < last_drag_x) {
278 /* motion to right */
285 if (ev->x < last_drag_x) {
286 /* motion to right */
296 wv = wv + (drag_dir * delta);
298 width_control->set_value (wv);
302 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
304 if (ev->x > last_drag_x) { // increasing
305 pv = pv * (1.0 + delta);
307 pv = pv * (1.0 - delta);
310 position_control->set_value (pv);