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