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