first pass at full output & gain coefficient interpolation for VBAP panning
[ardour.git] / gtk2_ardour / panner2d.cc
index 0216e76c2771269062fdf369c1873a5b609c2fd7..857c067ef5c0490fcf1a62a712b5e1321816ee0a 100644 (file)
 #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 <gtkmm2ext/gtk_ui.h>
+#include "ardour/pannable.h"
+#include "ardour/speakers.h"
 
 #include "panner2d.h"
 #include "keyboard.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, 0.0, 1.0, 0.01, 0.1)
-       , y (ya, 0.0, 1.0, 0.01, 0.1)
-       , azimuth (M_PI/2.0, 0.0, 2.0 * M_PI, 0.1, 0.5)
-       , text (txt ? strdup (txt) : 0)
+Panner2d::Target::Target (const AngularVector& a, const char *txt)
+       : position (a)
+       , text (txt)
+       , _selected (false)
 {
-       azimuth.set_value ((random() / (double) INT_MAX) * (2.0 * M_PI));
 }
 
 Panner2d::Target::~Target ()
-{ 
-       if (text) {
-               free (text);
-       }
+{
 }
 
 void
 Panner2d::Target::set_text (const char* txt)
 {
-       if (text) {
-               free (text);
-       }
-       text = strdup (txt);
+       text = txt;
 }
 
-Panner2d::Panner2d (Panner& p, int32_t h)
+Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
        : panner (p), width (0), height (h)
 {
-       allow_x = false;
-       allow_y = false;
-       allow_target = false;
+       panner->StateChanged.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
+       panner->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
+
+        panner->pannable()->pan_azimuth_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
+        panner->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));
-       panner.Changed.connect (mem_fun(*this, &Panner2d::handle_position_change));
-       
        drag_target = 0;
        set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
 }
@@ -82,188 +77,130 @@ Panner2d::Panner2d (Panner& p, int32_t h)
 Panner2d::~Panner2d()
 {
        for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
-               delete i->second;
+               delete *i;
        }
 }
 
 void
 Panner2d::reset (uint32_t n_inputs)
 {
-       Targets::size_type existing_pucks = pucks.size();
+        uint32_t nouts = panner->out().n_audio();
 
        /* pucks */
 
        while (pucks.size() < n_inputs) {
-               add_puck ("", 0.0, 0.0);
-       }
-       
-       while (pucks.size() > n_inputs) {
-               pucks.erase (pucks.begin());
+               add_puck ("", AngularVector());
        }
 
-       for (Targets::iterator x = pucks.begin(); x != pucks.end(); ++x) {
-               (*x).second->visible = false;
-       }
+       if (pucks.size() > n_inputs) {
+               for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
+                       delete pucks[i];
+               }
 
+               pucks.resize (n_inputs);
+       }
+                                               
        switch (n_inputs) {
        case 0:
                break;
-               
+
        case 1:
                pucks[0]->set_text ("");
-               pucks[0]->x.set_value (0.0);
-               pucks[0]->y.set_value (0.5);
-               pucks[0]->visible = true;
                break;
-               
+
        case 2:
                pucks[0]->set_text ("R");
-               pucks[0]->visible = true;
                pucks[1]->set_text ("L");
-               if (existing_pucks < 2) {
-                       pucks[1]->x.set_value (0.25f);
-                       pucks[1]->y.set_value (0.5f);
-               }
-               pucks[1]->visible = true;
                break;
-               
+
        default:
                for (uint32_t i = 0; i < n_inputs; ++i) {
                        char buf[64];
-                       snprintf (buf, sizeof (buf), "%" PRIu32, i);
+                       snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
                        pucks[i]->set_text (buf);
-                       
-                       if (existing_pucks < i) {
-                               float x, y;
-                               panner.streampanner (i).get_position (x, y);
-                               pucks[i]->x.set_value (x);
-                               pucks[i]->y.set_value (y);
-                       }
-
-                       pucks[i]->visible = true;
                }
                break;
        }
