Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour.git] / gtk2_ardour / panner2d.cc
index 0c04337b871f1da827dd33df6cfecf86af321d51..d61471d19e1ccadbc7e4ae8184e50969ecc8ec21 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <cmath>
 #include <climits>
 #include <string.h>
 
-#include <pbd/error.h>
-#include <ardour/panner.h>
-#include <gtkmm2ext/gtk_ui.h>
+#include <cairo.h>
+#include <gtkmm/menu.h>
+
+#include "gtkmm2ext/gtk_ui.h"
+
+#include "pbd/error.h"
+#include "pbd/cartesian.h"
+#include "ardour/panner.h"
+#include "ardour/panner_shell.h"
+#include "ardour/pannable.h"
+#include "ardour/speakers.h"
 
 #include "panner2d.h"
 #include "keyboard.h"
 #include "gui_thread.h"
+#include "utils.h"
+#include "public_editor.h"
 
 #include "i18n.h"
 
 using namespace std;
 using namespace Gtk;
-using namespace sigc;
 using namespace ARDOUR;
+using namespace PBD;
+using Gtkmm2ext::Keyboard;
 
-Panner2d::Target::Target (float xa, float ya, const char *txt)
-       : x (xa), y (ya), text (txt ? strdup (txt) : 0)
+static const int large_size_threshold = 100;
+static const int large_border_width = 25;
+static const int small_border_width = 8;
+
+Panner2d::Target::Target (const AngularVector& a, const char *txt)
+       : position (a)
+       , text (txt)
+       , _selected (false)
 {
-       if (text) {
-               textlen = strlen (txt);
-       } else {
-               textlen = 0;
-       }
 }
 
 Panner2d::Target::~Target ()
-{ 
-       if (text) {
-               free (text);
-       }
+{
 }
 
-Panner2d::Panner2d (Panner& p, int32_t w, int32_t h)
-       : panner (p), width (w), height (h)
+void
+Panner2d::Target::set_text (const char* txt)
+{
+       text = txt;
+}
+
+Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
+       : panner_shell (p)
+        , position (AngularVector (0.0, 0.0), "")
+        , width (0)
+        , height (h)
+        , last_width (0)
 {
-       context_menu = 0;
-       bypass_menu_item = 0;
+       panner_shell->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
 
-       allow_x = false;
-       allow_y = false;
-       allow_target = false;
+        panner_shell->pannable()->pan_azimuth_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
+        panner_shell->pannable()->pan_width_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
 
-       panner.StateChanged.connect (mem_fun(*this, &Panner2d::handle_state_change));
-       
        drag_target = 0;
        set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
 
+        handle_position_change ();
 }
 
 Panner2d::~Panner2d()
 {
-       for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
-               delete i->second;
+       for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
+               delete *i;
        }
 }
 
 void
 Panner2d::reset (uint32_t n_inputs)
 {
-       /* add pucks */
-       
-       drop_pucks ();
-       
-       switch (n_inputs) {
-       case 0:
-               break;
-               
-       case 1:
-               add_puck ("", 0.0f, 0.5f);
-               break;
-               
-       case 2:
-               add_puck ("L", 0.5f, 0.25f);
-               add_puck ("R", 0.25f, 0.5f);
-               show_puck (0);
-               show_puck (1);
-               break;
-               
-       default:
-               for (uint32_t i = 0; i < n_inputs; ++i) {
-                       char buf[64];
-                       snprintf (buf, sizeof (buf), "%" PRIu32, i);
-                       add_puck (buf, 0.0f, 0.5f);
-                       show_puck (i);
-               }
-               break;
-       }
-       
-       /* add all outputs */
-       
-       drop_targets ();
-       
-       for (uint32_t n = 0; n < panner.nouts(); ++n) {
-               add_target (panner.output (n).x, panner.output (n).y);
-       }
-       
-       allow_x_motion (true);
-       allow_y_motion (true);
-       allow_target_motion (true);
-}
+        uint32_t nouts = panner_shell->panner()->out().n_audio();
 
