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