in case sidechain is fed by a Send, show the send's gain control
[ardour.git] / gtk2_ardour / plugin_pin_dialog.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <boost/algorithm/string.hpp>
21
22 #include <gtkmm/table.h>
23 #include <gtkmm/frame.h>
24 #include <gtkmm/box.h>
25 #include <gtkmm/label.h>
26
27 #include "pbd/replace_all.h"
28
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31
32 #include "ardour/audioengine.h"
33 #include "ardour/plugin.h"
34 #include "ardour/port.h"
35 #include "ardour/profile.h"
36 #include "ardour/session.h"
37
38 #include "plugin_pin_dialog.h"
39 #include "gui_thread.h"
40 #include "timers.h"
41 #include "tooltips.h"
42 #include "ui_config.h"
43
44 #include "i18n.h"
45
46 using namespace ARDOUR;
47 using namespace PBD;
48 using namespace std;
49 using namespace Gtk;
50 using namespace Gtkmm2ext;
51
52 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
53         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
54         , _set_config (_("Manual Config"), ArdourButton::led_default_elements)
55         , _tgl_sidechain (_("Side Chain"), ArdourButton::led_default_elements)
56         , _add_plugin (_("+"))
57         , _del_plugin (_("-"))
58         , _add_output_audio (_("+"))
59         , _del_output_audio (_("-"))
60         , _add_output_midi (_("+"))
61         , _del_output_midi (_("-"))
62         , _add_sc_audio (_("Audio"))
63         , _add_sc_midi (_("MIDI"))
64         , _pi (pi)
65         , _pin_box_size (10)
66         , _width (0)
67         , _height (0)
68         , _innerwidth (0)
69         , _margin_x (28)
70         , _margin_y (40)
71         , _min_width (300)
72         , _min_height (200)
73         , _n_inputs (0)
74         , _n_sidechains (0)
75         , _position_valid (false)
76         , _ignore_updates (false)
77         , _sidechain_selector (0)
78         , _dragging (false)
79 {
80         assert (pi->owner ()); // Route
81
82         _pi->PluginIoReConfigure.connect (
83                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
84                         );
85
86         _pi->PluginMapChanged.connect (
87                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
88                         );
89
90         _pi->PluginConfigChanged.connect (
91                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
92                         );
93
94         _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
95         _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
96         _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
97
98         _tgl_sidechain.set_name ("pinrouting sidechain");
99         _set_config.set_name ("pinrouting custom");
100
101         Menu_Helpers::MenuList& citems = reset_menu.items ();
102         reset_menu.set_name ("ArdourContextMenu");
103         citems.clear ();
104         citems.push_back (Menu_Helpers::MenuElem (_("Reset"), sigc::mem_fun (*this, &PluginPinDialog::reset_mapping)));
105
106         _pm_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
107         _add_plugin.set_tweaks (ArdourButton::Square);
108         _del_plugin.set_tweaks (ArdourButton::Square);
109         _pm_size_group->add_widget (_add_plugin);
110         _pm_size_group->add_widget (_del_plugin);
111         _pm_size_group->add_widget (_add_output_audio);
112         _pm_size_group->add_widget (_del_output_audio);
113         _pm_size_group->add_widget (_add_output_midi);
114         _pm_size_group->add_widget (_del_output_midi);
115
116         Box* box;
117         Frame *f;
118
119         VBox* tl = manage (new VBox ());
120         tl->set_border_width (2);
121         tl->set_spacing (2);
122
123         VBox* tr = manage (new VBox ());
124         tr->set_border_width (2);
125         tr->set_spacing (2);
126
127         /* left side */
128         tl->pack_start (_set_config, false, false);
129
130         box = manage (new HBox ());
131         box->set_border_width (2);
132         box->pack_start (_add_plugin, true, false);
133         box->pack_start (_del_plugin, true, false);
134         f = manage (new Frame ());
135         f->set_label (_("Instances"));
136         f->add (*box);
137         tl->pack_start (*f, false, false);
138
139         box = manage (new HBox ());
140         box->set_border_width (2);
141         box->pack_start (_add_output_audio, true, false);
142         box->pack_start (_del_output_audio, true, false);
143         f = manage (new Frame ());
144         f->set_label (_("Audio Out"));
145         f->add (*box);
146         tl->pack_start (*f, false, false);
147
148         box = manage (new HBox ());
149         box->set_border_width (2);
150         box->pack_start (_add_output_midi, true, false);
151         box->pack_start (_del_output_midi, true, false);
152         f = manage (new Frame ());
153         f->set_label (_("MIDI Out"));
154         f->add (*box);
155         tl->pack_start (*f, false, false);
156
157         tl->pack_start (*manage (new Label ("")), true, true); // invisible separator
158         tl->pack_start (*manage (new HSeparator ()), false, false, 4);
159         _out_presets.disable_scrolling ();
160         ARDOUR_UI_UTILS::set_tooltip (_out_presets, _("Output Presets"));
161         tl->pack_start (_out_presets, false, false);
162
163         /* right side */
164         _sidechain_tbl = manage (new Gtk::Table ());
165         _sidechain_tbl->set_spacings (2);
166
167         tr->pack_start (_tgl_sidechain, false, false);
168         tr->pack_start (*_sidechain_tbl, true, true);
169
170         box = manage (new VBox ());
171         box->set_border_width (2);
172         box->set_spacing (2);
173         box->pack_start (_add_sc_audio, false, false);
174         box->pack_start (_add_sc_midi , false, false);
175         f = manage (new Frame ());
176         f->set_label (_("Add Sidechain Input"));
177         f->add (*box);
178
179         tr->pack_start (*f, false, false);
180
181         /* global packing */
182         HBox* hbox = manage (new HBox ());
183         hbox->set_spacing (4);
184         hbox->pack_start (*tl, false, false);
185         hbox->pack_start (darea, true, true);
186         hbox->pack_start (*tr, false, false);
187
188         VBox* vbox = manage (new VBox ());
189         vbox->pack_start (*hbox, true, true);
190         set_border_width (4);
191         add (*vbox);
192         vbox->show_all ();
193
194         plugin_reconfigured ();
195
196         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
197         darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
198         darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
199         darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
200         darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
201         darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
202         darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
203
204         _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
205
206         _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
207         _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
208         _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
209
210         _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
211         _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
212         _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
213         _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
214         _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
215         _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
216
217         AudioEngine::instance ()->PortConnectedOrDisconnected.connect (
218                         _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
219                         );
220 }
221
222 PluginPinDialog::~PluginPinDialog ()
223 {
224         delete _sidechain_selector;
225 }
226
227 void
228 PluginPinDialog::set_session (ARDOUR::Session *s)
229 {
230         SessionHandlePtr::set_session (s);
231         plugin_reconfigured ();
232 }
233
234 void
235 PluginPinDialog::queue_idle_update ()
236 {
237         /* various actions here are directly executed, in the GUI thread,
238          * with the GUI-thread eventually taking the process and processor lock.
239          * "connect gui_context()" will call back immediately and this
240          * signal-handler will run with the locks held.
241          *
242          * This will lead to a crash with calling nth_send() which takes
243          * a processor read-lock while holding a write lock in the same thread.
244          *
245          * decouple update to GUI idle.
246          *
247          * BUT, do delete existing controls here (in case they're affected by
248          * the change and hit by the Timer before idle comes around)
249          */
250         for (list<Control*>::iterator i = _controls.begin (); i != _controls.end (); ++i) {
251                 _sidechain_tbl->remove ((*i)->box);
252                 delete *i;
253         }
254         _controls.clear ();
255         Glib::signal_idle().connect (sigc::mem_fun(*this, &PluginPinDialog::idle_update));
256 }
257
258 bool
259 PluginPinDialog::idle_update ()
260 {
261         plugin_reconfigured ();
262         return false;
263 }
264
265
266 void
267 PluginPinDialog::plugin_reconfigured ()
268 {
269         ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
270         if (_ignore_updates) {
271                 return;
272         }
273         _n_plugins = _pi->get_count ();
274         _pi->configured_io (_in, _out);
275         _ins = _pi->internal_streams (); // with sidechain
276         _sinks = _pi->natural_input_streams ();
277         _sources = _pi->natural_output_streams ();
278
279
280         _tgl_sidechain.set_active (_pi->has_sidechain ());
281         _add_sc_audio.set_sensitive (_pi->has_sidechain ());
282         _add_sc_midi.set_sensitive (_pi->has_sidechain ());
283
284         if (_pi->custom_cfg ()) {
285                 _set_config.set_active (true);
286                 _add_plugin.set_sensitive (true);
287                 _add_output_audio.set_sensitive (true);
288                 _add_output_midi.set_sensitive (true);
289                 _del_plugin.set_sensitive (_n_plugins > 1);
290                 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
291                 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
292                 _out_presets.set_sensitive (false);
293                 _out_presets.set_text (_("Manual"));
294         } else {
295                 _set_config.set_active (false);
296                 _add_plugin.set_sensitive (false);
297                 _add_output_audio.set_sensitive (false);
298                 _add_output_midi.set_sensitive (false);
299                 _del_plugin.set_sensitive (false);
300                 _del_output_audio.set_sensitive (false);
301                 _del_output_midi.set_sensitive (false);
302                 _out_presets.set_sensitive (true);
303                 refill_output_presets ();
304         }
305
306         if (!_pi->has_sidechain () && _sidechain_selector) {
307                 delete _sidechain_selector;
308                 _sidechain_selector = 0;
309         }
310
311         refill_sidechain_table ();
312
313         /* update elements */
314
315         _elements.clear ();
316         _hover.reset ();
317         _actor.reset ();
318         _selection.reset ();
319         _drag_dst.reset ();
320         _dragging = false;
321
322         _n_inputs = _n_sidechains = 0;
323
324         for (uint32_t i = 0; i < _ins.n_total (); ++i) {
325                 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
326                 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
327                 bool sidechain = id >= _in.get (dt) ? true : false;
328                 if (sidechain) {
329                         ++_n_sidechains;
330                 } else {
331                         ++_n_inputs;
332                 }
333
334                 CtrlWidget cw (CtrlWidget ("", Input, dt, id, 0, sidechain));
335                 _elements.push_back (cw);
336         }
337
338         for (uint32_t i = 0; i < _out.n_total (); ++i) {
339                 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
340                 _elements.push_back (CtrlWidget ("", Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
341         }
342
343         for (uint32_t n = 0; n < _n_plugins; ++n) {
344                 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
345                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
346                         DataType dt (i < _sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
347                         int idx = (dt == DataType::MIDI) ? i : i - _sinks.n_midi ();
348                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
349                         CtrlWidget cw (CtrlWidget (iod.name, Sink, dt, idx, n, iod.is_sidechain));
350                         _elements.push_back (cw);
351                 }
352                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
353                         DataType dt (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO);
354                         int idx = (dt == DataType::MIDI) ? i : i - _sources.n_midi ();
355                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, false, idx));
356                         _elements.push_back (CtrlWidget (iod.name, Source, dt, idx, n));
357                 }
358         }
359
360         /* calc minimum size */
361         const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
362         const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
363         uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
364         min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
365         min_width = std::max ((uint32_t)300, min_width);
366
367         min_width = 50 + 10 * ceilf (min_width / 10.f);
368
369         uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
370         min_height = std::max ((uint32_t)200, min_height);
371         min_height = 4 * ceilf (min_height / 4.f);
372
373         if (min_width != _min_width || min_height != _min_height) {
374                 _min_width = min_width;
375                 _min_height = min_height;
376                 darea.queue_resize ();
377         }
378
379         _position_valid = false;
380         darea.queue_draw ();
381 }
382
383 void
384 PluginPinDialog::refill_sidechain_table ()
385 {
386         Table_Helpers::TableList& kids = _sidechain_tbl->children ();
387         for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
388                 i = kids.erase (i);
389         }
390         _sidechain_tbl->resize (1, 1);
391         for (list<Control*>::iterator i = _controls.begin (); i != _controls.end (); ++i) {
392                 delete *i;
393         }
394         _controls.clear ();
395         if (!_pi->has_sidechain () && _sidechain_selector) {
396                 return;
397         }
398         boost::shared_ptr<IO> io = _pi->sidechain_input ();
399         if (!io) {
400                 return;
401         }
402
403         uint32_t r = 0;
404         PortSet& p (io->ports ());
405         bool can_remove = p.num_ports () > 1;
406         for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i) {
407                 r += add_port_to_table (*i, r, can_remove);
408         }
409         for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i) {
410                 r += add_port_to_table (*i, r, can_remove);
411         }
412         _sidechain_tbl->show_all ();
413 }
414
415 void
416 PluginPinDialog::refill_output_presets ()
417 {
418         using namespace Menu_Helpers;
419         _out_presets.clear_items ();
420         PluginOutputConfiguration ppc (_pi->plugin (0)->possible_output ());
421
422         bool need_dropdown = true;
423
424         if (ppc.size () == 0) {
425                 need_dropdown = false;
426         }
427
428         if (!_pi->strict_io () && ppc.size () == 1) {
429                 need_dropdown = false;
430         }
431
432         if (_pi->strict_io () && ppc.size () == 1) {
433                 // "stereo" is currently preferred default for instruments, see PluginInsert
434                 if (ppc.find (2) != ppc.end ()) {
435                         need_dropdown = false;
436                 }
437         }
438
439         if (!_pi->needs_midi_input ()) {
440                 /* loose definition of instruments, maybe impose additional
441                  * || _pi->natural_input_streams ().n_audio () != 0
442                  * and special case variable output plugins
443                  * && !_pi->plugin (0)->info->reconfigurable_io()
444                  */
445                 need_dropdown = false;
446         }
447
448         if (!need_dropdown) {
449                 _out_presets.set_sensitive (false);
450                 _out_presets.set_text (_("Automatic"));
451                 return;
452         }
453
454         _out_presets.AddMenuElem (MenuElem (_("Automatic"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::select_output_preset), 0)));
455
456         const uint32_t n_audio = _pi->preset_out ().n_audio ();
457         if (n_audio == 0) {
458                 _out_presets.set_text (_("Automatic"));
459         }
460
461         if (ppc.find (0) != ppc.end ()) {
462                 // anyting goes
463                 ppc.clear ();
464                 if (n_audio != 0) {
465                         ppc.insert (n_audio);
466                 }
467                 ppc.insert (1);
468                 ppc.insert (2);
469                 ppc.insert (8);
470                 ppc.insert (16);
471                 ppc.insert (24);
472                 ppc.insert (32);
473         }
474
475         for (PluginOutputConfiguration::const_iterator i = ppc.begin () ; i != ppc.end (); ++i) {
476                 assert (*i > 0);
477                 std::string tmp;
478                 switch (*i) {
479                         case 1:
480                                 tmp = _("Mono");
481                                 break;
482                         case 2:
483                                 tmp = _("Stereo");
484                                 break;
485                         default:
486                                 tmp = string_compose (P_("%1 Channel", "%1 Channels", *i), *i);
487                                 break;
488                 }
489                 _out_presets.AddMenuElem (MenuElem (tmp, sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::select_output_preset), *i)));
490                 if (n_audio == *i) {
491                         _out_presets.set_text (tmp);
492                 }
493         }
494 }
495
496 uint32_t
497 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
498 {
499         std::string lbl;
500         std::string tip = p->name ();
501         std::vector<std::string> cns;
502         p->get_connections (cns);
503
504         // TODO proper labels, see MixerStrip::update_io_button()
505         if (cns.size () == 0) {
506                 lbl = "-";
507         } else if (cns.size () > 1) {
508                 lbl = "...";
509                 tip += " &lt;- ";
510         } else {
511                 string lpn (PROGRAM_NAME);
512                 boost::to_lower (lpn);
513                 std::string program_port_prefix = lpn + ":"; // e.g. "ardour:"
514
515                 lbl = cns[0];
516                 tip += " &lt;- ";
517                 if (lbl.find ("system:capture_") == 0) {
518                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
519                         if (lbl.empty ()) {
520                                 lbl = cns[0].substr (15);
521                         }
522                 } else if (lbl.find ("system:midi_capture_") == 0) {
523                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
524                         if (lbl.empty ()) {
525                                 // "system:midi_capture_123" -> "123"
526                                 lbl = "M " + cns[0].substr (20);
527                         }
528                 } else if (lbl.find (program_port_prefix) == 0) {
529                         lbl = lbl.substr (program_port_prefix.size ());
530                 }
531         }
532         for (std::vector<std::string>::const_iterator i = cns.begin (); i != cns.end (); ++i) {
533                 tip += *i;
534                 tip += " ";
535         }
536         replace_all (lbl, "_", " ");
537
538         ArdourButton *pb = manage (new ArdourButton (lbl));
539         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
540         pb->set_layout_ellipsize_width (108 * PANGO_SCALE);
541         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
542         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
543
544         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
545         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
546
547         pb = manage (new ArdourButton ("-"));
548         _sidechain_tbl->attach (*pb, 1, 2, r, r + 1, FILL, SHRINK);
549         if (can_remove) {
550                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
551         } else {
552                 pb->set_sensitive (false);
553         }
554
555         uint32_t rv = 1;
556
557         if (cns.size () == 1 && _session) {
558                 /* check if it's an Ardour Send feeding.. */
559                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
560                 for (ARDOUR::RouteList::const_iterator i = routes->begin (); i != routes->end (); ++i) {
561                         uint32_t nth = 0;
562                         boost::shared_ptr<Processor> proc;
563                         /* nth_send () takes a processor read-lock */
564                         while ((proc = (*i)->nth_send (nth))) {
565                                 boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
566                                 if (!send || !send->output ()) {
567                                         ++nth;
568                                         continue;
569                                 }
570                                 if (!send->output ()->connected_to (p->name ())) {
571                                         ++nth;
572                                         continue;
573                                 }
574                                 /* if processor goes away, we're notified by the port disconnect,
575                                  * there should be no need to explicily connect to proc->DropReferences
576                                  */
577                                 set<Evoral::Parameter> p = proc->what_can_be_automated ();
578                                 for (set<Evoral::Parameter>::iterator i = p.begin (); i != p.end (); ++i) {
579                                         Control* c = new Control (proc->automation_control (*i), _("Send"));
580                                         _controls.push_back (c);
581                                         ++r; ++rv;
582                                         _sidechain_tbl->attach (c->box, 0, 2, r, r + 1, EXPAND|FILL, SHRINK);
583                                 }
584                                 break;
585                         }
586                 }
587         }
588         return rv;
589 }
590
591 void
592 PluginPinDialog::update_element_pos ()
593 {
594         /* layout sizes */
595         _innerwidth = _width - 2. * _margin_x;
596
597         const double yc   = rint (_height * .5);
598         const double bxh2 = rint (_margin_y * .45); // TODO grow?
599         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
600         const double bxw2 = rint (bxw * .5);
601         const double y_in = _margin_y;
602         const double y_out = _height - _margin_y;
603
604         _bxw2 = bxw2;
605         _bxh2 = bxh2;
606
607         const double dx = _pin_box_size * .5;
608
609         uint32_t sc_cnt = 0;
610         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
611                 switch (i->e->ct) {
612                         case Input:
613                                 if (i->e->sc) {
614                                         i->x = _innerwidth + _margin_x - dx;
615                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
616                                         i->h = _pin_box_size;
617                                         i->w = 1.5 * _pin_box_size;
618                                         ++ sc_cnt;
619                                 } else {
620                                         uint32_t idx = i->e->id;
621                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
622                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
623                                         i->w = _pin_box_size;
624                                         i->h = 1.5 * _pin_box_size;
625                                         i->y = y_in - i->h;
626                                 }
627                                 break;
628                         case Output:
629                                 {
630                                         uint32_t idx = i->e->id;
631                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
632                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
633                                         i->y = y_out;
634                                         i->w = _pin_box_size;
635                                         i->h = 1.5 * _pin_box_size;
636                                 }
637                                 break;
638                         case Sink:
639                                 {
640                                         uint32_t idx = i->e->id;
641                                         if (i->e->dt == DataType::AUDIO) { idx += _sinks.n_midi (); }
642                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
643                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
644                                         i->y = yc - bxh2 - dx;
645                                         i->w = _pin_box_size;
646                                         i->h = _pin_box_size;
647                                 }
648                                 break;
649                         case Source:
650                                 {
651                                         uint32_t idx = i->e->id;
652                                         if (i->e->dt == DataType::AUDIO) { idx += _sources.n_midi (); }
653                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
654                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
655                                         i->y = yc + bxh2 - dx;
656                                         i->w = _pin_box_size;
657                                         i->h = _pin_box_size;
658                                 }
659                                 break;
660                 }
661         }
662 }
663
664 void
665 PluginPinDialog::set_color (cairo_t* cr, bool midi)
666 {
667         // see also gtk2_ardour/processor_box.cc
668         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
669         static const uint32_t midi_port_color = 0x960909FF; //Red
670
671         if (midi) {
672                 cairo_set_source_rgb (cr,
673                                 UINT_RGBA_R_FLT (midi_port_color),
674                                 UINT_RGBA_G_FLT (midi_port_color),
675                                 UINT_RGBA_B_FLT (midi_port_color));
676         } else {
677                 cairo_set_source_rgb (cr,
678                                 UINT_RGBA_R_FLT (audio_port_color),
679                                 UINT_RGBA_G_FLT (audio_port_color),
680                                 UINT_RGBA_B_FLT (audio_port_color));
681         }
682 }
683
684 void
685 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
686 {
687         if (w.e->sc) {
688                 const double dy = w.h * .5;
689                 const double dx = w.w - dy;
690                 cairo_move_to (cr, w.x, w.y + dy);
691                 cairo_rel_line_to (cr,  dy, -dy);
692                 cairo_rel_line_to (cr,  dx,  0);
693                 cairo_rel_line_to (cr,   0,  w.h);
694                 cairo_rel_line_to (cr, -dx,  0);
695         } else {
696                 const double dir = (w.e->ct == Input) ? 1 : -1;
697                 const double dx = w.w * .5;
698                 const double dy = w.h - dx;
699
700                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
701                 cairo_rel_line_to (cr,     -dx, -dx * dir);
702                 cairo_rel_line_to (cr,      0., -dy * dir);
703                 cairo_rel_line_to (cr, 2. * dx,        0.);
704                 cairo_rel_line_to (cr,      0.,  dy * dir);
705         }
706         cairo_close_path  (cr);
707
708         cairo_set_line_width (cr, 1.0);
709         cairo_set_source_rgb (cr, 0, 0, 0);
710         cairo_stroke_preserve (cr);
711
712         set_color (cr, w.e->dt == DataType::MIDI);
713
714         if (w.e->sc) {
715                 assert (w.e->ct == Input);
716                 cairo_fill_preserve (cr);
717                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
718         }
719
720         if (w.e == _selection || w.e == _actor) {
721                 cairo_fill_preserve (cr);
722                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
723         } else if (w.prelight) {
724                 cairo_fill_preserve (cr);
725                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
726         }
727         cairo_fill (cr);
728 }
729
730 void
731 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
732 {
733         const double dx = w.w * .5;
734         const double dy = w.h * .5;
735
736         cairo_move_to (cr, w.x + dx, w.y);
737         cairo_rel_line_to (cr, -dx,  dy);
738         cairo_rel_line_to (cr,  dx,  dy);
739         cairo_rel_line_to (cr,  dx, -dy);
740         cairo_close_path  (cr);
741
742         cairo_set_line_width (cr, 1.0);
743         cairo_set_source_rgb (cr, 0, 0, 0);
744         cairo_stroke_preserve (cr);
745
746         set_color (cr, w.e->dt == DataType::MIDI);
747
748         if (w.e->sc) {
749                 assert (w.e->ct == Sink);
750                 cairo_fill_preserve (cr);
751                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
752         }
753
754         if (w.e == _selection || w.e == _actor) {
755                 cairo_fill_preserve (cr);
756                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
757         } else if (w.prelight) {
758                 cairo_fill_preserve (cr);
759                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
760         }
761         cairo_fill (cr);
762
763         if ((w.prelight || w.e == _selection) && !w.name.empty ()) {
764                 int text_width;
765                 int text_height;
766                 Glib::RefPtr<Pango::Layout> layout;
767                 layout = Pango::Layout::create (get_pango_context ());
768                 layout->set_text (w.name);
769                 layout->get_pixel_size (text_width, text_height);
770
771                 rounded_rectangle (cr, w.x + dx - .5 * text_width - 2, w.y - text_height - 2,  text_width + 4, text_height + 2, 7);
772                 cairo_set_source_rgba (cr, 0, 0, 0, .5);
773                 cairo_fill (cr);
774
775                 cairo_move_to (cr, w.x + dx - .5 * text_width, w.y - text_height - 1);
776                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
777                 pango_cairo_show_layout (cr, layout->gobj ());
778         }
779 }
780
781 double
782 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
783 {
784         if (!midi) { i += n_midi; }
785         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
786 }
787
788 const PluginPinDialog::CtrlWidget&
789 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
790 {
791         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
792                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
793                         return *i;
794                 }
795         }
796         assert (0);
797         fatal << string_compose (_("programming error: %1"),
798                         X_("Invalid Plugin I/O Port."))
799                 << endmsg;
800         abort (); /*NOTREACHED*/
801         static CtrlWidget screw_old_compilers ("", Input, DataType::NIL, 0);
802         return screw_old_compilers;
803 }
804
805 void
806 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
807 {
808         switch (w.e->ct) {
809                 case Input:
810                         if (w.e->sc) {
811                                 x = w.x;
812                                 y = w.y + w.h * .5;
813                         } else {
814                                 x = w.x + w.w * .5;
815                                 y = w.y + w.h;
816                         }
817                         break;
818                 case Output:
819                         x = w.x + w.w * .5;
820                         y = w.y;
821                         break;
822                 case Sink:
823                         x = w.x + w.w * .5;
824                         y = w.y;
825                         break;
826                 case Source:
827                         x = w.x + w.w * .5;
828                         y = w.y + w.h;
829                         break;
830         }
831 }
832
833 void
834 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
835 {
836         const double bz = 2 * _pin_box_size;
837         double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
838         if (x0 > _width * .5) { bc *= -1; }
839
840         cairo_move_to (cr, x0, y0);
841         if (horiz) {
842                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
843         } else {
844                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
845         }
846         cairo_set_line_width (cr, 3.0);
847         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
848         cairo_set_source_rgb (cr, 1, 0, 0);
849         if (dashed) {
850                 const double dashes[] = { 5, 7 };
851                 cairo_set_dash (cr, dashes, 2, 0);
852         }
853         set_color (cr, midi);
854         cairo_stroke (cr);
855         if (dashed) {
856                 cairo_set_dash (cr, 0, 0, 0);
857         }
858 }
859
860 void
861 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
862 {
863         double x0, x1, y0, y1;
864         edge_coordinates (w0, x0, y0);
865         edge_coordinates (w1, x1, y1);
866         assert (w0.e->dt == w1.e->dt);
867         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
868 }
869
870
871 bool
872 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
873 {
874         Gtk::Allocation a = darea.get_allocation ();
875         double const width = a.get_width ();
876         double const height = a.get_height ();
877
878         if (!_position_valid) {
879                 _width = width;
880                 _height = height;
881                 update_element_pos ();
882                 _position_valid = true;
883         }
884
885         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
886         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
887         cairo_clip (cr);
888
889         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
890         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
891         cairo_rectangle (cr, 0, 0, width, height);
892         cairo_fill (cr);
893
894         const double yc = rint (_height * .5);
895
896         /* processor box */
897         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
898         cairo_set_line_width (cr, 1.0);
899         cairo_set_source_rgb (cr, .1, .1, .3);
900         cairo_stroke_preserve (cr);
901         cairo_set_source_rgb (cr, .3, .3, .3);
902         cairo_fill (cr);
903
904         /* labels */
905         Glib::RefPtr<Pango::Layout> layout;
906         layout = Pango::Layout::create (get_pango_context ());
907
908         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
909         layout->set_width (_height * PANGO_SCALE);
910
911         int text_width;
912         int text_height;
913
914         layout->set_text (_route ()->name ());
915         layout->get_pixel_size (text_width, text_height);
916         cairo_save (cr);
917         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
918         cairo_rotate (cr, M_PI * -.5);
919         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
920         pango_cairo_show_layout (cr, layout->gobj ());
921         cairo_new_path (cr);
922         cairo_restore (cr);
923
924         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
925         layout->set_text (_pi->name ());
926         layout->get_pixel_size (text_width, text_height);
927         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
928         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
929         pango_cairo_show_layout (cr, layout->gobj ());
930
931         if (_pi->signal_latency () > 0) {
932                 // TODO: this needs a better location also format to msec (and cache)
933                 layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
934                 layout->set_text (string_compose (_("Latency %1 spl"), _pi->signal_latency ()));
935                 layout->get_pixel_size (text_width, text_height);
936                 cairo_move_to (cr, _margin_x + _pin_box_size * .5, _margin_y + 2);
937                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
938                 pango_cairo_show_layout (cr, layout->gobj ());
939         }
940
941         if (_pi->strict_io () && !Profile->get_mixbus ()) {
942                 layout->set_text (_("Strict I/O"));
943                 layout->get_pixel_size (text_width, text_height);
944                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
945                 const double sy0 = _height - 3 - text_height;
946
947                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
948                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
949                 cairo_fill (cr);
950
951                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
952                 cairo_move_to (cr, sx0, sy0);
953                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
954                 pango_cairo_show_layout (cr, layout->gobj ());
955         }
956
957         /* draw midi-bypass (behind) */
958         if (_pi->has_midi_bypass ()) {
959                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
960                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
961                 draw_connection (cr, cw0, cw1, true);
962         }
963
964         /* thru connections */
965         const ChanMapping::Mappings thru_map = _pi->thru_map ().mappings ();
966         for (ChanMapping::Mappings::const_iterator t = thru_map.begin (); t != thru_map.end (); ++t) {
967                 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
968                         const CtrlWidget& cw0 = get_io_ctrl (Output, t->first, c->first);
969                         const CtrlWidget& cw1 = get_io_ctrl (Input, t->first, c->second);
970                         if (!(_dragging && cw1.e == _selection && cw0.e == _drag_dst)) {
971                                 draw_connection (cr, cw1, cw0, true);
972                         }
973                 }
974         }
975
976
977         /* plugins & connection wires */
978         for (uint32_t i = 0; i < _n_plugins; ++i) {
979                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
980
981                 /* plugin box */
982                 cairo_set_source_rgb (cr, .5, .5, .5);
983                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
984                 cairo_fill (cr);
985
986                 layout->set_width (1.9 * _bxw2 * PANGO_SCALE);
987                 layout->set_text (string_compose (_("Instance #%1"), i + 1));
988                 layout->get_pixel_size (text_width, text_height);
989                 cairo_move_to (cr, x0 - text_width * .5, yc - text_height * .5);
990                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
991                 pango_cairo_show_layout (cr, layout->gobj ());
992
993                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
994                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
995
996                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
997                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
998                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
999                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
1000                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1001                                         draw_connection (cr, cw0, cw1);
1002                                 }
1003                         }
1004                 }
1005
1006                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
1007                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1008                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
1009                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
1010                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1011                                         draw_connection (cr, cw0, cw1);
1012                                 }
1013                         }
1014                 }
1015         }
1016
1017         /* pins and ports */
1018         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
1019                 switch (i->e->ct) {
1020                         case Input:
1021                         case Output:
1022                                 draw_io_pin (cr, *i);
1023                                 break;
1024                         case Sink:
1025                         case Source:
1026                                 draw_plugin_pin (cr, *i);
1027                                 break;
1028                 }
1029         }
1030
1031         /* DnD wire */
1032         CtrlWidget *drag_src = NULL;
1033         if (_dragging) {
1034                 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1035                         if (i->e  == _selection ) {
1036                                 drag_src = &(*i);
1037                         }
1038                 }
1039         }
1040
1041         if (drag_src) {
1042                 double x0, y0;
1043                 if (_selection->ct == Input || _selection->ct == Source) {
1044                         edge_coordinates (*drag_src, x0, y0);
1045                         draw_connection (cr, x0, _drag_x, y0, _drag_y,
1046                                         _selection->dt == DataType::MIDI, _selection->sc);
1047                 } else {
1048                         edge_coordinates (*drag_src, x0, y0);
1049                         draw_connection (cr, _drag_x, x0, _drag_y, y0,
1050                                         _selection->dt == DataType::MIDI, _selection->sc);
1051                 }
1052         }
1053
1054         cairo_destroy (cr);
1055         return true;
1056 }
1057
1058 void
1059 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
1060 {
1061         req->width = _min_width;
1062         req->height = _min_height;
1063 }
1064
1065 void
1066 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
1067 {
1068         _position_valid = false;
1069 }
1070
1071 bool
1072 PluginPinDialog::drag_type_matches (const CtrlElem& e)
1073 {
1074         if (!_dragging || !_selection) {
1075                 return true;
1076         }
1077         if (_selection->dt != e->dt) {
1078                 return false;
1079         }
1080         if (_selection->ct == Input  && e->ct == Sink)   { return true; }
1081         if (_selection->ct == Sink   && e->ct == Input)  { return true; }
1082         if (_selection->ct == Output && e->ct == Source) { return true; }
1083         if (_selection->ct == Source && e->ct == Output) { return true; }
1084         if (_selection->ct == Input  && e->ct == Output) { return true; }
1085         if (_selection->ct == Output && e->ct == Input)  { return true; }
1086         return false;
1087 }
1088
1089 void
1090 PluginPinDialog::start_drag (const CtrlElem& e, double x, double y)
1091 {
1092         assert (_selection == e);
1093         _drag_dst.reset ();
1094         if (e->ct == Sink) {
1095                 bool valid;
1096                 const ChanMapping& map (_pi->input_map (e->ip));
1097                 uint32_t idx = map.get (e->dt, e->id, &valid);
1098                 if (valid) {
1099                         const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1100                         _drag_dst = e;
1101                         _selection = cw.e;
1102                 }
1103         }
1104         else if (e->ct == Output) {
1105                 for (uint32_t i = 0; i < _n_plugins; ++i) {
1106                         bool valid;
1107                         const ChanMapping& map (_pi->output_map (i));
1108                         uint32_t idx = map.get_src (e->dt, e->id, &valid);
1109                         if (valid) {
1110                                 const CtrlWidget& cw = get_io_ctrl (Source, e->dt, idx, i);
1111                                 _drag_dst = e;
1112                                 _selection = cw.e;
1113                                 break;
1114                         }
1115                 }
1116                 if (!_drag_dst) {
1117                         bool valid;
1118                         const ChanMapping& map (_pi->thru_map ());
1119                         uint32_t idx = map.get (e->dt, e->id, &valid);
1120                         if (valid) {
1121                                 const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1122                                 _drag_dst = e;
1123                                 _selection = cw.e;
1124                         }
1125                 }
1126         }
1127         _dragging = true;
1128         _drag_x = x;
1129         _drag_y = y;
1130 }
1131
1132 bool
1133 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
1134 {
1135         bool changed = false;
1136         _hover.reset ();
1137         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1138                 if (ev->x >= i->x && ev->x <= i->x + i->w
1139                                 && ev->y >= i->y && ev->y <= i->y + i->h
1140                                 && drag_type_matches (i->e))
1141                 {
1142                         if (!i->prelight) changed = true;
1143                         i->prelight = true;
1144                         _hover = i->e;
1145                 } else {
1146                         if (i->prelight) changed = true;
1147                         i->prelight = false;
1148                 }
1149         }
1150         if (_dragging) {
1151                 _drag_x = ev->x;
1152                 _drag_y = ev->y;
1153         }
1154         if (changed || _dragging) {
1155                 darea.queue_draw ();
1156         }
1157         return true;
1158 }
1159
1160 bool
1161 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
1162 {
1163         if (ev->type != GDK_BUTTON_PRESS) {
1164                 return false;
1165         }
1166
1167         switch (ev->button) {
1168                 case 1:
1169                         _drag_dst.reset ();
1170                         if (!_selection || (_selection && !_hover)) {
1171                                 _selection = _hover;
1172                                 _actor.reset ();
1173                                 if (_selection) {
1174                                         start_drag (_selection, ev->x, ev->y);
1175                                 } else {
1176                                         darea.queue_draw ();
1177                                 }
1178                         } else if (_selection && _hover && _selection != _hover) {
1179                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
1180                                 else if (_selection->ct == Input  && _hover->ct == Sink)   { _actor = _hover; }
1181                                 else if (_selection->ct == Sink   && _hover->ct == Input)  { _actor = _hover; }
1182                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
1183                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
1184                                 else if (_selection->ct == Input  && _hover->ct == Output) { _actor = _hover; }
1185                                 else if (_selection->ct == Output && _hover->ct == Input)  { _actor = _hover; }
1186                                 if (!_actor) {
1187                                         _selection = _hover;
1188                                         start_drag (_selection, ev->x, ev->y);
1189                                 } else {
1190                                         darea.queue_draw ();
1191                                 }
1192                         } else if (_hover) {
1193                                 _selection = _hover;
1194                                 _actor.reset ();
1195                                 start_drag (_selection, ev->x, ev->y);
1196                         }
1197                         break;
1198                 case 3:
1199                         _drag_dst.reset ();
1200                         if (_selection != _hover) {
1201                                 _selection = _hover;
1202                                 darea.queue_draw ();
1203                         }
1204                         _actor.reset ();
1205                         break;
1206                 default:
1207                         break;
1208         }
1209
1210         return true;
1211 }
1212
1213 bool
1214 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
1215 {
1216         if (_dragging && _selection && _drag_dst && _drag_dst == _hover) {
1217                 // select click. (or re-connect same)
1218                 assert (_selection != _hover);
1219                 _actor.reset ();
1220                 _dragging = false;
1221                 _drag_dst.reset ();
1222                 _selection =_hover;
1223                 darea.queue_draw ();
1224                 return true;
1225         }
1226
1227         if (_dragging && _hover && _hover != _selection) {
1228                 _actor = _hover;
1229         }
1230
1231         if (_hover == _actor && _actor && ev->button == 1) {
1232                 assert (_selection);
1233                 assert (_selection->dt == _actor->dt);
1234                 if (_drag_dst) {
1235                         assert (_dragging && _selection != _drag_dst);
1236                         handle_disconnect (_drag_dst, true);
1237                 }
1238                 if      (_selection->ct == Input && _actor->ct == Sink) {
1239                         handle_input_action (_actor, _selection);
1240                 }
1241                 else if (_selection->ct == Sink && _actor->ct == Input) {
1242                         handle_input_action (_selection, _actor);
1243                 }
1244                 else if (_selection->ct == Output && _actor->ct == Source) {
1245                         handle_output_action (_actor, _selection);
1246                 }
1247                 else if (_selection->ct == Source && _actor->ct == Output) {
1248                         handle_output_action (_selection, _actor);
1249                 }
1250                 else if (_selection->ct == Input && _actor->ct == Output) {
1251                         handle_thru_action (_actor, _selection);
1252                 }
1253                 else if (_selection->ct == Output && _actor->ct == Input) {
1254                         handle_thru_action (_selection, _actor);
1255                 }
1256                 _selection.reset ();
1257         } else if (_hover == _selection && _selection && ev->button == 3) {
1258                 handle_disconnect (_selection);
1259         } else if (!_hover && ev->button == 3) {
1260                 reset_menu.popup (1, ev->time);
1261         }
1262
1263         if (_dragging && _hover != _selection) {
1264                 _selection.reset ();
1265         }
1266         _actor.reset ();
1267         _dragging = false;
1268         _drag_dst.reset ();
1269         darea.queue_draw ();
1270         return true;
1271 }
1272
1273 void
1274 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
1275 {
1276         const int pc = s->ip;
1277         bool valid;
1278         ChanMapping in_map (_pi->input_map (pc));
1279         uint32_t idx = in_map.get (s->dt, s->id, &valid);
1280
1281         if (valid && idx == i->id) {
1282                 // disconnect
1283                 if (!_dragging) {
1284                         in_map.unset (s->dt, s->id);
1285                         _pi->set_input_map (pc, in_map);
1286                 } else {
1287                         plugin_reconfigured ();
1288                 }
1289         }
1290         else if (!valid) {
1291                 // connect
1292                 in_map.set (s->dt, s->id, i->id);
1293                 _pi->set_input_map (pc, in_map);
1294         }
1295         else {
1296                 // reconnect
1297                 in_map.unset (s->dt, s->id);
1298                 in_map.set (s->dt, s->id, i->id);
1299                 _pi->set_input_map (pc, in_map);
1300         }
1301 }
1302
1303 void
1304 PluginPinDialog::disconnect_other_outputs (uint32_t skip_pc, DataType dt, uint32_t id)
1305 {
1306         _ignore_updates = true;
1307         for (uint32_t n = 0; n < _n_plugins; ++n) {
1308                 if (n == skip_pc) {
1309                         continue;
1310                 }
1311                 bool valid;
1312                 ChanMapping n_out_map (_pi->output_map (n));
1313                 uint32_t idx = n_out_map.get_src (dt, id, &valid);
1314                 if (valid) {
1315                         n_out_map.unset (dt, idx);
1316                         _pi->set_output_map (n, n_out_map);
1317                 }
1318         }
1319         _ignore_updates = false;
1320 }
1321
1322 void
1323 PluginPinDialog::disconnect_other_thru (DataType dt, uint32_t id)
1324 {
1325         _ignore_updates = true;
1326         bool valid;
1327         ChanMapping n_thru_map (_pi->thru_map ());
1328         n_thru_map.get (dt, id, &valid);
1329         if (valid) {
1330                 n_thru_map.unset (dt, id);
1331                 _pi->set_thru_map (n_thru_map);
1332         }
1333         _ignore_updates = false;
1334 }
1335
1336 void
1337 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
1338 {
1339         const uint32_t pc = s->ip;
1340         bool valid;
1341         ChanMapping out_map (_pi->output_map (pc));
1342         uint32_t idx = out_map.get (s->dt, s->id, &valid);
1343
1344         if (valid && idx == o->id) {
1345                 // disconnect
1346                 if (!_dragging) {
1347                         out_map.unset (s->dt, s->id);
1348                         _pi->set_output_map (pc, out_map);
1349                 } else {
1350                         plugin_reconfigured ();
1351                 }
1352         }
1353         else {
1354                 // disconnect source
1355                 disconnect_other_outputs (pc, s->dt, o->id);
1356                 disconnect_other_thru (s->dt, o->id);
1357                 out_map = _pi->output_map (pc); // re-read map
1358                 if (valid) {
1359                         out_map.unset (s->dt, s->id);
1360                 }
1361                 idx = out_map.get_src (s->dt, o->id, &valid);
1362                 if (valid) {
1363                         out_map.unset (s->dt, idx);
1364                 }
1365                 // connect
1366                 out_map.set (s->dt, s->id, o->id);
1367                 _pi->set_output_map (pc, out_map);
1368         }
1369 }
1370
1371 void
1372 PluginPinDialog::handle_thru_action (const CtrlElem &o, const CtrlElem &i)
1373 {
1374         bool valid;
1375         ChanMapping thru_map (_pi->thru_map ());
1376         uint32_t idx = thru_map.get (o->dt, o->id, &valid);
1377
1378         if (valid && idx == i->id) {
1379                 if (!_dragging) {
1380                         thru_map.unset (o->dt, o->id);
1381                 }
1382         } else {
1383                 // disconnect other outputs first
1384                 disconnect_other_outputs (UINT32_MAX, o->dt, o->id);
1385                 disconnect_other_thru (o->dt, o->id);
1386                 thru_map = _pi->thru_map (); // re-read map
1387
1388                 thru_map.set (o->dt, o->id, i->id);
1389         }
1390         _pi->set_thru_map (thru_map);
1391 }
1392
1393 bool
1394 PluginPinDialog::handle_disconnect (const CtrlElem &e, bool no_signal)
1395 {
1396         _ignore_updates = true;
1397         bool changed = false;
1398         bool valid;
1399
1400         switch (e->ct) {
1401                 case Input:
1402                         {
1403                                 ChanMapping n_thru_map (_pi->thru_map ());
1404                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1405                                         uint32_t idx = n_thru_map.get (e->dt, i, &valid);
1406                                         if (valid && idx == e->id) {
1407                                                 n_thru_map.unset (e->dt, i);
1408                                                 changed = true;
1409                                         }
1410                                 }
1411                                 if (changed) {
1412                                         _pi->set_thru_map (n_thru_map);
1413                                 }
1414                         }
1415                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1416                                 ChanMapping map (_pi->input_map (n));
1417                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
1418                                         uint32_t idx = map.get (e->dt, i, &valid);
1419                                         if (valid && idx == e->id) {
1420                                                 map.unset (e->dt, i);
1421                                                 changed = true;
1422                                         }
1423                                 }
1424                                 _pi->set_input_map (n, map);
1425                         }
1426                         break;
1427                 case Sink:
1428                         {
1429                                 ChanMapping map (_pi->input_map (e->ip));
1430                                 map.get (e->dt, e->id, &valid);
1431                                 if (valid) {
1432                                         map.unset (e->dt, e->id);
1433                                         _pi->set_input_map (e->ip, map);
1434                                         changed = true;
1435                                 }
1436                         }
1437                         break;
1438                 case Source:
1439                         {
1440                                 ChanMapping map (_pi->output_map (e->ip));
1441                                 map.get (e->dt, e->id, &valid);
1442                                 if (valid) {
1443                                         map.unset (e->dt, e->id);
1444                                         _pi->set_output_map (e->ip, map);
1445                                         changed = true;
1446                                 }
1447                         }
1448                         break;
1449                 case Output:
1450                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1451                                 ChanMapping map (_pi->output_map (n));
1452                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1453                                         uint32_t idx = map.get (e->dt, i, &valid);
1454                                         if (valid && idx == e->id) {
1455                                                 map.unset (e->dt, i);
1456                                                 changed = true;
1457                                         }
1458                                 }
1459                                 if (changed) {
1460                                         _pi->set_output_map (n, map);
1461                                 }
1462                         }
1463                         {
1464                                 ChanMapping n_thru_map (_pi->thru_map ());
1465                                 n_thru_map.get (e->dt, e->id, &valid);
1466                                 if (valid) {
1467                                         n_thru_map.unset (e->dt, e->id);
1468                                         changed = true;
1469                                         _pi->set_thru_map (n_thru_map);
1470                                 }
1471                         }
1472                         break;
1473         }
1474         _ignore_updates = false;
1475         if (changed && !no_signal) {
1476                 plugin_reconfigured ();
1477         }
1478         return changed;
1479 }
1480
1481 void
1482 PluginPinDialog::toggle_sidechain ()
1483 {
1484         if (_session && _session->actively_recording ()) { return; }
1485         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1486 }
1487
1488 void
1489 PluginPinDialog::connect_sidechain ()
1490 {
1491         if (!_session) { return; }
1492
1493         if (_sidechain_selector == 0) {
1494                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1495         }
1496
1497         if (_sidechain_selector->is_visible ()) {
1498                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1499         } else {
1500                 _sidechain_selector->present ();
1501         }
1502 }
1503
1504 void
1505 PluginPinDialog::reset_configuration ()
1506 {
1507         if (_set_config.get_active ()) {
1508                 _route ()->reset_plugin_insert (_pi);
1509         } else {
1510                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1511         }
1512 }
1513
1514 void
1515 PluginPinDialog::reset_mapping ()
1516 {
1517         _pi->reset_map ();
1518 }
1519
1520 void
1521 PluginPinDialog::select_output_preset (uint32_t n_audio)
1522 {
1523         if (_session && _session->actively_recording ()) { return; }
1524         ChanCount out (DataType::AUDIO, n_audio);
1525         _route ()->plugin_preset_output (_pi, out);
1526 }
1527
1528 void
1529 PluginPinDialog::add_remove_plugin_clicked (bool add)
1530 {
1531         if (_session && _session->actively_recording ()) { return; }
1532         ChanCount out = _out;
1533         assert (add || _n_plugins > 0);
1534         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1535 }
1536
1537 void
1538 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1539 {
1540         if (_session && _session->actively_recording ()) { return; }
1541         ChanCount out = _out;
1542         assert (add || out.get (dt) > 0);
1543         out.set (dt, out.get (dt) + (add ? 1 : -1));
1544         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1545 }
1546
1547 void
1548 PluginPinDialog::add_sidechain_port (DataType dt)
1549 {
1550         if (_session && _session->actively_recording ()) { return; }
1551         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1552         if (!io) {
1553                 return;
1554         }
1555
1556         // this triggers a PluginIoReConfigure with process and processor write lock held
1557         // from /this/ thread.
1558         io->add_port ("", this, dt);
1559 }
1560
1561 void
1562 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1563 {
1564         if (_session && _session->actively_recording ()) { return; }
1565         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1566         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1567         if (!io || !p) {
1568                 return;
1569         }
1570         io->remove_port (p, this);
1571 }
1572
1573 void
1574 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1575 {
1576         if (_session && _session->actively_recording ()) { return; }
1577         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1578         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1579         if (!io || !p) {
1580                 return;
1581         }
1582         p->disconnect_all ();
1583 }
1584
1585 void
1586 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1587 {
1588         if (_session && _session->actively_recording ()) { return; }
1589         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1590         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1591         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1592         if (!io || !p0 || !p1) {
1593                 return;
1594         }
1595         _ignore_updates = true;
1596         p0->disconnect_all ();
1597         _ignore_updates = false;
1598         p0->connect (p1->name ());
1599 }
1600
1601 bool
1602 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1603 {
1604         if (_session && _session->actively_recording ()) { return false; }
1605         if (ev->button == 3) {
1606                 connect_sidechain ();
1607         }
1608         return false;
1609 }
1610
1611 struct RouteCompareByName {
1612         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1613                 return a->name ().compare (b->name ()) < 0;
1614         }
1615 };
1616
1617 bool
1618 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1619 {
1620         using namespace Menu_Helpers;
1621         if (!_session || _session->actively_recording ()) { return false; }
1622         if (!_session->engine ().connected ()) { return false; }
1623
1624         if (ev->button == 1) {
1625                 MenuList& citems = input_menu.items ();
1626                 input_menu.set_name ("ArdourContextMenu");
1627                 citems.clear ();
1628
1629                 boost::shared_ptr<Port> p = wp.lock ();
1630                 if (p && p->connected ()) {
1631                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1632                         citems.push_back (SeparatorElem ());
1633                 }
1634
1635 #if 0
1636                 // TODO add system inputs, too ?!
1637                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1638                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1639                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1640                         }
1641                         //maybe_add_bundle_to_input_menu (*i, current);
1642                 }
1643 #endif
1644
1645                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1646                 RouteList copy = *routes;
1647                 copy.sort (RouteCompareByName ());
1648                 uint32_t added = 0;
1649                 for (ARDOUR::RouteList::const_iterator i = copy.begin (); i != copy.end (); ++i) {
1650                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1651                 }
1652
1653                 if (added > 0) {
1654                         citems.push_back (SeparatorElem ());
1655                 }
1656                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1657                 input_menu.popup (1, ev->time);
1658         }
1659         return false;
1660 }
1661
1662 uint32_t
1663 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1664 {
1665         uint32_t added = 0;
1666         using namespace Menu_Helpers;
1667         if (r->output () == _route ()->output ()) {
1668                 return added;
1669         }
1670
1671         if (_route ()->feeds_according_to_graph (r)) {
1672                 return added;
1673         }
1674
1675         MenuList& citems = input_menu.items ();
1676         const IOVector& iov (r->all_outputs ());
1677
1678         for (IOVector::const_iterator o = iov.begin (); o != iov.end (); ++o) {
1679                 boost::shared_ptr<IO> op = o->lock ();
1680                 if (!op) {
1681                         continue;
1682                 }
1683                 PortSet& p (op->ports ());
1684                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1685                         std::string n = i->name ();
1686                         replace_all (n, "_", " ");
1687                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1688                         ++added;
1689                 }
1690         }
1691         return added;
1692 }
1693
1694 void
1695 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1696 {
1697         boost::shared_ptr<Port> p0 = w0.lock ();
1698         boost::shared_ptr<Port> p1 = w1.lock ();
1699
1700         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1701         if (!io) { return; }
1702
1703         if (p0 && io->has_port (p0)) {
1704                 queue_idle_update ();
1705         }
1706         else if (p1 && io->has_port (p1)) {
1707                 queue_idle_update ();
1708         }
1709 }
1710
1711 /* lifted from ProcessorEntry::Control */
1712 PluginPinDialog::Control::Control (boost::shared_ptr<AutomationControl> c, string const & n)
1713         : _control (c)
1714         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain ()), 0, 1, 0.01, 0.1)
1715         , _slider (&_adjustment, boost::shared_ptr<PBD::Controllable> (), 0, max (13.f, rintf (13.f * UIConfiguration::instance ().get_ui_scale ())))
1716         , _slider_persistant_tooltip (&_slider)
1717         , _ignore_ui_adjustment (false)
1718         , _name (n)
1719 {
1720         _slider.set_controllable (c);
1721         box.set_padding (0, 0, 4, 4);
1722
1723         _slider.set_name ("ProcessorControlSlider");
1724         _slider.set_text (_name);
1725
1726         box.add (_slider);
1727         _slider.show ();
1728
1729         const ARDOUR::ParameterDescriptor& desc = c->desc ();
1730         double const lo = c->internal_to_interface (desc.lower);
1731         double const up = c->internal_to_interface (desc.upper);
1732         double const normal = c->internal_to_interface (desc.normal);
1733         double smallstep = desc.smallstep;
1734         double largestep = desc.largestep;
1735
1736         if (smallstep == 0.0) {
1737                 smallstep = up / 1000.;
1738         } else {
1739                 smallstep = c->internal_to_interface (desc.lower + smallstep);
1740         }
1741
1742         if (largestep == 0.0) {
1743                 largestep = up / 40.;
1744         } else {
1745                 largestep = c->internal_to_interface (desc.lower + largestep);
1746         }
1747
1748         _adjustment.set_lower (lo);
1749         _adjustment.set_upper (up);
1750         _adjustment.set_step_increment (smallstep);
1751         _adjustment.set_page_increment (largestep);
1752         _slider.set_default_value (normal);
1753
1754         _adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
1755         // dup. currently timers are used :(
1756         //c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
1757
1758         // yuck, do we really need to do this?
1759         // according to c404374 this is only needed for send automation
1760         timer_connection = Timers::rapid_connect (sigc::mem_fun (*this, &Control::control_changed));
1761
1762         control_changed ();
1763         set_tooltip ();
1764
1765         /* We're providing our own PersistentTooltip */
1766         set_no_tooltip_whatsoever (_slider);
1767 }
1768
1769 PluginPinDialog::Control::~Control ()
1770 {
1771         timer_connection.disconnect ();
1772 }
1773
1774 void
1775 PluginPinDialog::Control::set_tooltip ()
1776 {
1777         boost::shared_ptr<AutomationControl> c = _control.lock ();
1778         if (!c) {
1779                 return;
1780         }
1781         char tmp[256];
1782         snprintf (tmp, sizeof (tmp), "%s: %.2f", _name.c_str (), c->internal_to_user (c->get_value ()));
1783
1784         string sm = Gtkmm2ext::markup_escape_text (tmp);
1785         _slider_persistant_tooltip.set_tip (sm);
1786 }
1787
1788 void
1789 PluginPinDialog::Control::slider_adjusted ()
1790 {
1791         if (_ignore_ui_adjustment) {
1792                 return;
1793         }
1794         boost::shared_ptr<AutomationControl> c = _control.lock ();
1795         if (!c) {
1796                 return;
1797         }
1798         c->set_value ( c->interface_to_internal (_adjustment.get_value ()) , Controllable::NoGroup);
1799         set_tooltip ();
1800 }
1801
1802
1803 void
1804 PluginPinDialog::Control::control_changed ()
1805 {
1806         boost::shared_ptr<AutomationControl> c = _control.lock ();
1807         if (!c) {
1808                 return;
1809         }
1810
1811         _ignore_ui_adjustment = true;
1812
1813         // as long as rapid timers are used, only update the tooltip
1814         // if the value has changed.
1815         const double nval = c->internal_to_interface (c->get_value ());
1816         if (_adjustment.get_value () != nval) {
1817                 _adjustment.set_value (nval);
1818                 set_tooltip ();
1819         }
1820
1821         _ignore_ui_adjustment = false;
1822 }