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 <gtkmm/window.h>
27 #include "pbd/controllable.h"
28 #include "pbd/compose.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/gtk_ui.h"
32 #include "gtkmm2ext/keyboard.h"
34 #include "ardour/panner.h"
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38 #include "stereo_panner.h"
39 #include "rgb_macros.h"
46 using namespace Gtkmm2ext;
48 static const int pos_box_size = 10;
49 static const int lr_box_size = 15;
50 static const int step_down = 10;
51 static const int top_step = 2;
53 StereoPanner::ColorScheme StereoPanner::colors[3];
54 bool StereoPanner::have_colors = false;
55 PBD::Signal0<void> StereoPanner::color_change;
57 StereoPanner::StereoPanner (boost::shared_ptr<PBD::Controllable> position, boost::shared_ptr<PBD::Controllable> width)
58 : position_control (position)
59 , width_control (width)
61 , dragging_position (false)
70 position_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
71 width_control->Changed.connect (connections, invalidator(*this), boost::bind (&StereoPanner::value_change, this), gui_context());
74 set_flags (Gtk::CAN_FOCUS);
76 add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|
77 Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|
78 Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|
80 Gdk::POINTER_MOTION_MASK);
82 color_change.connect (connections, invalidator (*this), boost::bind (&DrawingArea::queue_draw, this), gui_context());
85 StereoPanner::~StereoPanner ()
90 StereoPanner::set_tooltip ()
92 double pos = position_control->get_value(); // 0..1
93 double w = width_control->get_value (); // -1..+1
94 int lpos = (int) lrint ((pos - (w/2.0)) * 100.0);
95 int rpos = (int) lrint ((pos + (w/2.0)) * 100.0);
97 Gtkmm2ext::UI::instance()->set_tip (this, string_compose (_("L:%1 R:%2 Width: %3%%"),
99 (int) floor (w * 100.0)).c_str());
103 StereoPanner::value_change ()
105 cerr << this << " Value change, pos = " << position_control->get_value() << " w = " << width_control->get_value() << endl;
111 StereoPanner::on_expose_event (GdkEventExpose* ev)
113 Glib::RefPtr<Gdk::Window> win (get_window());
114 Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
116 cairo_t* cr = gdk_cairo_create (win->gobj());
119 double pos = position_control->get_value (); /* 0..1 */
120 double swidth = width_control->get_value (); /* -1..+1 */
121 double fswidth = fabs (swidth);
126 height = get_height ();
130 } else if (swidth < 0.0) {
136 o = colors[state].outline;
137 f = colors[state].fill;
138 t = colors[state].text;
139 b = colors[state].background;
143 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(b), UINT_RGBA_G_FLT(b), UINT_RGBA_B_FLT(b), UINT_RGBA_A_FLT(b));
144 cairo_rectangle (cr, 0, 0, width, height);
147 /* compute the centers of the L/R boxes based on the current stereo width */
149 int usable_width = width - lr_box_size;
150 double center = (lr_box_size/2.0) + (usable_width * pos);
151 int left = lrint (center - (fswidth * usable_width / 2.0)); // center of leftmost box
152 int right = lrint (center + (fswidth * usable_width / 2.0)); // center of rightmost box
154 cerr << this << " pos " << pos << " width = " << width << " swidth = " << swidth << " center @ " << center << " L = " << left << " R = " << right << endl;
156 /* compute & draw the line through the box */
158 cairo_set_line_width (cr, 2);
159 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(o), UINT_RGBA_G_FLT(o), UINT_RGBA_B_FLT(o), UINT_RGBA_A_FLT(o));
160 cairo_move_to (cr, left, top_step+(pos_box_size/2)+step_down);
161 cairo_line_to (cr, left, top_step+(pos_box_size/2));
162 cairo_line_to (cr, right, top_step+(pos_box_size/2));
163 cairo_line_to (cr, right, top_step+(pos_box_size/2) + step_down);
167 /* flip where the L/R boxes are drawn */
173 left -= lr_box_size/2;
174 right -= lr_box_size/2;
178 (lr_box_size/2)+step_down,
179 lr_box_size, lr_box_size);
180 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(o), UINT_RGBA_G_FLT(o), UINT_RGBA_B_FLT(o), UINT_RGBA_A_FLT(o));
181 cairo_stroke_preserve (cr);
182 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(f), UINT_RGBA_G_FLT(f), UINT_RGBA_B_FLT(f), UINT_RGBA_A_FLT(f));
189 (lr_box_size/2) + step_down + 13);
190 cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
192 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(t), UINT_RGBA_G_FLT(t), UINT_RGBA_B_FLT(t), UINT_RGBA_A_FLT(t));
193 cairo_show_text (cr, _("L"));
200 (lr_box_size/2)+step_down,
201 lr_box_size, lr_box_size);
202 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(o), UINT_RGBA_G_FLT(o), UINT_RGBA_B_FLT(o), UINT_RGBA_A_FLT(o));
203 cairo_stroke_preserve (cr);
204 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(f), UINT_RGBA_G_FLT(f), UINT_RGBA_B_FLT(f), UINT_RGBA_A_FLT(f));
211 (lr_box_size/2)+step_down + 13);
212 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(t), UINT_RGBA_G_FLT(t), UINT_RGBA_B_FLT(t), UINT_RGBA_A_FLT(t));
214 cairo_show_text (cr, _("M"));
216 cairo_show_text (cr, _("R"));
219 /* draw the central box */
221 cairo_set_line_width (cr, 1);
222 cairo_rectangle (cr, lrint (center - (pos_box_size/2.0)), top_step, pos_box_size, pos_box_size);
223 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(o), UINT_RGBA_G_FLT(o), UINT_RGBA_B_FLT(o), UINT_RGBA_A_FLT(o));
224 cairo_stroke_preserve (cr);
225 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(f), UINT_RGBA_G_FLT(f), UINT_RGBA_B_FLT(f), UINT_RGBA_A_FLT(f));
235 StereoPanner::on_button_press_event (GdkEventButton* ev)
237 drag_start_x = ev->x;
241 /* top section of widget is for position drags */
242 dragging_position = true;
244 dragging_position = false;
247 if (ev->type == GDK_2BUTTON_PRESS) {
248 if (dragging_position) {
249 cerr << "Reset pos\n";
250 position_control->set_value (0.5); // reset position to center
252 cerr << "Reset width\n";
253 width_control->set_value (1.0); // reset position to full, LR
264 StereoPanner::on_button_release_event (GdkEventButton* ev)
267 dragging_position = false;
272 StereoPanner::on_scroll_event (GdkEventScroll* ev)
274 double one_degree = 1.0/180.0;
275 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
276 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
279 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
282 step = one_degree * 5.0;
285 switch (ev->direction) {
286 case GDK_SCROLL_LEFT:
288 width_control->set_value (wv);
292 position_control->set_value (pv);
294 case GDK_SCROLL_RIGHT:
296 width_control->set_value (wv);
298 case GDK_SCROLL_DOWN:
300 position_control->set_value (pv);
308 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
315 double delta = (abs (ev->x - last_drag_x)) / (w/2.0);
318 if (!dragging_position) {
319 double wv = width_control->get_value();
322 double opx; // compute the operational x-coordinate given the current pos+width
325 /* positive value: increasing width means adding */
328 /* positive value: increasing width means subtracting */
332 if (drag_start_x < w/2) {
333 /* started left of center */
335 opx = position_control->get_value() - (wv/2.0);
339 if (ev->x > last_drag_x) {
341 cerr << "was left, still left, move left\n";
344 cerr << "was left, still left, move right\n";
349 if (ev->x > last_drag_x) {
351 cerr << "was left, gone right, move left\n";
354 cerr << "was left, gone right, move right\n";
359 /* started right of center */
361 opx = position_control->get_value() + (wv/2.0);
365 if (ev->x < last_drag_x) {
366 /* motion to right */
367 cerr << "was right, still right, move right\n";
370 cerr << "was right, still right, move left\n";
375 if (ev->x < last_drag_x) {
376 /* motion to right */
377 cerr << "was right, gone left, move right\n";
380 cerr << "was right, gone left, move left\n";
388 wv = wv + (drag_dir * delta);
390 cerr << this << " set width to " << wv << endl;
391 width_control->set_value (wv);
395 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
397 if (ev->x > last_drag_x) { // increasing
403 cerr << this << " set position to " << pv << endl;
404 position_control->set_value (pv);
412 StereoPanner::on_key_press_event (GdkEventKey* ev)
414 double one_degree = 1.0/180.0;
415 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
416 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
419 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
422 step = one_degree * 5.0;
425 /* up/down control width because we consider pan position more "important"
426 (and thus having higher "sense" priority) than width.
429 switch (ev->keyval) {
431 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
432 width_control->set_value (1.0);
434 width_control->set_value (wv + step);
438 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
439 width_control->set_value (-1.0);
441 width_control->set_value (wv - step);
446 position_control->set_value (pv);
450 position_control->set_value (pv);
456 width_control->set_value (0.0);
467 StereoPanner::on_key_release_event (GdkEventKey* ev)
473 StereoPanner::on_enter_notify_event (GdkEventCrossing* ev)
476 Keyboard::magic_widget_grab_focus ();
481 StereoPanner::on_leave_notify_event (GdkEventCrossing*)
483 Keyboard::magic_widget_drop_focus ();
488 StereoPanner::set_colors ()
490 colors[Normal].fill = ARDOUR_UI::config()->canvasvar_StereoPannerFill.get();
491 colors[Normal].outline = ARDOUR_UI::config()->canvasvar_StereoPannerOutline.get();
492 colors[Normal].text = ARDOUR_UI::config()->canvasvar_StereoPannerText.get();
493 colors[Normal].background = ARDOUR_UI::config()->canvasvar_StereoPannerBackground.get();
495 colors[Mono].fill = ARDOUR_UI::config()->canvasvar_StereoPannerMonoFill.get();
496 colors[Mono].outline = ARDOUR_UI::config()->canvasvar_StereoPannerMonoOutline.get();
497 colors[Mono].text = ARDOUR_UI::config()->canvasvar_StereoPannerMonoText.get();
498 colors[Mono].background = ARDOUR_UI::config()->canvasvar_StereoPannerMonoBackground.get();
500 colors[Inverted].fill = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedFill.get();
501 colors[Inverted].outline = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedOutline.get();
502 colors[Inverted].text = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedText.get();
503 colors[Inverted].background = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedBackground.get();
505 color_change (); /* EMIT SIGNAL */