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