-void
-Panner2d::size_allocate_impl (GtkAllocation *alloc)
-{
-       width = alloc->width;
-       height = alloc->height;
+       /* signals */
 
-       DrawingArea::size_allocate_impl (alloc);
-}
+       while (signals.size() < n_inputs) {
+               add_signal ("", AngularVector());
+       }
 
-int
-Panner2d::add_puck (const char* text, float x, float y)
-{
-       Target* puck = new Target (x, y, text);
+       if (signals.size() > n_inputs) {
+               for (uint32_t i = signals.size(); i < n_inputs; ++i) {
+                       delete signals[i];
+               }
 
-       pair<int,Target *> newpair;
-       newpair.first = pucks.size();
-       newpair.second = puck;
+               signals.resize (n_inputs);
+       }
 
-       pucks.insert (newpair);
-       puck->visible = true;
-       
-       return 0;
-}
+        label_signals ();
 
-int
-Panner2d::add_target (float x, float y)
-{
-       Target *target = new Target (x, y, "");
+        for (uint32_t i = 0; i < n_inputs; ++i) {
+                signals[i]->position = panner_shell->panner()->signal_position (i);
+        }
 
-       pair<int,Target *> newpair;
-       newpair.first = targets.size();
-       newpair.second = target;
+       /* add all outputs */
 
-       targets.insert (newpair);
-       target->visible = true;
-       queue_draw ();
+       while (speakers.size() < nouts) {
+               add_speaker (AngularVector());
+       }
 
-       return newpair.first;
-}
+       if (speakers.size() > nouts) {
+               for (uint32_t i = nouts; i < speakers.size(); ++i) {
+                       delete speakers[i];
+               }
 
-void
-Panner2d::drop_targets ()
-{
-       for (Targets::iterator i = targets.begin(); i != targets.end(); ) {
+               speakers.resize (nouts);
+       }
 
-               Targets::iterator tmp;
+       for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
+               (*x)->visible = false;
+       }
 
-               tmp = i;
-               ++tmp;
+        vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
 
-               delete i->second;
-               targets.erase (i);
+       for (uint32_t n = 0; n < nouts; ++n) {
+               char buf[16];
 
-               i = tmp;
+               snprintf (buf, sizeof (buf), "%d", n+1);
+               speakers[n]->set_text (buf);
+               speakers[n]->position = the_speakers[n].angles();
+               speakers[n]->visible = true;
        }
 
        queue_draw ();
 }
 
 void
-Panner2d::drop_pucks ()
+Panner2d::on_size_allocate (Gtk::Allocation& alloc)
 {
-       for (Targets::iterator i = pucks.begin(); i != pucks.end(); ) {
+       width = alloc.get_width();
+       height = alloc.get_height();
 
-               Targets::iterator tmp;
+        if (height > large_size_threshold) {
+                border = large_border_width;
+        } else {
+                border = small_border_width;
+        }
 
-               tmp = i;
-               ++tmp;
+        radius = min (width, height);
+        radius -= border;
+        radius /= 2;
 
-               delete i->second;
-               pucks.erase (i);
+        hoffset = max ((double) (width - height), border);
+        voffset = max ((double) (height - width), border);
 
-               i = tmp;
-       }
+        hoffset /= 2.0;
+        voffset /= 2.0;
 
-       queue_draw ();
+       DrawingArea::on_size_allocate (alloc);
 }
 
-void
-Panner2d::remove_target (int which)
+int
+Panner2d::add_signal (const char* text, const AngularVector& a)
 {
-       Targets::iterator i = targets.find (which);
+       Target* signal = new Target (a, text);
+       signals.push_back (signal);
+       signal->visible = true;
 
-       if (i != targets.end()) {
-               delete i->second;
-               targets.erase (i);
-               queue_draw ();
-       }
-}              
+       return 0;
+}
+
+int
+Panner2d::add_speaker (const AngularVector& a)
+{
+       Target* speaker = new Target (a, "");
+       speakers.push_back (speaker);
+       speaker->visible = true;
+       queue_draw ();
+
+       return speakers.size() - 1;
+}
 
 void
 Panner2d::handle_state_change ()
 {
-       ENSURE_GUI_THREAD(mem_fun(*this, &Panner2d::handle_state_change));
-
        queue_draw ();
 }
 
 void
