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