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