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