zero/reset closed file-descriptors
[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_size_request (48, -1);
696         processor_display.set_spacing (2);
697
698         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
699         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
700
701         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
702         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
703
704         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
705         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
706
707         processor_scroller.show ();
708         processor_display.show ();
709
710         if (parent) {
711                 parent->DeliveryChanged.connect (
712                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
713                         );
714         }
715
716         ARDOUR_UI::instance()->set_tip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
717 }
718
719 ProcessorBox::~ProcessorBox ()
720 {
721         /* it may appear as if we should delete processor_menu but that is a
722          * pointer to a widget owned by the UI Manager, and has potentially
723          * be returned to many other ProcessorBoxes. GTK doesn't really make
724          * clear the ownership of this widget, which is a menu and thus is
725          * never packed into any container other than an implict GtkWindow.
726          *
727          * For now, until or if we ever get clarification over the ownership
728          * story just let it continue to exist. At worst, its a small memory leak.
729          */
730 }
731
732 void
733 ProcessorBox::set_route (boost::shared_ptr<Route> r)
734 {
735         if (_route == r) {
736                 return;
737         }
738
739         _route_connections.drop_connections();
740
741         /* new route: any existing block on processor redisplay must be meaningless */
742         no_processor_redisplay = false;
743         _route = r;
744
745         _route->processors_changed.connect (
746                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
747                 );
748
749         _route->DropReferences.connect (
750                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
751                 );
752
753         _route->PropertyChanged.connect (
754                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
755                 );
756
757         redisplay_processors ();
758 }
759
760 void
761 ProcessorBox::route_going_away ()
762 {
763         /* don't keep updating display as processors are deleted */
764         no_processor_redisplay = true;
765         _route.reset ();
766 }
767
768 void
769 ProcessorBox::object_drop(DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
770 {
771         boost::shared_ptr<Processor> p;
772         if (position) {
773                 p = position->processor ();
774                 if (!p) {
775                         /* dropped on the blank entry (which will be before the
776                            fader), so use the first non-blank child as our
777                            `dropped on' processor */
778                         list<ProcessorEntry*> c = processor_display.children ();
779                         list<ProcessorEntry*>::iterator i = c.begin ();
780                         while (dynamic_cast<BlankProcessorEntry*> (*i)) {
781                                 assert (i != c.end ());
782                                 ++i;
783                         }
784
785                         assert (i != c.end ());
786                         p = (*i)->processor ();
787                         assert (p);
788                 }
789         }
790
791         list<ProcessorEntry*> children = source->selection ();
792         list<boost::shared_ptr<Processor> > procs;
793         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
794                 if ((*i)->processor ()) {
795                         procs.push_back ((*i)->processor ());
796                 }
797         }
798
799         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
800                 XMLNode& state = (*i)->get_state ();
801                 XMLNodeList nlist;
802                 nlist.push_back (&state);
803                 paste_processor_state (nlist, p);
804                 delete &state;
805         }
806
807         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
808            ourselves.
809         */
810
811         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
812                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
813                 if (other) {
814                         other->delete_dragged_processors (procs);
815                 }
816         }
817 }
818
819 void
820 ProcessorBox::set_width (Width w)
821 {
822         if (_width == w) {
823                 return;
824         }
825
826         _width = w;
827
828         list<ProcessorEntry*> children = processor_display.children ();
829         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
830                 (*i)->set_enum_width (w);
831         }
832
833         queue_resize ();
834 }
835
836 Gtk::Menu*
837 ProcessorBox::build_possible_aux_menu ()
838 {
839         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
840
841         if (rl->empty()) {
842                 /* No aux sends if there are no busses */
843                 return 0;
844         }
845
846         using namespace Menu_Helpers;
847         Menu* menu = manage (new Menu);
848         MenuList& items = menu->items();
849
850         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
851                 if (!_route->internal_send_for (*r) && *r != _route) {
852                         items.push_back (MenuElem ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
853                 }
854         }
855
856         return menu;
857 }
858
859 void
860 ProcessorBox::show_processor_menu (int arg)
861 {
862         if (processor_menu == 0) {
863                 processor_menu = build_processor_menu ();
864                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
865         }
866
867         /* Sort out the plugin submenu */
868
869         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
870
871         if (plugin_menu_item) {
872                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
873         }
874
875         /* And the aux submenu */
876
877         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
878
879         if (aux_menu_item) {
880                 Menu* m = build_possible_aux_menu();
881                 if (m && !m->items().empty()) {
882                         aux_menu_item->set_submenu (*m);
883                         aux_menu_item->set_sensitive (true);
884                 } else {
885                         /* stupid gtkmm: we need to pass a null reference here */
886                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
887                         aux_menu_item->set_sensitive (false);
888                 }
889         }
890
891         ProcessorEntry* single_selection = 0;
892         if (processor_display.selection().size() == 1) {
893                 single_selection = processor_display.selection().front();
894         }
895
896         /* And the controls submenu */
897
898         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
899
900         if (controls_menu_item) {
901                 if (single_selection) {
902                         Menu* m = single_selection->build_controls_menu ();
903                         if (m && !m->items().empty()) {
904                                 controls_menu_item->set_submenu (*m);
905                                 controls_menu_item->set_sensitive (true);
906                         } else {
907                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
908                                 controls_menu_item->set_sensitive (false);
909                         }
910                 }
911         }
912
913         /* Sensitise actions as approprioate */
914
915         cut_action->set_sensitive (can_cut());
916         paste_action->set_sensitive (!_rr_selection.processors.empty());
917
918         const bool sensitive = !processor_display.selection().empty();
919         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
920         edit_action->set_sensitive (one_processor_can_be_edited ());
921
922         boost::shared_ptr<PluginInsert> pi;
923         if (single_selection) {
924                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
925         }
926
927         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
928         edit_generic_action->set_sensitive (pi && pi->plugin()->has_editor ());
929
930         /* disallow rename for multiple selections, for plugin inserts and for the fader */
931         rename_action->set_sensitive (single_selection && !pi && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ()));
932
933         processor_menu->popup (1, arg);
934
935         /* Add a placeholder gap to the processor list to indicate where a processor would be
936            inserted were one chosen from the menu.
937         */
938         int x, y;
939         processor_display.get_pointer (x, y);
940         _placement = processor_display.add_placeholder (y);
941
942         if (_visible_prefader_processors == 0 && _placement > 0) {
943                 --_placement;
944         }
945 }
946
947 bool
948 ProcessorBox::enter_notify (GdkEventCrossing*)
949 {
950         _current_processor_box = this;
951         return false;
952 }
953
954 bool
955 ProcessorBox::leave_notify (GdkEventCrossing*)
956 {
957         return false;
958 }
959
960 void
961 ProcessorBox::processor_operation (ProcessorOperation op) 
962 {
963         ProcSelection targets;
964
965         get_selected_processors (targets);
966
967         if (targets.empty()) {
968
969                 int x, y;
970                 processor_display.get_pointer (x, y);
971
972                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
973
974                 if (pointer.first && pointer.first->processor()) {
975                         targets.push_back (pointer.first->processor ());
976                 }
977         }
978
979         switch (op) {
980         case ProcessorsSelectAll:
981                 processor_display.select_all ();
982                 break;
983
984         case ProcessorsCopy:
985                 copy_processors (targets);
986                 break;
987
988         case ProcessorsCut:
989                 cut_processors (targets);
990                 break;
991
992         case ProcessorsPaste:
993                 if (targets.empty()) {
994                         paste_processors ();
995                 } else {
996                         paste_processors (targets.front());
997                 }
998                 break;
999
1000         case ProcessorsDelete:
1001                 delete_processors (targets);
1002                 break;
1003
1004         case ProcessorsToggleActive:
1005                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
1006                         if ((*i)->active()) {
1007                                 (*i)->deactivate ();
1008                         } else {
1009                                 (*i)->activate ();
1010                         }
1011                 }
1012                 break;
1013
1014         case ProcessorsAB:
1015                 ab_plugins ();
1016                 break;
1017
1018         default:
1019                 break;
1020         }
1021 }
1022
1023 bool
1024 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
1025 {
1026         boost::shared_ptr<Processor> processor;
1027         if (child) {
1028                 processor = child->processor ();
1029         }
1030
1031         int ret = false;
1032         bool selected = processor_display.selected (child);
1033
1034         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
1035
1036                 if (_session->engine().connected()) {
1037                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
1038                         if (Config->get_use_plugin_own_gui ()) {
1039                                 toggle_edit_processor (processor);
1040                         } else {
1041                                 toggle_edit_generic_processor (processor);
1042                         }
1043                 }
1044                 ret = true;
1045
1046         } else if (processor && ev->button == 1 && selected) {
1047
1048                 // this is purely informational but necessary for route params UI
1049                 ProcessorSelected (processor); // emit
1050
1051         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
1052
1053                 choose_plugin ();
1054                 _get_plugin_selector()->show_manager ();
1055         }
1056
1057         return ret;
1058 }
1059
1060 bool
1061 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
1062 {
1063         boost::shared_ptr<Processor> processor;
1064         if (child) {
1065                 processor = child->processor ();
1066         }
1067
1068         if (processor && Keyboard::is_delete_event (ev)) {
1069
1070                 Glib::signal_idle().connect (sigc::bind (
1071                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
1072                                 boost::weak_ptr<Processor>(processor)));
1073
1074         } else if (Keyboard::is_context_menu_event (ev)) {
1075
1076                 show_processor_menu (ev->time);
1077
1078         } else if (processor && Keyboard::is_button2_event (ev)
1079 #ifndef GTKOSX
1080                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
1081 #endif
1082                 ) {
1083
1084                 /* button2-click with no/appropriate modifiers */
1085
1086                 if (processor->active()) {
1087                         processor->deactivate ();
1088                 } else {
1089                         processor->activate ();
1090                 }
1091         }
1092
1093         return false;
1094 }
1095
1096 Menu *
1097 ProcessorBox::build_processor_menu ()
1098 {
1099         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
1100         processor_menu->set_name ("ArdourContextMenu");
1101         return processor_menu;
1102 }
1103
1104 void
1105 ProcessorBox::select_all_processors ()
1106 {
1107         processor_display.select_all ();
1108 }
1109
1110 void
1111 ProcessorBox::deselect_all_processors ()
1112 {
1113         processor_display.select_none ();
1114 }
1115
1116 void
1117 ProcessorBox::choose_plugin ()
1118 {
1119         _get_plugin_selector()->set_interested_object (*this);
1120 }
1121
1122 /** @return true if an error occurred, otherwise false */
1123 bool
1124 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
1125 {
1126         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
1127
1128                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
1129
1130                 Route::ProcessorStreams err_streams;
1131
1132                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
1133                         weird_plugin_dialog (**p, err_streams);
1134                         return true;
1135                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
1136                 } else {
1137
1138                         if (Profile->get_sae()) {
1139                                 processor->activate ();
1140                         }
1141                 }
1142         }
1143
1144         return false;
1145 }
1146
1147 void
1148 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
1149 {
1150         ArdourDialog dialog (_("Plugin Incompatibility"));
1151         Label label;
1152
1153         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
1154                         p.name(), streams.index);
1155
1156         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
1157         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
1158
1159         text += _("\nThis plugin has:\n");
1160         if (has_midi) {
1161                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
1162                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
1163         }
1164         if (has_audio) {
1165                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
1166                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
1167         }
1168
1169         text += _("\nbut at the insertion point, there are:\n");
1170         if (has_midi) {
1171                 uint32_t const n = streams.count.n_midi ();
1172                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
1173         }
1174         if (has_audio) {
1175                 uint32_t const n = streams.count.n_audio ();
1176                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
1177         }
1178
1179         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
1180         label.set_text(text);
1181
1182         dialog.get_vbox()->pack_start (label);
1183         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1184
1185         dialog.set_name (X_("PluginIODialog"));
1186         dialog.set_position (Gtk::WIN_POS_MOUSE);
1187         dialog.set_modal (true);
1188         dialog.show_all ();
1189
1190         dialog.run ();
1191 }
1192
1193 void
1194 ProcessorBox::choose_insert ()
1195 {
1196         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
1197         _route->add_processor_by_index (processor, _placement);
1198 }
1199
1200 /* Caller must not hold process lock */
1201 void
1202 ProcessorBox::choose_send ()
1203 {
1204         boost::shared_ptr<Send> send (new Send (*_session, _route->pannable(), _route->mute_master()));
1205
1206         /* make an educated guess at the initial number of outputs for the send */
1207         ChanCount outs = (_session->master_out())
1208                         ? _session->master_out()->n_outputs()
1209                         : _route->n_outputs();
1210
1211         /* XXX need processor lock on route */
1212         try {
1213                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1214                 send->output()->ensure_io (outs, false, this);
1215         } catch (AudioEngine::PortRegistrationFailure& err) {
1216                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
1217                 return;
1218         }
1219
1220         /* let the user adjust the IO setup before creation.
1221
1222            Note: this dialog is NOT modal - we just leave it to run and it will
1223            return when its Finished signal is emitted - typically when the window
1224            is closed.
1225          */
1226
1227         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
1228         ios->show ();
1229
1230         /* keep a reference to the send so it doesn't get deleted while
1231            the IOSelectorWindow is doing its stuff
1232         */
1233         _processor_being_created = send;
1234
1235         ios->selector().Finished.connect (sigc::bind (
1236                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
1237                         boost::weak_ptr<Processor>(send), ios));
1238
1239 }
1240
1241 void
1242 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1243 {
1244         boost::shared_ptr<Processor> processor (weak_processor.lock());
1245
1246         /* drop our temporary reference to the new send */
1247         _processor_being_created.reset ();
1248
1249         if (!processor) {
1250                 return;
1251         }
1252
1253         switch (r) {
1254         case IOSelector::Cancelled:
1255                 // processor will go away when all shared_ptrs to it vanish
1256                 break;
1257
1258         case IOSelector::Accepted:
1259                 _route->add_processor_by_index (processor, _placement);
1260                 if (Profile->get_sae()) {
1261                         processor->activate ();
1262                 }
1263                 break;
1264         }
1265
1266         delete_when_idle (ios);
1267 }
1268
1269 void
1270 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1271 {
1272         boost::shared_ptr<Processor> processor (weak_processor.lock());
1273
1274         /* drop our temporary reference to the new return */
1275         _processor_being_created.reset ();
1276
1277         if (!processor) {
1278                 return;
1279         }
1280
1281         switch (r) {
1282         case IOSelector::Cancelled:
1283                 // processor will go away when all shared_ptrs to it vanish
1284                 break;
1285
1286         case IOSelector::Accepted:
1287                 _route->add_processor_by_index (processor, _placement);
1288                 if (Profile->get_sae()) {
1289                         processor->activate ();
1290                 }
1291                 break;
1292         }
1293
1294         delete_when_idle (ios);
1295 }
1296
1297 void
1298 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
1299 {
1300         if (!_route) {
1301                 return;
1302         }
1303
1304         boost::shared_ptr<Route> target = wr.lock();
1305
1306         if (!target) {
1307                 return;
1308         }
1309
1310         _session->add_internal_send (target, _placement, _route);
1311 }
1312
1313 void
1314 ProcessorBox::route_processors_changed (RouteProcessorChange c)
1315 {
1316         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
1317                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
1318                 return;
1319         }
1320
1321         redisplay_processors ();
1322 }
1323
1324 void
1325 ProcessorBox::redisplay_processors ()
1326 {
1327         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
1328         bool     fader_seen;
1329
1330         if (no_processor_redisplay) {
1331                 return;
1332         }
1333
1334         processor_display.clear ();
1335
1336         _visible_prefader_processors = 0;
1337         fader_seen = false;
1338
1339         _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &ProcessorBox::help_count_visible_prefader_processors), 
1340                                                &_visible_prefader_processors, &fader_seen));
1341
1342         if (_visible_prefader_processors == 0) { // fader only
1343                 BlankProcessorEntry* bpe = new BlankProcessorEntry (this, _width);
1344                 processor_display.add_child (bpe);
1345         }
1346
1347         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
1348
1349         for (list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin(); i != _processor_window_proxies.end(); ++i) {
1350                 (*i)->marked = false;
1351         }
1352
1353         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
1354
1355         /* trim dead wood from the processor window proxy list */
1356
1357         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin();
1358         while (i != _processor_window_proxies.end()) {
1359                 list<ProcessorWindowProxy*>::iterator j = i;
1360                 ++j;
1361
1362                 if (!(*i)->marked) {
1363                         ARDOUR_UI::instance()->remove_window_proxy (*i);
1364                         delete *i;
1365                         _processor_window_proxies.erase (i);
1366                 }
1367
1368                 i = j;
1369         }
1370
1371         setup_entry_positions ();
1372 }
1373
1374 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
1375  *  not already have one.
1376  */
1377 void
1378 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
1379 {
1380         boost::shared_ptr<Processor> p = w.lock ();
1381         if (!p) {
1382                 return;
1383         }
1384
1385         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
1386         while (i != _processor_window_proxies.end()) {
1387
1388                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1389
1390                 if (p == t) {
1391                         /* this processor is already on the list; done */
1392                         (*i)->marked = true;
1393                         return;
1394                 }
1395
1396                 ++i;
1397         }
1398
1399         /* not on the list; add it */
1400
1401         string loc;
1402         if (_parent_strip) {
1403                 if (_parent_strip->mixer_owned()) {
1404                         loc = X_("M");
1405                 } else {
1406                         loc = X_("R");
1407                 }
1408         } else {
1409                 loc = X_("P");
1410         }
1411
1412         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1413                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1414                 _session->extra_xml (X_("UI")),
1415                 this,
1416                 w);
1417
1418         wp->marked = true;
1419
1420         /* if the processor already has an existing UI,
1421            note that so that we don't recreate it
1422         */
1423
1424         void* existing_ui = p->get_ui ();
1425
1426         if (existing_ui) {
1427                 wp->set (static_cast<Gtk::Window*>(existing_ui));
1428         }
1429
1430         _processor_window_proxies.push_back (wp);
1431         ARDOUR_UI::instance()->add_window_proxy (wp);
1432 }
1433
1434 void
1435 ProcessorBox::help_count_visible_prefader_processors (boost::weak_ptr<Processor> p, uint32_t* cnt, bool* amp_seen)
1436 {
1437         boost::shared_ptr<Processor> processor (p.lock ());
1438
1439         if (processor && processor->display_to_user()) {
1440
1441                 if (boost::dynamic_pointer_cast<Amp>(processor)) {
1442                         *amp_seen = true;
1443                 } else {
1444                         if (!*amp_seen) {
1445                                 (*cnt)++;
1446                         }
1447                 }
1448         }
1449 }
1450
1451 void
1452 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1453 {
1454         boost::shared_ptr<Processor> processor (p.lock ());
1455
1456         if (!processor || !processor->display_to_user()) {
1457                 return;
1458         }
1459
1460         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
1461         ProcessorEntry* e = 0;
1462         if (plugin_insert) {
1463                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
1464         } else {
1465                 e = new ProcessorEntry (this, processor, _width);
1466         }
1467
1468         /* Set up this entry's state from the GUIObjectState */
1469         XMLNode* proc = entry_gui_object_state (e);
1470         if (proc) {
1471                 e->set_control_state (proc);
1472         }
1473         
1474         if (boost::dynamic_pointer_cast<Send> (processor)) {
1475                 /* Always show send controls */
1476                 e->show_all_controls ();
1477         }
1478
1479         processor_display.add_child (e);
1480 }
1481
1482 void
1483 ProcessorBox::reordered ()
1484 {
1485         compute_processor_sort_keys ();
1486         setup_entry_positions ();
1487 }
1488
1489 void
1490 ProcessorBox::setup_entry_positions ()
1491 {
1492         list<ProcessorEntry*> children = processor_display.children ();
1493         bool pre_fader = true;
1494
1495         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1496                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor())) {
1497                         pre_fader = false;
1498                         (*i)->set_position (ProcessorEntry::Fader);
1499                 } else {
1500                         if (pre_fader) {
1501                                 (*i)->set_position (ProcessorEntry::PreFader);
1502                         } else {
1503                                 (*i)->set_position (ProcessorEntry::PostFader);
1504                         }
1505                 }
1506         }
1507 }
1508
1509 void
1510 ProcessorBox::compute_processor_sort_keys ()
1511 {
1512         list<ProcessorEntry*> children = processor_display.children ();
1513         Route::ProcessorList our_processors;
1514
1515         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1516                 if ((*i)->processor()) {
1517                         our_processors.push_back ((*i)->processor ());
1518                 }
1519         }
1520
1521         if (_route->reorder_processors (our_processors)) {
1522                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1523                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1524                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1525                    when the drag is torn down after this handler function is finished.
1526                 */
1527                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1528         }
1529 }
1530
1531 void
1532 ProcessorBox::report_failed_reorder ()
1533 {
1534         /* reorder failed, so redisplay */
1535
1536         redisplay_processors ();
1537
1538         /* now tell them about the problem */
1539
1540         ArdourDialog dialog (_("Plugin Incompatibility"));
1541         Label label;
1542
1543         label.set_text (_("\
1544 You cannot reorder these plugins/sends/inserts\n\
1545 in that way because the inputs and\n\
1546 outputs will not work correctly."));
1547
1548         dialog.get_vbox()->set_border_width (12);
1549         dialog.get_vbox()->pack_start (label);
1550         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1551
1552         dialog.set_name (X_("PluginIODialog"));
1553         dialog.set_position (Gtk::WIN_POS_MOUSE);
1554         dialog.set_modal (true);
1555         dialog.show_all ();
1556
1557         dialog.run ();
1558 }
1559
1560 void
1561 ProcessorBox::rename_processors ()
1562 {
1563         ProcSelection to_be_renamed;
1564
1565         get_selected_processors (to_be_renamed);
1566
1567         if (to_be_renamed.empty()) {
1568                 return;
1569         }
1570
1571         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1572                 rename_processor (*i);
1573         }
1574 }
1575
1576 bool
1577 ProcessorBox::can_cut () const
1578 {
1579         vector<boost::shared_ptr<Processor> > sel;
1580
1581         get_selected_processors (sel);
1582
1583         /* cut_processors () does not cut inserts */
1584
1585         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1586
1587                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1588                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1589                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1590                         return true;
1591                 }
1592         }
1593
1594         return false;
1595 }
1596
1597 void
1598 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1599 {
1600         if (to_be_removed.empty()) {
1601                 return;
1602         }
1603
1604         XMLNode* node = new XMLNode (X_("cut"));
1605         Route::ProcessorList to_cut;
1606
1607         no_processor_redisplay = true;
1608         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1609                 // Cut only plugins, sends and returns
1610                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1611                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1612                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1613
1614                         Window* w = get_processor_ui (*i);
1615
1616                         if (w) {
1617                                 w->hide ();
1618                         }
1619
1620                         XMLNode& child ((*i)->get_state());
1621                         node->add_child_nocopy (child);
1622                         to_cut.push_back (*i);
1623                 }
1624         }
1625
1626         if (_route->remove_processors (to_cut) != 0) {
1627                 delete node;
1628                 no_processor_redisplay = false;
1629                 return;
1630         }
1631
1632         _rr_selection.set (node);
1633
1634         no_processor_redisplay = false;
1635         redisplay_processors ();
1636 }
1637
1638 void
1639 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1640 {
1641         if (to_be_copied.empty()) {
1642                 return;
1643         }
1644
1645         XMLNode* node = new XMLNode (X_("copy"));
1646
1647         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1648                 // Copy only plugins, sends, returns
1649                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1650                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1651                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1652                         node->add_child_nocopy ((*i)->get_state());
1653                 }
1654         }
1655
1656         _rr_selection.set (node);
1657 }
1658
1659 void
1660 ProcessorBox::delete_processors (const ProcSelection& targets)
1661 {
1662         if (targets.empty()) {
1663                 return;
1664         }
1665
1666         no_processor_redisplay = true;
1667
1668         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1669
1670                 Window* w = get_processor_ui (*i);
1671
1672                 if (w) {
1673                         w->hide ();
1674                 }
1675
1676                 _route->remove_processor(*i);
1677         }
1678
1679         no_processor_redisplay = false;
1680         redisplay_processors ();
1681 }
1682
1683 void
1684 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1685 {
1686         list<boost::shared_ptr<Processor> >::const_iterator x;
1687
1688         no_processor_redisplay = true;
1689         for (x = procs.begin(); x != procs.end(); ++x) {
1690
1691                 Window* w = get_processor_ui (*x);
1692
1693                 if (w) {
1694                         w->hide ();
1695                 }
1696
1697                 _route->remove_processor(*x);
1698         }
1699
1700         no_processor_redisplay = false;
1701         redisplay_processors ();
1702 }
1703
1704 gint
1705 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1706 {
1707         boost::shared_ptr<Processor> processor (weak_processor.lock());
1708
1709         if (!processor) {
1710                 return false;
1711         }
1712
1713         /* NOT copied to _mixer.selection() */
1714
1715         no_processor_redisplay = true;
1716         _route->remove_processor (processor);
1717         no_processor_redisplay = false;
1718         redisplay_processors ();
1719
1720         return false;
1721 }
1722
1723 void
1724 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1725 {
1726         ArdourPrompter name_prompter (true);
1727         string result;
1728         name_prompter.set_title (_("Rename Processor"));
1729         name_prompter.set_prompt (_("New name:"));
1730         name_prompter.set_initial_text (processor->name());
1731         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1732         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1733         name_prompter.show_all ();
1734
1735         switch (name_prompter.run ()) {
1736
1737         case Gtk::RESPONSE_ACCEPT:
1738                 name_prompter.get_result (result);
1739                 if (result.length()) {
1740
1741                        int tries = 0;
1742                        string test = result;
1743
1744                        while (tries < 100) {
1745                                if (_session->io_name_is_legal (test)) {
1746                                        result = test;
1747                                        break;
1748                                }
1749                                tries++;
1750
1751                                test = string_compose ("%1-%2", result, tries);
1752                        }
1753
1754                        if (tries < 100) {
1755                                processor->set_name (result);
1756                        } else {
1757                                /* unlikely! */
1758                                ARDOUR_UI::instance()->popup_error
1759                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
1760                        }
1761                 }
1762                 break;
1763         }
1764
1765         return;
1766 }
1767
1768 void
1769 ProcessorBox::paste_processors ()
1770 {
1771         if (_rr_selection.processors.empty()) {
1772                 return;
1773         }
1774
1775         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1776 }
1777
1778 void
1779 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1780 {
1781
1782         if (_rr_selection.processors.empty()) {
1783                 return;
1784         }
1785
1786         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1787 }
1788
1789 void
1790 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1791 {
1792         XMLNodeConstIterator niter;
1793         list<boost::shared_ptr<Processor> > copies;
1794
1795         if (nlist.empty()) {
1796                 return;
1797         }
1798
1799         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1800
1801                 XMLProperty const * type = (*niter)->property ("type");
1802                 XMLProperty const * role = (*niter)->property ("role");
1803                 assert (type);
1804
1805                 boost::shared_ptr<Processor> p;
1806                 try {
1807                         if (type->value() == "meter" ||
1808                             type->value() == "main-outs" ||
1809                             type->value() == "amp" ||
1810                             type->value() == "intreturn") {
1811                                 /* do not paste meter, main outs, amp or internal returns */
1812                                 continue;
1813
1814                         } else if (type->value() == "intsend") {
1815                                 
1816                                 /* aux sends are OK, but those used for
1817                                  * other purposes, are not.
1818                                  */
1819                                 
1820                                 assert (role);
1821
1822                                 if (role->value() != "Aux") {
1823                                         continue;
1824                                 }
1825
1826                                 XMLNode n (**niter);
1827                                 InternalSend* s = new InternalSend (*_session, _route->pannable(), _route->mute_master(),
1828                                                                     boost::shared_ptr<Route>(), Delivery::Aux); 
1829
1830                                 IOProcessor::prepare_for_reset (n, s->name());
1831
1832                                 if (s->set_state (n, Stateful::loading_state_version)) {
1833                                         delete s;
1834                                         return;
1835                                 }
1836
1837                                 p.reset (s);
1838
1839                         } else if (type->value() == "send") {
1840
1841                                 XMLNode n (**niter);
1842                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
1843
1844                                 IOProcessor::prepare_for_reset (n, s->name());
1845                                 
1846                                 if (s->set_state (n, Stateful::loading_state_version)) {
1847                                         delete s;
1848                                         return;
1849                                 }
1850
1851                                 p.reset (s);
1852
1853                         } else if (type->value() == "return") {
1854
1855                                 XMLNode n (**niter);
1856                                 Return* r = new Return (*_session);
1857
1858                                 IOProcessor::prepare_for_reset (n, r->name());
1859
1860                                 if (r->set_state (n, Stateful::loading_state_version)) {
1861                                         delete r;
1862                                         return;
1863                                 }
1864
1865                                 p.reset (r);
1866
1867                         } else if (type->value() == "port") {
1868
1869                                 XMLNode n (**niter);
1870                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
1871                                 
1872                                 IOProcessor::prepare_for_reset (n, pi->name());
1873                                 
1874                                 if (pi->set_state (n, Stateful::loading_state_version)) {
1875                                         return;
1876                                 }
1877                                 
1878                                 p.reset (pi);
1879
1880                         } else {
1881                                 /* XXX its a bit limiting to assume that everything else
1882                                    is a plugin.
1883                                 */
1884
1885                                 p.reset (new PluginInsert (*_session));
1886                                 p->set_state (**niter, Stateful::current_state_version);
1887                         }
1888
1889                         copies.push_back (p);
1890                 }
1891
1892                 catch (...) {
1893                         error << _("plugin insert constructor failed") << endmsg;
1894                 }
1895         }
1896
1897         if (copies.empty()) {
1898                 return;
1899         }
1900
1901         if (_route->add_processors (copies, p)) {
1902
1903                 string msg = _(
1904                         "Copying the set of processors on the clipboard failed,\n\
1905 probably because the I/O configuration of the plugins\n\
1906 could not match the configuration of this track.");
1907                 MessageDialog am (msg);
1908                 am.run ();
1909         }
1910 }
1911
1912 void
1913 ProcessorBox::get_selected_processors (ProcSelection& processors) const
1914 {
1915         const list<ProcessorEntry*> selection = processor_display.selection ();
1916         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
1917                 processors.push_back ((*i)->processor ());
1918         }
1919 }
1920
1921 void
1922 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1923 {
1924         list<ProcessorEntry*> selection = processor_display.selection ();
1925         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1926                 (this->*method) ((*i)->processor ());
1927         }
1928 }
1929
1930 void
1931 ProcessorBox::all_visible_processors_active (bool state)
1932 {
1933         _route->all_visible_processors_active (state);
1934 }
1935
1936 void
1937 ProcessorBox::ab_plugins ()
1938 {
1939         _route->ab_plugins (ab_direction);
1940         ab_direction = !ab_direction;
1941 }
1942
1943
1944 void
1945 ProcessorBox::clear_processors ()
1946 {
1947         string prompt;
1948         vector<string> choices;
1949
1950         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1951                                    "(this cannot be undone)"), _route->name());
1952
1953         choices.push_back (_("Cancel"));
1954         choices.push_back (_("Yes, remove them all"));
1955
1956         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1957
1958         if (prompter.run () == 1) {
1959                 _route->clear_processors (PreFader);
1960                 _route->clear_processors (PostFader);
1961         }
1962 }
1963
1964 void
1965 ProcessorBox::clear_processors (Placement p)
1966 {
1967         string prompt;
1968         vector<string> choices;
1969
1970         if (p == PreFader) {
1971                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1972                                            "(this cannot be undone)"), _route->name());
1973         } else {
1974                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1975                                            "(this cannot be undone)"), _route->name());
1976         }
1977
1978         choices.push_back (_("Cancel"));
1979         choices.push_back (_("Yes, remove them all"));
1980
1981         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1982
1983         if (prompter.run () == 1) {
1984                 _route->clear_processors (p);
1985         }
1986 }
1987
1988 bool
1989 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
1990 {
1991         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
1992         if (at && at->freeze_state() == AudioTrack::Frozen) {
1993                 return false;
1994         }
1995
1996         if (
1997                 (boost::dynamic_pointer_cast<Send> (processor) && !boost::dynamic_pointer_cast<InternalSend> (processor))||
1998                 boost::dynamic_pointer_cast<Return> (processor) ||
1999                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
2000                 boost::dynamic_pointer_cast<PortInsert> (processor)
2001                 ) {
2002                 return true;
2003         }
2004
2005         return false;
2006 }
2007
2008 bool
2009 ProcessorBox::one_processor_can_be_edited ()
2010 {
2011         list<ProcessorEntry*> selection = processor_display.selection ();
2012         list<ProcessorEntry*>::iterator i = selection.begin();
2013         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
2014                 ++i;
2015         }
2016
2017         return (i != selection.end());
2018 }
2019
2020 void
2021 ProcessorBox::toggle_edit_processor (boost::shared_ptr<Processor> processor)
2022 {
2023         boost::shared_ptr<Send> send;
2024         boost::shared_ptr<InternalSend> internal_send;
2025         boost::shared_ptr<Return> retrn;
2026         boost::shared_ptr<PluginInsert> plugin_insert;
2027         boost::shared_ptr<PortInsert> port_insert;
2028         Window* gidget = 0;
2029
2030         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
2031
2032                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
2033                         return;
2034                 }
2035         }
2036
2037         if (boost::dynamic_pointer_cast<Amp> (processor)) {
2038
2039                 if (_parent_strip) {
2040                         _parent_strip->revert_to_default_display ();
2041                 }
2042
2043         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2044
2045                 if (!_session->engine().connected()) {
2046                         return;
2047                 }
2048
2049                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2050                         SendUIWindow* w = new SendUIWindow (send, _session);
2051                         w->show ();
2052                 } else {
2053                         /* assign internal send to main fader */
2054                         if (_parent_strip) {
2055                                 if (_parent_strip->current_delivery() == send) {
2056                                         _parent_strip->revert_to_default_display ();
2057                                 } else {
2058                                         _parent_strip->show_send(send);
2059                                 }
2060                         } 
2061                 }
2062
2063         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
2064
2065                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
2066                         /* no GUI for these */
2067                         return;
2068                 }
2069
2070                 if (!_session->engine().connected()) {
2071                         return;
2072                 }
2073
2074                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
2075
2076                 ReturnUIWindow *return_ui;
2077                 Window* w = get_processor_ui (retrn);
2078
2079                 if (w == 0) {
2080
2081                         return_ui = new ReturnUIWindow (retrn, _session);
2082                         return_ui->set_title (retrn->name ());
2083                         set_processor_ui (send, return_ui);
2084
2085                 } else {
2086                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
2087                 }
2088
2089                 gidget = return_ui;
2090
2091         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2092
2093                 PluginUIWindow *plugin_ui;
2094
2095                 /* these are both allowed to be null */
2096
2097                 Container* toplevel = get_toplevel();
2098                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
2099
2100                 Window* w = get_processor_ui (plugin_insert);
2101
2102                 if (w == 0) {
2103
2104                         plugin_ui = new PluginUIWindow (win, plugin_insert);
2105                         plugin_ui->set_title (generate_processor_title (plugin_insert));
2106                         set_processor_ui (plugin_insert, plugin_ui);
2107
2108                 } else {
2109                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
2110                         plugin_ui->set_parent (win);
2111                 }
2112
2113                 gidget = plugin_ui;
2114
2115         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
2116
2117                 if (!_session->engine().connected()) {
2118                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
2119                         msg.run ();
2120                         return;
2121                 }
2122
2123                 PortInsertWindow *io_selector;
2124
2125                 Window* w = get_processor_ui (port_insert);
2126
2127                 if (w == 0) {
2128                         io_selector = new PortInsertWindow (_session, port_insert);
2129                         set_processor_ui (port_insert, io_selector);
2130
2131                 } else {
2132                         io_selector = dynamic_cast<PortInsertWindow *> (w);
2133                 }
2134
2135                 gidget = io_selector;
2136         }
2137
2138         if (gidget) {
2139                 if (gidget->is_visible()) {
2140                         gidget->hide ();
2141                 } else {
2142                         gidget->show_all ();
2143                         gidget->present ();
2144                 }
2145         }
2146 }
2147
2148 /** Toggle a generic (Ardour-generated) plugin UI */
2149 void
2150 ProcessorBox::toggle_edit_generic_processor (boost::shared_ptr<Processor> processor)
2151 {
2152         boost::shared_ptr<PluginInsert> plugin_insert
2153                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
2154         if (!plugin_insert) {
2155                 return;
2156         }
2157
2158         Container*      toplevel  = get_toplevel();
2159         Window*         win       = dynamic_cast<Gtk::Window*>(toplevel);
2160         PluginUIWindow* plugin_ui = new PluginUIWindow(win, plugin_insert, true, false);
2161         plugin_ui->set_title(generate_processor_title (plugin_insert));
2162
2163         if (plugin_ui->is_visible()) {
2164                 plugin_ui->hide();
2165         } else {
2166                 plugin_ui->show_all();
2167                 plugin_ui->present();
2168         }
2169 }
2170
2171 void
2172 ProcessorBox::register_actions ()
2173 {
2174         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("ProcessorMenu"));
2175         Glib::RefPtr<Action> act;
2176
2177         /* new stuff */
2178         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
2179                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
2180
2181         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
2182                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
2183         ActionManager::jack_sensitive_actions.push_back (act);
2184         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New External Send ..."),
2185                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
2186         ActionManager::jack_sensitive_actions.push_back (act);
2187
2188         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
2189
2190         ActionManager::register_action (popup_act_grp, X_("controls"), _("Controls"));
2191
2192         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
2193                         sigc::ptr_fun (ProcessorBox::rb_clear));
2194         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
2195                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
2196         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
2197                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
2198
2199         /* standard editing stuff */
2200         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
2201                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
2202         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
2203         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
2204                         sigc::ptr_fun (ProcessorBox::rb_copy));
2205         ActionManager::plugin_selection_sensitive_actions.push_back(act);
2206
2207         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
2208                         sigc::ptr_fun (ProcessorBox::rb_delete));
2209         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
2210
2211         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
2212                         sigc::ptr_fun (ProcessorBox::rb_paste));
2213         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
2214                         sigc::ptr_fun (ProcessorBox::rb_rename));
2215         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
2216                         sigc::ptr_fun (ProcessorBox::rb_select_all));
2217         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
2218                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
2219
2220         /* activation etc. */
2221
2222         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate All"),
2223                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
2224         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate All"),
2225                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
2226         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
2227                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
2228
2229         /* show editors */
2230         edit_action = ActionManager::register_action (
2231                 popup_act_grp, X_("edit"), _("Edit..."),
2232                 sigc::ptr_fun (ProcessorBox::rb_edit));
2233
2234         edit_generic_action = ActionManager::register_action (
2235                 popup_act_grp, X_("edit-generic"), _("Edit with basic controls..."),
2236                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
2237
2238         ActionManager::add_action_group (popup_act_grp);
2239 }
2240
2241 void
2242 ProcessorBox::rb_edit_generic ()
2243 {
2244         if (_current_processor_box == 0) {
2245                 return;
2246         }
2247
2248         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_generic_processor);
2249 }
2250
2251 void
2252 ProcessorBox::rb_ab_plugins ()
2253 {
2254         if (_current_processor_box == 0) {
2255                 return;
2256         }
2257
2258         _current_processor_box->ab_plugins ();
2259 }
2260
2261 void
2262 ProcessorBox::rb_choose_plugin ()
2263 {
2264         if (_current_processor_box == 0) {
2265                 return;
2266         }
2267         _current_processor_box->choose_plugin ();
2268 }
2269
2270 void
2271 ProcessorBox::rb_choose_insert ()
2272 {
2273         if (_current_processor_box == 0) {
2274                 return;
2275         }
2276         _current_processor_box->choose_insert ();
2277 }
2278
2279 void
2280 ProcessorBox::rb_choose_send ()
2281 {
2282         if (_current_processor_box == 0) {
2283                 return;
2284         }
2285         _current_processor_box->choose_send ();
2286 }
2287
2288 void
2289 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
2290 {
2291         if (_current_processor_box == 0) {
2292                 return;
2293         }
2294
2295         _current_processor_box->choose_aux (wr);
2296 }
2297
2298 void
2299 ProcessorBox::rb_clear ()
2300 {
2301         if (_current_processor_box == 0) {
2302                 return;
2303         }
2304
2305         _current_processor_box->clear_processors ();
2306 }
2307
2308
2309 void
2310 ProcessorBox::rb_clear_pre ()
2311 {
2312         if (_current_processor_box == 0) {
2313                 return;
2314         }
2315
2316         _current_processor_box->clear_processors (PreFader);
2317 }
2318
2319
2320 void
2321 ProcessorBox::rb_clear_post ()
2322 {
2323         if (_current_processor_box == 0) {
2324                 return;
2325         }
2326
2327         _current_processor_box->clear_processors (PostFader);
2328 }
2329
2330 void
2331 ProcessorBox::rb_cut ()
2332 {
2333         if (_current_processor_box == 0) {
2334                 return;
2335         }
2336
2337         _current_processor_box->processor_operation (ProcessorsCut);
2338 }
2339
2340 void
2341 ProcessorBox::rb_delete ()
2342 {
2343         if (_current_processor_box == 0) {
2344                 return;
2345         }
2346
2347         _current_processor_box->processor_operation (ProcessorsDelete);
2348 }
2349
2350 void
2351 ProcessorBox::rb_copy ()
2352 {
2353         if (_current_processor_box == 0) {
2354                 return;
2355         }
2356         _current_processor_box->processor_operation (ProcessorsCopy);
2357 }
2358
2359 void
2360 ProcessorBox::rb_paste ()
2361 {
2362         if (_current_processor_box == 0) {
2363                 return;
2364         }
2365
2366         _current_processor_box->processor_operation (ProcessorsPaste);
2367 }
2368
2369 void
2370 ProcessorBox::rb_rename ()
2371 {
2372         if (_current_processor_box == 0) {
2373                 return;
2374         }
2375         _current_processor_box->rename_processors ();
2376 }
2377
2378 void
2379 ProcessorBox::rb_select_all ()
2380 {
2381         if (_current_processor_box == 0) {
2382                 return;
2383         }
2384
2385         _current_processor_box->processor_operation (ProcessorsSelectAll);
2386 }
2387
2388 void
2389 ProcessorBox::rb_deselect_all ()
2390 {
2391         if (_current_processor_box == 0) {
2392                 return;
2393         }
2394
2395         _current_processor_box->deselect_all_processors ();
2396 }
2397
2398 void
2399 ProcessorBox::rb_activate_all ()
2400 {
2401         if (_current_processor_box == 0) {
2402                 return;
2403         }
2404
2405         _current_processor_box->all_visible_processors_active (true);
2406 }
2407
2408 void
2409 ProcessorBox::rb_deactivate_all ()
2410 {
2411         if (_current_processor_box == 0) {
2412                 return;
2413         }
2414         _current_processor_box->all_visible_processors_active (false);
2415 }
2416
2417 void
2418 ProcessorBox::rb_edit ()
2419 {
2420         if (_current_processor_box == 0) {
2421                 return;
2422         }
2423
2424         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_processor);
2425 }
2426
2427 void
2428 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
2429 {
2430         if (!what_changed.contains (ARDOUR::Properties::name)) {
2431                 return;
2432         }
2433
2434         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
2435
2436         boost::shared_ptr<Processor> processor;
2437         boost::shared_ptr<PluginInsert> plugin_insert;
2438         boost::shared_ptr<Send> send;
2439
2440         list<ProcessorEntry*> children = processor_display.children();
2441
2442         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
2443
2444                 processor = (*iter)->processor ();
2445
2446                 if (!processor) {
2447                         continue;
2448                 }
2449
2450                 Window* w = get_processor_ui (processor);
2451
2452                 if (!w) {
2453                         continue;
2454                 }
2455
2456                 /* rename editor windows for sends and plugins */
2457
2458                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2459                         w->set_title (send->name ());
2460                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2461                         w->set_title (generate_processor_title (plugin_insert));
2462                 }
2463         }
2464 }
2465
2466 string
2467 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
2468 {
2469         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
2470         string::size_type email_pos;
2471
2472         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
2473                 maker = maker.substr (0, email_pos - 1);
2474         }
2475
2476         if (maker.length() > 32) {
2477                 maker = maker.substr (0, 32);
2478                 maker += " ...";
2479         }
2480
2481         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
2482 }
2483
2484 /** @param p Processor.
2485  *  @return the UI window for \a p.
2486  */
2487 Window *
2488 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
2489 {
2490         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_proxies.begin ();
2491         while (i != _processor_window_proxies.end()) {
2492                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2493                 if (t && t == p) {
2494                         return (*i)->get ();
2495                 }
2496
2497                 ++i;
2498         }
2499
2500         return 0;
2501 }
2502
2503 /** Make a note of the UI window that a processor is using.
2504  *  @param p Processor.
2505  *  @param w UI window.
2506  */
2507 void
2508 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2509 {
2510         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
2511
2512         p->set_ui (w);
2513
2514         while (i != _processor_window_proxies.end()) {
2515                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2516                 if (t && t == p) {
2517                         (*i)->set (w);
2518                         return;
2519                 }
2520
2521                 ++i;
2522         }
2523
2524         /* we shouldn't get here, because the ProcessorUIList should always contain
2525            an entry for each processor.
2526         */
2527         assert (false);
2528 }
2529
2530 void
2531 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
2532 {
2533         boost::shared_ptr<Delivery> d = w.lock ();
2534         if (!d) {
2535                 return;
2536         }
2537
2538         list<ProcessorEntry*> children = processor_display.children ();
2539         list<ProcessorEntry*>::const_iterator i = children.begin();
2540         while (i != children.end() && (*i)->processor() != d) {
2541                 ++i;
2542         }
2543
2544         if (i == children.end()) {
2545                 processor_display.set_active (0);
2546         } else {
2547                 processor_display.set_active (*i);
2548         }
2549 }
2550
2551 /** Called to repair the damage of Editor::show_window doing a show_all() */
2552 void
2553 ProcessorBox::hide_things ()
2554 {
2555         list<ProcessorEntry*> c = processor_display.children ();
2556         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
2557                 (*i)->hide_things ();
2558         }
2559 }
2560
2561 void
2562 ProcessorBox::processor_menu_unmapped ()
2563 {
2564         processor_display.remove_placeholder ();
2565 }
2566
2567 XMLNode *
2568 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
2569 {
2570         if (!_parent_strip) {
2571                 return 0;
2572         }
2573
2574         GUIObjectState& st = _parent_strip->gui_object_state ();
2575         
2576         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
2577         assert (strip);
2578         return st.get_or_add_node (strip, entry->state_id());
2579 }
2580
2581 void
2582 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
2583 {
2584         XMLNode* proc = entry_gui_object_state (entry);
2585         if (!proc) {
2586                 return;
2587         }
2588
2589         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
2590         proc->remove_nodes_and_delete (X_("Object"));
2591         entry->add_control_state (proc);
2592 }
2593
2594 ProcessorWindowProxy::ProcessorWindowProxy (
2595         string const & name,
2596         XMLNode const * node,
2597         ProcessorBox* box,
2598         boost::weak_ptr<Processor> processor
2599         )
2600         : WindowProxy<Gtk::Window> (name, node)
2601         , marked (false)
2602         , _processor_box (box)
2603         , _processor (processor)
2604 {
2605
2606 }
2607
2608
2609 void
2610 ProcessorWindowProxy::show ()
2611 {
2612         boost::shared_ptr<Processor> p = _processor.lock ();
2613         if (!p) {
2614                 return;
2615         }
2616
2617         _processor_box->toggle_edit_processor (p);
2618 }
2619