Fix sample-rate override when changing backends or devices
[ardour.git] / gtk2_ardour / panner2d.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21 #include <climits>
22 #include <string.h>
23
24 #include <cairo.h>
25 #include <gtkmm/menu.h>
26
27 #include "gtkmm2ext/gtk_ui.h"
28
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"
35
36 #include "gtkmm2ext/colors.h"
37
38 #include "panner2d.h"
39 #include "keyboard.h"
40 #include "gui_thread.h"
41 #include "rgb_macros.h"
42 #include "utils.h"
43 #include "public_editor.h"
44 #include "ui_config.h"
45
46 #include "pbd/i18n.h"
47
48 using namespace std;
49 using namespace Gtk;
50 using namespace ARDOUR;
51 using namespace ARDOUR_UI_UTILS;
52 using namespace PBD;
53 using Gtkmm2ext::Keyboard;
54
55 Panner2d::ColorScheme Panner2d::colors;
56 bool Panner2d::have_colors = false;
57
58 static const int large_size_threshold = 100;
59 static const int large_border_width = 25;
60 static const int small_border_width = 8;
61
62 Panner2d::Target::Target (const AngularVector& a, const char *txt)
63         : position (a)
64         , text (txt)
65         , _selected (false)
66 {
67 }
68
69 Panner2d::Target::~Target ()
70 {
71 }
72
73 void
74 Panner2d::Target::set_text (const char* txt)
75 {
76         text = txt;
77 }
78
79 Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
80 : panner_shell (p)
81         , position (AngularVector (0.0, 0.0), "")
82         , width (0)
83         , height (h)
84         , last_width (0)
85         , have_elevation (false)
86         , _send_mode (false)
87 {
88         if (!have_colors) {
89                 set_colors ();
90                 have_colors = true;
91         }
92
93         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &Panner2d::color_handler));
94
95         panner_shell->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
96
97         panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
98
99         drag_target = 0;
100         set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
101
102         handle_state_change ();
103         handle_position_change ();
104 }
105
106 Panner2d::~Panner2d()
107 {
108         for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
109                 delete *i;
110         }
111         for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
112                 delete *i;
113         }
114 }
115
116 void
117 Panner2d::set_colors ()
118 {
119         // TODO get all colors from theme, resolve dups
120         colors.background = UIConfiguration::instance().color ("mono panner bg");
121         colors.crosshairs =          0x4884a9ff; // 0.282, 0.517, 0.662, 1.0
122         colors.signalcircle_border = 0x84c5e1ff; // 0.517, 0.772, 0.882, 1.0
123         colors.signalcircle =        0x4884a9ff; // 0.282, 0.517, 0.662, 1.0  // also used with a = 0.9
124         colors.diffusion =           0x4884a973; // 0.282, 0.517, 0.662, 0.45
125         colors.diffusion_inv =       0xff6b6b73; // 1.0,   0.419, 0.419, 0.45
126         colors.pos_outline =         0xffe7e7d9; // 1.0,   0.905, 0.905, 0.85
127         colors.pos_fill =            0xff6b6bd9; // 1.0,   0.419, 0.419, 0.85
128         colors.signal_outline =      0x84c5e1cc; // 0.517, 0.772, 0.882, 0.8
129         colors.signal_fill =         0x4884a9bf; // 0.282, 0.517, 0.662, 0.75
130         colors.speaker_fill =        0x4884a9ff; // 0.282, 0.517, 0.662, 1.0
131         colors.text =                0x84c5e1e6; // 0.517, 0.772, 0.882, 0.9
132 }
133
134 void
135 Panner2d::color_handler ()
136 {
137         set_colors ();
138         queue_draw ();
139 }
140
141 void
142 Panner2d::reset (uint32_t n_inputs)
143 {
144         uint32_t nouts = panner_shell->panner()->out().n_audio();
145
146         /* signals */
147
148         while (signals.size() < n_inputs) {
149                 add_signal ("", AngularVector());
150         }
151
152         if (signals.size() > n_inputs) {
153                 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
154                         delete signals[i];
155                 }
156
157                 signals.resize (n_inputs);
158         }
159
160         label_signals ();
161
162         for (uint32_t i = 0; i < n_inputs; ++i) {
163                 signals[i]->position = panner_shell->panner()->signal_position (i);
164         }
165
166         /* add all outputs */
167
168         while (speakers.size() < nouts) {
169                 add_speaker (AngularVector());
170         }
171
172         if (speakers.size() > nouts) {
173                 for (uint32_t i = nouts; i < speakers.size(); ++i) {
174                         delete speakers[i];
175                 }
176
177                 speakers.resize (nouts);
178         }
179
180         for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
181                 (*x)->visible = false;
182         }
183
184         vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
185
186         for (uint32_t n = 0; n < nouts; ++n) {
187                 char buf[16];
188
189                 snprintf (buf, sizeof (buf), "%d", n+1);
190                 speakers[n]->set_text (buf);
191                 speakers[n]->position = the_speakers[n].angles();
192                 speakers[n]->visible = true;
193         }
194
195         queue_draw ();
196 }
197
198 void
199 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
200 {
201         width = alloc.get_width();
202         height = alloc.get_height();
203
204         if (height > large_size_threshold) {
205                 border = large_border_width;
206         } else {
207                 border = small_border_width;
208         }
209
210         radius = min (width, height);
211         radius -= border;
212         radius /= 2;
213         radius = rint(radius) + .5;
214
215         hoffset = max ((double) (width - height), border);
216         voffset = max ((double) (height - width), border);
217
218         hoffset = rint(hoffset / 2.0);
219         voffset = rint(voffset / 2.0);
220
221         DrawingArea::on_size_allocate (alloc);
222 }
223
224 int
225 Panner2d::add_signal (const char* text, const AngularVector& a)
226 {
227         Target* signal = new Target (a, text);
228         signals.push_back (signal);
229         signal->visible = true;
230
231         return 0;
232 }
233
234 int
235 Panner2d::add_speaker (const AngularVector& a)
236 {
237         Target* speaker = new Target (a, "");
238         speakers.push_back (speaker);
239         speaker->visible = true;
240         queue_draw ();
241
242         return speakers.size() - 1;
243 }
244
245 void
246 Panner2d::handle_state_change ()
247 {
248         panner_connections.drop_connections();
249         if (!panner_shell->panner()) {
250                 return;
251         }
252
253         panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
254
255         set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
256         set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
257         bool elev = have_elevation;
258         have_elevation = (p == params.end()) ? false : true;
259         if (elev != have_elevation) {
260                 handle_position_change();
261         }
262         queue_draw ();
263 }
264
265 void
266 Panner2d::label_signals ()
267 {
268         uint32_t sz = signals.size();
269
270         switch (sz) {
271                 case 0:
272                         break;
273
274                 case 1:
275                         signals[0]->set_text ("");
276                         break;
277
278                 case 2:
279                         signals[0]->set_text (S_("Panner|L"));
280                         signals[1]->set_text (S_("Panner|R"));
281                         break;
282
283                 default:
284                         for (uint32_t i = 0; i < sz; ++i) {
285                                 char buf[64];
286                                 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
287                                 signals[i]->set_text (buf);
288                         }
289                         break;
290         }
291 }
292
293 void
294 Panner2d::handle_position_change ()
295 {
296         uint32_t n;
297         double w = panner_shell->pannable()->pan_width_control->get_value();
298
299         position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
300                                            panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
301
302         for (uint32_t i = 0; i < signals.size(); ++i) {
303                 signals[i]->position = panner_shell->panner()->signal_position (i);
304         }
305
306         if (w * last_width <= 0) {
307                 /* changed sign */
308                 label_signals ();
309         }
310
311         last_width = w;
312
313         vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
314
315         for (n = 0; n < speakers.size(); ++n) {
316                 speakers[n]->position = the_speakers[n].angles();
317         }
318
319         queue_draw ();
320 }
321
322 void
323 Panner2d::move_signal (int which, const AngularVector& a)
324 {
325         if (which >= int (speakers.size())) {
326                 return;
327         }
328
329         speakers[which]->position = a;
330         queue_draw ();
331 }
332
333 Panner2d::Target *
334 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
335 {
336         Target *closest = 0;
337         Target *candidate;
338         float distance;
339         float best_distance = FLT_MAX;
340         CartesianVector c;
341
342         /* start with the position itself */
343
344         PBD::AngularVector dp = position.position;
345         if (!have_elevation) dp.ele = 0;
346         dp.azi = 270 - dp.azi;
347         dp.cartesian (c);
348
349         cart_to_gtk (c);
350         best_distance = sqrt ((c.x - x) * (c.x - x) +
351                         (c.y - y) * (c.y - y));
352         closest = &position;
353
354 #if 0 // TODO signal grab -> change width, not position
355         for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
356                 candidate = *i;
357
358                 candidate->position.cartesian (c);
359                 cart_to_gtk (c);
360
361                 distance = sqrt ((c.x - x) * (c.x - x) +
362                                 (c.y - y) * (c.y - y));
363
364                 if (distance < best_distance) {
365                         closest = candidate;
366                         best_distance = distance;
367                 }
368         }
369 #endif
370
371         is_signal = true;
372
373         if (height > large_size_threshold) {
374                 /* "big" */
375                 if (best_distance > 30) { // arbitrary
376                         closest = 0;
377                 }
378         } else {
379                 /* "small" */
380                 if (best_distance > 10) { // arbitrary
381                         closest = 0;
382                 }
383         }
384
385         /* if we didn't find a signal close by, check the speakers */
386
387         if (!closest) {
388                 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
389                         candidate = *i;
390                         PBD::AngularVector sp = candidate->position;
391                         sp.azi = 270 -sp.azi;
392                         CartesianVector c;
393                         sp.cartesian (c);
394                         cart_to_gtk (c);
395
396                         distance = sqrt ((c.x - x) * (c.x - x) +
397                                         (c.y - y) * (c.y - y));
398
399                         if (distance < best_distance) {
400                                 closest = candidate;
401                                 best_distance = distance;
402                         }
403                 }
404
405                 if (height > large_size_threshold) {
406                         /* "big" */
407                         if (best_distance < 30) { // arbitrary
408                                 is_signal = false;
409                         } else {
410                                 closest = 0;
411                         }
412                 } else {
413                         /* "small" */
414                         if (best_distance < 10) { // arbitrary
415                                 is_signal = false;
416                         } else {
417                                 closest = 0;
418                         }
419                 }
420         }
421
422         return closest;
423 }
424
425 void
426 Panner2d::set_send_drawing_mode (bool onoff)
427 {
428         if (_send_mode != onoff) {
429                 _send_mode = onoff;
430                 queue_draw ();
431         }
432 }
433
434 bool
435 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
436 {
437         gint x, y;
438         GdkModifierType state;
439
440         if (ev->is_hint) {
441                 gdk_window_get_pointer (ev->window, &x, &y, &state);
442         } else {
443                 x = (int) floor (ev->x);
444                 y = (int) floor (ev->y);
445                 state = (GdkModifierType) ev->state;
446         }
447
448         if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
449                 did_move = true;
450         }
451
452         return handle_motion (x, y, state);
453 }
454
455 #define CSSRGBA(CL) \
456         cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(CL), UINT_RGBA_G_FLT(CL), UINT_RGBA_B_FLT(CL), UINT_RGBA_A_FLT(CL));
457
458 #define CSSRGB(CL, A) \
459         cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(CL), UINT_RGBA_G_FLT(CL), UINT_RGBA_B_FLT(CL), A);
460 bool
461 Panner2d::on_expose_event (GdkEventExpose *event)
462 {
463         CartesianVector c;
464         cairo_t* cr;
465         bool xsmall = (height <= large_size_threshold);
466         const double diameter = radius*2.0;
467
468         cr = gdk_cairo_create (get_window()->gobj());
469
470         /* background */
471
472         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
473
474         uint32_t bg = colors.background;
475         if (_send_mode) {
476                 bg = UIConfiguration::instance().color ("send bg");
477         }
478
479         if (!panner_shell->bypassed()) {
480                 CSSRGBA(bg);
481         } else {
482                 CSSRGB(bg, 0.2);
483         }
484         cairo_fill_preserve (cr);
485         cairo_clip (cr);
486
487         /* offset to give us some border */
488
489         cairo_translate (cr, hoffset, voffset);
490
491         cairo_set_line_width (cr, 1.0);
492
493         /* horizontal line of "crosshairs" */
494
495         CSSRGBA(colors.crosshairs);
496         cairo_move_to (cr, 0.0, radius);
497         cairo_line_to (cr, diameter, radius);
498         cairo_stroke (cr);
499
500         /* vertical line of "crosshairs" */
501
502         cairo_move_to (cr, radius, 0);
503         cairo_line_to (cr, radius, diameter);
504         cairo_stroke (cr);
505
506         /* the circle on which signals live */
507
508         cairo_set_line_width (cr, 1.5);
509         CSSRGBA(colors.signalcircle_border);
510         cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
511         cairo_stroke (cr);
512
513         for (uint32_t rad = 15; rad < 90; rad += 15) {
514                 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
515                 if (rad == 45) {
516                         CSSRGBA(colors.signalcircle);
517                 } else {
518                         CSSRGB(colors.signalcircle, 0.9);
519                 }
520                 cairo_new_path (cr);
521                 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
522                 cairo_stroke (cr);
523         }
524
525         if (!panner_shell->bypassed()) {
526                 /* convention top == front ^= azimuth == .5 (same as stereo/mono panners) */
527
528                 if (signals.size() > 1) {
529                         /* arc to show "diffusion" */
530
531                         double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
532                         double position_angle = panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
533
534                         cairo_save (cr);
535                         cairo_translate (cr, radius, radius);
536                         cairo_rotate (cr, M_PI / 2.0);
537                         cairo_rotate (cr, position_angle - (width_angle/2.0));
538                         cairo_move_to (cr, 0, 0);
539                         cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
540                         cairo_close_path (cr);
541                         if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
542                                 /* normal width */
543                                 CSSRGBA(colors.diffusion);
544                         } else {
545                                 /* inverse width */
546                                 CSSRGBA(colors.diffusion_inv);
547                         }
548                         cairo_fill (cr);
549                         cairo_restore (cr);
550                 }
551
552                 double arc_radius;
553
554                 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
555
556                 if (xsmall) {
557                         arc_radius = 4.0;
558                 } else {
559                         cairo_set_font_size (cr, 10);
560                         arc_radius = 12.0;
561                 }
562
563                 /* draw position */
564
565                 PBD::AngularVector dp = position.position;
566                 if (!have_elevation) dp.ele = 0;
567                 dp.azi = 270 - dp.azi;
568                 dp.cartesian (c);
569                 cart_to_gtk (c);
570
571                 cairo_new_path (cr);
572                 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
573                 CSSRGBA(colors.pos_fill);
574                 cairo_fill_preserve (cr);
575                 CSSRGBA(colors.pos_outline);
576                 cairo_stroke (cr);
577
578                 /* signals */
579
580                 if (signals.size() > 0) {
581                         for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
582                                 Target* signal = *i;
583
584                                 if (signal->visible) {
585
586                                         /* TODO check for overlap - multiple src at same position
587                                          * -> visualize it properly
588                                          */
589                                         PBD::AngularVector sp = signal->position;
590                                         if (!have_elevation) sp.ele = 0;
591                                         sp.azi += 270.0;
592                                         sp.cartesian (c);
593                                         cart_to_gtk (c);
594
595                                         cairo_new_path (cr);
596                                         cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
597                                         CSSRGBA(colors.signal_fill);
598                                         cairo_fill_preserve (cr);
599                                         CSSRGBA(colors.signal_outline);
600                                         cairo_stroke (cr);
601
602                                         if (!xsmall && !signal->text.empty()) {
603                                                 CSSRGBA(colors.text);
604                                                 /* the +/- adjustments are a hack to try to center the text in the circle
605                                                  * TODO use pango get_pixel_size() -- see mono_panner.cc
606                                                  */
607                                                 if (xsmall) {
608                                                         cairo_move_to (cr, c.x - 1, c.y + 1);
609                                                 } else {
610                                                         cairo_move_to (cr, c.x - 4, c.y + 4);
611                                                 }
612                                                 cairo_show_text (cr, signal->text.c_str());
613                                         }
614                                 }
615                         }
616                 }
617
618                 /* speakers */
619
620                 int n = 0;
621
622                 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
623                         Target *speaker = *i;
624                         char buf[256];
625                         ++n;
626
627                         if (speaker->visible) {
628
629                                 PBD::AngularVector sp = speaker->position;
630                                 sp.azi += 270.0;
631                                 CartesianVector c;
632                                 sp.cartesian (c);
633                                 cart_to_gtk (c);
634
635                                 snprintf (buf, sizeof (buf), "%d", n);
636
637                                 /* stroke out a speaker shape */
638
639                                 cairo_move_to (cr, c.x, c.y);
640                                 cairo_save (cr);
641                                 cairo_rotate (cr, -(sp.azi/360.0) * (2.0 * M_PI));
642                                 if (xsmall) {
643                                         cairo_scale (cr, 0.8, 0.8);
644                                 } else {
645                                         cairo_scale (cr, 1.2, 1.2);
646                                 }
647                                 cairo_rel_line_to (cr, 4, -2);
648                                 cairo_rel_line_to (cr, 0, -7);
649                                 cairo_rel_line_to (cr, 5, +5);
650                                 cairo_rel_line_to (cr, 5, 0);
651                                 cairo_rel_line_to (cr, 0, 5);
652                                 cairo_rel_line_to (cr, -5, 0);
653                                 cairo_rel_line_to (cr, -5, +5);
654                                 cairo_rel_line_to (cr, 0, -7);
655                                 cairo_close_path (cr);
656                                 CSSRGBA(colors.speaker_fill);
657                                 cairo_fill (cr);
658                                 cairo_restore (cr);
659
660                                 if (!xsmall) {
661                                         cairo_set_font_size (cr, 16);
662
663                                         /* move the text in just a bit */
664
665                                         AngularVector textpos (speaker->position.azi + 270.0, speaker->position.ele, 0.85);
666
667                                         textpos.cartesian (c);
668                                         cart_to_gtk (c);
669                                         cairo_move_to (cr, c.x, c.y);
670                                         cairo_show_text (cr, buf);
671                                 }
672
673                         }
674                 }
675         }
676
677         cairo_destroy (cr);
678
679         return true;
680 }
681
682 bool
683 Panner2d::on_button_press_event (GdkEventButton *ev)
684 {
685         GdkModifierType state;
686         int x;
687         int y;
688         bool is_signal;
689
690         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
691                 return false;
692         }
693
694         did_move = false;
695
696         switch (ev->button) {
697         case 1:
698         case 2:
699                 x = ev->x - hoffset;
700                 y = ev->y - voffset;
701
702                 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
703                         if (!is_signal) {
704                                 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
705                                 drag_target = 0;
706                         } else {
707                                 drag_target->set_selected (true);
708                         }
709                 }
710
711                 state = (GdkModifierType) ev->state;
712                 return handle_motion (ev->x, ev->y, state);
713                 break;
714
715         default:
716                 break;
717         }
718
719         return false;
720 }
721
722 bool
723 Panner2d::on_button_release_event (GdkEventButton *ev)
724 {
725         gint x, y;
726         GdkModifierType state;
727         bool ret = false;
728
729         switch (ev->button) {
730         case 1:
731                 x = (int) floor (ev->x);
732                 y = (int) floor (ev->y);
733                 state = (GdkModifierType) ev->state;
734                 ret = handle_motion (x, y, state);
735                 drag_target = 0;
736                 break;
737
738         case 2:
739                 x = (int) floor (ev->x);
740                 y = (int) floor (ev->y);
741                 state = (GdkModifierType) ev->state;
742
743                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
744                         toggle_bypass ();
745                         ret = true;
746                 } else {
747                         ret = handle_motion (x, y, state);
748                 }
749
750                 drag_target = 0;
751                 break;
752
753         case 3:
754                 break;
755
756         }
757
758         return ret;
759 }
760
761 gint
762 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
763 {
764         if (drag_target == 0) {
765                 return false;
766         }
767
768         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
769                 return false;
770         }
771
772         evx -= hoffset;
773         evy -= voffset;
774
775         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
776                 CartesianVector c;
777                 bool need_move = false;
778
779                 drag_target->position.cartesian (c);
780                 cart_to_gtk (c);
781
782                 if ((evx != c.x) || (evy != c.y)) {
783                         need_move = true;
784                 }
785
786                 if (need_move) {
787                         CartesianVector cp (evx, evy, 0.0);
788                         AngularVector av;
789                         gtk_to_cart (cp);
790
791                         if (!have_elevation) {
792                                 clamp_to_circle (cp.x, cp.y);
793                                 cp.angular (av);
794                                 av.azi = fmod(270 - av.azi, 360);
795                                 if (drag_target == &position) {
796                                         double degree_fract = av.azi / 360.0;
797                                         panner_shell->panner()->set_position (degree_fract);
798                                 }
799                         } else {
800                                 /* sphere projection */
801                                 sphere_project (cp.x, cp.y, cp.z);
802
803                                 double r2d = 180.0 / M_PI;
804                                 av.azi = r2d * atan2(cp.y, cp.x);
805                                 av.ele = r2d * asin(cp.z);
806                                 av.azi = fmod(270 - av.azi, 360);
807
808                                 if (drag_target == &position) {
809                                         double azi_fract = av.azi / 360.0;
810                                         double ele_fract = av.ele / 90.0;
811                                         panner_shell->panner()->set_position (azi_fract);
812                                         panner_shell->panner()->set_elevation (ele_fract);
813                                 }
814                         }
815                 }
816         }
817
818         return true;
819 }
820
821 bool
822 Panner2d::on_scroll_event (GdkEventScroll* ev)
823 {
824         switch (ev->direction) {
825         case GDK_SCROLL_UP:
826         case GDK_SCROLL_RIGHT:
827                 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
828                 break;
829
830         case GDK_SCROLL_DOWN:
831         case GDK_SCROLL_LEFT:
832                 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
833                 break;
834         }
835         return true;
836 }
837
838 void
839 Panner2d::cart_to_gtk (CartesianVector& c) const
840 {
841         /* cartesian coordinate space:
842               center = 0.0
843               dimension = 2.0 * 2.0
844               increasing y moves up
845               so max values along each axis are -1..+1
846
847            GTK uses a coordinate space that is:
848               top left = 0.0
849               dimension = (radius*2.0) * (radius*2.0)
850               increasing y moves down
851         */
852         const double diameter = radius*2.0;
853
854         c.x = diameter * ((c.x + 1.0) / 2.0);
855         /* extra subtraction inverts the y-axis to match "increasing y moves down" */
856         c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
857 }
858
859 void
860 Panner2d::gtk_to_cart (CartesianVector& c) const
861 {
862         const double diameter = radius*2.0;
863         c.x = ((c.x / diameter) * 2.0) - 1.0;
864         c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
865 }
866
867 void
868 Panner2d::sphere_project (double& x, double& y, double& z)
869 {
870         double r, r2;
871         r2 = x * x + y * y;
872         if (r2 < 1.0) {
873                 z = sqrt (1.0 - r2);
874         } else {
875                 r = sqrt (r2);
876                 x = x / r;
877                 y = y / r;
878                 z = 0.0;
879         }
880 }
881
882 void
883 Panner2d::clamp_to_circle (double& x, double& y)
884 {
885         double azi, ele;
886         double z = 0.0;
887         double l;
888         PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
889         PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
890 }
891
892 void
893 Panner2d::toggle_bypass ()
894 {
895         panner_shell->set_bypassed (!panner_shell->bypassed());
896 }
897
898 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
899         : ArdourWindow (_("Panner (2D)"))
900         , widget (p, h)
901         , bypass_button (_("Bypass"))
902         , width_adjustment (0, -100, 100, 1, 5, 0)
903         , width_spinner (width_adjustment)
904 {
905         widget.set_name ("MixerPanZone");
906
907         set_title (_("Panner"));
908         widget.set_size_request (h, h);
909
910         bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
911         width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
912
913         p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
914         /* needed for the width-spinbox in the main window */
915         p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
916         p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
917
918
919         button_box.set_spacing (6);
920         button_box.pack_start (bypass_button, false, false);
921
922         left_side.set_spacing (6);
923
924         left_side.pack_start (button_box, false, false);
925
926         Gtk::Label* l = manage (new Label (
927                                 p->panner()->describe_parameter(PanWidthAutomation),
928                                 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
929         spinner_box.pack_start (*l, false, false);
930         spinner_box.pack_start (width_spinner, false, false);
931         left_side.pack_start (spinner_box, false, false);
932
933         l->show ();
934         bypass_button.show ();
935         button_box.show ();
936         width_spinner.show ();
937         spinner_box.show ();
938         left_side.show ();
939
940         hpacker.set_spacing (6);
941         hpacker.set_border_width (12);
942         hpacker.pack_start (widget, false, false);
943         hpacker.pack_start (left_side, false, false);
944         hpacker.show ();
945
946         add (hpacker);
947         reset (inputs);
948         set_width();
949         set_bypassed();
950         widget.show ();
951 }
952
953 void
954 Panner2dWindow::reset (uint32_t n_inputs)
955 {
956         widget.reset (n_inputs);
957 }
958
959 void
960 Panner2dWindow::bypass_toggled ()
961 {
962         bool view = bypass_button.get_active ();
963         bool model = widget.get_panner_shell()->bypassed ();
964
965         if (model != view) {
966                 widget.get_panner_shell()->set_bypassed (view);
967         }
968 }
969 void
970 Panner2dWindow::width_changed ()
971 {
972         float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
973         float view  = width_spinner.get_value() / 100.0;
974         if (model != view) {
975                 widget.get_panner_shell()->panner()->set_width (view);
976         }
977 }
978
979 void
980 Panner2dWindow::pannable_handler ()
981 {
982         panvalue_connections.drop_connections();
983         widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
984         set_width();
985 }
986
987 void
988 Panner2dWindow::set_bypassed ()
989 {
990         bool view = bypass_button.get_active ();
991         bool model = widget.get_panner_shell()->bypassed ();
992         if (model != view) {
993                 bypass_button.set_active(model);
994         }
995
996         set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
997         set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
998         if (p == params.end()) {
999                 spinner_box.set_sensitive(false);
1000         } else {
1001                 spinner_box.set_sensitive(true);
1002         }
1003 }
1004
1005 void
1006 Panner2dWindow::set_width ()
1007 {
1008         // rounding of spinbox is different from slider -- TODO use slider
1009         float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
1010         float view  = (width_spinner.get_value());
1011         if (model != view) {
1012                 width_spinner.set_value (model);
1013         }
1014 }
1015
1016 bool
1017 Panner2dWindow::on_key_press_event (GdkEventKey* event)
1018 {
1019         return relay_key_press (event, this);
1020 }
1021
1022 bool
1023 Panner2dWindow::on_key_release_event (GdkEventKey*)
1024 {
1025         return true;
1026 }