Update GPL boilerplate and (C)
[ardour.git] / gtk2_ardour / processor_box.cc
1 /*
2  * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2007 Doug McLain <doug@nostar.net>
5  * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
7  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
8  * Copyright (C) 2014-2015 Ben Loftis <ben@harrisonconsoles.com>
9  * Copyright (C) 2014-2016 Tim Mayberry <mojofunk@gmail.com>
10  * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
11  * Copyright (C) 2018 Len Ovens <len@ovenwerks.net>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28 #ifdef WAF_BUILD
29 #include "gtk2ardour-config.h"
30 #endif
31
32 #include <cmath>
33 #include <iostream>
34 #include <set>
35
36 #include <sigc++/bind.h>
37
38 #include <glibmm/miscutils.h>
39 #include <glibmm/fileutils.h>
40
41 #include <gtkmm/messagedialog.h>
42
43 #include "gtkmm2ext/colors.h"
44 #include "gtkmm2ext/gtk_ui.h"
45 #include "gtkmm2ext/menu_elems.h"
46 #include "gtkmm2ext/utils.h"
47 #include "gtkmm2ext/doi.h"
48 #include "gtkmm2ext/rgb_macros.h"
49
50 #include "widgets/choice.h"
51 #include "widgets/prompter.h"
52 #include "widgets/tooltips.h"
53
54 #include "ardour/amp.h"
55 #include "ardour/audio_track.h"
56 #include "ardour/audioengine.h"
57 #include "ardour/internal_return.h"
58 #include "ardour/internal_send.h"
59 #include "ardour/luaproc.h"
60 #include "ardour/luascripting.h"
61 #include "ardour/meter.h"
62 #include "ardour/panner_shell.h"
63 #include "ardour/plugin_insert.h"
64 #include "ardour/pannable.h"
65 #include "ardour/port_insert.h"
66 #include "ardour/profile.h"
67 #include "ardour/return.h"
68 #include "ardour/route.h"
69 #include "ardour/send.h"
70 #include "ardour/session.h"
71 #include "ardour/types.h"
72 #include "ardour/value_as_string.h"
73
74 #include "LuaBridge/LuaBridge.h"
75
76 #include "actions.h"
77 #include "ardour_dialog.h"
78 #include "ardour_ui.h"
79 #include "gui_thread.h"
80 #include "io_selector.h"
81 #include "keyboard.h"
82 #include "luainstance.h"
83 #include "mixer_ui.h"
84 #include "mixer_strip.h"
85 #include "plugin_pin_dialog.h"
86 #include "plugin_selector.h"
87 #include "plugin_ui.h"
88 #include "port_insert_ui.h"
89 #include "processor_box.h"
90 #include "processor_selection.h"
91 #include "public_editor.h"
92 #include "return_ui.h"
93 #include "script_selector.h"
94 #include "send_ui.h"
95 #include "timers.h"
96 #include "new_plugin_preset_dialog.h"
97
98 #include "pbd/i18n.h"
99
100 #ifdef AUDIOUNIT_SUPPORT
101 class AUPluginUI;
102 #endif
103
104 #ifndef NDEBUG
105 bool ProcessorBox::show_all_processors = false;
106 #endif
107
108 using namespace std;
109 using namespace ARDOUR;
110 using namespace PBD;
111 using namespace Gtk;
112 using namespace Glib;
113 using namespace Gtkmm2ext;
114 using namespace ArdourWidgets;
115
116 ProcessorBox*  ProcessorBox::_current_processor_box = 0;
117 RefPtr<Action> ProcessorBox::paste_action;
118 RefPtr<Action> ProcessorBox::cut_action;
119 RefPtr<Action> ProcessorBox::copy_action;
120 RefPtr<Action> ProcessorBox::rename_action;
121 RefPtr<Action> ProcessorBox::delete_action;
122 RefPtr<Action> ProcessorBox::backspace_action;
123 RefPtr<Action> ProcessorBox::manage_pins_action;
124 RefPtr<Action> ProcessorBox::disk_io_action;
125 RefPtr<Action> ProcessorBox::edit_action;
126 RefPtr<Action> ProcessorBox::edit_generic_action;
127 RefPtr<ActionGroup> ProcessorBox::processor_box_actions;
128 Gtkmm2ext::Bindings* ProcessorBox::bindings = 0;
129
130
131 // TODO consolidate with PluginPinDialog::set_color
132 static void set_routing_color (cairo_t* cr, bool midi)
133 {
134         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
135         static const uint32_t midi_port_color = 0x960909FF; //Red
136
137         if (midi) {
138                 cairo_set_source_rgb (cr,
139                                 UINT_RGBA_R_FLT(midi_port_color),
140                                 UINT_RGBA_G_FLT(midi_port_color),
141                                 UINT_RGBA_B_FLT(midi_port_color));
142         } else {
143                 cairo_set_source_rgb (cr,
144                                 UINT_RGBA_R_FLT(audio_port_color),
145                                 UINT_RGBA_G_FLT(audio_port_color),
146                                 UINT_RGBA_B_FLT(audio_port_color));
147         }
148 }
149
150 ProcessorEntry::ProcessorEntry (ProcessorBox* parent, boost::shared_ptr<Processor> p, Width w)
151         : _button (ArdourButton::led_default_elements)
152         , _position (PreFader)
153         , _position_num(0)
154         , _parent (parent)
155         , _selectable(true)
156         , _unknown_processor(false)
157         , _processor (p)
158         , _width (w)
159         , input_icon(true)
160         , output_icon(false)
161         , routing_icon(true)
162         , output_routing_icon(false)
163         , _plugin_display(0)
164 {
165         _vbox.show ();
166
167         _button.set_distinct_led_click (true);
168         _button.set_fallthrough_to_parent(true);
169         _button.set_led_left (true);
170         _button.signal_led_clicked.connect (sigc::mem_fun (*this, &ProcessorEntry::led_clicked));
171         _button.set_text (name (_width));
172
173         if (boost::dynamic_pointer_cast<PeakMeter> (_processor)) {
174                 _button.set_elements(ArdourButton::Element(_button.elements() & ~ArdourButton::Indicator));
175         }
176         if (boost::dynamic_pointer_cast<UnknownProcessor> (_processor)) {
177                 _button.set_elements(ArdourButton::Element(_button.elements() & ~ArdourButton::Indicator));
178                 _unknown_processor = true;
179         }
180         {
181                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
182                 if (pi && pi->plugin()) {
183                         _plugin_preset_pointer = PluginPresetPtr (new PluginPreset (pi->plugin()->get_info()));
184                 }
185         }
186         if (_processor) {
187
188                 _vbox.pack_start (routing_icon);
189                 _vbox.pack_start (input_icon);
190                 _vbox.pack_start (_button, true, true);
191
192                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
193                 if (pi && pi->plugin() && pi->plugin()->has_inline_display()) {
194                         if (pi->plugin()->get_info()->type != ARDOUR::Lua) {
195                                 _plugin_display = new PluginInlineDisplay (*this, pi->plugin(),
196                                                 std::max (60.f, rintf(112.f * UIConfiguration::instance().get_ui_scale())));
197                         } else {
198                                 assert (boost::dynamic_pointer_cast<LuaProc>(pi->plugin()));
199                                 _plugin_display = new LuaPluginDisplay (*this, boost::dynamic_pointer_cast<LuaProc>(pi->plugin()),
200                                                 std::max (60.f, rintf(112.f * UIConfiguration::instance().get_ui_scale())));
201                         }
202                         _vbox.pack_start (*_plugin_display);
203                         _plugin_display->set_no_show_all (true);
204                         if (UIConfiguration::instance().get_show_inline_display_by_default ()) {
205                                 _plugin_display->show ();
206                         }
207                 }
208                 _vbox.pack_end (output_routing_icon);
209                 _vbox.pack_end (output_icon);
210
211                 _button.set_active (_processor->enabled ());
212
213                 input_icon.set_no_show_all(true);
214                 routing_icon.set_no_show_all(true);
215                 output_icon.set_no_show_all(true);
216                 output_routing_icon.set_no_show_all(true);
217
218                 _button.show ();
219                 input_icon.hide();
220                 output_icon.show();
221                 routing_icon.hide();
222                 output_routing_icon.hide();
223
224                 _processor->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_active_changed, this), gui_context());
225                 _processor->PropertyChanged.connect (name_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_property_changed, this, _1), gui_context());
226                 _processor->ConfigurationChanged.connect (config_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_configuration_changed, this, _1, _2), gui_context());
227
228                 const uint32_t limit_inline_controls = UIConfiguration::instance().get_max_inline_controls ();
229
230                 set<Evoral::Parameter> p = _processor->what_can_be_automated ();
231                 for (set<Evoral::Parameter>::iterator i = p.begin(); i != p.end(); ++i) {
232
233                         std::string label = _processor->describe_parameter (*i);
234
235                         if (label == X_("hidden")) {
236                                 continue;
237                         }
238
239                         if (boost::dynamic_pointer_cast<Send> (_processor)) {
240                                 label = _("Send");
241                         } else if (boost::dynamic_pointer_cast<Return> (_processor)) {
242                                 label = _("Return");
243                         }
244
245                         Control* c = new Control (_processor->automation_control (*i), label);
246
247                         _controls.push_back (c);
248
249                         if (boost::dynamic_pointer_cast<Amp> (_processor) == 0) {
250                                 /* Add non-Amp (Fader & Trim) controls to the processor box */
251                                 _vbox.pack_start (c->box);
252                         }
253
254                         if (limit_inline_controls > 0 && _controls.size() >= limit_inline_controls) {
255                                 break;
256                         }
257                 }
258
259                 setup_tooltip ();
260                 setup_visuals ();
261         } else {
262                 _vbox.set_size_request (-1, _button.size_request().height);
263         }
264 }
265
266 ProcessorEntry::~ProcessorEntry ()
267 {
268         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
269                 delete *i;
270         }
271         delete _plugin_display;
272 }
273
274 EventBox&
275 ProcessorEntry::action_widget ()
276 {
277         return _button;
278 }
279
280 Gtk::Widget&
281 ProcessorEntry::widget ()
282 {
283         return _vbox;
284 }
285
286 string
287 ProcessorEntry::drag_text () const
288 {
289         return name (Wide);
290 }
291
292 bool
293 ProcessorEntry::can_copy_state (Gtkmm2ext::DnDVBoxChild* o) const
294 {
295         ProcessorEntry *other = dynamic_cast<ProcessorEntry*> (o);
296         if (!other) {
297                 return false;
298         }
299         boost::shared_ptr<ARDOUR::Processor> otherproc = other->processor();
300         boost::shared_ptr<PluginInsert> my_pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
301         boost::shared_ptr<PluginInsert> ot_pi = boost::dynamic_pointer_cast<PluginInsert> (otherproc);
302         if (boost::dynamic_pointer_cast<UnknownProcessor> (_processor)) {
303                 return false;
304         }
305         if (boost::dynamic_pointer_cast<UnknownProcessor> (otherproc)) {
306                 return false;
307         }
308         if (!my_pi || !ot_pi) {
309                 return false;
310         }
311         if (my_pi->type() != ot_pi->type()) {
312                 return false;
313         }
314         boost::shared_ptr<Plugin> my_p = my_pi->plugin();
315         boost::shared_ptr<Plugin> ot_p = ot_pi->plugin();
316         if (!my_p || !ot_p) {
317                 return false;
318         }
319         if (my_p->unique_id() != ot_p->unique_id()) {
320                 return false;
321         }
322         return true;
323 }
324
325 bool
326 ProcessorEntry::drag_data_get (Glib::RefPtr<Gdk::DragContext> const, Gtk::SelectionData &data)
327 {
328         if (data.get_target() == "PluginPresetPtr") {
329                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
330
331                 if (!_plugin_preset_pointer || !pi) {
332                         data.set (data.get_target(), 8, NULL, 0);
333                         return true;
334                 }
335
336                 boost::shared_ptr<ARDOUR::Plugin> plugin = pi->plugin();
337                 assert (plugin);
338
339                 PluginManager& manager (PluginManager::instance());
340                 bool fav = manager.get_status (_plugin_preset_pointer->_pip) == PluginManager::Favorite;
341
342                 NewPluginPresetDialog d (plugin,
343                                 string_compose(_("New Favorite Preset for \"%1\""),_plugin_preset_pointer->_pip->name), !fav);
344
345                 _plugin_preset_pointer->_preset.valid = false;
346
347                 switch (d.run ()) {
348                         case Gtk::RESPONSE_CANCEL:
349                                 data.set (data.get_target(), 8, NULL, 0);
350                                 return true;
351                                 break;
352
353                         case Gtk::RESPONSE_NO:
354                                 break;
355
356                         case Gtk::RESPONSE_ACCEPT:
357                                 if (d.name().empty()) {
358                                         break;
359                                 }
360
361                                 if (d.replace ()) {
362                                         plugin->remove_preset (d.name ());
363                                 }
364
365                                 Plugin::PresetRecord const r = plugin->save_preset (d.name());
366
367                                 if (!r.uri.empty ()) {
368                                         _plugin_preset_pointer->_preset.uri   = r.uri;
369                                         _plugin_preset_pointer->_preset.label = r.label;
370                                         _plugin_preset_pointer->_preset.user  = r.user;
371                                         _plugin_preset_pointer->_preset.valid = r.valid;
372                                 }
373                 }
374                 data.set (data.get_target(), 8, (const guchar *) &_plugin_preset_pointer, sizeof (PluginPresetPtr));
375                 return true;
376         }
377         return false;
378 }
379
380 void
381 ProcessorEntry::set_position (Position p, uint32_t num)
382 {
383         _position = p;
384         _position_num = num;
385         setup_visuals ();
386 }
387
388 void
389 ProcessorEntry::set_visual_state (Gtkmm2ext::VisualState s, bool yn)
390 {
391         if (yn) {
392                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() | s));
393         } else {
394                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() & ~s));
395         }
396 }
397
398 void
399 ProcessorEntry::setup_visuals ()
400 {
401         if (_unknown_processor) {
402                 _button.set_name ("processor stub");
403                 return;
404         }
405         boost::shared_ptr<Send> send;
406         if ((send = boost::dynamic_pointer_cast<Send> (_processor))) {
407                 if (send->remove_on_disconnect ()) {
408                         _button.set_name ("processor sidechain");
409                         return;
410                 }
411         }
412
413         boost::shared_ptr<InternalSend> aux;
414         if ((aux = boost::dynamic_pointer_cast<InternalSend> (_processor))) {
415                 if (aux->allow_feedback ()) {
416                         _button.set_name ("processor auxfeedback");
417                         return;
418                 }
419         }
420
421         switch (_position) {
422         case PreFader:
423                 if (_plugin_display) { _plugin_display->set_name ("processor prefader"); }
424                 _button.set_name ("processor prefader");
425                 break;
426
427         case Fader:
428                 _button.set_name ("processor fader");
429                 break;
430
431         case PostFader:
432                 if (_plugin_display) { _plugin_display->set_name ("processor postfader"); }
433                 _button.set_name ("processor postfader");
434                 break;
435         }
436 }
437
438
439 boost::shared_ptr<Processor>
440 ProcessorEntry::processor () const
441 {
442         if (!_processor) {
443                 return boost::shared_ptr<Processor>();
444         }
445         return _processor;
446 }
447
448 void
449 ProcessorEntry::set_enum_width (Width w)
450 {
451         _width = w;
452         _button.set_text (name (_width));
453 }
454
455 void
456 ProcessorEntry::led_clicked(GdkEventButton *ev)
457 {
458         bool ctrl_shift_pressed = false;
459         Keyboard::ModifierMask ctrl_shift_mask = Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier);
460
461         if (Keyboard::modifier_state_equals (ev->state, ctrl_shift_mask)) {
462                 ctrl_shift_pressed = true;
463         }
464
465         if (_processor) {
466                 if (_button.get_active ()) {
467                         if (ctrl_shift_pressed) {
468                                 _parent->all_visible_processors_active(false);
469
470                                 if (_position == Fader) {
471                                         _processor->enable (false);
472                                 }
473                         }
474                         else {
475                                 _processor->enable (false);
476                         }
477
478                 } else {
479                         if (ctrl_shift_pressed) {
480                                 _parent->all_visible_processors_active(true);
481
482                                 if (_position == Fader) {
483                                         _processor->enable (true);
484                                 }
485                         }
486                         else {
487                                 _processor->enable (true);
488                         }
489                 }
490         }
491 }
492
493 void
494 ProcessorEntry::processor_active_changed ()
495 {
496         if (_processor) {
497                 _button.set_active (_processor->enabled ());
498         }
499 }
500
501 void
502 ProcessorEntry::processor_property_changed (const PropertyChange& what_changed)
503 {
504         if (what_changed.contains (ARDOUR::Properties::name)) {
505                 _button.set_text (name (_width));
506                 setup_tooltip ();
507         }
508 }
509
510 void
511 ProcessorEntry::processor_configuration_changed (const ChanCount in, const ChanCount out)
512 {
513         _parent->setup_routing_feeds ();
514         input_icon.queue_draw();
515         output_icon.queue_draw();
516         routing_icon.queue_draw();
517         output_routing_icon.queue_draw();
518 }
519
520 void
521 ProcessorEntry::setup_tooltip ()
522 {
523         if (_processor) {
524                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
525                 if (pi) {
526                         std::string postfix = "";
527                         uint32_t replicated;
528
529                         if (pi->plugin()->has_inline_display()) {
530                                 postfix += string_compose(_("\n%1+double-click to toggle inline-display"), Keyboard::tertiary_modifier_name ());
531                         }
532
533                         if ((replicated = pi->get_count()) > 1) {
534                                 postfix += string_compose(_("\nThis plugin has been replicated %1 times."), replicated);
535                         }
536
537                         if (pi->plugin()->has_editor()) {
538                                 set_tooltip (_button,
539                                                 string_compose (_("<b>%1</b>\nDouble-click to show GUI.\n%2+double-click to show generic GUI.%3"), name (Wide), Keyboard::secondary_modifier_name (), postfix));
540                         } else {
541                                 set_tooltip (_button,
542                                                 string_compose (_("<b>%1</b>\nDouble-click to show generic GUI.%2"), name (Wide), postfix));
543                         }
544                         return;
545                 }
546                 if(boost::dynamic_pointer_cast<UnknownProcessor> (_processor)) {
547                         ARDOUR_UI::instance()->set_tip (_button,
548                                         string_compose (_("<b>%1</b>\nThe Plugin is not available on this system\nand has been replaced by a stub."), name (Wide)));
549                         return;
550                 }
551                 boost::shared_ptr<Send> send;
552                 if ((send = boost::dynamic_pointer_cast<Send> (_processor)) != 0 &&
553                                 !boost::dynamic_pointer_cast<InternalSend>(_processor)) {
554                         if (send->remove_on_disconnect ()) {
555                                 set_tooltip (_button, string_compose ("<b>&gt; %1</b>\nThis (sidechain) send will be removed when disconnected.", _processor->name()));
556                         } else {
557                                 set_tooltip (_button, string_compose ("<b>&gt; %1</b>", _processor->name()));
558                         }
559                         return;
560                 }
561         }
562         set_tooltip (_button, string_compose ("<b>%1</b>", name (Wide)));
563 }
564
565 string
566 ProcessorEntry::name (Width w) const
567 {
568         boost::shared_ptr<Send> send;
569         string name_display;
570
571         if (!_processor) {
572                 return string();
573         }
574
575         if ((send = boost::dynamic_pointer_cast<Send> (_processor)) != 0 &&
576             !boost::dynamic_pointer_cast<InternalSend>(_processor)) {
577
578                 name_display += '>';
579                 std::string send_name;
580                 bool pretty_ok = true;
581
582                 if (send->remove_on_disconnect ()) {
583                         // assume it's a sidechain, find pretty name of connected port(s)
584                         PortSet& ps (send->output ()->ports ());
585                         for (PortSet::iterator i = ps.begin (); i != ps.end () && pretty_ok; ++i) {
586                                 vector<string> connections;
587                                 if (i->get_connections (connections)) {
588                                         vector<string>::const_iterator ci;
589                                         for (ci = connections.begin(); ci != connections.end(); ++ci) {
590                                                 std::string pn = AudioEngine::instance()->get_pretty_name_by_name (*ci);
591                                                 if (pn.empty ()) {
592                                                         continue;
593                                                 }
594                                                 if (send_name.empty ()) {
595                                                         send_name = pn;
596                                                 } else if (send_name != pn) {
597                                                         // pretty names don't match
598                                                         pretty_ok = false;
599                                                         break;
600                                                 }
601                                         }
602                                 }
603                         }
604                 }
605
606                 if (!pretty_ok) {
607                         send_name = "";
608                 }
609
610                 /* grab the send name out of its overall name */
611                 if (send_name.empty()) {
612                         send_name = send->name();
613                         string::size_type lbracket, rbracket;
614                         lbracket = send_name.find ('[');
615                         rbracket = send_name.find (']');
616                         send_name = send_name.substr (lbracket+1, lbracket-rbracket-1);
617                 }
618
619                 switch (w) {
620                 case Wide:
621                         name_display += send_name;
622                         break;
623                 case Narrow:
624                         name_display += PBD::short_version (send_name, 5);
625                         break;
626                 }
627
628         } else {
629                 boost::shared_ptr<ARDOUR::PluginInsert> pi;
630                 if ((pi = boost::dynamic_pointer_cast<ARDOUR::PluginInsert> (_processor)) != 0 && pi->get_count() > 1) {
631                         switch (w) {
632                                 case Wide:
633                                         name_display += "* ";
634                                         break;
635                                 case Narrow:
636                                         name_display += "*";
637                                         break;
638                         }
639                 }
640
641                 switch (w) {
642                 case Wide:
643                         name_display += _processor->display_name();
644                         break;
645                 case Narrow:
646                         name_display += PBD::short_version (_processor->display_name(), 5);
647                         break;
648                 }
649         }
650
651         return name_display;
652 }
653
654 void
655 ProcessorEntry::show_all_controls ()
656 {
657         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
658                 (*i)->set_visible (true);
659         }
660
661         _parent->update_gui_object_state (this);
662 }
663
664 void
665 ProcessorEntry::hide_all_controls ()
666 {
667         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
668                 (*i)->set_visible (false);
669         }
670
671         _parent->update_gui_object_state (this);
672 }
673
674 void
675 ProcessorEntry::add_control_state (XMLNode* node) const
676 {
677         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
678                 (*i)->add_state (node);
679         }
680
681         if (_plugin_display) {
682                 XMLNode* c = new XMLNode (X_("Object"));
683                 c->set_property (X_("id"), X_("InlineDisplay"));
684                 c->set_property (X_("visible"), _plugin_display->is_visible ());
685                 node->add_child_nocopy (*c);
686         }
687 }
688
689 void
690 ProcessorEntry::set_control_state (XMLNode const * node)
691 {
692         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
693                 (*i)->set_state (node);
694         }
695
696         if (_plugin_display) {
697                 XMLNode* n = GUIObjectState::get_node (node, X_("InlineDisplay"));
698                 if (!n) return;
699
700                 bool visible;
701                 if (n->get_property (X_("visible"), visible)) {
702                         if (visible) {
703                                 _plugin_display->show ();
704                         } else {
705                                 _plugin_display->hide ();
706                         }
707                 }
708         }
709 }
710
711 string
712 ProcessorEntry::state_id () const
713 {
714         return string_compose ("processor %1", _processor->id().to_s());
715 }
716
717 void
718 ProcessorEntry::hide_things ()
719 {
720         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
721                 (*i)->hide_things ();
722         }
723 }
724
725
726 Menu*
727 ProcessorEntry::build_controls_menu ()
728 {
729         using namespace Menu_Helpers;
730
731         if (!_plugin_display && _controls.empty ()) {
732                 return NULL;
733         }
734
735         Menu* menu = manage (new Menu);
736         MenuList& items = menu->items ();
737
738         if (_plugin_display) {
739                 items.push_back (CheckMenuElem (_("Inline Display")));
740                 Gtk::CheckMenuItem* c = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
741                 c->set_active (_plugin_display->is_visible ());
742                 c->signal_toggled().connect (sigc::mem_fun (*this, &ProcessorEntry::toggle_inline_display_visibility));
743         }
744
745         if (_controls.empty ()) {
746                 return menu;
747         } else {
748                 items.push_back (SeparatorElem ());
749         }
750
751         items.push_back (
752                 MenuElem (_("Show All Controls"), sigc::mem_fun (*this, &ProcessorEntry::show_all_controls))
753                 );
754
755         items.push_back (
756                 MenuElem (_("Hide All Controls"), sigc::mem_fun (*this, &ProcessorEntry::hide_all_controls))
757                 );
758
759         items.push_back (SeparatorElem ());
760
761         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
762                 items.push_back (CheckMenuElemNoMnemonic ((*i)->name ()));
763                 Gtk::CheckMenuItem* c = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
764                 c->set_active ((*i)->visible ());
765                 c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ProcessorEntry::toggle_control_visibility), *i));
766         }
767
768         return menu;
769 }
770
771 void
772 ProcessorEntry::toggle_inline_display_visibility ()
773 {
774         if (_plugin_display->is_visible ()) {
775                 _plugin_display->hide();
776         } else {
777                 _plugin_display->show();
778         }
779         _parent->update_gui_object_state (this);
780 }
781
782 void
783 ProcessorEntry::toggle_control_visibility (Control* c)
784 {
785         c->set_visible (!c->visible ());
786         _parent->update_gui_object_state (this);
787 }
788
789 Menu*
790 ProcessorEntry::build_send_options_menu ()
791 {
792         using namespace Menu_Helpers;
793         Menu* menu = manage (new Menu);
794         MenuList& items = menu->items ();
795
796         if (!ARDOUR::Profile->get_mixbus()) {
797                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (_processor);
798                 if (send) {
799                         items.push_back (CheckMenuElem (_("Link panner controls")));
800                         Gtk::CheckMenuItem* c = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
801                         c->set_active (send->panner_shell()->is_linked_to_route());
802                         c->signal_toggled().connect (sigc::mem_fun (*this, &ProcessorEntry::toggle_panner_link));
803                 }
804         }
805
806         boost::shared_ptr<InternalSend> aux = boost::dynamic_pointer_cast<InternalSend> (_processor);
807         if (aux) {
808                 items.push_back (CheckMenuElem (_("Allow Feedback Loop")));
809                 Gtk::CheckMenuItem* c = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
810                 c->set_active (aux->allow_feedback());
811                 c->signal_toggled().connect (sigc::mem_fun (*this, &ProcessorEntry::toggle_allow_feedback));
812         }
813         return menu;
814 }
815
816 void
817 ProcessorEntry::toggle_panner_link ()
818 {
819         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (_processor);
820         if (send) {
821                 send->panner_shell()->set_linked_to_route(!send->panner_shell()->is_linked_to_route());
822         }
823 }
824
825 void
826 ProcessorEntry::toggle_allow_feedback ()
827 {
828         boost::shared_ptr<InternalSend> aux = boost::dynamic_pointer_cast<InternalSend> (_processor);
829         if (aux) {
830                 aux->set_allow_feedback (!aux->allow_feedback ());
831         }
832 }
833
834 ProcessorEntry::Control::Control (boost::shared_ptr<AutomationControl> c, string const & n)
835         : _control (c)
836         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
837         , _slider (&_adjustment, boost::shared_ptr<PBD::Controllable>(), 0, max(13.f, rintf(13.f * UIConfiguration::instance().get_ui_scale())))
838         , _slider_persistant_tooltip (&_slider)
839         , _button (ArdourButton::led_default_elements)
840         , _ignore_ui_adjustment (false)
841         , _visible (false)
842         , _name (n)
843 {
844         _slider.set_controllable (c);
845         box.set_padding(0, 0, 4, 4);
846
847         if (c->toggled()) {
848                 _button.set_text (_name);
849                 _button.set_led_left (true);
850                 _button.set_name ("processor control button");
851                 box.add (_button);
852                 _button.show ();
853
854                 _button.signal_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked));
855                 _button.signal_led_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked_event));
856                 c->Changed.connect (_connections, invalidator (*this), boost::bind (&Control::control_changed, this), gui_context ());
857                 if (c->alist ()) {
858                         c->alist()->automation_state_changed.connect (_connections, invalidator (*this), boost::bind (&Control::control_automation_state_changed, this), gui_context());
859                         control_automation_state_changed ();
860                 }
861
862         } else {
863
864                 _slider.set_name ("ProcessorControlSlider");
865                 _slider.set_text (_name);
866
867                 box.add (_slider);
868                 _slider.show ();
869
870                 const ARDOUR::ParameterDescriptor& desc = c->desc();
871                 double const lo        = c->internal_to_interface (desc.lower);
872                 double const up        = c->internal_to_interface (desc.upper);
873                 double const normal    = c->internal_to_interface (desc.normal);
874                 double const smallstep = c->internal_to_interface (desc.lower + desc.smallstep);
875                 double const largestep = c->internal_to_interface (desc.lower + desc.largestep);
876
877                 _adjustment.set_lower (lo);
878                 _adjustment.set_upper (up);
879                 _adjustment.set_step_increment (smallstep);
880                 _adjustment.set_page_increment (largestep);
881                 _slider.set_default_value (normal);
882
883                 _slider.StartGesture.connect(sigc::mem_fun(*this, &Control::start_touch));
884                 _slider.StopGesture.connect(sigc::mem_fun(*this, &Control::end_touch));
885
886                 _adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
887                 c->Changed.connect (_connections, invalidator (*this), boost::bind (&Control::control_changed, this), gui_context ());
888                 if (c->alist ()) {
889                         c->alist()->automation_state_changed.connect (_connections, invalidator (*this), boost::bind (&Control::control_automation_state_changed, this), gui_context());
890                         control_automation_state_changed ();
891                 }
892         }
893
894         control_changed ();
895         set_tooltip ();
896
897         /* We're providing our own PersistentTooltip */
898         set_no_tooltip_whatsoever (_slider);
899 }
900
901 ProcessorEntry::Control::~Control ()
902 {
903 }
904
905 void
906 ProcessorEntry::Control::set_tooltip ()
907 {
908         boost::shared_ptr<AutomationControl> c = _control.lock ();
909
910         if (!c) {
911                 return;
912         }
913         std::string tt = _name + ": " + ARDOUR::value_as_string (c->desc(), c->get_value ());
914         string sm = Gtkmm2ext::markup_escape_text (tt);
915         _slider_persistant_tooltip.set_tip (sm);
916         ArdourWidgets::set_tooltip (_button, Gtkmm2ext::markup_escape_text (sm));
917 }
918
919 void
920 ProcessorEntry::Control::slider_adjusted ()
921 {
922         if (_ignore_ui_adjustment) {
923                 return;
924         }
925
926         boost::shared_ptr<AutomationControl> c = _control.lock ();
927
928         if (!c) {
929                 return;
930         }
931
932         c->set_value ( c->interface_to_internal(_adjustment.get_value ()) , Controllable::NoGroup);
933         set_tooltip ();
934 }
935
936 void
937 ProcessorEntry::Control::start_touch ()
938 {
939         boost::shared_ptr<AutomationControl> c = _control.lock ();
940         if (!c) {
941                 return;
942         }
943         c->start_touch (c->session().transport_sample());
944 }
945
946 void
947 ProcessorEntry::Control::end_touch ()
948 {
949         boost::shared_ptr<AutomationControl> c = _control.lock ();
950         if (!c) {
951                 return;
952         }
953         c->stop_touch (c->session().transport_sample());
954 }
955
956 void
957 ProcessorEntry::Control::button_clicked ()
958 {
959         boost::shared_ptr<AutomationControl> c = _control.lock ();
960
961         if (!c) {
962                 return;
963         }
964
965         bool const n = _button.get_active ();
966
967         c->set_value (n ? 0 : 1, Controllable::NoGroup);
968         _button.set_active (!n);
969         set_tooltip ();
970 }
971
972 void
973 ProcessorEntry::Control::button_clicked_event (GdkEventButton *ev)
974 {
975         (void) ev;
976
977         button_clicked ();
978 }
979
980 void
981 ProcessorEntry::Control::control_automation_state_changed ()
982 {
983         boost::shared_ptr<AutomationControl> c = _control.lock ();
984         if (!c) {
985                 return;
986         }
987         bool x = c->alist()->automation_state() & Play;
988         if (c->toggled ()) {
989                 _button.set_sensitive (!x);
990         } else {
991                 _slider.set_sensitive (!x);
992         }
993 }
994
995 void
996 ProcessorEntry::Control::control_changed ()
997 {
998         boost::shared_ptr<AutomationControl> c = _control.lock ();
999         if (!c) {
1000                 return;
1001         }
1002
1003         _ignore_ui_adjustment = true;
1004
1005         if (c->toggled ()) {
1006                 _button.set_active (c->get_value() > 0.5);
1007         } else {
1008                 // Note: the _slider watches the controllable by itself
1009                 const double nval = c->internal_to_interface (c->get_value ());
1010                 if (_adjustment.get_value() != nval) {
1011                         _adjustment.set_value (nval);
1012                         set_tooltip ();
1013                 }
1014         }
1015
1016         _ignore_ui_adjustment = false;
1017 }
1018
1019 void
1020 ProcessorEntry::Control::add_state (XMLNode* node) const
1021 {
1022         XMLNode* c = new XMLNode (X_("Object"));
1023         c->set_property (X_("id"), state_id ());
1024         c->set_property (X_("visible"), _visible);
1025         node->add_child_nocopy (*c);
1026 }
1027
1028 void
1029 ProcessorEntry::Control::set_state (XMLNode const * node)
1030 {
1031         XMLNode* n = GUIObjectState::get_node (node, state_id ());
1032         if (n) {
1033                 bool visible;
1034                 if (n->get_property (X_("visible"), visible)) {
1035                         set_visible (visible);
1036                 }
1037         } else {
1038                 set_visible (false);
1039         }
1040 }
1041
1042 void
1043 ProcessorEntry::Control::set_visible (bool v)
1044 {
1045         if (v) {
1046                 box.show ();
1047         } else {
1048                 box.hide ();
1049         }
1050
1051         _visible = v;
1052 }
1053
1054 /** Called when the Editor might have re-shown things that
1055     we want hidden.
1056 */
1057 void
1058 ProcessorEntry::Control::hide_things ()
1059 {
1060         if (!_visible) {
1061                 box.hide ();
1062         }
1063 }
1064
1065 string
1066 ProcessorEntry::Control::state_id () const
1067 {
1068         boost::shared_ptr<AutomationControl> c = _control.lock ();
1069         assert (c);
1070
1071         return string_compose (X_("control %1"), c->id().to_s ());
1072 }
1073
1074 PluginInsertProcessorEntry::PluginInsertProcessorEntry (ProcessorBox* b, boost::shared_ptr<ARDOUR::PluginInsert> p, Width w)
1075         : ProcessorEntry (b, p, w)
1076         , _plugin_insert (p)
1077 {
1078         p->PluginIoReConfigure.connect (
1079                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1080                 );
1081         p->PluginMapChanged.connect (
1082                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1083                 );
1084         p->PluginConfigChanged.connect (
1085                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1086                 );
1087 }
1088
1089 void
1090 PluginInsertProcessorEntry::iomap_changed ()
1091 {
1092         _parent->setup_routing_feeds ();
1093         routing_icon.queue_draw();
1094         output_routing_icon.queue_draw();
1095 }
1096
1097 void
1098 PluginInsertProcessorEntry::hide_things ()
1099 {
1100         ProcessorEntry::hide_things ();
1101 }
1102
1103 ProcessorEntry::PortIcon::PortIcon(bool input) {
1104         _input = input;
1105         _ports = ARDOUR::ChanCount(ARDOUR::DataType::AUDIO, 1);
1106         set_size_request (-1, std::max (2.f, rintf(2.f * UIConfiguration::instance().get_ui_scale())));
1107 }
1108
1109 bool
1110 ProcessorEntry::PortIcon::on_expose_event (GdkEventExpose* ev)
1111 {
1112         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
1113
1114         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
1115         cairo_clip (cr);
1116
1117         Gtk::Allocation a = get_allocation();
1118         double const width = a.get_width();
1119         double const height = a.get_height();
1120
1121         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
1122         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
1123
1124         cairo_rectangle (cr, 0, 0, width, height);
1125         cairo_fill (cr);
1126
1127         const double dx = rint(max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1128         for (uint32_t i = 0; i < _ports.n_total(); ++i) {
1129                 set_routing_color (cr, i < _ports.n_midi());
1130                 const double x = ProcessorEntry::RoutingIcon::pin_x_pos (i, width, _ports.n_total(), 0 , false);
1131                 cairo_rectangle (cr, x - .5 - dx * .5, 0, 1 + dx, height);
1132                 cairo_fill(cr);
1133         }
1134
1135         cairo_destroy(cr);
1136         return true;
1137 }
1138
1139 ProcessorEntry::RoutingIcon::RoutingIcon (bool input)
1140         : _fed_by (false)
1141         , _input (input)
1142 {
1143         set_terminal (false);
1144 }
1145
1146 void
1147 ProcessorEntry::RoutingIcon::set_terminal (bool b) {
1148         _terminal = b;
1149         int h = std::max (8.f, rintf(8.f * sqrt (UIConfiguration::instance().get_ui_scale())));
1150         if (_terminal) {
1151                 h += std::max (4.f, rintf(4.f * sqrt (UIConfiguration::instance().get_ui_scale())));
1152         }
1153         set_size_request (-1, h);
1154 }
1155
1156 void
1157 ProcessorEntry::RoutingIcon::set (
1158                 const ARDOUR::ChanCount& in,
1159                 const ARDOUR::ChanCount& out,
1160                 const ARDOUR::ChanCount& sinks,
1161                 const ARDOUR::ChanCount& sources,
1162                 const ARDOUR::ChanMapping& in_map,
1163                 const ARDOUR::ChanMapping& out_map,
1164                 const ARDOUR::ChanMapping& thru_map)
1165 {
1166         _in       = in;
1167         _out      = out;
1168         _sources  = sources;
1169         _sinks    = sinks;
1170         _in_map   = in_map;
1171         _out_map  = out_map;
1172         _thru_map = thru_map;
1173 }
1174
1175 bool
1176 ProcessorEntry::RoutingIcon::in_identity () const {
1177         if (_thru_map.n_total () > 0) {
1178                 return false;
1179         }
1180         if (!_in_map.is_monotonic () || !_in_map.is_identity ()) {
1181                 return false;
1182         }
1183         if (_in_map.n_total () != _sinks.n_total () || _in.n_total () != _sinks.n_total ()) {
1184                 return false;
1185         }
1186         return true;
1187 }
1188
1189 bool
1190 ProcessorEntry::RoutingIcon::out_identity () const {
1191         if (_thru_map.n_total () > 0) {
1192                 // TODO skip if trhu is not connected to any of next's inputs
1193                 return false;
1194         }
1195         if (!_out_map.is_monotonic () || !_out_map.is_identity ()) {
1196                 return false;
1197         }
1198         if (_out_map.n_total () != _sources.n_total () || _out.n_total () != _sources.n_total ()) {
1199                 return false;
1200         }
1201         return true;
1202 }
1203
1204 bool
1205 ProcessorEntry::RoutingIcon::can_coalesce () const {
1206         if (_thru_map.n_total () > 0) {
1207                 return false;
1208         }
1209         if (_fed_by && _f_out != _f_sources) {
1210                 return false;
1211         }
1212         if (_fed_by && !_f_out_map.is_identity () && !_in_map.is_identity ()) {
1213                 return false;
1214         }
1215         if (_input && _sinks == _in && (!_fed_by || _f_out == _in)) {
1216                 return true;
1217         }
1218         return false;
1219 }
1220
1221 void
1222 ProcessorEntry::RoutingIcon::set_fed_by (
1223                                 const ARDOUR::ChanCount& out,
1224                                 const ARDOUR::ChanCount& sources,
1225                                 const ARDOUR::ChanMapping& out_map,
1226                                 const ARDOUR::ChanMapping& thru_map)
1227 {
1228         _f_out      = out;
1229         _f_sources  = sources;
1230         _f_out_map  = out_map;
1231         _f_thru_map = thru_map;
1232         _fed_by     = true;
1233 }
1234
1235 void
1236 ProcessorEntry::RoutingIcon::set_feeding (
1237                                 const ARDOUR::ChanCount& in,
1238                                 const ARDOUR::ChanCount& sinks,
1239                                 const ARDOUR::ChanMapping& in_map,
1240                                 const ARDOUR::ChanMapping& thru_map)
1241 {
1242         _i_in       = in;
1243         _i_sinks    = sinks;
1244         _i_in_map   = in_map;
1245         _i_thru_map = thru_map;
1246         _feeding    = true;
1247 }
1248
1249 double
1250 ProcessorEntry::RoutingIcon::pin_x_pos (uint32_t i, double width, uint32_t n_total, uint32_t n_midi, bool midi)
1251 {
1252         if (!midi) { i += n_midi; }
1253         if (n_total == 1) {
1254                 assert (i == 0);
1255                 return rint (width * .5) +.5;
1256         }
1257         return rint (width * (.15 + .7 * i / (n_total - 1))) + .5;
1258 }
1259
1260 void
1261 ProcessorEntry::RoutingIcon::draw_gnd (cairo_t* cr, double x0, double y0, double height, bool midi)
1262 {
1263         const double dx = 1 + rint (max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1264         const double y1 = rint (height * .66) + .5;
1265
1266         cairo_save (cr);
1267         cairo_translate (cr, x0, y0);
1268         cairo_move_to (cr, 0, height);
1269         cairo_line_to (cr, 0, y1);
1270         cairo_move_to (cr, 0 - dx, y1);
1271         cairo_line_to (cr, 0 + dx, y1);
1272
1273         set_routing_color (cr, midi);
1274         cairo_set_line_width (cr, 1.0);
1275         cairo_stroke (cr);
1276         cairo_restore (cr);
1277 }
1278
1279 void
1280 ProcessorEntry::RoutingIcon::draw_sidechain (cairo_t* cr, double x0, double y0, double height, bool midi)
1281 {
1282         const double dx = 1 + rint (max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1283         const double y1 = rint (height * .5) - .5;
1284
1285         cairo_save (cr);
1286         cairo_translate (cr, x0, y0);
1287         cairo_move_to (cr, 0 - dx, height);
1288         cairo_line_to (cr, 0, y1);
1289         cairo_line_to (cr, 0 + dx, height);
1290         cairo_close_path (cr);
1291
1292         set_routing_color (cr, midi);
1293         cairo_fill (cr);
1294         cairo_restore (cr);
1295 }
1296
1297 void
1298 ProcessorEntry::RoutingIcon::draw_thru_src (cairo_t* cr, double x0, double y0, double height, bool midi)
1299 {
1300         const double rad = 1;
1301         const double y1 = height - rad - 1.5;
1302
1303         cairo_arc (cr, x0, y0 + y1, rad, 0, 2. * M_PI);
1304         cairo_move_to (cr, x0, y0 + height - 1.5);
1305         cairo_line_to (cr, x0, y0 + height);
1306         set_routing_color (cr, midi);
1307         cairo_set_line_width  (cr, 1.0);
1308         cairo_stroke (cr);
1309 }
1310
1311 void
1312 ProcessorEntry::RoutingIcon::draw_thru_sink (cairo_t* cr, double x0, double y0, double height, bool midi)
1313 {
1314         const double rad = 1;
1315         const double y1 = rad + 1;
1316
1317         cairo_arc (cr, x0, y0 + y1, rad, 0, 2. * M_PI);
1318         cairo_move_to (cr, x0, y0);
1319         cairo_line_to (cr, x0, y0 + 1);
1320         set_routing_color (cr, midi);
1321         cairo_set_line_width  (cr, 1.0);
1322         cairo_stroke (cr);
1323 }
1324
1325 void
1326 ProcessorEntry::RoutingIcon::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool dashed)
1327 {
1328         double bz = abs (y1 - y0);
1329
1330         cairo_move_to (cr, x0, y0);
1331         cairo_curve_to (cr, x0, y0 + bz, x1, y1 - bz, x1, y1);
1332         cairo_set_line_width  (cr, 1.0);
1333         cairo_set_line_cap  (cr,  CAIRO_LINE_CAP_ROUND);
1334         cairo_set_source_rgb (cr, 1, 0, 0);
1335         if (dashed) {
1336                 const double dashes[] = { 2, 3 };
1337                 cairo_set_dash (cr, dashes, 2, 0);
1338         }
1339         set_routing_color (cr, midi);
1340         cairo_stroke (cr);
1341         if (dashed) {
1342                 cairo_set_dash (cr, 0, 0, 0);
1343         }
1344 }
1345
1346 bool
1347 ProcessorEntry::RoutingIcon::on_expose_event (GdkEventExpose* ev)
1348 {
1349         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
1350
1351         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
1352         cairo_clip (cr);
1353
1354         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1355
1356         Gtk::Allocation a = get_allocation();
1357         double const width = a.get_width();
1358         double const height = a.get_height();
1359
1360         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
1361         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
1362
1363         cairo_rectangle (cr, 0, 0, width, height);
1364         cairo_fill (cr);
1365
1366         if (_input) {
1367                 if (can_coalesce ()) {
1368                         expose_coalesced_input_map (cr, width, height);
1369                 } else {
1370                         expose_input_map (cr, width, height);
1371                 }
1372         } else {
1373                 expose_output_map (cr, width, height);
1374         }
1375
1376         cairo_destroy(cr);
1377         return true;
1378 }
1379
1380 void
1381 ProcessorEntry::RoutingIcon::expose_coalesced_input_map (cairo_t* cr, const double width, const double height)
1382 {
1383         const uint32_t pc_in = _sinks.n_total();
1384         const uint32_t pc_in_midi = _sinks.n_midi();
1385
1386         for (uint32_t i = 0; i < pc_in; ++i) {
1387                 const bool is_midi = i < pc_in_midi;
1388                 bool valid_in;
1389                 uint32_t pn = is_midi ? i : i - pc_in_midi;
1390                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1391                 uint32_t idx = _in_map.get (dt, pn, &valid_in);
1392                 if (!valid_in) {
1393                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1394                         draw_gnd (cr, x, 0, height, is_midi);
1395                         continue;
1396                 }
1397                 if (idx >= _in.get (dt)) {
1398                         // side-chain, probably
1399                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1400                         draw_sidechain (cr, x, 0, height, is_midi);
1401                         continue;
1402                 }
1403                 double c_x0;
1404                 double c_x1 = pin_x_pos (i, width, pc_in, 0, false);
1405
1406                 if (_fed_by) {
1407                         bool valid_src;
1408                         uint32_t src = _f_out_map.get_src (dt, idx, &valid_src);
1409                         if (!valid_src) {
1410                                 double x = pin_x_pos (i, width, pc_in, 0, false);
1411                                 bool valid_thru;
1412                                 _f_thru_map.get (dt, idx, &valid_thru);
1413                                 if (valid_thru) {
1414                                         draw_thru_src (cr, x, 0, height, is_midi);
1415                                 } else {
1416                                         draw_gnd (cr, x, 0, height, is_midi);
1417                                 }
1418                                 continue;
1419                         }
1420                         c_x0 = pin_x_pos (src, width, _f_sources.n_total(), _f_sources.n_midi(), is_midi);
1421                 } else {
1422                         c_x0 = pin_x_pos (idx, width, _in.n_total(), _in.n_midi(), is_midi);
1423                 }
1424                 draw_connection (cr, c_x0, c_x1, 0, height, is_midi);
1425         }
1426 }
1427
1428 void
1429 ProcessorEntry::RoutingIcon::expose_input_map (cairo_t* cr, const double width, const double height)
1430 {
1431         const uint32_t n_in = _in.n_total();
1432         const uint32_t n_in_midi = _in.n_midi();
1433         const uint32_t pc_in = _sinks.n_total();
1434         const uint32_t pc_in_midi = _sinks.n_midi();
1435
1436         // draw inputs to this
1437         for (uint32_t i = 0; i < pc_in; ++i) {
1438                 const bool is_midi = i < pc_in_midi;
1439                 bool valid_in;
1440                 uint32_t pn = is_midi ? i : i - pc_in_midi;
1441                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1442                 uint32_t idx = _in_map.get (dt, pn, &valid_in);
1443                 // check if it's fed
1444                 bool valid_src = true;
1445                 if (valid_in && idx < _in.get (dt) && _fed_by) {
1446                         bool valid_out;
1447                         bool valid_thru;
1448                         _f_out_map.get_src (dt, idx, &valid_out);
1449                         _f_thru_map.get (dt, idx, &valid_thru);
1450                         if (!valid_out && !valid_thru) {
1451                                 valid_src = false;
1452                         }
1453                 }
1454                 if (!valid_in || !valid_src) {
1455                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1456                         draw_gnd (cr, x, 0, height, is_midi);
1457                         continue;
1458                 }
1459                 if (idx >= _in.get (dt)) {
1460                         // side-chain, probably
1461                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1462                         draw_sidechain (cr, x, 0, height, is_midi);
1463                         continue;
1464                 }
1465                 double c_x1 = pin_x_pos (i, width, pc_in, 0, false);
1466                 double c_x0 = pin_x_pos (idx, width, n_in, n_in_midi, is_midi);
1467                 draw_connection (cr, c_x0, c_x1, 0, height, is_midi);
1468         }
1469
1470         // draw reverse thru
1471         for (uint32_t i = 0; i < n_in; ++i) {
1472                 const bool is_midi = i < n_in_midi;
1473                 bool valid_thru;
1474                 uint32_t pn = is_midi ? i : i - n_in_midi;
1475                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1476                 _thru_map.get_src (dt, pn, &valid_thru);
1477                 if (!valid_thru) {
1478                         continue;
1479                 }
1480                 double x = pin_x_pos (i, width, n_in, 0, is_midi);
1481                 draw_thru_sink (cr, x, 0, height, is_midi);
1482         }
1483 }
1484
1485 void
1486 ProcessorEntry::RoutingIcon::expose_output_map (cairo_t* cr, const double width, const double height)
1487 {
1488         int dh = std::max (4.f, rintf(4.f * UIConfiguration::instance().get_ui_scale()));
1489         double ht = _terminal ? height - dh : height;
1490
1491         // draw outputs of this
1492         const uint32_t pc_out = _sources.n_total();
1493         const uint32_t pc_out_midi = _sources.n_midi();
1494         const uint32_t n_out = _out.n_total();
1495         const uint32_t n_out_midi = _out.n_midi();
1496
1497         for (uint32_t i = 0; i < pc_out; ++i) {
1498                 const bool is_midi = i < pc_out_midi;
1499                 bool valid_out;
1500                 uint32_t pn = is_midi ? i : i - pc_out_midi;
1501                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1502                 uint32_t idx = _out_map.get (dt, pn, &valid_out);
1503                 if (!valid_out) {
1504                         continue;
1505                 }
1506                 // skip connections that are not used in the next's input
1507                 if (_feeding) {
1508                         bool valid_thru, valid_sink;
1509                         _i_in_map.get_src (dt, idx, &valid_sink);
1510                         _i_thru_map.get_src (dt, idx, &valid_thru);
1511                         if (!valid_thru && !valid_sink) {
1512                                 if (!is_midi || i != 0) { // special case midi-bypass
1513                                         continue;
1514                                 }
1515                         }
1516                 }
1517                 double c_x0 = pin_x_pos (i, width, pc_out, 0, false);
1518                 double c_x1 = pin_x_pos (idx, width, n_out, n_out_midi, is_midi);
1519                 draw_connection (cr, c_x0, c_x1, 0, ht, is_midi);
1520         }
1521
1522         for (uint32_t i = 0; i < n_out; ++i) {
1523                 const bool is_midi = i < n_out_midi;
1524                 uint32_t pn = is_midi ? i : i - n_out_midi;
1525                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1526                 double x = pin_x_pos (i, width, n_out, 0, is_midi);
1527
1528                 if (!_terminal) {
1529                         bool valid_thru_f = false;
1530                         // skip connections that are not used in the next's input
1531                         if (_feeding) {
1532                                 bool valid_sink;
1533                                 _i_in_map.get_src (dt, pn, &valid_sink);
1534                                 _i_thru_map.get_src (dt, pn, &valid_thru_f);
1535                                 if (!valid_thru_f && !valid_sink) {
1536                                         if (!is_midi || i != 0) { // special case midi-bypass
1537                                                 continue;
1538                                         }
1539                                 }
1540                         }
1541
1542                         bool valid_src;
1543                         _out_map.get_src (dt, pn, &valid_src);
1544                         if (!valid_src) {
1545                                 bool valid_thru;
1546                                 uint32_t idx = _thru_map.get (dt, pn, &valid_thru);
1547                                 if (valid_thru) {
1548                                         if (idx >= _in.get (dt)) {
1549                                                 draw_sidechain (cr, x, 0, height, is_midi);
1550                                         } else {
1551                                                 draw_thru_src (cr, x, 0, height, is_midi);
1552                                         }
1553                                 } else if (valid_thru_f){
1554                                         // gnd is part of input, unless it's a thru input
1555                                         // (also only true if !coalesced into one small display)
1556                                         draw_gnd (cr, x, 0, height, is_midi);
1557                                 }
1558                         }
1559                 } else {
1560                         // terminal node, add arrows
1561                         bool valid_src;
1562                         _out_map.get_src (dt, pn, &valid_src);
1563                         if (!valid_src) {
1564                                 bool valid_thru;
1565                                 uint32_t idx = _thru_map.get (dt, pn, &valid_thru);
1566                                 if (valid_thru) {
1567                                         if (idx >= _in.get (dt)) {
1568                                                 draw_sidechain (cr, x, 0, height - dh, is_midi);
1569                                         } else {
1570                                                 draw_thru_src (cr, x, 0, height - dh, is_midi);
1571                                         }
1572                                 } else {
1573                                         draw_gnd (cr, x, 0, height - dh, is_midi);
1574                                 }
1575                         }
1576
1577                         set_routing_color (cr, is_midi);
1578                         cairo_set_line_width (cr, 1.0);
1579                         cairo_move_to (cr, x, height - dh);
1580                         cairo_line_to (cr, x, height - 2);
1581                         cairo_stroke (cr);
1582
1583                         const double ar = dh - 1;
1584                         cairo_move_to (cr, x - ar, height - ar);
1585                         cairo_line_to (cr, x     , height - .5);
1586                         cairo_line_to (cr, x + ar, height - ar);
1587                         cairo_line_to (cr, x     , height - ar * .5);
1588                         cairo_close_path (cr);
1589                         cairo_fill_preserve (cr);
1590                         cairo_stroke (cr);
1591                 }
1592         }
1593 }
1594
1595 ProcessorEntry::PluginInlineDisplay::PluginInlineDisplay (ProcessorEntry& e, boost::shared_ptr<ARDOUR::Plugin> p, uint32_t max_height)
1596         : PluginDisplay (p, max_height)
1597         , _entry (e)
1598         , _scroll (false)
1599         , _given_max_height (max_height)
1600 {
1601         std::string postfix = string_compose(_("\n%1+double-click to toggle inline-display"), Keyboard::tertiary_modifier_name ());
1602
1603         if (_plug->has_editor()) {
1604                 set_tooltip (*this,
1605                                 string_compose (_("<b>%1</b>\nDouble-click to show GUI.\n%2+double-click to show generic GUI.%3"), e.name (Wide), Keyboard::primary_modifier_name (), postfix));
1606         } else {
1607                 set_tooltip (*this,
1608                                 string_compose (_("<b>%1</b>\nDouble-click to show generic GUI.%2"), e.name (Wide), postfix));
1609         }
1610 }
1611
1612
1613 bool
1614 ProcessorEntry::PluginInlineDisplay::on_button_press_event (GdkEventButton *ev)
1615 {
1616         assert (_entry.processor ());
1617
1618         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_entry.processor());
1619         // duplicated code :(
1620         // consider some tweaks to pass this up to the DnDVBox somehow:
1621         // select processor, then call (private)
1622         //_entry._parent->processor_button_press_event (ev, &_entry);
1623         if (pi && pi->plugin() && pi->plugin()->has_inline_display()
1624                         && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)
1625                         && ev->button == 1
1626                         && ev->type == GDK_2BUTTON_PRESS) {
1627                 _entry.toggle_inline_display_visibility ();
1628                 return true;
1629         }
1630         else if (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS)) {
1631                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
1632                         _entry._parent->generic_edit_processor (_entry.processor ());
1633                 } else {
1634                         _entry._parent->edit_processor (_entry.processor ());
1635                 }
1636                 return true;
1637         }
1638         return false;
1639 }
1640
1641 void
1642 ProcessorEntry::PluginInlineDisplay::on_size_request (Requisition* req)
1643 {
1644         req->width = 56;
1645         req->height = _cur_height;
1646 }
1647
1648
1649 void
1650 ProcessorEntry::PluginInlineDisplay::update_height_alloc (uint32_t inline_height)
1651 {
1652         /* work-around scroll-bar + aspect ratio
1653          * show inline-view -> height changes -> scrollbar gets added
1654          * -> width changes -> inline-view, fixed aspect ratio -> height changes
1655          * -> scroll bar is removed [-> width changes ; repeat ]
1656          */
1657         uint32_t shm = std::min (_max_height, inline_height);
1658         bool sc = false;
1659         Gtk::Container* pr = get_parent();
1660         for (uint32_t i = 0; i < 4 && pr; ++i) {
1661                 // VBox, EventBox, ViewPort, ScrolledWindow
1662                 pr = pr->get_parent();
1663         }
1664         Gtk::ScrolledWindow* sw = dynamic_cast<Gtk::ScrolledWindow*> (pr);
1665         if (sw) {
1666                 const Gtk::VScrollbar* vsb = sw->get_vscrollbar();
1667                 sc = vsb && vsb->is_visible();
1668         }
1669
1670         if (shm != _cur_height) {
1671                 queue_resize ();
1672                 if (!_scroll && sc) {
1673                         _max_height = shm;
1674                 } else {
1675                         _max_height = _given_max_height;
1676                 }
1677                 _cur_height = shm;
1678         }
1679
1680         _scroll = sc;
1681 }
1682
1683 void
1684 ProcessorEntry::PluginInlineDisplay::display_frame (cairo_t* cr, double w, double h)
1685 {
1686         Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, w - 1, h + 1, 7);
1687 }
1688
1689 ProcessorEntry::LuaPluginDisplay::LuaPluginDisplay (ProcessorEntry& e, boost::shared_ptr<ARDOUR::LuaProc> p, uint32_t max_height)
1690         : PluginInlineDisplay (e, p, max_height)
1691         , _luaproc (p)
1692         , _lua_render_inline (0)
1693 {
1694         p->setup_lua_inline_gui (&lua_gui);
1695
1696         lua_State* LG = lua_gui.getState ();
1697         LuaInstance::bind_cairo (LG);
1698         luabridge::LuaRef lua_render = luabridge::getGlobal (LG, "render_inline");
1699         assert (lua_render.isFunction ());
1700         _lua_render_inline = new luabridge::LuaRef (lua_render);
1701 }
1702
1703 ProcessorEntry::LuaPluginDisplay::~LuaPluginDisplay ()
1704 {
1705         delete (_lua_render_inline);
1706 }
1707
1708 uint32_t
1709 ProcessorEntry::LuaPluginDisplay::render_inline (cairo_t *cr, uint32_t width)
1710 {
1711         Cairo::Context ctx (cr);
1712         try {
1713                 luabridge::LuaRef rv = (*_lua_render_inline)((Cairo::Context *)&ctx, width, _max_height);
1714                 lua_gui.collect_garbage_step ();
1715                 if (rv.isTable ()) {
1716                         uint32_t h = rv[2];
1717                         return h;
1718                 }
1719         } catch (luabridge::LuaException const& e) {
1720 #ifndef NDEBUG
1721                 cerr << "LuaException:" << e.what () << endl;
1722 #endif
1723         } catch (...) { }
1724         return 0;
1725 }
1726
1727
1728 static std::list<Gtk::TargetEntry> drop_targets()
1729 {
1730         std::list<Gtk::TargetEntry> tmp;
1731         tmp.push_back (Gtk::TargetEntry ("processor")); // from processor-box to processor-box
1732         tmp.push_back (Gtk::TargetEntry ("PluginInfoPtr")); // from plugin-manager
1733         tmp.push_back (Gtk::TargetEntry ("PluginFavoritePtr")); // from sidebar
1734         return tmp;
1735 }
1736
1737 static std::list<Gtk::TargetEntry> drag_targets()
1738 {
1739         std::list<Gtk::TargetEntry> tmp;
1740         tmp.push_back (Gtk::TargetEntry ("PluginPresetPtr")); // to sidebar (optional preset)
1741         tmp.push_back (Gtk::TargetEntry ("processor")); // to processor-box (copy)
1742         return tmp;
1743 }
1744
1745 static std::list<Gtk::TargetEntry> drag_targets_noplugin()
1746 {
1747         std::list<Gtk::TargetEntry> tmp;
1748         tmp.push_back (Gtk::TargetEntry ("processor")); // to processor box (sends, faders re-order)
1749         return tmp;
1750 }
1751
1752 ProcessorBox::ProcessorBox (ARDOUR::Session* sess, boost::function<PluginSelector*()> get_plugin_selector,
1753                             ProcessorSelection& psel, MixerStrip* parent, bool owner_is_mixer)
1754         : _parent_strip (parent)
1755         , _owner_is_mixer (owner_is_mixer)
1756         , ab_direction (true)
1757         , _get_plugin_selector (get_plugin_selector)
1758         , _placement (-1)
1759         , _p_selection(psel)
1760         , processor_display (drop_targets())
1761         , _redisplay_pending (false)
1762 {
1763         set_session (sess);
1764
1765         /* ProcessorBox actions and bindings created statically by call to
1766          * ProcessorBox::register_actions(), made by ARDOUR_UI so that actions
1767          * are available for context menus.
1768          */
1769
1770         processor_display.set_data ("ardour-bindings", bindings);
1771
1772         _width = Wide;
1773         processor_menu = 0;
1774         no_processor_redisplay = false;
1775
1776         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1777         processor_scroller.add (processor_display);
1778         pack_start (processor_scroller, true, true);
1779
1780         processor_display.set_flags (CAN_FOCUS);
1781         processor_display.set_name ("ProcessorList");
1782         processor_display.set_data ("processorbox", this);
1783         processor_display.set_size_request (48, -1);
1784         processor_display.set_spacing (0);
1785
1786         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
1787         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
1788
1789         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
1790         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
1791
1792         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
1793         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
1794         processor_display.DropFromExternal.connect (sigc::mem_fun (*this, &ProcessorBox::plugin_drop));
1795
1796         processor_scroller.show ();
1797         processor_display.show ();
1798
1799         if (parent) {
1800                 parent->DeliveryChanged.connect (
1801                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
1802                         );
1803         }
1804
1805         set_tooltip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
1806 }
1807
1808 ProcessorBox::~ProcessorBox ()
1809 {
1810         /* it may appear as if we should delete processor_menu but that is a
1811          * pointer to a widget owned by the UI Manager, and has potentially
1812          * be returned to many other ProcessorBoxes. GTK doesn't really make
1813          * clear the ownership of this widget, which is a menu and thus is
1814          * never packed into any container other than an implict GtkWindow.
1815          *
1816          * For now, until or if we ever get clarification over the ownership
1817          * story just let it continue to exist. At worst, its a small memory leak.
1818          */
1819 }
1820
1821 void
1822 ProcessorBox::set_route (boost::shared_ptr<Route> r)
1823 {
1824         if (_route == r) {
1825                 return;
1826         }
1827
1828         _route_connections.drop_connections();
1829
1830         /* new route: any existing block on processor redisplay must be meaningless */
1831         no_processor_redisplay = false;
1832         _route = r;
1833
1834         _route->processors_changed.connect (
1835                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
1836                 );
1837
1838         _route->DropReferences.connect (
1839                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
1840                 );
1841
1842         _route->PropertyChanged.connect (
1843                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
1844                 );
1845
1846         redisplay_processors ();
1847 }
1848
1849 void
1850 ProcessorBox::route_going_away ()
1851 {
1852         no_processor_redisplay = true;
1853         processor_display.clear ();
1854         _route.reset ();
1855 }
1856
1857 boost::shared_ptr<Processor>
1858 ProcessorBox::find_drop_position (ProcessorEntry* position)
1859 {
1860         boost::shared_ptr<Processor> p;
1861         if (position) {
1862                 p = position->processor ();
1863                 if (!p) {
1864                         /* dropped on the blank entry (which will be before the
1865                                  fader), so use the first non-blank child as our
1866                                  `dropped on' processor */
1867                         list<ProcessorEntry*> c = processor_display.children ();
1868                         list<ProcessorEntry*>::iterator i = c.begin ();
1869
1870                         assert (i != c.end ());
1871                         p = (*i)->processor ();
1872                         assert (p);
1873                 }
1874         }
1875         return p;
1876 }
1877
1878 void
1879 ProcessorBox::_drop_plugin_preset (Gtk::SelectionData const &data, Route::ProcessorList &pl)
1880 {
1881                 const void * d = data.get_data();
1882                 const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
1883
1884                 PluginPresetList nfos;
1885                 TreeView* source;
1886                 tv->get_object_drag_data (nfos, &source);
1887
1888                 for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1889                         PluginPresetPtr ppp = (*i);
1890                         PluginInfoPtr pip = ppp->_pip;
1891                         PluginPtr p = pip->load (*_session);
1892                         if (!p) {
1893                                 continue;
1894                         }
1895
1896                         if (ppp->_preset.valid) {
1897                                 p->load_preset (ppp->_preset);
1898                         }
1899
1900                         boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
1901                         if (Config->get_new_plugins_active ()) {
1902                                 processor->enable (true);
1903                         }
1904                         pl.push_back (processor);
1905                 }
1906 }
1907
1908 void
1909 ProcessorBox::_drop_plugin (Gtk::SelectionData const &data, Route::ProcessorList &pl)
1910 {
1911                 const void * d = data.get_data();
1912                 const Gtkmm2ext::DnDTreeView<ARDOUR::PluginInfoPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginInfoPtr>*>(d);
1913                 PluginInfoList nfos;
1914
1915                 TreeView* source;
1916                 tv->get_object_drag_data (nfos, &source);
1917
1918                 for (list<PluginInfoPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1919                         PluginPtr p = (*i)->load (*_session);
1920                         if (!p) {
1921                                 continue;
1922                         }
1923                         boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
1924                         if (Config->get_new_plugins_active ()) {
1925                                 processor->enable (true);
1926                         }
1927                         pl.push_back (processor);
1928                 }
1929 }
1930
1931 void
1932 ProcessorBox::plugin_drop (Gtk::SelectionData const &data, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
1933 {
1934         if (!_session) {
1935                 return;
1936         }
1937
1938         boost::shared_ptr<Processor> p = find_drop_position (position);
1939         Route::ProcessorList pl;
1940
1941         if (data.get_target() == "PluginInfoPtr") {
1942                 _drop_plugin (data, pl);
1943         }
1944         else if (data.get_target() == "PluginFavoritePtr") {
1945                 _drop_plugin_preset (data, pl);
1946         }
1947         else {
1948                 return;
1949         }
1950
1951         Route::ProcessorStreams err;
1952         if (_route->add_processors (pl, p, &err)) {
1953                 string msg = _(
1954                                 "Processor Drag/Drop failed. Probably because\n\
1955 the I/O configuration of the plugins could\n\
1956 not match the configuration of this track.");
1957                 MessageDialog am (msg);
1958                 am.run ();
1959         }
1960 }
1961
1962 void
1963 ProcessorBox::object_drop (DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
1964 {
1965         if (Gdk::ACTION_LINK == context->get_selected_action()) {
1966                 list<ProcessorEntry*> children = source->selection ();
1967                 assert (children.size() == 1);
1968                 ProcessorEntry* other = *children.begin();
1969                 assert (other->can_copy_state (position));
1970                 boost::shared_ptr<ARDOUR::Processor> otherproc = other->processor();
1971                 boost::shared_ptr<ARDOUR::Processor> proc = position->processor();
1972                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
1973                 assert (otherproc && proc && pi);
1974
1975                 PBD::ID id = pi->id();
1976                 XMLNode& state = otherproc->get_state ();
1977                 /* strip side-chain state (processor inside processor must be a side-chain)
1978                  * otherwise we'll end up with duplicate ports-names.
1979                  * (this needs a better solution which retains connections)
1980                  */
1981                 state.remove_nodes_and_delete ("Processor");
1982                 proc->set_state (state, Stateful::loading_state_version);
1983                 boost::dynamic_pointer_cast<PluginInsert>(proc)->update_id (id);
1984                 return;
1985         }
1986
1987         boost::shared_ptr<Processor> p = find_drop_position (position);
1988
1989         list<ProcessorEntry*> children = source->selection (true);
1990         list<boost::shared_ptr<Processor> > procs;
1991         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1992                 if ((*i)->processor ()) {
1993                         if (boost::dynamic_pointer_cast<UnknownProcessor> ((*i)->processor())) {
1994                                 continue;
1995                         }
1996                         if (boost::dynamic_pointer_cast<PortInsert> ((*i)->processor())) {
1997                                 continue;
1998                         }
1999                         procs.push_back ((*i)->processor ());
2000                 }
2001         }
2002
2003         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
2004                 XMLNode& state = (*i)->get_state ();
2005                 XMLNodeList nlist;
2006                 nlist.push_back (&state);
2007                 paste_processor_state (nlist, p);
2008                 delete &state;
2009         }
2010
2011         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
2012            ourselves.
2013         */
2014
2015         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
2016                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
2017                 if (other) {
2018                         other->delete_dragged_processors (procs);
2019                 }
2020         }
2021 }
2022
2023 void
2024 ProcessorBox::set_width (Width w)
2025 {
2026         if (_width == w) {
2027                 return;
2028         }
2029
2030         _width = w;
2031
2032         list<ProcessorEntry*> children = processor_display.children ();
2033         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
2034                 (*i)->set_enum_width (w);
2035         }
2036
2037         queue_resize ();
2038 }
2039
2040 Gtk::Menu*
2041 ProcessorBox::build_possible_aux_menu ()
2042 {
2043         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2044
2045         if (rl->empty()) {
2046                 /* No aux sends if there are no busses */
2047                 return 0;
2048         }
2049
2050         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2051                 return 0;
2052         }
2053
2054         using namespace Menu_Helpers;
2055         Menu* menu = manage (new Menu);
2056         MenuList& items = menu->items();
2057
2058         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2059                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2060                         /* don't allow sending to master or monitor or to self */
2061                         continue;
2062                 }
2063                 if ((*r)->is_foldbackbus ()) {
2064                         continue;
2065                 }
2066                 if (_route->internal_send_for (*r)) {
2067                         /* aux-send to target already exists */
2068                         continue;
2069                 }
2070                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
2071         }
2072
2073         return menu;
2074 }
2075
2076 Gtk::Menu*
2077 ProcessorBox::build_possible_listener_menu ()
2078 {
2079         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2080
2081         if (rl->empty()) {
2082                 /* No aux sends if there are no busses */
2083                 return 0;
2084         }
2085
2086         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2087                 return 0;
2088         }
2089
2090         using namespace Menu_Helpers;
2091         Menu* menu = manage (new Menu);
2092         MenuList& items = menu->items();
2093
2094         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2095                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2096                         /* don't allow sending to master or monitor or to self */
2097                         continue;
2098                 }
2099                 if (!(*r)->is_foldbackbus ()) {
2100                         continue;
2101                 }
2102                 if (_route->internal_send_for (*r)) {
2103                         /* aux-send to target already exists */
2104                         continue;
2105                 }
2106                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
2107         }
2108
2109         return menu;
2110 }
2111
2112 Gtk::Menu*
2113 ProcessorBox::build_possible_remove_listener_menu ()
2114 {
2115         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2116
2117         if (rl->empty()) {
2118                 /* No aux sends if there are no busses */
2119                 return 0;
2120         }
2121
2122         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2123                 return 0;
2124         }
2125
2126         using namespace Menu_Helpers;
2127         Menu* menu = manage (new Menu);
2128         MenuList& items = menu->items();
2129
2130         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2131                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2132                         /* don't allow sending to master or monitor or to self */
2133                         continue;
2134                 }
2135                 if (!(*r)->is_foldbackbus ()) {
2136                         continue;
2137                 }
2138                 if (!_route->internal_send_for (*r)) {
2139                         /* aux-send to target already exists */
2140                         continue;
2141                 }
2142                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_remove_aux), boost::weak_ptr<Route>(*r))));
2143         }
2144
2145         return menu;
2146 }
2147
2148 void
2149 ProcessorBox::show_processor_menu (int arg)
2150 {
2151         if (processor_menu == 0) {
2152                 processor_menu = build_processor_menu ();
2153                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
2154         }
2155
2156         /* Sort out the plugin submenu */
2157
2158         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
2159
2160         if (plugin_menu_item) {
2161                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
2162         }
2163
2164         /* And the aux submenu */
2165
2166         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
2167
2168         if (aux_menu_item) {
2169                 Menu* m = build_possible_aux_menu();
2170                 if (m && !m->items().empty()) {
2171                         aux_menu_item->set_submenu (*m);
2172                         aux_menu_item->set_sensitive (true);
2173                 } else {
2174                         delete m;
2175                         /* stupid gtkmm: we need to pass a null reference here */
2176                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
2177                         aux_menu_item->set_sensitive (false);
2178                 }
2179         }
2180
2181         Gtk::MenuItem* listen_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newlisten"));
2182
2183         if (listen_menu_item) {
2184                 Menu* m = build_possible_listener_menu();
2185                 if (m && !m->items().empty()) {
2186                         listen_menu_item->set_submenu (*m);
2187                         listen_menu_item->set_sensitive (true);
2188                 } else {
2189                         delete m;
2190                         /* stupid gtkmm: we need to pass a null reference here */
2191                         gtk_menu_item_set_submenu (listen_menu_item->gobj(), 0);
2192                         listen_menu_item->set_sensitive (false);
2193                 }
2194         }
2195
2196         Gtk::MenuItem* remove_listen_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/removelisten"));
2197
2198         if (remove_listen_menu_item) {
2199                 Menu* m = build_possible_remove_listener_menu();
2200                 if (m && !m->items().empty()) {
2201                         remove_listen_menu_item->set_submenu (*m);
2202                         remove_listen_menu_item->set_sensitive (true);
2203                 } else {
2204                         delete m;
2205                         /* stupid gtkmm: we need to pass a null reference here */
2206                         gtk_menu_item_set_submenu (remove_listen_menu_item->gobj(), 0);
2207                         remove_listen_menu_item->set_sensitive (false);
2208                 }
2209         }
2210
2211         ActionManager::get_action (X_("ProcessorMenu"), "newinsert")->set_sensitive (!_route->is_monitor () && !_route->is_foldbackbus ());
2212         ActionManager::get_action (X_("ProcessorMenu"), "newsend")->set_sensitive (!_route->is_monitor () && !_route->is_foldbackbus ());
2213
2214         ProcessorEntry* single_selection = 0;
2215         if (processor_display.selection().size() == 1) {
2216                 single_selection = processor_display.selection().front();
2217         }
2218
2219         /* And the controls submenu */
2220
2221         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
2222
2223         if (controls_menu_item) {
2224                 if (single_selection) {
2225                         Menu* m = single_selection->build_controls_menu ();
2226                         if (m && !m->items().empty()) {
2227                                 controls_menu_item->set_submenu (*m);
2228                                 controls_menu_item->set_sensitive (true);
2229                         } else {
2230                                 delete m;
2231                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
2232                                 controls_menu_item->set_sensitive (false);
2233                         }
2234                 } else {
2235                         controls_menu_item->set_sensitive (false);
2236                 }
2237         }
2238
2239
2240         Gtk::MenuItem* send_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/send_options"));
2241         if (send_menu_item) {
2242                 if (single_selection && !_route->is_monitor()) {
2243                         Menu* m = single_selection->build_send_options_menu ();
2244                         if (m && !m->items().empty()) {
2245                                 send_menu_item->set_submenu (*m);
2246                                 send_menu_item->set_sensitive (true);
2247                         } else {
2248                                 delete m;
2249                                 gtk_menu_item_set_submenu (send_menu_item->gobj(), 0);
2250                                 send_menu_item->set_sensitive (false);
2251                         }
2252                 } else {
2253                         send_menu_item->set_sensitive (false);
2254                 }
2255         }
2256
2257         /* Sensitise actions as approprioate */
2258
2259         const bool sensitive = !processor_display.selection().empty() && ! stub_processor_selected ();
2260
2261         paste_action->set_sensitive (!_p_selection.processors.empty());
2262         cut_action->set_sensitive (sensitive && can_cut ());
2263         copy_action->set_sensitive (sensitive);
2264         delete_action->set_sensitive (sensitive || stub_processor_selected ());
2265         backspace_action->set_sensitive (sensitive || stub_processor_selected ());
2266
2267         edit_action->set_sensitive (one_processor_can_be_edited ());
2268         edit_generic_action->set_sensitive (one_processor_can_be_edited ());
2269
2270         boost::shared_ptr<PluginInsert> pi;
2271         if (single_selection) {
2272                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
2273         }
2274
2275         manage_pins_action->set_sensitive (pi != 0);
2276         if (boost::dynamic_pointer_cast<Track>(_route)) {
2277                 disk_io_action->set_sensitive (true);
2278         } else {
2279                 disk_io_action->set_sensitive (false);
2280         }
2281
2282         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
2283         edit_action->set_sensitive (pi && pi->plugin()->has_editor ());
2284
2285         /* disallow rename for multiple selections, for plugin inserts and for the fader */
2286         rename_action->set_sensitive (single_selection
2287                         && !pi
2288                         && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ())
2289                         && !boost::dynamic_pointer_cast<UnknownProcessor> (single_selection->processor ()));
2290
2291         processor_menu->popup (1, arg);
2292
2293         /* Add a placeholder gap to the processor list to indicate where a processor would be
2294            inserted were one chosen from the menu.
2295         */
2296         int x, y;
2297         processor_display.get_pointer (x, y);
2298         _placement = processor_display.add_placeholder (y);
2299 }
2300
2301 bool
2302 ProcessorBox::enter_notify (GdkEventCrossing*)
2303 {
2304         processor_display.grab_focus ();
2305         _current_processor_box = this;
2306         return false;
2307 }
2308
2309 bool
2310 ProcessorBox::leave_notify (GdkEventCrossing* ev)
2311 {
2312         if (ev->detail == GDK_NOTIFY_INFERIOR) {
2313                 return false;
2314         }
2315
2316         Widget* top = get_toplevel();
2317
2318         if (top->is_toplevel()) {
2319                 Window* win = dynamic_cast<Window*> (top);
2320                 gtk_window_set_focus (win->gobj(), 0);
2321         }
2322
2323         return false;
2324 }
2325
2326 bool
2327 ProcessorBox::processor_operation (ProcessorOperation op)
2328 {
2329         ProcSelection targets;
2330
2331         get_selected_processors (targets);
2332
2333 /*      if (targets.empty()) {
2334
2335                 int x, y;
2336                 processor_display.get_pointer (x, y);
2337
2338                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
2339
2340                 if (pointer.first && pointer.first->processor()) {
2341                         targets.push_back (pointer.first->processor ());
2342                 }
2343         }
2344 */
2345
2346         if ((op == ProcessorsDelete) && targets.empty()) {
2347                 return false;  //nothing to delete.  return false so the editor-mixer, because the user was probably intending to delete something in the editor
2348         }
2349
2350         switch (op) {
2351         case ProcessorsSelectAll:
2352                 processor_display.select_all ();
2353                 break;
2354
2355         case ProcessorsSelectNone:
2356                 processor_display.select_none ();
2357                 break;
2358
2359         case ProcessorsCopy:
2360                 copy_processors (targets);
2361                 break;
2362
2363         case ProcessorsCut:
2364                 cut_processors (targets);
2365                 break;
2366
2367         case ProcessorsPaste:
2368                 // some processors are not selectable (e.g fader, meter), target is empty.
2369                 if (targets.empty() && _placement >= 0) {
2370                         assert (_route);
2371                         boost::shared_ptr<Processor> proc = _route->before_processor_for_index (_placement);
2372                         if (proc) {
2373                                 targets.push_back (proc);
2374                         }
2375                 }
2376                 if (targets.empty()) {
2377                         paste_processors ();
2378                 } else {
2379                         paste_processors (targets.front());
2380                 }
2381                 break;
2382
2383         case ProcessorsDelete:
2384                 delete_processors (targets);
2385                 break;
2386
2387         case ProcessorsToggleActive:
2388                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
2389                         if (!(*i)->display_to_user ()) {
2390                                 assert (0); // these should not be selectable to begin with.
2391                                 continue;
2392                         }
2393                         if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
2394                                 continue;
2395                         }
2396 #ifdef MIXBUS
2397                         if (boost::dynamic_pointer_cast<PluginInsert> (*i)->is_channelstrip()) {
2398                                 continue;
2399                         }
2400 #endif
2401                         (*i)->enable (!(*i)->enabled ());
2402                 }
2403                 break;
2404
2405         case ProcessorsAB:
2406                 ab_plugins ();
2407                 break;
2408
2409         default:
2410                 break;
2411         }
2412
2413         return true;
2414 }
2415
2416 ProcessorWindowProxy*
2417 ProcessorBox::find_window_proxy (boost::shared_ptr<Processor> processor) const
2418 {
2419         return  processor->window_proxy();
2420 }
2421
2422
2423 bool
2424 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
2425 {
2426         boost::shared_ptr<Processor> processor;
2427         if (child) {
2428                 processor = child->processor ();
2429         }
2430
2431         int ret = false;
2432         bool selected = processor_display.selected (child);
2433
2434         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (processor);
2435         if (pi && pi->plugin() && pi->plugin()->has_inline_display()
2436                         && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)
2437                         && ev->button == 1
2438                         && ev->type == GDK_2BUTTON_PRESS) {
2439                 child->toggle_inline_display_visibility ();
2440                 return true;
2441         }
2442
2443         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
2444
2445                 if (!one_processor_can_be_edited ()) {
2446                         return true;
2447                 }
2448                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
2449                         return true;
2450                 }
2451
2452                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
2453                         generic_edit_processor (processor);
2454                 } else {
2455                         edit_processor (processor);
2456                 }
2457
2458                 ret = true;
2459
2460         } else if (Keyboard::is_context_menu_event (ev)) {
2461
2462                 show_processor_menu (ev->time);
2463
2464                 ret = true;
2465
2466         } else if (processor && ev->button == 1 && selected) {
2467
2468                 // this is purely informational but necessary for route params UI
2469                 ProcessorSelected (processor); // emit
2470
2471         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
2472
2473                 choose_plugin ();
2474                 _get_plugin_selector()->show_manager ();
2475         }
2476
2477         return ret;
2478 }
2479
2480 bool
2481 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
2482 {
2483         boost::shared_ptr<Processor> processor;
2484         if (child) {
2485                 processor = child->processor ();
2486         }
2487
2488         if (processor && Keyboard::is_delete_event (ev)) {
2489
2490                 Glib::signal_idle().connect (sigc::bind (
2491                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
2492                                 boost::weak_ptr<Processor>(processor)));
2493
2494         } else if (processor && Keyboard::is_button2_event (ev)
2495 #ifndef __APPLE__
2496                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
2497 #endif
2498                 ) {
2499
2500                 /* button2-click with no/appropriate modifiers */
2501                 processor->enable (!processor->enabled ());
2502         }
2503
2504         return false;
2505 }
2506
2507 Menu *
2508 ProcessorBox::build_processor_menu ()
2509 {
2510         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
2511         processor_menu->set_name ("ArdourContextMenu");
2512         return processor_menu;
2513 }
2514
2515 void
2516 ProcessorBox::select_all_processors ()
2517 {
2518         processor_display.select_all ();
2519 }
2520
2521 void
2522 ProcessorBox::deselect_all_processors ()
2523 {
2524         processor_display.select_none ();
2525 }
2526
2527 void
2528 ProcessorBox::choose_plugin ()
2529 {
2530         _get_plugin_selector()->set_interested_object (*this);
2531 }
2532
2533 /** @return true if an error occurred, otherwise false */
2534 bool
2535 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
2536 {
2537         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
2538
2539                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
2540
2541                 Route::ProcessorStreams err_streams;
2542
2543                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
2544                         weird_plugin_dialog (**p, err_streams);
2545                         return true;
2546                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
2547                 } else if (plugins.size() == 1 && UIConfiguration::instance().get_open_gui_after_adding_plugin()) {
2548                         if (processor->what_can_be_automated ().size () == 0) {
2549                                 ; /* plugin without controls, don't show ui */
2550                         }
2551                         else if (boost::dynamic_pointer_cast<PluginInsert>(processor)->plugin()->has_inline_display() && UIConfiguration::instance().get_prefer_inline_over_gui()) {
2552                                 ; /* only show inline display */
2553                         }
2554                         else if (processor_can_be_edited (processor)) {
2555                                 if (!ARDOUR_UI_UTILS::engine_is_running()) {
2556                                         return true;
2557                                 } else if ((*p)->has_editor ()) {
2558                                         edit_processor (processor);
2559                                 } else if (boost::dynamic_pointer_cast<PluginInsert>(processor)->plugin()->parameter_count() > 0) {
2560                                         generic_edit_processor (processor);
2561                                 }
2562                         }
2563                 }
2564                 /* add next processor below the currently added.
2565                  * Note: placement < 0: add the bottom */
2566                 if (_placement >= 0) {
2567                         ++_placement;
2568                 }
2569         }
2570
2571         return false;
2572 }
2573
2574 void
2575 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
2576 {
2577         /* XXX this needs to be re-worked!
2578          *
2579          * With new pin-management "streams" is no longer correct.
2580          * p.get_info () is also incorrect for variable i/o plugins (always -1,-1).
2581          *
2582          * Since pin-management was added, this dialog will only show in a very rare
2583          * condition (non-replicated variable i/o configuration failed).
2584          *
2585          * TODO: simplify the message after the string-freeze is lifted.
2586          */
2587         ArdourDialog dialog (_("Plugin Incompatibility"));
2588         Label label;
2589
2590         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
2591                         p.name(), streams.index);
2592
2593         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
2594         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
2595
2596         text += _("\nThis plugin has:\n");
2597         if (has_midi) {
2598                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
2599                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
2600         }
2601         if (has_audio) {
2602                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
2603                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
2604         }
2605
2606         text += _("\nbut at the insertion point, there are:\n");
2607         if (has_midi) {
2608                 uint32_t const n = streams.count.n_midi ();
2609                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
2610         }
2611         if (has_audio) {
2612                 uint32_t const n = streams.count.n_audio ();
2613                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
2614         }
2615
2616         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
2617         label.set_text(text);
2618
2619         dialog.get_vbox()->pack_start (label);
2620         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
2621
2622         dialog.set_name (X_("PluginIODialog"));
2623         dialog.set_modal (true);
2624         dialog.show_all ();
2625
2626         dialog.run ();
2627 }
2628
2629 void
2630 ProcessorBox::choose_insert ()
2631 {
2632         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
2633         _route->add_processor_by_index (processor, _placement);
2634 }
2635
2636 /* Caller must not hold process lock */
2637 void
2638 ProcessorBox::choose_send ()
2639 {
2640         boost::shared_ptr<Send> send (new Send (*_session, _route->pannable (), _route->mute_master()));
2641
2642         /* make an educated guess at the initial number of outputs for the send */
2643         ChanCount outs = (_route->n_outputs().n_audio() && _session->master_out())
2644                         ? _session->master_out()->n_outputs()
2645                         : _route->n_outputs();
2646
2647         /* XXX need processor lock on route */
2648         try {
2649                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
2650                 send->output()->ensure_io (outs, false, this);
2651         } catch (AudioEngine::PortRegistrationFailure& err) {
2652                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
2653                 return;
2654         }
2655
2656         /* let the user adjust the IO setup before creation.
2657
2658            Note: this dialog is NOT modal - we just leave it to run and it will
2659            return when its Finished signal is emitted - typically when the window
2660            is closed.
2661          */
2662
2663         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
2664         ios->show ();
2665
2666         /* keep a reference to the send so it doesn't get deleted while
2667            the IOSelectorWindow is doing its stuff
2668         */
2669         _processor_being_created = send;
2670
2671         ios->selector().Finished.connect (sigc::bind (
2672                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
2673                         boost::weak_ptr<Processor>(send), ios));
2674
2675 }
2676
2677 void
2678 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
2679 {
2680         boost::shared_ptr<Processor> processor (weak_processor.lock());
2681
2682         /* drop our temporary reference to the new send */
2683         _processor_being_created.reset ();
2684
2685         if (!processor) {
2686                 return;
2687         }
2688
2689         switch (r) {
2690         case IOSelector::Cancelled:
2691                 // processor will go away when all shared_ptrs to it vanish
2692                 break;
2693
2694         case IOSelector::Accepted:
2695                 _route->add_processor_by_index (processor, _placement);
2696                 break;
2697         }
2698
2699         delete_when_idle (ios);
2700 }
2701
2702 void
2703 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
2704 {
2705         boost::shared_ptr<Processor> processor (weak_processor.lock());
2706
2707         /* drop our temporary reference to the new return */
2708         _processor_being_created.reset ();
2709
2710         if (!processor) {
2711                 return;
2712         }
2713
2714         switch (r) {
2715         case IOSelector::Cancelled:
2716                 // processor will go away when all shared_ptrs to it vanish
2717                 break;
2718
2719         case IOSelector::Accepted:
2720                 _route->add_processor_by_index (processor, _placement);
2721                 break;
2722         }
2723
2724         delete_when_idle (ios);
2725 }
2726
2727 void
2728 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
2729 {
2730         if (!_route) {
2731                 return;
2732         }
2733
2734         boost::shared_ptr<Route> target = wr.lock();
2735
2736         if (!target) {
2737                 return;
2738         }
2739
2740         if (target->is_foldbackbus ()) {
2741                 _route->add_foldback_send (target);
2742         } else {
2743                 _session->add_internal_send (target, _placement, _route);
2744         }
2745 }
2746
2747 void
2748 ProcessorBox::remove_aux (boost::weak_ptr<Route> wr)
2749 {
2750         if (!_route) {
2751                 return;
2752         }
2753
2754         boost::shared_ptr<Route> target = wr.lock();
2755
2756         if (!target) {
2757                 return;
2758         }
2759         boost::shared_ptr<Send>  send = _route->internal_send_for (target);
2760         boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (send);
2761         _route->remove_processor (proc);
2762
2763 }
2764
2765 void
2766 ProcessorBox::route_processors_changed (RouteProcessorChange c)
2767 {
2768         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
2769                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
2770                 return;
2771         }
2772
2773         redisplay_processors ();
2774 }
2775
2776 void
2777 ProcessorBox::redisplay_processors ()
2778 {
2779         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
2780
2781         if (no_processor_redisplay) {
2782                 return;
2783         }
2784
2785         processor_display.clear ();
2786
2787         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
2788         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
2789         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_pin_mgr));
2790
2791         setup_entry_positions ();
2792 }
2793
2794 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
2795  *  not already have one.
2796  */
2797 void
2798 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
2799 {
2800         boost::shared_ptr<Processor> p = w.lock ();
2801         if (!p) {
2802                 return;
2803         }
2804         if (p->window_proxy()) {
2805                 return;
2806         }
2807
2808         /* see also ProcessorBox::get_editor_window */
2809         bool have_ui = false;
2810
2811         if (boost::dynamic_pointer_cast<PluginInsert> (p)) {
2812                 have_ui = true;
2813         }
2814         else if (boost::dynamic_pointer_cast<PortInsert> (p)) {
2815                 have_ui = true;
2816         }
2817         else if (boost::dynamic_pointer_cast<Send> (p)) {
2818                 if (!boost::dynamic_pointer_cast<InternalSend> (p)) {
2819                         have_ui = true;
2820                 }
2821         }
2822         else if (boost::dynamic_pointer_cast<Return> (p)) {
2823                 if (!boost::dynamic_pointer_cast<InternalReturn> (p)) {
2824                         have_ui = true;
2825                 }
2826         }
2827
2828         if (!have_ui) {
2829                 return;
2830         }
2831
2832         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
2833                         string_compose ("P-%1-%2", _route->id(), p->id()),
2834                         this,
2835                         w);
2836
2837         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
2838
2839         if (ui_xml) {
2840                 wp->set_state (*ui_xml, 0);
2841         }
2842
2843         p->set_window_proxy (wp);
2844         WM::Manager::instance().register_window (wp);
2845 }
2846
2847 void
2848 ProcessorBox::maybe_add_processor_pin_mgr (boost::weak_ptr<Processor> w)
2849 {
2850         boost::shared_ptr<Processor> p = w.lock ();
2851         if (!p || p->pinmgr_proxy ()) {
2852                 return;
2853         }
2854         if (!boost::dynamic_pointer_cast<PluginInsert> (p)) {
2855                 return;
2856         }
2857
2858         PluginPinWindowProxy* wp = new PluginPinWindowProxy (
2859                         string_compose ("PM-%1-%2", _route->id(), p->id()), w);
2860         wp->set_session (_session);
2861
2862         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
2863         if (ui_xml) {
2864                 wp->set_state (*ui_xml, 0);
2865         }
2866
2867         p->set_pingmgr_proxy (wp);
2868         WM::Manager::instance().register_window (wp);
2869 }
2870
2871 void
2872 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
2873 {
2874         boost::shared_ptr<Processor> processor (p.lock ());
2875
2876         if (!processor || ( !processor->display_to_user()
2877 #ifndef NDEBUG
2878                             && !show_all_processors
2879 #endif
2880                           )
2881            ) {
2882                 return;
2883         }
2884
2885         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
2886
2887         ProcessorEntry* e = 0;
2888         if (plugin_insert) {
2889                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
2890         } else {
2891                 e = new ProcessorEntry (this, processor, _width);
2892         }
2893
2894         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
2895         boost::shared_ptr<PortInsert> ext = boost::dynamic_pointer_cast<PortInsert> (processor);
2896         boost::shared_ptr<UnknownProcessor> stub = boost::dynamic_pointer_cast<UnknownProcessor> (processor);
2897
2898         //faders and meters are not deletable, copy/paste-able, so they shouldn't be selectable
2899         if (!send && !plugin_insert && !ext && !stub)
2900                 e->set_selectable(false);
2901
2902         bool mark_send_visible = false;
2903         if (send && _parent_strip) {
2904                 /* show controls of new sends by default */
2905                 GUIObjectState& st = _parent_strip->gui_object_state ();
2906                 XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
2907                 assert (strip);
2908                 /* check if state exists, if not it must be a new send */
2909                 if (!st.get_node(strip, e->state_id())) {
2910                         mark_send_visible = true;
2911                 }
2912         }
2913
2914         /* Set up this entry's state from the GUIObjectState */
2915         XMLNode* proc = entry_gui_object_state (e);
2916         if (proc) {
2917                 e->set_control_state (proc);
2918         }
2919
2920         if (mark_send_visible) {
2921                 e->show_all_controls ();
2922         }
2923
2924         if (plugin_insert
2925 #ifdef MIXBUS
2926                         && !plugin_insert->plugin(0)->is_channelstrip()
2927 #endif
2928                  )
2929         {
2930                 processor_display.add_child (e, drag_targets());
2931         } else {
2932                 processor_display.add_child (e, drag_targets_noplugin());
2933         }
2934 }
2935
2936 void
2937 ProcessorBox::reordered ()
2938 {
2939         compute_processor_sort_keys ();
2940         setup_entry_positions ();
2941 }
2942
2943 void
2944 ProcessorBox::setup_routing_feeds ()
2945 {
2946         list<ProcessorEntry*> children = processor_display.children ();
2947         /* first set the i/o maps for every processor */
2948         list<ProcessorEntry*>::iterator prev = children.begin();
2949
2950         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
2951                 boost::shared_ptr<ARDOUR::Processor> p = (*i)->processor();
2952                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
2953
2954                 list<ProcessorEntry*>::iterator next = i;
2955                 next++;
2956
2957                 if (pi) {
2958                         ChanCount sinks = pi->natural_input_streams ();
2959                         ChanCount sources = pi->natural_output_streams ();
2960                         uint32_t count = pi->get_count ();
2961                         ChanCount in, out;
2962                         pi->configured_io (in, out);
2963
2964                         ChanCount midi_thru;
2965                         ChanCount midi_bypass;
2966                         ChanMapping input_map (pi->input_map ());
2967                         if (pi->has_midi_thru ()) {
2968                                  midi_thru.set(DataType::MIDI, 1);
2969                                  input_map.set (DataType::MIDI, 0, 0);
2970                         }
2971                         if (pi->has_midi_bypass ()) {
2972                                  midi_bypass.set(DataType::MIDI, 1);
2973                         }
2974
2975                         (*i)->input_icon.set_ports (sinks * count + midi_thru);
2976                         (*i)->output_icon.set_ports (sources * count + midi_bypass);
2977
2978                         (*i)->routing_icon.set (
2979                                         in, out,
2980                                         sinks * count + midi_thru,
2981                                         sources * count + midi_bypass,
2982                                         input_map,
2983                                         pi->output_map (),
2984                                         pi->thru_map ());
2985
2986                         if (next != children.end()) {
2987                                 (*next)->routing_icon.set_fed_by (out, sources * count + midi_bypass,
2988                                                 pi->output_map (), pi->thru_map ());
2989                         }
2990
2991                         if (prev != i) {
2992                                 (*prev)->routing_icon.set_feeding (in, sinks * count + midi_thru,
2993                                                 pi->input_map (), pi->thru_map ());
2994                         }
2995
2996                 } else {
2997                         (*i)->input_icon.set_ports (p->input_streams());
2998                         (*i)->output_icon.set_ports (p->output_streams());
2999                         ChanMapping inmap (p->input_streams ());
3000                         ChanMapping outmap (p->output_streams ());
3001                         ChanMapping thrumap;
3002                         (*i)->routing_icon.set (
3003                                         p->input_streams(),
3004                                         p->output_streams(),
3005                                         p->input_streams(),
3006                                         p->output_streams(),
3007                                         inmap, outmap, thrumap);
3008
3009                         if (next != children.end()) {
3010                                 (*next)->routing_icon.set_fed_by (
3011                                                 p->output_streams(),
3012                                                 p->output_streams(),
3013                                                 outmap, thrumap);
3014                         }
3015                         if (prev != i) {
3016                                 (*prev)->routing_icon.set_feeding (
3017                                                 p->input_streams(),
3018                                                 p->output_streams(),
3019                                                 inmap, thrumap);
3020                         }
3021                 }
3022
3023                 if (i == children.begin()) {
3024                         (*i)->routing_icon.unset_fed_by ();
3025                 }
3026                 prev = i;
3027                 (*i)->input_icon.hide();
3028         }
3029
3030         /* now set which icons need to be displayed */
3031         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3032                 (*i)->output_routing_icon.copy_state ((*i)->routing_icon);
3033
3034                 if ((*i)->routing_icon.in_identity ()) {
3035                         (*i)->routing_icon.hide();
3036                         if (i == children.begin()) {
3037                                 (*i)->input_icon.show();
3038                         }
3039                 } else {
3040                         (*i)->routing_icon.show();
3041                         (*i)->routing_icon.queue_draw();
3042                         (*i)->input_icon.show();
3043                 }
3044
3045                 list<ProcessorEntry*>::iterator next = i;
3046                 if (++next == children.end()) {
3047                         // last processor in the chain
3048                         (*i)->output_routing_icon.set_terminal(true);
3049                         (*i)->output_routing_icon.unset_feeding ();
3050                         if ((*i)->routing_icon.out_identity ()) {
3051                                 (*i)->output_routing_icon.hide();
3052                         } else {
3053                                 (*i)->output_routing_icon.show();
3054                                 (*i)->output_routing_icon.queue_draw();
3055                         }
3056                 } else {
3057                         (*i)->output_routing_icon.set_terminal(false);
3058                         if (   !(*i)->routing_icon.out_identity ()
3059                                         && !(*next)->routing_icon.in_identity ()
3060                                         &&  (*next)->routing_icon.can_coalesce ()) {
3061                                 (*i)->output_routing_icon.hide();
3062                         } else if (!(*i)->routing_icon.out_identity ()) {
3063                                 (*i)->output_routing_icon.show();
3064                                 (*i)->output_routing_icon.queue_draw();
3065                                 (*next)->input_icon.show();
3066                         } else {
3067                                 (*i)->output_routing_icon.hide();
3068                         }
3069                 }
3070         }
3071 }
3072
3073 void
3074 ProcessorBox::setup_entry_positions ()
3075 {
3076         list<ProcessorEntry*> children = processor_display.children ();
3077         bool pre_fader = true;
3078
3079         uint32_t num = 0;
3080         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3081                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor()) &&
3082                     boost::dynamic_pointer_cast<Amp>((*i)->processor())->gain_control()->parameter().type() == GainAutomation) {
3083                         pre_fader = false;
3084                         (*i)->set_position (ProcessorEntry::Fader, num++);
3085                 } else {
3086                         if (pre_fader) {
3087                                 (*i)->set_position (ProcessorEntry::PreFader, num++);
3088                         } else {
3089                                 (*i)->set_position (ProcessorEntry::PostFader, num++);
3090                         }
3091                 }
3092         }
3093         setup_routing_feeds ();
3094 }
3095
3096 void
3097 ProcessorBox::compute_processor_sort_keys ()
3098 {
3099         list<ProcessorEntry*> children = processor_display.children ();
3100         Route::ProcessorList our_processors;
3101
3102         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3103                 if ((*i)->processor()) {
3104                         our_processors.push_back ((*i)->processor ());
3105                 }
3106         }
3107
3108         if (_route->reorder_processors (our_processors)) {
3109                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
3110                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
3111                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
3112                    when the drag is torn down after this handler function is finished.
3113                 */
3114                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
3115         }
3116 }
3117
3118 void
3119 ProcessorBox::report_failed_reorder ()
3120 {
3121         /* reorder failed, so redisplay */
3122
3123         redisplay_processors ();
3124
3125         /* now tell them about the problem */
3126
3127         ArdourDialog dialog (_("Plugin Incompatibility"));
3128         Label label;
3129
3130         label.set_text (_("\
3131 You cannot reorder these plugins/sends/inserts\n\
3132 in that way because the inputs and\n\
3133 outputs will not work correctly."));
3134
3135         dialog.get_vbox()->set_border_width (12);
3136         dialog.get_vbox()->pack_start (label);
3137         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
3138
3139         dialog.set_name (X_("PluginIODialog"));
3140         dialog.set_modal (true);
3141         dialog.show_all ();
3142
3143         dialog.run ();
3144 }
3145
3146 void
3147 ProcessorBox::rename_processors ()
3148 {
3149         ProcSelection to_be_renamed;
3150
3151         get_selected_processors (to_be_renamed);
3152
3153         if (to_be_renamed.empty()) {
3154                 return;
3155         }
3156
3157         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
3158                 rename_processor (*i);
3159         }
3160 }
3161
3162 bool
3163 ProcessorBox::can_cut () const
3164 {
3165         vector<boost::shared_ptr<Processor> > sel;
3166
3167         get_selected_processors (sel);
3168
3169         /* cut_processors () does not cut inserts */
3170
3171         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
3172
3173                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3174                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3175                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3176                         return true;
3177                 }
3178         }
3179
3180         return false;
3181 }
3182
3183 bool
3184 ProcessorBox::stub_processor_selected () const
3185 {
3186         vector<boost::shared_ptr<Processor> > sel;
3187
3188         get_selected_processors (sel);
3189
3190         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
3191                 if (boost::dynamic_pointer_cast<UnknownProcessor>((*i)) != 0) {
3192                         return true;
3193                 }
3194         }
3195
3196         return false;
3197 }
3198
3199 void
3200 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
3201 {
3202         if (to_be_removed.empty()) {
3203                 return;
3204         }
3205
3206         XMLNode* node = new XMLNode (X_("cut"));
3207         Route::ProcessorList to_cut;
3208
3209         no_processor_redisplay = true;
3210         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
3211                 // Cut only plugins, sends and returns
3212                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3213                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3214                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3215
3216                         Window* w = get_processor_ui (*i);
3217
3218                         if (w) {
3219                                 w->hide ();
3220                         }
3221
3222                         XMLNode& child ((*i)->get_state());
3223                         node->add_child_nocopy (child);
3224                         to_cut.push_back (*i);
3225                 }
3226         }
3227
3228         if (_route->remove_processors (to_cut) != 0) {
3229                 delete node;
3230                 no_processor_redisplay = false;
3231                 return;
3232         }
3233
3234         _p_selection.set (node);
3235
3236         no_processor_redisplay = false;
3237         redisplay_processors ();
3238 }
3239
3240 void
3241 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
3242 {
3243         if (to_be_copied.empty()) {
3244                 return;
3245         }
3246
3247         XMLNode* node = new XMLNode (X_("copy"));
3248
3249         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
3250                 // Copy only plugins, sends, returns
3251                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3252                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3253                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3254                         node->add_child_nocopy ((*i)->get_state());
3255                 }
3256         }
3257
3258         _p_selection.set (node);
3259 }
3260
3261 void
3262 ProcessorBox::delete_processors (const ProcSelection& targets)
3263 {
3264         if (targets.empty()) {
3265                 return;
3266         }
3267
3268         no_processor_redisplay = true;
3269
3270         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
3271
3272                 Window* w = get_processor_ui (*i);
3273
3274                 if (w) {
3275                         w->hide ();
3276                 }
3277
3278                 _route->remove_processor(*i);
3279         }
3280
3281         no_processor_redisplay = false;
3282         redisplay_processors ();
3283 }
3284
3285 void
3286 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
3287 {
3288         list<boost::shared_ptr<Processor> >::const_iterator x;
3289
3290         no_processor_redisplay = true;
3291         for (x = procs.begin(); x != procs.end(); ++x) {
3292
3293                 Window* w = get_processor_ui (*x);
3294
3295                 if (w) {
3296                         w->hide ();
3297                 }
3298
3299                 _route->remove_processor(*x);
3300         }
3301
3302         no_processor_redisplay = false;
3303         redisplay_processors ();
3304 }
3305
3306 gint
3307 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
3308 {
3309         boost::shared_ptr<Processor> processor (weak_processor.lock());
3310
3311         if (!processor) {
3312                 return false;
3313         }
3314
3315         /* NOT copied to _mixer.selection() */
3316
3317         no_processor_redisplay = true;
3318         _route->remove_processor (processor);
3319         no_processor_redisplay = false;
3320         redisplay_processors ();
3321
3322         return false;
3323 }
3324
3325 void
3326 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
3327 {
3328         Prompter name_prompter (true);
3329         string result;
3330         name_prompter.set_title (_("Rename Processor"));
3331         name_prompter.set_prompt (_("New name:"));
3332         name_prompter.set_initial_text (processor->name());
3333         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
3334         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
3335         name_prompter.show_all ();
3336
3337         switch (name_prompter.run ()) {
3338
3339         case Gtk::RESPONSE_ACCEPT:
3340                 name_prompter.get_result (result);
3341                 if (result.length()) {
3342
3343                         int tries = 0;
3344                         string test = result;
3345
3346                         while (tries < 100) {
3347                                 if (_session->io_name_is_legal (test)) {
3348                                         result = test;
3349                                         break;
3350                                 }
3351                                 tries++;
3352
3353                                 test = string_compose ("%1-%2", result, tries);
3354                         }
3355
3356                         if (tries < 100) {
3357                                 processor->set_name (result);
3358                         } else {
3359                                 /* unlikely! */
3360                                 ARDOUR_UI::instance()->popup_error
3361                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
3362                         }
3363                 }
3364                 break;
3365         }
3366
3367         return;
3368 }
3369
3370 void
3371 ProcessorBox::paste_processors ()
3372 {
3373         if (_p_selection.processors.empty()) {
3374                 return;
3375         }
3376
3377         paste_processor_state (_p_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
3378 }
3379
3380 void
3381 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
3382 {
3383
3384         if (_p_selection.processors.empty()) {
3385                 return;
3386         }
3387
3388         paste_processor_state (_p_selection.processors.get_node().children(), before);
3389 }
3390
3391 void
3392 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
3393 {
3394         XMLNodeConstIterator niter;
3395         list<boost::shared_ptr<Processor> > copies;
3396
3397         if (nlist.empty()) {
3398                 return;
3399         }
3400
3401         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
3402
3403                 XMLProperty const * type = (*niter)->property ("type");
3404                 XMLProperty const * role = (*niter)->property ("role");
3405                 assert (type);
3406
3407                 boost::shared_ptr<Processor> p;
3408                 try {
3409                         if (type->value() == "meter" ||
3410                             type->value() == "main-outs" ||
3411                             type->value() == "amp" ||
3412                             type->value() == "intreturn") {
3413                                 /* do not paste meter, main outs, amp or internal returns */
3414                                 continue;
3415
3416                         } else if (type->value() == "intsend") {
3417
3418                                 /* aux sends are OK, but those used for
3419                                  * other purposes, are not.
3420                                  */
3421
3422                                 assert (role);
3423
3424                                 if (role->value() != "Aux") {
3425                                         continue;
3426                                 }
3427
3428                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
3429                                 XMLNode n (**niter);
3430                                 InternalSend* s = new InternalSend (*_session, sendpan, _route->mute_master(),
3431                                                 _route, boost::shared_ptr<Route>(), Delivery::Aux);
3432
3433                                 IOProcessor::prepare_for_reset (n, s->name());
3434
3435                                 if (s->set_state (n, Stateful::loading_state_version)) {
3436                                         delete s;
3437                                         return;
3438                                 }
3439
3440                                 p.reset (s);
3441
3442                         } else if (type->value() == "send") {
3443
3444                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
3445                                 XMLNode n (**niter);
3446
3447                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
3448
3449                                 IOProcessor::prepare_for_reset (n, s->name());
3450
3451                                 if (s->set_state (n, Stateful::loading_state_version)) {
3452                                         delete s;
3453                                         return;
3454                                 }
3455
3456                                 p.reset (s);
3457
3458                         } else if (type->value() == "return") {
3459
3460                                 XMLNode n (**niter);
3461                                 Return* r = new Return (*_session);
3462
3463                                 IOProcessor::prepare_for_reset (n, r->name());
3464
3465                                 if (r->set_state (n, Stateful::loading_state_version)) {
3466                                         delete r;
3467                                         return;
3468                                 }
3469
3470                                 p.reset (r);
3471
3472                         } else if (type->value() == "port") {
3473
3474                                 XMLNode n (**niter);
3475                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
3476
3477                                 IOProcessor::prepare_for_reset (n, pi->name());
3478
3479                                 if (pi->set_state (n, Stateful::loading_state_version)) {
3480                                         return;
3481                                 }
3482
3483                                 p.reset (pi);
3484                         } else {
3485                                 /* XXX its a bit limiting to assume that everything else
3486                                    is a plugin.
3487                                 */
3488                                 p.reset (new PluginInsert (*_session));
3489                                 /* we can't use RAII Stateful::ForceIDRegeneration
3490                                  * because that'd void copying the state and wrongly bump
3491                                  * the state-version counter.
3492                                  * we need to load the state (incl external files) first and
3493                                  * only then update the ID)
3494                                  */
3495                                 PBD::ID id = p->id();
3496                                 /* strip side-chain state (processor inside processor must be a side-chain)
3497                                  * otherwise we'll end up with duplicate ports-names.
3498                                  * (this needs a better solution which retains connections)
3499                                  * We really would want Stateful::ForceIDRegeneration here :(
3500                                  */
3501                                 XMLNode state (**niter);
3502                                 state.remove_nodes_and_delete ("Processor");
3503
3504                                 p->set_state (state, Stateful::current_state_version);
3505                                 boost::dynamic_pointer_cast<PluginInsert>(p)->update_id (id);
3506                         }
3507
3508                         copies.push_back (p);
3509                 }
3510
3511                 catch (...) {
3512                         error << _("plugin insert constructor failed") << endmsg;
3513                 }
3514         }
3515
3516         if (copies.empty()) {
3517                 return;
3518         }
3519
3520         if (_route->add_processors (copies, p)) {
3521
3522                 string msg = _(
3523                         "Copying the set of processors on the clipboard failed,\n\
3524 probably because the I/O configuration of the plugins\n\
3525 could not match the configuration of this track.");
3526                 MessageDialog am (msg);
3527                 am.run ();
3528         }
3529 }
3530
3531 void
3532 ProcessorBox::get_selected_processors (ProcSelection& processors) const
3533 {
3534         const list<ProcessorEntry*> selection = processor_display.selection (true);
3535         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
3536                 processors.push_back ((*i)->processor ());
3537         }
3538 }
3539
3540 void
3541 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
3542 {
3543         list<ProcessorEntry*> selection = processor_display.selection ();
3544         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
3545                 (this->*method) ((*i)->processor ());
3546         }
3547 }
3548
3549 void
3550 ProcessorBox::all_visible_processors_active (bool state)
3551 {
3552         _route->all_visible_processors_active (state);
3553 }
3554
3555 void
3556 ProcessorBox::ab_plugins ()
3557 {
3558         _route->ab_plugins (ab_direction);
3559         ab_direction = !ab_direction;
3560 }
3561
3562 void
3563 ProcessorBox::set_disk_io_position (DiskIOPoint diop)
3564 {
3565         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_route);
3566         if (t) {
3567                 t->set_disk_io_point (diop);
3568         }
3569 }
3570
3571 void
3572 ProcessorBox::clear_processors ()
3573 {
3574         string prompt;
3575         vector<string> choices;
3576
3577         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
3578                                    "(this cannot be undone)"), _route->name());
3579
3580         choices.push_back (_("Cancel"));
3581         choices.push_back (_("Yes, remove them all"));
3582
3583         ArdourWidgets::Choice prompter (_("Remove processors"), prompt, choices);
3584
3585         if (prompter.run () == 1) {
3586                 _route->clear_processors (PreFader);
3587                 _route->clear_processors (PostFader);
3588         }
3589 }
3590
3591 void
3592 ProcessorBox::clear_processors (Placement p)
3593 {
3594         string prompt;
3595         vector<string> choices;
3596
3597         if (p == PreFader) {
3598                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
3599                                            "(this cannot be undone)"), _route->name());
3600         } else {
3601                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
3602                                            "(this cannot be undone)"), _route->name());
3603         }
3604
3605         choices.push_back (_("Cancel"));
3606         choices.push_back (_("Yes, remove them all"));
3607
3608         ArdourWidgets::Choice prompter (_("Remove processors"), prompt, choices);
3609
3610         if (prompter.run () == 1) {
3611                 _route->clear_processors (p);
3612         }
3613 }
3614
3615 bool
3616 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
3617 {
3618         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
3619         if (at && at->freeze_state() == AudioTrack::Frozen) {
3620                 return false;
3621         }
3622
3623         if (
3624                 boost::dynamic_pointer_cast<Send> (processor) ||
3625                 boost::dynamic_pointer_cast<Return> (processor) ||
3626                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
3627                 boost::dynamic_pointer_cast<PortInsert> (processor)
3628                 ) {
3629                 return true;
3630         }
3631
3632         return false;
3633 }
3634
3635 bool
3636 ProcessorBox::one_processor_can_be_edited ()
3637 {
3638         list<ProcessorEntry*> selection = processor_display.selection ();
3639         list<ProcessorEntry*>::iterator i = selection.begin();
3640         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
3641                 ++i;
3642         }
3643
3644         return (i != selection.end());
3645 }
3646
3647 Gtk::Window*
3648 ProcessorBox::get_editor_window (boost::shared_ptr<Processor> processor, bool use_custom)
3649 {
3650         boost::shared_ptr<Send> send;
3651         boost::shared_ptr<InternalSend> internal_send;
3652         boost::shared_ptr<Return> retrn;
3653         boost::shared_ptr<PluginInsert> plugin_insert;
3654         boost::shared_ptr<PortInsert> port_insert;
3655         Window* gidget = 0;
3656
3657         /* This method may or may not return a Window, but if it does not it
3658          * will modify the parent mixer strip appearance layout to allow
3659          * "editing" the @param processor that was passed in.
3660          *
3661          * So for example, if the processor is an Amp (gain), the parent strip
3662          * will be forced back into a model where the fader controls the main gain.
3663          * If the processor is a send, then we map the send controls onto the
3664          * strip.
3665          *
3666          * Plugins and others will return a window for control.
3667          */
3668
3669         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
3670
3671                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
3672                         return 0;
3673                 }
3674         }
3675
3676         if (boost::dynamic_pointer_cast<Amp> (processor) && boost::dynamic_pointer_cast<Amp> (processor)->gain_control()->parameter().type() == GainAutomation) {
3677
3678                 if (_parent_strip) {
3679                         _parent_strip->revert_to_default_display ();
3680                 }
3681
3682         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
3683
3684                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3685                         return 0;
3686                 }
3687
3688                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
3689
3690                         gidget = new SendUIWindow (send, _session);
3691                 }
3692
3693         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
3694
3695                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
3696                         /* no GUI for these */
3697                         return 0;
3698                 }
3699
3700                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3701                         return 0;
3702                 }
3703
3704                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
3705
3706                 ReturnUIWindow *return_ui;
3707                 Window* w = get_processor_ui (retrn);
3708
3709                 if (w == 0) {
3710
3711                         return_ui = new ReturnUIWindow (retrn, _session);
3712                         return_ui->set_title (retrn->name ());
3713                         set_processor_ui (send, return_ui);
3714
3715                 } else {
3716                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
3717                 }
3718
3719                 gidget = return_ui;
3720
3721         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
3722
3723                 PluginUIWindow *plugin_ui;
3724
3725                 /* these are both allowed to be null */
3726
3727                 Window* w = get_processor_ui (plugin_insert);
3728
3729                 if (w == 0) {
3730                         plugin_ui = new PluginUIWindow (plugin_insert, false, use_custom);
3731                         plugin_ui->set_title (generate_processor_title (plugin_insert));
3732                         set_processor_ui (plugin_insert, plugin_ui);
3733
3734                 } else {
3735                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
3736                 }
3737
3738                 gidget = plugin_ui;
3739
3740         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
3741
3742                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3743                         return 0;
3744                 }
3745
3746                 PortInsertWindow *io_selector;
3747
3748                 Window* w = get_processor_ui (port_insert);
3749
3750                 if (w == 0) {
3751                         io_selector = new PortInsertWindow (_session, port_insert);
3752                         set_processor_ui (port_insert, io_selector);
3753
3754                 } else {
3755                         io_selector = dynamic_cast<PortInsertWindow *> (w);
3756                 }
3757
3758                 gidget = io_selector;
3759         }
3760
3761         return gidget;
3762 }
3763
3764 Gtk::Window*
3765 ProcessorBox::get_generic_editor_window (boost::shared_ptr<Processor> processor)
3766 {
3767         boost::shared_ptr<PluginInsert> plugin_insert
3768                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
3769
3770         if (!plugin_insert) {
3771                 return 0;
3772         }
3773
3774         PluginUIWindow* win = new PluginUIWindow (plugin_insert, true, false);
3775         win->set_title (generate_processor_title (plugin_insert));
3776
3777         return win;
3778 }
3779
3780 void
3781 ProcessorBox::register_actions ()
3782 {
3783         /* We need to use a static object as the owner, since these actions
3784            need to be considered ownable by all ProcessorBox objects
3785         */
3786
3787         load_bindings ();
3788
3789         processor_box_actions = ActionManager::create_action_group (bindings, X_("ProcessorMenu"));
3790
3791         Glib::RefPtr<Action> act;
3792
3793         /* new stuff */
3794         ActionManager::register_action (processor_box_actions, X_("newplugin"), _("New Plugin"),
3795                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
3796
3797         act = ActionManager::register_action (processor_box_actions, X_("newinsert"), _("New Insert"),
3798                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
3799         ActionManager::engine_sensitive_actions.push_back (act);
3800         act = ActionManager::register_action (processor_box_actions, X_("newsend"), _("New External Send ..."),
3801                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
3802         ActionManager::engine_sensitive_actions.push_back (act);
3803
3804         ActionManager::register_action (processor_box_actions, X_("newaux"), _("New Aux Send ..."));
3805         ActionManager::register_action (processor_box_actions, X_("newlisten"), _("New Monitor Send ..."));
3806         ActionManager::register_action (processor_box_actions, X_("removelisten"), _("Remove Monitor Send ..."));
3807
3808         ActionManager::register_action (processor_box_actions, X_("controls"), _("Controls"));
3809         ActionManager::register_action (processor_box_actions, X_("send_options"), _("Send Options"));
3810
3811         ActionManager::register_action (processor_box_actions, X_("clear"), _("Clear (all)"),
3812                         sigc::ptr_fun (ProcessorBox::rb_clear));
3813         ActionManager::register_action (processor_box_actions, X_("clear_pre"), _("Clear (pre-fader)"),
3814                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
3815         ActionManager::register_action (processor_box_actions, X_("clear_post"), _("Clear (post-fader)"),
3816                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
3817
3818         /* standard editing stuff */
3819
3820         cut_action = ActionManager::register_action (processor_box_actions, X_("cut"), _("Cut"),
3821                                                             sigc::ptr_fun (ProcessorBox::rb_cut));
3822         copy_action = ActionManager::register_action (processor_box_actions, X_("copy"), _("Copy"),
3823                                                              sigc::ptr_fun (ProcessorBox::rb_copy));
3824         delete_action = ActionManager::register_action (processor_box_actions, X_("delete"), _("Delete"),
3825                                                                sigc::ptr_fun (ProcessorBox::rb_delete));
3826         backspace_action = ActionManager::register_action (processor_box_actions, X_("backspace"), _("Delete"),
3827                                                                sigc::ptr_fun (ProcessorBox::rb_delete));
3828
3829         ActionManager::plugin_selection_sensitive_actions.push_back (cut_action);
3830         ActionManager::plugin_selection_sensitive_actions.push_back (copy_action);
3831         ActionManager::plugin_selection_sensitive_actions.push_back (delete_action);
3832         ActionManager::plugin_selection_sensitive_actions.push_back (backspace_action);
3833
3834         paste_action = ActionManager::register_action (processor_box_actions, X_("paste"), _("Paste"),
3835                         sigc::ptr_fun (ProcessorBox::rb_paste));
3836         rename_action = ActionManager::register_action (processor_box_actions, X_("rename"), _("Rename"),
3837                         sigc::ptr_fun (ProcessorBox::rb_rename));
3838         ActionManager::register_action (processor_box_actions, X_("selectall"), _("Select All"),
3839                         sigc::ptr_fun (ProcessorBox::rb_select_all));
3840         ActionManager::register_action (processor_box_actions, X_("deselectall"), _("Deselect All"),
3841                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
3842
3843         /* activation etc. */
3844
3845         ActionManager::register_action (processor_box_actions, X_("activate_all"), _("Activate All"),
3846                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
3847         ActionManager::register_action (processor_box_actions, X_("deactivate_all"), _("Deactivate All"),
3848                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
3849         ActionManager::register_action (processor_box_actions, X_("ab_plugins"), _("A/B Plugins"),
3850                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
3851
3852         manage_pins_action = ActionManager::register_action (
3853                 processor_box_actions, X_("manage-pins"), _("Pin Connections..."),
3854                 sigc::ptr_fun (ProcessorBox::rb_manage_pins));
3855
3856         /* Disk IO stuff */
3857         disk_io_action = ActionManager::register_action (processor_box_actions, X_("disk-io-menu"), _("Disk I/O ..."));
3858         ActionManager::register_action (processor_box_actions, X_("disk-io-prefader"), _("Pre-Fader."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOPreFader));
3859         ActionManager::register_action (processor_box_actions, X_("disk-io-postfader"), _("Post-Fader."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOPostFader));
3860         ActionManager::register_action (processor_box_actions, X_("disk-io-custom"), _("Custom."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOCustom));
3861
3862         /* show editors */
3863         edit_action = ActionManager::register_action (
3864                 processor_box_actions, X_("edit"), _("Edit..."),
3865                 sigc::ptr_fun (ProcessorBox::rb_edit));
3866
3867         edit_generic_action = ActionManager::register_action (
3868                 processor_box_actions, X_("edit-generic"), _("Edit with generic controls..."),
3869                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
3870
3871 }
3872
3873 void
3874 ProcessorBox::rb_edit_generic ()
3875 {
3876         if (_current_processor_box == 0) {
3877                 return;
3878         }
3879
3880         _current_processor_box->for_selected_processors (&ProcessorBox::generic_edit_processor);
3881 }
3882
3883 void
3884 ProcessorBox::rb_ab_plugins ()
3885 {
3886         if (_current_processor_box == 0) {
3887                 return;
3888         }
3889
3890         _current_processor_box->ab_plugins ();
3891 }
3892
3893 void
3894 ProcessorBox::rb_set_disk_io_position (DiskIOPoint diop)
3895 {
3896         if (_current_processor_box == 0) {
3897                 return;
3898         }
3899
3900         _current_processor_box->set_disk_io_position (diop);
3901 }
3902
3903 void
3904 ProcessorBox::rb_manage_pins ()
3905 {
3906         if (_current_processor_box == 0) {
3907                 return;
3908         }
3909
3910         _current_processor_box->for_selected_processors (&ProcessorBox::manage_pins);
3911 }
3912 void
3913 ProcessorBox::rb_choose_plugin ()
3914 {
3915         if (_current_processor_box == 0) {
3916                 return;
3917         }
3918         _current_processor_box->choose_plugin ();
3919 }
3920
3921 void
3922 ProcessorBox::rb_choose_insert ()
3923 {
3924         if (_current_processor_box == 0) {
3925                 return;
3926         }
3927         _current_processor_box->choose_insert ();
3928 }
3929
3930 void
3931 ProcessorBox::rb_choose_send ()
3932 {
3933         if (_current_processor_box == 0) {
3934                 return;
3935         }
3936         _current_processor_box->choose_send ();
3937 }
3938
3939 void
3940 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
3941 {
3942         if (_current_processor_box == 0) {
3943                 return;
3944         }
3945
3946         _current_processor_box->choose_aux (wr);
3947 }
3948
3949 void
3950 ProcessorBox::rb_remove_aux (boost::weak_ptr<Route> wr)
3951 {
3952         if (_current_processor_box == 0) {
3953                 return;
3954         }
3955
3956         _current_processor_box->remove_aux (wr);
3957 }
3958
3959 void
3960 ProcessorBox::rb_clear ()
3961 {
3962         if (_current_processor_box == 0) {
3963                 return;
3964         }
3965
3966         _current_processor_box->clear_processors ();
3967 }
3968
3969
3970 void
3971 ProcessorBox::rb_clear_pre ()
3972 {
3973         if (_current_processor_box == 0) {
3974                 return;
3975         }
3976
3977         _current_processor_box->clear_processors (PreFader);
3978 }
3979
3980
3981 void
3982 ProcessorBox::rb_clear_post ()
3983 {
3984         if (_current_processor_box == 0) {
3985                 return;
3986         }
3987
3988         _current_processor_box->clear_processors (PostFader);
3989 }
3990
3991 void
3992 ProcessorBox::rb_cut ()
3993 {
3994         if (_current_processor_box == 0) {
3995                 return;
3996         }
3997
3998         _current_processor_box->processor_operation (ProcessorsCut);
3999 }
4000
4001 void
4002 ProcessorBox::rb_delete ()
4003 {
4004         if (_current_processor_box == 0) {
4005                 return;
4006         }
4007
4008         _current_processor_box->processor_operation (ProcessorsDelete);
4009 }
4010
4011 void
4012 ProcessorBox::rb_copy ()
4013 {
4014         if (_current_processor_box == 0) {
4015                 return;
4016         }
4017         _current_processor_box->processor_operation (ProcessorsCopy);
4018 }
4019
4020 void
4021 ProcessorBox::rb_paste ()
4022 {
4023         if (_current_processor_box == 0) {
4024                 return;
4025         }
4026
4027         _current_processor_box->processor_operation (ProcessorsPaste);
4028 }
4029
4030 void
4031 ProcessorBox::rb_rename ()
4032 {
4033         if (_current_processor_box == 0) {
4034                 return;
4035         }
4036         _current_processor_box->rename_processors ();
4037 }
4038
4039 void
4040 ProcessorBox::rb_select_all ()
4041 {
4042         if (_current_processor_box == 0) {
4043                 return;
4044         }
4045
4046         _current_processor_box->processor_operation (ProcessorsSelectAll);
4047 }
4048
4049 void
4050 ProcessorBox::rb_deselect_all ()
4051 {
4052         if (_current_processor_box == 0) {
4053                 return;
4054         }
4055
4056         _current_processor_box->deselect_all_processors ();
4057 }
4058
4059 void
4060 ProcessorBox::rb_activate_all ()
4061 {
4062         if (_current_processor_box == 0) {
4063                 return;
4064         }
4065
4066         _current_processor_box->all_visible_processors_active (true);
4067 }
4068
4069 void
4070 ProcessorBox::rb_deactivate_all ()
4071 {
4072         if (_current_processor_box == 0) {
4073                 return;
4074         }
4075         _current_processor_box->all_visible_processors_active (false);
4076 }
4077
4078 void
4079 ProcessorBox::rb_edit ()
4080 {
4081         if (_current_processor_box == 0) {
4082                 return;
4083         }
4084
4085         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
4086 }
4087
4088 bool
4089 ProcessorBox::edit_aux_send (boost::shared_ptr<Processor> processor)
4090 {
4091         if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
4092                 return false;
4093         }
4094
4095         if (_parent_strip) {
4096                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
4097                 if (_parent_strip->current_delivery() == send) {
4098                         _parent_strip->revert_to_default_display ();
4099                 } else {
4100                         _parent_strip->show_send(send);
4101                 }
4102         }
4103         return true;
4104 }
4105
4106 void
4107 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
4108 {
4109         if (!processor) {
4110                 return;
4111         }
4112         if (edit_aux_send (processor)) {
4113                 return;
4114         }
4115         if (!ARDOUR_UI_UTILS::engine_is_running ()) {
4116                 return;
4117         }
4118
4119         ProcessorWindowProxy* proxy = find_window_proxy (processor);
4120
4121         if (proxy) {
4122                 proxy->set_custom_ui_mode (true);
4123                 proxy->show_the_right_window ();
4124         }
4125 }
4126
4127 void
4128 ProcessorBox::generic_edit_processor (boost::shared_ptr<Processor> processor)
4129 {
4130         if (!processor) {
4131                 return;
4132         }
4133         if (edit_aux_send (processor)) {
4134                 return;
4135         }
4136         if (!ARDOUR_UI_UTILS::engine_is_running ()) {
4137                 return;
4138         }
4139
4140         ProcessorWindowProxy* proxy = find_window_proxy (processor);
4141
4142         if (proxy) {
4143                 proxy->set_custom_ui_mode (false);
4144                 proxy->show_the_right_window ();
4145         }
4146 }
4147
4148 void
4149 ProcessorBox::manage_pins (boost::shared_ptr<Processor> processor)
4150 {
4151         if (!processor) {
4152                 return;
4153         }
4154         PluginPinWindowProxy* proxy = processor->pinmgr_proxy ();
4155         if (proxy) {
4156                 proxy->get (true);
4157                 proxy->present();
4158         }
4159 }
4160
4161
4162 void
4163 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
4164 {
4165         if (!what_changed.contains (ARDOUR::Properties::name)) {
4166                 return;
4167         }
4168
4169         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
4170
4171         boost::shared_ptr<Processor> processor;
4172         boost::shared_ptr<PluginInsert> plugin_insert;
4173         boost::shared_ptr<Send> send;
4174
4175         list<ProcessorEntry*> children = processor_display.children();
4176
4177         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
4178
4179                 processor = (*iter)->processor ();
4180
4181                 if (!processor) {
4182                         continue;
4183                 }
4184
4185                 Window* w = get_processor_ui (processor);
4186
4187                 if (!w) {
4188                         continue;
4189                 }
4190
4191                 /* rename editor windows for sends and plugins */
4192
4193                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
4194                         w->set_title (send->name ());
4195                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
4196                         w->set_title (generate_processor_title (plugin_insert));
4197                 }
4198         }
4199 }
4200
4201 string
4202 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
4203 {
4204         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
4205         string::size_type email_pos;
4206
4207         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
4208                 maker = maker.substr (0, email_pos - 1);
4209         }
4210
4211         if (maker.length() > 32) {
4212                 maker = maker.substr (0, 32);
4213                 maker += " ...";
4214         }
4215
4216         SessionObject* owner = pi->owner();
4217
4218         if (owner) {
4219                 return string_compose(_("%1: %2 (by %3)"), owner->name(), pi->name(), maker);
4220         } else {
4221                 return string_compose(_("%1 (by %2)"), pi->name(), maker);
4222         }
4223 }
4224
4225 /** @param p Processor.
4226  *  @return the UI window for \a p.
4227  */
4228 Window *
4229 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
4230 {
4231         ProcessorWindowProxy* wp = p->window_proxy();
4232         if (wp) {
4233                 return wp->get ();
4234         }
4235         return 0;
4236 }
4237
4238 /** Make a note of the UI window that a processor is using.
4239  *  @param p Processor.
4240  *  @param w UI window.
4241  */
4242 void
4243 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
4244 {
4245         assert (p->window_proxy());
4246         p->window_proxy()->use_window (*w);
4247 }
4248
4249 void
4250 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
4251 {
4252         boost::shared_ptr<Delivery> d = w.lock ();
4253         if (!d) {
4254                 return;
4255         }
4256
4257         list<ProcessorEntry*> children = processor_display.children ();
4258         list<ProcessorEntry*>::const_iterator i = children.begin();
4259         while (i != children.end() && (*i)->processor() != d) {
4260                 ++i;
4261         }
4262
4263         if (i == children.end()) {
4264                 processor_display.set_active (0);
4265         } else {
4266                 processor_display.set_active (*i);
4267         }
4268 }
4269
4270 /** Called to repair the damage of Editor::show_window doing a show_all() */
4271 void
4272 ProcessorBox::hide_things ()
4273 {
4274         list<ProcessorEntry*> c = processor_display.children ();
4275         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
4276                 (*i)->hide_things ();
4277         }
4278 }
4279
4280 void
4281 ProcessorBox::processor_menu_unmapped ()
4282 {
4283         processor_display.remove_placeholder ();
4284         /* make all possibly-desensitized actions sensitive again so that
4285            they be activated by other means (e.g. bindings)
4286         */
4287         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, true);
4288 }
4289
4290 XMLNode *
4291 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
4292 {
4293         if (!_parent_strip) {
4294                 return 0;
4295         }
4296
4297         GUIObjectState& st = _parent_strip->gui_object_state ();
4298
4299         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
4300         assert (strip);
4301         return st.get_or_add_node (strip, entry->state_id());
4302 }
4303
4304 void
4305 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
4306 {
4307         XMLNode* proc = entry_gui_object_state (entry);
4308         if (!proc) {
4309                 return;
4310         }
4311
4312         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
4313         proc->remove_nodes_and_delete (X_("Object"));
4314         entry->add_control_state (proc);
4315 }
4316
4317 bool
4318 ProcessorBox::is_editor_mixer_strip() const
4319 {
4320         return _parent_strip && !_parent_strip->mixer_owned();
4321 }
4322
4323 ProcessorWindowProxy::ProcessorWindowProxy (string const & name, ProcessorBox* box, boost::weak_ptr<Processor> processor)
4324         : WM::ProxyBase (name, string())
4325         , _processor_box (box)
4326         , _processor (processor)
4327         , is_custom (true)
4328         , want_custom (true)
4329 {
4330         boost::shared_ptr<Processor> p = _processor.lock ();
4331         if (!p) {
4332                 return;
4333         }
4334         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&ProcessorWindowProxy::processor_going_away, this), gui_context());
4335
4336         p->ToggleUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::show_the_right_window, this, false), gui_context());
4337         p->ShowUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::show_the_right_window, this, true), gui_context());
4338         p->HideUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::hide, this), gui_context());
4339 }
4340
4341 ProcessorWindowProxy::~ProcessorWindowProxy()
4342 {
4343         /* processor window proxies do not own the windows they create with
4344          * ::get(), so set _window to null before the normal WindowProxy method
4345          * deletes it.
4346          */
4347         _window = 0;
4348 }
4349
4350 void
4351 ProcessorWindowProxy::processor_going_away ()
4352 {
4353         gui_connections.drop_connections ();
4354         delete _window;
4355         _window = 0;
4356         WM::Manager::instance().remove (this);
4357         /* should be no real reason to do this, since the object that would
4358            send DropReferences is about to be deleted, but lets do it anyway.
4359         */
4360         going_away_connection.disconnect();
4361         delete this;
4362 }
4363
4364 ARDOUR::SessionHandlePtr*
4365 ProcessorWindowProxy::session_handle()
4366 {
4367         /* we don't care */
4368         return 0;
4369 }
4370
4371 XMLNode&
4372 ProcessorWindowProxy::get_state ()
4373 {
4374         XMLNode *node;
4375         node = &ProxyBase::get_state();
4376         node->set_property (X_("custom-ui"), is_custom);
4377         return *node;
4378 }
4379
4380 int
4381 ProcessorWindowProxy::set_state (const XMLNode& node, int /*version*/)
4382 {
4383         XMLNodeList children = node.children ();
4384         XMLNodeList::const_iterator i = children.begin ();
4385         while (i != children.end()) {
4386                 std::string name;
4387                 if ((*i)->name() == X_("Window") && (*i)->get_property (X_("name"), name) && name == _name) {
4388                         break;
4389                 }
4390                 ++i;
4391         }
4392
4393         if (i != children.end()) {
4394                 (*i)->get_property (X_("custom-ui"), want_custom);
4395         }
4396
4397         return ProxyBase::set_state (node, 0);
4398 }
4399
4400 Gtk::Window*
4401 ProcessorWindowProxy::get (bool create)
4402 {
4403         boost::shared_ptr<Processor> p = _processor.lock ();
4404
4405         if (!p) {
4406                 return 0;
4407         }
4408         if (_window && (is_custom != want_custom)) {
4409                 /* drop existing window - wrong type */
4410                 set_state_mask (Gtkmm2ext::WindowProxy::StateMask (state_mask () & ~WindowProxy::Size));
4411                 drop_window ();
4412         }
4413
4414         if (!_window) {
4415                 if (!create) {
4416                         return 0;
4417                 }
4418
4419                 is_custom = want_custom;
4420                 _window = _processor_box->get_editor_window (p, is_custom);
4421
4422                 if (_window) {
4423                         setup ();
4424                         _window->show_all ();
4425                 }
4426         }
4427         return _window;
4428 }
4429
4430 void
4431 ProcessorWindowProxy::show_the_right_window (bool show_not_toggle)
4432 {
4433         if (_window && (is_custom != want_custom)) {
4434                 /* drop existing window - wrong type */
4435                 set_state_mask (Gtkmm2ext::WindowProxy::StateMask (state_mask () & ~WindowProxy::Size));
4436                 drop_window ();
4437         }
4438         if (_window && fully_visible () && show_not_toggle) {
4439                 return;
4440         }
4441         toggle ();
4442 }
4443
4444
4445 PluginPinWindowProxy::PluginPinWindowProxy(std::string const &name, boost::weak_ptr<ARDOUR::Processor> processor)
4446         : WM::ProxyBase (name, string())
4447         , _processor (processor)
4448 {
4449         boost::shared_ptr<Processor> p = _processor.lock ();
4450         if (!p) {
4451                 return;
4452         }
4453         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&PluginPinWindowProxy::processor_going_away, this), gui_context());
4454 }
4455
4456 PluginPinWindowProxy::~PluginPinWindowProxy()
4457 {
4458         _window = 0;
4459 }
4460
4461 ARDOUR::SessionHandlePtr*
4462 PluginPinWindowProxy::session_handle ()
4463 {
4464         ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
4465         if (aw) { return aw; }
4466         return 0;
4467 }
4468
4469 Gtk::Window*
4470 PluginPinWindowProxy::get (bool create)
4471 {
4472         boost::shared_ptr<Processor> p = _processor.lock ();
4473         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
4474         if (!p || !pi) {
4475                 return 0;
4476         }
4477
4478         if (!_window) {
4479                 if (!create) {
4480                         return 0;
4481                 }
4482                 _window = new PluginPinDialog (pi);
4483                 ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
4484                 if (aw) {
4485                         aw->set_session (_session);
4486                 }
4487                 _window->show_all ();
4488         }
4489         return _window;
4490 }
4491
4492 void
4493 PluginPinWindowProxy::processor_going_away ()
4494 {
4495         delete _window;
4496         _window = 0;
4497         WM::Manager::instance().remove (this);
4498         going_away_connection.disconnect();
4499         delete this;
4500 }
4501
4502 void
4503 ProcessorBox::load_bindings ()
4504 {
4505         bindings = Bindings::get_bindings (X_("Processor Box"));
4506 }