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