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