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