-Panner2d::move_target (int which, float x, float y)
+Panner2d::label_signals ()
 {
-       Targets::iterator i = targets.find (which);
-       Target *target;
-
-       if (!allow_target) {
-               return;
-       }
+        double w = panner_shell->pannable()->pan_width_control->get_value();
+        uint32_t sz = signals.size();
 
-       if (i != targets.end()) {
-               target = i->second;
-               target->x = x;
-               target->y = y;
-               
-               queue_draw ();
-       }
-}              
+       switch (sz) {
+       case 0:
+               break;
 
-void
-Panner2d::move_puck (int which, float x, float y)
-{
-       Targets::iterator i = pucks.find (which);
-       Target *target;
-
-       if (i != pucks.end()) {
-               target = i->second;
-               target->x = x;
-               target->y = y;
-               
-               queue_draw ();
-       }
-}              
+       case 1:
+               signals[0]->set_text ("");
+               break;
 
-void
-Panner2d::show_puck (int which)
-{
-       Targets::iterator i = pucks.find (which);
+       case 2:
+                if (w  >= 0.0) {
+                        signals[0]->set_text ("R");
+                        signals[1]->set_text ("L");
+                } else {
+                        signals[0]->set_text ("L");
+                        signals[1]->set_text ("R");
+                }
+               break;
 
-       if (i != pucks.end()) {
-               Target* puck = i->second;
-               if (!puck->visible) {
-                       puck->visible = true;
-                       queue_draw ();
+       default:
+               for (uint32_t i = 0; i < sz; ++i) {
+                       char buf[64];
+                        if (w >= 0.0) {
+                                snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
+                        } else {
+                                snprintf (buf, sizeof (buf), "%" PRIu32, sz - i);
+                        }
+                       signals[i]->set_text (buf);
                }
+               break;
        }
 }
 
 void
-Panner2d::hide_puck (int which)
+Panner2d::handle_position_change ()
 {
-       Targets::iterator i = pucks.find (which);
+       uint32_t n;
+        double w = panner_shell->pannable()->pan_width_control->get_value();
 
-       if (i != pucks.end()) {
-               Target* puck = i->second;
-               if (!puck->visible) {
-                       puck->visible = false;
-                       queue_draw ();
-               }
-       }
-}
+        position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0, 0.0);
 
-void
-Panner2d::show_target (int which)
-{
-       Targets::iterator i = targets.find (which);
-       if (i != targets.end()) {
-               if (!i->second->visible) {
-                       i->second->visible = true;
-                       queue_draw ();
-               }
+        for (uint32_t i = 0; i < signals.size(); ++i) {
+                signals[i]->position = panner_shell->panner()->signal_position (i);
+        }
+
+        if (w * last_width <= 0) {
+                /* changed sign */
+                label_signals ();
+        }
+
+        last_width = w;
+
+        vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
+
+       for (n = 0; n < speakers.size(); ++n) {
+               speakers[n]->position = the_speakers[n].angles();
        }
+
+       queue_draw ();
 }
 
 void
-Panner2d::hide_target (int which)
+Panner2d::move_signal (int which, const AngularVector& a)
 {
-       Targets::iterator i = targets.find (which);
-       if (i != targets.end()) {
-               if (i->second->visible) {
-                       i->second->visible = false;
-                       queue_draw ();
-               }
+       if (which >= int (speakers.size())) {
+               return;
        }
+
+       speakers[which]->position = a;
+       queue_draw ();
 }
 
 Panner2d::Target *
-Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck) const
+Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
 {
-       gdouble efx, efy;
        Target *closest = 0;
        Target *candidate;
        float distance;
        float best_distance = FLT_MAX;
-       int pwhich;
+        CartesianVector c;
 
-       efx = x/width;
-       efy = y/height;
-       which = 0;
-       pwhich = 0;
-       is_puck = false;
+        /* start with the position itself
+         */
 
-       for (Targets::const_iterator i = targets.begin(); i != targets.end(); ++i, ++which) {
-               candidate = i->second;
+        position.position.cartesian (c);
+        cart_to_gtk (c);
+        best_distance = sqrt ((c.x - x) * (c.x - x) +
+                         (c.y - y) * (c.y - y));
+        closest = &position;
 
-               distance = sqrt ((candidate->x - efx) * (candidate->x - efx) +
-                                (candidate->y - efy) * (candidate->y - efy));
+       for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
+               candidate = *i;
 
-               if (distance < best_distance) {
-                       closest = candidate;
-                       best_distance = distance;
-               }
-       }
+               candidate->position.cartesian (c);
+               cart_to_gtk (c);
 
-       for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
-               candidate = i->second;
+               distance = sqrt ((c.x - x) * (c.x - x) +
+                                (c.y - y) * (c.y - y));
 
-               distance = sqrt ((candidate->x - efx) * (candidate->x - efx) +
-                                (candidate->y - efy) * (candidate->y - efy));
-
-               if (distance < best_distance) {
+                if (distance < best_distance) {
                        closest = candidate;
                        best_distance = distance;
-                       is_puck = true;
-                       which = pwhich;
                }
        }
-       
+
+        is_signal = true;
+
+        if (height > large_size_threshold) {
+                /* "big" */
+                if (best_distance > 30) { // arbitrary
+                        closest = 0;
+                }
+        } else {
+                /* "small" */
+                if (best_distance > 10) { // arbitrary
+                        closest = 0;
+                }
+        }
+
+        /* if we didn't find a signal close by, check the speakers */
+
+        if (!closest) {
+                for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
+                        candidate = *i;
+
+                        candidate->position.cartesian (c);
+                        cart_to_gtk (c);
+
+                        distance = sqrt ((c.x - x) * (c.x - x) +
+                                         (c.y - y) * (c.y - y));
+
+                        if (distance < best_distance) {
+                                closest = candidate;
+                                best_distance = distance;
+                        }
+                }
+
+                if (height > large_size_threshold) {
+                        /* "big" */
+                        if (best_distance < 30) { // arbitrary
+                                is_signal = false;
+                        } else {
+                                closest = 0;
+                        }
+                } else {
+                        /* "small" */
+                        if (best_distance < 10) { // arbitrary
+                                is_signal = false;
+                        } else {
+                                closest = 0;
+                        }
+                }
+        }
+
        return closest;
-}              
+}
 