-       
+
+        for (uint32_t i = 0; i < n_inputs; ++i) {
+                pucks[i]->position = panner->signal_position (i);
+        }
+
        /* add all outputs */
-       
-       while (targets.size() < panner.nouts()) {
-               add_target (0.0, 0.0);
+
+       while (targets.size() < nouts) {
+               add_target (AngularVector());
        }
-       
-       while (targets.size() > panner.nouts()) {
-               targets.erase (targets.begin());
+
+       if (targets.size() > nouts) {
+               for (uint32_t i = nouts; i < targets.size(); ++i) {
+                       delete targets[i];
+               }
+
+               targets.resize (nouts);
        }
 
        for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
-               (*x).second->visible = false;
+               (*x)->visible = false;
        }
 
-       for (uint32_t n = 0; n < panner.nouts(); ++n) {
+        vector<Speaker>& speakers (panner->get_speakers()->speakers());
+
+       for (uint32_t n = 0; n < nouts; ++n) {
                char buf[16];
 
                snprintf (buf, sizeof (buf), "%d", n+1);
                targets[n]->set_text (buf);
-               targets[n]->x.set_value (panner.output(n).x);
-               targets[n]->y.set_value (panner.output(n).y);
+               targets[n]->position = speakers[n].angles();
                targets[n]->visible = true;
        }
-       
-       allow_x_motion (true);
-       allow_y_motion (true);
-       allow_target_motion (true);
 
        queue_draw ();
 }
 
-Gtk::Adjustment&
-Panner2d::azimuth (uint32_t which)
-{
-       assert (which < pucks.size());
-       return pucks[which]->azimuth;
-}
-
 void
 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
 {
        width = alloc.get_width();
        height = alloc.get_height();
 
+        /* our idea of our width/height must be "square
+         */
+
        if (height > 100) {
                width -= 20;
                height -= 20;
        }
 
+        dimen = min (width, height);
+        
        DrawingArea::on_size_allocate (alloc);
 }
 
 int
-Panner2d::add_puck (const char* text, float x, float y)
+Panner2d::add_puck (const char* text, const AngularVector& a)
 {
-       Target* puck = new Target (x, y, text);
-
-       pair<int,Target *> newpair;
-       newpair.first = pucks.size();
-       newpair.second = puck;
-
-       pucks.insert (newpair);
+       Target* puck = new Target (a, text);
+       pucks.push_back (puck);
        puck->visible = true;
-       
+
        return 0;
 }
 
 int
-Panner2d::add_target (float x, float y)
+Panner2d::add_target (const AngularVector& a)
 {
-       Target *target = new Target (x, y, "");
-
-       pair<int,Target *> newpair;
-       newpair.first = targets.size();
-       newpair.second = target;
-
-       targets.insert (newpair);
+       Target* target = new Target (a, "");
+       targets.push_back (target);
        target->visible = true;
        queue_draw ();
 
-       return newpair.first;
-}
-
-void
-Panner2d::drop_targets ()
-{
-       for (Targets::iterator i = targets.begin(); i != targets.end(); ) {
-
-               Targets::iterator tmp;
-
-               tmp = i;
-               ++tmp;
-
-               delete i->second;
-               targets.erase (i);
-
-               i = tmp;
-       }
-
-       queue_draw ();
+       return targets.size() - 1;
 }
 
-void
-Panner2d::remove_target (int which)
-{
-       Targets::iterator i = targets.find (which);
-
-       if (i != targets.end()) {
-               delete i->second;
-               targets.erase (i);
-               queue_draw ();
-       }
-}              
-
 void
 Panner2d::handle_state_change ()
 {
-       ENSURE_GUI_THREAD(mem_fun(*this, &Panner2d::handle_state_change));
-
        queue_draw ();
 }
 
@@ -271,160 +208,67 @@ void
 Panner2d::handle_position_change ()
 {
        uint32_t n;
-       ENSURE_GUI_THREAD(mem_fun(*this, &Panner2d::handle_position_change));
 
-       for (n = 0; n < pucks.size(); ++n) {
-               float x, y;
-               panner.streampanner(n).get_position (x, y);
-               pucks[n]->x.set_value (x);
-               pucks[n]->y.set_value (y);
-       }
+        for (uint32_t i = 0; i < pucks.size(); ++i) {
+                pucks[i]->position = panner->signal_position (i);
+        }
+
+        vector<Speaker>& speakers (panner->get_speakers()->speakers());
 
        for (n = 0; n < targets.size(); ++n) {
-               targets[n]->x.set_value (panner.output(n).x);
-               targets[n]->y.set_value (panner.output(n).y);
+               targets[n]->position = speakers[n].angles();
        }
 
        queue_draw ();
 }
 
 void
-Panner2d::move_target (int which, float x, float y)
+Panner2d::move_puck (int which, const AngularVector& a)
 {
-       Targets::iterator i = targets.find (which);
-       Target *target;
-
-       if (!allow_target) {
+       if (which >= int (targets.size())) {
                return;
        }
-
-       if (i != targets.end()) {
-               target = i->second;
-               target->x.set_value (x);
-               target->y.set_value (y);
-               
-               queue_draw ();
-       }
-}              
-
-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.set_value (x);
-               target->y.set_value (y);
-               
-               queue_draw ();
-       }
-}              
-
-void
-Panner2d::show_puck (int which)
-{
-       Targets::iterator i = pucks.find (which);
-
-       if (i != pucks.end()) {
-               Target* puck = i->second;
-               if (!puck->visible) {
-                       puck->visible = true;
-                       queue_draw ();
-               }
-       }
-}
-
-void
-Panner2d::hide_puck (int which)
-{
-       Targets::iterator i = pucks.find (which);
-
-       if (i != pucks.end()) {
-               Target* puck = i->second;
-               if (!puck->visible) {
-                       puck->visible = false;
-                       queue_draw ();
-               }
-       }
-}
-
-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 ();
-               }
-       }
-}
-
-void
-Panner2d::hide_target (int which)
-{
-       Targets::iterator i = targets.find (which);
-       if (i != targets.end()) {
-               if (i->second->visible) {
-                       i->second->visible = false;
-                       queue_draw ();
-               }
-       }
+       
+       targets[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, int& which) const
 {
-       gdouble efx, efy;
-       gdouble cx, cy;
        Target *closest = 0;
        Target *candidate;
        float distance;
        float best_distance = FLT_MAX;
        int pwhich;
 
-       efx = x/width;
-       efy = y/height;
        which = 0;
        pwhich = 0;
-       is_puck = false;
-
-       for (Targets::const_iterator i = targets.begin(); i != targets.end(); ++i, ++which) {
-               candidate = i->second;
-
-               cx = candidate->x.get_value();
-               cy = candidate->y.get_value();
-
-               distance = sqrt ((cx - efx) * (cx - efx) +
-                                (cy - efy) * (cy - efy));
-
-               if (distance < best_distance) {
-                       closest = candidate;
-                       best_distance = distance;
-               }
-       }
 
        for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
-               candidate = i->second;
+               candidate = *i;
+
+               CartesianVector c;
 
-               cx = candidate->x.get_value();
-               cy = candidate->y.get_value();
+               candidate->position.cartesian (c);
+               cart_to_gtk (c);
 
-               distance = sqrt ((cx - efx) * (cx - efx) +
-                                (cy - efy) * (cy - efy));
+               distance = sqrt ((c.x - x) * (c.x - x) +
+                                (c.y - y) * (c.y - y));
 
                if (distance < best_distance) {
                        closest = candidate;
                        best_distance = distance;
-                       is_puck = true;
                        which = pwhich;
                }
        }
-       
+
+       if (best_distance > 20) { // arbitrary 
+               return 0;
+       }
+
        return closest;
-}              
+}
 
 bool
 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
