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