-gint
-Panner2d::motion_notify_event_impl (GdkEventMotion *ev)
+bool
+Panner2d::on_motion_notify_event (GdkEventMotion *ev)
 {
        gint x, y;
        GdkModifierType state;
@@ -365,272 +377,483 @@ Panner2d::motion_notify_event_impl (GdkEventMotion *ev)
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
        }
+
+        if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
+                did_move = true;
+        }
+
        return handle_motion (x, y, state);
 }
-gint
-Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
+
+bool
+Panner2d::on_expose_event (GdkEventExpose *event)
 {
-       if (drag_target == 0 || (state & GDK_BUTTON1_MASK) == 0) {
-               return FALSE;
-       }
+        CartesianVector c;
+       cairo_t* cr;
+        bool small = (height <= large_size_threshold);
+        const double diameter = radius*2.0;
 
-       int x, y;
-       bool need_move = false;
+       cr = gdk_cairo_create (get_window()->gobj());
 
-       if (!drag_is_puck && !allow_target) {
-               return TRUE;
-       }
+        /* background */
 
-       if (allow_x || !drag_is_puck) {
-               float new_x;
-               x = min (evx, width - 1);
-               x = max (x, 0);
-               new_x = (float) x / (width - 1);
-               if (new_x != drag_target->x) {
-                       drag_target->x = new_x;
-                       need_move = true;
-               }
+       cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
+       if (!panner_shell->bypassed()) {
+               cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
+       } else {
+               cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
        }
+       cairo_fill_preserve (cr);
+       cairo_clip (cr);
 
-       if (allow_y || drag_is_puck) {
-               float new_y;
-               y = min (evy, height - 1);
-               y = max (y, 0);
-               new_y = (float) y / (height - 1);
-               if (new_y != drag_target->y) {
-                       drag_target->y = new_y;
-                       need_move = true;
-               }
-       }
+        /* offset to give us some border */
 
-       if (need_move) {
-               queue_draw ();
+        cairo_translate (cr, hoffset, voffset);
 
-               if (drag_is_puck) {
-                       
-                       panner[drag_index]->set_position (drag_target->x, drag_target->y);
+       cairo_set_line_width (cr, 1.0);
 
-               } else {
+       /* horizontal line of "crosshairs" */
 
-                       TargetMoved (drag_index);
-               }
-       }
+        cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
+       cairo_move_to (cr, 0.0, radius);
+       cairo_line_to (cr, diameter, radius);
+       cairo_stroke (cr);
 
-       return TRUE;
-}
+       /* vertical line of "crosshairs" */
 
-gint
-Panner2d::expose_event_impl (GdkEventExpose *event)
-{
-       gint x, y;
-       float fx, fy;
-
-       /* redraw the background */
-
-       get_window().draw_rectangle (get_style()->get_bg_gc(get_state()),
-                                    true,
-                                    event->area.x, event->area.y,
-                                    event->area.width, event->area.height);
-       
-
-       if (!panner.bypassed()) {
-
-               for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
-
-                       Target* puck = i->second;
-
-                       if (puck->visible) {
-                               /* redraw puck */
-                               
-                               fx = min (puck->x, 1.0f);
-                               fx = max (fx, -1.0f);
-                               x = (gint) floor (width * fx - 4);
-                               
-                               fy = min (puck->y, 1.0f);
-                               fy = max (fy, -1.0f);
-                               y = (gint) floor (height * fy - 4);
-                               
-                               get_window().draw_arc (get_style()->get_fg_gc(Gtk::STATE_NORMAL),
-                                                      true,
-                                                      x, y,
-                                                      8, 8,
-                                                      0, 360 * 64);
-                               get_window().draw_text (get_style()->get_font(),
-                                                       get_style()->get_fg_gc(Gtk::STATE_NORMAL),
-                                                       x + 6, y + 6,
-                                                       puck->text,
-                                                       puck->textlen);
-                       }
+       cairo_move_to (cr, radius, 0);
+       cairo_line_to (cr, radius, diameter);
+       cairo_stroke (cr);
+
+       /* the circle on which signals live */
+
+       cairo_set_line_width (cr, 2.0);
+        cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
+       cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
+       cairo_stroke (cr);
+
+       /* 3 other circles of smaller diameter circle on which signals live */
+
+       cairo_set_line_width (cr, 1.0);
+        cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
+       cairo_arc (cr, radius, radius, radius * 0.75, 0, 2.0 * M_PI);
+       cairo_stroke (cr);
+        cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
+       cairo_arc (cr, radius, radius, radius * 0.50, 0, 2.0 * M_PI);
+       cairo_stroke (cr);
+       cairo_arc (cr, radius, radius, radius * 0.25, 0, 2.0 * M_PI);
+       cairo_stroke (cr);
+
+        if (signals.size() > 1) {
+                /* arc to show "diffusion" */
+
+                double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
+                double position_angle = (2 * M_PI) - panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
+
+                cairo_save (cr);
+                cairo_translate (cr, radius, radius);
+                cairo_rotate (cr, position_angle - (width_angle/2.0));
+                cairo_move_to (cr, 0, 0);
+                cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
+                cairo_close_path (cr);
+                if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
+                        /* normal width */
+                        cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
+                } else {
+                        /* inverse width */
+                        cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
+                }
+                cairo_fill (cr);
+                cairo_restore (cr);
+        }
+
+       if (!panner_shell->bypassed()) {
+
+               double arc_radius;
+
+               cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
+
+               if (small) {
+                       arc_radius = 4.0;
+               } else {
+                       cairo_set_font_size (cr, 10);
+                       arc_radius = 12.0;
                }
 
-               /* redraw any visible targets */
-
-               for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
-                       Target *target = i->second;
-
-                       if (target->visible) {
-                               
-                               /* why -8 ??? why is this necessary ? */
-                               
-                               fx = min (target->x, 1.0f);
-                               fx = max (fx, -1.0f);
-                               x = (gint) floor ((width - 8) * fx);
-                       
-                               fy = min (target->y, 1.0f);
-                               fy = max (fy, -1.0f);
-                               y = (gint) floor ((height - 8) * fy);
-
-                               get_window().draw_rectangle (get_style()->get_fg_gc(GTK_STATE_ACTIVE),
-                                                            true,
-                                                            x, y,
-                                                            4, 4);
+                /* signals */
+
+                if (signals.size() > 1) {
+                        for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
+                                Target* signal = *i;
+
+                                if (signal->visible) {
+
+                                        signal->position.cartesian (c);
+                                        cart_to_gtk (c);
+
+                                        cairo_new_path (cr);
+                                        cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
+                                        cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
+                                        cairo_fill_preserve (cr);
+                                        cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
+                                        cairo_stroke (cr);
+
+                                        if (!small && !signal->text.empty()) {
+                                                cairo_set_source_rgb (cr, 0.517, 0.772, 0.882);
+                                                /* the +/- adjustments are a hack to try to center the text in the circle */
+                                                if (small) {
+                                                        cairo_move_to (cr, c.x - 1, c.y + 1);
+                                                } else {
+                                                        cairo_move_to (cr, c.x - 4, c.y + 4);
+                                                }
+                                                cairo_show_text (cr, signal->text.c_str());
+                                        }
+                                }
+                        }
+                }
+
+                /* speakers */
+
+               int n = 0;
+
+               for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
+                       Target *speaker = *i;
+                       char buf[256];
+                       ++n;
+
+                       if (speaker->visible) {
+
+                               CartesianVector c;
+
+                               speaker->position.cartesian (c);
+                               cart_to_gtk (c);
+
+                               snprintf (buf, sizeof (buf), "%d", n);
+
+                                /* stroke out a speaker shape */
+
+                                cairo_move_to (cr, c.x, c.y);
+                                cairo_save (cr);
+                                cairo_rotate (cr, -(speaker->position.azi/360.0) * (2.0 * M_PI));
+                                if (small) {
+                                        cairo_scale (cr, 0.8, 0.8);
+                                } else {
+                                        cairo_scale (cr, 1.2, 1.2);
+                                }
+                                cairo_rel_line_to (cr, 4, -2);
+                                cairo_rel_line_to (cr, 0, -7);
+                                cairo_rel_line_to (cr, 5, +5);
+                                cairo_rel_line_to (cr, 5, 0);
+                                cairo_rel_line_to (cr, 0, 5);
+                                cairo_rel_line_to (cr, -5, 0);
+                                cairo_rel_line_to (cr, -5, +5);
+                                cairo_rel_line_to (cr, 0, -7);
+                                cairo_close_path (cr);
+                               cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
+                               cairo_fill (cr);
+                                cairo_restore (cr);
+
+                                if (!small) {
+                                        cairo_set_font_size (cr, 16);
+
+                                        /* move the text in just a bit */
+
+                                        AngularVector textpos (speaker->position.azi, speaker->position.ele, 0.85);
+                                        textpos.cartesian (c);
+                                        cart_to_gtk (c);
+                                        cairo_move_to (cr, c.x, c.y);
+                                        cairo_show_text (cr, buf);
+                                }
+
                        }
                }
+
+                /* draw position */
+
+                position.position.cartesian (c);
+                cart_to_gtk (c);
+
+                cairo_new_path (cr);
+                cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
+                cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
+                cairo_fill_preserve (cr);
+                cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
+                cairo_stroke (cr);
        }
 
-       return TRUE;
+       cairo_destroy (cr);
+
+       return true;
 }
 
