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