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