Use InlineControl Port Property
[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                 boost::shared_ptr<AutomationControl> c = _control.lock ();
1039                 set_visible (c && (c->flags () & Controllable::InlineControl));
1040         }
1041 }
1042
1043 void
1044 ProcessorEntry::Control::set_visible (bool v)
1045 {
1046         if (v) {
1047                 box.show ();
1048         } else {
1049                 box.hide ();
1050         }
1051
1052         _visible = v;
1053 }
1054
1055 /** Called when the Editor might have re-shown things that
1056     we want hidden.
1057 */
1058 void
1059 ProcessorEntry::Control::hide_things ()
1060 {
1061         if (!_visible) {
1062                 box.hide ();
1063         }
1064 }
1065
1066 string
1067 ProcessorEntry::Control::state_id () const
1068 {
1069         boost::shared_ptr<AutomationControl> c = _control.lock ();
1070         assert (c);
1071
1072         return string_compose (X_("control %1"), c->id().to_s ());
1073 }
1074
1075 PluginInsertProcessorEntry::PluginInsertProcessorEntry (ProcessorBox* b, boost::shared_ptr<ARDOUR::PluginInsert> p, Width w)
1076         : ProcessorEntry (b, p, w)
1077         , _plugin_insert (p)
1078 {
1079         p->PluginIoReConfigure.connect (
1080                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1081                 );
1082         p->PluginMapChanged.connect (
1083                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1084                 );
1085         p->PluginConfigChanged.connect (
1086                 _iomap_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::iomap_changed, this), gui_context()
1087                 );
1088 }
1089
1090 void
1091 PluginInsertProcessorEntry::iomap_changed ()
1092 {
1093         _parent->setup_routing_feeds ();
1094         routing_icon.queue_draw();
1095         output_routing_icon.queue_draw();
1096 }
1097
1098 void
1099 PluginInsertProcessorEntry::hide_things ()
1100 {
1101         ProcessorEntry::hide_things ();
1102 }
1103
1104 ProcessorEntry::PortIcon::PortIcon(bool input) {
1105         _input = input;
1106         _ports = ARDOUR::ChanCount(ARDOUR::DataType::AUDIO, 1);
1107         set_size_request (-1, std::max (2.f, rintf(2.f * UIConfiguration::instance().get_ui_scale())));
1108 }
1109
1110 bool
1111 ProcessorEntry::PortIcon::on_expose_event (GdkEventExpose* ev)
1112 {
1113         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
1114
1115         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
1116         cairo_clip (cr);
1117
1118         Gtk::Allocation a = get_allocation();
1119         double const width = a.get_width();
1120         double const height = a.get_height();
1121
1122         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
1123         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
1124
1125         cairo_rectangle (cr, 0, 0, width, height);
1126         cairo_fill (cr);
1127
1128         const double dx = rint(max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1129         for (uint32_t i = 0; i < _ports.n_total(); ++i) {
1130                 set_routing_color (cr, i < _ports.n_midi());
1131                 const double x = ProcessorEntry::RoutingIcon::pin_x_pos (i, width, _ports.n_total(), 0 , false);
1132                 cairo_rectangle (cr, x - .5 - dx * .5, 0, 1 + dx, height);
1133                 cairo_fill(cr);
1134         }
1135
1136         cairo_destroy(cr);
1137         return true;
1138 }
1139
1140 ProcessorEntry::RoutingIcon::RoutingIcon (bool input)
1141         : _fed_by (false)
1142         , _input (input)
1143 {
1144         set_terminal (false);
1145 }
1146
1147 void
1148 ProcessorEntry::RoutingIcon::set_terminal (bool b) {
1149         _terminal = b;
1150         int h = std::max (8.f, rintf(8.f * sqrt (UIConfiguration::instance().get_ui_scale())));
1151         if (_terminal) {
1152                 h += std::max (4.f, rintf(4.f * sqrt (UIConfiguration::instance().get_ui_scale())));
1153         }
1154         set_size_request (-1, h);
1155 }
1156
1157 void
1158 ProcessorEntry::RoutingIcon::set (
1159                 const ARDOUR::ChanCount& in,
1160                 const ARDOUR::ChanCount& out,
1161                 const ARDOUR::ChanCount& sinks,
1162                 const ARDOUR::ChanCount& sources,
1163                 const ARDOUR::ChanMapping& in_map,
1164                 const ARDOUR::ChanMapping& out_map,
1165                 const ARDOUR::ChanMapping& thru_map)
1166 {
1167         _in       = in;
1168         _out      = out;
1169         _sources  = sources;
1170         _sinks    = sinks;
1171         _in_map   = in_map;
1172         _out_map  = out_map;
1173         _thru_map = thru_map;
1174 }
1175
1176 bool
1177 ProcessorEntry::RoutingIcon::in_identity () const {
1178         if (_thru_map.n_total () > 0) {
1179                 return false;
1180         }
1181         if (!_in_map.is_monotonic () || !_in_map.is_identity ()) {
1182                 return false;
1183         }
1184         if (_in_map.n_total () != _sinks.n_total () || _in.n_total () != _sinks.n_total ()) {
1185                 return false;
1186         }
1187         return true;
1188 }
1189
1190 bool
1191 ProcessorEntry::RoutingIcon::out_identity () const {
1192         if (_thru_map.n_total () > 0) {
1193                 // TODO skip if trhu is not connected to any of next's inputs
1194                 return false;
1195         }
1196         if (!_out_map.is_monotonic () || !_out_map.is_identity ()) {
1197                 return false;
1198         }
1199         if (_out_map.n_total () != _sources.n_total () || _out.n_total () != _sources.n_total ()) {
1200                 return false;
1201         }
1202         return true;
1203 }
1204
1205 bool
1206 ProcessorEntry::RoutingIcon::can_coalesce () const {
1207         if (_thru_map.n_total () > 0) {
1208                 return false;
1209         }
1210         if (_fed_by && _f_out != _f_sources) {
1211                 return false;
1212         }
1213         if (_fed_by && !_f_out_map.is_identity () && !_in_map.is_identity ()) {
1214                 return false;
1215         }
1216         if (_input && _sinks == _in && (!_fed_by || _f_out == _in)) {
1217                 return true;
1218         }
1219         return false;
1220 }
1221
1222 void
1223 ProcessorEntry::RoutingIcon::set_fed_by (
1224                                 const ARDOUR::ChanCount& out,
1225                                 const ARDOUR::ChanCount& sources,
1226                                 const ARDOUR::ChanMapping& out_map,
1227                                 const ARDOUR::ChanMapping& thru_map)
1228 {
1229         _f_out      = out;
1230         _f_sources  = sources;
1231         _f_out_map  = out_map;
1232         _f_thru_map = thru_map;
1233         _fed_by     = true;
1234 }
1235
1236 void
1237 ProcessorEntry::RoutingIcon::set_feeding (
1238                                 const ARDOUR::ChanCount& in,
1239                                 const ARDOUR::ChanCount& sinks,
1240                                 const ARDOUR::ChanMapping& in_map,
1241                                 const ARDOUR::ChanMapping& thru_map)
1242 {
1243         _i_in       = in;
1244         _i_sinks    = sinks;
1245         _i_in_map   = in_map;
1246         _i_thru_map = thru_map;
1247         _feeding    = true;
1248 }
1249
1250 double
1251 ProcessorEntry::RoutingIcon::pin_x_pos (uint32_t i, double width, uint32_t n_total, uint32_t n_midi, bool midi)
1252 {
1253         if (!midi) { i += n_midi; }
1254         if (n_total == 1) {
1255                 assert (i == 0);
1256                 return rint (width * .5) +.5;
1257         }
1258         return rint (width * (.15 + .7 * i / (n_total - 1))) + .5;
1259 }
1260
1261 void
1262 ProcessorEntry::RoutingIcon::draw_gnd (cairo_t* cr, double x0, double y0, double height, bool midi)
1263 {
1264         const double dx = 1 + rint (max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1265         const double y1 = rint (height * .66) + .5;
1266
1267         cairo_save (cr);
1268         cairo_translate (cr, x0, y0);
1269         cairo_move_to (cr, 0, height);
1270         cairo_line_to (cr, 0, y1);
1271         cairo_move_to (cr, 0 - dx, y1);
1272         cairo_line_to (cr, 0 + dx, y1);
1273
1274         set_routing_color (cr, midi);
1275         cairo_set_line_width (cr, 1.0);
1276         cairo_stroke (cr);
1277         cairo_restore (cr);
1278 }
1279
1280 void
1281 ProcessorEntry::RoutingIcon::draw_sidechain (cairo_t* cr, double x0, double y0, double height, bool midi)
1282 {
1283         const double dx = 1 + rint (max(2., 2. * UIConfiguration::instance().get_ui_scale()));
1284         const double y1 = rint (height * .5) - .5;
1285
1286         cairo_save (cr);
1287         cairo_translate (cr, x0, y0);
1288         cairo_move_to (cr, 0 - dx, height);
1289         cairo_line_to (cr, 0, y1);
1290         cairo_line_to (cr, 0 + dx, height);
1291         cairo_close_path (cr);
1292
1293         set_routing_color (cr, midi);
1294         cairo_fill (cr);
1295         cairo_restore (cr);
1296 }
1297
1298 void
1299 ProcessorEntry::RoutingIcon::draw_thru_src (cairo_t* cr, double x0, double y0, double height, bool midi)
1300 {
1301         const double rad = 1;
1302         const double y1 = height - rad - 1.5;
1303
1304         cairo_arc (cr, x0, y0 + y1, rad, 0, 2. * M_PI);
1305         cairo_move_to (cr, x0, y0 + height - 1.5);
1306         cairo_line_to (cr, x0, y0 + height);
1307         set_routing_color (cr, midi);
1308         cairo_set_line_width  (cr, 1.0);
1309         cairo_stroke (cr);
1310 }
1311
1312 void
1313 ProcessorEntry::RoutingIcon::draw_thru_sink (cairo_t* cr, double x0, double y0, double height, bool midi)
1314 {
1315         const double rad = 1;
1316         const double y1 = rad + 1;
1317
1318         cairo_arc (cr, x0, y0 + y1, rad, 0, 2. * M_PI);
1319         cairo_move_to (cr, x0, y0);
1320         cairo_line_to (cr, x0, y0 + 1);
1321         set_routing_color (cr, midi);
1322         cairo_set_line_width  (cr, 1.0);
1323         cairo_stroke (cr);
1324 }
1325
1326 void
1327 ProcessorEntry::RoutingIcon::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool dashed)
1328 {
1329         double bz = abs (y1 - y0);
1330
1331         cairo_move_to (cr, x0, y0);
1332         cairo_curve_to (cr, x0, y0 + bz, x1, y1 - bz, x1, y1);
1333         cairo_set_line_width  (cr, 1.0);
1334         cairo_set_line_cap  (cr,  CAIRO_LINE_CAP_ROUND);
1335         cairo_set_source_rgb (cr, 1, 0, 0);
1336         if (dashed) {
1337                 const double dashes[] = { 2, 3 };
1338                 cairo_set_dash (cr, dashes, 2, 0);
1339         }
1340         set_routing_color (cr, midi);
1341         cairo_stroke (cr);
1342         if (dashed) {
1343                 cairo_set_dash (cr, 0, 0, 0);
1344         }
1345 }
1346
1347 bool
1348 ProcessorEntry::RoutingIcon::on_expose_event (GdkEventExpose* ev)
1349 {
1350         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
1351
1352         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
1353         cairo_clip (cr);
1354
1355         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1356
1357         Gtk::Allocation a = get_allocation();
1358         double const width = a.get_width();
1359         double const height = a.get_height();
1360
1361         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
1362         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
1363
1364         cairo_rectangle (cr, 0, 0, width, height);
1365         cairo_fill (cr);
1366
1367         if (_input) {
1368                 if (can_coalesce ()) {
1369                         expose_coalesced_input_map (cr, width, height);
1370                 } else {
1371                         expose_input_map (cr, width, height);
1372                 }
1373         } else {
1374                 expose_output_map (cr, width, height);
1375         }
1376
1377         cairo_destroy(cr);
1378         return true;
1379 }
1380
1381 void
1382 ProcessorEntry::RoutingIcon::expose_coalesced_input_map (cairo_t* cr, const double width, const double height)
1383 {
1384         const uint32_t pc_in = _sinks.n_total();
1385         const uint32_t pc_in_midi = _sinks.n_midi();
1386
1387         for (uint32_t i = 0; i < pc_in; ++i) {
1388                 const bool is_midi = i < pc_in_midi;
1389                 bool valid_in;
1390                 uint32_t pn = is_midi ? i : i - pc_in_midi;
1391                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1392                 uint32_t idx = _in_map.get (dt, pn, &valid_in);
1393                 if (!valid_in) {
1394                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1395                         draw_gnd (cr, x, 0, height, is_midi);
1396                         continue;
1397                 }
1398                 if (idx >= _in.get (dt)) {
1399                         // side-chain, probably
1400                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1401                         draw_sidechain (cr, x, 0, height, is_midi);
1402                         continue;
1403                 }
1404                 double c_x0;
1405                 double c_x1 = pin_x_pos (i, width, pc_in, 0, false);
1406
1407                 if (_fed_by) {
1408                         bool valid_src;
1409                         uint32_t src = _f_out_map.get_src (dt, idx, &valid_src);
1410                         if (!valid_src) {
1411                                 double x = pin_x_pos (i, width, pc_in, 0, false);
1412                                 bool valid_thru;
1413                                 _f_thru_map.get (dt, idx, &valid_thru);
1414                                 if (valid_thru) {
1415                                         draw_thru_src (cr, x, 0, height, is_midi);
1416                                 } else {
1417                                         draw_gnd (cr, x, 0, height, is_midi);
1418                                 }
1419                                 continue;
1420                         }
1421                         c_x0 = pin_x_pos (src, width, _f_sources.n_total(), _f_sources.n_midi(), is_midi);
1422                 } else {
1423                         c_x0 = pin_x_pos (idx, width, _in.n_total(), _in.n_midi(), is_midi);
1424                 }
1425                 draw_connection (cr, c_x0, c_x1, 0, height, is_midi);
1426         }
1427 }
1428
1429 void
1430 ProcessorEntry::RoutingIcon::expose_input_map (cairo_t* cr, const double width, const double height)
1431 {
1432         const uint32_t n_in = _in.n_total();
1433         const uint32_t n_in_midi = _in.n_midi();
1434         const uint32_t pc_in = _sinks.n_total();
1435         const uint32_t pc_in_midi = _sinks.n_midi();
1436
1437         // draw inputs to this
1438         for (uint32_t i = 0; i < pc_in; ++i) {
1439                 const bool is_midi = i < pc_in_midi;
1440                 bool valid_in;
1441                 uint32_t pn = is_midi ? i : i - pc_in_midi;
1442                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1443                 uint32_t idx = _in_map.get (dt, pn, &valid_in);
1444                 // check if it's fed
1445                 bool valid_src = true;
1446                 if (valid_in && idx < _in.get (dt) && _fed_by) {
1447                         bool valid_out;
1448                         bool valid_thru;
1449                         _f_out_map.get_src (dt, idx, &valid_out);
1450                         _f_thru_map.get (dt, idx, &valid_thru);
1451                         if (!valid_out && !valid_thru) {
1452                                 valid_src = false;
1453                         }
1454                 }
1455                 if (!valid_in || !valid_src) {
1456                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1457                         draw_gnd (cr, x, 0, height, is_midi);
1458                         continue;
1459                 }
1460                 if (idx >= _in.get (dt)) {
1461                         // side-chain, probably
1462                         double x = pin_x_pos (i, width, pc_in, 0, is_midi);
1463                         draw_sidechain (cr, x, 0, height, is_midi);
1464                         continue;
1465                 }
1466                 double c_x1 = pin_x_pos (i, width, pc_in, 0, false);
1467                 double c_x0 = pin_x_pos (idx, width, n_in, n_in_midi, is_midi);
1468                 draw_connection (cr, c_x0, c_x1, 0, height, is_midi);
1469         }
1470
1471         // draw reverse thru
1472         for (uint32_t i = 0; i < n_in; ++i) {
1473                 const bool is_midi = i < n_in_midi;
1474                 bool valid_thru;
1475                 uint32_t pn = is_midi ? i : i - n_in_midi;
1476                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1477                 _thru_map.get_src (dt, pn, &valid_thru);
1478                 if (!valid_thru) {
1479                         continue;
1480                 }
1481                 double x = pin_x_pos (i, width, n_in, 0, is_midi);
1482                 draw_thru_sink (cr, x, 0, height, is_midi);
1483         }
1484 }
1485
1486 void
1487 ProcessorEntry::RoutingIcon::expose_output_map (cairo_t* cr, const double width, const double height)
1488 {
1489         int dh = std::max (4.f, rintf(4.f * UIConfiguration::instance().get_ui_scale()));
1490         double ht = _terminal ? height - dh : height;
1491
1492         // draw outputs of this
1493         const uint32_t pc_out = _sources.n_total();
1494         const uint32_t pc_out_midi = _sources.n_midi();
1495         const uint32_t n_out = _out.n_total();
1496         const uint32_t n_out_midi = _out.n_midi();
1497
1498         for (uint32_t i = 0; i < pc_out; ++i) {
1499                 const bool is_midi = i < pc_out_midi;
1500                 bool valid_out;
1501                 uint32_t pn = is_midi ? i : i - pc_out_midi;
1502                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1503                 uint32_t idx = _out_map.get (dt, pn, &valid_out);
1504                 if (!valid_out) {
1505                         continue;
1506                 }
1507                 // skip connections that are not used in the next's input
1508                 if (_feeding) {
1509                         bool valid_thru, valid_sink;
1510                         _i_in_map.get_src (dt, idx, &valid_sink);
1511                         _i_thru_map.get_src (dt, idx, &valid_thru);
1512                         if (!valid_thru && !valid_sink) {
1513                                 if (!is_midi || i != 0) { // special case midi-bypass
1514                                         continue;
1515                                 }
1516                         }
1517                 }
1518                 double c_x0 = pin_x_pos (i, width, pc_out, 0, false);
1519                 double c_x1 = pin_x_pos (idx, width, n_out, n_out_midi, is_midi);
1520                 draw_connection (cr, c_x0, c_x1, 0, ht, is_midi);
1521         }
1522
1523         for (uint32_t i = 0; i < n_out; ++i) {
1524                 const bool is_midi = i < n_out_midi;
1525                 uint32_t pn = is_midi ? i : i - n_out_midi;
1526                 DataType dt = is_midi ? DataType::MIDI : DataType::AUDIO;
1527                 double x = pin_x_pos (i, width, n_out, 0, is_midi);
1528
1529                 if (!_terminal) {
1530                         bool valid_thru_f = false;
1531                         // skip connections that are not used in the next's input
1532                         if (_feeding) {
1533                                 bool valid_sink;
1534                                 _i_in_map.get_src (dt, pn, &valid_sink);
1535                                 _i_thru_map.get_src (dt, pn, &valid_thru_f);
1536                                 if (!valid_thru_f && !valid_sink) {
1537                                         if (!is_midi || i != 0) { // special case midi-bypass
1538                                                 continue;
1539                                         }
1540                                 }
1541                         }
1542
1543                         bool valid_src;
1544                         _out_map.get_src (dt, pn, &valid_src);
1545                         if (!valid_src) {
1546                                 bool valid_thru;
1547                                 uint32_t idx = _thru_map.get (dt, pn, &valid_thru);
1548                                 if (valid_thru) {
1549                                         if (idx >= _in.get (dt)) {
1550                                                 draw_sidechain (cr, x, 0, height, is_midi);
1551                                         } else {
1552                                                 draw_thru_src (cr, x, 0, height, is_midi);
1553                                         }
1554                                 } else if (valid_thru_f){
1555                                         // gnd is part of input, unless it's a thru input
1556                                         // (also only true if !coalesced into one small display)
1557                                         draw_gnd (cr, x, 0, height, is_midi);
1558                                 }
1559                         }
1560                 } else {
1561                         // terminal node, add arrows
1562                         bool valid_src;
1563                         _out_map.get_src (dt, pn, &valid_src);
1564                         if (!valid_src) {
1565                                 bool valid_thru;
1566                                 uint32_t idx = _thru_map.get (dt, pn, &valid_thru);
1567                                 if (valid_thru) {
1568                                         if (idx >= _in.get (dt)) {
1569                                                 draw_sidechain (cr, x, 0, height - dh, is_midi);
1570                                         } else {
1571                                                 draw_thru_src (cr, x, 0, height - dh, is_midi);
1572                                         }
1573                                 } else {
1574                                         draw_gnd (cr, x, 0, height - dh, is_midi);
1575                                 }
1576                         }
1577
1578                         set_routing_color (cr, is_midi);
1579                         cairo_set_line_width (cr, 1.0);
1580                         cairo_move_to (cr, x, height - dh);
1581                         cairo_line_to (cr, x, height - 2);
1582                         cairo_stroke (cr);
1583
1584                         const double ar = dh - 1;
1585                         cairo_move_to (cr, x - ar, height - ar);
1586                         cairo_line_to (cr, x     , height - .5);
1587                         cairo_line_to (cr, x + ar, height - ar);
1588                         cairo_line_to (cr, x     , height - ar * .5);
1589                         cairo_close_path (cr);
1590                         cairo_fill_preserve (cr);
1591                         cairo_stroke (cr);
1592                 }
1593         }
1594 }
1595
1596 ProcessorEntry::PluginInlineDisplay::PluginInlineDisplay (ProcessorEntry& e, boost::shared_ptr<ARDOUR::Plugin> p, uint32_t max_height)
1597         : PluginDisplay (p, max_height)
1598         , _entry (e)
1599         , _scroll (false)
1600         , _given_max_height (max_height)
1601 {
1602         std::string postfix = string_compose(_("\n%1+double-click to toggle inline-display"), Keyboard::tertiary_modifier_name ());
1603
1604         if (_plug->has_editor()) {
1605                 set_tooltip (*this,
1606                                 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));
1607         } else {
1608                 set_tooltip (*this,
1609                                 string_compose (_("<b>%1</b>\nDouble-click to show generic GUI.%2"), e.name (Wide), postfix));
1610         }
1611 }
1612
1613
1614 bool
1615 ProcessorEntry::PluginInlineDisplay::on_button_press_event (GdkEventButton *ev)
1616 {
1617         assert (_entry.processor ());
1618
1619         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_entry.processor());
1620         // duplicated code :(
1621         // consider some tweaks to pass this up to the DnDVBox somehow:
1622         // select processor, then call (private)
1623         //_entry._parent->processor_button_press_event (ev, &_entry);
1624         if (pi && pi->plugin() && pi->plugin()->has_inline_display()
1625                         && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)
1626                         && ev->button == 1
1627                         && ev->type == GDK_2BUTTON_PRESS) {
1628                 _entry.toggle_inline_display_visibility ();
1629                 return true;
1630         }
1631         else if (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS)) {
1632                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
1633                         _entry._parent->generic_edit_processor (_entry.processor ());
1634                 } else {
1635                         _entry._parent->edit_processor (_entry.processor ());
1636                 }
1637                 return true;
1638         }
1639         return false;
1640 }
1641
1642 void
1643 ProcessorEntry::PluginInlineDisplay::on_size_request (Requisition* req)
1644 {
1645         req->width = 56;
1646         req->height = _cur_height;
1647 }
1648
1649
1650 void
1651 ProcessorEntry::PluginInlineDisplay::update_height_alloc (uint32_t inline_height)
1652 {
1653         /* work-around scroll-bar + aspect ratio
1654          * show inline-view -> height changes -> scrollbar gets added
1655          * -> width changes -> inline-view, fixed aspect ratio -> height changes
1656          * -> scroll bar is removed [-> width changes ; repeat ]
1657          */
1658         uint32_t shm = std::min (_max_height, inline_height);
1659         bool sc = false;
1660         Gtk::Container* pr = get_parent();
1661         for (uint32_t i = 0; i < 4 && pr; ++i) {
1662                 // VBox, EventBox, ViewPort, ScrolledWindow
1663                 pr = pr->get_parent();
1664         }
1665         Gtk::ScrolledWindow* sw = dynamic_cast<Gtk::ScrolledWindow*> (pr);
1666         if (sw) {
1667                 const Gtk::VScrollbar* vsb = sw->get_vscrollbar();
1668                 sc = vsb && vsb->is_visible();
1669         }
1670
1671         if (shm != _cur_height) {
1672                 queue_resize ();
1673                 if (!_scroll && sc) {
1674                         _max_height = shm;
1675                 } else {
1676                         _max_height = _given_max_height;
1677                 }
1678                 _cur_height = shm;
1679         }
1680
1681         _scroll = sc;
1682 }
1683
1684 void
1685 ProcessorEntry::PluginInlineDisplay::display_frame (cairo_t* cr, double w, double h)
1686 {
1687         Gtkmm2ext::rounded_rectangle (cr, .5, -1.5, w - 1, h + 1, 7);
1688 }
1689
1690 ProcessorEntry::LuaPluginDisplay::LuaPluginDisplay (ProcessorEntry& e, boost::shared_ptr<ARDOUR::LuaProc> p, uint32_t max_height)
1691         : PluginInlineDisplay (e, p, max_height)
1692         , _luaproc (p)
1693         , _lua_render_inline (0)
1694 {
1695         p->setup_lua_inline_gui (&lua_gui);
1696
1697         lua_State* LG = lua_gui.getState ();
1698         LuaInstance::bind_cairo (LG);
1699         luabridge::LuaRef lua_render = luabridge::getGlobal (LG, "render_inline");
1700         assert (lua_render.isFunction ());
1701         _lua_render_inline = new luabridge::LuaRef (lua_render);
1702 }
1703
1704 ProcessorEntry::LuaPluginDisplay::~LuaPluginDisplay ()
1705 {
1706         delete (_lua_render_inline);
1707 }
1708
1709 uint32_t
1710 ProcessorEntry::LuaPluginDisplay::render_inline (cairo_t *cr, uint32_t width)
1711 {
1712         Cairo::Context ctx (cr);
1713         try {
1714                 luabridge::LuaRef rv = (*_lua_render_inline)((Cairo::Context *)&ctx, width, _max_height);
1715                 lua_gui.collect_garbage_step ();
1716                 if (rv.isTable ()) {
1717                         uint32_t h = rv[2];
1718                         return h;
1719                 }
1720         } catch (luabridge::LuaException const& e) {
1721 #ifndef NDEBUG
1722                 cerr << "LuaException:" << e.what () << endl;
1723 #endif
1724         } catch (...) { }
1725         return 0;
1726 }
1727
1728
1729 static std::list<Gtk::TargetEntry> drop_targets()
1730 {
1731         std::list<Gtk::TargetEntry> tmp;
1732         tmp.push_back (Gtk::TargetEntry ("processor")); // from processor-box to processor-box
1733         tmp.push_back (Gtk::TargetEntry ("PluginInfoPtr")); // from plugin-manager
1734         tmp.push_back (Gtk::TargetEntry ("PluginFavoritePtr")); // from sidebar
1735         return tmp;
1736 }
1737
1738 static std::list<Gtk::TargetEntry> drag_targets()
1739 {
1740         std::list<Gtk::TargetEntry> tmp;
1741         tmp.push_back (Gtk::TargetEntry ("PluginPresetPtr")); // to sidebar (optional preset)
1742         tmp.push_back (Gtk::TargetEntry ("processor")); // to processor-box (copy)
1743         return tmp;
1744 }
1745
1746 static std::list<Gtk::TargetEntry> drag_targets_noplugin()
1747 {
1748         std::list<Gtk::TargetEntry> tmp;
1749         tmp.push_back (Gtk::TargetEntry ("processor")); // to processor box (sends, faders re-order)
1750         return tmp;
1751 }
1752
1753 ProcessorBox::ProcessorBox (ARDOUR::Session* sess, boost::function<PluginSelector*()> get_plugin_selector,
1754                             ProcessorSelection& psel, MixerStrip* parent, bool owner_is_mixer)
1755         : _parent_strip (parent)
1756         , _owner_is_mixer (owner_is_mixer)
1757         , ab_direction (true)
1758         , _get_plugin_selector (get_plugin_selector)
1759         , _placement (-1)
1760         , _p_selection(psel)
1761         , processor_display (drop_targets())
1762         , _redisplay_pending (false)
1763 {
1764         set_session (sess);
1765
1766         /* ProcessorBox actions and bindings created statically by call to
1767          * ProcessorBox::register_actions(), made by ARDOUR_UI so that actions
1768          * are available for context menus.
1769          */
1770
1771         processor_display.set_data ("ardour-bindings", bindings);
1772
1773         _width = Wide;
1774         processor_menu = 0;
1775         no_processor_redisplay = false;
1776
1777         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1778         processor_scroller.add (processor_display);
1779         pack_start (processor_scroller, true, true);
1780
1781         processor_display.set_flags (CAN_FOCUS);
1782         processor_display.set_name ("ProcessorList");
1783         processor_display.set_data ("processorbox", this);
1784         processor_display.set_size_request (48, -1);
1785         processor_display.set_spacing (0);
1786
1787         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
1788         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
1789
1790         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
1791         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
1792
1793         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
1794         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
1795         processor_display.DropFromExternal.connect (sigc::mem_fun (*this, &ProcessorBox::plugin_drop));
1796
1797         processor_scroller.show ();
1798         processor_display.show ();
1799
1800         if (parent) {
1801                 parent->DeliveryChanged.connect (
1802                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
1803                         );
1804         }
1805
1806         set_tooltip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
1807 }
1808
1809 ProcessorBox::~ProcessorBox ()
1810 {
1811         /* it may appear as if we should delete processor_menu but that is a
1812          * pointer to a widget owned by the UI Manager, and has potentially
1813          * be returned to many other ProcessorBoxes. GTK doesn't really make
1814          * clear the ownership of this widget, which is a menu and thus is
1815          * never packed into any container other than an implict GtkWindow.
1816          *
1817          * For now, until or if we ever get clarification over the ownership
1818          * story just let it continue to exist. At worst, its a small memory leak.
1819          */
1820 }
1821
1822 void
1823 ProcessorBox::set_route (boost::shared_ptr<Route> r)
1824 {
1825         if (_route == r) {
1826                 return;
1827         }
1828
1829         _route_connections.drop_connections();
1830
1831         /* new route: any existing block on processor redisplay must be meaningless */
1832         no_processor_redisplay = false;
1833         _route = r;
1834
1835         _route->processors_changed.connect (
1836                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
1837                 );
1838
1839         _route->DropReferences.connect (
1840                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
1841                 );
1842
1843         _route->PropertyChanged.connect (
1844                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
1845                 );
1846
1847         redisplay_processors ();
1848 }
1849
1850 void
1851 ProcessorBox::route_going_away ()
1852 {
1853         no_processor_redisplay = true;
1854         processor_display.clear ();
1855         _route.reset ();
1856 }
1857
1858 boost::shared_ptr<Processor>
1859 ProcessorBox::find_drop_position (ProcessorEntry* position)
1860 {
1861         boost::shared_ptr<Processor> p;
1862         if (position) {
1863                 p = position->processor ();
1864                 if (!p) {
1865                         /* dropped on the blank entry (which will be before the
1866                                  fader), so use the first non-blank child as our
1867                                  `dropped on' processor */
1868                         list<ProcessorEntry*> c = processor_display.children ();
1869                         list<ProcessorEntry*>::iterator i = c.begin ();
1870
1871                         assert (i != c.end ());
1872                         p = (*i)->processor ();
1873                         assert (p);
1874                 }
1875         }
1876         return p;
1877 }
1878
1879 void
1880 ProcessorBox::_drop_plugin_preset (Gtk::SelectionData const &data, Route::ProcessorList &pl)
1881 {
1882                 const void * d = data.get_data();
1883                 const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
1884
1885                 PluginPresetList nfos;
1886                 TreeView* source;
1887                 tv->get_object_drag_data (nfos, &source);
1888
1889                 for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1890                         PluginPresetPtr ppp = (*i);
1891                         PluginInfoPtr pip = ppp->_pip;
1892                         PluginPtr p = pip->load (*_session);
1893                         if (!p) {
1894                                 continue;
1895                         }
1896
1897                         if (ppp->_preset.valid) {
1898                                 p->load_preset (ppp->_preset);
1899                         }
1900
1901                         boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
1902                         if (Config->get_new_plugins_active ()) {
1903                                 processor->enable (true);
1904                         }
1905                         pl.push_back (processor);
1906                 }
1907 }
1908
1909 void
1910 ProcessorBox::_drop_plugin (Gtk::SelectionData const &data, Route::ProcessorList &pl)
1911 {
1912                 const void * d = data.get_data();
1913                 const Gtkmm2ext::DnDTreeView<ARDOUR::PluginInfoPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginInfoPtr>*>(d);
1914                 PluginInfoList nfos;
1915
1916                 TreeView* source;
1917                 tv->get_object_drag_data (nfos, &source);
1918
1919                 for (list<PluginInfoPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1920                         PluginPtr p = (*i)->load (*_session);
1921                         if (!p) {
1922                                 continue;
1923                         }
1924                         boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
1925                         if (Config->get_new_plugins_active ()) {
1926                                 processor->enable (true);
1927                         }
1928                         pl.push_back (processor);
1929                 }
1930 }
1931
1932 void
1933 ProcessorBox::plugin_drop (Gtk::SelectionData const &data, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
1934 {
1935         if (!_session) {
1936                 return;
1937         }
1938
1939         boost::shared_ptr<Processor> p = find_drop_position (position);
1940         Route::ProcessorList pl;
1941
1942         if (data.get_target() == "PluginInfoPtr") {
1943                 _drop_plugin (data, pl);
1944         }
1945         else if (data.get_target() == "PluginFavoritePtr") {
1946                 _drop_plugin_preset (data, pl);
1947         }
1948         else {
1949                 return;
1950         }
1951
1952         Route::ProcessorStreams err;
1953         if (_route->add_processors (pl, p, &err)) {
1954                 string msg = _(
1955                                 "Processor Drag/Drop failed. Probably because\n\
1956 the I/O configuration of the plugins could\n\
1957 not match the configuration of this track.");
1958                 MessageDialog am (msg);
1959                 am.run ();
1960         }
1961 }
1962
1963 void
1964 ProcessorBox::object_drop (DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
1965 {
1966         if (Gdk::ACTION_LINK == context->get_selected_action()) {
1967                 list<ProcessorEntry*> children = source->selection ();
1968                 assert (children.size() == 1);
1969                 ProcessorEntry* other = *children.begin();
1970                 assert (other->can_copy_state (position));
1971                 boost::shared_ptr<ARDOUR::Processor> otherproc = other->processor();
1972                 boost::shared_ptr<ARDOUR::Processor> proc = position->processor();
1973                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
1974                 assert (otherproc && proc && pi);
1975
1976                 PBD::ID id = pi->id();
1977                 XMLNode& state = otherproc->get_state ();
1978                 /* strip side-chain state (processor inside processor must be a side-chain)
1979                  * otherwise we'll end up with duplicate ports-names.
1980                  * (this needs a better solution which retains connections)
1981                  */
1982                 state.remove_nodes_and_delete ("Processor");
1983                 proc->set_state (state, Stateful::loading_state_version);
1984                 boost::dynamic_pointer_cast<PluginInsert>(proc)->update_id (id);
1985                 return;
1986         }
1987
1988         boost::shared_ptr<Processor> p = find_drop_position (position);
1989
1990         list<ProcessorEntry*> children = source->selection (true);
1991         list<boost::shared_ptr<Processor> > procs;
1992         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1993                 if ((*i)->processor ()) {
1994                         if (boost::dynamic_pointer_cast<UnknownProcessor> ((*i)->processor())) {
1995                                 continue;
1996                         }
1997                         if (boost::dynamic_pointer_cast<PortInsert> ((*i)->processor())) {
1998                                 continue;
1999                         }
2000                         procs.push_back ((*i)->processor ());
2001                 }
2002         }
2003
2004         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
2005                 XMLNode& state = (*i)->get_state ();
2006                 XMLNodeList nlist;
2007                 nlist.push_back (&state);
2008                 paste_processor_state (nlist, p);
2009                 delete &state;
2010         }
2011
2012         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
2013            ourselves.
2014         */
2015
2016         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
2017                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
2018                 if (other) {
2019                         other->delete_dragged_processors (procs);
2020                 }
2021         }
2022 }
2023
2024 void
2025 ProcessorBox::set_width (Width w)
2026 {
2027         if (_width == w) {
2028                 return;
2029         }
2030
2031         _width = w;
2032
2033         list<ProcessorEntry*> children = processor_display.children ();
2034         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
2035                 (*i)->set_enum_width (w);
2036         }
2037
2038         queue_resize ();
2039 }
2040
2041 Gtk::Menu*
2042 ProcessorBox::build_possible_aux_menu ()
2043 {
2044         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2045
2046         if (rl->empty()) {
2047                 /* No aux sends if there are no busses */
2048                 return 0;
2049         }
2050
2051         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2052                 return 0;
2053         }
2054
2055         using namespace Menu_Helpers;
2056         Menu* menu = manage (new Menu);
2057         MenuList& items = menu->items();
2058
2059         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2060                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2061                         /* don't allow sending to master or monitor or to self */
2062                         continue;
2063                 }
2064                 if ((*r)->is_foldbackbus ()) {
2065                         continue;
2066                 }
2067                 if (_route->internal_send_for (*r)) {
2068                         /* aux-send to target already exists */
2069                         continue;
2070                 }
2071                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
2072         }
2073
2074         return menu;
2075 }
2076
2077 Gtk::Menu*
2078 ProcessorBox::build_possible_listener_menu ()
2079 {
2080         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2081
2082         if (rl->empty()) {
2083                 /* No aux sends if there are no busses */
2084                 return 0;
2085         }
2086
2087         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2088                 return 0;
2089         }
2090
2091         using namespace Menu_Helpers;
2092         Menu* menu = manage (new Menu);
2093         MenuList& items = menu->items();
2094
2095         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2096                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2097                         /* don't allow sending to master or monitor or to self */
2098                         continue;
2099                 }
2100                 if (!(*r)->is_foldbackbus ()) {
2101                         continue;
2102                 }
2103                 if (_route->internal_send_for (*r)) {
2104                         /* aux-send to target already exists */
2105                         continue;
2106                 }
2107                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
2108         }
2109
2110         return menu;
2111 }
2112
2113 Gtk::Menu*
2114 ProcessorBox::build_possible_remove_listener_menu ()
2115 {
2116         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
2117
2118         if (rl->empty()) {
2119                 /* No aux sends if there are no busses */
2120                 return 0;
2121         }
2122
2123         if (_route->is_monitor () || _route->is_foldbackbus ()) {
2124                 return 0;
2125         }
2126
2127         using namespace Menu_Helpers;
2128         Menu* menu = manage (new Menu);
2129         MenuList& items = menu->items();
2130
2131         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
2132                 if ((*r)->is_master() || (*r)->is_monitor () || *r == _route) {
2133                         /* don't allow sending to master or monitor or to self */
2134                         continue;
2135                 }
2136                 if (!(*r)->is_foldbackbus ()) {
2137                         continue;
2138                 }
2139                 if (!_route->internal_send_for (*r)) {
2140                         /* aux-send to target already exists */
2141                         continue;
2142                 }
2143                 items.push_back (MenuElemNoMnemonic ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_remove_aux), boost::weak_ptr<Route>(*r))));
2144         }
2145
2146         return menu;
2147 }
2148
2149 void
2150 ProcessorBox::show_processor_menu (int arg)
2151 {
2152         if (processor_menu == 0) {
2153                 processor_menu = build_processor_menu ();
2154                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
2155         }
2156
2157         /* Sort out the plugin submenu */
2158
2159         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
2160
2161         if (plugin_menu_item) {
2162                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
2163         }
2164
2165         /* And the aux submenu */
2166
2167         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
2168
2169         if (aux_menu_item) {
2170                 Menu* m = build_possible_aux_menu();
2171                 if (m && !m->items().empty()) {
2172                         aux_menu_item->set_submenu (*m);
2173                         aux_menu_item->set_sensitive (true);
2174                 } else {
2175                         delete m;
2176                         /* stupid gtkmm: we need to pass a null reference here */
2177                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
2178                         aux_menu_item->set_sensitive (false);
2179                 }
2180         }
2181
2182         Gtk::MenuItem* listen_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newlisten"));
2183
2184         if (listen_menu_item) {
2185                 Menu* m = build_possible_listener_menu();
2186                 if (m && !m->items().empty()) {
2187                         listen_menu_item->set_submenu (*m);
2188                         listen_menu_item->set_sensitive (true);
2189                 } else {
2190                         delete m;
2191                         /* stupid gtkmm: we need to pass a null reference here */
2192                         gtk_menu_item_set_submenu (listen_menu_item->gobj(), 0);
2193                         listen_menu_item->set_sensitive (false);
2194                 }
2195         }
2196
2197         Gtk::MenuItem* remove_listen_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/removelisten"));
2198
2199         if (remove_listen_menu_item) {
2200                 Menu* m = build_possible_remove_listener_menu();
2201                 if (m && !m->items().empty()) {
2202                         remove_listen_menu_item->set_submenu (*m);
2203                         remove_listen_menu_item->set_sensitive (true);
2204                 } else {
2205                         delete m;
2206                         /* stupid gtkmm: we need to pass a null reference here */
2207                         gtk_menu_item_set_submenu (remove_listen_menu_item->gobj(), 0);
2208                         remove_listen_menu_item->set_sensitive (false);
2209                 }
2210         }
2211
2212         ActionManager::get_action (X_("ProcessorMenu"), "newinsert")->set_sensitive (!_route->is_monitor () && !_route->is_foldbackbus ());
2213         ActionManager::get_action (X_("ProcessorMenu"), "newsend")->set_sensitive (!_route->is_monitor () && !_route->is_foldbackbus ());
2214
2215         ProcessorEntry* single_selection = 0;
2216         if (processor_display.selection().size() == 1) {
2217                 single_selection = processor_display.selection().front();
2218         }
2219
2220         /* And the controls submenu */
2221
2222         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
2223
2224         if (controls_menu_item) {
2225                 if (single_selection) {
2226                         Menu* m = single_selection->build_controls_menu ();
2227                         if (m && !m->items().empty()) {
2228                                 controls_menu_item->set_submenu (*m);
2229                                 controls_menu_item->set_sensitive (true);
2230                         } else {
2231                                 delete m;
2232                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
2233                                 controls_menu_item->set_sensitive (false);
2234                         }
2235                 } else {
2236                         controls_menu_item->set_sensitive (false);
2237                 }
2238         }
2239
2240
2241         Gtk::MenuItem* send_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/send_options"));
2242         if (send_menu_item) {
2243                 if (single_selection && !_route->is_monitor()) {
2244                         Menu* m = single_selection->build_send_options_menu ();
2245                         if (m && !m->items().empty()) {
2246                                 send_menu_item->set_submenu (*m);
2247                                 send_menu_item->set_sensitive (true);
2248                         } else {
2249                                 delete m;
2250                                 gtk_menu_item_set_submenu (send_menu_item->gobj(), 0);
2251                                 send_menu_item->set_sensitive (false);
2252                         }
2253                 } else {
2254                         send_menu_item->set_sensitive (false);
2255                 }
2256         }
2257
2258         /* Sensitise actions as approprioate */
2259
2260         const bool sensitive = !processor_display.selection().empty() && ! stub_processor_selected ();
2261
2262         paste_action->set_sensitive (!_p_selection.processors.empty());
2263         cut_action->set_sensitive (sensitive && can_cut ());
2264         copy_action->set_sensitive (sensitive);
2265         delete_action->set_sensitive (sensitive || stub_processor_selected ());
2266         backspace_action->set_sensitive (sensitive || stub_processor_selected ());
2267
2268         edit_action->set_sensitive (one_processor_can_be_edited ());
2269         edit_generic_action->set_sensitive (one_processor_can_be_edited ());
2270
2271         boost::shared_ptr<PluginInsert> pi;
2272         if (single_selection) {
2273                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
2274         }
2275
2276         manage_pins_action->set_sensitive (pi != 0);
2277         if (boost::dynamic_pointer_cast<Track>(_route)) {
2278                 disk_io_action->set_sensitive (true);
2279         } else {
2280                 disk_io_action->set_sensitive (false);
2281         }
2282
2283         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
2284         edit_action->set_sensitive (pi && pi->plugin()->has_editor ());
2285
2286         /* disallow rename for multiple selections, for plugin inserts and for the fader */
2287         rename_action->set_sensitive (single_selection
2288                         && !pi
2289                         && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ())
2290                         && !boost::dynamic_pointer_cast<UnknownProcessor> (single_selection->processor ()));
2291
2292         processor_menu->popup (1, arg);
2293
2294         /* Add a placeholder gap to the processor list to indicate where a processor would be
2295            inserted were one chosen from the menu.
2296         */
2297         int x, y;
2298         processor_display.get_pointer (x, y);
2299         _placement = processor_display.add_placeholder (y);
2300 }
2301
2302 bool
2303 ProcessorBox::enter_notify (GdkEventCrossing*)
2304 {
2305         processor_display.grab_focus ();
2306         _current_processor_box = this;
2307         return false;
2308 }
2309
2310 bool
2311 ProcessorBox::leave_notify (GdkEventCrossing* ev)
2312 {
2313         if (ev->detail == GDK_NOTIFY_INFERIOR) {
2314                 return false;
2315         }
2316
2317         Widget* top = get_toplevel();
2318
2319         if (top->is_toplevel()) {
2320                 Window* win = dynamic_cast<Window*> (top);
2321                 gtk_window_set_focus (win->gobj(), 0);
2322         }
2323
2324         return false;
2325 }
2326
2327 bool
2328 ProcessorBox::processor_operation (ProcessorOperation op)
2329 {
2330         ProcSelection targets;
2331
2332         get_selected_processors (targets);
2333
2334 /*      if (targets.empty()) {
2335
2336                 int x, y;
2337                 processor_display.get_pointer (x, y);
2338
2339                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
2340
2341                 if (pointer.first && pointer.first->processor()) {
2342                         targets.push_back (pointer.first->processor ());
2343                 }
2344         }
2345 */
2346
2347         if ((op == ProcessorsDelete) && targets.empty()) {
2348                 return false;  //nothing to delete.  return false so the editor-mixer, because the user was probably intending to delete something in the editor
2349         }
2350
2351         switch (op) {
2352         case ProcessorsSelectAll:
2353                 processor_display.select_all ();
2354                 break;
2355
2356         case ProcessorsSelectNone:
2357                 processor_display.select_none ();
2358                 break;
2359
2360         case ProcessorsCopy:
2361                 copy_processors (targets);
2362                 break;
2363
2364         case ProcessorsCut:
2365                 cut_processors (targets);
2366                 break;
2367
2368         case ProcessorsPaste:
2369                 // some processors are not selectable (e.g fader, meter), target is empty.
2370                 if (targets.empty() && _placement >= 0) {
2371                         assert (_route);
2372                         boost::shared_ptr<Processor> proc = _route->before_processor_for_index (_placement);
2373                         if (proc) {
2374                                 targets.push_back (proc);
2375                         }
2376                 }
2377                 if (targets.empty()) {
2378                         paste_processors ();
2379                 } else {
2380                         paste_processors (targets.front());
2381                 }
2382                 break;
2383
2384         case ProcessorsDelete:
2385                 delete_processors (targets);
2386                 break;
2387
2388         case ProcessorsToggleActive:
2389                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
2390                         if (!(*i)->display_to_user ()) {
2391                                 assert (0); // these should not be selectable to begin with.
2392                                 continue;
2393                         }
2394                         if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
2395                                 continue;
2396                         }
2397 #ifdef MIXBUS
2398                         if (boost::dynamic_pointer_cast<PluginInsert> (*i)->is_channelstrip()) {
2399                                 continue;
2400                         }
2401 #endif
2402                         (*i)->enable (!(*i)->enabled ());
2403                 }
2404                 break;
2405
2406         case ProcessorsAB:
2407                 ab_plugins ();
2408                 break;
2409
2410         default:
2411                 break;
2412         }
2413
2414         return true;
2415 }
2416
2417 ProcessorWindowProxy*
2418 ProcessorBox::find_window_proxy (boost::shared_ptr<Processor> processor) const
2419 {
2420         return  processor->window_proxy();
2421 }
2422
2423
2424 bool
2425 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
2426 {
2427         boost::shared_ptr<Processor> processor;
2428         if (child) {
2429                 processor = child->processor ();
2430         }
2431
2432         int ret = false;
2433         bool selected = processor_display.selected (child);
2434
2435         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (processor);
2436         if (pi && pi->plugin() && pi->plugin()->has_inline_display()
2437                         && Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)
2438                         && ev->button == 1
2439                         && ev->type == GDK_2BUTTON_PRESS) {
2440                 child->toggle_inline_display_visibility ();
2441                 return true;
2442         }
2443
2444         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
2445
2446                 if (!one_processor_can_be_edited ()) {
2447                         return true;
2448                 }
2449                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
2450                         return true;
2451                 }
2452
2453                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
2454                         generic_edit_processor (processor);
2455                 } else {
2456                         edit_processor (processor);
2457                 }
2458
2459                 ret = true;
2460
2461         } else if (Keyboard::is_context_menu_event (ev)) {
2462
2463                 show_processor_menu (ev->time);
2464
2465                 ret = true;
2466
2467         } else if (processor && ev->button == 1 && selected) {
2468
2469                 // this is purely informational but necessary for route params UI
2470                 ProcessorSelected (processor); // emit
2471
2472         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
2473
2474                 choose_plugin ();
2475                 _get_plugin_selector()->show_manager ();
2476         }
2477
2478         return ret;
2479 }
2480
2481 bool
2482 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
2483 {
2484         boost::shared_ptr<Processor> processor;
2485         if (child) {
2486                 processor = child->processor ();
2487         }
2488
2489         if (processor && Keyboard::is_delete_event (ev)) {
2490
2491                 Glib::signal_idle().connect (sigc::bind (
2492                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
2493                                 boost::weak_ptr<Processor>(processor)));
2494
2495         } else if (processor && Keyboard::is_button2_event (ev)
2496 #ifndef __APPLE__
2497                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
2498 #endif
2499                 ) {
2500
2501                 /* button2-click with no/appropriate modifiers */
2502                 processor->enable (!processor->enabled ());
2503         }
2504
2505         return false;
2506 }
2507
2508 Menu *
2509 ProcessorBox::build_processor_menu ()
2510 {
2511         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
2512         processor_menu->set_name ("ArdourContextMenu");
2513         return processor_menu;
2514 }
2515
2516 void
2517 ProcessorBox::select_all_processors ()
2518 {
2519         processor_display.select_all ();
2520 }
2521
2522 void
2523 ProcessorBox::deselect_all_processors ()
2524 {
2525         processor_display.select_none ();
2526 }
2527
2528 void
2529 ProcessorBox::choose_plugin ()
2530 {
2531         _get_plugin_selector()->set_interested_object (*this);
2532 }
2533
2534 /** @return true if an error occurred, otherwise false */
2535 bool
2536 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
2537 {
2538         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
2539
2540                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
2541
2542                 Route::ProcessorStreams err_streams;
2543
2544                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
2545                         weird_plugin_dialog (**p, err_streams);
2546                         return true;
2547                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
2548                 } else if (plugins.size() == 1 && UIConfiguration::instance().get_open_gui_after_adding_plugin()) {
2549                         if (processor->what_can_be_automated ().size () == 0) {
2550                                 ; /* plugin without controls, don't show ui */
2551                         }
2552                         else if (boost::dynamic_pointer_cast<PluginInsert>(processor)->plugin()->has_inline_display() && UIConfiguration::instance().get_prefer_inline_over_gui()) {
2553                                 ; /* only show inline display */
2554                         }
2555                         else if (processor_can_be_edited (processor)) {
2556                                 if (!ARDOUR_UI_UTILS::engine_is_running()) {
2557                                         return true;
2558                                 } else if ((*p)->has_editor ()) {
2559                                         edit_processor (processor);
2560                                 } else if (boost::dynamic_pointer_cast<PluginInsert>(processor)->plugin()->parameter_count() > 0) {
2561                                         generic_edit_processor (processor);
2562                                 }
2563                         }
2564                 }
2565                 /* add next processor below the currently added.
2566                  * Note: placement < 0: add the bottom */
2567                 if (_placement >= 0) {
2568                         ++_placement;
2569                 }
2570         }
2571
2572         return false;
2573 }
2574
2575 void
2576 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
2577 {
2578         /* XXX this needs to be re-worked!
2579          *
2580          * With new pin-management "streams" is no longer correct.
2581          * p.get_info () is also incorrect for variable i/o plugins (always -1,-1).
2582          *
2583          * Since pin-management was added, this dialog will only show in a very rare
2584          * condition (non-replicated variable i/o configuration failed).
2585          *
2586          * TODO: simplify the message after the string-freeze is lifted.
2587          */
2588         ArdourDialog dialog (_("Plugin Incompatibility"));
2589         Label label;
2590
2591         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
2592                         p.name(), streams.index);
2593
2594         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
2595         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
2596
2597         text += _("\nThis plugin has:\n");
2598         if (has_midi) {
2599                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
2600                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
2601         }
2602         if (has_audio) {
2603                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
2604                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
2605         }
2606
2607         text += _("\nbut at the insertion point, there are:\n");
2608         if (has_midi) {
2609                 uint32_t const n = streams.count.n_midi ();
2610                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
2611         }
2612         if (has_audio) {
2613                 uint32_t const n = streams.count.n_audio ();
2614                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
2615         }
2616
2617         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
2618         label.set_text(text);
2619
2620         dialog.get_vbox()->pack_start (label);
2621         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
2622
2623         dialog.set_name (X_("PluginIODialog"));
2624         dialog.set_modal (true);
2625         dialog.show_all ();
2626
2627         dialog.run ();
2628 }
2629
2630 void
2631 ProcessorBox::choose_insert ()
2632 {
2633         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
2634         _route->add_processor_by_index (processor, _placement);
2635 }
2636
2637 /* Caller must not hold process lock */
2638 void
2639 ProcessorBox::choose_send ()
2640 {
2641         boost::shared_ptr<Send> send (new Send (*_session, _route->pannable (), _route->mute_master()));
2642
2643         /* make an educated guess at the initial number of outputs for the send */
2644         ChanCount outs = (_route->n_outputs().n_audio() && _session->master_out())
2645                         ? _session->master_out()->n_outputs()
2646                         : _route->n_outputs();
2647
2648         /* XXX need processor lock on route */
2649         try {
2650                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
2651                 send->output()->ensure_io (outs, false, this);
2652         } catch (AudioEngine::PortRegistrationFailure& err) {
2653                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
2654                 return;
2655         }
2656
2657         /* let the user adjust the IO setup before creation.
2658
2659            Note: this dialog is NOT modal - we just leave it to run and it will
2660            return when its Finished signal is emitted - typically when the window
2661            is closed.
2662          */
2663
2664         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
2665         ios->show ();
2666
2667         /* keep a reference to the send so it doesn't get deleted while
2668            the IOSelectorWindow is doing its stuff
2669         */
2670         _processor_being_created = send;
2671
2672         ios->selector().Finished.connect (sigc::bind (
2673                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
2674                         boost::weak_ptr<Processor>(send), ios));
2675
2676 }
2677
2678 void
2679 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
2680 {
2681         boost::shared_ptr<Processor> processor (weak_processor.lock());
2682
2683         /* drop our temporary reference to the new send */
2684         _processor_being_created.reset ();
2685
2686         if (!processor) {
2687                 return;
2688         }
2689
2690         switch (r) {
2691         case IOSelector::Cancelled:
2692                 // processor will go away when all shared_ptrs to it vanish
2693                 break;
2694
2695         case IOSelector::Accepted:
2696                 _route->add_processor_by_index (processor, _placement);
2697                 break;
2698         }
2699
2700         delete_when_idle (ios);
2701 }
2702
2703 void
2704 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
2705 {
2706         boost::shared_ptr<Processor> processor (weak_processor.lock());
2707
2708         /* drop our temporary reference to the new return */
2709         _processor_being_created.reset ();
2710
2711         if (!processor) {
2712                 return;
2713         }
2714
2715         switch (r) {
2716         case IOSelector::Cancelled:
2717                 // processor will go away when all shared_ptrs to it vanish
2718                 break;
2719
2720         case IOSelector::Accepted:
2721                 _route->add_processor_by_index (processor, _placement);
2722                 break;
2723         }
2724
2725         delete_when_idle (ios);
2726 }
2727
2728 void
2729 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
2730 {
2731         if (!_route) {
2732                 return;
2733         }
2734
2735         boost::shared_ptr<Route> target = wr.lock();
2736
2737         if (!target) {
2738                 return;
2739         }
2740
2741         if (target->is_foldbackbus ()) {
2742                 _route->add_foldback_send (target);
2743         } else {
2744                 _session->add_internal_send (target, _placement, _route);
2745         }
2746 }
2747
2748 void
2749 ProcessorBox::remove_aux (boost::weak_ptr<Route> wr)
2750 {
2751         if (!_route) {
2752                 return;
2753         }
2754
2755         boost::shared_ptr<Route> target = wr.lock();
2756
2757         if (!target) {
2758                 return;
2759         }
2760         boost::shared_ptr<Send>  send = _route->internal_send_for (target);
2761         boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (send);
2762         _route->remove_processor (proc);
2763
2764 }
2765
2766 void
2767 ProcessorBox::route_processors_changed (RouteProcessorChange c)
2768 {
2769         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
2770                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
2771                 return;
2772         }
2773
2774         redisplay_processors ();
2775 }
2776
2777 void
2778 ProcessorBox::redisplay_processors ()
2779 {
2780         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
2781
2782         if (no_processor_redisplay) {
2783                 return;
2784         }
2785
2786         processor_display.clear ();
2787
2788         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
2789         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
2790         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_pin_mgr));
2791
2792         setup_entry_positions ();
2793 }
2794
2795 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
2796  *  not already have one.
2797  */
2798 void
2799 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
2800 {
2801         boost::shared_ptr<Processor> p = w.lock ();
2802         if (!p) {
2803                 return;
2804         }
2805         if (p->window_proxy()) {
2806                 return;
2807         }
2808
2809         /* see also ProcessorBox::get_editor_window */
2810         bool have_ui = false;
2811
2812         if (boost::dynamic_pointer_cast<PluginInsert> (p)) {
2813                 have_ui = true;
2814         }
2815         else if (boost::dynamic_pointer_cast<PortInsert> (p)) {
2816                 have_ui = true;
2817         }
2818         else if (boost::dynamic_pointer_cast<Send> (p)) {
2819                 if (!boost::dynamic_pointer_cast<InternalSend> (p)) {
2820                         have_ui = true;
2821                 }
2822         }
2823         else if (boost::dynamic_pointer_cast<Return> (p)) {
2824                 if (!boost::dynamic_pointer_cast<InternalReturn> (p)) {
2825                         have_ui = true;
2826                 }
2827         }
2828
2829         if (!have_ui) {
2830                 return;
2831         }
2832
2833         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
2834                         string_compose ("P-%1-%2", _route->id(), p->id()),
2835                         this,
2836                         w);
2837
2838         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
2839
2840         if (ui_xml) {
2841                 wp->set_state (*ui_xml, 0);
2842         }
2843
2844         p->set_window_proxy (wp);
2845         WM::Manager::instance().register_window (wp);
2846 }
2847
2848 void
2849 ProcessorBox::maybe_add_processor_pin_mgr (boost::weak_ptr<Processor> w)
2850 {
2851         boost::shared_ptr<Processor> p = w.lock ();
2852         if (!p || p->pinmgr_proxy ()) {
2853                 return;
2854         }
2855         if (!boost::dynamic_pointer_cast<PluginInsert> (p)) {
2856                 return;
2857         }
2858
2859         PluginPinWindowProxy* wp = new PluginPinWindowProxy (
2860                         string_compose ("PM-%1-%2", _route->id(), p->id()), w);
2861         wp->set_session (_session);
2862
2863         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
2864         if (ui_xml) {
2865                 wp->set_state (*ui_xml, 0);
2866         }
2867
2868         p->set_pingmgr_proxy (wp);
2869         WM::Manager::instance().register_window (wp);
2870 }
2871
2872 void
2873 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
2874 {
2875         boost::shared_ptr<Processor> processor (p.lock ());
2876
2877         if (!processor || ( !processor->display_to_user()
2878 #ifndef NDEBUG
2879                             && !show_all_processors
2880 #endif
2881                           )
2882            ) {
2883                 return;
2884         }
2885
2886         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
2887
2888         ProcessorEntry* e = 0;
2889         if (plugin_insert) {
2890                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
2891         } else {
2892                 e = new ProcessorEntry (this, processor, _width);
2893         }
2894
2895         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
2896         boost::shared_ptr<PortInsert> ext = boost::dynamic_pointer_cast<PortInsert> (processor);
2897         boost::shared_ptr<UnknownProcessor> stub = boost::dynamic_pointer_cast<UnknownProcessor> (processor);
2898
2899         //faders and meters are not deletable, copy/paste-able, so they shouldn't be selectable
2900         if (!send && !plugin_insert && !ext && !stub) {
2901                 e->set_selectable(false);
2902         }
2903
2904         /* Set up this entry's state from the GUIObjectState */
2905         XMLNode* proc = entry_gui_object_state (e);
2906         if (proc) {
2907                 e->set_control_state (proc);
2908         }
2909
2910         if (plugin_insert
2911 #ifdef MIXBUS
2912                         && !plugin_insert->plugin(0)->is_channelstrip()
2913 #endif
2914                  )
2915         {
2916                 processor_display.add_child (e, drag_targets());
2917         } else {
2918                 processor_display.add_child (e, drag_targets_noplugin());
2919         }
2920 }
2921
2922 void
2923 ProcessorBox::reordered ()
2924 {
2925         compute_processor_sort_keys ();
2926         setup_entry_positions ();
2927 }
2928
2929 void
2930 ProcessorBox::setup_routing_feeds ()
2931 {
2932         list<ProcessorEntry*> children = processor_display.children ();
2933         /* first set the i/o maps for every processor */
2934         list<ProcessorEntry*>::iterator prev = children.begin();
2935
2936         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
2937                 boost::shared_ptr<ARDOUR::Processor> p = (*i)->processor();
2938                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
2939
2940                 list<ProcessorEntry*>::iterator next = i;
2941                 next++;
2942
2943                 if (pi) {
2944                         ChanCount sinks = pi->natural_input_streams ();
2945                         ChanCount sources = pi->natural_output_streams ();
2946                         uint32_t count = pi->get_count ();
2947                         ChanCount in, out;
2948                         pi->configured_io (in, out);
2949
2950                         ChanCount midi_thru;
2951                         ChanCount midi_bypass;
2952                         ChanMapping input_map (pi->input_map ());
2953                         if (pi->has_midi_thru ()) {
2954                                  midi_thru.set(DataType::MIDI, 1);
2955                                  input_map.set (DataType::MIDI, 0, 0);
2956                         }
2957                         if (pi->has_midi_bypass ()) {
2958                                  midi_bypass.set(DataType::MIDI, 1);
2959                         }
2960
2961                         (*i)->input_icon.set_ports (sinks * count + midi_thru);
2962                         (*i)->output_icon.set_ports (sources * count + midi_bypass);
2963
2964                         (*i)->routing_icon.set (
2965                                         in, out,
2966                                         sinks * count + midi_thru,
2967                                         sources * count + midi_bypass,
2968                                         input_map,
2969                                         pi->output_map (),
2970                                         pi->thru_map ());
2971
2972                         if (next != children.end()) {
2973                                 (*next)->routing_icon.set_fed_by (out, sources * count + midi_bypass,
2974                                                 pi->output_map (), pi->thru_map ());
2975                         }
2976
2977                         if (prev != i) {
2978                                 (*prev)->routing_icon.set_feeding (in, sinks * count + midi_thru,
2979                                                 pi->input_map (), pi->thru_map ());
2980                         }
2981
2982                 } else {
2983                         (*i)->input_icon.set_ports (p->input_streams());
2984                         (*i)->output_icon.set_ports (p->output_streams());
2985                         ChanMapping inmap (p->input_streams ());
2986                         ChanMapping outmap (p->output_streams ());
2987                         ChanMapping thrumap;
2988                         (*i)->routing_icon.set (
2989                                         p->input_streams(),
2990                                         p->output_streams(),
2991                                         p->input_streams(),
2992                                         p->output_streams(),
2993                                         inmap, outmap, thrumap);
2994
2995                         if (next != children.end()) {
2996                                 (*next)->routing_icon.set_fed_by (
2997                                                 p->output_streams(),
2998                                                 p->output_streams(),
2999                                                 outmap, thrumap);
3000                         }
3001                         if (prev != i) {
3002                                 (*prev)->routing_icon.set_feeding (
3003                                                 p->input_streams(),
3004                                                 p->output_streams(),
3005                                                 inmap, thrumap);
3006                         }
3007                 }
3008
3009                 if (i == children.begin()) {
3010                         (*i)->routing_icon.unset_fed_by ();
3011                 }
3012                 prev = i;
3013                 (*i)->input_icon.hide();
3014         }
3015
3016         /* now set which icons need to be displayed */
3017         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3018                 (*i)->output_routing_icon.copy_state ((*i)->routing_icon);
3019
3020                 if ((*i)->routing_icon.in_identity ()) {
3021                         (*i)->routing_icon.hide();
3022                         if (i == children.begin()) {
3023                                 (*i)->input_icon.show();
3024                         }
3025                 } else {
3026                         (*i)->routing_icon.show();
3027                         (*i)->routing_icon.queue_draw();
3028                         (*i)->input_icon.show();
3029                 }
3030
3031                 list<ProcessorEntry*>::iterator next = i;
3032                 if (++next == children.end()) {
3033                         // last processor in the chain
3034                         (*i)->output_routing_icon.set_terminal(true);
3035                         (*i)->output_routing_icon.unset_feeding ();
3036                         if ((*i)->routing_icon.out_identity ()) {
3037                                 (*i)->output_routing_icon.hide();
3038                         } else {
3039                                 (*i)->output_routing_icon.show();
3040                                 (*i)->output_routing_icon.queue_draw();
3041                         }
3042                 } else {
3043                         (*i)->output_routing_icon.set_terminal(false);
3044                         if (   !(*i)->routing_icon.out_identity ()
3045                                         && !(*next)->routing_icon.in_identity ()
3046                                         &&  (*next)->routing_icon.can_coalesce ()) {
3047                                 (*i)->output_routing_icon.hide();
3048                         } else if (!(*i)->routing_icon.out_identity ()) {
3049                                 (*i)->output_routing_icon.show();
3050                                 (*i)->output_routing_icon.queue_draw();
3051                                 (*next)->input_icon.show();
3052                         } else {
3053                                 (*i)->output_routing_icon.hide();
3054                         }
3055                 }
3056         }
3057 }
3058
3059 void
3060 ProcessorBox::setup_entry_positions ()
3061 {
3062         list<ProcessorEntry*> children = processor_display.children ();
3063         bool pre_fader = true;
3064
3065         uint32_t num = 0;
3066         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3067                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor()) &&
3068                     boost::dynamic_pointer_cast<Amp>((*i)->processor())->gain_control()->parameter().type() == GainAutomation) {
3069                         pre_fader = false;
3070                         (*i)->set_position (ProcessorEntry::Fader, num++);
3071                 } else {
3072                         if (pre_fader) {
3073                                 (*i)->set_position (ProcessorEntry::PreFader, num++);
3074                         } else {
3075                                 (*i)->set_position (ProcessorEntry::PostFader, num++);
3076                         }
3077                 }
3078         }
3079         setup_routing_feeds ();
3080 }
3081
3082 void
3083 ProcessorBox::compute_processor_sort_keys ()
3084 {
3085         list<ProcessorEntry*> children = processor_display.children ();
3086         Route::ProcessorList our_processors;
3087
3088         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
3089                 if ((*i)->processor()) {
3090                         our_processors.push_back ((*i)->processor ());
3091                 }
3092         }
3093
3094         if (_route->reorder_processors (our_processors)) {
3095                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
3096                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
3097                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
3098                    when the drag is torn down after this handler function is finished.
3099                 */
3100                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
3101         }
3102 }
3103
3104 void
3105 ProcessorBox::report_failed_reorder ()
3106 {
3107         /* reorder failed, so redisplay */
3108
3109         redisplay_processors ();
3110
3111         /* now tell them about the problem */
3112
3113         ArdourDialog dialog (_("Plugin Incompatibility"));
3114         Label label;
3115
3116         label.set_text (_("\
3117 You cannot reorder these plugins/sends/inserts\n\
3118 in that way because the inputs and\n\
3119 outputs will not work correctly."));
3120
3121         dialog.get_vbox()->set_border_width (12);
3122         dialog.get_vbox()->pack_start (label);
3123         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
3124
3125         dialog.set_name (X_("PluginIODialog"));
3126         dialog.set_modal (true);
3127         dialog.show_all ();
3128
3129         dialog.run ();
3130 }
3131
3132 void
3133 ProcessorBox::rename_processors ()
3134 {
3135         ProcSelection to_be_renamed;
3136
3137         get_selected_processors (to_be_renamed);
3138
3139         if (to_be_renamed.empty()) {
3140                 return;
3141         }
3142
3143         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
3144                 rename_processor (*i);
3145         }
3146 }
3147
3148 bool
3149 ProcessorBox::can_cut () const
3150 {
3151         vector<boost::shared_ptr<Processor> > sel;
3152
3153         get_selected_processors (sel);
3154
3155         /* cut_processors () does not cut inserts */
3156
3157         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
3158
3159                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3160                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3161                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3162                         return true;
3163                 }
3164         }
3165
3166         return false;
3167 }
3168
3169 bool
3170 ProcessorBox::stub_processor_selected () const
3171 {
3172         vector<boost::shared_ptr<Processor> > sel;
3173
3174         get_selected_processors (sel);
3175
3176         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
3177                 if (boost::dynamic_pointer_cast<UnknownProcessor>((*i)) != 0) {
3178                         return true;
3179                 }
3180         }
3181
3182         return false;
3183 }
3184
3185 void
3186 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
3187 {
3188         if (to_be_removed.empty()) {
3189                 return;
3190         }
3191
3192         XMLNode* node = new XMLNode (X_("cut"));
3193         Route::ProcessorList to_cut;
3194
3195         no_processor_redisplay = true;
3196         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
3197                 // Cut only plugins, sends and returns
3198                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3199                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3200                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3201
3202                         Window* w = get_processor_ui (*i);
3203
3204                         if (w) {
3205                                 w->hide ();
3206                         }
3207
3208                         XMLNode& child ((*i)->get_state());
3209                         node->add_child_nocopy (child);
3210                         to_cut.push_back (*i);
3211                 }
3212         }
3213
3214         if (_route->remove_processors (to_cut) != 0) {
3215                 delete node;
3216                 no_processor_redisplay = false;
3217                 return;
3218         }
3219
3220         _p_selection.set (node);
3221
3222         no_processor_redisplay = false;
3223         redisplay_processors ();
3224 }
3225
3226 void
3227 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
3228 {
3229         if (to_be_copied.empty()) {
3230                 return;
3231         }
3232
3233         XMLNode* node = new XMLNode (X_("copy"));
3234
3235         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
3236                 // Copy only plugins, sends, returns
3237                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
3238                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
3239                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
3240                         node->add_child_nocopy ((*i)->get_state());
3241                 }
3242         }
3243
3244         _p_selection.set (node);
3245 }
3246
3247 void
3248 ProcessorBox::delete_processors (const ProcSelection& targets)
3249 {
3250         if (targets.empty()) {
3251                 return;
3252         }
3253
3254         no_processor_redisplay = true;
3255
3256         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
3257
3258                 Window* w = get_processor_ui (*i);
3259
3260                 if (w) {
3261                         w->hide ();
3262                 }
3263
3264                 _route->remove_processor(*i);
3265         }
3266
3267         no_processor_redisplay = false;
3268         redisplay_processors ();
3269 }
3270
3271 void
3272 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
3273 {
3274         list<boost::shared_ptr<Processor> >::const_iterator x;
3275
3276         no_processor_redisplay = true;
3277         for (x = procs.begin(); x != procs.end(); ++x) {
3278
3279                 Window* w = get_processor_ui (*x);
3280
3281                 if (w) {
3282                         w->hide ();
3283                 }
3284
3285                 _route->remove_processor(*x);
3286         }
3287
3288         no_processor_redisplay = false;
3289         redisplay_processors ();
3290 }
3291
3292 gint
3293 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
3294 {
3295         boost::shared_ptr<Processor> processor (weak_processor.lock());
3296
3297         if (!processor) {
3298                 return false;
3299         }
3300
3301         /* NOT copied to _mixer.selection() */
3302
3303         no_processor_redisplay = true;
3304         _route->remove_processor (processor);
3305         no_processor_redisplay = false;
3306         redisplay_processors ();
3307
3308         return false;
3309 }
3310
3311 void
3312 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
3313 {
3314         Prompter name_prompter (true);
3315         string result;
3316         name_prompter.set_title (_("Rename Processor"));
3317         name_prompter.set_prompt (_("New name:"));
3318         name_prompter.set_initial_text (processor->name());
3319         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
3320         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
3321         name_prompter.show_all ();
3322
3323         switch (name_prompter.run ()) {
3324
3325         case Gtk::RESPONSE_ACCEPT:
3326                 name_prompter.get_result (result);
3327                 if (result.length()) {
3328
3329                         int tries = 0;
3330                         string test = result;
3331
3332                         while (tries < 100) {
3333                                 if (_session->io_name_is_legal (test)) {
3334                                         result = test;
3335                                         break;
3336                                 }
3337                                 tries++;
3338
3339                                 test = string_compose ("%1-%2", result, tries);
3340                         }
3341
3342                         if (tries < 100) {
3343                                 processor->set_name (result);
3344                         } else {
3345                                 /* unlikely! */
3346                                 ARDOUR_UI::instance()->popup_error
3347                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
3348                         }
3349                 }
3350                 break;
3351         }
3352
3353         return;
3354 }
3355
3356 void
3357 ProcessorBox::paste_processors ()
3358 {
3359         if (_p_selection.processors.empty()) {
3360                 return;
3361         }
3362
3363         paste_processor_state (_p_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
3364 }
3365
3366 void
3367 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
3368 {
3369
3370         if (_p_selection.processors.empty()) {
3371                 return;
3372         }
3373
3374         paste_processor_state (_p_selection.processors.get_node().children(), before);
3375 }
3376
3377 void
3378 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
3379 {
3380         XMLNodeConstIterator niter;
3381         list<boost::shared_ptr<Processor> > copies;
3382
3383         if (nlist.empty()) {
3384                 return;
3385         }
3386
3387         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
3388
3389                 XMLProperty const * type = (*niter)->property ("type");
3390                 XMLProperty const * role = (*niter)->property ("role");
3391                 assert (type);
3392
3393                 boost::shared_ptr<Processor> p;
3394                 try {
3395                         if (type->value() == "meter" ||
3396                             type->value() == "main-outs" ||
3397                             type->value() == "amp" ||
3398                             type->value() == "intreturn") {
3399                                 /* do not paste meter, main outs, amp or internal returns */
3400                                 continue;
3401
3402                         } else if (type->value() == "intsend") {
3403
3404                                 /* aux sends are OK, but those used for
3405                                  * other purposes, are not.
3406                                  */
3407
3408                                 assert (role);
3409
3410                                 if (role->value() != "Aux") {
3411                                         continue;
3412                                 }
3413
3414                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
3415                                 XMLNode n (**niter);
3416                                 InternalSend* s = new InternalSend (*_session, sendpan, _route->mute_master(),
3417                                                 _route, boost::shared_ptr<Route>(), Delivery::Aux);
3418
3419                                 IOProcessor::prepare_for_reset (n, s->name());
3420
3421                                 if (s->set_state (n, Stateful::loading_state_version)) {
3422                                         delete s;
3423                                         return;
3424                                 }
3425
3426                                 p.reset (s);
3427
3428                         } else if (type->value() == "send") {
3429
3430                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
3431                                 XMLNode n (**niter);
3432
3433                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
3434
3435                                 IOProcessor::prepare_for_reset (n, s->name());
3436
3437                                 if (s->set_state (n, Stateful::loading_state_version)) {
3438                                         delete s;
3439                                         return;
3440                                 }
3441
3442                                 p.reset (s);
3443
3444                         } else if (type->value() == "return") {
3445
3446                                 XMLNode n (**niter);
3447                                 Return* r = new Return (*_session);
3448
3449                                 IOProcessor::prepare_for_reset (n, r->name());
3450
3451                                 if (r->set_state (n, Stateful::loading_state_version)) {
3452                                         delete r;
3453                                         return;
3454                                 }
3455
3456                                 p.reset (r);
3457
3458                         } else if (type->value() == "port") {
3459
3460                                 XMLNode n (**niter);
3461                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
3462
3463                                 IOProcessor::prepare_for_reset (n, pi->name());
3464
3465                                 if (pi->set_state (n, Stateful::loading_state_version)) {
3466                                         return;
3467                                 }
3468
3469                                 p.reset (pi);
3470                         } else {
3471                                 /* XXX its a bit limiting to assume that everything else
3472                                    is a plugin.
3473                                 */
3474                                 p.reset (new PluginInsert (*_session));
3475                                 /* we can't use RAII Stateful::ForceIDRegeneration
3476                                  * because that'd void copying the state and wrongly bump
3477                                  * the state-version counter.
3478                                  * we need to load the state (incl external files) first and
3479                                  * only then update the ID)
3480                                  */
3481                                 PBD::ID id = p->id();
3482                                 /* strip side-chain state (processor inside processor must be a side-chain)
3483                                  * otherwise we'll end up with duplicate ports-names.
3484                                  * (this needs a better solution which retains connections)
3485                                  * We really would want Stateful::ForceIDRegeneration here :(
3486                                  */
3487                                 XMLNode state (**niter);
3488                                 state.remove_nodes_and_delete ("Processor");
3489
3490                                 p->set_state (state, Stateful::current_state_version);
3491                                 boost::dynamic_pointer_cast<PluginInsert>(p)->update_id (id);
3492                         }
3493
3494                         copies.push_back (p);
3495                 }
3496
3497                 catch (...) {
3498                         error << _("plugin insert constructor failed") << endmsg;
3499                 }
3500         }
3501
3502         if (copies.empty()) {
3503                 return;
3504         }
3505
3506         if (_route->add_processors (copies, p)) {
3507
3508                 string msg = _(
3509                         "Copying the set of processors on the clipboard failed,\n\
3510 probably because the I/O configuration of the plugins\n\
3511 could not match the configuration of this track.");
3512                 MessageDialog am (msg);
3513                 am.run ();
3514         }
3515 }
3516
3517 void
3518 ProcessorBox::get_selected_processors (ProcSelection& processors) const
3519 {
3520         const list<ProcessorEntry*> selection = processor_display.selection (true);
3521         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
3522                 processors.push_back ((*i)->processor ());
3523         }
3524 }
3525
3526 void
3527 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
3528 {
3529         list<ProcessorEntry*> selection = processor_display.selection ();
3530         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
3531                 (this->*method) ((*i)->processor ());
3532         }
3533 }
3534
3535 void
3536 ProcessorBox::all_visible_processors_active (bool state)
3537 {
3538         _route->all_visible_processors_active (state);
3539 }
3540
3541 void
3542 ProcessorBox::ab_plugins ()
3543 {
3544         _route->ab_plugins (ab_direction);
3545         ab_direction = !ab_direction;
3546 }
3547
3548 void
3549 ProcessorBox::set_disk_io_position (DiskIOPoint diop)
3550 {
3551         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_route);
3552         if (t) {
3553                 t->set_disk_io_point (diop);
3554         }
3555 }
3556
3557 void
3558 ProcessorBox::clear_processors ()
3559 {
3560         string prompt;
3561         vector<string> choices;
3562
3563         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
3564                                    "(this cannot be undone)"), _route->name());
3565
3566         choices.push_back (_("Cancel"));
3567         choices.push_back (_("Yes, remove them all"));
3568
3569         ArdourWidgets::Choice prompter (_("Remove processors"), prompt, choices);
3570
3571         if (prompter.run () == 1) {
3572                 _route->clear_processors (PreFader);
3573                 _route->clear_processors (PostFader);
3574         }
3575 }
3576
3577 void
3578 ProcessorBox::clear_processors (Placement p)
3579 {
3580         string prompt;
3581         vector<string> choices;
3582
3583         if (p == PreFader) {
3584                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
3585                                            "(this cannot be undone)"), _route->name());
3586         } else {
3587                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
3588                                            "(this cannot be undone)"), _route->name());
3589         }
3590
3591         choices.push_back (_("Cancel"));
3592         choices.push_back (_("Yes, remove them all"));
3593
3594         ArdourWidgets::Choice prompter (_("Remove processors"), prompt, choices);
3595
3596         if (prompter.run () == 1) {
3597                 _route->clear_processors (p);
3598         }
3599 }
3600
3601 bool
3602 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
3603 {
3604         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
3605         if (at && at->freeze_state() == AudioTrack::Frozen) {
3606                 return false;
3607         }
3608
3609         if (
3610                 boost::dynamic_pointer_cast<Send> (processor) ||
3611                 boost::dynamic_pointer_cast<Return> (processor) ||
3612                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
3613                 boost::dynamic_pointer_cast<PortInsert> (processor)
3614                 ) {
3615                 return true;
3616         }
3617
3618         return false;
3619 }
3620
3621 bool
3622 ProcessorBox::one_processor_can_be_edited ()
3623 {
3624         list<ProcessorEntry*> selection = processor_display.selection ();
3625         list<ProcessorEntry*>::iterator i = selection.begin();
3626         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
3627                 ++i;
3628         }
3629
3630         return (i != selection.end());
3631 }
3632
3633 Gtk::Window*
3634 ProcessorBox::get_editor_window (boost::shared_ptr<Processor> processor, bool use_custom)
3635 {
3636         boost::shared_ptr<Send> send;
3637         boost::shared_ptr<InternalSend> internal_send;
3638         boost::shared_ptr<Return> retrn;
3639         boost::shared_ptr<PluginInsert> plugin_insert;
3640         boost::shared_ptr<PortInsert> port_insert;
3641         Window* gidget = 0;
3642
3643         /* This method may or may not return a Window, but if it does not it
3644          * will modify the parent mixer strip appearance layout to allow
3645          * "editing" the @param processor that was passed in.
3646          *
3647          * So for example, if the processor is an Amp (gain), the parent strip
3648          * will be forced back into a model where the fader controls the main gain.
3649          * If the processor is a send, then we map the send controls onto the
3650          * strip.
3651          *
3652          * Plugins and others will return a window for control.
3653          */
3654
3655         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
3656
3657                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
3658                         return 0;
3659                 }
3660         }
3661
3662         if (boost::dynamic_pointer_cast<Amp> (processor) && boost::dynamic_pointer_cast<Amp> (processor)->gain_control()->parameter().type() == GainAutomation) {
3663
3664                 if (_parent_strip) {
3665                         _parent_strip->revert_to_default_display ();
3666                 }
3667
3668         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
3669
3670                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3671                         return 0;
3672                 }
3673
3674                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
3675
3676                         gidget = new SendUIWindow (send, _session);
3677                 }
3678
3679         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
3680
3681                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
3682                         /* no GUI for these */
3683                         return 0;
3684                 }
3685
3686                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3687                         return 0;
3688                 }
3689
3690                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
3691
3692                 ReturnUIWindow *return_ui;
3693                 Window* w = get_processor_ui (retrn);
3694
3695                 if (w == 0) {
3696
3697                         return_ui = new ReturnUIWindow (retrn, _session);
3698                         return_ui->set_title (retrn->name ());
3699                         set_processor_ui (send, return_ui);
3700
3701                 } else {
3702                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
3703                 }
3704
3705                 gidget = return_ui;
3706
3707         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
3708
3709                 PluginUIWindow *plugin_ui;
3710
3711                 /* these are both allowed to be null */
3712
3713                 Window* w = get_processor_ui (plugin_insert);
3714
3715                 if (w == 0) {
3716                         plugin_ui = new PluginUIWindow (plugin_insert, false, use_custom);
3717                         plugin_ui->set_title (generate_processor_title (plugin_insert));
3718                         set_processor_ui (plugin_insert, plugin_ui);
3719
3720                 } else {
3721                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
3722                 }
3723
3724                 gidget = plugin_ui;
3725
3726         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
3727
3728                 if (!ARDOUR_UI_UTILS::engine_is_running ()) {
3729                         return 0;
3730                 }
3731
3732                 PortInsertWindow *io_selector;
3733
3734                 Window* w = get_processor_ui (port_insert);
3735
3736                 if (w == 0) {
3737                         io_selector = new PortInsertWindow (_session, port_insert);
3738                         set_processor_ui (port_insert, io_selector);
3739
3740                 } else {
3741                         io_selector = dynamic_cast<PortInsertWindow *> (w);
3742                 }
3743
3744                 gidget = io_selector;
3745         }
3746
3747         return gidget;
3748 }
3749
3750 Gtk::Window*
3751 ProcessorBox::get_generic_editor_window (boost::shared_ptr<Processor> processor)
3752 {
3753         boost::shared_ptr<PluginInsert> plugin_insert
3754                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
3755
3756         if (!plugin_insert) {
3757                 return 0;
3758         }
3759
3760         PluginUIWindow* win = new PluginUIWindow (plugin_insert, true, false);
3761         win->set_title (generate_processor_title (plugin_insert));
3762
3763         return win;
3764 }
3765
3766 void
3767 ProcessorBox::register_actions ()
3768 {
3769         /* We need to use a static object as the owner, since these actions
3770            need to be considered ownable by all ProcessorBox objects
3771         */
3772
3773         load_bindings ();
3774
3775         processor_box_actions = ActionManager::create_action_group (bindings, X_("ProcessorMenu"));
3776
3777         Glib::RefPtr<Action> act;
3778
3779         /* new stuff */
3780         ActionManager::register_action (processor_box_actions, X_("newplugin"), _("New Plugin"),
3781                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
3782
3783         act = ActionManager::register_action (processor_box_actions, X_("newinsert"), _("New Insert"),
3784                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
3785         ActionManager::engine_sensitive_actions.push_back (act);
3786         act = ActionManager::register_action (processor_box_actions, X_("newsend"), _("New External Send ..."),
3787                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
3788         ActionManager::engine_sensitive_actions.push_back (act);
3789
3790         ActionManager::register_action (processor_box_actions, X_("newaux"), _("New Aux Send ..."));
3791         ActionManager::register_action (processor_box_actions, X_("newlisten"), _("New Monitor Send ..."));
3792         ActionManager::register_action (processor_box_actions, X_("removelisten"), _("Remove Monitor Send ..."));
3793
3794         ActionManager::register_action (processor_box_actions, X_("controls"), _("Controls"));
3795         ActionManager::register_action (processor_box_actions, X_("send_options"), _("Send Options"));
3796
3797         ActionManager::register_action (processor_box_actions, X_("clear"), _("Clear (all)"),
3798                         sigc::ptr_fun (ProcessorBox::rb_clear));
3799         ActionManager::register_action (processor_box_actions, X_("clear_pre"), _("Clear (pre-fader)"),
3800                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
3801         ActionManager::register_action (processor_box_actions, X_("clear_post"), _("Clear (post-fader)"),
3802                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
3803
3804         /* standard editing stuff */
3805
3806         cut_action = ActionManager::register_action (processor_box_actions, X_("cut"), _("Cut"),
3807                                                             sigc::ptr_fun (ProcessorBox::rb_cut));
3808         copy_action = ActionManager::register_action (processor_box_actions, X_("copy"), _("Copy"),
3809                                                              sigc::ptr_fun (ProcessorBox::rb_copy));
3810         delete_action = ActionManager::register_action (processor_box_actions, X_("delete"), _("Delete"),
3811                                                                sigc::ptr_fun (ProcessorBox::rb_delete));
3812         backspace_action = ActionManager::register_action (processor_box_actions, X_("backspace"), _("Delete"),
3813                                                                sigc::ptr_fun (ProcessorBox::rb_delete));
3814
3815         ActionManager::plugin_selection_sensitive_actions.push_back (cut_action);
3816         ActionManager::plugin_selection_sensitive_actions.push_back (copy_action);
3817         ActionManager::plugin_selection_sensitive_actions.push_back (delete_action);
3818         ActionManager::plugin_selection_sensitive_actions.push_back (backspace_action);
3819
3820         paste_action = ActionManager::register_action (processor_box_actions, X_("paste"), _("Paste"),
3821                         sigc::ptr_fun (ProcessorBox::rb_paste));
3822         rename_action = ActionManager::register_action (processor_box_actions, X_("rename"), _("Rename"),
3823                         sigc::ptr_fun (ProcessorBox::rb_rename));
3824         ActionManager::register_action (processor_box_actions, X_("selectall"), _("Select All"),
3825                         sigc::ptr_fun (ProcessorBox::rb_select_all));
3826         ActionManager::register_action (processor_box_actions, X_("deselectall"), _("Deselect All"),
3827                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
3828
3829         /* activation etc. */
3830
3831         ActionManager::register_action (processor_box_actions, X_("activate_all"), _("Activate All"),
3832                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
3833         ActionManager::register_action (processor_box_actions, X_("deactivate_all"), _("Deactivate All"),
3834                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
3835         ActionManager::register_action (processor_box_actions, X_("ab_plugins"), _("A/B Plugins"),
3836                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
3837
3838         manage_pins_action = ActionManager::register_action (
3839                 processor_box_actions, X_("manage-pins"), _("Pin Connections..."),
3840                 sigc::ptr_fun (ProcessorBox::rb_manage_pins));
3841
3842         /* Disk IO stuff */
3843         disk_io_action = ActionManager::register_action (processor_box_actions, X_("disk-io-menu"), _("Disk I/O ..."));
3844         ActionManager::register_action (processor_box_actions, X_("disk-io-prefader"), _("Pre-Fader."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOPreFader));
3845         ActionManager::register_action (processor_box_actions, X_("disk-io-postfader"), _("Post-Fader."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOPostFader));
3846         ActionManager::register_action (processor_box_actions, X_("disk-io-custom"), _("Custom."), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_set_disk_io_position), DiskIOCustom));
3847
3848         /* show editors */
3849         edit_action = ActionManager::register_action (
3850                 processor_box_actions, X_("edit"), _("Edit..."),
3851                 sigc::ptr_fun (ProcessorBox::rb_edit));
3852
3853         edit_generic_action = ActionManager::register_action (
3854                 processor_box_actions, X_("edit-generic"), _("Edit with generic controls..."),
3855                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
3856
3857 }
3858
3859 void
3860 ProcessorBox::rb_edit_generic ()
3861 {
3862         if (_current_processor_box == 0) {
3863                 return;
3864         }
3865
3866         _current_processor_box->for_selected_processors (&ProcessorBox::generic_edit_processor);
3867 }
3868
3869 void
3870 ProcessorBox::rb_ab_plugins ()
3871 {
3872         if (_current_processor_box == 0) {
3873                 return;
3874         }
3875
3876         _current_processor_box->ab_plugins ();
3877 }
3878
3879 void
3880 ProcessorBox::rb_set_disk_io_position (DiskIOPoint diop)
3881 {
3882         if (_current_processor_box == 0) {
3883                 return;
3884         }
3885
3886         _current_processor_box->set_disk_io_position (diop);
3887 }
3888
3889 void
3890 ProcessorBox::rb_manage_pins ()
3891 {
3892         if (_current_processor_box == 0) {
3893                 return;
3894         }
3895
3896         _current_processor_box->for_selected_processors (&ProcessorBox::manage_pins);
3897 }
3898 void
3899 ProcessorBox::rb_choose_plugin ()
3900 {
3901         if (_current_processor_box == 0) {
3902                 return;
3903         }
3904         _current_processor_box->choose_plugin ();
3905 }
3906
3907 void
3908 ProcessorBox::rb_choose_insert ()
3909 {
3910         if (_current_processor_box == 0) {
3911                 return;
3912         }
3913         _current_processor_box->choose_insert ();
3914 }
3915
3916 void
3917 ProcessorBox::rb_choose_send ()
3918 {
3919         if (_current_processor_box == 0) {
3920                 return;
3921         }
3922         _current_processor_box->choose_send ();
3923 }
3924
3925 void
3926 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
3927 {
3928         if (_current_processor_box == 0) {
3929                 return;
3930         }
3931
3932         _current_processor_box->choose_aux (wr);
3933 }
3934
3935 void
3936 ProcessorBox::rb_remove_aux (boost::weak_ptr<Route> wr)
3937 {
3938         if (_current_processor_box == 0) {
3939                 return;
3940         }
3941
3942         _current_processor_box->remove_aux (wr);
3943 }
3944
3945 void
3946 ProcessorBox::rb_clear ()
3947 {
3948         if (_current_processor_box == 0) {
3949                 return;
3950         }
3951
3952         _current_processor_box->clear_processors ();
3953 }
3954
3955
3956 void
3957 ProcessorBox::rb_clear_pre ()
3958 {
3959         if (_current_processor_box == 0) {
3960                 return;
3961         }
3962
3963         _current_processor_box->clear_processors (PreFader);
3964 }
3965
3966
3967 void
3968 ProcessorBox::rb_clear_post ()
3969 {
3970         if (_current_processor_box == 0) {
3971                 return;
3972         }
3973
3974         _current_processor_box->clear_processors (PostFader);
3975 }
3976
3977 void
3978 ProcessorBox::rb_cut ()
3979 {
3980         if (_current_processor_box == 0) {
3981                 return;
3982         }
3983
3984         _current_processor_box->processor_operation (ProcessorsCut);
3985 }
3986
3987 void
3988 ProcessorBox::rb_delete ()
3989 {
3990         if (_current_processor_box == 0) {
3991                 return;
3992         }
3993
3994         _current_processor_box->processor_operation (ProcessorsDelete);
3995 }
3996
3997 void
3998 ProcessorBox::rb_copy ()
3999 {
4000         if (_current_processor_box == 0) {
4001                 return;
4002         }
4003         _current_processor_box->processor_operation (ProcessorsCopy);
4004 }
4005
4006 void
4007 ProcessorBox::rb_paste ()
4008 {
4009         if (_current_processor_box == 0) {
4010                 return;
4011         }
4012
4013         _current_processor_box->processor_operation (ProcessorsPaste);
4014 }
4015
4016 void
4017 ProcessorBox::rb_rename ()
4018 {
4019         if (_current_processor_box == 0) {
4020                 return;
4021         }
4022         _current_processor_box->rename_processors ();
4023 }
4024
4025 void
4026 ProcessorBox::rb_select_all ()
4027 {
4028         if (_current_processor_box == 0) {
4029                 return;
4030         }
4031
4032         _current_processor_box->processor_operation (ProcessorsSelectAll);
4033 }
4034
4035 void
4036 ProcessorBox::rb_deselect_all ()
4037 {
4038         if (_current_processor_box == 0) {
4039                 return;
4040         }
4041
4042         _current_processor_box->deselect_all_processors ();
4043 }
4044
4045 void
4046 ProcessorBox::rb_activate_all ()
4047 {
4048         if (_current_processor_box == 0) {
4049                 return;
4050         }
4051
4052         _current_processor_box->all_visible_processors_active (true);
4053 }
4054
4055 void
4056 ProcessorBox::rb_deactivate_all ()
4057 {
4058         if (_current_processor_box == 0) {
4059                 return;
4060         }
4061         _current_processor_box->all_visible_processors_active (false);
4062 }
4063
4064 void
4065 ProcessorBox::rb_edit ()
4066 {
4067         if (_current_processor_box == 0) {
4068                 return;
4069         }
4070
4071         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
4072 }
4073
4074 bool
4075 ProcessorBox::edit_aux_send (boost::shared_ptr<Processor> processor)
4076 {
4077         if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
4078                 return false;
4079         }
4080
4081         if (_parent_strip) {
4082                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
4083                 if (_parent_strip->current_delivery() == send) {
4084                         _parent_strip->revert_to_default_display ();
4085                 } else {
4086                         _parent_strip->show_send(send);
4087                 }
4088         }
4089         return true;
4090 }
4091
4092 void
4093 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
4094 {
4095         if (!processor) {
4096                 return;
4097         }
4098         if (edit_aux_send (processor)) {
4099                 return;
4100         }
4101         if (!ARDOUR_UI_UTILS::engine_is_running ()) {
4102                 return;
4103         }
4104
4105         ProcessorWindowProxy* proxy = find_window_proxy (processor);
4106
4107         if (proxy) {
4108                 proxy->set_custom_ui_mode (true);
4109                 proxy->show_the_right_window ();
4110         }
4111 }
4112
4113 void
4114 ProcessorBox::generic_edit_processor (boost::shared_ptr<Processor> processor)
4115 {
4116         if (!processor) {
4117                 return;
4118         }
4119         if (edit_aux_send (processor)) {
4120                 return;
4121         }
4122         if (!ARDOUR_UI_UTILS::engine_is_running ()) {
4123                 return;
4124         }
4125
4126         ProcessorWindowProxy* proxy = find_window_proxy (processor);
4127
4128         if (proxy) {
4129                 proxy->set_custom_ui_mode (false);
4130                 proxy->show_the_right_window ();
4131         }
4132 }
4133
4134 void
4135 ProcessorBox::manage_pins (boost::shared_ptr<Processor> processor)
4136 {
4137         if (!processor) {
4138                 return;
4139         }
4140         PluginPinWindowProxy* proxy = processor->pinmgr_proxy ();
4141         if (proxy) {
4142                 proxy->get (true);
4143                 proxy->present();
4144         }
4145 }
4146
4147
4148 void
4149 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
4150 {
4151         if (!what_changed.contains (ARDOUR::Properties::name)) {
4152                 return;
4153         }
4154
4155         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
4156
4157         boost::shared_ptr<Processor> processor;
4158         boost::shared_ptr<PluginInsert> plugin_insert;
4159         boost::shared_ptr<Send> send;
4160
4161         list<ProcessorEntry*> children = processor_display.children();
4162
4163         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
4164
4165                 processor = (*iter)->processor ();
4166
4167                 if (!processor) {
4168                         continue;
4169                 }
4170
4171                 Window* w = get_processor_ui (processor);
4172
4173                 if (!w) {
4174                         continue;
4175                 }
4176
4177                 /* rename editor windows for sends and plugins */
4178
4179                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
4180                         w->set_title (send->name ());
4181                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
4182                         w->set_title (generate_processor_title (plugin_insert));
4183                 }
4184         }
4185 }
4186
4187 string
4188 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
4189 {
4190         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
4191         string::size_type email_pos;
4192
4193         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
4194                 maker = maker.substr (0, email_pos - 1);
4195         }
4196
4197         if (maker.length() > 32) {
4198                 maker = maker.substr (0, 32);
4199                 maker += " ...";
4200         }
4201
4202         SessionObject* owner = pi->owner();
4203
4204         if (owner) {
4205                 return string_compose(_("%1: %2 (by %3)"), owner->name(), pi->name(), maker);
4206         } else {
4207                 return string_compose(_("%1 (by %2)"), pi->name(), maker);
4208         }
4209 }
4210
4211 /** @param p Processor.
4212  *  @return the UI window for \a p.
4213  */
4214 Window *
4215 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
4216 {
4217         ProcessorWindowProxy* wp = p->window_proxy();
4218         if (wp) {
4219                 return wp->get ();
4220         }
4221         return 0;
4222 }
4223
4224 /** Make a note of the UI window that a processor is using.
4225  *  @param p Processor.
4226  *  @param w UI window.
4227  */
4228 void
4229 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
4230 {
4231         assert (p->window_proxy());
4232         p->window_proxy()->use_window (*w);
4233 }
4234
4235 void
4236 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
4237 {
4238         boost::shared_ptr<Delivery> d = w.lock ();
4239         if (!d) {
4240                 return;
4241         }
4242
4243         list<ProcessorEntry*> children = processor_display.children ();
4244         list<ProcessorEntry*>::const_iterator i = children.begin();
4245         while (i != children.end() && (*i)->processor() != d) {
4246                 ++i;
4247         }
4248
4249         if (i == children.end()) {
4250                 processor_display.set_active (0);
4251         } else {
4252                 processor_display.set_active (*i);
4253         }
4254 }
4255
4256 /** Called to repair the damage of Editor::show_window doing a show_all() */
4257 void
4258 ProcessorBox::hide_things ()
4259 {
4260         list<ProcessorEntry*> c = processor_display.children ();
4261         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
4262                 (*i)->hide_things ();
4263         }
4264 }
4265
4266 void
4267 ProcessorBox::processor_menu_unmapped ()
4268 {
4269         processor_display.remove_placeholder ();
4270         /* make all possibly-desensitized actions sensitive again so that
4271            they be activated by other means (e.g. bindings)
4272         */
4273         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, true);
4274 }
4275
4276 XMLNode *
4277 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
4278 {
4279         if (!_parent_strip) {
4280                 return 0;
4281         }
4282
4283         GUIObjectState& st = _parent_strip->gui_object_state ();
4284
4285         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
4286         assert (strip);
4287         return st.get_or_add_node (strip, entry->state_id());
4288 }
4289
4290 void
4291 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
4292 {
4293         XMLNode* proc = entry_gui_object_state (entry);
4294         if (!proc) {
4295                 return;
4296         }
4297
4298         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
4299         proc->remove_nodes_and_delete (X_("Object"));
4300         entry->add_control_state (proc);
4301 }
4302
4303 bool
4304 ProcessorBox::is_editor_mixer_strip() const
4305 {
4306         return _parent_strip && !_parent_strip->mixer_owned();
4307 }
4308
4309 ProcessorWindowProxy::ProcessorWindowProxy (string const & name, ProcessorBox* box, boost::weak_ptr<Processor> processor)
4310         : WM::ProxyBase (name, string())
4311         , _processor_box (box)
4312         , _processor (processor)
4313         , is_custom (true)
4314         , want_custom (true)
4315 {
4316         boost::shared_ptr<Processor> p = _processor.lock ();
4317         if (!p) {
4318                 return;
4319         }
4320         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&ProcessorWindowProxy::processor_going_away, this), gui_context());
4321
4322         p->ToggleUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::show_the_right_window, this, false), gui_context());
4323         p->ShowUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::show_the_right_window, this, true), gui_context());
4324         p->HideUI.connect (gui_connections, invalidator (*this), boost::bind (&ProcessorWindowProxy::hide, this), gui_context());
4325 }
4326
4327 ProcessorWindowProxy::~ProcessorWindowProxy()
4328 {
4329         /* processor window proxies do not own the windows they create with
4330          * ::get(), so set _window to null before the normal WindowProxy method
4331          * deletes it.
4332          */
4333         _window = 0;
4334 }
4335
4336 void
4337 ProcessorWindowProxy::processor_going_away ()
4338 {
4339         gui_connections.drop_connections ();
4340         delete _window;
4341         _window = 0;
4342         WM::Manager::instance().remove (this);
4343         /* should be no real reason to do this, since the object that would
4344            send DropReferences is about to be deleted, but lets do it anyway.
4345         */
4346         going_away_connection.disconnect();
4347         delete this;
4348 }
4349
4350 ARDOUR::SessionHandlePtr*
4351 ProcessorWindowProxy::session_handle()
4352 {
4353         /* we don't care */
4354         return 0;
4355 }
4356
4357 XMLNode&
4358 ProcessorWindowProxy::get_state ()
4359 {
4360         XMLNode *node;
4361         node = &ProxyBase::get_state();
4362         node->set_property (X_("custom-ui"), is_custom);
4363         return *node;
4364 }
4365
4366 int
4367 ProcessorWindowProxy::set_state (const XMLNode& node, int /*version*/)
4368 {
4369         XMLNodeList children = node.children ();
4370         XMLNodeList::const_iterator i = children.begin ();
4371         while (i != children.end()) {
4372                 std::string name;
4373                 if ((*i)->name() == X_("Window") && (*i)->get_property (X_("name"), name) && name == _name) {
4374                         break;
4375                 }
4376                 ++i;
4377         }
4378
4379         if (i != children.end()) {
4380                 (*i)->get_property (X_("custom-ui"), want_custom);
4381         }
4382
4383         return ProxyBase::set_state (node, 0);
4384 }
4385
4386 Gtk::Window*
4387 ProcessorWindowProxy::get (bool create)
4388 {
4389         boost::shared_ptr<Processor> p = _processor.lock ();
4390
4391         if (!p) {
4392                 return 0;
4393         }
4394         if (_window && (is_custom != want_custom)) {
4395                 /* drop existing window - wrong type */
4396                 set_state_mask (Gtkmm2ext::WindowProxy::StateMask (state_mask () & ~WindowProxy::Size));
4397                 drop_window ();
4398         }
4399
4400         if (!_window) {
4401                 if (!create) {
4402                         return 0;
4403                 }
4404
4405                 is_custom = want_custom;
4406                 _window = _processor_box->get_editor_window (p, is_custom);
4407
4408                 if (_window) {
4409                         setup ();
4410                         _window->show_all ();
4411                 }
4412         }
4413         return _window;
4414 }
4415
4416 void
4417 ProcessorWindowProxy::show_the_right_window (bool show_not_toggle)
4418 {
4419         if (_window && (is_custom != want_custom)) {
4420                 /* drop existing window - wrong type */
4421                 set_state_mask (Gtkmm2ext::WindowProxy::StateMask (state_mask () & ~WindowProxy::Size));
4422                 drop_window ();
4423         }
4424         if (_window && fully_visible () && show_not_toggle) {
4425                 return;
4426         }
4427         toggle ();
4428 }
4429
4430
4431 PluginPinWindowProxy::PluginPinWindowProxy(std::string const &name, boost::weak_ptr<ARDOUR::Processor> processor)
4432         : WM::ProxyBase (name, string())
4433         , _processor (processor)
4434 {
4435         boost::shared_ptr<Processor> p = _processor.lock ();
4436         if (!p) {
4437                 return;
4438         }
4439         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&PluginPinWindowProxy::processor_going_away, this), gui_context());
4440 }
4441
4442 PluginPinWindowProxy::~PluginPinWindowProxy()
4443 {
4444         _window = 0;
4445 }
4446
4447 ARDOUR::SessionHandlePtr*
4448 PluginPinWindowProxy::session_handle ()
4449 {
4450         ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
4451         if (aw) { return aw; }
4452         return 0;
4453 }
4454
4455 Gtk::Window*
4456 PluginPinWindowProxy::get (bool create)
4457 {
4458         boost::shared_ptr<Processor> p = _processor.lock ();
4459         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
4460         if (!p || !pi) {
4461                 return 0;
4462         }
4463
4464         if (!_window) {
4465                 if (!create) {
4466                         return 0;
4467                 }
4468                 _window = new PluginPinDialog (pi);
4469                 ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
4470                 if (aw) {
4471                         aw->set_session (_session);
4472                 }
4473                 _window->show_all ();
4474         }
4475         return _window;
4476 }
4477
4478 void
4479 PluginPinWindowProxy::processor_going_away ()
4480 {
4481         delete _window;
4482         _window = 0;
4483         WM::Manager::instance().remove (this);
4484         going_away_connection.disconnect();
4485         delete this;
4486 }
4487
4488 void
4489 ProcessorBox::load_bindings ()
4490 {
4491         bindings = Bindings::get_bindings (X_("Processor Box"));
4492 }