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