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