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