@@ -446,7 +290,6 @@ bool
 Panner2d::on_expose_event (GdkEventExpose *event)
 {
        gint x, y;
-       float fx, fy;
        cairo_t* cr;
 
        cr = gdk_cairo_create (get_window()->gobj());
@@ -454,7 +297,7 @@ Panner2d::on_expose_event (GdkEventExpose *event)
        cairo_set_line_width (cr, 1.0);
 
        cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
-       if (!panner.bypassed()) {
+       if (!panner->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);
@@ -466,19 +309,25 @@ Panner2d::on_expose_event (GdkEventExpose *event)
                cairo_translate (cr, 10.0, 10.0);
        }
 
+       /* horizontal line of "crosshairs" */
+
        cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
        cairo_move_to (cr, 0.5, height/2.0+0.5);
-       cairo_line_to (cr, height+0.5, height/2+0.5);
+       cairo_line_to (cr, width+0.5, height/2+0.5);
        cairo_stroke (cr);
 
-       cairo_move_to (cr, height/2+0.5, 0.5);
-       cairo_line_to (cr, height/2+0.5, height+0.5);
+       /* vertical line of "crosshairs" */
+       
+       cairo_move_to (cr, width/2+0.5, 0.5);
+       cairo_line_to (cr, width/2+0.5, height+0.5);
        cairo_stroke (cr);
 
-       cairo_arc (cr, height/2, height/2, height/2, 0, 2.0 * M_PI);
+       /* the circle on which signals live */
+
+       cairo_arc (cr, width/2, height/2, dimen/2, 0, 2.0 * M_PI);
        cairo_stroke (cr);
 
-       if (!panner.bypassed()) {
+       if (!panner->bypassed()) {
                float arc_radius;
 
                cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
@@ -493,82 +342,53 @@ Panner2d::on_expose_event (GdkEventExpose *event)
 
                for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
 
-                       Target* puck = i->second;
+                       Target* puck = *i;
 
                        if (puck->visible) {
                                /* redraw puck */
-                               
-                               fx = min (puck->x.get_value(), 1.0);
-                               fx = max (fx, -1.0f);
-                               x = (gint) floor (width * fx - 4);
-                               
-                               fy = min (puck->y.get_value(), 1.0);
-                               fy = max (fy, -1.0f);
-                               y = (gint) floor (height * fy - 4);
-                               
+
+                               CartesianVector c;
+                                
+                               puck->position.cartesian (c);
+                               cart_to_gtk (c);
+
+                               x = (gint) floor (c.x);
+                               y = (gint) floor (c.y);
+
+                               /* XXX need to shift circles so that they are centered on the circle */
+                                        
                                cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
                                cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
                                cairo_close_path (cr);
                                cairo_fill (cr);
 
-                               /* arrow */
-                               
-                               if (height > 100.0f) {
-
-                                       float endx, endy;
-                                       endx = x;
-                                       endy = y;
-                                       
-                                       cairo_save (cr);
-                                       cairo_translate (cr, x, y);
-                                       cairo_rotate (cr, puck->azimuth.get_value());
-
-                                       /* horizontal left-to-right line (rotation will rotate it, duh) */
-
-                                       endx = 30.0;
-                                       endy = 0.0;
-
-                                       /* stem */
-                                       cairo_set_line_width (cr, 4.0);
-                                       cairo_move_to (cr, 0.0, 0.0);
-                                       cairo_line_to (cr, endx, endy);
-                                       cairo_stroke (cr);
-                                       
-                                       /* arrow head */
-                                       
-                                       cairo_move_to (cr, endx - 10.0, endy + 10.0);
-                                       cairo_line_to (cr, endx, endy);
-                                       cairo_line_to (cr, endx - 10.0, endy - 10.0);
-                                       cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-                                       cairo_stroke (cr);
-
-                                       cairo_restore (cr);
-                               }
-
                                cairo_move_to (cr, x + 6, y + 6);
-                               cairo_show_text (cr, puck->text);
+
+                               char buf[256];
+                               snprintf (buf, sizeof (buf), "%s:%d", puck->text.c_str(), (int) lrint (puck->position.azi));
+                               cairo_show_text (cr, buf);
                        }
                }
 
                /* redraw any visible targets */
-               
+
                int n = 0;
 
                for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
-                       Target *target = i->second;
+                       Target *target = *i;
                        char buf[256];
                        ++n;
 
                        if (target->visible) {
-                               
-                               fx = min (target->x.get_value(), 1.0);
-                               fx = max (fx, -1.0f);
-                               x = (gint) floor (width  * fx);
-                       
-                               fy = min (target->y.get_value(), 1.0);
-                               fy = max (fy, -1.0f);
-                               y = (gint) floor (height * fy);
 
+                               CartesianVector c;
+                                
+                               target->position.cartesian (c);
+                               cart_to_gtk (c);
+
+                               x = (int) floor (c.x);
+                               y = (int) floor (c.y);
+                                
                                snprintf (buf, sizeof (buf), "%d", n);
 
                                cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
@@ -576,13 +396,14 @@ Panner2d::on_expose_event (GdkEventExpose *event)
                                cairo_fill (cr);
                                cairo_move_to (cr, x+6, y+6);
                                cairo_show_text (cr, buf);
+
                        }
                }
        }
 
        cairo_destroy (cr);
 
