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