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