-       return TRUE;
+       return true;
 }
 
 bool
@@ -597,7 +418,10 @@ Panner2d::on_button_press_event (GdkEventButton *ev)
        switch (ev->button) {
        case 1:
        case 2:
-               drag_target = find_closest_object (ev->x, ev->y, drag_index, drag_is_puck);
+               if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
+                       drag_target->set_selected (true);
+               }
+
                drag_x = (int) floor (ev->x);
                drag_y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
@@ -608,8 +432,8 @@ Panner2d::on_button_press_event (GdkEventButton *ev)
        default:
                break;
        }
-       
-       return FALSE;
+
+       return false;
 }
 
 bool
@@ -625,23 +449,21 @@ Panner2d::on_button_release_event (GdkEventButton *ev)
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
 
-               if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
-
-
+               if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
+                        
                        for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
                                //Target* puck = i->second;
-
                                /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
                        }
 
                        queue_draw ();
                        PuckMoved (-1);
-                       ret = true;
+                       ret = true;
 
                } else {
                        ret = handle_motion (x, y, state);
                }
-               
+
                drag_target = 0;
                break;
 
@@ -650,13 +472,13 @@ Panner2d::on_button_release_event (GdkEventButton *ev)
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
 
-               if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
+               if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
                        toggle_bypass ();
                        ret = true;
                } else {
                        ret = handle_motion (x, y, state);
                }
