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