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