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