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