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