-               
+
                drag_target = 0;
                break;
 
@@ -679,95 +501,96 @@ Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
                return false;
        }
 
-       int x, y;
-       bool need_move = false;
-
-       if (!drag_is_puck && !allow_target) {
-               cerr << "dip = " << drag_is_puck << " at = " << allow_target << endl;
-               return true;
-       }
 
        if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
-
-               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.get_value()) {
-                               drag_target->x.set_value (new_x);
-                               need_move = true;
-                       }
+               CartesianVector c;
+               bool need_move = false;
+                
+               drag_target->position.cartesian (c);
+               cart_to_gtk (c);
+
+               if ((evx != c.x) || (evy != c.y)) {
+                       need_move = true;
                }
-               
-               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.get_value()) {
-                               drag_target->y.set_value (new_y);
-                               need_move = true;
-                       }
-               }
-               
+
                if (need_move) {
-                       
-                       if (drag_is_puck) {
-                               
-                               panner.streampanner(drag_index).set_position (drag_target->x.get_value(), drag_target->y.get_value(), false);
-                               
-                       } else {
-                               
-                               TargetMoved (drag_index);
-                       }
+                       CartesianVector cp (evx, evy, 0.0);
 
-                       queue_draw ();
-               }
-               
+                       /* canonicalize position */
 
