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