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