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