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