-       } else if ((state & GDK_BUTTON2_MASK) && !(state & GDK_BUTTON1_MASK)) {
+                       gtk_to_cart (cp);
 
-               if (!drag_is_puck) {
-                       return false;
-               }
+                       /* position actual signal on circle */
 
-               int xdelta = drag_x - evx;
-               int ydelta = drag_x - evy;
-               
-               drag_target->azimuth.set_value (drag_target->azimuth.get_value() + (2 * M_PI) * ((float)ydelta)/height * ((float) -xdelta)/height);
-               queue_draw ();
-       }
+                       clamp_to_circle (cp.x, cp.y);
+                        
+                       /* generate an angular representation and set drag target (GUI) position */
+
+                       cp.angular (drag_target->position); /* sets drag target position */
+
+                        double degree_fract = drag_target->position.azi / 360.0;
+
+                       panner->set_position (degree_fract);
+               }
+       } 
 
        return true;
 }
 
 void
-Panner2d::toggle_bypass ()
+Panner2d::cart_to_gtk (CartesianVector& c) const
 {
-       panner.set_bypassed (!panner.bypassed());
+       /* "c" uses a coordinate space that is:
+            
+          center = 0.0
+          dimension = 2.0 * 2.0
+          so max values along each axis are -1..+1
+
+          GTK uses a coordinate space that is:
+
+          top left = 0.0
+          dimension = width * height
+          so max values along each axis are 0,width and
+          0,height
+       */
+        
+        const uint32_t hoffset = (width - dimen)/2;
+        const uint32_t voffset = (height - dimen)/2;
+
+       c.x = hoffset + ((dimen / 2) * (c.x + 1));
+       c.y = voffset + ((dimen / 2) * (1 - c.y));
+
+       /* XXX z-axis not handled - 2D for now */
 }
 
 void
-Panner2d::allow_x_motion (bool yn)
+Panner2d::gtk_to_cart (CartesianVector& c) const
 {
-       allow_x = yn;
+       c.x = ((c.x / (dimen / 2.0)) - 1.0);
+       c.y = -((c.y / (dimen / 2.0)) - 1.0);
+
+       /* XXX z-axis not handled - 2D for now */
 }
 
 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;
+        
+       PBD::cart_to_azi_ele (x, y, z, azi, ele);
+       PBD::azi_ele_to_cart (azi, ele, x, y, z);
 }
 
 void
-Panner2d::allow_y_motion (bool yn)
+Panner2d::toggle_bypass ()
 {
-       allow_y = yn;
+       panner->set_bypassed (!panner->bypassed());
 }
 
-Panner2dWindow::Panner2dWindow (Panner&p, int32_t h, uint32_t inputs)
-       : widget (p, h)
+Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
+       : ArdourDialog (_("Panner (2D)"))
+        , widget (p, h)
        , reset_button (_("Reset"))
        , bypass_button (_("Bypass"))
        , mute_button (_("Mute"))
@@ -776,7 +599,7 @@ Panner2dWindow::Panner2dWindow (Panner&p, int32_t h, uint32_t inputs)
 
        set_title (_("Panner"));
        widget.set_size_request (h, h);
-       
+
        button_box.set_spacing (6);
        button_box.pack_start (reset_button, false, false);
        button_box.pack_start (bypass_button, false, false);
@@ -800,8 +623,8 @@ Panner2dWindow::Panner2dWindow (Panner&p, int32_t h, uint32_t inputs)
        hpacker.pack_start (widget, false, false);
        hpacker.pack_start (left_side, false, false);
        hpacker.show ();
-       
-       add (hpacker);
+
+       get_vbox()->pack_start (hpacker);
        reset (inputs);
        widget.show ();
 }
@@ -811,10 +634,11 @@ Panner2dWindow::reset (uint32_t n_inputs)
 {
        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.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 ();
        }
 
@@ -823,4 +647,5 @@ Panner2dWindow::reset (uint32_t n_inputs)
                delete spinners.back();
                spinners.erase (--spinners.end());
        }
+#endif
 }