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