-gint
-Panner2d::button_press_event_impl (GdkEventButton *ev)
+bool
+Panner2d::on_button_press_event (GdkEventButton *ev)
 {
+       GdkModifierType state;
+        int x;
+        int y;
+        bool is_signal;
+
+       if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
+               return false;
+       }
+
+        did_move = false;
+
        switch (ev->button) {
        case 1:
-               gint x, y;
-               GdkModifierType state;
+       case 2:
+                x = ev->x - border;
+                y = ev->y - border;
+
+               if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
+                        if (!is_signal) {
+                                panner_shell->panner()->set_position (drag_target->position.azi/360.0);
+                                drag_target = 0;
+                        } else {
+                                drag_target->set_selected (true);
+                        }
+               }
 
-               drag_target = find_closest_object (ev->x, ev->y, drag_index, drag_is_puck);
-               
-               x = (int) floor (ev->x);
-               y = (int) floor (ev->y);
+               drag_x = ev->x;
+               drag_y = ev->y;
                state = (GdkModifierType) ev->state;
 
-               return handle_motion (x, y, state);
+               return handle_motion (drag_x, drag_y, state);
                break;
+
        default:
                break;
        }
-       
-       return FALSE;
+
+       return false;
 }
 
-gint
-Panner2d::button_release_event_impl (GdkEventButton *ev)
+bool
+Panner2d::on_button_release_event (GdkEventButton *ev)
 {
+       gint x, y;
+       GdkModifierType state;
+       bool ret = false;
+
        switch (ev->button) {
        case 1:
-               gint x, y;
-               int ret;
-               GdkModifierType state;
-
                x = (int) floor (ev->x);
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
+                ret = handle_motion (x, y, state);
+               drag_target = 0;
+               break;
 
-               if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::Shift))) {
-                       
-                       for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
-                               Target* puck = i->second;
-                               puck->x = 0.5;
-                               puck->y = 0.5;
-                       }
-
-                       queue_draw ();
-                       PuckMoved (-1);
-                       ret = TRUE;
+       case 2:
+               x = (int) floor (ev->x);
+               y = (int) floor (ev->y);
+               state = (GdkModifierType) ev->state;
 
