Make faders visually desensitised when their tracks are in automation-play.
[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
32 #include <glibmm/miscutils.h>
33
34 #include <gtkmm/messagedialog.h>
35
36 #include <gtkmm2ext/gtk_ui.h>
37 #include <gtkmm2ext/utils.h>
38 #include <gtkmm2ext/choice.h>
39 #include <gtkmm2ext/utils.h>
40 #include <gtkmm2ext/doi.h>
41
42 #include "ardour/amp.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/audioengine.h"
45 #include "ardour/internal_return.h"
46 #include "ardour/internal_send.h"
47 #include "ardour/plugin_insert.h"
48 #include "ardour/port_insert.h"
49 #include "ardour/profile.h"
50 #include "ardour/return.h"
51 #include "ardour/route.h"
52 #include "ardour/send.h"
53 #include "ardour/session.h"
54 #include "ardour/types.h"
55
56 #include "actions.h"
57 #include "ardour_dialog.h"
58 #include "ardour_ui.h"
59 #include "gui_thread.h"
60 #include "io_selector.h"
61 #include "keyboard.h"
62 #include "mixer_ui.h"
63 #include "mixer_strip.h"
64 #include "plugin_selector.h"
65 #include "plugin_ui.h"
66 #include "port_insert_ui.h"
67 #include "processor_box.h"
68 #include "public_editor.h"
69 #include "return_ui.h"
70 #include "route_processor_selection.h"
71 #include "send_ui.h"
72 #include "utils.h"
73
74 #include "i18n.h"
75
76 #ifdef AUDIOUNIT_SUPPORT
77 class AUPluginUI;
78 #endif
79
80 using namespace std;
81 using namespace ARDOUR;
82 using namespace PBD;
83 using namespace Gtk;
84 using namespace Glib;
85 using namespace Gtkmm2ext;
86
87 ProcessorBox* ProcessorBox::_current_processor_box = 0;
88 RefPtr<Action> ProcessorBox::paste_action;
89 RefPtr<Action> ProcessorBox::cut_action;
90 RefPtr<Action> ProcessorBox::rename_action;
91 RefPtr<Action> ProcessorBox::edit_action;
92 RefPtr<Action> ProcessorBox::edit_generic_action;
93 Glib::RefPtr<Gdk::Pixbuf> ProcessorEntry::_slider_pixbuf;
94 Glib::RefPtr<Gdk::Pixbuf> ProcessorEntry::_slider_pixbuf_desensitised;
95
96 ProcessorEntry::ProcessorEntry (ProcessorBox* parent, boost::shared_ptr<Processor> p, Width w)
97         : _button (ArdourButton::led_default_elements)
98         , _position (PreFader)
99         , _parent (parent)
100         , _processor (p)
101         , _width (w)
102         , _visual_state (Gtk::STATE_NORMAL)
103 {
104         _vbox.show ();
105         
106         _button.set_diameter (3);
107         _button.set_distinct_led_click (true);
108         _button.set_led_left (true);
109         _button.signal_led_clicked.connect (sigc::mem_fun (*this, &ProcessorEntry::led_clicked));
110         _button.set_text (name (_width));
111
112         if (_processor) {
113
114                 _vbox.pack_start (_button, true, true);
115
116                 _button.set_active (_processor->active());
117                 _button.show ();
118                 
119                 _processor->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_active_changed, this), gui_context());
120                 _processor->PropertyChanged.connect (name_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_property_changed, this, _1), gui_context());
121
122                 set<Evoral::Parameter> p = _processor->what_can_be_automated ();
123                 for (set<Evoral::Parameter>::iterator i = p.begin(); i != p.end(); ++i) {
124                         
125                         Control* c = new Control (
126                                 _slider_pixbuf,
127                                 _slider_pixbuf_desensitised,
128                                 _processor->automation_control (*i),
129                                 _processor->describe_parameter (*i)
130                                 );
131                         
132                         _controls.push_back (c);
133
134                         if (boost::dynamic_pointer_cast<Amp> (_processor) == 0) {
135                                 /* Add non-Amp controls to the processor box */
136                                 _vbox.pack_start (c->box);
137                         }
138
139                         if (boost::dynamic_pointer_cast<Send> (_processor)) {
140                                 /* Don't label send faders */
141                                 c->hide_label ();
142                         }
143                 }
144
145                 setup_tooltip ();
146                 setup_visuals ();
147         } else {
148                 _vbox.set_size_request (-1, _button.size_request().height);
149         }
150 }
151
152 ProcessorEntry::~ProcessorEntry ()
153 {
154         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
155                 delete *i;
156         }
157 }
158
159 EventBox&
160 ProcessorEntry::action_widget ()
161 {
162         return _button;
163 }
164
165 Gtk::Widget&
166 ProcessorEntry::widget ()
167 {
168         return _vbox;
169 }
170
171 string
172 ProcessorEntry::drag_text () const
173 {
174         return name (Wide);
175 }
176
177 void
178 ProcessorEntry::set_position (Position p)
179 {
180         _position = p;
181         setup_visuals ();
182 }
183
184 void
185 ProcessorEntry::set_visual_state (Gtkmm2ext::VisualState s, bool yn)
186 {
187         if (yn) {
188                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() | s));
189         } else {
190                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() & ~s));
191         }
192 }
193
194 void
195 ProcessorEntry::setup_visuals ()
196 {
197         switch (_position) {
198         case PreFader:
199                 _button.set_name ("processor prefader");
200                 break;
201
202         case Fader:
203                 _button.set_name ("processor fader");
204                 break;
205
206         case PostFader:
207                 _button.set_name ("processor postfader");
208                 break;
209         }
210 }
211
212
213 boost::shared_ptr<Processor>
214 ProcessorEntry::processor () const
215 {
216         return _processor;
217 }
218
219 void
220 ProcessorEntry::set_enum_width (Width w)
221 {
222         _width = w;
223 }
224
225 void
226 ProcessorEntry::led_clicked()
227 {
228         if (_processor) {
229                 if (_button.get_active ()) {
230                         _processor->deactivate ();
231                 } else {
232                         _processor->activate ();
233                 }
234         }
235 }
236
237 void
238 ProcessorEntry::processor_active_changed ()
239 {
240         if (_processor) {
241                 _button.set_active (_processor->active());
242         }
243 }
244
245 void
246 ProcessorEntry::processor_property_changed (const PropertyChange& what_changed)
247 {
248         if (what_changed.contains (ARDOUR::Properties::name)) {
249                 _button.set_text (name (_width));
250                 setup_tooltip ();
251         }
252 }
253
254 void
255 ProcessorEntry::setup_tooltip ()
256 {
257         ARDOUR_UI::instance()->set_tip (_button, name (Wide));
258 }
259
260 string
261 ProcessorEntry::name (Width w) const
262 {
263         boost::shared_ptr<Send> send;
264         string name_display;
265
266         if (!_processor) {
267                 return string();
268         }
269
270         if ((send = boost::dynamic_pointer_cast<Send> (_processor)) != 0 &&
271             !boost::dynamic_pointer_cast<InternalSend>(_processor)) {
272
273                 name_display += '>';
274
275                 /* grab the send name out of its overall name */
276
277                 string::size_type lbracket, rbracket;
278                 lbracket = send->name().find ('[');
279                 rbracket = send->name().find (']');
280
281                 switch (w) {
282                 case Wide:
283                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
284                         break;
285                 case Narrow:
286                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
287                         break;
288                 }
289
290         } else {
291
292                 switch (w) {
293                 case Wide:
294                         name_display += _processor->display_name();
295                         break;
296                 case Narrow:
297                         name_display += PBD::short_version (_processor->display_name(), 5);
298                         break;
299                 }
300
301         }
302
303         return name_display;
304 }
305
306 void
307 ProcessorEntry::setup_slider_pix ()
308 {
309         _slider_pixbuf = ::get_icon ("fader_belt_h_thin");
310         assert (_slider_pixbuf);
311         _slider_pixbuf_desensitised = ::get_icon ("fader_belt_h_thin_desensitised");
312         assert (_slider_pixbuf_desensitised);
313 }
314
315 void
316 ProcessorEntry::set_pixel_width (int p)
317 {
318         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
319                 (*i)->set_pixel_width (p);
320         }
321 }
322
323 void
324 ProcessorEntry::show_all_controls ()
325 {
326         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
327                 (*i)->set_visible (true);
328         }
329
330         _parent->update_gui_object_state (this);
331 }
332
333 void
334 ProcessorEntry::hide_all_controls ()
335 {
336         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
337                 (*i)->set_visible (false);
338         }
339
340         _parent->update_gui_object_state (this);
341 }
342
343 void
344 ProcessorEntry::add_control_state (XMLNode* node) const
345 {
346         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
347                 (*i)->add_state (node);
348         }
349 }
350
351 void
352 ProcessorEntry::set_control_state (XMLNode const * node)
353 {
354         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
355                 (*i)->set_state (node);
356         }
357 }
358
359 string
360 ProcessorEntry::state_id () const
361 {
362         return string_compose ("processor %1", _processor->id().to_s());
363 }
364
365 void
366 ProcessorEntry::hide_things ()
367 {
368         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
369                 (*i)->hide_things ();
370         }
371 }
372
373
374 Menu *
375 ProcessorEntry::build_controls_menu ()
376 {
377         using namespace Menu_Helpers;
378         Menu* menu = manage (new Menu);
379         MenuList& items = menu->items ();
380
381         items.push_back (
382                 MenuElem (_("Show All Controls"), sigc::mem_fun (*this, &ProcessorEntry::show_all_controls))
383                 );
384                 
385         items.push_back (
386                 MenuElem (_("Hide All Controls"), sigc::mem_fun (*this, &ProcessorEntry::hide_all_controls))
387                 );
388
389         if (!_controls.empty ()) {
390                 items.push_back (SeparatorElem ());
391         }
392         
393         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
394                 items.push_back (CheckMenuElem ((*i)->name ()));
395                 CheckMenuItem* c = dynamic_cast<CheckMenuItem*> (&items.back ());
396                 c->set_active ((*i)->visible ());
397                 c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ProcessorEntry::toggle_control_visibility), *i));
398         }
399
400         return menu;
401 }
402
403 void
404 ProcessorEntry::toggle_control_visibility (Control* c)
405 {
406         c->set_visible (!c->visible ());
407         _parent->update_gui_object_state (this);
408 }
409
410 ProcessorEntry::Control::Control (Glib::RefPtr<Gdk::Pixbuf> s, Glib::RefPtr<Gdk::Pixbuf> sd, boost::shared_ptr<AutomationControl> c, string const & n)
411         : _control (c)
412         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
413         , _slider (s, sd, &_adjustment, 0, false)
414         , _button (ArdourButton::Element (ArdourButton::Text | ArdourButton::Indicator))
415         , _ignore_ui_adjustment (false)
416         , _visible (false)
417         , _name (n)
418 {
419         _slider.set_controllable (c);
420
421         if (c->toggled()) {
422                 _button.set_text (_name);
423                 _button.set_led_left (true);
424                 _button.set_name ("processor control button");
425                 box.pack_start (_button);
426                 _button.show ();
427
428                 _button.signal_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked));
429                 _button.signal_led_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked));
430                 c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
431
432         } else {
433                 
434                 box.pack_start (_label);
435                 _label.show ();
436                 _label.set_text (_name);
437                 box.pack_start (_slider);
438                 _slider.show ();
439
440                 double const lo = c->internal_to_interface (c->lower ());
441                 double const up = c->internal_to_interface (c->upper ());
442                 
443                 _adjustment.set_lower (lo);
444                 _adjustment.set_upper (up);
445                 _adjustment.set_step_increment ((up - lo) / 100);
446                 _adjustment.set_page_increment ((up - lo) / 10);
447                 _slider.set_default_value (c->internal_to_interface (c->normal ()));
448                 
449                 _adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
450                 c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
451         }
452
453         control_changed ();
454         set_tooltip ();
455 }
456
457 void
458 ProcessorEntry::Control::set_tooltip ()
459 {
460         boost::shared_ptr<AutomationControl> c = _control.lock ();
461
462         if (!c) {
463                 return;
464         }
465         
466         stringstream s;
467         s << _name << ": ";
468         if (c->toggled ()) {
469                 s << (c->get_value() > 0.5 ? _("on") : _("off"));
470         } else {
471                 s << c->internal_to_interface (c->get_value ());
472         }
473         
474         ARDOUR_UI::instance()->set_tip (_label, s.str ());
475         ARDOUR_UI::instance()->set_tip (_slider, s.str ());
476         ARDOUR_UI::instance()->set_tip (_button, s.str ());
477 }
478
479 void
480 ProcessorEntry::Control::set_pixel_width (int p)
481 {
482         _slider.set_fader_length (p);
483 }
484
485 void
486 ProcessorEntry::Control::slider_adjusted ()
487 {
488         if (_ignore_ui_adjustment) {
489                 return;
490         }
491         
492         boost::shared_ptr<AutomationControl> c = _control.lock ();
493
494         if (!c) {
495                 return;
496         }
497
498         c->set_value (c->interface_to_internal (_adjustment.get_value ()));
499 }
500
501 void
502 ProcessorEntry::Control::button_clicked ()
503 {
504         boost::shared_ptr<AutomationControl> c = _control.lock ();
505
506         if (!c) {
507                 return;
508         }
509
510         bool const n = _button.get_active ();
511
512         c->set_value (n ? 0 : 1);
513         _button.set_active (!n);
514 }
515
516 void
517 ProcessorEntry::Control::control_changed ()
518 {
519         boost::shared_ptr<AutomationControl> c = _control.lock ();
520         if (!c) {
521                 return;
522         }
523
524         _ignore_ui_adjustment = true;
525
526         if (c->toggled ()) {
527
528                 _button.set_active (c->get_value() > 0.5);
529                 
530         } else {
531
532                 _adjustment.set_value (c->internal_to_interface (c->get_value ()));
533                 
534                 stringstream s;
535                 s.precision (1);
536                 s.setf (ios::fixed, ios::floatfield);
537                 s << c->internal_to_user (c->get_value ());
538                 
539                 _slider.set_tooltip_text (s.str ());
540         }
541         
542         _ignore_ui_adjustment = false;
543 }
544
545 void
546 ProcessorEntry::Control::add_state (XMLNode* node) const
547 {
548         XMLNode* c = new XMLNode (X_("Object"));
549         c->add_property (X_("id"), state_id ());
550         c->add_property (X_("visible"), _visible);
551         node->add_child_nocopy (*c);
552 }
553
554 void
555 ProcessorEntry::Control::set_state (XMLNode const * node)
556 {
557         XMLNode* n = GUIObjectState::get_node (node, state_id ());
558         if (n) {
559                 XMLProperty* p = n->property (X_("visible"));
560                 set_visible (p && string_is_affirmative (p->value ()));
561         } else {
562                 set_visible (false);
563         }
564 }
565
566 void
567 ProcessorEntry::Control::set_visible (bool v)
568 {
569         if (v) {
570                 box.show ();
571         } else {
572                 box.hide ();
573         }
574         
575         _visible = v;
576 }
577
578 /** Called when the Editor might have re-shown things that
579     we want hidden.
580 */
581 void
582 ProcessorEntry::Control::hide_things ()
583 {
584         if (!_visible) {
585                 box.hide ();
586         }
587 }
588
589 void
590 ProcessorEntry::Control::hide_label ()
591 {
592         _label.hide ();
593 }
594
595 string
596 ProcessorEntry::Control::state_id () const
597 {
598         boost::shared_ptr<AutomationControl> c = _control.lock ();
599         assert (c);
600
601         return string_compose (X_("control %1"), c->id().to_s ());
602 }
603
604 BlankProcessorEntry::BlankProcessorEntry (ProcessorBox* b, Width w)
605         : ProcessorEntry (b, boost::shared_ptr<Processor>(), w)
606 {
607 }
608
609 PluginInsertProcessorEntry::PluginInsertProcessorEntry (ProcessorBox* b, boost::shared_ptr<ARDOUR::PluginInsert> p, Width w)
610         : ProcessorEntry (b, p, w)
611         , _plugin_insert (p)
612 {
613         p->SplittingChanged.connect (
614                 _splitting_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::plugin_insert_splitting_changed, this), gui_context()
615                 );
616
617         _splitting_icon.set_size_request (-1, 12);
618
619         _vbox.pack_start (_splitting_icon);
620         _vbox.reorder_child (_splitting_icon, 0);
621
622         plugin_insert_splitting_changed ();
623 }
624
625 void
626 PluginInsertProcessorEntry::plugin_insert_splitting_changed ()
627 {
628         if (_plugin_insert->splitting ()) {
629                 _splitting_icon.show ();
630         } else {
631                 _splitting_icon.hide ();
632         }
633 }
634
635 void
636 PluginInsertProcessorEntry::hide_things ()
637 {
638         ProcessorEntry::hide_things ();
639         plugin_insert_splitting_changed ();
640 }
641
642 void
643 PluginInsertProcessorEntry::setup_visuals ()
644 {
645         switch (_position) {
646         case PreFader:
647                 _splitting_icon.set_name ("ProcessorPreFader");
648                 break;
649
650         case Fader:
651                 _splitting_icon.set_name ("ProcessorFader");
652                 break;
653
654         case PostFader:
655                 _splitting_icon.set_name ("ProcessorPostFader");
656                 break;
657         }
658
659         ProcessorEntry::setup_visuals ();
660 }
661
662 bool
663 PluginInsertProcessorEntry::SplittingIcon::on_expose_event (GdkEventExpose* ev)
664 {
665         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
666
667         cairo_set_line_width (cr, 1);
668
669         double const width = ev->area.width;
670         double const height = ev->area.height;
671
672         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
673         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
674
675         cairo_rectangle (cr, 0, 0, width, height);
676         cairo_fill (cr);
677
678         Gdk::Color const fg = get_style()->get_fg (STATE_NORMAL);
679         cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
680
681         cairo_move_to (cr, width * 0.3, height);
682         cairo_line_to (cr, width * 0.3, height * 0.5);
683         cairo_line_to (cr, width * 0.7, height * 0.5);
684         cairo_line_to (cr, width * 0.7, height);
685         cairo_move_to (cr, width * 0.5, height * 0.5);
686         cairo_line_to (cr, width * 0.5, 0);
687         cairo_stroke (cr);
688
689         return true;
690 }
691
692 ProcessorBox::ProcessorBox (ARDOUR::Session* sess, boost::function<PluginSelector*()> get_plugin_selector,
693                             RouteProcessorSelection& rsel, MixerStrip* parent, bool owner_is_mixer)
694         : _parent_strip (parent)
695         , _owner_is_mixer (owner_is_mixer)
696         , ab_direction (true)
697         , _get_plugin_selector (get_plugin_selector)
698         , _placement (-1)
699         , _visible_prefader_processors (0)
700         , _rr_selection(rsel)
701 {
702         set_session (sess);
703
704         _width = Wide;
705         processor_menu = 0;
706         no_processor_redisplay = false;
707
708         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
709         processor_scroller.add (processor_display);
710         pack_start (processor_scroller, true, true);
711
712         processor_display.set_flags (CAN_FOCUS);
713         processor_display.set_name ("ProcessorList");
714         processor_display.set_size_request (48, -1);
715         processor_display.set_data ("processorbox", this);
716         processor_display.set_spacing (2);
717
718         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
719         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
720
721         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
722         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
723
724         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
725         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
726
727         processor_scroller.show ();
728         processor_display.show ();
729
730         if (parent) {
731                 parent->DeliveryChanged.connect (
732                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
733                         );
734         }
735
736         ARDOUR_UI::instance()->set_tip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
737 }
738
739 ProcessorBox::~ProcessorBox ()
740 {
741         /* it may appear as if we should delete processor_menu but that is a
742          * pointer to a widget owned by the UI Manager, and has potentially
743          * be returned to many other ProcessorBoxes. GTK doesn't really make
744          * clear the ownership of this widget, which is a menu and thus is
745          * never packed into any container other than an implict GtkWindow.
746          *
747          * For now, until or if we ever get clarification over the ownership
748          * story just let it continue to exist. At worst, its a small memory leak.
749          */
750 }
751
752 void
753 ProcessorBox::set_route (boost::shared_ptr<Route> r)
754 {
755         if (_route == r) {
756                 return;
757         }
758
759         _route_connections.drop_connections();
760
761         /* new route: any existing block on processor redisplay must be meaningless */
762         no_processor_redisplay = false;
763         _route = r;
764
765         _route->processors_changed.connect (
766                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
767                 );
768
769         _route->DropReferences.connect (
770                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
771                 );
772
773         _route->PropertyChanged.connect (
774                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
775                 );
776
777         redisplay_processors ();
778 }
779
780 void
781 ProcessorBox::route_going_away ()
782 {
783         /* don't keep updating display as processors are deleted */
784         no_processor_redisplay = true;
785         _route.reset ();
786 }
787
788 void
789 ProcessorBox::object_drop(DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
790 {
791         boost::shared_ptr<Processor> p;
792         if (position) {
793                 p = position->processor ();
794                 if (!p) {
795                         /* dropped on the blank entry (which will be before the
796                            fader), so use the first non-blank child as our
797                            `dropped on' processor */
798                         list<ProcessorEntry*> c = processor_display.children ();
799                         list<ProcessorEntry*>::iterator i = c.begin ();
800                         while (dynamic_cast<BlankProcessorEntry*> (*i)) {
801                                 assert (i != c.end ());
802                                 ++i;
803                         }
804
805                         assert (i != c.end ());
806                         p = (*i)->processor ();
807                         assert (p);
808                 }
809         }
810
811         list<ProcessorEntry*> children = source->selection ();
812         list<boost::shared_ptr<Processor> > procs;
813         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
814                 if ((*i)->processor ()) {
815                         procs.push_back ((*i)->processor ());
816                 }
817         }
818
819         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
820                 XMLNode& state = (*i)->get_state ();
821                 XMLNodeList nlist;
822                 nlist.push_back (&state);
823                 paste_processor_state (nlist, p);
824                 delete &state;
825         }
826
827         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
828            ourselves.
829         */
830
831         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
832                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
833                 if (other) {
834                         other->delete_dragged_processors (procs);
835                 }
836         }
837 }
838
839 void
840 ProcessorBox::set_width (Width w)
841 {
842         if (_width == w) {
843                 return;
844         }
845
846         _width = w;
847
848         list<ProcessorEntry*> children = processor_display.children ();
849         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
850                 (*i)->set_enum_width (w);
851         }
852
853         redisplay_processors ();
854 }
855
856 Gtk::Menu*
857 ProcessorBox::build_possible_aux_menu ()
858 {
859         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
860
861         if (rl->empty()) {
862                 return 0;
863         }
864
865         using namespace Menu_Helpers;
866         Menu* menu = manage (new Menu);
867         MenuList& items = menu->items();
868
869         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
870                 if (!_route->internal_send_for (*r) && *r != _route) {
871                         items.push_back (MenuElem ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
872                 }
873         }
874
875         return menu;
876 }
877
878 void
879 ProcessorBox::show_processor_menu (int arg)
880 {
881         if (processor_menu == 0) {
882                 processor_menu = build_processor_menu ();
883                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
884         }
885
886         /* Sort out the plugin submenu */
887
888         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
889
890         if (plugin_menu_item) {
891                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
892         }
893
894         /* And the aux submenu */
895
896         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
897
898         if (aux_menu_item) {
899                 Menu* m = build_possible_aux_menu();
900                 if (m && !m->items().empty()) {
901                         aux_menu_item->set_submenu (*m);
902                         aux_menu_item->set_sensitive (true);
903                 } else {
904                         /* stupid gtkmm: we need to pass a null reference here */
905                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
906                         aux_menu_item->set_sensitive (false);
907                 }
908         }
909
910         ProcessorEntry* single_selection = 0;
911         if (processor_display.selection().size() == 1) {
912                 single_selection = processor_display.selection().front();
913         }
914
915         /* And the controls submenu */
916
917         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
918
919         if (controls_menu_item) {
920                 if (single_selection) {
921                         Menu* m = single_selection->build_controls_menu ();
922                         if (m && !m->items().empty()) {
923                                 controls_menu_item->set_submenu (*m);
924                                 controls_menu_item->set_sensitive (true);
925                         } else {
926                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
927                                 controls_menu_item->set_sensitive (false);
928                         }
929                 }
930         }
931
932         /* Sensitise actions as approprioate */
933
934         cut_action->set_sensitive (can_cut());
935         paste_action->set_sensitive (!_rr_selection.processors.empty());
936
937         const bool sensitive = !processor_display.selection().empty();
938         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
939         edit_action->set_sensitive (one_processor_can_be_edited ());
940
941         boost::shared_ptr<PluginInsert> pi;
942         if (single_selection) {
943                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
944         }
945
946         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
947         edit_generic_action->set_sensitive (pi && pi->plugin()->has_editor ());
948
949         /* disallow rename for multiple selections, for plugin inserts and for the fader */
950         rename_action->set_sensitive (single_selection && !pi && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ()));
951
952         processor_menu->popup (1, arg);
953
954         /* Add a placeholder gap to the processor list to indicate where a processor would be
955            inserted were one chosen from the menu.
956         */
957         int x, y;
958         processor_display.get_pointer (x, y);
959         _placement = processor_display.add_placeholder (y);
960
961         if (_visible_prefader_processors == 0 && _placement > 0) {
962                 --_placement;
963         }
964 }
965
966 bool
967 ProcessorBox::enter_notify (GdkEventCrossing*)
968 {
969         _current_processor_box = this;
970         return false;
971 }
972
973 bool
974 ProcessorBox::leave_notify (GdkEventCrossing*)
975 {
976         return false;
977 }
978
979 void
980 ProcessorBox::processor_operation (ProcessorOperation op) 
981 {
982         ProcSelection targets;
983
984         get_selected_processors (targets);
985
986         if (targets.empty()) {
987
988                 int x, y;
989                 processor_display.get_pointer (x, y);
990
991                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
992
993                 if (pointer.first && pointer.first->processor()) {
994                         targets.push_back (pointer.first->processor ());
995                 }
996         }
997
998         switch (op) {
999         case ProcessorsSelectAll:
1000                 processor_display.select_all ();
1001                 break;
1002
1003         case ProcessorsCopy:
1004                 copy_processors (targets);
1005                 break;
1006
1007         case ProcessorsCut:
1008                 cut_processors (targets);
1009                 break;
1010
1011         case ProcessorsPaste:
1012                 if (targets.empty()) {
1013                         paste_processors ();
1014                 } else {
1015                         paste_processors (targets.front());
1016                 }
1017                 break;
1018
1019         case ProcessorsDelete:
1020                 delete_processors (targets);
1021                 break;
1022
1023         case ProcessorsToggleActive:
1024                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
1025                         if ((*i)->active()) {
1026                                 (*i)->deactivate ();
1027                         } else {
1028                                 (*i)->activate ();
1029                         }
1030                 }
1031                 break;
1032
1033         case ProcessorsAB:
1034                 ab_plugins ();
1035                 break;
1036
1037         default:
1038                 break;
1039         }
1040 }
1041
1042 bool
1043 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
1044 {
1045         boost::shared_ptr<Processor> processor;
1046         if (child) {
1047                 processor = child->processor ();
1048         }
1049
1050         int ret = false;
1051         bool selected = processor_display.selected (child);
1052
1053         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
1054
1055                 if (_session->engine().connected()) {
1056                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
1057                         if (Config->get_use_plugin_own_gui ()) {
1058                                 toggle_edit_processor (processor);
1059                         } else {
1060                                 toggle_edit_generic_processor (processor);
1061                         }
1062                 }
1063                 ret = true;
1064
1065         } else if (processor && ev->button == 1 && selected) {
1066
1067                 // this is purely informational but necessary for route params UI
1068                 ProcessorSelected (processor); // emit
1069
1070         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
1071
1072                 choose_plugin ();
1073                 _get_plugin_selector()->show_manager ();
1074         }
1075
1076         return ret;
1077 }
1078
1079 bool
1080 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
1081 {
1082         boost::shared_ptr<Processor> processor;
1083         if (child) {
1084                 processor = child->processor ();
1085         }
1086
1087         if (processor && Keyboard::is_delete_event (ev)) {
1088
1089                 Glib::signal_idle().connect (sigc::bind (
1090                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
1091                                 boost::weak_ptr<Processor>(processor)));
1092
1093         } else if (Keyboard::is_context_menu_event (ev)) {
1094
1095                 show_processor_menu (ev->time);
1096
1097         } else if (processor && Keyboard::is_button2_event (ev)
1098 #ifndef GTKOSX
1099                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
1100 #endif
1101                 ) {
1102
1103                 /* button2-click with no/appropriate modifiers */
1104
1105                 if (processor->active()) {
1106                         processor->deactivate ();
1107                 } else {
1108                         processor->activate ();
1109                 }
1110         }
1111
1112         return false;
1113 }
1114
1115 Menu *
1116 ProcessorBox::build_processor_menu ()
1117 {
1118         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
1119         processor_menu->set_name ("ArdourContextMenu");
1120         return processor_menu;
1121 }
1122
1123 void
1124 ProcessorBox::select_all_processors ()
1125 {
1126         processor_display.select_all ();
1127 }
1128
1129 void
1130 ProcessorBox::deselect_all_processors ()
1131 {
1132         processor_display.select_none ();
1133 }
1134
1135 void
1136 ProcessorBox::choose_plugin ()
1137 {
1138         _get_plugin_selector()->set_interested_object (*this);
1139 }
1140
1141 /** @return true if an error occurred, otherwise false */
1142 bool
1143 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
1144 {
1145         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
1146
1147                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
1148
1149                 Route::ProcessorStreams err_streams;
1150
1151                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
1152                         weird_plugin_dialog (**p, err_streams);
1153                         return true;
1154                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
1155                 } else {
1156
1157                         if (Profile->get_sae()) {
1158                                 processor->activate ();
1159                         }
1160                 }
1161         }
1162
1163         return false;
1164 }
1165
1166 void
1167 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
1168 {
1169         ArdourDialog dialog (_("Plugin Incompatibility"));
1170         Label label;
1171
1172         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
1173                         p.name(), streams.index);
1174
1175         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
1176         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
1177
1178         text += _("\nThis plugin has:\n");
1179         if (has_midi) {
1180                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
1181                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
1182         }
1183         if (has_audio) {
1184                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
1185                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
1186         }
1187
1188         text += _("\nbut at the insertion point, there are:\n");
1189         if (has_midi) {
1190                 uint32_t const n = streams.count.n_midi ();
1191                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
1192         }
1193         if (has_audio) {
1194                 uint32_t const n = streams.count.n_audio ();
1195                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
1196         }
1197
1198         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
1199         label.set_text(text);
1200
1201         dialog.get_vbox()->pack_start (label);
1202         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1203
1204         dialog.set_name (X_("PluginIODialog"));
1205         dialog.set_position (Gtk::WIN_POS_MOUSE);
1206         dialog.set_modal (true);
1207         dialog.show_all ();
1208
1209         dialog.run ();
1210 }
1211
1212 void
1213 ProcessorBox::choose_insert ()
1214 {
1215         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
1216         _route->add_processor_by_index (processor, _placement);
1217 }
1218
1219 /* Caller must not hold process lock */
1220 void
1221 ProcessorBox::choose_send ()
1222 {
1223         boost::shared_ptr<Send> send (new Send (*_session, _route->pannable(), _route->mute_master()));
1224
1225         /* make an educated guess at the initial number of outputs for the send */
1226         ChanCount outs = (_session->master_out())
1227                         ? _session->master_out()->n_outputs()
1228                         : _route->n_outputs();
1229
1230         /* XXX need processor lock on route */
1231         try {
1232                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1233                 send->output()->ensure_io (outs, false, this);
1234         } catch (AudioEngine::PortRegistrationFailure& err) {
1235                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
1236                 return;
1237         }
1238
1239         /* let the user adjust the IO setup before creation.
1240
1241            Note: this dialog is NOT modal - we just leave it to run and it will
1242            return when its Finished signal is emitted - typically when the window
1243            is closed.
1244          */
1245
1246         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
1247         ios->show ();
1248
1249         /* keep a reference to the send so it doesn't get deleted while
1250            the IOSelectorWindow is doing its stuff
1251         */
1252         _processor_being_created = send;
1253
1254         ios->selector().Finished.connect (sigc::bind (
1255                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
1256                         boost::weak_ptr<Processor>(send), ios));
1257
1258 }
1259
1260 void
1261 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1262 {
1263         boost::shared_ptr<Processor> processor (weak_processor.lock());
1264
1265         /* drop our temporary reference to the new send */
1266         _processor_being_created.reset ();
1267
1268         if (!processor) {
1269                 return;
1270         }
1271
1272         switch (r) {
1273         case IOSelector::Cancelled:
1274                 // processor will go away when all shared_ptrs to it vanish
1275                 break;
1276
1277         case IOSelector::Accepted:
1278                 _route->add_processor_by_index (processor, _placement);
1279                 if (Profile->get_sae()) {
1280                         processor->activate ();
1281                 }
1282                 break;
1283         }
1284
1285         delete_when_idle (ios);
1286 }
1287
1288 void
1289 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1290 {
1291         boost::shared_ptr<Processor> processor (weak_processor.lock());
1292
1293         /* drop our temporary reference to the new return */
1294         _processor_being_created.reset ();
1295
1296         if (!processor) {
1297                 return;
1298         }
1299
1300         switch (r) {
1301         case IOSelector::Cancelled:
1302                 // processor will go away when all shared_ptrs to it vanish
1303                 break;
1304
1305         case IOSelector::Accepted:
1306                 _route->add_processor_by_index (processor, _placement);
1307                 if (Profile->get_sae()) {
1308                         processor->activate ();
1309                 }
1310                 break;
1311         }
1312
1313         delete_when_idle (ios);
1314 }
1315
1316 void
1317 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
1318 {
1319         if (!_route) {
1320                 return;
1321         }
1322
1323         boost::shared_ptr<Route> target = wr.lock();
1324
1325         if (!target) {
1326                 return;
1327         }
1328
1329         _session->add_internal_send (target, _placement, _route);
1330 }
1331
1332 void
1333 ProcessorBox::route_processors_changed (RouteProcessorChange c)
1334 {
1335         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
1336                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
1337                 return;
1338         }
1339
1340         redisplay_processors ();
1341 }
1342
1343 void
1344 ProcessorBox::redisplay_processors ()
1345 {
1346         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
1347         bool     fader_seen;
1348
1349         if (no_processor_redisplay) {
1350                 return;
1351         }
1352
1353         processor_display.clear ();
1354
1355         _visible_prefader_processors = 0;
1356         fader_seen = false;
1357
1358         _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &ProcessorBox::help_count_visible_prefader_processors), 
1359                                                &_visible_prefader_processors, &fader_seen));
1360
1361         if (_visible_prefader_processors == 0) { // fader only
1362                 BlankProcessorEntry* bpe = new BlankProcessorEntry (this, _width);
1363                 bpe->set_pixel_width (get_allocation().get_width());
1364                 processor_display.add_child (bpe);
1365         }
1366
1367         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
1368
1369         for (list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin(); i != _processor_window_proxies.end(); ++i) {
1370                 (*i)->marked = false;
1371         }
1372
1373         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
1374
1375         /* trim dead wood from the processor window proxy list */
1376
1377         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin();
1378         while (i != _processor_window_proxies.end()) {
1379                 list<ProcessorWindowProxy*>::iterator j = i;
1380                 ++j;
1381
1382                 if (!(*i)->marked) {
1383                         ARDOUR_UI::instance()->remove_window_proxy (*i);
1384                         delete *i;
1385                         _processor_window_proxies.erase (i);
1386                 }
1387
1388                 i = j;
1389         }
1390
1391         setup_entry_positions ();
1392 }
1393
1394 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
1395  *  not already have one.
1396  */
1397 void
1398 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
1399 {
1400         boost::shared_ptr<Processor> p = w.lock ();
1401         if (!p) {
1402                 return;
1403         }
1404
1405         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
1406         while (i != _processor_window_proxies.end()) {
1407
1408                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1409
1410                 if (p == t) {
1411                         /* this processor is already on the list; done */
1412                         (*i)->marked = true;
1413                         return;
1414                 }
1415
1416                 ++i;
1417         }
1418
1419         /* not on the list; add it */
1420
1421         string loc;
1422         if (_parent_strip) {
1423                 if (_parent_strip->mixer_owned()) {
1424                         loc = X_("M");
1425                 } else {
1426                         loc = X_("R");
1427                 }
1428         } else {
1429                 loc = X_("P");
1430         }
1431
1432         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1433                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1434                 _session->extra_xml (X_("UI")),
1435                 this,
1436                 w);
1437
1438         wp->marked = true;
1439
1440         /* if the processor already has an existing UI,
1441            note that so that we don't recreate it
1442         */
1443
1444         void* existing_ui = p->get_ui ();
1445
1446         if (existing_ui) {
1447                 wp->set (static_cast<Gtk::Window*>(existing_ui));
1448         }
1449
1450         _processor_window_proxies.push_back (wp);
1451         ARDOUR_UI::instance()->add_window_proxy (wp);
1452 }
1453
1454 void
1455 ProcessorBox::help_count_visible_prefader_processors (boost::weak_ptr<Processor> p, uint32_t* cnt, bool* amp_seen)
1456 {
1457         boost::shared_ptr<Processor> processor (p.lock ());
1458
1459         if (processor && processor->display_to_user()) {
1460
1461                 if (boost::dynamic_pointer_cast<Amp>(processor)) {
1462                         *amp_seen = true;
1463                 } else {
1464                         if (!*amp_seen) {
1465                                 (*cnt)++;
1466                         }
1467                 }
1468         }
1469 }
1470
1471 void
1472 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1473 {
1474         boost::shared_ptr<Processor> processor (p.lock ());
1475
1476         if (!processor || !processor->display_to_user()) {
1477                 return;
1478         }
1479
1480         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
1481         ProcessorEntry* e = 0;
1482         if (plugin_insert) {
1483                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
1484         } else {
1485                 e = new ProcessorEntry (this, processor, _width);
1486         }
1487
1488         e->set_pixel_width (get_allocation().get_width());
1489
1490         /* Set up this entry's state from the GUIObjectState */
1491         XMLNode* proc = entry_gui_object_state (e);
1492         if (proc) {
1493                 e->set_control_state (proc);
1494         }
1495         
1496         if (boost::dynamic_pointer_cast<Send> (processor)) {
1497                 /* Always show send controls */
1498                 e->show_all_controls ();
1499         }
1500
1501         processor_display.add_child (e);
1502 }
1503
1504 void
1505 ProcessorBox::reordered ()
1506 {
1507         compute_processor_sort_keys ();
1508         setup_entry_positions ();
1509 }
1510
1511 void
1512 ProcessorBox::setup_entry_positions ()
1513 {
1514         list<ProcessorEntry*> children = processor_display.children ();
1515         bool pre_fader = true;
1516
1517         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1518                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor())) {
1519                         pre_fader = false;
1520                         (*i)->set_position (ProcessorEntry::Fader);
1521                 } else {
1522                         if (pre_fader) {
1523                                 (*i)->set_position (ProcessorEntry::PreFader);
1524                         } else {
1525                                 (*i)->set_position (ProcessorEntry::PostFader);
1526                         }
1527                 }
1528         }
1529 }
1530
1531 void
1532 ProcessorBox::compute_processor_sort_keys ()
1533 {
1534         list<ProcessorEntry*> children = processor_display.children ();
1535         Route::ProcessorList our_processors;
1536
1537         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1538                 if ((*i)->processor()) {
1539                         our_processors.push_back ((*i)->processor ());
1540                 }
1541         }
1542
1543         if (_route->reorder_processors (our_processors)) {
1544                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1545                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1546                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1547                    when the drag is torn down after this handler function is finished.
1548                 */
1549                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1550         }
1551 }
1552
1553 void
1554 ProcessorBox::report_failed_reorder ()
1555 {
1556         /* reorder failed, so redisplay */
1557
1558         redisplay_processors ();
1559
1560         /* now tell them about the problem */
1561
1562         ArdourDialog dialog (_("Plugin Incompatibility"));
1563         Label label;
1564
1565         label.set_text (_("\
1566 You cannot reorder these plugins/sends/inserts\n\
1567 in that way because the inputs and\n\
1568 outputs will not work correctly."));
1569
1570         dialog.get_vbox()->set_border_width (12);
1571         dialog.get_vbox()->pack_start (label);
1572         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1573
1574         dialog.set_name (X_("PluginIODialog"));
1575         dialog.set_position (Gtk::WIN_POS_MOUSE);
1576         dialog.set_modal (true);
1577         dialog.show_all ();
1578
1579         dialog.run ();
1580 }
1581
1582 void
1583 ProcessorBox::rename_processors ()
1584 {
1585         ProcSelection to_be_renamed;
1586
1587         get_selected_processors (to_be_renamed);
1588
1589         if (to_be_renamed.empty()) {
1590                 return;
1591         }
1592
1593         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1594                 rename_processor (*i);
1595         }
1596 }
1597
1598 bool
1599 ProcessorBox::can_cut () const
1600 {
1601         vector<boost::shared_ptr<Processor> > sel;
1602
1603         get_selected_processors (sel);
1604
1605         /* cut_processors () does not cut inserts */
1606
1607         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1608
1609                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1610                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1611                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1612                         return true;
1613                 }
1614         }
1615
1616         return false;
1617 }
1618
1619 void
1620 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1621 {
1622         if (to_be_removed.empty()) {
1623                 return;
1624         }
1625
1626         XMLNode* node = new XMLNode (X_("cut"));
1627         Route::ProcessorList to_cut;
1628
1629         no_processor_redisplay = true;
1630         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1631                 // Cut only plugins, sends and returns
1632                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1633                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1634                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1635
1636                         Window* w = get_processor_ui (*i);
1637
1638                         if (w) {
1639                                 w->hide ();
1640                         }
1641
1642                         XMLNode& child ((*i)->get_state());
1643                         node->add_child_nocopy (child);
1644                         to_cut.push_back (*i);
1645                 }
1646         }
1647
1648         if (_route->remove_processors (to_cut) != 0) {
1649                 delete node;
1650                 no_processor_redisplay = false;
1651                 return;
1652         }
1653
1654         _rr_selection.set (node);
1655
1656         no_processor_redisplay = false;
1657         redisplay_processors ();
1658 }
1659
1660 void
1661 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1662 {
1663         if (to_be_copied.empty()) {
1664                 return;
1665         }
1666
1667         XMLNode* node = new XMLNode (X_("copy"));
1668
1669         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1670                 // Copy only plugins, sends, returns
1671                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1672                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1673                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1674                         node->add_child_nocopy ((*i)->get_state());
1675                 }
1676         }
1677
1678         _rr_selection.set (node);
1679 }
1680
1681 void
1682 ProcessorBox::delete_processors (const ProcSelection& targets)
1683 {
1684         if (targets.empty()) {
1685                 return;
1686         }
1687
1688         no_processor_redisplay = true;
1689
1690         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1691
1692                 Window* w = get_processor_ui (*i);
1693
1694                 if (w) {
1695                         w->hide ();
1696                 }
1697
1698                 _route->remove_processor(*i);
1699         }
1700
1701         no_processor_redisplay = false;
1702         redisplay_processors ();
1703 }
1704
1705 void
1706 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1707 {
1708         list<boost::shared_ptr<Processor> >::const_iterator x;
1709
1710         no_processor_redisplay = true;
1711         for (x = procs.begin(); x != procs.end(); ++x) {
1712
1713                 Window* w = get_processor_ui (*x);
1714
1715                 if (w) {
1716                         w->hide ();
1717                 }
1718
1719                 _route->remove_processor(*x);
1720         }
1721
1722         no_processor_redisplay = false;
1723         redisplay_processors ();
1724 }
1725
1726 gint
1727 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1728 {
1729         boost::shared_ptr<Processor> processor (weak_processor.lock());
1730
1731         if (!processor) {
1732                 return false;
1733         }
1734
1735         /* NOT copied to _mixer.selection() */
1736
1737         no_processor_redisplay = true;
1738         _route->remove_processor (processor);
1739         no_processor_redisplay = false;
1740         redisplay_processors ();
1741
1742         return false;
1743 }
1744
1745 void
1746 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1747 {
1748         ArdourPrompter name_prompter (true);
1749         string result;
1750         name_prompter.set_title (_("Rename Processor"));
1751         name_prompter.set_prompt (_("New name:"));
1752         name_prompter.set_initial_text (processor->name());
1753         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1754         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1755         name_prompter.show_all ();
1756
1757         switch (name_prompter.run ()) {
1758
1759         case Gtk::RESPONSE_ACCEPT:
1760                 name_prompter.get_result (result);
1761                 if (result.length()) {
1762
1763                        int tries = 0;
1764                        string test = result;
1765
1766                        while (tries < 100) {
1767                                if (_session->io_name_is_legal (test)) {
1768                                        result = test;
1769                                        break;
1770                                }
1771                                tries++;
1772
1773                                test = string_compose ("%1-%2", result, tries);
1774                        }
1775
1776                        if (tries < 100) {
1777                                processor->set_name (result);
1778                        } else {
1779                                /* unlikely! */
1780                                ARDOUR_UI::instance()->popup_error
1781                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
1782                        }
1783                 }
1784                 break;
1785         }
1786
1787         return;
1788 }
1789
1790 void
1791 ProcessorBox::paste_processors ()
1792 {
1793         if (_rr_selection.processors.empty()) {
1794                 return;
1795         }
1796
1797         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1798 }
1799
1800 void
1801 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1802 {
1803
1804         if (_rr_selection.processors.empty()) {
1805                 return;
1806         }
1807
1808         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1809 }
1810
1811 void
1812 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1813 {
1814         XMLNodeConstIterator niter;
1815         list<boost::shared_ptr<Processor> > copies;
1816
1817         if (nlist.empty()) {
1818                 return;
1819         }
1820
1821         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1822
1823                 XMLProperty const * type = (*niter)->property ("type");
1824                 assert (type);
1825
1826                 boost::shared_ptr<Processor> p;
1827                 try {
1828                         if (type->value() == "meter" ||
1829                             type->value() == "main-outs" ||
1830                             type->value() == "amp" ||
1831                             type->value() == "intsend" || type->value() == "intreturn") {
1832                                 /* do not paste meter, main outs, amp or internal send/returns */
1833                                 continue;
1834
1835                         } else if (type->value() == "send") {
1836
1837                                 XMLNode n (**niter);
1838                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
1839
1840                                 IOProcessor::prepare_for_reset (n, s->name());
1841                                 
1842                                 if (s->set_state (n, Stateful::loading_state_version)) {
1843                                         delete s;
1844                                         return;
1845                                 }
1846
1847                                 p.reset (s);
1848
1849                         } else if (type->value() == "return") {
1850
1851                                 XMLNode n (**niter);
1852                                 Return* r = new Return (*_session);
1853
1854                                 IOProcessor::prepare_for_reset (n, r->name());
1855
1856                                 if (r->set_state (n, Stateful::loading_state_version)) {
1857                                         delete r;
1858                                         return;
1859                                 }
1860
1861                                 p.reset (r);
1862
1863                         } else if (type->value() == "port") {
1864
1865                                 XMLNode n (**niter);
1866                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
1867                                 
1868                                 IOProcessor::prepare_for_reset (n, pi->name());
1869                                 
1870                                 if (pi->set_state (n, Stateful::loading_state_version)) {
1871                                         return;
1872                                 }
1873                                 
1874                                 p.reset (pi);
1875
1876                         } else {
1877                                 /* XXX its a bit limiting to assume that everything else
1878                                    is a plugin.
1879                                 */
1880
1881                                 p.reset (new PluginInsert (*_session));
1882                                 p->set_state (**niter, Stateful::current_state_version);
1883                         }
1884
1885                         copies.push_back (p);
1886                 }
1887
1888                 catch (...) {
1889                         cerr << "plugin insert constructor failed\n";
1890                 }
1891         }
1892
1893         if (copies.empty()) {
1894                 return;
1895         }
1896
1897         if (_route->add_processors (copies, p)) {
1898
1899                 string msg = _(
1900                         "Copying the set of processors on the clipboard failed,\n\
1901 probably because the I/O configuration of the plugins\n\
1902 could not match the configuration of this track.");
1903                 MessageDialog am (msg);
1904                 am.run ();
1905         }
1906 }
1907
1908 void
1909 ProcessorBox::get_selected_processors (ProcSelection& processors) const
1910 {
1911         const list<ProcessorEntry*> selection = processor_display.selection ();
1912         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
1913                 processors.push_back ((*i)->processor ());
1914         }
1915 }
1916
1917 void
1918 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1919 {
1920         list<ProcessorEntry*> selection = processor_display.selection ();
1921         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1922                 (this->*method) ((*i)->processor ());
1923         }
1924 }
1925
1926 void
1927 ProcessorBox::all_visible_processors_active (bool state)
1928 {
1929         _route->all_visible_processors_active (state);
1930 }
1931
1932 void
1933 ProcessorBox::ab_plugins ()
1934 {
1935         _route->ab_plugins (ab_direction);
1936         ab_direction = !ab_direction;
1937 }
1938
1939
1940 void
1941 ProcessorBox::clear_processors ()
1942 {
1943         string prompt;
1944         vector<string> choices;
1945
1946         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1947                                    "(this cannot be undone)"), _route->name());
1948
1949         choices.push_back (_("Cancel"));
1950         choices.push_back (_("Yes, remove them all"));
1951
1952         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1953
1954         if (prompter.run () == 1) {
1955                 _route->clear_processors (PreFader);
1956                 _route->clear_processors (PostFader);
1957         }
1958 }
1959
1960 void
1961 ProcessorBox::clear_processors (Placement p)
1962 {
1963         string prompt;
1964         vector<string> choices;
1965
1966         if (p == PreFader) {
1967                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1968                                            "(this cannot be undone)"), _route->name());
1969         } else {
1970                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1971                                            "(this cannot be undone)"), _route->name());
1972         }
1973
1974         choices.push_back (_("Cancel"));
1975         choices.push_back (_("Yes, remove them all"));
1976
1977         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1978
1979         if (prompter.run () == 1) {
1980                 _route->clear_processors (p);
1981         }
1982 }
1983
1984 bool
1985 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
1986 {
1987         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
1988         if (at && at->freeze_state() == AudioTrack::Frozen) {
1989                 return false;
1990         }
1991
1992         if (
1993                 (boost::dynamic_pointer_cast<Send> (processor) && !boost::dynamic_pointer_cast<InternalSend> (processor))||
1994                 boost::dynamic_pointer_cast<Return> (processor) ||
1995                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
1996                 boost::dynamic_pointer_cast<PortInsert> (processor)
1997                 ) {
1998                 return true;
1999         }
2000
2001         return false;
2002 }
2003
2004 bool
2005 ProcessorBox::one_processor_can_be_edited ()
2006 {
2007         list<ProcessorEntry*> selection = processor_display.selection ();
2008         list<ProcessorEntry*>::iterator i = selection.begin();
2009         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
2010                 ++i;
2011         }
2012
2013         return (i != selection.end());
2014 }
2015
2016 void
2017 ProcessorBox::toggle_edit_processor (boost::shared_ptr<Processor> processor)
2018 {
2019         boost::shared_ptr<Send> send;
2020         boost::shared_ptr<InternalSend> internal_send;
2021         boost::shared_ptr<Return> retrn;
2022         boost::shared_ptr<PluginInsert> plugin_insert;
2023         boost::shared_ptr<PortInsert> port_insert;
2024         Window* gidget = 0;
2025
2026         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
2027
2028                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
2029                         return;
2030                 }
2031         }
2032
2033         if (boost::dynamic_pointer_cast<Amp> (processor)) {
2034
2035                 if (_parent_strip) {
2036                         _parent_strip->revert_to_default_display ();
2037                 }
2038
2039         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2040
2041                 if (!_session->engine().connected()) {
2042                         return;
2043                 }
2044
2045                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2046                         SendUIWindow* w = new SendUIWindow (send, _session);
2047                         w->show ();
2048                 } else {
2049                         /* assign internal send to main fader */
2050                         if (_parent_strip) {
2051                                 if (_parent_strip->current_delivery() == send) {
2052                                         _parent_strip->revert_to_default_display ();
2053                                 } else {
2054                                         _parent_strip->show_send(send);
2055                                 }
2056                         } 
2057                 }
2058
2059         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
2060
2061                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
2062                         /* no GUI for these */
2063                         return;
2064                 }
2065
2066                 if (!_session->engine().connected()) {
2067                         return;
2068                 }
2069
2070                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
2071
2072                 ReturnUIWindow *return_ui;
2073                 Window* w = get_processor_ui (retrn);
2074
2075                 if (w == 0) {
2076
2077                         return_ui = new ReturnUIWindow (retrn, _session);
2078                         return_ui->set_title (retrn->name ());
2079                         set_processor_ui (send, return_ui);
2080
2081                 } else {
2082                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
2083                 }
2084
2085                 gidget = return_ui;
2086
2087         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2088
2089                 PluginUIWindow *plugin_ui;
2090
2091                 /* these are both allowed to be null */
2092
2093                 Container* toplevel = get_toplevel();
2094                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
2095
2096                 Window* w = get_processor_ui (plugin_insert);
2097
2098                 if (w == 0) {
2099
2100                         plugin_ui = new PluginUIWindow (win, plugin_insert);
2101                         plugin_ui->set_title (generate_processor_title (plugin_insert));
2102                         set_processor_ui (plugin_insert, plugin_ui);
2103
2104                 } else {
2105                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
2106                         plugin_ui->set_parent (win);
2107                 }
2108
2109                 gidget = plugin_ui;
2110
2111         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
2112
2113                 if (!_session->engine().connected()) {
2114                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
2115                         msg.run ();
2116                         return;
2117                 }
2118
2119                 PortInsertWindow *io_selector;
2120
2121                 Window* w = get_processor_ui (port_insert);
2122
2123                 if (w == 0) {
2124                         io_selector = new PortInsertWindow (_session, port_insert);
2125                         set_processor_ui (port_insert, io_selector);
2126
2127                 } else {
2128                         io_selector = dynamic_cast<PortInsertWindow *> (w);
2129                 }
2130
2131                 gidget = io_selector;
2132         }
2133
2134         if (gidget) {
2135                 if (gidget->is_visible()) {
2136                         gidget->hide ();
2137                 } else {
2138                         gidget->show_all ();
2139                         gidget->present ();
2140                 }
2141         }
2142 }
2143
2144 /** Toggle a generic (Ardour-generated) plugin UI */
2145 void
2146 ProcessorBox::toggle_edit_generic_processor (boost::shared_ptr<Processor> processor)
2147 {
2148         boost::shared_ptr<PluginInsert> plugin_insert
2149                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
2150         if (!plugin_insert) {
2151                 return;
2152         }
2153
2154         Container*      toplevel  = get_toplevel();
2155         Window*         win       = dynamic_cast<Gtk::Window*>(toplevel);
2156         PluginUIWindow* plugin_ui = new PluginUIWindow(win, plugin_insert, true, false);
2157         plugin_ui->set_title(generate_processor_title (plugin_insert));
2158
2159         if (plugin_ui->is_visible()) {
2160                 plugin_ui->hide();
2161         } else {
2162                 plugin_ui->show_all();
2163                 plugin_ui->present();
2164         }
2165 }
2166
2167 void
2168 ProcessorBox::register_actions ()
2169 {
2170         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("ProcessorMenu"));
2171         Glib::RefPtr<Action> act;
2172
2173         /* new stuff */
2174         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
2175                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
2176
2177         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
2178                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
2179         ActionManager::jack_sensitive_actions.push_back (act);
2180         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New External Send ..."),
2181                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
2182         ActionManager::jack_sensitive_actions.push_back (act);
2183
2184         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
2185
2186         ActionManager::register_action (popup_act_grp, X_("controls"), _("Controls"));
2187
2188         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
2189                         sigc::ptr_fun (ProcessorBox::rb_clear));
2190         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
2191                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
2192         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
2193                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
2194
2195         /* standard editing stuff */
2196         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
2197                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
2198         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
2199         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
2200                         sigc::ptr_fun (ProcessorBox::rb_copy));
2201         ActionManager::plugin_selection_sensitive_actions.push_back(act);
2202
2203         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
2204                         sigc::ptr_fun (ProcessorBox::rb_delete));
2205         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
2206
2207         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
2208                         sigc::ptr_fun (ProcessorBox::rb_paste));
2209         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
2210                         sigc::ptr_fun (ProcessorBox::rb_rename));
2211         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
2212                         sigc::ptr_fun (ProcessorBox::rb_select_all));
2213         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
2214                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
2215
2216         /* activation etc. */
2217
2218         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate All"),
2219                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
2220         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate All"),
2221                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
2222         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
2223                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
2224
2225         /* show editors */
2226         edit_action = ActionManager::register_action (
2227                 popup_act_grp, X_("edit"), _("Edit..."),
2228                 sigc::ptr_fun (ProcessorBox::rb_edit));
2229
2230         edit_generic_action = ActionManager::register_action (
2231                 popup_act_grp, X_("edit-generic"), _("Edit with basic controls..."),
2232                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
2233
2234         ActionManager::add_action_group (popup_act_grp);
2235 }
2236
2237 void
2238 ProcessorBox::rb_edit_generic ()
2239 {
2240         if (_current_processor_box == 0) {
2241                 return;
2242         }
2243
2244         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_generic_processor);
2245 }
2246
2247 void
2248 ProcessorBox::rb_ab_plugins ()
2249 {
2250         if (_current_processor_box == 0) {
2251                 return;
2252         }
2253
2254         _current_processor_box->ab_plugins ();
2255 }
2256
2257 void
2258 ProcessorBox::rb_choose_plugin ()
2259 {
2260         if (_current_processor_box == 0) {
2261                 return;
2262         }
2263         _current_processor_box->choose_plugin ();
2264 }
2265
2266 void
2267 ProcessorBox::rb_choose_insert ()
2268 {
2269         if (_current_processor_box == 0) {
2270                 return;
2271         }
2272         _current_processor_box->choose_insert ();
2273 }
2274
2275 void
2276 ProcessorBox::rb_choose_send ()
2277 {
2278         if (_current_processor_box == 0) {
2279                 return;
2280         }
2281         _current_processor_box->choose_send ();
2282 }
2283
2284 void
2285 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
2286 {
2287         if (_current_processor_box == 0) {
2288                 return;
2289         }
2290
2291         _current_processor_box->choose_aux (wr);
2292 }
2293
2294 void
2295 ProcessorBox::rb_clear ()
2296 {
2297         if (_current_processor_box == 0) {
2298                 return;
2299         }
2300
2301         _current_processor_box->clear_processors ();
2302 }
2303
2304
2305 void
2306 ProcessorBox::rb_clear_pre ()
2307 {
2308         if (_current_processor_box == 0) {
2309                 return;
2310         }
2311
2312         _current_processor_box->clear_processors (PreFader);
2313 }
2314
2315
2316 void
2317 ProcessorBox::rb_clear_post ()
2318 {
2319         if (_current_processor_box == 0) {
2320                 return;
2321         }
2322
2323         _current_processor_box->clear_processors (PostFader);
2324 }
2325
2326 void
2327 ProcessorBox::rb_cut ()
2328 {
2329         if (_current_processor_box == 0) {
2330                 return;
2331         }
2332
2333         _current_processor_box->processor_operation (ProcessorsCut);
2334 }
2335
2336 void
2337 ProcessorBox::rb_delete ()
2338 {
2339         if (_current_processor_box == 0) {
2340                 return;
2341         }
2342
2343         _current_processor_box->processor_operation (ProcessorsDelete);
2344 }
2345
2346 void
2347 ProcessorBox::rb_copy ()
2348 {
2349         if (_current_processor_box == 0) {
2350                 return;
2351         }
2352         _current_processor_box->processor_operation (ProcessorsCopy);
2353 }
2354
2355 void
2356 ProcessorBox::rb_paste ()
2357 {
2358         if (_current_processor_box == 0) {
2359                 return;
2360         }
2361
2362         _current_processor_box->processor_operation (ProcessorsPaste);
2363 }
2364
2365 void
2366 ProcessorBox::rb_rename ()
2367 {
2368         if (_current_processor_box == 0) {
2369                 return;
2370         }
2371         _current_processor_box->rename_processors ();
2372 }
2373
2374 void
2375 ProcessorBox::rb_select_all ()
2376 {
2377         if (_current_processor_box == 0) {
2378                 return;
2379         }
2380
2381         _current_processor_box->processor_operation (ProcessorsSelectAll);
2382 }
2383
2384 void
2385 ProcessorBox::rb_deselect_all ()
2386 {
2387         if (_current_processor_box == 0) {
2388                 return;
2389         }
2390
2391         _current_processor_box->deselect_all_processors ();
2392 }
2393
2394 void
2395 ProcessorBox::rb_activate_all ()
2396 {
2397         if (_current_processor_box == 0) {
2398                 return;
2399         }
2400
2401         _current_processor_box->all_visible_processors_active (true);
2402 }
2403
2404 void
2405 ProcessorBox::rb_deactivate_all ()
2406 {
2407         if (_current_processor_box == 0) {
2408                 return;
2409         }
2410         _current_processor_box->all_visible_processors_active (false);
2411 }
2412
2413 void
2414 ProcessorBox::rb_edit ()
2415 {
2416         if (_current_processor_box == 0) {
2417                 return;
2418         }
2419
2420         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_processor);
2421 }
2422
2423 void
2424 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
2425 {
2426         if (!what_changed.contains (ARDOUR::Properties::name)) {
2427                 return;
2428         }
2429
2430         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
2431
2432         boost::shared_ptr<Processor> processor;
2433         boost::shared_ptr<PluginInsert> plugin_insert;
2434         boost::shared_ptr<Send> send;
2435
2436         list<ProcessorEntry*> children = processor_display.children();
2437
2438         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
2439
2440                 processor = (*iter)->processor ();
2441
2442                 if (!processor) {
2443                         continue;
2444                 }
2445
2446                 Window* w = get_processor_ui (processor);
2447
2448                 if (!w) {
2449                         continue;
2450                 }
2451
2452                 /* rename editor windows for sends and plugins */
2453
2454                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2455                         w->set_title (send->name ());
2456                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2457                         w->set_title (generate_processor_title (plugin_insert));
2458                 }
2459         }
2460 }
2461
2462 string
2463 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
2464 {
2465         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
2466         string::size_type email_pos;
2467
2468         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
2469                 maker = maker.substr (0, email_pos - 1);
2470         }
2471
2472         if (maker.length() > 32) {
2473                 maker = maker.substr (0, 32);
2474                 maker += " ...";
2475         }
2476
2477         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
2478 }
2479
2480 void
2481 ProcessorBox::on_size_allocate (Allocation& a)
2482 {
2483         HBox::on_size_allocate (a);
2484
2485         list<ProcessorEntry*> children = processor_display.children ();
2486         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
2487                 (*i)->set_pixel_width (a.get_width ());
2488         }
2489 }
2490
2491 /** @param p Processor.
2492  *  @return the UI window for \a p.
2493  */
2494 Window *
2495 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
2496 {
2497         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_proxies.begin ();
2498         while (i != _processor_window_proxies.end()) {
2499                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2500                 if (t && t == p) {
2501                         return (*i)->get ();
2502                 }
2503
2504                 ++i;
2505         }
2506
2507         return 0;
2508 }
2509
2510 /** Make a note of the UI window that a processor is using.
2511  *  @param p Processor.
2512  *  @param w UI window.
2513  */
2514 void
2515 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2516 {
2517         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
2518
2519         p->set_ui (w);
2520
2521         while (i != _processor_window_proxies.end()) {
2522                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2523                 if (t && t == p) {
2524                         (*i)->set (w);
2525                         return;
2526                 }
2527
2528                 ++i;
2529         }
2530
2531         /* we shouldn't get here, because the ProcessorUIList should always contain
2532            an entry for each processor.
2533         */
2534         assert (false);
2535 }
2536
2537 void
2538 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
2539 {
2540         boost::shared_ptr<Delivery> d = w.lock ();
2541         if (!d) {
2542                 return;
2543         }
2544
2545         list<ProcessorEntry*> children = processor_display.children ();
2546         list<ProcessorEntry*>::const_iterator i = children.begin();
2547         while (i != children.end() && (*i)->processor() != d) {
2548                 ++i;
2549         }
2550
2551         if (i == children.end()) {
2552                 processor_display.set_active (0);
2553         } else {
2554                 processor_display.set_active (*i);
2555         }
2556 }
2557
2558 /** Called to repair the damage of Editor::show_window doing a show_all() */
2559 void
2560 ProcessorBox::hide_things ()
2561 {
2562         list<ProcessorEntry*> c = processor_display.children ();
2563         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
2564                 (*i)->hide_things ();
2565         }
2566 }
2567
2568 void
2569 ProcessorBox::processor_menu_unmapped ()
2570 {
2571         processor_display.remove_placeholder ();
2572 }
2573
2574 XMLNode *
2575 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
2576 {
2577         if (!_parent_strip) {
2578                 return 0;
2579         }
2580
2581         GUIObjectState& st = _parent_strip->gui_object_state ();
2582         
2583         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
2584         assert (strip);
2585         return st.get_or_add_node (strip, entry->state_id());
2586 }
2587
2588 void
2589 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
2590 {
2591         XMLNode* proc = entry_gui_object_state (entry);
2592         if (!proc) {
2593                 return;
2594         }
2595
2596         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
2597         proc->remove_nodes_and_delete (X_("Object"));
2598         entry->add_control_state (proc);
2599 }
2600
2601 ProcessorWindowProxy::ProcessorWindowProxy (
2602         string const & name,
2603         XMLNode const * node,
2604         ProcessorBox* box,
2605         boost::weak_ptr<Processor> processor
2606         )
2607         : WindowProxy<Gtk::Window> (name, node)
2608         , marked (false)
2609         , _processor_box (box)
2610         , _processor (processor)
2611 {
2612
2613 }
2614
2615
2616 void
2617 ProcessorWindowProxy::show ()
2618 {
2619         boost::shared_ptr<Processor> p = _processor.lock ();
2620         if (!p) {
2621                 return;
2622         }
2623
2624         _processor_box->toggle_edit_processor (p);
2625 }
2626