1 /*
2  * Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <gtkmm.h>
20 #include "gtkmm2ext/utils.h"
21 #include "gtkmm2ext/gtk_ui.h"
22 #include "gtkmm2ext/gui_thread.h"
23 #include "ardour/panner.h"
24 #include "pbd/controllable.h"
25 #include "stereo_panner_editor.h"
26 #include "stereo_panner.h"
27 #include "pbd/i18n.h"
28 
29 using namespace std;
30 using namespace Gtk;
31 using namespace Gtkmm2ext;
32 
33 using PBD::Controllable;
34 
StereoPannerEditor(StereoPanner * p)35 StereoPannerEditor::StereoPannerEditor (StereoPanner* p)
36 	: PannerEditor (_("Stereo Panner"))
37 	, _panner (p)
38 	, _ignore_changes (false)
39 {
40 	Table* t = manage (new Table (2, 3));
41 	t->set_spacings (6);
42 
43 	int n = 0;
44 
45 	t->attach (*manage (left_aligned_label (_("Position"))), 0, 1, n, n + 1);
46 	t->attach (_position, 1, 2, n, n + 1);
47 	t->attach (*manage (left_aligned_label (_("%"))), 2, 3, n, n + 1);
48 	++n;
49 
50 	t->attach (*manage (left_aligned_label (_("Width"))), 0, 1, n, n + 1);
51 	t->attach (_width, 1, 2, n, n + 1);
52 	t->attach (*manage (left_aligned_label (_("%"))), 2, 3, n, n + 1);
53 	++n;
54 
55 	get_vbox()->pack_start (*manage (t));
56 	get_vbox()->set_spacing (6);
57 
58 	_position.set_increments (1, 10);
59 	_width.set_increments (1, 10);
60 	set_position_range ();
61 	set_width_range ();
62 
63 	_panner->get_position_controllable()->Changed.connect (
64 		_connections, invalidator (*this), boost::bind (&StereoPannerEditor::update_editor, this), gui_context ()
65 		);
66 
67 	_panner->get_width_controllable()->Changed.connect (
68 		_connections, invalidator (*this), boost::bind (&StereoPannerEditor::update_editor, this), gui_context ()
69 		);
70 
71 	_panner->DropReferences.connect (_connections, invalidator (*this), boost::bind (&StereoPannerEditor::panner_going_away, this), gui_context ());
72 	_position.signal_value_changed().connect (sigc::mem_fun (*this, &StereoPannerEditor::position_changed));
73 	_width.signal_value_changed().connect (sigc::mem_fun (*this, &StereoPannerEditor::width_changed));
74 
75 	show_all ();
76 	update_editor ();
77 }
78 
79 void
panner_going_away()80 StereoPannerEditor::panner_going_away ()
81 {
82 	_panner = 0;
83 }
84 
85 void
update_editor()86 StereoPannerEditor::update_editor ()
87 {
88 	if (!_panner) {
89 		return;
90 	}
91 
92 	_ignore_changes = true;
93 	_position.set_value (100 * _panner->get_position_controllable()->get_value ());
94 	_width.set_value (100 * _panner->get_width_controllable()->get_value ());
95 	_ignore_changes = false;
96 }
97 
98 void
position_changed()99 StereoPannerEditor::position_changed ()
100 {
101 	if (_ignore_changes || !_panner) {
102 		return;
103 	}
104 
105 	_ignore_changes = true;
106 	double const v = _position.get_value() / 100;
107 	_panner->get_position_controllable()->set_value (v, Controllable::NoGroup);
108 	set_width_range ();
109 	_ignore_changes = false;
110 }
111 
112 void
width_changed()113 StereoPannerEditor::width_changed ()
114 {
115 	if (_ignore_changes || !_panner) {
116 		return;
117 	}
118 
119 	_ignore_changes = true;
120 	double const v = _width.get_value() / 100;
121 	_panner->get_width_controllable()->set_value (v, Controllable::NoGroup);
122 	set_position_range ();
123 	_ignore_changes = false;
124 }
125 
126 void
set_position_range()127 StereoPannerEditor::set_position_range ()
128 {
129 	pair<double, double> const pr = _panner->panner()->position_range ();
130 	_position.set_range (pr.first * 100, pr.second * 100);
131 }
132 
133 void
set_width_range()134 StereoPannerEditor::set_width_range ()
135 {
136 	pair<double, double> const wr = _panner->panner()->width_range ();
137 	_width.set_range (wr.first * 100, wr.second * 100);
138 }
139 
140