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