+               if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
+                       toggle_bypass ();
+                       ret = true;
                } else {
                        ret = handle_motion (x, y, state);
                }
-               
-               drag_target = 0;
 
-               return ret;
+               drag_target = 0;
                break;
-       case 2:
-               toggle_bypass ();
-               return TRUE;
 
        case 3:
-               show_context_menu ();
                break;
 
        }
 
-       return FALSE;
+       return ret;
 }
 
-void
-Panner2d::toggle_bypass ()
+gint
+Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
 {
-       if (bypass_menu_item && (panner.bypassed() != bypass_menu_item->get_active())) {
-               panner.set_bypassed (!panner.bypassed());
+       if (drag_target == 0) {
+               return false;
+       }
+
+       if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
+               return false;
        }
-}
 
-void
-Panner2d::show_context_menu ()
-{
-       using namespace Menu_Helpers;
 
-       if (context_menu == 0) {
-               context_menu = manage (new Menu);
-               context_menu->set_name ("ArdourContextMenu");
-               MenuList& items = context_menu->items();
+       if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
+               CartesianVector c;
+               bool need_move = false;
 
-               items.push_back (CheckMenuElem (_("Bypass")));
-               bypass_menu_item = static_cast<CheckMenuItem*> (items.back());
-               bypass_menu_item->toggled.connect (mem_fun(*this, &Panner2d::toggle_bypass));
+               drag_target->position.cartesian (c);
+               cart_to_gtk (c);
+
+               if ((evx != c.x) || (evy != c.y)) {
+                       need_move = true;
+               }
+
+               if (need_move) {
+                       CartesianVector cp (evx, evy, 0.0);
+                        AngularVector av;
+
+                       /* canonicalize position and then clamp to the circle */
+
+                       gtk_to_cart (cp);
+                       clamp_to_circle (cp.x, cp.y);
+
+                       /* generate an angular representation of the current mouse position */
+
+                       cp.angular (av);
+
+                        if (drag_target == &position) {
+                                double degree_fract = av.azi / 360.0;
+                                panner_shell->panner()->set_position (degree_fract);
+                        }
+               }
+       }
+
+       return true;
+}
 
