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