ab6d9f546d027192f39602337e24a9fcc72fea41
[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/stop_signal.h>
41 #include <gtkmm2ext/doi.h>
42
43 #include "ardour/amp.h"
44 #include "ardour/ardour.h"
45 #include "ardour/audio_diskstream.h"
46 #include "ardour/audio_track.h"
47 #include "ardour/audioengine.h"
48 #include "ardour/internal_send.h"
49 #include "ardour/ladspa_plugin.h"
50 #include "ardour/meter.h"
51 #include "ardour/plugin_insert.h"
52 #include "ardour/port_insert.h"
53 #include "ardour/profile.h"
54 #include "ardour/return.h"
55 #include "ardour/route.h"
56 #include "ardour/send.h"
57 #include "ardour/session.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 "processor_box.h"
70 #include "public_editor.h"
71 #include "return_ui.h"
72 #include "route_processor_selection.h"
73 #include "send_ui.h"
74 #include "utils.h"
75
76 #include "i18n.h"
77
78 #ifdef HAVE_AUDIOUNITS
79 class AUPluginUI;
80 #endif
81
82 using namespace std;
83 using namespace ARDOUR;
84 using namespace PBD;
85 using namespace Gtk;
86 using namespace Glib;
87 using namespace Gtkmm2ext;
88
89 ProcessorBox* ProcessorBox::_current_processor_box = 0;
90 RefPtr<Action> ProcessorBox::paste_action;
91 Glib::RefPtr<Gdk::Pixbuf> SendProcessorEntry::_slider;
92
93 ProcessorEntry::ProcessorEntry (boost::shared_ptr<Processor> p, Width w)
94         : _processor (p)
95         , _width (w)
96 {
97         _hbox.pack_start (_active, false, false);
98         _event_box.add (_name);
99         _hbox.pack_start (_event_box, true, true);
100         _vbox.pack_start (_hbox);
101
102         _name.set_alignment (0, 0.5);
103         _name.set_text (name ());
104         _name.set_padding (2, 2);
105         
106         _active.set_active (_processor->active ());
107         _active.signal_toggled().connect (sigc::mem_fun (*this, &ProcessorEntry::active_toggled));
108         
109         _processor->ActiveChanged.connect (sigc::mem_fun (*this, &ProcessorEntry::processor_active_changed));
110         _processor->NameChanged.connect (sigc::mem_fun (*this, &ProcessorEntry::processor_name_changed));
111 }
112
113 EventBox&
114 ProcessorEntry::action_widget ()
115 {
116         return _event_box;
117 }
118
119 Gtk::Widget&
120 ProcessorEntry::widget ()
121 {
122         return _vbox;
123 }
124
125 string
126 ProcessorEntry::drag_text () const
127 {
128         return name ();
129 }
130
131 boost::shared_ptr<Processor>
132 ProcessorEntry::processor () const
133 {
134         return _processor;
135 }
136
137 void
138 ProcessorEntry::set_enum_width (Width w)
139 {
140         _width = w;
141 }
142
143 void
144 ProcessorEntry::active_toggled ()
145 {
146         if (_active.get_active ()) {
147                 if (!_processor->active ()) {
148                         _processor->activate ();
149                 }
150         } else {
151                 if (_processor->active ()) {
152                         _processor->deactivate ();
153                 }
154         }
155 }
156
157 void
158 ProcessorEntry::processor_active_changed ()
159 {
160         if (_active.get_active () != _processor->active ()) {
161                 _active.set_active (_processor->active ());
162         }
163 }
164
165 void
166 ProcessorEntry::processor_name_changed ()
167 {
168         _name.set_text (name ());
169 }
170
171 string
172 ProcessorEntry::name () const
173 {
174         boost::shared_ptr<Send> send;
175         string name_display;
176         
177         if (!_processor->active()) {
178                 name_display = " (";
179         }
180         
181         if ((send = boost::dynamic_pointer_cast<Send> (_processor)) != 0 &&
182             !boost::dynamic_pointer_cast<InternalSend>(_processor)) {
183                 
184                 name_display += '>';
185                 
186                 /* grab the send name out of its overall name */
187                 
188                 string::size_type lbracket, rbracket;
189                 lbracket = send->name().find ('[');
190                 rbracket = send->name().find (']');
191                 
192                 switch (_width) {
193                 case Wide:
194                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
195                         break;
196                 case Narrow:
197                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
198                         break;
199                 }
200                 
201         } else {
202                 
203                 switch (_width) {
204                 case Wide:
205                         name_display += _processor->display_name();
206                         break;
207                 case Narrow:
208                         name_display += PBD::short_version (_processor->display_name(), 5);
209                         break;
210                 }
211                 
212         }
213         
214         if (!_processor->active()) {
215                 name_display += ')';
216         }
217         
218         return name_display;
219 }
220
221 SendProcessorEntry::SendProcessorEntry (boost::shared_ptr<Send> s, Width w)
222         : ProcessorEntry (s, w),
223           _send (s),
224           _adjustment (0, 0, 1, 0.01, 0.1),
225           _fader (_slider, &_adjustment, 0, false),
226           _ignore_gain_change (false)
227 {
228         _fader.set_controllable (_send->amp()->gain_control ());
229         _vbox.pack_start (_fader);
230
231         _adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &SendProcessorEntry::gain_adjusted));
232         _send->amp()->gain_control()->Changed.connect (sigc::mem_fun (*this, &SendProcessorEntry::show_gain));
233         show_gain ();
234 }
235
236 void
237 SendProcessorEntry::setup_slider_pix ()
238 {
239         _slider = ::get_icon ("fader_belt_h_thin");
240         assert (_slider);
241 }
242
243 void
244 SendProcessorEntry::show_gain ()
245 {
246         ENSURE_GUI_THREAD (*this, &SendProcessorEntry::show_gain)
247         
248         float const value = gain_to_slider_position (_send->amp()->gain ());
249
250         if (_adjustment.get_value() != value) {
251                 _ignore_gain_change = true;
252                 _adjustment.set_value (value);
253                 _ignore_gain_change = false;
254         }
255 }
256
257 void
258 SendProcessorEntry::gain_adjusted ()
259 {
260         if (_ignore_gain_change) {
261                 return;
262         }
263
264         _send->amp()->set_gain (slider_position_to_gain (_adjustment.get_value()), this);
265 }
266
267 void
268 SendProcessorEntry::set_pixel_width (int p)
269 {
270         _fader.set_fader_length (p);
271 }
272
273 ProcessorBox::ProcessorBox (ARDOUR::Session& sess, sigc::slot<PluginSelector*> get_plugin_selector,
274                         RouteRedirectSelection& rsel, MixerStrip* parent, bool owner_is_mixer)
275         : _session(sess)
276         , _parent_strip (parent)
277         , _owner_is_mixer (owner_is_mixer)
278         , ab_direction (true)
279         , _get_plugin_selector (get_plugin_selector)
280         , _placement(PreFader)
281         , _rr_selection(rsel)
282 {
283         _width = Wide;
284         processor_menu = 0;
285         send_action_menu = 0;
286         no_processor_redisplay = false;
287
288         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
289         processor_scroller.add (processor_display);
290         pack_start (processor_scroller, true, true);
291
292         processor_display.set_flags (CAN_FOCUS);
293         processor_display.set_name ("ProcessorSelector");
294         processor_display.set_size_request (48, -1);
295         processor_display.set_data ("processorbox", this);
296
297         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
298         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
299
300         processor_display.signal_key_press_event().connect (sigc::mem_fun(*this, &ProcessorBox::processor_key_press_event));
301         processor_display.signal_key_release_event().connect (sigc::mem_fun(*this, &ProcessorBox::processor_key_release_event));
302
303         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
304         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
305
306         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
307         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
308         processor_display.SelectionChanged.connect (sigc::mem_fun (*this, &ProcessorBox::selection_changed));
309 }
310
311 ProcessorBox::~ProcessorBox ()
312 {
313 }
314
315 void
316 ProcessorBox::set_route (boost::shared_ptr<Route> r)
317 {
318         if (_route == r) {
319                 return;
320         }
321         
322         connections.clear ();
323
324         /* new route: any existing block on processor redisplay must be meaningless */
325         no_processor_redisplay = false;
326         _route = r;
327
328         connections.push_back (_route->processors_changed.connect (sigc::mem_fun (*this, &ProcessorBox::route_processors_changed)));
329         connections.push_back (_route->GoingAway.connect (
330                         sigc::mem_fun (*this, &ProcessorBox::route_going_away)));
331         connections.push_back (_route->NameChanged.connect (
332                         sigc::mem_fun(*this, &ProcessorBox::route_name_changed)));
333
334         redisplay_processors ();
335 }
336
337 void
338 ProcessorBox::route_going_away ()
339 {
340         /* don't keep updating display as processors are deleted */
341         no_processor_redisplay = true;
342 }
343
344 void
345 ProcessorBox::object_drop(DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
346 {
347         boost::shared_ptr<Processor> p;
348         if (position) {
349                 p = position->processor ();
350         }
351
352         list<ProcessorEntry*> children = source->selection ();
353         list<boost::shared_ptr<Processor> > procs;
354         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
355                 procs.push_back ((*i)->processor ());
356         }
357
358         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
359                 XMLNode& state = (*i)->get_state ();
360                 XMLNodeList nlist;
361                 nlist.push_back (&state);
362                 paste_processor_state (nlist, p);
363                 delete &state;
364         }
365
366         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
367            ourselves.
368         */
369
370         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
371                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
372                 if (other) {
373                         cerr << "source was another processor box, delete the selected items\n";
374                         other->delete_dragged_processors (procs);
375                 }
376         }
377 }
378
379 void
380 ProcessorBox::update()
381 {
382         redisplay_processors ();
383 }
384
385 void
386 ProcessorBox::set_width (Width w)
387 {
388         if (_width == w) {
389                 return;
390         }
391         
392         _width = w;
393
394         list<ProcessorEntry*> children = processor_display.children ();
395         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
396                 (*i)->set_enum_width (w);
397         }
398
399         redisplay_processors ();
400 }
401
402 void
403 ProcessorBox::remove_processor_gui (boost::shared_ptr<Processor> processor)
404 {
405         boost::shared_ptr<Send> send;
406         boost::shared_ptr<Return> retrn;
407         boost::shared_ptr<PortInsert> port_insert;
408
409         if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
410                 PortInsertUI *io_selector = reinterpret_cast<PortInsertUI *> (port_insert->get_gui());
411                 port_insert->set_gui (0);
412                 delete io_selector;
413         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
414                 SendUIWindow *sui = reinterpret_cast<SendUIWindow*> (send->get_gui());
415                 send->set_gui (0);
416                 delete sui;
417         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
418                 ReturnUIWindow *rui = reinterpret_cast<ReturnUIWindow*> (retrn->get_gui());
419                 retrn->set_gui (0);
420                 delete rui;
421         }
422 }
423
424 void
425 ProcessorBox::build_send_action_menu ()
426 {
427         using namespace Menu_Helpers;
428
429         send_action_menu = new Menu;
430         send_action_menu->set_name ("ArdourContextMenu");
431         MenuList& items = send_action_menu->items();
432
433         items.push_back (MenuElem (_("New send"), sigc::mem_fun(*this, &ProcessorBox::new_send)));
434         items.push_back (MenuElem (_("Show send controls"), sigc::mem_fun(*this, &ProcessorBox::show_send_controls)));
435 }
436
437 Gtk::Menu*
438 ProcessorBox::build_possible_aux_menu ()
439 {
440         boost::shared_ptr<RouteList> rl = _session.get_routes_with_internal_returns();
441
442         if (rl->empty()) {
443                 return 0;
444         }
445
446         using namespace Menu_Helpers;
447         Menu* menu = manage (new Menu);
448         MenuList& items = menu->items();
449
450         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
451                 if (!_route->internal_send_for (*r) && *r != _route) {
452                         items.push_back (MenuElem ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
453                 }
454         }
455
456         return menu;
457 }
458
459 void
460 ProcessorBox::show_send_controls ()
461 {
462 }
463
464 void
465 ProcessorBox::new_send ()
466 {
467 }
468
469 void
470 ProcessorBox::show_processor_menu (gint arg)
471 {
472         if (processor_menu == 0) {
473                 processor_menu = build_processor_menu ();
474         }
475
476         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/processormenu/newplugin"));
477
478         if (plugin_menu_item) {
479                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
480         }
481
482         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/processormenu/newaux"));
483
484         if (aux_menu_item) {
485                 Menu* m = build_possible_aux_menu();
486                 if (m && !m->items().empty()) {
487                         aux_menu_item->set_submenu (*m);
488                         aux_menu_item->set_sensitive (true);
489                 } else {
490                         /* stupid gtkmm: we need to pass a null reference here */
491                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
492                         aux_menu_item->set_sensitive (false);
493                 }
494         }
495
496         paste_action->set_sensitive (!_rr_selection.processors.empty());
497
498         processor_menu->popup (1, arg);
499 }
500
501 bool
502 ProcessorBox::enter_notify (GdkEventCrossing*)
503 {
504         _current_processor_box = this;
505         Keyboard::magic_widget_grab_focus ();
506         processor_display.grab_focus ();
507
508         return false;
509 }
510
511 bool
512 ProcessorBox::leave_notify (GdkEventCrossing* ev)
513 {
514         switch (ev->detail) {
515         case GDK_NOTIFY_INFERIOR:
516                 break;
517         default:
518                 Keyboard::magic_widget_drop_focus ();
519         }
520
521         return false;
522 }
523
524 bool
525 ProcessorBox::processor_key_press_event (GdkEventKey *)
526 {
527         /* do real stuff on key release */
528         return false;
529 }
530
531 bool
532 ProcessorBox::processor_key_release_event (GdkEventKey *ev)
533 {
534         bool ret = false;
535         ProcSelection targets;
536
537         get_selected_processors (targets);
538
539         if (targets.empty()) {
540
541                 int x, y;
542                 processor_display.get_pointer (x, y);
543
544                 pair<ProcessorEntry *, int> const pointer = processor_display.get_child_at_position (x, y);
545
546                 if (pointer.first) {
547                         targets.push_back (pointer.first->processor ());
548                 }
549         }
550
551
552         switch (ev->keyval) {
553         case GDK_a:
554                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
555                         processor_display.select_all ();
556                         ret = true;
557                 } 
558                 break;
559
560         case GDK_c:
561                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
562                         copy_processors (targets);
563                         ret = true;
564                 }
565                 break;
566
567         case GDK_x:
568                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
569                         cut_processors (targets);
570                         ret = true;
571                 }
572                 break;
573
574         case GDK_v:
575                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
576                         if (targets.empty()) {
577                                 paste_processors ();
578                         } else {
579                                 paste_processors (targets.front());
580                         }
581                         ret = true;
582                 }
583                 break;
584
585         case GDK_Up:
586                 break;
587
588         case GDK_Down:
589                 break;
590
591         case GDK_Delete:
592         case GDK_BackSpace:
593                 delete_processors (targets);
594                 ret = true;
595                 break;
596
597         case GDK_Return:
598                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
599                         if ((*i)->active()) {
600                                 (*i)->deactivate ();
601                         } else {
602                                 (*i)->activate ();
603                         }
604                 }
605                 ret = true;
606                 break;
607
608         case GDK_slash:
609                 ab_plugins ();
610                 ret = true;
611                 break;
612
613         default:
614                 break;
615         }
616
617         return ret;
618 }
619
620 bool
621 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
622 {
623         boost::shared_ptr<Processor> processor;
624         if (child) {
625                 processor = child->processor ();
626         }
627         
628         int ret = false;
629         bool selected = processor_display.selected (child);
630
631         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
632
633                 if (_session.engine().connected()) {
634                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
635                         edit_processor (processor);
636                 }
637                 ret = true;
638
639         } else if (processor && ev->button == 1 && selected) {
640
641                 // this is purely informational but necessary for route params UI
642                 ProcessorSelected (processor); // emit
643
644         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
645
646                 choose_plugin ();
647                 _get_plugin_selector()->show_manager ();
648         }
649
650         return ret;
651 }
652
653 bool
654 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
655 {
656         boost::shared_ptr<Processor> processor;
657         if (child) {
658                 processor = child->processor ();
659         }
660         
661         int ret = false;
662
663         if (processor && Keyboard::is_delete_event (ev)) {
664
665                 Glib::signal_idle().connect (sigc::bind (
666                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
667                                 boost::weak_ptr<Processor>(processor)));
668                 ret = true;
669
670         } else if (Keyboard::is_context_menu_event (ev)) {
671
672                 /* figure out if we are above or below the fader/amp processor,
673                    and set the next insert position appropriately.
674                 */
675
676                 if (processor) {
677                         if (_route->processor_is_prefader (processor)) {
678                                 _placement = PreFader;
679                         } else {
680                                 _placement = PostFader;
681                         }
682                 } else {
683                         _placement = PostFader;
684                 }
685
686                 show_processor_menu (ev->time);
687                 ret = true;
688
689         } else if (processor && Keyboard::is_button2_event (ev)
690 #ifndef GTKOSX
691                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
692 #endif
693                 ) {
694
695                 /* button2-click with no/appropriate modifiers */
696
697                 if (processor->active()) {
698                         processor->deactivate ();
699                 } else {
700                         processor->activate ();
701                 }
702                 ret = true;
703
704         }
705
706         return false;
707 }
708
709 Menu *
710 ProcessorBox::build_processor_menu ()
711 {
712         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/processormenu") );
713         processor_menu->set_name ("ArdourContextMenu");
714
715         show_all_children();
716
717         return processor_menu;
718 }
719
720 void
721 ProcessorBox::selection_changed ()
722 {
723         bool sensitive = (processor_display.selection().empty()) ? false : true;
724         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
725 }
726
727 void
728 ProcessorBox::select_all_processors ()
729 {
730         processor_display.select_all ();
731 }
732
733 void
734 ProcessorBox::deselect_all_processors ()
735 {
736         processor_display.select_none ();
737 }
738
739 void
740 ProcessorBox::choose_plugin ()
741 {
742         _get_plugin_selector()->set_interested_object (*this);
743 }
744
745 void
746 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
747 {
748         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
749
750                 boost::shared_ptr<Processor> processor (new PluginInsert (_session, *p));
751
752                 Route::ProcessorStreams err_streams;
753
754                 if (Config->get_new_plugins_active()) {
755                         processor->activate ();
756                 }
757
758                 if (_route->add_processor (processor, _placement, &err_streams)) {
759                         weird_plugin_dialog (**p, err_streams);
760                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
761                 } else {
762
763                         if (Profile->get_sae()) {
764                                 processor->activate ();
765                         }
766                 }
767         }
768 }
769
770 void
771 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
772 {
773         ArdourDialog dialog (_("ardour: weird plugin dialog"));
774         Label label;
775
776         string text = string_compose(_("You attempted to add the plugin \"%1\" at index %2.\n"),
777                         p.name(), streams.index);
778
779         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
780         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
781
782         text += _("\nThis plugin has:\n");
783         if (has_midi) {
784                 text += string_compose("\t%1 ", p.get_info()->n_inputs.n_midi()) + _("MIDI input(s)\n");
785         }
786         if (has_audio) {
787                 text += string_compose("\t%1 ", p.get_info()->n_inputs.n_audio()) + _("audio input(s)\n");
788         }
789
790         text += _("\nBut at the insertion point, there are:\n");
791         if (has_midi) {
792                 text += string_compose("\t%1 ", streams.count.n_midi()) + _("MIDI channel(s)\n");
793         }
794         if (has_audio) {
795                 text += string_compose("\t%1 ", streams.count.n_audio()) + _("audio channel(s)\n");
796         }
797
798         text += _("\nArdour is unable to insert this plugin here.\n");
799         label.set_text(text);
800
801         dialog.get_vbox()->pack_start (label);
802         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
803
804         dialog.set_name (X_("PluginIODialog"));
805         dialog.set_position (Gtk::WIN_POS_MOUSE);
806         dialog.set_modal (true);
807         dialog.show_all ();
808
809         dialog.run ();
810 }
811
812 void
813 ProcessorBox::choose_insert ()
814 {
815         boost::shared_ptr<Processor> processor (new PortInsert (_session, _route->mute_master()));
816         _route->add_processor (processor, _placement);
817 }
818
819 void
820 ProcessorBox::choose_send ()
821 {
822         boost::shared_ptr<Send> send (new Send (_session, _route->mute_master()));
823
824         /* make an educated guess at the initial number of outputs for the send */
825         ChanCount outs = (_session.master_out())
826                         ? _session.master_out()->n_outputs()
827                         : _route->n_outputs();
828
829         /* XXX need processor lock on route */
830         try {
831                 send->output()->ensure_io (outs, false, this);
832         } catch (AudioEngine::PortRegistrationFailure& err) {
833                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
834                 return;
835         }
836
837         /* let the user adjust the IO setup before creation.
838
839            Note: this dialog is NOT modal - we just leave it to run and it will
840            return when its Finished signal is emitted - typically when the window
841            is closed.
842          */
843
844         IOSelectorWindow *ios = new IOSelectorWindow (&_session, send->output(), true);
845         ios->show_all ();
846
847         /* keep a reference to the send so it doesn't get deleted while
848            the IOSelectorWindow is doing its stuff
849         */
850         _processor_being_created = send;
851
852         ios->selector().Finished.connect (sigc::bind (
853                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
854                         boost::weak_ptr<Processor>(send), ios));
855
856 }
857
858 void
859 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
860 {
861         boost::shared_ptr<Processor> processor (weak_processor.lock());
862
863         /* drop our temporary reference to the new send */
864         _processor_being_created.reset ();
865
866         if (!processor) {
867                 return;
868         }
869
870         switch (r) {
871         case IOSelector::Cancelled:
872                 // processor will go away when all shared_ptrs to it vanish
873                 break;
874
875         case IOSelector::Accepted:
876                 _route->add_processor (processor, _placement);
877                 if (Profile->get_sae()) {
878                         processor->activate ();
879                 }
880                 break;
881         }
882
883         delete_when_idle (ios);
884 }
885
886 void
887 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
888 {
889         boost::shared_ptr<Processor> processor (weak_processor.lock());
890
891         /* drop our temporary reference to the new return */
892         _processor_being_created.reset ();
893
894         if (!processor) {
895                 return;
896         }
897
898         switch (r) {
899         case IOSelector::Cancelled:
900                 // processor will go away when all shared_ptrs to it vanish
901                 break;
902
903         case IOSelector::Accepted:
904                 _route->add_processor (processor, _placement);
905                 if (Profile->get_sae()) {
906                         processor->activate ();
907                 }
908                 break;
909         }
910
911         delete_when_idle (ios);
912 }
913
914 void
915 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
916 {
917         if (!_route) {
918                 return;
919         }
920
921         boost::shared_ptr<Route> target = wr.lock();
922
923         if (!target) {
924                 return;
925         }
926
927         boost::shared_ptr<RouteList> rlist (new RouteList);
928         rlist->push_back (_route);
929
930         _session.add_internal_sends (target, PreFader, rlist);
931 }
932
933 void
934 ProcessorBox::route_processors_changed (RouteProcessorChange c)
935 {
936         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
937                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
938                 return;
939         }
940
941         redisplay_processors ();
942 }
943
944 void
945 ProcessorBox::redisplay_processors ()
946 {
947         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors)
948
949         if (no_processor_redisplay) {
950                 return;
951         }
952
953         processor_display.clear ();
954
955         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
956
957         build_processor_tooltip (processor_eventbox, _("Inserts, sends & plugins:"));
958 }
959
960 void
961 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
962 {
963         boost::shared_ptr<Processor> processor (p.lock ());
964
965         if (!processor || !processor->display_to_user()) {
966                 return;
967         }
968
969         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
970         ProcessorEntry* e = 0;
971         if (send) {
972                 e = new SendProcessorEntry (send, _width);
973         } else {
974                 e = new ProcessorEntry (processor, _width);
975         }
976         e->set_pixel_width (get_allocation().get_width());
977         processor_display.add_child (e);
978 }
979
980
981 void
982 ProcessorBox::build_processor_tooltip (EventBox& box, string start)
983 {
984         string tip(start);
985
986         list<ProcessorEntry*> children = processor_display.children ();
987         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
988                 tip += '\n';
989                 tip += (*i)->processor()->name();
990         }
991         
992         ARDOUR_UI::instance()->tooltips().set_tip (box, tip);
993 }
994
995 void
996 ProcessorBox::reordered ()
997 {
998         compute_processor_sort_keys ();
999 }
1000
1001 void
1002 ProcessorBox::compute_processor_sort_keys ()
1003 {
1004         list<ProcessorEntry*> children = processor_display.children ();
1005         Route::ProcessorList our_processors;
1006
1007         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
1008                 our_processors.push_back ((*iter)->processor ());
1009         }
1010
1011         if (_route->reorder_processors (our_processors)) {
1012
1013                 /* reorder failed, so redisplay */
1014
1015                 redisplay_processors ();
1016
1017                 /* now tell them about the problem */
1018
1019                 ArdourDialog dialog (_("ardour: weird plugin dialog"));
1020                 Label label;
1021
1022                 label.set_text (_("\
1023 You cannot reorder this set of processors\n\
1024 in that way because the inputs and\n\
1025 outputs do not work correctly."));
1026
1027                 dialog.get_vbox()->pack_start (label);
1028                 dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1029
1030                 dialog.set_name (X_("PluginIODialog"));
1031                 dialog.set_position (Gtk::WIN_POS_MOUSE);
1032                 dialog.set_modal (true);
1033                 dialog.show_all ();
1034
1035                 dialog.run ();
1036         }
1037 }
1038
1039 void
1040 ProcessorBox::rename_processors ()
1041 {
1042         ProcSelection to_be_renamed;
1043
1044         get_selected_processors (to_be_renamed);
1045
1046         if (to_be_renamed.empty()) {
1047                 return;
1048         }
1049
1050         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1051                 rename_processor (*i);
1052         }
1053 }
1054
1055 void
1056 ProcessorBox::cut_processors ()
1057 {
1058         ProcSelection to_be_removed;
1059
1060         get_selected_processors (to_be_removed);
1061 }
1062
1063 void
1064 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1065 {
1066         if (to_be_removed.empty()) {
1067                 return;
1068         }
1069
1070         XMLNode* node = new XMLNode (X_("cut"));
1071         Route::ProcessorList to_cut;
1072
1073         no_processor_redisplay = true;
1074         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1075                 // Cut only plugins, sends and returns
1076                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1077                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1078                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1079
1080                         void* gui = (*i)->get_gui ();
1081
1082                         if (gui) {
1083                                 static_cast<Gtk::Widget*>(gui)->hide ();
1084                         }
1085
1086                         XMLNode& child ((*i)->get_state());
1087                         node->add_child_nocopy (child);
1088                         to_cut.push_back (*i);
1089                 }
1090         }
1091
1092         if (_route->remove_processors (to_cut) != 0) {
1093                 delete node;
1094                 no_processor_redisplay = false;
1095                 return;
1096         }
1097
1098         _rr_selection.set (node);
1099
1100         no_processor_redisplay = false;
1101         redisplay_processors ();
1102 }
1103
1104 void
1105 ProcessorBox::copy_processors ()
1106 {
1107         ProcSelection to_be_copied;
1108         get_selected_processors (to_be_copied);
1109         copy_processors (to_be_copied);
1110 }
1111
1112 void
1113 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1114 {
1115         if (to_be_copied.empty()) {
1116                 return;
1117         }
1118
1119         XMLNode* node = new XMLNode (X_("copy"));
1120
1121         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1122                 // Copy only plugins, sends, returns
1123                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1124                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1125                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1126                         node->add_child_nocopy ((*i)->get_state());
1127                 }
1128         }
1129
1130         _rr_selection.set (node);
1131 }
1132
1133 void
1134 ProcessorBox::delete_processors ()
1135 {
1136         ProcSelection to_be_deleted;
1137         get_selected_processors (to_be_deleted);
1138         delete_processors (to_be_deleted);
1139 }
1140
1141 void
1142 ProcessorBox::delete_processors (const ProcSelection& targets)
1143 {
1144         if (targets.empty()) {
1145                 return;
1146         }
1147
1148         no_processor_redisplay = true;
1149
1150         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1151
1152                 void* gui = (*i)->get_gui ();
1153
1154                 if (gui) {
1155                         static_cast<Gtk::Widget*>(gui)->hide ();
1156                 }
1157
1158                 _route->remove_processor(*i);
1159         }
1160
1161         no_processor_redisplay = false;
1162         redisplay_processors ();
1163 }
1164
1165 void
1166 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1167 {
1168         list<boost::shared_ptr<Processor> >::const_iterator x;
1169
1170         no_processor_redisplay = true;
1171         for (x = procs.begin(); x != procs.end(); ++x) {
1172
1173                 void* gui = (*x)->get_gui ();
1174
1175                 if (gui) {
1176                         static_cast<Gtk::Widget*>(gui)->hide ();
1177                 }
1178
1179                 _route->remove_processor(*x);
1180         }
1181
1182         no_processor_redisplay = false;
1183         redisplay_processors ();
1184 }
1185
1186 gint
1187 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1188 {
1189         boost::shared_ptr<Processor> processor (weak_processor.lock());
1190
1191         if (!processor) {
1192                 return false;
1193         }
1194
1195         /* NOT copied to _mixer.selection() */
1196
1197         no_processor_redisplay = true;
1198         _route->remove_processor (processor);
1199         no_processor_redisplay = false;
1200         redisplay_processors ();
1201
1202         return false;
1203 }
1204
1205 void
1206 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1207 {
1208         ArdourPrompter name_prompter (true);
1209         string result;
1210         name_prompter.set_title (_("Rename Processor"));
1211         name_prompter.set_prompt (_("New name:"));
1212         name_prompter.set_initial_text (processor->name());
1213         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1214         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1215         name_prompter.show_all ();
1216
1217         switch (name_prompter.run ()) {
1218
1219         case Gtk::RESPONSE_ACCEPT:
1220                 name_prompter.get_result (result);
1221                 if (result.length()) {
1222                         if (_session.route_by_name (result)) {
1223                                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name."));
1224                                 return;
1225                         }
1226                         processor->set_name (result);
1227                 }
1228                 break;
1229         }
1230
1231         return;
1232 }
1233
1234 void
1235 ProcessorBox::paste_processors ()
1236 {
1237         if (_rr_selection.processors.empty()) {
1238                 return;
1239         }
1240
1241         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1242 }
1243
1244 void
1245 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1246 {
1247
1248         if (_rr_selection.processors.empty()) {
1249                 return;
1250         }
1251
1252         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1253 }
1254
1255 void
1256 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1257 {
1258         XMLNodeConstIterator niter;
1259         list<boost::shared_ptr<Processor> > copies;
1260
1261         if (nlist.empty()) {
1262                 return;
1263         }
1264
1265         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1266
1267                 XMLProperty const * type = (*niter)->property ("type");
1268                 assert (type);
1269
1270                 boost::shared_ptr<Processor> p;
1271                 try {
1272                         if (type->value() == "meter" ||
1273                             type->value() == "main-outs" ||
1274                             type->value() == "amp" ||
1275                             type->value() == "intsend" || type->value() == "intreturn") {
1276                                 /* do not paste meter, main outs, amp or internal send/returns */
1277                                 continue;
1278
1279                         } else if (type->value() == "send") {
1280
1281                                 XMLNode n (**niter);
1282                                 Send::make_unique (n, _session);
1283                                 p.reset (new Send (_session, _route->mute_master(), n));
1284
1285                         } else if (type->value() == "return") {
1286
1287                                 XMLNode n (**niter);
1288                                 Return::make_unique (n, _session);
1289                                 p.reset (new Return (_session, **niter));
1290
1291                         } else {
1292                                 /* XXX its a bit limiting to assume that everything else
1293                                    is a plugin.
1294                                 */
1295                                 p.reset (new PluginInsert (_session, **niter));
1296                         }
1297
1298                         copies.push_back (p);
1299                 }
1300
1301                 catch (...) {
1302                         cerr << "plugin insert constructor failed\n";
1303                 }
1304         }
1305
1306         if (copies.empty()) {
1307                 return;
1308         }
1309
1310         if (_route->add_processors (copies, p)) {
1311
1312                 string msg = _(
1313                         "Copying the set of processors on the clipboard failed,\n\
1314 probably because the I/O configuration of the plugins\n\
1315 could not match the configuration of this track.");
1316                 MessageDialog am (msg);
1317                 am.run ();
1318         }
1319 }
1320
1321 void
1322 ProcessorBox::activate_processor (boost::shared_ptr<Processor> r)
1323 {
1324         r->activate ();
1325 }
1326
1327 void
1328 ProcessorBox::deactivate_processor (boost::shared_ptr<Processor> r)
1329 {
1330         r->deactivate ();
1331 }
1332
1333 void
1334 ProcessorBox::get_selected_processors (ProcSelection& processors)
1335 {
1336         list<ProcessorEntry*> selection = processor_display.selection ();
1337         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1338                 processors.push_back ((*i)->processor ());
1339         }
1340 }
1341
1342 void
1343 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1344 {
1345         list<ProcessorEntry*> selection = processor_display.selection ();
1346         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1347                 (this->*method) ((*i)->processor ());
1348         }
1349 }
1350
1351 void
1352 ProcessorBox::all_processors_active (bool state)
1353 {
1354         _route->all_processors_active (_placement, state);
1355 }
1356
1357 void
1358 ProcessorBox::ab_plugins ()
1359 {
1360         _route->ab_plugins (ab_direction);
1361         ab_direction = !ab_direction;
1362 }
1363
1364
1365 void
1366 ProcessorBox::clear_processors ()
1367 {
1368         string prompt;
1369         vector<string> choices;
1370
1371         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1372                                    "(this cannot be undone)"), _route->name());
1373
1374         choices.push_back (_("Cancel"));
1375         choices.push_back (_("Yes, remove them all"));
1376
1377         Gtkmm2ext::Choice prompter (prompt, choices);
1378
1379         if (prompter.run () == 1) {
1380                 _route->clear_processors (PreFader);
1381                 _route->clear_processors (PostFader);
1382         }
1383 }
1384
1385 void
1386 ProcessorBox::clear_processors (Placement p)
1387 {
1388         string prompt;
1389         vector<string> choices;
1390
1391         if (p == PreFader) {
1392                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1393                                            "(this cannot be undone)"), _route->name());
1394         } else {
1395                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1396                                            "(this cannot be undone)"), _route->name());
1397         }
1398
1399         choices.push_back (_("Cancel"));
1400         choices.push_back (_("Yes, remove them all"));
1401
1402         Gtkmm2ext::Choice prompter (prompt, choices);
1403
1404         if (prompter.run () == 1) {
1405                 _route->clear_processors (p);
1406         }
1407 }
1408
1409 void
1410 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
1411 {
1412         boost::shared_ptr<Send> send;
1413         boost::shared_ptr<Return> retrn;
1414         boost::shared_ptr<PluginInsert> plugin_insert;
1415         boost::shared_ptr<PortInsert> port_insert;
1416         Window* gidget = 0;
1417
1418         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1419
1420                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1421                         return;
1422                 }
1423         }
1424
1425         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1426
1427                 if (!_session.engine().connected()) {
1428                         return;
1429                 }
1430
1431 #ifdef OLD_SEND_EDITING
1432                 SendUIWindow *send_ui;
1433
1434                 if (send->get_gui() == 0) {
1435                         send_ui = new SendUIWindow (send, _session);
1436                         send_ui->set_title (send->name());
1437                         send->set_gui (send_ui);
1438
1439                 } else {
1440                         send_ui = reinterpret_cast<SendUIWindow *> (send->get_gui());
1441                 }
1442
1443                 gidget = send_ui;
1444 #else
1445                 if (_parent_strip) {
1446                         _parent_strip->show_send (send);
1447                 }
1448 #endif
1449
1450         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
1451
1452                 if (!_session.engine().connected()) {
1453                         return;
1454                 }
1455
1456                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
1457
1458                 ReturnUIWindow *return_ui;
1459
1460                 if (retrn->get_gui() == 0) {
1461
1462                         return_ui = new ReturnUIWindow (retrn, _session);
1463                         return_ui->set_title (retrn->name ());
1464                         send->set_gui (return_ui);
1465
1466                 } else {
1467                         return_ui = reinterpret_cast<ReturnUIWindow *> (retrn->get_gui());
1468                 }
1469
1470                 gidget = return_ui;
1471
1472         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1473
1474                 PluginUIWindow *plugin_ui;
1475
1476                 /* these are both allowed to be null */
1477
1478                 Container* toplevel = get_toplevel();
1479                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1480
1481                 if (plugin_insert->get_gui() == 0) {
1482
1483                         plugin_ui = new PluginUIWindow (win, plugin_insert);
1484                         plugin_ui->set_title (generate_processor_title (plugin_insert));
1485                         plugin_insert->set_gui (plugin_ui);
1486
1487                 } else {
1488                         plugin_ui = reinterpret_cast<PluginUIWindow *> (plugin_insert->get_gui());
1489                         plugin_ui->set_parent (win);
1490                 }
1491
1492                 gidget = plugin_ui;
1493
1494         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
1495
1496                 if (!_session.engine().connected()) {
1497                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1498                         msg.run ();
1499                         return;
1500                 }
1501
1502                 PortInsertWindow *io_selector;
1503
1504                 if (port_insert->get_gui() == 0) {
1505                         io_selector = new PortInsertWindow (&_session, port_insert);
1506                         port_insert->set_gui (io_selector);
1507
1508                 } else {
1509                         io_selector = reinterpret_cast<PortInsertWindow *> (port_insert->get_gui());
1510                 }
1511
1512                 gidget = io_selector;
1513         }
1514
1515         if (gidget) {
1516                 if (gidget->is_visible()) {
1517                         gidget->get_window()->raise ();
1518                 } else {
1519                         gidget->show_all ();
1520                         gidget->present ();
1521                 }
1522         }
1523 }
1524
1525 void
1526 ProcessorBox::register_actions ()
1527 {
1528         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("processormenu"));
1529         Glib::RefPtr<Action> act;
1530
1531         /* new stuff */
1532         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
1533                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
1534
1535         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
1536                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
1537         ActionManager::jack_sensitive_actions.push_back (act);
1538         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New Send ..."),
1539                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
1540         ActionManager::jack_sensitive_actions.push_back (act);
1541
1542         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
1543
1544         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
1545                         sigc::ptr_fun (ProcessorBox::rb_clear));
1546         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
1547                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
1548         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
1549                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
1550
1551         /* standard editing stuff */
1552         act = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
1553                         sigc::ptr_fun (ProcessorBox::rb_cut));
1554         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1555         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
1556                         sigc::ptr_fun (ProcessorBox::rb_copy));
1557         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1558
1559         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
1560                         sigc::ptr_fun (ProcessorBox::rb_delete));
1561         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1562
1563         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
1564                         sigc::ptr_fun (ProcessorBox::rb_paste));
1565         act = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
1566                         sigc::ptr_fun (ProcessorBox::rb_rename));
1567         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1568         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
1569                         sigc::ptr_fun (ProcessorBox::rb_select_all));
1570         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
1571                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
1572
1573         /* activation etc. */
1574
1575         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate all"),
1576                                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
1577         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate all"),
1578                                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
1579         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
1580                                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
1581
1582         /* show editors */
1583         act = ActionManager::register_action (popup_act_grp, X_("edit"), _("Edit"),
1584                                               sigc::ptr_fun (ProcessorBox::rb_edit));
1585         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1586
1587         ActionManager::add_action_group (popup_act_grp);
1588 }
1589
1590 void
1591 ProcessorBox::rb_ab_plugins ()
1592 {
1593         if (_current_processor_box == 0) {
1594                 return;
1595         }
1596
1597         _current_processor_box->ab_plugins ();
1598 }
1599
1600 void
1601 ProcessorBox::rb_choose_plugin ()
1602 {
1603         if (_current_processor_box == 0) {
1604                 return;
1605         }
1606         _current_processor_box->choose_plugin ();
1607 }
1608
1609 void
1610 ProcessorBox::rb_choose_insert ()
1611 {
1612         if (_current_processor_box == 0) {
1613                 return;
1614         }
1615         _current_processor_box->choose_insert ();
1616 }
1617
1618 void
1619 ProcessorBox::rb_choose_send ()
1620 {
1621         if (_current_processor_box == 0) {
1622                 return;
1623         }
1624         _current_processor_box->choose_send ();
1625 }
1626
1627 void
1628 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
1629 {
1630         if (_current_processor_box == 0) {
1631                 return;
1632         }
1633
1634         _current_processor_box->choose_aux (wr);
1635 }
1636
1637 void
1638 ProcessorBox::rb_clear ()
1639 {
1640         if (_current_processor_box == 0) {
1641                 return;
1642         }
1643
1644         _current_processor_box->clear_processors ();
1645 }
1646
1647
1648 void
1649 ProcessorBox::rb_clear_pre ()
1650 {
1651         if (_current_processor_box == 0) {
1652                 return;
1653         }
1654
1655         _current_processor_box->clear_processors (PreFader);
1656 }
1657
1658
1659 void
1660 ProcessorBox::rb_clear_post ()
1661 {
1662         if (_current_processor_box == 0) {
1663                 return;
1664         }
1665
1666         _current_processor_box->clear_processors (PostFader);
1667 }
1668
1669 void
1670 ProcessorBox::rb_cut ()
1671 {
1672         if (_current_processor_box == 0) {
1673                 return;
1674         }
1675
1676         _current_processor_box->cut_processors ();
1677 }
1678
1679 void
1680 ProcessorBox::rb_delete ()
1681 {
1682         if (_current_processor_box == 0) {
1683                 return;
1684         }
1685
1686         _current_processor_box->delete_processors ();
1687 }
1688
1689 void
1690 ProcessorBox::rb_copy ()
1691 {
1692         if (_current_processor_box == 0) {
1693                 return;
1694         }
1695         _current_processor_box->copy_processors ();
1696 }
1697
1698 void
1699 ProcessorBox::rb_paste ()
1700 {
1701         if (_current_processor_box == 0) {
1702                 return;
1703         }
1704
1705         _current_processor_box->paste_processors ();
1706 }
1707
1708 void
1709 ProcessorBox::rb_rename ()
1710 {
1711         if (_current_processor_box == 0) {
1712                 return;
1713         }
1714         _current_processor_box->rename_processors ();
1715 }
1716
1717 void
1718 ProcessorBox::rb_select_all ()
1719 {
1720         if (_current_processor_box == 0) {
1721                 return;
1722         }
1723
1724         _current_processor_box->select_all_processors ();
1725 }
1726
1727 void
1728 ProcessorBox::rb_deselect_all ()
1729 {
1730         if (_current_processor_box == 0) {
1731                 return;
1732         }
1733
1734         _current_processor_box->deselect_all_processors ();
1735 }
1736
1737 void
1738 ProcessorBox::rb_activate_all ()
1739 {
1740         if (_current_processor_box == 0) {
1741                 return;
1742         }
1743
1744         _current_processor_box->all_processors_active (true);
1745 }
1746
1747 void
1748 ProcessorBox::rb_deactivate_all ()
1749 {
1750         if (_current_processor_box == 0) {
1751                 return;
1752         }
1753         _current_processor_box->all_processors_active (false);
1754 }
1755
1756 void
1757 ProcessorBox::rb_edit ()
1758 {
1759         if (_current_processor_box == 0) {
1760                 return;
1761         }
1762
1763         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
1764 }
1765
1766 void
1767 ProcessorBox::route_name_changed ()
1768 {
1769         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_name_changed)
1770
1771         boost::shared_ptr<Processor> processor;
1772         boost::shared_ptr<PluginInsert> plugin_insert;
1773         boost::shared_ptr<Send> send;
1774
1775         list<ProcessorEntry*> children = processor_display.children();
1776
1777         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
1778
1779                 processor = (*iter)->processor ();
1780
1781                 void* gui = processor->get_gui();
1782
1783                 if (!gui) {
1784                         continue;
1785                 }
1786
1787                 /* rename editor windows for sends and plugins */
1788
1789                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1790                         static_cast<Window*>(gui)->set_title (send->name ());
1791                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1792                         static_cast<Window*>(gui)->set_title (generate_processor_title (plugin_insert));
1793                 }
1794         }
1795 }
1796
1797 string
1798 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
1799 {
1800         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
1801         string::size_type email_pos;
1802
1803         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1804                 maker = maker.substr (0, email_pos - 1);
1805         }
1806
1807         if (maker.length() > 32) {
1808                 maker = maker.substr (0, 32);
1809                 maker += " ...";
1810         }
1811
1812         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
1813 }
1814
1815 void
1816 ProcessorBox::on_size_allocate (Allocation& a)
1817 {
1818         HBox::on_size_allocate (a);
1819
1820         list<ProcessorEntry*> children = processor_display.children ();
1821         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1822                 (*i)->set_pixel_width (a.get_width ());
1823         }
1824 }