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 ()
110 StereoPanner::on_expose_event (GdkEventExpose* ev)
112 Glib::RefPtr<Gdk::Window> win (get_window());
113 Glib::RefPtr<Gdk::GC> gc (get_style()->get_base_gc (get_state()));
115 cairo_t* cr = gdk_cairo_create (win->gobj());
118 double pos = position_control->get_value (); /* 0..1 */
119 double swidth = width_control->get_value (); /* -1..+1 */
120 double fswidth = fabs (swidth);
125 height = get_height ();
129 } else if (swidth < 0.0) {
135 o = colors[state].outline;
136 f = colors[state].fill;
137 t = colors[state].text;
138 b = colors[state].background;
142 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));
143 cairo_rectangle (cr, 0, 0, width, height);
146 /* compute the centers of the L/R boxes based on the current stereo width */
148 int usable_width = width - lr_box_size;
149 int center = lr_box_size/2 + (int) floor (usable_width * pos);
150 int left = center - (int) floor (fswidth * usable_width / 2.0); // center of leftmost box
151 int right = center + (int) floor (fswidth * usable_width / 2.0); // center of rightmost box
153 // cerr << "pos " << pos << " width = " << width << " swidth = " << swidth << " center @ " << center << " L = " << left << " R = " << right << endl;
155 /* compute & draw the line through the box */
157 cairo_set_line_width (cr, 2);
158 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));
159 cairo_move_to (cr, left, top_step+(pos_box_size/2)+step_down);
160 cairo_line_to (cr, left, top_step+(pos_box_size/2));
161 cairo_line_to (cr, right, top_step+(pos_box_size/2));
162 cairo_line_to (cr, right, top_step+(pos_box_size/2) + step_down);
166 /* flip where the L/R boxes are drawn */
172 left -= lr_box_size/2;
173 right -= lr_box_size/2;
177 (lr_box_size/2)+step_down,
178 lr_box_size, lr_box_size);
179 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));
180 cairo_stroke_preserve (cr);
181 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));
188 (lr_box_size/2) + step_down + 13);
189 cairo_select_font_face (cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
191 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));
192 cairo_show_text (cr, "L");
199 (lr_box_size/2)+step_down,
200 lr_box_size, lr_box_size);
201 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));
202 cairo_stroke_preserve (cr);
203 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));
210 (lr_box_size/2)+step_down + 13);
211 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));
213 cairo_show_text (cr, "M");
215 cairo_show_text (cr, "R");
218 /* draw the central box */
220 cairo_set_line_width (cr, 1);
221 cairo_rectangle (cr, center - (pos_box_size/2), top_step, pos_box_size, pos_box_size);
222 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));
223 cairo_stroke_preserve (cr);
224 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));
234 StereoPanner::on_button_press_event (GdkEventButton* ev)
236 drag_start_x = ev->x;
240 /* top section of widget is for position drags */
241 dragging_position = true;
243 dragging_position = false;
246 if (ev->type == GDK_2BUTTON_PRESS) {
247 if (dragging_position) {
248 cerr << "Reset pos\n";
249 position_control->set_value (0.5); // reset position to center
251 cerr << "Reset width\n";
252 width_control->set_value (1.0); // reset position to full, LR
263 StereoPanner::on_button_release_event (GdkEventButton* ev)
266 dragging_position = false;
271 StereoPanner::on_scroll_event (GdkEventScroll* ev)
273 double one_degree = 1.0/180.0;
274 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
275 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
278 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
281 step = one_degree * 5.0;
284 switch (ev->direction) {
285 case GDK_SCROLL_LEFT:
287 width_control->set_value (wv);
291 position_control->set_value (pv);
293 case GDK_SCROLL_RIGHT:
295 width_control->set_value (wv);
297 case GDK_SCROLL_DOWN:
299 position_control->set_value (pv);
307 StereoPanner::on_motion_notify_event (GdkEventMotion* ev)
314 double delta = (abs (ev->x - last_drag_x)) / (w/2.0);
317 if (!dragging_position) {
318 double wv = width_control->get_value();
321 double opx; // compute the operational x-coordinate given the current pos+width
324 /* positive value: increasing width means adding */
327 /* positive value: increasing width means subtracting */
331 if (drag_start_x < w/2) {
332 /* started left of center */
334 opx = position_control->get_value() - (wv/2.0);
338 if (ev->x > last_drag_x) {
346 if (ev->x > last_drag_x) {
354 /* started right of center */
356 opx = position_control->get_value() + (wv/2.0);
360 if (ev->x < last_drag_x) {
361 /* motion to right */
368 if (ev->x < last_drag_x) {
369 /* motion to right */
379 wv = wv + (drag_dir * delta);
381 width_control->set_value (wv);
385 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
387 if (ev->x > last_drag_x) { // increasing
393 position_control->set_value (pv);
401 StereoPanner::on_key_press_event (GdkEventKey* ev)
403 double one_degree = 1.0/180.0;
404 double pv = position_control->get_value(); // 0..1.0 ; 0 = left
405 double wv = width_control->get_value(); // 0..1.0 ; 0 = left
408 if (Keyboard::modifier_state_contains (ev->state, Keyboard::PrimaryModifier)) {
411 step = one_degree * 5.0;
414 /* up/down control width because we consider pan position more "important"
415 (and thus having higher "sense" priority) than width.
418 switch (ev->keyval) {
420 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
421 width_control->set_value (1.0);
423 width_control->set_value (wv + step);
427 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
428 width_control->set_value (-1.0);
430 width_control->set_value (wv - step);
435 position_control->set_value (pv);
439 position_control->set_value (pv);
445 width_control->set_value (0.0);
456 StereoPanner::on_key_release_event (GdkEventKey* ev)
462 StereoPanner::on_enter_notify_event (GdkEventCrossing* ev)
465 Keyboard::magic_widget_grab_focus ();
470 StereoPanner::on_leave_notify_event (GdkEventCrossing*)
472 Keyboard::magic_widget_drop_focus ();
477 StereoPanner::set_colors ()
479 colors[Normal].fill = ARDOUR_UI::config()->canvasvar_StereoPannerFill.get();
480 colors[Normal].outline = ARDOUR_UI::config()->canvasvar_StereoPannerOutline.get();
481 colors[Normal].text = ARDOUR_UI::config()->canvasvar_StereoPannerText.get();
482 colors[Normal].background = ARDOUR_UI::config()->canvasvar_StereoPannerBackground.get();
484 colors[Mono].fill = ARDOUR_UI::config()->canvasvar_StereoPannerMonoFill.get();
485 colors[Mono].outline = ARDOUR_UI::config()->canvasvar_StereoPannerMonoOutline.get();
486 colors[Mono].text = ARDOUR_UI::config()->canvasvar_StereoPannerMonoText.get();
487 colors[Mono].background = ARDOUR_UI::config()->canvasvar_StereoPannerMonoBackground.get();
489 colors[Inverted].fill = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedFill.get();
490 colors[Inverted].outline = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedOutline.get();
491 colors[Inverted].text = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedText.get();
492 colors[Inverted].background = ARDOUR_UI::config()->canvasvar_StereoPannerInvertedBackground.get();
494 color_change (); /* EMIT SIGNAL */