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