Use the channel selector to decide which channel to add program changes to.
[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                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
771                 text += string_compose (ngettext ("\t%1 MIDI input", "\t%1 MIDI inputs", n), n);
772         }
773         if (has_audio) {
774                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
775                 text += string_compose (ngettext ("\t%1 audio input", "\t%1 audio inputs", n), n);
776         }
777
778         text += _("\nBut at the insertion point, there are:\n");
779         if (has_midi) {
780                 uint32_t const n = streams.count.n_midi ();
781                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
782         }
783         if (has_audio) {
784                 uint32_t const n = streams.count.n_audio ();
785                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
786         }
787
788         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
789         label.set_text(text);
790
791         dialog.get_vbox()->pack_start (label);
792         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
793
794         dialog.set_name (X_("PluginIODialog"));
795         dialog.set_position (Gtk::WIN_POS_MOUSE);
796         dialog.set_modal (true);
797         dialog.show_all ();
798
799         dialog.run ();
800 }
801
802 void
803 ProcessorBox::choose_insert ()
804 {
805         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->mute_master()));
806         _route->add_processor (processor, _placement);
807 }
808
809 /* Caller must not hold process lock */
810 void
811 ProcessorBox::choose_send ()
812 {
813         boost::shared_ptr<Send> send (new Send (*_session, _route->mute_master()));
814
815         /* make an educated guess at the initial number of outputs for the send */
816         ChanCount outs = (_session->master_out())
817                         ? _session->master_out()->n_outputs()
818                         : _route->n_outputs();
819
820         /* XXX need processor lock on route */
821         try {
822                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
823                 send->output()->ensure_io (outs, false, this);
824         } catch (AudioEngine::PortRegistrationFailure& err) {
825                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
826                 return;
827         }
828
829         /* let the user adjust the IO setup before creation.
830
831            Note: this dialog is NOT modal - we just leave it to run and it will
832            return when its Finished signal is emitted - typically when the window
833            is closed.
834          */
835
836         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
837         ios->show_all ();
838
839         /* keep a reference to the send so it doesn't get deleted while
840            the IOSelectorWindow is doing its stuff
841         */
842         _processor_being_created = send;
843
844         ios->selector().Finished.connect (sigc::bind (
845                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
846                         boost::weak_ptr<Processor>(send), ios));
847
848 }
849
850 void
851 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
852 {
853         boost::shared_ptr<Processor> processor (weak_processor.lock());
854
855         /* drop our temporary reference to the new send */
856         _processor_being_created.reset ();
857
858         if (!processor) {
859                 return;
860         }
861
862         switch (r) {
863         case IOSelector::Cancelled:
864                 // processor will go away when all shared_ptrs to it vanish
865                 break;
866
867         case IOSelector::Accepted:
868                 _route->add_processor (processor, _placement);
869                 if (Profile->get_sae()) {
870                         processor->activate ();
871                 }
872                 break;
873         }
874
875         delete_when_idle (ios);
876 }
877
878 void
879 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
880 {
881         boost::shared_ptr<Processor> processor (weak_processor.lock());
882
883         /* drop our temporary reference to the new return */
884         _processor_being_created.reset ();
885
886         if (!processor) {
887                 return;
888         }
889
890         switch (r) {
891         case IOSelector::Cancelled:
892                 // processor will go away when all shared_ptrs to it vanish
893                 break;
894
895         case IOSelector::Accepted:
896                 _route->add_processor (processor, _placement);
897                 if (Profile->get_sae()) {
898                         processor->activate ();
899                 }
900                 break;
901         }
902
903         delete_when_idle (ios);
904 }
905
906 void
907 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
908 {
909         if (!_route) {
910                 return;
911         }
912
913         boost::shared_ptr<Route> target = wr.lock();
914
915         if (!target) {
916                 return;
917         }
918
919         boost::shared_ptr<RouteList> rlist (new RouteList);
920         rlist->push_back (_route);
921
922         _session->add_internal_sends (target, PreFader, rlist);
923 }
924
925 void
926 ProcessorBox::route_processors_changed (RouteProcessorChange c)
927 {
928         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
929                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
930                 return;
931         }
932
933         redisplay_processors ();
934 }
935
936 void
937 ProcessorBox::redisplay_processors ()
938 {
939         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors)
940
941         if (no_processor_redisplay) {
942                 return;
943         }
944
945         processor_display.clear ();
946
947         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
948
949         build_processor_tooltip (processor_eventbox, _("Inserts, sends & plugins:"));
950
951         for (list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin(); i != _processor_window_proxies.end(); ++i) {
952                 (*i)->marked = false;
953         }
954                 
955         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
956
957         /* trim dead wood from the processor window proxy list */
958
959         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin();
960         while (i != _processor_window_proxies.end()) {
961                 list<ProcessorWindowProxy*>::iterator j = i;
962                 ++j;
963
964                 if (!(*i)->marked) {
965                         ARDOUR_UI::instance()->remove_window_proxy (*i);
966                         delete *i;
967                         _processor_window_proxies.erase (i);
968                 }
969
970                 i = j;
971         }
972 }
973
974 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
975  *  not already have one.
976  */
977 void
978 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
979 {
980         boost::shared_ptr<Processor> p = w.lock ();
981         if (!p) {
982                 return;
983         }
984
985         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
986         while (i != _processor_window_proxies.end()) {
987
988                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
989                 
990                 if (p == t) {
991                         /* this processor is already on the list; done */
992                         (*i)->marked = true;
993                         return;
994                 }
995
996                 ++i;
997         }
998
999         /* not on the list; add it */
1000
1001         string loc;
1002         if (_parent_strip) {
1003                 if (_parent_strip->mixer_owned()) {
1004                         loc = X_("M");
1005                 } else {
1006                         loc = X_("R");
1007                 }
1008         } else {
1009                 loc = X_("P");
1010         }
1011         
1012         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1013                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1014                 _session->extra_xml (X_("UI")),
1015                 this,
1016                 w);
1017         
1018         wp->marked = true;
1019         _processor_window_proxies.push_back (wp);
1020         ARDOUR_UI::instance()->add_window_proxy (wp);
1021 }
1022
1023 void
1024 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1025 {
1026         boost::shared_ptr<Processor> processor (p.lock ());
1027
1028         if (!processor || !processor->display_to_user()) {
1029                 return;
1030         }
1031
1032         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
1033         ProcessorEntry* e = 0;
1034         if (send) {
1035                 e = new SendProcessorEntry (send, _width);
1036         } else {
1037                 e = new ProcessorEntry (processor, _width);
1038         }
1039         e->set_pixel_width (get_allocation().get_width());
1040         processor_display.add_child (e);
1041 }
1042
1043
1044 void
1045 ProcessorBox::build_processor_tooltip (EventBox& box, string start)
1046 {
1047         string tip(start);
1048
1049         list<ProcessorEntry*> children = processor_display.children ();
1050         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1051                 tip += '\n';
1052                 tip += (*i)->processor()->name();
1053         }
1054         
1055         ARDOUR_UI::instance()->set_tip (box, tip);
1056 }
1057
1058 void
1059 ProcessorBox::reordered ()
1060 {
1061         compute_processor_sort_keys ();
1062 }
1063
1064 void
1065 ProcessorBox::compute_processor_sort_keys ()
1066 {
1067         list<ProcessorEntry*> children = processor_display.children ();
1068         Route::ProcessorList our_processors;
1069
1070         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
1071                 our_processors.push_back ((*iter)->processor ());
1072         }
1073
1074         if (_route->reorder_processors (our_processors)) {
1075                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1076                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1077                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1078                    when the drag is torn down after this handler function is finished.
1079                 */
1080                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1081         }
1082 }
1083
1084 void
1085 ProcessorBox::report_failed_reorder ()
1086 {
1087         /* reorder failed, so redisplay */
1088         
1089         redisplay_processors ();
1090         
1091         /* now tell them about the problem */
1092         
1093         ArdourDialog dialog (_("Plugin Incompatibility"));
1094         Label label;
1095         
1096         label.set_text (_("\
1097 You cannot reorder these plugins/sends/inserts\n\
1098 in that way because the inputs and\n\
1099 outputs will not work correctly."));
1100
1101         dialog.get_vbox()->set_border_width (12);
1102         dialog.get_vbox()->pack_start (label);
1103         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1104         
1105         dialog.set_name (X_("PluginIODialog"));
1106         dialog.set_position (Gtk::WIN_POS_MOUSE);
1107         dialog.set_modal (true);
1108         dialog.show_all ();
1109         
1110         dialog.run ();
1111 }
1112
1113 void
1114 ProcessorBox::rename_processors ()
1115 {
1116         ProcSelection to_be_renamed;
1117
1118         get_selected_processors (to_be_renamed);
1119
1120         if (to_be_renamed.empty()) {
1121                 return;
1122         }
1123
1124         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1125                 rename_processor (*i);
1126         }
1127 }
1128
1129 bool
1130 ProcessorBox::can_cut () const
1131 {
1132         vector<boost::shared_ptr<Processor> > sel;
1133
1134         get_selected_processors (sel);
1135         
1136         /* cut_processors () does not cut inserts */
1137
1138         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1139                 
1140                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1141                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1142                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1143                         return true;
1144                 }
1145         }
1146         
1147         return false;
1148 }
1149
1150 void
1151 ProcessorBox::cut_processors ()
1152 {
1153         ProcSelection to_be_removed;
1154
1155         get_selected_processors (to_be_removed);
1156 }
1157
1158 void
1159 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1160 {
1161         if (to_be_removed.empty()) {
1162                 return;
1163         }
1164
1165         XMLNode* node = new XMLNode (X_("cut"));
1166         Route::ProcessorList to_cut;
1167
1168         no_processor_redisplay = true;
1169         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1170                 // Cut only plugins, sends and returns
1171                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1172                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1173                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1174
1175                         Window* w = get_processor_ui (*i);
1176
1177                         if (w) {
1178                                 w->hide ();
1179                         }
1180
1181                         XMLNode& child ((*i)->get_state());
1182                         node->add_child_nocopy (child);
1183                         to_cut.push_back (*i);
1184                 }
1185         }
1186
1187         if (_route->remove_processors (to_cut) != 0) {
1188                 delete node;
1189                 no_processor_redisplay = false;
1190                 return;
1191         }
1192
1193         _rr_selection.set (node);
1194
1195         no_processor_redisplay = false;
1196         redisplay_processors ();
1197 }
1198
1199 void
1200 ProcessorBox::copy_processors ()
1201 {
1202         ProcSelection to_be_copied;
1203         get_selected_processors (to_be_copied);
1204         copy_processors (to_be_copied);
1205 }
1206
1207 void
1208 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1209 {
1210         if (to_be_copied.empty()) {
1211                 return;
1212         }
1213
1214         XMLNode* node = new XMLNode (X_("copy"));
1215
1216         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1217                 // Copy only plugins, sends, returns
1218                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1219                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1220                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1221                         node->add_child_nocopy ((*i)->get_state());
1222                 }
1223         }
1224
1225         _rr_selection.set (node);
1226 }
1227
1228 void
1229 ProcessorBox::delete_processors ()
1230 {
1231         ProcSelection to_be_deleted;
1232         get_selected_processors (to_be_deleted);
1233         delete_processors (to_be_deleted);
1234 }
1235
1236 void
1237 ProcessorBox::delete_processors (const ProcSelection& targets)
1238 {
1239         if (targets.empty()) {
1240                 return;
1241         }
1242
1243         no_processor_redisplay = true;
1244
1245         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1246
1247                 Window* w = get_processor_ui (*i);
1248
1249                 if (w) {
1250                         w->hide ();
1251                 }
1252
1253                 _route->remove_processor(*i);
1254         }
1255
1256         no_processor_redisplay = false;
1257         redisplay_processors ();
1258 }
1259
1260 void
1261 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1262 {
1263         list<boost::shared_ptr<Processor> >::const_iterator x;
1264
1265         no_processor_redisplay = true;
1266         for (x = procs.begin(); x != procs.end(); ++x) {
1267
1268                 Window* w = get_processor_ui (*x);
1269
1270                 if (w) {
1271                         w->hide ();
1272                 }
1273
1274                 _route->remove_processor(*x);
1275         }
1276
1277         no_processor_redisplay = false;
1278         redisplay_processors ();
1279 }
1280
1281 gint
1282 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1283 {
1284         boost::shared_ptr<Processor> processor (weak_processor.lock());
1285
1286         if (!processor) {
1287                 return false;
1288         }
1289
1290         /* NOT copied to _mixer.selection() */
1291
1292         no_processor_redisplay = true;
1293         _route->remove_processor (processor);
1294         no_processor_redisplay = false;
1295         redisplay_processors ();
1296
1297         return false;
1298 }
1299
1300 void
1301 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1302 {
1303         ArdourPrompter name_prompter (true);
1304         string result;
1305         name_prompter.set_title (_("Rename Processor"));
1306         name_prompter.set_prompt (_("New name:"));
1307         name_prompter.set_initial_text (processor->name());
1308         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1309         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1310         name_prompter.show_all ();
1311
1312         switch (name_prompter.run ()) {
1313
1314         case Gtk::RESPONSE_ACCEPT:
1315                 name_prompter.get_result (result);
1316                 if (result.length()) {
1317
1318                        int tries = 0;
1319                        string test = result;
1320
1321                        while (tries < 100) {
1322                                if (_session->io_name_is_legal (test)) {
1323                                        result = test;
1324                                        break;
1325                                }
1326                                tries++;
1327
1328                                test = string_compose ("%1-%2", result, tries);
1329                        }
1330
1331                        if (tries < 100) {
1332                                processor->set_name (result);
1333                        } else {
1334                                /* unlikely! */
1335                                ARDOUR_UI::instance()->popup_error
1336                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
1337                        }
1338                 }
1339                 break;
1340         }
1341
1342         return;
1343 }
1344
1345 void
1346 ProcessorBox::paste_processors ()
1347 {
1348         if (_rr_selection.processors.empty()) {
1349                 return;
1350         }
1351
1352         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1353 }
1354
1355 void
1356 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1357 {
1358
1359         if (_rr_selection.processors.empty()) {
1360                 return;
1361         }
1362
1363         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1364 }
1365
1366 void
1367 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1368 {
1369         XMLNodeConstIterator niter;
1370         list<boost::shared_ptr<Processor> > copies;
1371
1372         if (nlist.empty()) {
1373                 return;
1374         }
1375
1376         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1377
1378                 XMLProperty const * type = (*niter)->property ("type");
1379                 assert (type);
1380
1381                 boost::shared_ptr<Processor> p;
1382                 try {
1383                         if (type->value() == "meter" ||
1384                             type->value() == "main-outs" ||
1385                             type->value() == "amp" ||
1386                             type->value() == "intsend" || type->value() == "intreturn") {
1387                                 /* do not paste meter, main outs, amp or internal send/returns */
1388                                 continue;
1389
1390                         } else if (type->value() == "send") {
1391
1392                                 XMLNode n (**niter);
1393                                 Send::make_unique (n, *_session);
1394                                 Send* s = new Send (*_session, _route->mute_master());
1395                                 if (s->set_state (n, Stateful::loading_state_version)) {
1396                                         delete s;
1397                                         return;
1398                                 }
1399
1400                                 p.reset (s);
1401                                         
1402
1403                         } else if (type->value() == "return") {
1404
1405                                 XMLNode n (**niter);
1406                                 Return::make_unique (n, *_session);
1407                                 Return* r = new Return (*_session);
1408
1409                                 if (r->set_state (n, Stateful::loading_state_version)) {
1410                                         delete r;
1411                                         return;
1412                                 }
1413
1414                                 p.reset (r);
1415
1416                         } else {
1417                                 /* XXX its a bit limiting to assume that everything else
1418                                    is a plugin.
1419                                 */
1420
1421                                 p.reset (new PluginInsert (*_session));
1422                                 p->set_state (**niter, Stateful::current_state_version);
1423                         }
1424
1425                         copies.push_back (p);
1426                 }
1427
1428                 catch (...) {
1429                         cerr << "plugin insert constructor failed\n";
1430                 }
1431         }
1432
1433         if (copies.empty()) {
1434                 return;
1435         }
1436
1437         if (_route->add_processors (copies, p)) {
1438
1439                 string msg = _(
1440                         "Copying the set of processors on the clipboard failed,\n\
1441 probably because the I/O configuration of the plugins\n\
1442 could not match the configuration of this track.");
1443                 MessageDialog am (msg);
1444                 am.run ();
1445         }
1446 }
1447
1448 void
1449 ProcessorBox::activate_processor (boost::shared_ptr<Processor> r)
1450 {
1451         r->activate ();
1452 }
1453
1454 void
1455 ProcessorBox::deactivate_processor (boost::shared_ptr<Processor> r)
1456 {
1457         r->deactivate ();
1458 }
1459
1460 void
1461 ProcessorBox::get_selected_processors (ProcSelection& processors) const
1462 {
1463         const list<ProcessorEntry*> selection = processor_display.selection ();
1464         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
1465                 processors.push_back ((*i)->processor ());
1466         }
1467 }
1468
1469 void
1470 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1471 {
1472         list<ProcessorEntry*> selection = processor_display.selection ();
1473         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1474                 (this->*method) ((*i)->processor ());
1475         }
1476 }
1477
1478 void
1479 ProcessorBox::all_processors_active (bool state)
1480 {
1481         _route->all_processors_active (_placement, state);
1482 }
1483
1484 void
1485 ProcessorBox::ab_plugins ()
1486 {
1487         _route->ab_plugins (ab_direction);
1488         ab_direction = !ab_direction;
1489 }
1490
1491
1492 void
1493 ProcessorBox::clear_processors ()
1494 {
1495         string prompt;
1496         vector<string> choices;
1497
1498         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1499                                    "(this cannot be undone)"), _route->name());
1500
1501         choices.push_back (_("Cancel"));
1502         choices.push_back (_("Yes, remove them all"));
1503
1504         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1505
1506         if (prompter.run () == 1) {
1507                 _route->clear_processors (PreFader);
1508                 _route->clear_processors (PostFader);
1509         }
1510 }
1511
1512 void
1513 ProcessorBox::clear_processors (Placement p)
1514 {
1515         string prompt;
1516         vector<string> choices;
1517
1518         if (p == PreFader) {
1519                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1520                                            "(this cannot be undone)"), _route->name());
1521         } else {
1522                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1523                                            "(this cannot be undone)"), _route->name());
1524         }
1525
1526         choices.push_back (_("Cancel"));
1527         choices.push_back (_("Yes, remove them all"));
1528
1529         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1530
1531         if (prompter.run () == 1) {
1532                 _route->clear_processors (p);
1533         }
1534 }
1535
1536 bool
1537 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
1538 {
1539         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
1540         if (at && at->freeze_state() == AudioTrack::Frozen) {
1541                 return false;
1542         }
1543
1544         if (
1545                 boost::dynamic_pointer_cast<Send> (processor) ||
1546                 boost::dynamic_pointer_cast<Return> (processor) ||
1547                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
1548                 boost::dynamic_pointer_cast<PortInsert> (processor)
1549                 ) {
1550                 return true;
1551         }
1552
1553         return false;
1554 }
1555
1556 bool
1557 ProcessorBox::one_processor_can_be_edited ()
1558 {
1559         list<ProcessorEntry*> selection = processor_display.selection ();
1560         list<ProcessorEntry*>::iterator i = selection.begin();
1561         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
1562                 ++i;
1563         }
1564
1565         return (i != selection.end());
1566 }
1567
1568 void
1569 ProcessorBox::toggle_edit_processor (boost::shared_ptr<Processor> processor)
1570 {
1571         boost::shared_ptr<Send> send;
1572         boost::shared_ptr<Return> retrn;
1573         boost::shared_ptr<PluginInsert> plugin_insert;
1574         boost::shared_ptr<PortInsert> port_insert;
1575         Window* gidget = 0;
1576
1577         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1578
1579                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1580                         return;
1581                 }
1582         }
1583
1584         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1585
1586                 if (!_session->engine().connected()) {
1587                         return;
1588                 }
1589
1590                 if (_parent_strip) {
1591                         _parent_strip->show_send (send);
1592                 }
1593
1594         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
1595
1596                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
1597                         /* no GUI for these */
1598                         return;
1599                 }
1600
1601                 if (!_session->engine().connected()) {
1602                         return;
1603                 }
1604
1605                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
1606
1607                 ReturnUIWindow *return_ui;
1608                 Window* w = get_processor_ui (retrn);
1609
1610                 if (w == 0) {
1611
1612                         return_ui = new ReturnUIWindow (retrn, _session);
1613                         return_ui->set_title (retrn->name ());
1614                         set_processor_ui (send, return_ui);
1615
1616                 } else {
1617                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
1618                 }
1619
1620                 gidget = return_ui;
1621
1622         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1623
1624                 PluginUIWindow *plugin_ui;
1625
1626                 /* these are both allowed to be null */
1627
1628                 Container* toplevel = get_toplevel();
1629                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1630
1631                 Window* w = get_processor_ui (plugin_insert);
1632
1633                 if (w == 0) {
1634
1635                         plugin_ui = new PluginUIWindow (win, plugin_insert);
1636                         plugin_ui->set_title (generate_processor_title (plugin_insert));
1637                         set_processor_ui (plugin_insert, plugin_ui);
1638
1639                 } else {
1640                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
1641                         plugin_ui->set_parent (win);
1642                 }
1643
1644                 gidget = plugin_ui;
1645
1646         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
1647
1648                 if (!_session->engine().connected()) {
1649                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1650                         msg.run ();
1651                         return;
1652                 }
1653
1654                 PortInsertWindow *io_selector;
1655
1656                 Window* w = get_processor_ui (port_insert);
1657
1658                 if (w == 0) {
1659                         io_selector = new PortInsertWindow (_session, port_insert);
1660                         set_processor_ui (port_insert, io_selector);
1661
1662                 } else {
1663                         io_selector = dynamic_cast<PortInsertWindow *> (w);
1664                 }
1665
1666                 gidget = io_selector;
1667         }
1668
1669         if (gidget) {
1670                 if (gidget->is_visible()) {
1671                         gidget->hide ();
1672                 } else {
1673                         gidget->show_all ();
1674                         gidget->present ();
1675                 }
1676         }
1677 }
1678
1679 void
1680 ProcessorBox::register_actions ()
1681 {
1682         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("processormenu"));
1683         Glib::RefPtr<Action> act;
1684
1685         /* new stuff */
1686         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
1687                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
1688
1689         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
1690                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
1691         ActionManager::jack_sensitive_actions.push_back (act);
1692         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New Send ..."),
1693                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
1694         ActionManager::jack_sensitive_actions.push_back (act);
1695
1696         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
1697
1698         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
1699                         sigc::ptr_fun (ProcessorBox::rb_clear));
1700         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
1701                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
1702         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
1703                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
1704
1705         /* standard editing stuff */
1706         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
1707                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
1708         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
1709         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
1710                         sigc::ptr_fun (ProcessorBox::rb_copy));
1711         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1712
1713         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
1714                         sigc::ptr_fun (ProcessorBox::rb_delete));
1715         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1716
1717         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
1718                         sigc::ptr_fun (ProcessorBox::rb_paste));
1719         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
1720                         sigc::ptr_fun (ProcessorBox::rb_rename));
1721         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
1722                         sigc::ptr_fun (ProcessorBox::rb_select_all));
1723         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
1724                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
1725
1726         /* activation etc. */
1727
1728         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate all"),
1729                                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
1730         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate all"),
1731                                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
1732         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
1733                                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
1734
1735         /* show editors */
1736         edit_action = ActionManager::register_action (popup_act_grp, X_("edit"), _("Edit..."),
1737                                                       sigc::ptr_fun (ProcessorBox::rb_edit));
1738
1739         ActionManager::add_action_group (popup_act_grp);
1740 }
1741
1742 void
1743 ProcessorBox::rb_ab_plugins ()
1744 {
1745         if (_current_processor_box == 0) {
1746                 return;
1747         }
1748
1749         _current_processor_box->ab_plugins ();
1750 }
1751
1752 void
1753 ProcessorBox::rb_choose_plugin ()
1754 {
1755         if (_current_processor_box == 0) {
1756                 return;
1757         }
1758         _current_processor_box->choose_plugin ();
1759 }
1760
1761 void
1762 ProcessorBox::rb_choose_insert ()
1763 {
1764         if (_current_processor_box == 0) {
1765                 return;
1766         }
1767         _current_processor_box->choose_insert ();
1768 }
1769
1770 void
1771 ProcessorBox::rb_choose_send ()
1772 {
1773         if (_current_processor_box == 0) {
1774                 return;
1775         }
1776         _current_processor_box->choose_send ();
1777 }
1778
1779 void
1780 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
1781 {
1782         if (_current_processor_box == 0) {
1783                 return;
1784         }
1785
1786         _current_processor_box->choose_aux (wr);
1787 }
1788
1789 void
1790 ProcessorBox::rb_clear ()
1791 {
1792         if (_current_processor_box == 0) {
1793                 return;
1794         }
1795
1796         _current_processor_box->clear_processors ();
1797 }
1798
1799
1800 void
1801 ProcessorBox::rb_clear_pre ()
1802 {
1803         if (_current_processor_box == 0) {
1804                 return;
1805         }
1806
1807         _current_processor_box->clear_processors (PreFader);
1808 }
1809
1810
1811 void
1812 ProcessorBox::rb_clear_post ()
1813 {
1814         if (_current_processor_box == 0) {
1815                 return;
1816         }
1817
1818         _current_processor_box->clear_processors (PostFader);
1819 }
1820
1821 void
1822 ProcessorBox::rb_cut ()
1823 {
1824         if (_current_processor_box == 0) {
1825                 return;
1826         }
1827
1828         _current_processor_box->cut_processors ();
1829 }
1830
1831 void
1832 ProcessorBox::rb_delete ()
1833 {
1834         if (_current_processor_box == 0) {
1835                 return;
1836         }
1837
1838         _current_processor_box->delete_processors ();
1839 }
1840
1841 void
1842 ProcessorBox::rb_copy ()
1843 {
1844         if (_current_processor_box == 0) {
1845                 return;
1846         }
1847         _current_processor_box->copy_processors ();
1848 }
1849
1850 void
1851 ProcessorBox::rb_paste ()
1852 {
1853         if (_current_processor_box == 0) {
1854                 return;
1855         }
1856
1857         _current_processor_box->paste_processors ();
1858 }
1859
1860 void
1861 ProcessorBox::rb_rename ()
1862 {
1863         if (_current_processor_box == 0) {
1864                 return;
1865         }
1866         _current_processor_box->rename_processors ();
1867 }
1868
1869 void
1870 ProcessorBox::rb_select_all ()
1871 {
1872         if (_current_processor_box == 0) {
1873                 return;
1874         }
1875
1876         _current_processor_box->select_all_processors ();
1877 }
1878
1879 void
1880 ProcessorBox::rb_deselect_all ()
1881 {
1882         if (_current_processor_box == 0) {
1883                 return;
1884         }
1885
1886         _current_processor_box->deselect_all_processors ();
1887 }
1888
1889 void
1890 ProcessorBox::rb_activate_all ()
1891 {
1892         if (_current_processor_box == 0) {
1893                 return;
1894         }
1895
1896         _current_processor_box->all_processors_active (true);
1897 }
1898
1899 void
1900 ProcessorBox::rb_deactivate_all ()
1901 {
1902         if (_current_processor_box == 0) {
1903                 return;
1904         }
1905         _current_processor_box->all_processors_active (false);
1906 }
1907
1908 void
1909 ProcessorBox::rb_edit ()
1910 {
1911         if (_current_processor_box == 0) {
1912                 return;
1913         }
1914
1915         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_processor);
1916 }
1917
1918 void
1919 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
1920 {
1921         if (!what_changed.contains (ARDOUR::Properties::name)) {
1922                 return;
1923         }
1924
1925         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
1926
1927         boost::shared_ptr<Processor> processor;
1928         boost::shared_ptr<PluginInsert> plugin_insert;
1929         boost::shared_ptr<Send> send;
1930
1931         list<ProcessorEntry*> children = processor_display.children();
1932
1933         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
1934
1935                 processor = (*iter)->processor ();
1936
1937                 Window* w = get_processor_ui (processor);
1938
1939                 if (!w) {
1940                         continue;
1941                 }
1942
1943                 /* rename editor windows for sends and plugins */
1944
1945                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1946                         w->set_title (send->name ());
1947                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1948                         w->set_title (generate_processor_title (plugin_insert));
1949                 }
1950         }
1951 }
1952
1953 string
1954 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
1955 {
1956         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
1957         string::size_type email_pos;
1958
1959         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1960                 maker = maker.substr (0, email_pos - 1);
1961         }
1962
1963         if (maker.length() > 32) {
1964                 maker = maker.substr (0, 32);
1965                 maker += " ...";
1966         }
1967
1968         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
1969 }
1970
1971 void
1972 ProcessorBox::on_size_allocate (Allocation& a)
1973 {
1974         HBox::on_size_allocate (a);
1975
1976         list<ProcessorEntry*> children = processor_display.children ();
1977         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1978                 (*i)->set_pixel_width (a.get_width ());
1979         }
1980 }
1981
1982 /** @param p Processor.
1983  *  @return the UI window for \a p.
1984  */
1985 Window *
1986 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
1987 {
1988         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_proxies.begin ();
1989         while (i != _processor_window_proxies.end()) {
1990                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1991                 if (t && t == p) {
1992                         return (*i)->get ();
1993                 }
1994
1995                 ++i;
1996         }
1997
1998         /* we shouldn't get here, because the ProcessorUIList should always contain
1999            an entry for each processor.
2000         */
2001         assert (false);
2002         return 0;
2003 }
2004
2005 /** Make a note of the UI window that a processor is using.
2006  *  @param p Processor.
2007  *  @param w UI window.
2008  */
2009 void
2010 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2011 {
2012         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
2013         while (i != _processor_window_proxies.end()) {
2014                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2015                 if (t && t == p) {
2016                         (*i)->set (w);
2017                         return;
2018                 }
2019
2020                 ++i;
2021         }
2022
2023         /* we shouldn't get here, because the ProcessorUIList should always contain
2024            an entry for each processor.
2025         */
2026         assert (false);
2027 }
2028
2029 ProcessorWindowProxy::ProcessorWindowProxy (
2030         string const & name,
2031         XMLNode const * node,
2032         ProcessorBox* box,
2033         boost::weak_ptr<Processor> processor
2034         )
2035         : WindowProxy<Gtk::Window> (name, node)
2036         , marked (false)
2037         , _processor_box (box)
2038         , _processor (processor)
2039 {
2040
2041 }
2042
2043
2044 void
2045 ProcessorWindowProxy::show ()
2046 {
2047         boost::shared_ptr<Processor> p = _processor.lock ();
2048         if (!p) {
2049                 return;
2050         }
2051
2052         _processor_box->toggle_edit_processor (p);
2053 }