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