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