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