2 Copyright (C) 2002 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/menu.h>
27 #include "gtkmm2ext/gtk_ui.h"
29 #include "pbd/error.h"
30 #include "pbd/cartesian.h"
31 #include "ardour/panner.h"
32 #include "ardour/panner_shell.h"
33 #include "ardour/pannable.h"
34 #include "ardour/speakers.h"
38 #include "gui_thread.h"
40 #include "public_editor.h"
46 using namespace ARDOUR;
48 using Gtkmm2ext::Keyboard;
50 static const int large_size_threshold = 100;
51 static const int large_border_width = 25;
52 static const int small_border_width = 8;
54 Panner2d::Target::Target (const AngularVector& a, const char *txt)
61 Panner2d::Target::~Target ()
66 Panner2d::Target::set_text (const char* txt)
71 Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
73 , position (AngularVector (0.0, 0.0), "")
78 panner_shell->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
80 panner_shell->panner()->SignalPositionChanged.connect (panconnect, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
83 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
85 handle_position_change ();
90 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
96 Panner2d::reset (uint32_t n_inputs)
98 uint32_t nouts = panner_shell->panner()->out().n_audio();
102 while (signals.size() < n_inputs) {
103 add_signal ("", AngularVector());
106 if (signals.size() > n_inputs) {
107 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
111 signals.resize (n_inputs);
116 for (uint32_t i = 0; i < n_inputs; ++i) {
117 signals[i]->position = panner_shell->panner()->signal_position (i);
120 /* add all outputs */
122 while (speakers.size() < nouts) {
123 add_speaker (AngularVector());
126 if (speakers.size() > nouts) {
127 for (uint32_t i = nouts; i < speakers.size(); ++i) {
131 speakers.resize (nouts);
134 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
135 (*x)->visible = false;
138 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
140 for (uint32_t n = 0; n < nouts; ++n) {
143 snprintf (buf, sizeof (buf), "%d", n+1);
144 speakers[n]->set_text (buf);
145 speakers[n]->position = the_speakers[n].angles();
146 speakers[n]->visible = true;
153 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
155 width = alloc.get_width();
156 height = alloc.get_height();
158 if (height > large_size_threshold) {
159 border = large_border_width;
161 border = small_border_width;
164 radius = min (width, height);
168 hoffset = max ((double) (width - height), border);
169 voffset = max ((double) (height - width), border);
174 DrawingArea::on_size_allocate (alloc);
178 Panner2d::add_signal (const char* text, const AngularVector& a)
180 Target* signal = new Target (a, text);
181 signals.push_back (signal);
182 signal->visible = true;
188 Panner2d::add_speaker (const AngularVector& a)
190 Target* speaker = new Target (a, "");
191 speakers.push_back (speaker);
192 speaker->visible = true;
195 return speakers.size() - 1;
199 Panner2d::handle_state_change ()
201 panconnect.drop_connections();
202 panner_shell->panner()->SignalPositionChanged.connect (panconnect, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
207 Panner2d::label_signals ()
209 double w = panner_shell->pannable()->pan_width_control->get_value();
210 uint32_t sz = signals.size();
217 signals[0]->set_text ("");
222 signals[0]->set_text ("R");
223 signals[1]->set_text ("L");
225 signals[0]->set_text ("L");
226 signals[1]->set_text ("R");
231 for (uint32_t i = 0; i < sz; ++i) {
234 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
236 snprintf (buf, sizeof (buf), "%" PRIu32, sz - i);
238 signals[i]->set_text (buf);
245 Panner2d::handle_position_change ()
248 double w = panner_shell->pannable()->pan_width_control->get_value();
250 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0, 0.0);
252 for (uint32_t i = 0; i < signals.size(); ++i) {
253 signals[i]->position = panner_shell->panner()->signal_position (i);
256 if (w * last_width <= 0) {
263 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
265 for (n = 0; n < speakers.size(); ++n) {
266 speakers[n]->position = the_speakers[n].angles();
273 Panner2d::move_signal (int which, const AngularVector& a)
275 if (which >= int (speakers.size())) {
279 speakers[which]->position = a;
284 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
289 float best_distance = FLT_MAX;
292 /* start with the position itself
295 position.position.cartesian (c);
297 best_distance = sqrt ((c.x - x) * (c.x - x) +
298 (c.y - y) * (c.y - y));
301 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
304 candidate->position.cartesian (c);
307 distance = sqrt ((c.x - x) * (c.x - x) +
308 (c.y - y) * (c.y - y));
310 if (distance < best_distance) {
312 best_distance = distance;
318 if (height > large_size_threshold) {
320 if (best_distance > 30) { // arbitrary
325 if (best_distance > 10) { // arbitrary
330 /* if we didn't find a signal close by, check the speakers */
333 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
336 candidate->position.cartesian (c);
339 distance = sqrt ((c.x - x) * (c.x - x) +
340 (c.y - y) * (c.y - y));
342 if (distance < best_distance) {
344 best_distance = distance;
348 if (height > large_size_threshold) {
350 if (best_distance < 30) { // arbitrary
357 if (best_distance < 10) { // arbitrary
369 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
372 GdkModifierType state;
375 gdk_window_get_pointer (ev->window, &x, &y, &state);
377 x = (int) floor (ev->x);
378 y = (int) floor (ev->y);
379 state = (GdkModifierType) ev->state;
382 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
386 return handle_motion (x, y, state);
390 Panner2d::on_expose_event (GdkEventExpose *event)
394 bool small_size = (height <= large_size_threshold);
395 const double diameter = radius*2.0;
397 cr = gdk_cairo_create (get_window()->gobj());
401 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
402 if (!panner_shell->bypassed()) {
403 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
405 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
407 cairo_fill_preserve (cr);
410 /* offset to give us some border */
412 cairo_translate (cr, hoffset, voffset);
414 cairo_set_line_width (cr, 1.0);
416 /* horizontal line of "crosshairs" */
418 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
419 cairo_move_to (cr, 0.0, radius);
420 cairo_line_to (cr, diameter, radius);
423 /* vertical line of "crosshairs" */
425 cairo_move_to (cr, radius, 0);
426 cairo_line_to (cr, radius, diameter);
429 /* the circle on which signals live */
431 cairo_set_line_width (cr, 2.0);
432 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
433 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
436 /* 3 other circles of smaller diameter circle on which signals live */
438 cairo_set_line_width (cr, 1.0);
439 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
440 cairo_arc (cr, radius, radius, radius * 0.75, 0, 2.0 * M_PI);
442 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
443 cairo_arc (cr, radius, radius, radius * 0.50, 0, 2.0 * M_PI);
445 cairo_arc (cr, radius, radius, radius * 0.25, 0, 2.0 * M_PI);
448 if (signals.size() > 1) {
449 /* arc to show "diffusion" */
451 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
452 double position_angle = (2 * M_PI) - panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
455 cairo_translate (cr, radius, radius);
456 cairo_rotate (cr, position_angle - (width_angle/2.0));
457 cairo_move_to (cr, 0, 0);
458 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
459 cairo_close_path (cr);
460 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
462 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
465 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
471 if (!panner_shell->bypassed()) {
475 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
480 cairo_set_font_size (cr, 10);
486 if (signals.size() > 1) {
487 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
490 if (signal->visible) {
492 signal->position.cartesian (c);
496 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
497 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
498 cairo_fill_preserve (cr);
499 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
502 if (!small_size && !signal->text.empty()) {
503 cairo_set_source_rgb (cr, 0.517, 0.772, 0.882);
504 /* the +/- adjustments are a hack to try to center the text in the circle */
506 cairo_move_to (cr, c.x - 1, c.y + 1);
508 cairo_move_to (cr, c.x - 4, c.y + 4);
510 cairo_show_text (cr, signal->text.c_str());
520 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
521 Target *speaker = *i;
525 if (speaker->visible) {
529 speaker->position.cartesian (c);
532 snprintf (buf, sizeof (buf), "%d", n);
534 /* stroke out a speaker shape */
536 cairo_move_to (cr, c.x, c.y);
538 cairo_rotate (cr, -(speaker->position.azi/360.0) * (2.0 * M_PI));
540 cairo_scale (cr, 0.8, 0.8);
542 cairo_scale (cr, 1.2, 1.2);
544 cairo_rel_line_to (cr, 4, -2);
545 cairo_rel_line_to (cr, 0, -7);
546 cairo_rel_line_to (cr, 5, +5);
547 cairo_rel_line_to (cr, 5, 0);
548 cairo_rel_line_to (cr, 0, 5);
549 cairo_rel_line_to (cr, -5, 0);
550 cairo_rel_line_to (cr, -5, +5);
551 cairo_rel_line_to (cr, 0, -7);
552 cairo_close_path (cr);
553 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
558 cairo_set_font_size (cr, 16);
560 /* move the text in just a bit */
562 AngularVector textpos (speaker->position.azi, speaker->position.ele, 0.85);
563 textpos.cartesian (c);
565 cairo_move_to (cr, c.x, c.y);
566 cairo_show_text (cr, buf);
574 position.position.cartesian (c);
578 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
579 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
580 cairo_fill_preserve (cr);
581 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
591 Panner2d::on_button_press_event (GdkEventButton *ev)
593 GdkModifierType state;
598 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
604 switch (ev->button) {
610 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
612 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
615 drag_target->set_selected (true);
621 state = (GdkModifierType) ev->state;
623 return handle_motion (drag_x, drag_y, state);
634 Panner2d::on_button_release_event (GdkEventButton *ev)
637 GdkModifierType state;
640 switch (ev->button) {
642 x = (int) floor (ev->x);
643 y = (int) floor (ev->y);
644 state = (GdkModifierType) ev->state;
645 ret = handle_motion (x, y, state);
650 x = (int) floor (ev->x);
651 y = (int) floor (ev->y);
652 state = (GdkModifierType) ev->state;
654 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
658 ret = handle_motion (x, y, state);
673 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
675 if (drag_target == 0) {
679 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
684 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
686 bool need_move = false;
688 drag_target->position.cartesian (c);
691 if ((evx != c.x) || (evy != c.y)) {
696 CartesianVector cp (evx, evy, 0.0);
699 /* canonicalize position and then clamp to the circle */
702 clamp_to_circle (cp.x, cp.y);
704 /* generate an angular representation of the current mouse position */
708 if (drag_target == &position) {
709 double degree_fract = av.azi / 360.0;
710 panner_shell->panner()->set_position (degree_fract);
719 Panner2d::on_scroll_event (GdkEventScroll* ev)
721 switch (ev->direction) {
723 case GDK_SCROLL_RIGHT:
724 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
727 case GDK_SCROLL_DOWN:
728 case GDK_SCROLL_LEFT:
729 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
736 Panner2d::cart_to_gtk (CartesianVector& c) const
738 /* cartesian coordinate space:
740 dimension = 2.0 * 2.0
741 increasing y moves up
742 so max values along each axis are -1..+1
744 GTK uses a coordinate space that is:
746 dimension = (radius*2.0) * (radius*2.0)
747 increasing y moves down
749 const double diameter = radius*2.0;
751 c.x = diameter * ((c.x + 1.0) / 2.0);
752 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
753 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
757 Panner2d::gtk_to_cart (CartesianVector& c) const
759 const double diameter = radius*2.0;
760 c.x = ((c.x / diameter) * 2.0) - 1.0;
761 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
765 Panner2d::clamp_to_circle (double& x, double& y)
771 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
772 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
776 Panner2d::toggle_bypass ()
778 panner_shell->set_bypassed (!panner_shell->bypassed());
781 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
782 : ArdourWindow (_("Panner (2D)"))
784 , bypass_button (_("Bypass"))
786 widget.set_name ("MixerPanZone");
788 set_title (_("Panner"));
789 widget.set_size_request (h, h);
791 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
793 button_box.set_spacing (6);
794 button_box.pack_start (bypass_button, false, false);
796 spinner_box.set_spacing (6);
797 left_side.set_spacing (6);
799 left_side.pack_start (button_box, false, false);
800 left_side.pack_start (spinner_box, false, false);
802 bypass_button.show ();
807 hpacker.set_spacing (6);
808 hpacker.set_border_width (12);
809 hpacker.pack_start (widget, false, false);
810 hpacker.pack_start (left_side, false, false);
819 Panner2dWindow::reset (uint32_t n_inputs)
821 widget.reset (n_inputs);
824 while (spinners.size() < n_inputs) {
825 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
826 //spinner_box.pack_start (*spinners.back(), false, false);
827 //spinners.back()->set_digits (4);
828 spinners.back()->show ();
831 while (spinners.size() > n_inputs) {
832 spinner_box.remove (*spinners.back());
833 delete spinners.back();
834 spinners.erase (--spinners.end());
840 Panner2dWindow::bypass_toggled ()
842 bool view = bypass_button.get_active ();
843 bool model = widget.get_panner_shell()->bypassed ();
846 widget.get_panner_shell()->set_bypassed (view);
851 Panner2dWindow::on_key_press_event (GdkEventKey* event)
853 return relay_key_press (event, &PublicEditor::instance());
857 Panner2dWindow::on_key_release_event (GdkEventKey*)