add LED to sidechain toggle for consistency
[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 <gtkmm/table.h>
21 #include <gtkmm/box.h>
22 #include <gtkmm/label.h>
23
24 #include "pbd/replace_all.h"
25
26 #include "gtkmm2ext/utils.h"
27 #include "gtkmm2ext/rgb_macros.h"
28
29 #include "ardour/audioengine.h"
30 #include "ardour/plugin.h"
31 #include "ardour/port.h"
32 #include "ardour/session.h"
33
34 #include "plugin_pin_dialog.h"
35 #include "gui_thread.h"
36 #include "tooltips.h"
37 #include "ui_config.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46
47 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
48         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
49         , _set_config (_("Custom"), ArdourButton::led_default_elements)
50         , _rst_mapping (_("Reset"))
51         , _tgl_sidechain (_("Side Chain"), ArdourButton::led_default_elements)
52         , _add_plugin (_("+"))
53         , _del_plugin (_("-"))
54         , _add_output_audio (_("+"))
55         , _del_output_audio (_("-"))
56         , _add_output_midi (_("+"))
57         , _del_output_midi (_("-"))
58         , _add_sc_audio (_("A+"))
59         , _add_sc_midi (_("M+"))
60         , _pi (pi)
61         , _pin_box_size (10)
62         , _width (0)
63         , _height (0)
64         , _innerwidth (0)
65         , _margin_x (28)
66         , _margin_y (40)
67         , _min_width (300)
68         , _min_height (200)
69         , _n_inputs (0)
70         , _n_sidechains (0)
71         , _position_valid (false)
72         , _ignore_updates (false)
73         , _sidechain_selector (0)
74 {
75         assert (pi->owner ()); // Route
76
77         _pi->PluginIoReConfigure.connect (
78                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
79                         );
80
81         _pi->PluginMapChanged.connect (
82                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
83                         );
84
85         _pi->PluginConfigChanged.connect (
86                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
87                         );
88
89         _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
90         _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
91         _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
92
93         _tgl_sidechain.set_name ("pinrouting sidechain");
94         _set_config.set_name ("pinrouting custom");
95
96         _pm_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
97         _add_plugin.set_tweaks (ArdourButton::Square);
98         _del_plugin.set_tweaks (ArdourButton::Square);
99         _pm_size_group->add_widget (_add_plugin);
100         _pm_size_group->add_widget (_del_plugin);
101         _pm_size_group->add_widget (_add_output_audio);
102         _pm_size_group->add_widget (_del_output_audio);
103         _pm_size_group->add_widget (_add_output_midi);
104         _pm_size_group->add_widget (_del_output_midi);
105
106         _sc_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
107         _sc_size_group->add_widget (_add_sc_audio);
108         _sc_size_group->add_widget (_add_sc_midi);
109
110         Label* l;
111         Gtk::Separator *sep;
112         int r = 0;
113         Table* tl = manage (new Table (9, 2));
114         tl->set_border_width (0);
115         tl->set_spacings (2);
116
117         Table* tr = manage (new Table (4, 3));
118         tr->set_border_width (0);
119         tr->set_spacings (2);
120
121         /* left side table */
122         l = manage (new Label (_("<b>Config</b>"), ALIGN_CENTER));
123         l->set_use_markup ();
124         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
125         ++r;
126         tl->attach (_set_config, 0, 2, r, r + 1, FILL, SHRINK);
127         ++r;
128
129         sep = manage (new HSeparator ());
130         tl->attach (*sep, 0, 2, r, r + 1, FILL|EXPAND, FILL|EXPAND, 0, 4);
131         ++r;
132
133         l = manage (new Label (_("Instances"), ALIGN_CENTER));
134         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
135         ++r;
136         tl->attach (_add_plugin, 0, 1, r, r + 1, SHRINK, SHRINK);
137         tl->attach (_del_plugin, 1, 2, r, r + 1, SHRINK, SHRINK);
138         ++r;
139
140         l = manage (new Label (_("Audio Out"), ALIGN_CENTER));
141         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
142         ++r;
143         tl->attach (_add_output_audio, 0, 1, r, r + 1, SHRINK, SHRINK);
144         tl->attach (_del_output_audio, 1, 2, r, r + 1, SHRINK, SHRINK);
145         ++r;
146
147         l = manage (new Label (_("Midi Out"), ALIGN_CENTER));
148         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
149         ++r;
150         tl->attach (_add_output_midi, 0, 1, r, r + 1, SHRINK, SHRINK);
151         tl->attach (_del_output_midi, 1, 2, r, r + 1, SHRINK, SHRINK);
152         ++r;
153
154         /* right side table */
155         r = 0;
156         l = manage (new Label (_("<b>Connections</b>"), ALIGN_CENTER));
157         l->set_use_markup ();
158         tr->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
159         ++r;
160         tr->attach (_rst_mapping, 0, 2, r, r + 1, FILL, SHRINK);
161         ++r;
162
163         sep = manage (new HSeparator ());
164         tr->attach (*sep, 0, 2, r, r + 1, FILL|EXPAND, SHRINK, 0, 4);
165         ++r;
166
167         tr->attach (_tgl_sidechain, 0, 2, r, r + 1, FILL, SHRINK);
168         ++r;
169
170         _sidechain_tbl = manage (new Gtk::Table ());
171         _sidechain_tbl->set_spacings (2);
172         tr->attach (*_sidechain_tbl, 0, 2, r, r + 1, EXPAND|FILL, EXPAND|FILL, 0, 2);
173         ++r;
174
175         tr->attach (_add_sc_audio, 0, 1, r, r + 1, FILL, SHRINK);
176         tr->attach (_add_sc_midi, 1, 2, r, r + 1, FILL, SHRINK);
177         ++r;
178
179         HBox* hbox = manage (new HBox);
180         hbox->set_spacing (4);
181         hbox->pack_start (*tl, false, false);
182         hbox->pack_start (darea, true, true);
183         hbox->pack_start (*tr, false, false);
184
185         VBox* vbox = manage (new VBox);
186         vbox->pack_start (*hbox, true, true);
187         set_border_width (4);
188         add (*vbox);
189         vbox->show_all ();
190
191         plugin_reconfigured ();
192
193         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
194         darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
195         darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
196         darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
197         darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
198         darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
199         darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
200
201         _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
202
203         _rst_mapping.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_mapping));
204         _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
205         _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
206         _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
207
208         _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
209         _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
210         _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
211         _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
212         _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
213         _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
214
215         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
216                         _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
217                         );
218 }
219
220 PluginPinDialog::~PluginPinDialog ()
221 {
222         delete _sidechain_selector;
223 }
224
225 void
226 PluginPinDialog::plugin_reconfigured ()
227 {
228         ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
229         if (_ignore_updates) {
230                 return;
231         }
232         _n_plugins = _pi->get_count ();
233         _pi->configured_io (_in, _out);
234         _ins = _pi->internal_streams (); // with sidechain
235         _sinks = _pi->natural_input_streams ();
236         _sources = _pi->natural_output_streams ();
237
238         _tgl_sidechain.set_active (_pi->has_sidechain ());
239         _add_sc_audio.set_sensitive (_pi->has_sidechain ());
240         _add_sc_midi.set_sensitive (_pi->has_sidechain ());
241
242         if (_pi->custom_cfg ()) {
243                 _set_config.set_active (true);
244                 _add_plugin.set_sensitive (true);
245                 _add_output_audio.set_sensitive (true);
246                 _add_output_midi.set_sensitive (true);
247                 _del_plugin.set_sensitive (_n_plugins > 1);
248                 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
249                 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
250         } else {
251                 _set_config.set_active (false);
252                 _add_plugin.set_sensitive (false);
253                 _add_output_audio.set_sensitive (false);
254                 _add_output_midi.set_sensitive (false);
255                 _del_plugin.set_sensitive (false);
256                 _del_output_audio.set_sensitive (false);
257                 _del_output_midi.set_sensitive (false);
258         }
259
260         if (!_pi->has_sidechain () && _sidechain_selector) {
261                 delete _sidechain_selector;
262                 _sidechain_selector = 0;
263         }
264
265         refill_sidechain_table ();
266
267         /* update elements */
268
269         _elements.clear ();
270         _hover.reset ();
271         _actor.reset ();
272         _selection.reset ();
273
274         _n_inputs = _n_sidechains = 0;
275
276         for (uint32_t i = 0; i < _ins.n_total (); ++i) {
277                 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
278                 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
279                 bool sidechain = id >= _in.get (dt) ? true : false;
280                 if (sidechain) {
281                         ++_n_sidechains;
282                 } else {
283                         ++_n_inputs;
284                 }
285
286                 CtrlWidget cw (CtrlWidget (Input, dt, id, 0, sidechain));
287                 _elements.push_back (cw);
288         }
289
290         for (uint32_t i = 0; i < _out.n_total (); ++i) {
291                 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
292                 _elements.push_back (CtrlWidget (Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
293         }
294
295         for (uint32_t n = 0; n < _n_plugins; ++n) {
296                 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
297                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
298                         DataType dt (_sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
299                         int idx = (i < _sinks.n_midi ()) ? i : i - _sinks.n_midi ();
300                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
301                         CtrlWidget cw (CtrlWidget (Sink, dt, i, n, iod.is_sidechain));
302                         _elements.push_back (cw);
303                 }
304                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
305                         _elements.push_back (CtrlWidget (Source, (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO), i, n));
306                 }
307         }
308
309         /* calc minimum size */
310         const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
311         const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
312         uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
313         min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
314         min_width = std::max ((uint32_t)300, min_width);
315
316         min_width = 50 + 10 * ceilf (min_width / 10.f);
317
318         uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
319         min_height = std::max ((uint32_t)200, min_height);
320         min_height = 4 * ceilf (min_height / 4.f);
321
322         if (min_width != _min_width || min_height != _min_height) {
323                 _min_width = min_width;
324                 _min_height = min_height;
325                 darea.queue_resize ();
326         }
327
328         _position_valid = false;
329         darea.queue_draw ();
330 }
331
332 void
333 PluginPinDialog::refill_sidechain_table ()
334 {
335         Table_Helpers::TableList& kids = _sidechain_tbl->children ();
336         for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
337                 i = kids.erase (i);
338         }
339         _sidechain_tbl->resize (1, 1);
340         if (!_pi->has_sidechain () && _sidechain_selector) {
341                 return;
342         }
343         boost::shared_ptr<IO> io = _pi->sidechain_input ();
344         if (!io) {
345                 return;
346         }
347
348         uint32_t r = 0;
349         PortSet& p (io->ports ());
350         bool can_remove = p.num_ports () > 1;
351         for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i, ++r) {
352                 add_port_to_table (*i, r, can_remove);
353         }
354         for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i, ++r) {
355                 add_port_to_table (*i, r, can_remove);
356         }
357         _sidechain_tbl->show_all ();
358 }
359
360 void
361 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
362 {
363         std::string lbl;
364         std::string tip = p->name ();
365         std::vector<std::string> cns;
366         p->get_connections (cns);
367
368         // TODO proper labels, see MixerStrip::update_io_button()
369         if (cns.size () == 0) {
370                 lbl = "-";
371         } else if (cns.size () > 1) {
372                 lbl = "...";
373                 tip += " &lt;- ";
374         } else {
375                 lbl = cns[0];
376                 tip += " &lt;- ";
377                 if (lbl.find ("system:") == 0) {
378                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
379                         if (lbl.empty ()) {
380                                 lbl = cns[0].substr (7);
381                         }
382                 }
383         }
384         for (std::vector<std::string>::const_iterator i = cns.begin(); i != cns.end(); ++i) {
385                 tip += *i;
386                 tip += " ";
387         }
388
389         ArdourButton *pb = manage (new ArdourButton (lbl));
390         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
391         pb->set_layout_ellipsize_width (56 * PANGO_SCALE);
392         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
393         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
394
395         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
396         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
397
398         pb = manage (new ArdourButton ("-"));
399         _sidechain_tbl->attach (*pb, 1, 2, r, r +1 , FILL, SHRINK);
400         if (can_remove) {
401                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
402         } else {
403                 pb->set_sensitive (false);
404         }
405 }
406
407 void
408 PluginPinDialog::update_element_pos ()
409 {
410         /* layout sizes */
411         _innerwidth = _width - 2. * _margin_x;
412
413         const double yc   = rint (_height * .5);
414         const double bxh2 = rint (_margin_y * .45); // TODO grow?
415         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
416         const double bxw2 = rint (bxw * .5);
417         const double y_in = _margin_y;
418         const double y_out = _height - _margin_y;
419
420         _bxw2 = bxw2;
421         _bxh2 = bxh2;
422
423         const double dx = _pin_box_size * .5;
424
425         uint32_t sc_cnt = 0;
426         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
427                 switch (i->e->ct) {
428                         case Input:
429                                 if (i->e->sc) {
430                                         i->x = _innerwidth + _margin_x - dx;
431                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
432                                         i->h = _pin_box_size;
433                                         i->w = 1.5 * _pin_box_size;
434                                         ++ sc_cnt;
435                                 } else {
436                                         uint32_t idx = i->e->id;
437                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
438                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
439                                         i->w = _pin_box_size;
440                                         i->h = 1.5 * _pin_box_size;
441                                         i->y = y_in - i->h;
442                                 }
443                                 break;
444                         case Output:
445                                 {
446                                         uint32_t idx = i->e->id;
447                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
448                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
449                                         i->y = y_out;
450                                         i->w = _pin_box_size;
451                                         i->h = 1.5 * _pin_box_size;
452                                 }
453                                 break;
454                         case Sink:
455                                 {
456                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
457                                         i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
458                                         i->y = yc - bxh2 - dx;
459                                         i->w = _pin_box_size;
460                                         i->h = _pin_box_size;
461                                 }
462                                 break;
463                         case Source:
464                                 {
465                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
466                                         i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
467                                         i->y = yc + bxh2 - dx;
468                                         i->w = _pin_box_size;
469                                         i->h = _pin_box_size;
470                                 }
471                                 break;
472                 }
473         }
474 }
475
476 void
477 PluginPinDialog::set_color (cairo_t* cr, bool midi)
478 {
479         // see also gtk2_ardour/processor_box.cc
480         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
481         static const uint32_t midi_port_color = 0x960909FF; //Red
482
483         if (midi) {
484                 cairo_set_source_rgb (cr,
485                                 UINT_RGBA_R_FLT (midi_port_color),
486                                 UINT_RGBA_G_FLT (midi_port_color),
487                                 UINT_RGBA_B_FLT (midi_port_color));
488         } else {
489                 cairo_set_source_rgb (cr,
490                                 UINT_RGBA_R_FLT (audio_port_color),
491                                 UINT_RGBA_G_FLT (audio_port_color),
492                                 UINT_RGBA_B_FLT (audio_port_color));
493         }
494 }
495
496 void
497 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
498 {
499
500         if (w.e->sc) {
501                 const double dy = w.h * .5;
502                 const double dx = w.w - dy;
503                 cairo_move_to (cr, w.x, w.y + dy);
504                 cairo_rel_line_to (cr,  dy, -dy);
505                 cairo_rel_line_to (cr,  dx,  0);
506                 cairo_rel_line_to (cr,   0,  w.h);
507                 cairo_rel_line_to (cr, -dx,  0);
508         } else {
509                 const double dir = (w.e->ct == Input) ? 1 : -1;
510                 const double dx = w.w * .5;
511                 const double dy = w.h - dx;
512
513                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
514                 cairo_rel_line_to (cr,     -dx, -dx * dir);
515                 cairo_rel_line_to (cr,      0., -dy * dir);
516                 cairo_rel_line_to (cr, 2. * dx,        0.);
517                 cairo_rel_line_to (cr,      0.,  dy * dir);
518         }
519         cairo_close_path  (cr);
520
521         cairo_set_line_width (cr, 1.0);
522         cairo_set_source_rgb (cr, 0, 0, 0);
523         cairo_stroke_preserve (cr);
524
525         set_color (cr, w.e->dt == DataType::MIDI);
526
527         if (w.e->sc) {
528                 assert (w.e->ct == Input);
529                 cairo_fill_preserve (cr);
530                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
531         }
532
533         if (w.e == _selection || w.e == _actor) {
534                 cairo_fill_preserve (cr);
535                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
536         } else if (w.prelight) {
537                 cairo_fill_preserve (cr);
538                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
539         }
540         cairo_fill (cr);
541 }
542
543 void
544 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
545 {
546         const double dx = w.w * .5;
547         const double dy = w.h * .5;
548
549         cairo_move_to (cr, w.x + dx, w.y);
550         cairo_rel_line_to (cr, -dx,  dy);
551         cairo_rel_line_to (cr,  dx,  dy);
552         cairo_rel_line_to (cr,  dx, -dy);
553         cairo_close_path  (cr);
554
555         cairo_set_line_width (cr, 1.0);
556         cairo_set_source_rgb (cr, 0, 0, 0);
557         cairo_stroke_preserve (cr);
558
559         set_color (cr, w.e->dt == DataType::MIDI);
560
561         if (w.e->sc) {
562                 assert (w.e->ct == Sink);
563                 cairo_fill_preserve (cr);
564                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
565         }
566
567         if (w.e == _selection || w.e == _actor) {
568                 cairo_fill_preserve (cr);
569                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
570         } else if (w.prelight) {
571                 cairo_fill_preserve (cr);
572                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
573         }
574         cairo_fill (cr);
575 }
576
577 double
578 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
579 {
580         if (!midi) { i += n_midi; }
581         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
582 }
583
584 const PluginPinDialog::CtrlWidget&
585 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
586 {
587         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
588                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
589                         return *i;
590                 }
591         }
592         fatal << string_compose (_("programming error: %1"),
593                         X_("Invalid Plugin I/O Port."))
594                 << endmsg;
595         abort (); /*NOTREACHED*/
596         static CtrlWidget screw_old_compilers (Input, DataType::NIL, 0);
597         return screw_old_compilers;
598 }
599
600 void
601 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
602 {
603         switch (w.e->ct) {
604                 case Input:
605                         if (w.e->sc) {
606                                 x = w.x;
607                                 y = w.y + w.h * .5;
608                         } else {
609                                 x = w.x + w.w * .5;
610                                 y = w.y + w.h;
611                         }
612                         break;
613                 case Output:
614                         x = w.x + w.w * .5;
615                         y = w.y;
616                         break;
617                 case Sink:
618                         x = w.x + w.w * .5;
619                         y = w.y;
620                         break;
621                 case Source:
622                         x = w.x + w.w * .5;
623                         y = w.y + w.h;
624                         break;
625         }
626 }
627
628 void
629 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
630 {
631         const double bz = 2 * _pin_box_size;
632         const double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
633
634         cairo_move_to (cr, x0, y0);
635         if (horiz) {
636                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
637         } else {
638                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
639         }
640         cairo_set_line_width (cr, 3.0);
641         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
642         cairo_set_source_rgb (cr, 1, 0, 0);
643         if (dashed) {
644                 const double dashes[] = { 5, 7 };
645                 cairo_set_dash (cr, dashes, 2, 0);
646         }
647         set_color (cr, midi);
648         cairo_stroke (cr);
649         if (dashed) {
650                 cairo_set_dash (cr, 0, 0, 0);
651         }
652 }
653
654 void
655 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
656 {
657         double x0, x1, y0, y1;
658         edge_coordinates (w0, x0, y0);
659         edge_coordinates (w1, x1, y1);
660         assert (w0.e->dt == w1.e->dt);
661         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
662 }
663
664
665 bool
666 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
667 {
668         Gtk::Allocation a = darea.get_allocation ();
669         double const width = a.get_width ();
670         double const height = a.get_height ();
671
672         if (!_position_valid) {
673                 _width = width;
674                 _height = height;
675                 update_element_pos ();
676                 _position_valid = true;
677         }
678
679         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
680         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
681         cairo_clip (cr);
682
683         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
684         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
685         cairo_rectangle (cr, 0, 0, width, height);
686         cairo_fill (cr);
687
688         const double yc = rint (_height * .5);
689
690         /* processor box */
691         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
692         cairo_set_line_width (cr, 1.0);
693         cairo_set_source_rgb (cr, .1, .1, .3);
694         cairo_stroke_preserve (cr);
695         cairo_set_source_rgb (cr, .3, .3, .3);
696         cairo_fill (cr);
697
698         /* draw midi-bypass (behind) */
699         if (_pi->has_midi_bypass ()) {
700                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
701                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
702                 draw_connection (cr, cw0, cw1, true);
703         }
704
705         /* labels */
706         Glib::RefPtr<Pango::Layout> layout;
707         layout = Pango::Layout::create (get_pango_context ());
708
709         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
710         layout->set_width (_height * PANGO_SCALE);
711
712         int text_width;
713         int text_height;
714
715         layout->set_text (_route ()->name ());
716         layout->get_pixel_size (text_width, text_height);
717         cairo_save (cr);
718         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
719         cairo_rotate (cr, M_PI * -.5);
720         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
721         pango_cairo_show_layout (cr, layout->gobj ());
722         cairo_new_path (cr);
723         cairo_restore (cr);
724
725         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
726         layout->set_text (_pi->name ());
727         layout->get_pixel_size (text_width, text_height);
728         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
729         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
730         pango_cairo_show_layout (cr, layout->gobj ());
731
732         if (_pi->strict_io ()) {
733                 layout->set_text (_("Strict I/O"));
734                 layout->get_pixel_size (text_width, text_height);
735                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
736                 const double sy0 = _height - 3 - text_height;
737
738                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
739                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
740                 cairo_fill (cr);
741
742                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
743                 cairo_move_to (cr, sx0, sy0);
744                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
745                 pango_cairo_show_layout (cr, layout->gobj ());
746         }
747
748
749         /* plugins & connection wires */
750         for (uint32_t i = 0; i < _n_plugins; ++i) {
751                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
752
753                 /* plugin box */
754                 cairo_set_source_rgb (cr, .5, .5, .5);
755                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
756                 cairo_fill (cr);
757
758                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
759                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
760
761                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
762                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
763                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
764                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
765                                 draw_connection (cr, cw0, cw1);
766                         }
767                 }
768
769                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
770                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
771                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
772                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
773                                 draw_connection (cr, cw0, cw1);
774                         }
775                 }
776         }
777
778         /* pins and ports */
779         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
780                 switch (i->e->ct) {
781                         case Input:
782                         case Output:
783                                 draw_io_pin (cr, *i);
784                                 break;
785                         case Sink:
786                         case Source:
787                                 draw_plugin_pin (cr, *i);
788                                 break;
789                 }
790         }
791
792         cairo_destroy (cr);
793         return true;
794 }
795
796 void
797 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
798 {
799         req->width = _min_width;
800         req->height = _min_height;
801 }
802
803 void
804 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
805 {
806         _position_valid = false;
807 }
808
809 bool
810 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
811 {
812         bool changed = false;
813         _hover.reset ();
814         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
815                 if (ev->x >= i->x && ev->x <= i->x + i->w
816                                 && ev->y >= i->y && ev->y <= i->y + i->h)
817                 {
818                         if (!i->prelight) changed = true;
819                         i->prelight = true;
820                         _hover = i->e;
821                 } else {
822                         if (i->prelight) changed = true;
823                         i->prelight = false;
824                 }
825         }
826         if (changed) {
827                 darea.queue_draw ();
828         }
829         return true;
830 }
831
832 bool
833 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
834 {
835         if (ev->type != GDK_BUTTON_PRESS) {
836                 return false;
837         }
838
839         switch (ev->button) {
840                 case 1:
841                         if (!_selection || (_selection && !_hover)) {
842                                 _selection = _hover;
843                                 _actor.reset ();
844                                 darea.queue_draw ();
845                         } else if (_selection && _hover && _selection != _hover) {
846                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
847                                 else if (_selection->ct == Input && _hover->ct == Sink) { _actor = _hover; }
848                                 else if (_selection->ct == Sink && _hover->ct == Input) { _actor = _hover; }
849                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
850                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
851                                 if (!_actor) {
852                                 _selection = _hover;
853                                 }
854                                 darea.queue_draw ();
855                         }
856                         break;
857                 case 3:
858                         if (_selection != _hover) {
859                                 _selection = _hover;
860                                 darea.queue_draw ();
861                         }
862                         _actor.reset ();
863                         break;
864                 default:
865                         break;
866         }
867
868         return true;
869 }
870
871 bool
872 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
873 {
874         if (_hover == _actor && _actor && ev->button == 1) {
875                 assert (_selection);
876                 assert (_selection->dt == _actor->dt);
877                 if      (_selection->ct == Input && _actor->ct == Sink) {
878                         handle_input_action (_actor, _selection);
879                 }
880                 else if (_selection->ct == Sink && _actor->ct == Input) {
881                         handle_input_action (_selection, _actor);
882                 }
883                 else if (_selection->ct == Output && _actor->ct == Source) {
884                         handle_output_action (_actor, _selection);
885                 }
886                 else if (_selection->ct == Source && _actor->ct == Output) {
887                         handle_output_action (_selection, _actor);
888                 }
889                 _selection.reset ();
890         } else if (_hover == _selection && _selection && ev->button == 3) {
891                 handle_disconnect (_selection);
892         }
893         _actor.reset ();
894         darea.queue_draw ();
895         return true;
896 }
897
898 void
899 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
900 {
901         const int pc = s->ip;
902         bool valid;
903         ChanMapping in_map (_pi->input_map (pc));
904         uint32_t idx = in_map.get (s->dt, s->id, &valid);
905
906         if (valid && idx == i->id) {
907                 // disconnect
908                 in_map.unset (s->dt, s->id);
909                 _pi->set_input_map (pc, in_map);
910         }
911         else if (!valid) {
912                 // connect
913                 in_map.set (s->dt, s->id, i->id);
914                 _pi->set_input_map (pc, in_map);
915         }
916         else {
917                 // reconnect
918                 in_map.unset (s->dt, s->id);
919                 in_map.set (s->dt, s->id, i->id);
920                 _pi->set_input_map (pc, in_map);
921         }
922 }
923
924 void
925 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
926 {
927         const uint32_t pc = s->ip;
928         bool valid;
929         ChanMapping out_map (_pi->output_map (pc));
930         uint32_t idx = out_map.get (s->dt, s->id, &valid);
931
932         if (valid && idx == o->id) {
933                 // disconnect
934                 out_map.unset (s->dt, s->id);
935                 _pi->set_output_map (pc, out_map);
936         }
937         else {
938                 // disconnect source
939                 if (valid) {
940                         out_map.unset (s->dt, s->id);
941                 }
942                 // disconnect other outputs
943                 _ignore_updates = true;
944                 for (uint32_t n = 0; n < _n_plugins; ++n) {
945                         if (n == pc) {
946                                 continue;
947                         }
948                         ChanMapping n_out_map (_pi->output_map (n));
949                         idx = n_out_map.get_src (s->dt, o->id, &valid);
950                         if (valid) {
951                                 n_out_map.unset (s->dt, idx);
952                                 _pi->set_output_map (n, n_out_map);
953                         }
954                 }
955                 _ignore_updates = false;
956                 idx = out_map.get_src (s->dt, o->id, &valid);
957                 if (valid) {
958                         out_map.unset (s->dt, idx);
959                 }
960                 // connect
961                 out_map.set (s->dt, s->id, o->id);
962                 _pi->set_output_map (pc, out_map);
963         }
964 }
965
966 void
967 PluginPinDialog::handle_disconnect (const CtrlElem &e)
968 {
969         _ignore_updates = true;
970         bool changed = false;
971         bool valid;
972
973         switch (e->ct) {
974                 case Input:
975                         for (uint32_t n = 0; n < _n_plugins; ++n) {
976                                 ChanMapping map (_pi->input_map (n));
977                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
978                                         uint32_t idx = map.get (e->dt, i, &valid);
979                                         if (valid && idx == e->id) {
980                                                 map.unset (e->dt, i);
981                                                 changed = true;
982                                         }
983                                 }
984                                 _pi->set_input_map (n, map);
985                         }
986                         break;
987                 case Sink:
988                         {
989                                 ChanMapping map (_pi->input_map (e->ip));
990                                 map.get (e->dt, e->id, &valid);
991                                 if (valid) {
992                                         map.unset (e->dt, e->id);
993                                         _pi->set_input_map (e->ip, map);
994                                         changed = true;
995                                 }
996                         }
997                         break;
998                 case Source:
999                         {
1000                                 ChanMapping map (_pi->output_map (e->ip));
1001                                 map.get (e->dt, e->id, &valid);
1002                                 if (valid) {
1003                                         map.unset (e->dt, e->id);
1004                                         _pi->set_output_map (e->ip, map);
1005                                         changed = true;
1006                                 }
1007                         }
1008                         break;
1009                 case Output:
1010                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1011                                 ChanMapping map (_pi->output_map (n));
1012                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1013                                         uint32_t idx = map.get (e->dt, i, &valid);
1014                                         if (valid && idx == e->id) {
1015                                                 map.unset (e->dt, i);
1016                                                 changed = true;
1017                                         }
1018                                 }
1019                                 _pi->set_output_map (n, map);
1020                         }
1021                         break;
1022         }
1023         _ignore_updates = false;
1024         if (changed) {
1025                 plugin_reconfigured ();
1026         }
1027 }
1028
1029 void
1030 PluginPinDialog::toggle_sidechain ()
1031 {
1032         if (_session && _session->actively_recording()) { return; }
1033         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1034 }
1035
1036 void
1037 PluginPinDialog::connect_sidechain ()
1038 {
1039         if (!_session) { return; }
1040
1041         if (_sidechain_selector == 0) {
1042                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1043         }
1044
1045         if (_sidechain_selector->is_visible ()) {
1046                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1047         } else {
1048                 _sidechain_selector->present ();
1049         }
1050 }
1051
1052 void
1053 PluginPinDialog::reset_configuration ()
1054 {
1055         if (_set_config.get_active ()) {
1056                 _route ()->reset_plugin_insert (_pi);
1057         } else {
1058                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1059         }
1060 }
1061
1062 void
1063 PluginPinDialog::reset_mapping ()
1064 {
1065         _pi->reset_map ();
1066 }
1067
1068 void
1069 PluginPinDialog::add_remove_plugin_clicked (bool add)
1070 {
1071         if (_session && _session->actively_recording()) { return; }
1072         ChanCount out = _out;
1073         assert (add || _n_plugins > 0);
1074         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1075 }
1076
1077 void
1078 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1079 {
1080         if (_session && _session->actively_recording()) { return; }
1081         ChanCount out = _out;
1082         assert (add || out.get (dt) > 0);
1083         out.set (dt, out.get (dt) + (add ? 1 : -1));
1084         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1085 }
1086
1087 void
1088 PluginPinDialog::add_sidechain_port (DataType dt)
1089 {
1090         if (_session && _session->actively_recording()) { return; }
1091         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1092         if (!io) {
1093                 return;
1094         }
1095         io->add_port ("", this, dt);
1096 }
1097
1098 void
1099 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1100 {
1101         if (_session && _session->actively_recording()) { return; }
1102         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1103         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1104         if (!io || !p) {
1105                 return;
1106         }
1107         io->remove_port (p, this);
1108 }
1109
1110 void
1111 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1112 {
1113         if (_session && _session->actively_recording()) { return; }
1114         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1115         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1116         if (!io || !p) {
1117                 return;
1118         }
1119         p->disconnect_all ();
1120 }
1121
1122 void
1123 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1124 {
1125         if (_session && _session->actively_recording()) { return; }
1126         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1127         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1128         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1129         if (!io || !p0 || !p1) {
1130                 return;
1131         }
1132         p0->connect (p1->name ());
1133 }
1134
1135 bool
1136 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1137 {
1138         if (_session && _session->actively_recording()) { return false; }
1139         if (ev->button == 3) {
1140                 connect_sidechain ();
1141         }
1142         return false;
1143 }
1144
1145 struct RouteCompareByName {
1146         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1147                 return a->name().compare (b->name()) < 0;
1148         }
1149 };
1150
1151 bool
1152 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1153 {
1154         using namespace Menu_Helpers;
1155         if (!_session || _session->actively_recording()) { return false; }
1156         if (!_session->engine().connected()) { return false; }
1157
1158         if (ev->button == 1) {
1159                 MenuList& citems = input_menu.items();
1160                 input_menu.set_name ("ArdourContextMenu");
1161                 citems.clear();
1162
1163                 boost::shared_ptr<Port> p = wp.lock ();
1164                 if (p && p->connected ()) {
1165                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1166                         citems.push_back (SeparatorElem());
1167                 }
1168
1169 #if 0
1170                 // TODO add system inputs, too ?!
1171                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1172                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1173                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1174                         }
1175                         //maybe_add_bundle_to_input_menu (*i, current);
1176                 }
1177 #endif
1178
1179                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1180                 RouteList copy = *routes;
1181                 copy.sort (RouteCompareByName ());
1182                 uint32_t added = 0;
1183                 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
1184                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1185                 }
1186
1187                 if (added > 0) {
1188                         citems.push_back (SeparatorElem());
1189                 }
1190                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1191                 input_menu.popup (1, ev->time);
1192         }
1193         return false;
1194 }
1195
1196 uint32_t
1197 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1198 {
1199         uint32_t added = 0;
1200         using namespace Menu_Helpers;
1201         if (r->output () == _route()->output()) {
1202                 return added;
1203         }
1204
1205         if (_route ()->feeds_according_to_graph (r)) {
1206                 return added;
1207         }
1208
1209         MenuList& citems = input_menu.items();
1210         const IOVector& iov (r->all_outputs());
1211
1212         for (IOVector::const_iterator o = iov.begin(); o != iov.end(); ++o) {
1213                 boost::shared_ptr<IO> op = o->lock();
1214                 if (!op) {
1215                         continue;
1216                 }
1217                 PortSet& p (op->ports ());
1218                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1219                         std::string n = i->name ();
1220                         replace_all (n, "_", " ");
1221                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun(*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1222                         ++added;
1223                 }
1224         }
1225         return added;
1226 }
1227
1228 void
1229 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1230 {
1231         boost::shared_ptr<Port> p0 = w0.lock ();
1232         boost::shared_ptr<Port> p1 = w1.lock ();
1233
1234         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1235         if (!io) { return; }
1236
1237         if (p0 && io->has_port (p0)) {
1238                 plugin_reconfigured ();
1239         }
1240         else if (p1 && io->has_port (p1)) {
1241                 plugin_reconfigured ();
1242         }
1243 }