-       } 
+bool
+Panner2d::on_scroll_event (GdkEventScroll* ev)
+{
+        switch (ev->direction) {
+        case GDK_SCROLL_UP:
+        case GDK_SCROLL_RIGHT:
+                panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
+                break;
+
+        case GDK_SCROLL_DOWN:
+        case GDK_SCROLL_LEFT:
+                panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
+                break;
+        }
+        return true;
+}
 
-       bypass_menu_item->set_active (panner.bypassed());
-       context_menu->popup (1, 0);
+void
+Panner2d::cart_to_gtk (CartesianVector& c) const
+{
+       /* cartesian coordinate space:
+             center = 0.0
+              dimension = 2.0 * 2.0
+              increasing y moves up
+              so max values along each axis are -1..+1
+
+          GTK uses a coordinate space that is:
+             top left = 0.0
+              dimension = (radius*2.0) * (radius*2.0)
+              increasing y moves down
+       */
+        const double diameter = radius*2.0;
+
+        c.x = diameter * ((c.x + 1.0) / 2.0);
+        /* extra subtraction inverts the y-axis to match "increasing y moves down" */
+        c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
 }
 
 void
-Panner2d::allow_x_motion (bool yn)
+Panner2d::gtk_to_cart (CartesianVector& c) const
 {
-       allow_x = yn;
+        const double diameter = radius*2.0;
+       c.x = ((c.x / diameter) * 2.0) - 1.0;
+        c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
 }
 
 void
