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