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