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