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