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