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