-Panner2d::allow_target_motion (bool yn)
+Panner2d::clamp_to_circle (double& x, double& y)
 {
-       allow_target = yn;
+       double azi, ele;
+       double z = 0.0;
+        double l;
+
+       PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
+       PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
 }
 
 void
-Panner2d::allow_y_motion (bool yn)
+Panner2d::toggle_bypass ()
 {
-       allow_y = yn;
+       panner_shell->set_bypassed (!panner_shell->bypassed());
 }
 
-int
-Panner2d::puck_position (int which, float& x, float& y)
+Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
+       : ArdourDialog (_("Panner (2D)"))
+        , widget (p, h)
+       , bypass_button (_("Bypass"))
 {
-       Targets::iterator i;
+       widget.set_name ("MixerPanZone");
 
-       if ((i = pucks.find (which)) != pucks.end()) {
-               x = i->second->x;
-               y = i->second->y;
-               return 0;
-       }
+       set_title (_("Panner"));
+       widget.set_size_request (h, h);
+
+        bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
+
+       button_box.set_spacing (6);
+       button_box.pack_start (bypass_button, false, false);
+
+       spinner_box.set_spacing (6);
+       left_side.set_spacing (6);
+
+       left_side.pack_start (button_box, false, false);
+       left_side.pack_start (spinner_box, false, false);
+
+       bypass_button.show ();
+       button_box.show ();
+       spinner_box.show ();
+       left_side.show ();
+
+       hpacker.set_spacing (6);
+       hpacker.set_border_width (12);
+       hpacker.pack_start (widget, false, false);
+       hpacker.pack_start (left_side, false, false);
+       hpacker.show ();
 
-       return -1;
+       get_vbox()->pack_start (hpacker);
+       reset (inputs);
+       widget.show ();
 }
 
-int
-Panner2d::target_position (int which, float& x, float& y)
+void
+Panner2dWindow::reset (uint32_t n_inputs)
 {
-       Targets::iterator i;
+       widget.reset (n_inputs);
+
+#if 0
+       while (spinners.size() < n_inputs) {
+               // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
+               //spinner_box.pack_start (*spinners.back(), false, false);
+               //spinners.back()->set_digits (4);
+               spinners.back()->show ();
+       }
 
-       if ((i = targets.find (which)) != targets.end()) {
-               x = i->second->x;
-               y = i->second->y;
-               return 0;
+       while (spinners.size() > n_inputs) {
+               spinner_box.remove (*spinners.back());
+               delete spinners.back();
+               spinners.erase (--spinners.end());
        }
+#endif
+}
+
+void
+Panner2dWindow::bypass_toggled ()
+{
+        bool view = bypass_button.get_active ();
+        bool model = widget.get_panner_shell()->bypassed ();
 
-       return -1;
+        if (model != view) {
+                widget.get_panner_shell()->set_bypassed (view);
+        }
+}
+
+bool
+Panner2dWindow::on_key_press_event (GdkEventKey* event)
+{
+        return relay_key_press (event, &PublicEditor::instance());
+}
+
+bool
+Panner2dWindow::on_key_release_event (GdkEventKey *event)
+{
+        return true;
 }
-