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