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.
25 #include "pbd/controllable.h"
26 #include "pbd/compose.h"
28 #include "gtkmm2ext/gui_thread.h"
29 #include "gtkmm2ext/gtk_ui.h"
30 #include "gtkmm2ext/keyboard.h"
32 #include "ardour/panner.h"
33 #include "stereo_panner.h"
40 using namespace Gtkmm2ext;
42 static const int pos_box_size = 10;
43 static const int lr_box_size = 15;
44 static const int step_down = 10;
45 static const int top_step = 2;
47 StereoPanner::StereoPanner (boost::shared_ptr<PBD::Controllable> position, boost::shared_ptr<PBD::Controllable> width)
48 : position_control (position)
49 , width_control (width)
51 , dragging_position (false)
55 position_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
56 width_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
59 set_flags (Gtk::CAN_FOCUS);
61 add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|
62 Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|
63 Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|
65 Gdk::POINTER_MOTION_MASK);
68 StereoPanner::~StereoPanner ()
73 StereoPanner::set_tooltip ()
75 double pos = position_control->get_value(); // 0..1
76 double w = width_control->get_value (); // -1..+1
77 int lpos = (int) lrint ((pos - (w/2.0)) * 100.0);
78 int rpos = (int) lrint ((pos + (w/2.0)) * 100.0);
80 Gtkmm2ext::UI::instance()->set_tip (this, string_compose (_("L:%1 R:%2 Width: %3%%"),
82 (int) floor (w * 100.0)).c_str());
86 StereoPanner::value_change ()
93 StereoPanner::on_expose_event (GdkEventExpose* ev)
95 Glib::RefPtr<Gdk::Window> win (get_window());
96 Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
98 cairo_t* cr = gdk_cairo_create (win->gobj());
101 double pos = position_control->get_value (); /* 0..1 */
102 double swidth = width_control->get_value (); /* -1..+1 */
103 double fswidth = fabs (swidth);
106 height = get_height ();
110 cairo_set_source_rgb (cr, 0.184, 0.172, 0.172);
111 cairo_rectangle (cr, 0, 0, width, height);
114 /* compute the outer edges of the L/R boxes based on the current stereo width */
116 int usable_width = width - lr_box_size;
117 int center = lr_box_size/2 + (int) floor (usable_width * pos);
118 int left = center - (int) floor (fswidth * usable_width / 2.0); // center of leftmost box
119 int right = center + (int) floor (fswidth * usable_width / 2.0); // center of rightmost box
121 // cerr << "pos " << pos << " width = " << width << " swidth = " << swidth << " center @ " << center << " L = " << left << " R = " << right << endl;
123 /* compute & draw the line through the box */
125 cairo_set_line_width (cr, 2);
126 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
127 cairo_move_to (cr, left, top_step+(pos_box_size/2)+step_down);
128 cairo_line_to (cr, left, top_step+(pos_box_size/2));
129 cairo_line_to (cr, right, top_step+(pos_box_size/2));
130 cairo_line_to (cr, right, top_step+(pos_box_size/2) + step_down);
134 /* flip where the L/R boxes are drawn */
140 left -= lr_box_size/2;
141 right -= lr_box_size/2;
145 (lr_box_size/2)+step_down,
146 lr_box_size, lr_box_size);
147 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
148 cairo_stroke_preserve (cr);
149 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
156 (lr_box_size/2) + step_down + 13);
157 cairo_set_source_rgba (cr, 0.129, 0.054, 0.588, 1.0);
158 cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
159 cairo_show_text (cr, "L");
165 (lr_box_size/2)+step_down,
166 lr_box_size, lr_box_size);
167 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
168 cairo_stroke_preserve (cr);
169 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
176 (lr_box_size/2)+step_down + 13);
177 cairo_set_source_rgba (cr, 0.129, 0.054, 0.588, 1.0);
178 cairo_show_text (cr, "R");
180 /* draw the central box */
182 cairo_set_line_width (cr, 1);
183 cairo_rectangle (cr, center - (pos_box_size/2), top_step, pos_box_size, pos_box_size);
184 cairo_set_source_rgba (cr, 0.3137, 0.4431, 0.7843, 1.0);
185 cairo_stroke_preserve (cr);
186 cairo_set_source_rgba (cr, 0.4509, 0.7686, 0.8627, 0.8);
196 StereoPanner::on_button_press_event (GdkEventButton* ev)
198 drag_start_x = ev->x;
202 /* top section of widget is for position drags */
203 dragging_position = true;
205 dragging_position = false;
208 if (ev->type == GDK_2BUTTON_PRESS) {
209 if (dragging_position) {
210 cerr << "Reset pos\n";
211 position_control->set_value (0.5); // reset position to center
213 cerr << "Reset width\n";
214 width_control->set_value (1.0); // reset position to full, LR
225 StereoPanner::on_button_release_event (GdkEventButton* ev)
228 dragging_position = false;
233 StereoPanner::on_scroll_event (GdkEventScroll* ev)
235 double one_degree = 1.0/180.0;
236 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
237 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
240 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
243 step = one_degree * 5.0;
246 switch (ev->direction) {
247 case GDK_SCROLL_LEFT:
249 width_control->set_value (wv);
253 position_control->set_value (pv);
255 case GDK_SCROLL_RIGHT:
257 width_control->set_value (wv);
259 case GDK_SCROLL_DOWN:
261 position_control->set_value (pv);
269 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
276 double delta = (abs (ev->x - last_drag_x)) / (w/2.0);
279 if (!dragging_position) {
280 double wv = width_control->get_value();
283 double opx; // compute the operational x-coordinate given the current pos+width
286 /* positive value: increasing width means adding */
289 /* positive value: increasing width means subtracting */
293 if (drag_start_x < w/2) {
294 /* started left of center */
296 opx = position_control->get_value() - (wv/2.0);
300 if (ev->x > last_drag_x) {
308 if (ev->x > last_drag_x) {
316 /* started right of center */
318 opx = position_control->get_value() + (wv/2.0);
322 if (ev->x < last_drag_x) {
323 /* motion to right */
330 if (ev->x < last_drag_x) {
331 /* motion to right */
341 wv = wv + (drag_dir * delta);
343 width_control->set_value (wv);
347 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
349 if (ev->x > last_drag_x) { // increasing
350 pv = pv * (1.0 + delta);
352 pv = pv * (1.0 - delta);
355 position_control->set_value (pv);
363 StereoPanner::on_key_press_event (GdkEventKey* ev)
365 double one_degree = 1.0/180.0;
366 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
367 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
370 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
373 step = one_degree * 5.0;
376 switch (ev->keyval) {
379 width_control->set_value (wv);
383 position_control->set_value (pv);
387 position_control->set_value (pv);
391 width_control->set_value (wv);
394 return forward_key_press (ev);
401 StereoPanner::on_key_release_event (GdkEventKey* ev)
407 StereoPanner::on_enter_notify_event (GdkEventCrossing* ev)
410 Keyboard::magic_widget_grab_focus ();
415 StereoPanner::on_leave_notify_event (GdkEventCrossing*)
417 Keyboard::magic_widget_drop_focus ();