Merge branch 'master' into cairocanvas
[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 #include <gtkmm2ext/rgb_macros.h>
42
43 #include "ardour/amp.h"
44 #include "ardour/audio_track.h"
45 #include "ardour/audioengine.h"
46 #include "ardour/internal_return.h"
47 #include "ardour/internal_send.h"
48 #include "ardour/panner_shell.h"
49 #include "ardour/plugin_insert.h"
50 #include "ardour/pannable.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/types.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::edit_generic_action;
96
97 static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
98 static const uint32_t midi_port_color = 0x960909FF; //Red
99
100 ProcessorEntry::ProcessorEntry (ProcessorBox* parent, boost::shared_ptr<Processor> p, Width w)
101         : _button (ArdourButton::led_default_elements)
102         , _position (PreFader)
103         , _parent (parent)
104         , _processor (p)
105         , _width (w)
106         , _visual_state (Gtk::STATE_NORMAL)
107         , _input_icon(true)
108         , _output_icon(false)
109 {
110         _vbox.show ();
111         
112         _button.set_diameter (3);
113         _button.set_distinct_led_click (true);
114         _button.set_led_left (true);
115         _button.signal_led_clicked.connect (sigc::mem_fun (*this, &ProcessorEntry::led_clicked));
116         _button.set_text (name (_width));
117
118         if (_processor) {
119
120                 _vbox.pack_start (_routing_icon);
121                 _vbox.pack_start (_input_icon);
122                 _vbox.pack_start (_button, true, true);
123                 _vbox.pack_end (_output_icon);
124
125                 _button.set_active (_processor->active());
126
127                 _routing_icon.set_no_show_all(true);
128                 _input_icon.set_no_show_all(true);
129
130                 _button.show ();
131                 _routing_icon.set_visible(false);
132                 _input_icon.hide();
133                 _output_icon.show();
134
135                 _processor->ActiveChanged.connect (active_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_active_changed, this), gui_context());
136                 _processor->PropertyChanged.connect (name_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_property_changed, this, _1), gui_context());
137                 _processor->ConfigurationChanged.connect (config_connection, invalidator (*this), boost::bind (&ProcessorEntry::processor_configuration_changed, this, _1, _2), gui_context());
138
139                 set<Evoral::Parameter> p = _processor->what_can_be_automated ();
140                 for (set<Evoral::Parameter>::iterator i = p.begin(); i != p.end(); ++i) {
141                         
142                         std::string label = _processor->describe_parameter (*i);
143
144                         if (boost::dynamic_pointer_cast<Send> (_processor)) {
145                                 label = _("Send");
146                         } else if (boost::dynamic_pointer_cast<Return> (_processor)) {
147                                 label = _("Return");
148                         }
149
150                         Control* c = new Control (_processor->automation_control (*i), label);
151                         
152                         _controls.push_back (c);
153
154                         if (boost::dynamic_pointer_cast<Amp> (_processor) == 0) {
155                                 /* Add non-Amp controls to the processor box */
156                                 _vbox.pack_start (c->box);
157                         }
158                 }
159
160                 _input_icon.set_ports(_processor->input_streams());
161                 _output_icon.set_ports(_processor->output_streams());
162
163                 _routing_icon.set_sources(_processor->input_streams());
164                 _routing_icon.set_sinks(_processor->output_streams());
165
166                 setup_tooltip ();
167                 setup_visuals ();
168         } else {
169                 _vbox.set_size_request (-1, _button.size_request().height);
170         }
171 }
172
173 ProcessorEntry::~ProcessorEntry ()
174 {
175         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
176                 delete *i;
177         }
178 }
179
180 EventBox&
181 ProcessorEntry::action_widget ()
182 {
183         return _button;
184 }
185
186 Gtk::Widget&
187 ProcessorEntry::widget ()
188 {
189         return _vbox;
190 }
191
192 string
193 ProcessorEntry::drag_text () const
194 {
195         return name (Wide);
196 }
197
198 void
199 ProcessorEntry::set_position (Position p, uint32_t num)
200 {
201         _position = p;
202         _position_num = num;
203
204         if (_position_num == 0 || _routing_icon.get_visible()) {
205                 _input_icon.show();
206         } else {
207                 _input_icon.hide();
208         }
209
210         setup_visuals ();
211 }
212
213 void
214 ProcessorEntry::set_visual_state (Gtkmm2ext::VisualState s, bool yn)
215 {
216         if (yn) {
217                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() | s));
218         } else {
219                 _button.set_visual_state (Gtkmm2ext::VisualState (_button.visual_state() & ~s));
220         }
221 }
222
223 void
224 ProcessorEntry::setup_visuals ()
225 {
226         switch (_position) {
227         case PreFader:
228                 _button.set_name ("processor prefader");
229                 break;
230
231         case Fader:
232                 _button.set_name ("processor fader");
233                 break;
234
235         case PostFader:
236                 _button.set_name ("processor postfader");
237                 break;
238         }
239 }
240
241
242 boost::shared_ptr<Processor>
243 ProcessorEntry::processor () const
244 {
245         return _processor;
246 }
247
248 void
249 ProcessorEntry::set_enum_width (Width w)
250 {
251         _width = w;
252         _button.set_text (name (_width));
253 }
254
255 void
256 ProcessorEntry::led_clicked()
257 {
258         if (_processor) {
259                 if (_button.get_active ()) {
260                         _processor->deactivate ();
261                 } else {
262                         _processor->activate ();
263                 }
264         }
265 }
266
267 void
268 ProcessorEntry::processor_active_changed ()
269 {
270         if (_processor) {
271                 _button.set_active (_processor->active());
272         }
273 }
274
275 void
276 ProcessorEntry::processor_property_changed (const PropertyChange& what_changed)
277 {
278         if (what_changed.contains (ARDOUR::Properties::name)) {
279                 _button.set_text (name (_width));
280                 setup_tooltip ();
281         }
282 }
283
284 void
285 ProcessorEntry::processor_configuration_changed (const ChanCount in, const ChanCount out)
286 {
287         _input_icon.set_ports(in);
288         _output_icon.set_ports(out);
289         _routing_icon.set_sources(in);
290         _routing_icon.set_sinks(out);
291         _input_icon.queue_draw();
292         _output_icon.queue_draw();
293         _routing_icon.queue_draw();
294 }
295
296 void
297 ProcessorEntry::setup_tooltip ()
298 {
299         if (_processor) {
300                 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_processor);
301                 if (pi) {
302                         std::string postfix = "";
303                         uint32_t replicated;
304                         if ((replicated = pi->get_count()) > 1) {
305                                 postfix = string_compose(_("\nThis mono plugin has been replicated %1 times."), replicated);
306                         }
307                         if (pi->plugin()->has_editor()) {
308                                 ARDOUR_UI::instance()->set_tip (_button,
309                                                 string_compose (_("<b>%1</b>\nDouble-click to show GUI.\nAlt+double-click to show generic GUI.%2"), name (Wide), postfix));
310                         } else {
311                                 ARDOUR_UI::instance()->set_tip (_button,
312                                                 string_compose (_("<b>%1</b>\nDouble-click to show generic GUI.%2"), name (Wide), postfix));
313                         }
314                         return;
315                 }
316         }
317         ARDOUR_UI::instance()->set_tip (_button, string_compose ("<b>%1</b>", name (Wide)));
318 }
319
320 string
321 ProcessorEntry::name (Width w) const
322 {
323         boost::shared_ptr<Send> send;
324         string name_display;
325
326         if (!_processor) {
327                 return string();
328         }
329
330         if ((send = boost::dynamic_pointer_cast<Send> (_processor)) != 0 &&
331             !boost::dynamic_pointer_cast<InternalSend>(_processor)) {
332
333                 name_display += '>';
334
335                 /* grab the send name out of its overall name */
336
337                 string::size_type lbracket, rbracket;
338                 lbracket = send->name().find ('[');
339                 rbracket = send->name().find (']');
340
341                 switch (w) {
342                 case Wide:
343                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
344                         break;
345                 case Narrow:
346                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
347                         break;
348                 }
349
350         } else {
351                 boost::shared_ptr<ARDOUR::PluginInsert> pi;
352                 uint32_t replicated;
353                 if ((pi = boost::dynamic_pointer_cast<ARDOUR::PluginInsert> (_processor)) != 0
354                                 && (replicated = pi->get_count()) > 1)
355                 {
356                         name_display += string_compose(_("(%1x1) "), replicated);
357                 }
358
359                 switch (w) {
360                 case Wide:
361                         name_display += _processor->display_name();
362                         break;
363                 case Narrow:
364                         name_display += PBD::short_version (_processor->display_name(), 5);
365                         break;
366                 }
367
368         }
369
370         return name_display;
371 }
372
373 void
374 ProcessorEntry::show_all_controls ()
375 {
376         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
377                 (*i)->set_visible (true);
378         }
379
380         _parent->update_gui_object_state (this);
381 }
382
383 void
384 ProcessorEntry::hide_all_controls ()
385 {
386         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
387                 (*i)->set_visible (false);
388         }
389
390         _parent->update_gui_object_state (this);
391 }
392
393 void
394 ProcessorEntry::add_control_state (XMLNode* node) const
395 {
396         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
397                 (*i)->add_state (node);
398         }
399 }
400
401 void
402 ProcessorEntry::set_control_state (XMLNode const * node)
403 {
404         for (list<Control*>::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
405                 (*i)->set_state (node);
406         }
407 }
408
409 string
410 ProcessorEntry::state_id () const
411 {
412         return string_compose ("processor %1", _processor->id().to_s());
413 }
414
415 void
416 ProcessorEntry::hide_things ()
417 {
418         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
419                 (*i)->hide_things ();
420         }
421 }
422
423
424 Menu *
425 ProcessorEntry::build_controls_menu ()
426 {
427         using namespace Menu_Helpers;
428         Menu* menu = manage (new Menu);
429         MenuList& items = menu->items ();
430
431         items.push_back (
432                 MenuElem (_("Show All Controls"), sigc::mem_fun (*this, &ProcessorEntry::show_all_controls))
433                 );
434                 
435         items.push_back (
436                 MenuElem (_("Hide All Controls"), sigc::mem_fun (*this, &ProcessorEntry::hide_all_controls))
437                 );
438
439         if (!_controls.empty ()) {
440                 items.push_back (SeparatorElem ());
441         }
442         
443         for (list<Control*>::iterator i = _controls.begin(); i != _controls.end(); ++i) {
444                 items.push_back (CheckMenuElem ((*i)->name ()));
445                 Gtk::CheckMenuItem* c = dynamic_cast<Gtk::CheckMenuItem*> (&items.back ());
446                 c->set_active ((*i)->visible ());
447                 c->signal_toggled().connect (sigc::bind (sigc::mem_fun (*this, &ProcessorEntry::toggle_control_visibility), *i));
448         }
449
450         return menu;
451 }
452
453 void
454 ProcessorEntry::toggle_control_visibility (Control* c)
455 {
456         c->set_visible (!c->visible ());
457         _parent->update_gui_object_state (this);
458 }
459
460 Menu *
461 ProcessorEntry::build_send_options_menu ()
462 {
463         using namespace Menu_Helpers;
464         Menu* menu = manage (new Menu);
465         MenuList& items = menu->items ();
466
467         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (_processor);
468         if (send) {
469
470                 items.push_back (CheckMenuElem (_("Link panner controls")));
471                 CheckMenuItem* c = dynamic_cast<CheckMenuItem*> (&items.back ());
472                 c->set_active (send->panner_shell()->is_linked_to_route());
473                 c->signal_toggled().connect (sigc::mem_fun (*this, &ProcessorEntry::toggle_panner_link));
474
475         }
476         return menu;
477 }
478
479 void
480 ProcessorEntry::toggle_panner_link ()
481 {
482         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (_processor);
483         if (send) {
484                 send->panner_shell()->set_linked_to_route(!send->panner_shell()->is_linked_to_route());
485         }
486 }
487
488 ProcessorEntry::Control::Control (boost::shared_ptr<AutomationControl> c, string const & n)
489         : _control (c)
490         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
491         , _slider (&_adjustment, 0, 13, false)
492         , _slider_persistant_tooltip (&_slider)
493         , _button (ArdourButton::led_default_elements)
494         , _ignore_ui_adjustment (false)
495         , _visible (false)
496         , _name (n)
497 {
498         _slider.set_controllable (c);
499         box.set_padding(0, 0, 4, 4);
500
501         if (c->toggled()) {
502                 _button.set_text (_name);
503                 _button.set_led_left (true);
504                 _button.set_name ("processor control button");
505                 box.add (_button);
506                 _button.show ();
507
508                 _button.signal_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked));
509                 _button.signal_led_clicked.connect (sigc::mem_fun (*this, &Control::button_clicked));
510                 c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
511
512         } else {
513                 
514                 _slider.set_name ("ProcessorControlSlider");
515                 _slider.set_text (_name);
516
517                 box.add (_slider);
518                 _slider.show ();
519
520                 double const lo = c->internal_to_interface (c->lower ());
521                 double const up = c->internal_to_interface (c->upper ());
522                 
523                 _adjustment.set_lower (lo);
524                 _adjustment.set_upper (up);
525                 _adjustment.set_step_increment ((up - lo) / 100);
526                 _adjustment.set_page_increment ((up - lo) / 10);
527                 _slider.set_default_value (c->internal_to_interface (c->normal ()));
528                 
529                 _adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
530                 c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
531         }
532
533         ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &Control::control_changed));
534         
535         control_changed ();
536         set_tooltip ();
537
538         /* We're providing our own PersistentTooltip */
539         set_no_tooltip_whatsoever (_slider);
540 }
541
542 void
543 ProcessorEntry::Control::set_tooltip ()
544 {
545         boost::shared_ptr<AutomationControl> c = _control.lock ();
546
547         if (!c) {
548                 return;
549         }
550         
551         stringstream s;
552         s << _name << ": ";
553         if (c->toggled ()) {
554                 s << (c->get_value() > 0.5 ? _("on") : _("off"));
555         } else {
556                 s << setprecision(2) << fixed;
557                 s << c->internal_to_user (c->get_value ());
558         }
559
560         string sm = Glib::Markup::escape_text (s.str());
561         
562         _slider_persistant_tooltip.set_tip (sm);
563         ARDOUR_UI::instance()->set_tip (_button, sm);
564 }
565
566 void
567 ProcessorEntry::Control::slider_adjusted ()
568 {
569         if (_ignore_ui_adjustment) {
570                 return;
571         }
572         
573         boost::shared_ptr<AutomationControl> c = _control.lock ();
574
575         if (!c) {
576                 return;
577         }
578
579         c->set_value (c->interface_to_internal (_adjustment.get_value ()));
580         set_tooltip ();
581 }
582
583 void
584 ProcessorEntry::Control::button_clicked ()
585 {
586         boost::shared_ptr<AutomationControl> c = _control.lock ();
587
588         if (!c) {
589                 return;
590         }
591
592         bool const n = _button.get_active ();
593
594         c->set_value (n ? 0 : 1);
595         _button.set_active (!n);
596         set_tooltip ();
597 }
598
599 void
600 ProcessorEntry::Control::control_changed ()
601 {
602         boost::shared_ptr<AutomationControl> c = _control.lock ();
603         if (!c) {
604                 return;
605         }
606
607         _ignore_ui_adjustment = true;
608
609         if (c->toggled ()) {
610
611                 _button.set_active (c->get_value() > 0.5);
612                 
613         } else {
614
615                 _adjustment.set_value (c->internal_to_interface (c->get_value ()));
616                 
617                 stringstream s;
618                 s.precision (1);
619                 s.setf (ios::fixed, ios::floatfield);
620                 s << c->internal_to_user (c->get_value ());
621         }
622         
623         _ignore_ui_adjustment = false;
624 }
625
626 void
627 ProcessorEntry::Control::add_state (XMLNode* node) const
628 {
629         XMLNode* c = new XMLNode (X_("Object"));
630         c->add_property (X_("id"), state_id ());
631         c->add_property (X_("visible"), _visible);
632         node->add_child_nocopy (*c);
633 }
634
635 void
636 ProcessorEntry::Control::set_state (XMLNode const * node)
637 {
638         XMLNode* n = GUIObjectState::get_node (node, state_id ());
639         if (n) {
640                 XMLProperty* p = n->property (X_("visible"));
641                 set_visible (p && string_is_affirmative (p->value ()));
642         } else {
643                 set_visible (false);
644         }
645 }
646
647 void
648 ProcessorEntry::Control::set_visible (bool v)
649 {
650         if (v) {
651                 box.show ();
652         } else {
653                 box.hide ();
654         }
655         
656         _visible = v;
657 }
658
659 /** Called when the Editor might have re-shown things that
660     we want hidden.
661 */
662 void
663 ProcessorEntry::Control::hide_things ()
664 {
665         if (!_visible) {
666                 box.hide ();
667         }
668 }
669
670 string
671 ProcessorEntry::Control::state_id () const
672 {
673         boost::shared_ptr<AutomationControl> c = _control.lock ();
674         assert (c);
675
676         return string_compose (X_("control %1"), c->id().to_s ());
677 }
678
679 PluginInsertProcessorEntry::PluginInsertProcessorEntry (ProcessorBox* b, boost::shared_ptr<ARDOUR::PluginInsert> p, Width w)
680         : ProcessorEntry (b, p, w)
681         , _plugin_insert (p)
682 {
683         p->PluginIoReConfigure.connect (
684                 _splitting_connection, invalidator (*this), boost::bind (&PluginInsertProcessorEntry::plugin_insert_splitting_changed, this), gui_context()
685                 );
686
687         plugin_insert_splitting_changed ();
688 }
689
690 void
691 PluginInsertProcessorEntry::plugin_insert_splitting_changed ()
692 {
693         _output_icon.set_ports(_plugin_insert->output_streams());
694         _routing_icon.set_splitting(_plugin_insert->splitting ());
695
696         ChanCount sources = _plugin_insert->input_streams();
697         ChanCount sinks = _plugin_insert->natural_input_streams();
698
699         /* replicated instances */
700         if (!_plugin_insert->splitting () && _plugin_insert->get_count() > 1) {
701                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
702                         sinks.set(*t, sinks.get(*t) * _plugin_insert->get_count());
703                 }
704         }
705         /* MIDI bypass */
706         if (_plugin_insert->natural_output_streams().n_midi() == 0 &&
707                         _plugin_insert->output_streams().n_midi() == 1) {
708                 sinks.set(DataType::MIDI, 1);
709                 sources.set(DataType::MIDI, 1);
710         }
711
712         _input_icon.set_ports(sinks);
713         _routing_icon.set_sinks(sinks);
714         _routing_icon.set_sources(sources);
715
716         if (_plugin_insert->splitting () ||
717                         _plugin_insert->input_streams().n_audio() < _plugin_insert->natural_input_streams().n_audio()
718                  )
719         {
720                 _routing_icon.set_size_request (-1, 7);
721                 _routing_icon.set_visible(true);
722                 _input_icon.show();
723         } else {
724                 _routing_icon.set_visible(false);
725                 if (_position_num != 0) {
726                         _input_icon.hide();
727                 }
728         }
729
730         _input_icon.queue_draw();
731         _output_icon.queue_draw();
732         _routing_icon.queue_draw();
733 }
734
735 void
736 PluginInsertProcessorEntry::hide_things ()
737 {
738         ProcessorEntry::hide_things ();
739         plugin_insert_splitting_changed ();
740 }
741
742
743 bool
744 ProcessorEntry::PortIcon::on_expose_event (GdkEventExpose* ev)
745 {
746         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
747
748         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
749         cairo_clip (cr);
750
751         Gtk::Allocation a = get_allocation();
752         double const width = a.get_width();
753         double const height = a.get_height();
754
755         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
756         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
757
758         cairo_rectangle (cr, 0, 0, width, height);
759         cairo_fill (cr);
760
761         if (_ports.n_total() > 1) {
762                 for (uint32_t i = 0; i < _ports.n_total(); ++i) {
763                         if (i < _ports.n_midi()) {
764                                 cairo_set_source_rgb (cr,
765                                                 UINT_RGBA_R_FLT(midi_port_color),
766                                                 UINT_RGBA_G_FLT(midi_port_color),
767                                                 UINT_RGBA_B_FLT(midi_port_color));
768                         } else {
769                                 cairo_set_source_rgb (cr,
770                                                 UINT_RGBA_R_FLT(audio_port_color),
771                                                 UINT_RGBA_G_FLT(audio_port_color),
772                                                 UINT_RGBA_B_FLT(audio_port_color));
773                         }
774                         const float x = rintf(width * (.2f + .6f * i / (_ports.n_total() - 1.f)));
775                         cairo_rectangle (cr, x-1, 0, 3, height);
776                         cairo_fill(cr);
777                 }
778         } else if (_ports.n_total() == 1) {
779                 if (_ports.n_midi() == 1) {
780                         cairo_set_source_rgb (cr,
781                                         UINT_RGBA_R_FLT(midi_port_color),
782                                         UINT_RGBA_G_FLT(midi_port_color),
783                                         UINT_RGBA_B_FLT(midi_port_color));
784                 } else {
785                         cairo_set_source_rgb (cr,
786                                         UINT_RGBA_R_FLT(audio_port_color),
787                                         UINT_RGBA_G_FLT(audio_port_color),
788                                         UINT_RGBA_B_FLT(audio_port_color));
789                 }
790                 const float x = rintf(width * .5);
791                 cairo_rectangle (cr, x-1, 0, 3, height);
792                 cairo_fill(cr);
793                 cairo_stroke(cr);
794         }
795
796         cairo_destroy(cr);
797         return true;
798 }
799
800 bool
801 ProcessorEntry::RoutingIcon::on_expose_event (GdkEventExpose* ev)
802 {
803         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
804
805         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
806         cairo_clip (cr);
807
808         cairo_set_line_width (cr, 1.0);
809         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
810
811         Gtk::Allocation a = get_allocation();
812         double const width = a.get_width();
813         double const height = a.get_height();
814
815         Gdk::Color const bg = get_style()->get_bg (STATE_NORMAL);
816         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
817
818         cairo_rectangle (cr, 0, 0, width, height);
819         cairo_fill (cr);
820
821         Gdk::Color const fg = get_style()->get_fg (STATE_NORMAL);
822         cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
823
824         const uint32_t sources = _sources.n_total();
825         const uint32_t sinks = _sinks.n_total();
826
827         /* MIDI */
828         const uint32_t midi_sources = _sources.n_midi();
829         const uint32_t midi_sinks = _sinks.n_midi();
830
831         cairo_set_source_rgb (cr,
832                         UINT_RGBA_R_FLT(midi_port_color),
833                         UINT_RGBA_G_FLT(midi_port_color),
834                         UINT_RGBA_B_FLT(midi_port_color));
835         if (midi_sources > 0 && midi_sinks > 0 && sinks > 1 && sources > 1) {
836                 for (uint32_t i = 0 ; i < midi_sources; ++i) {
837                         const float si_x  = rintf(width * (.2f + .6f * i  / (sinks - 1.f))) + .5f;
838                         const float si_x0 = rintf(width * (.2f + .6f * i / (sources - 1.f))) + .5f;
839                         cairo_move_to (cr, si_x, height);
840                         cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
841                         cairo_stroke (cr);
842                 }
843         } else if (midi_sources == 1 && midi_sinks == 1 && sinks == 1 && sources == 1) {
844                 const float si_x = rintf(width * .5f) + .5f;
845                 cairo_move_to (cr, si_x, height);
846                 cairo_line_to (cr, si_x, 0);
847                 cairo_stroke (cr);
848         } else if (midi_sources == 1 && midi_sinks == 1) {
849                 /* unusual cases -- removed synth, midi-track w/audio plugins */
850                 const float si_x  = rintf(width * (sinks   > 1 ? .2f : .5f)) + .5f;
851                 const float si_x0 = rintf(width * (sources > 1 ? .2f : .5f)) + .5f;
852                 cairo_move_to (cr, si_x, height);
853                 cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
854                 cairo_stroke (cr);
855         }
856
857         /* AUDIO */
858         const uint32_t audio_sources = _sources.n_audio();
859         const uint32_t audio_sinks = _sinks.n_audio();
860         cairo_set_source_rgb (cr,
861                         UINT_RGBA_R_FLT(audio_port_color),
862                         UINT_RGBA_G_FLT(audio_port_color),
863                         UINT_RGBA_B_FLT(audio_port_color));
864
865         if (_splitting) {
866                 assert(audio_sources < 2);
867                 assert(audio_sinks > 1);
868                 /* assume there is only ever one MIDI port */
869                 const float si_x0 = rintf(width * (midi_sources > 0 ? .8f : .5f)) + .5f;
870                 for (uint32_t i = midi_sinks; i < sinks; ++i) {
871                         const float si_x = rintf(width * (.2f + .6f * i / (sinks - 1.f))) + .5f;
872                         cairo_move_to (cr, si_x, height);
873                         cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
874                         cairo_stroke (cr);
875                 }
876         } else if (audio_sources > 1) {
877                 for (uint32_t i = 0 ; i < audio_sources; ++i) {
878                         const float si_x = rintf(width * (.2f + .6f * (i + midi_sinks) / (sinks - 1.f))) + .5f;
879                         const float si_x0 = rintf(width * (.2f + .6f * (i + midi_sources) / (sources - 1.f))) + .5f;
880                         cairo_move_to (cr, si_x, height);
881                         cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
882                         cairo_stroke (cr);
883                 }
884         } else if (audio_sources == 1 && audio_sinks == 1) {
885                 const float si_x = rintf(width * .5f) + .5f;
886                 cairo_move_to (cr, si_x, height);
887                 cairo_line_to (cr, si_x, 0);
888                 cairo_stroke (cr);
889         }
890         cairo_destroy(cr);
891         return true;
892 }
893
894 ProcessorBox::ProcessorBox (ARDOUR::Session* sess, boost::function<PluginSelector*()> get_plugin_selector,
895                             RouteProcessorSelection& rsel, MixerStrip* parent, bool owner_is_mixer)
896         : _parent_strip (parent)
897         , _owner_is_mixer (owner_is_mixer)
898         , ab_direction (true)
899         , _get_plugin_selector (get_plugin_selector)
900         , _placement (-1)
901         , _visible_prefader_processors (0)
902         , _rr_selection(rsel)
903         , _redisplay_pending (false)
904
905 {
906         set_session (sess);
907
908         _width = Wide;
909         processor_menu = 0;
910         no_processor_redisplay = false;
911
912         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
913         processor_scroller.add (processor_display);
914         pack_start (processor_scroller, true, true);
915
916         processor_display.set_flags (CAN_FOCUS);
917         processor_display.set_name ("ProcessorList");
918         processor_display.set_data ("processorbox", this);
919         processor_display.set_size_request (48, -1);
920         processor_display.set_spacing (0);
921
922         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
923         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
924
925         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
926         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
927
928         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
929         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
930
931         processor_scroller.show ();
932         processor_display.show ();
933
934         if (parent) {
935                 parent->DeliveryChanged.connect (
936                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
937                         );
938         }
939
940         ARDOUR_UI::instance()->set_tip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
941 }
942
943 ProcessorBox::~ProcessorBox ()
944 {
945         /* it may appear as if we should delete processor_menu but that is a
946          * pointer to a widget owned by the UI Manager, and has potentially
947          * be returned to many other ProcessorBoxes. GTK doesn't really make
948          * clear the ownership of this widget, which is a menu and thus is
949          * never packed into any container other than an implict GtkWindow.
950          *
951          * For now, until or if we ever get clarification over the ownership
952          * story just let it continue to exist. At worst, its a small memory leak.
953          */
954 }
955
956 void
957 ProcessorBox::set_route (boost::shared_ptr<Route> r)
958 {
959         if (_route == r) {
960                 return;
961         }
962
963         _route_connections.drop_connections();
964
965         /* new route: any existing block on processor redisplay must be meaningless */
966         no_processor_redisplay = false;
967         _route = r;
968
969         _route->processors_changed.connect (
970                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
971                 );
972
973         _route->DropReferences.connect (
974                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
975                 );
976
977         _route->PropertyChanged.connect (
978                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
979                 );
980
981         redisplay_processors ();
982 }
983
984 void
985 ProcessorBox::route_going_away ()
986 {
987         /* don't keep updating display as processors are deleted */
988         no_processor_redisplay = true;
989         _route.reset ();
990 }
991
992 void
993 ProcessorBox::object_drop(DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
994 {
995         boost::shared_ptr<Processor> p;
996         if (position) {
997                 p = position->processor ();
998                 if (!p) {
999                         /* dropped on the blank entry (which will be before the
1000                            fader), so use the first non-blank child as our
1001                            `dropped on' processor */
1002                         list<ProcessorEntry*> c = processor_display.children ();
1003                         list<ProcessorEntry*>::iterator i = c.begin ();
1004
1005                         assert (i != c.end ());
1006                         p = (*i)->processor ();
1007                         assert (p);
1008                 }
1009         }
1010
1011         list<ProcessorEntry*> children = source->selection ();
1012         list<boost::shared_ptr<Processor> > procs;
1013         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1014                 if ((*i)->processor ()) {
1015                         procs.push_back ((*i)->processor ());
1016                 }
1017         }
1018
1019         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
1020                 XMLNode& state = (*i)->get_state ();
1021                 XMLNodeList nlist;
1022                 nlist.push_back (&state);
1023                 paste_processor_state (nlist, p);
1024                 delete &state;
1025         }
1026
1027         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
1028            ourselves.
1029         */
1030
1031         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
1032                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
1033                 if (other) {
1034                         other->delete_dragged_processors (procs);
1035                 }
1036         }
1037 }
1038
1039 void
1040 ProcessorBox::set_width (Width w)
1041 {
1042         if (_width == w) {
1043                 return;
1044         }
1045
1046         _width = w;
1047
1048         list<ProcessorEntry*> children = processor_display.children ();
1049         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1050                 (*i)->set_enum_width (w);
1051         }
1052
1053         queue_resize ();
1054 }
1055
1056 Gtk::Menu*
1057 ProcessorBox::build_possible_aux_menu ()
1058 {
1059         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
1060
1061         if (rl->empty()) {
1062                 /* No aux sends if there are no busses */
1063                 return 0;
1064         }
1065
1066         using namespace Menu_Helpers;
1067         Menu* menu = manage (new Menu);
1068         MenuList& items = menu->items();
1069
1070         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
1071                 if (!_route->internal_send_for (*r) && *r != _route) {
1072                         items.push_back (MenuElem ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
1073                 }
1074         }
1075
1076         return menu;
1077 }
1078
1079 void
1080 ProcessorBox::show_processor_menu (int arg)
1081 {
1082         if (processor_menu == 0) {
1083                 processor_menu = build_processor_menu ();
1084                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
1085         }
1086
1087         /* Sort out the plugin submenu */
1088
1089         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
1090
1091         if (plugin_menu_item) {
1092                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
1093         }
1094
1095         /* And the aux submenu */
1096
1097         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
1098
1099         if (aux_menu_item) {
1100                 Menu* m = build_possible_aux_menu();
1101                 if (m && !m->items().empty()) {
1102                         aux_menu_item->set_submenu (*m);
1103                         aux_menu_item->set_sensitive (true);
1104                 } else {
1105                         /* stupid gtkmm: we need to pass a null reference here */
1106                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
1107                         aux_menu_item->set_sensitive (false);
1108                 }
1109         }
1110
1111         ProcessorEntry* single_selection = 0;
1112         if (processor_display.selection().size() == 1) {
1113                 single_selection = processor_display.selection().front();
1114         }
1115
1116         /* And the controls submenu */
1117
1118         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
1119
1120         if (controls_menu_item) {
1121                 if (single_selection) {
1122                         Menu* m = single_selection->build_controls_menu ();
1123                         if (m && !m->items().empty()) {
1124                                 controls_menu_item->set_submenu (*m);
1125                                 controls_menu_item->set_sensitive (true);
1126                         } else {
1127                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
1128                                 controls_menu_item->set_sensitive (false);
1129                         }
1130                 }
1131         }
1132
1133         Gtk::MenuItem* send_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/send_options"));
1134         if (send_menu_item) {
1135                 if (single_selection) {
1136                         Menu* m = single_selection->build_send_options_menu ();
1137                         if (m && !m->items().empty()) {
1138                                 send_menu_item->set_submenu (*m);
1139                                 send_menu_item->set_sensitive (true);
1140                         } else {
1141                                 gtk_menu_item_set_submenu (send_menu_item->gobj(), 0);
1142                                 send_menu_item->set_sensitive (false);
1143                         }
1144                 }
1145         }
1146
1147         /* Sensitise actions as approprioate */
1148
1149         cut_action->set_sensitive (can_cut());
1150         paste_action->set_sensitive (!_rr_selection.processors.empty());
1151
1152         const bool sensitive = !processor_display.selection().empty();
1153         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
1154         edit_action->set_sensitive (one_processor_can_be_edited ());
1155         edit_generic_action->set_sensitive (one_processor_can_be_edited ());
1156
1157         boost::shared_ptr<PluginInsert> pi;
1158         if (single_selection) {
1159                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
1160         }
1161
1162         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
1163         edit_action->set_sensitive (pi && pi->plugin()->has_editor ());
1164
1165         /* disallow rename for multiple selections, for plugin inserts and for the fader */
1166         rename_action->set_sensitive (single_selection && !pi && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ()));
1167
1168         processor_menu->popup (1, arg);
1169
1170         /* Add a placeholder gap to the processor list to indicate where a processor would be
1171            inserted were one chosen from the menu.
1172         */
1173         int x, y;
1174         processor_display.get_pointer (x, y);
1175         _placement = processor_display.add_placeholder (y);
1176
1177         if (_visible_prefader_processors == 0 && _placement > 0) {
1178                 --_placement;
1179         }
1180 }
1181
1182 bool
1183 ProcessorBox::enter_notify (GdkEventCrossing*)
1184 {
1185         _current_processor_box = this;
1186         return false;
1187 }
1188
1189 bool
1190 ProcessorBox::leave_notify (GdkEventCrossing*)
1191 {
1192         return false;
1193 }
1194
1195 void
1196 ProcessorBox::processor_operation (ProcessorOperation op) 
1197 {
1198         ProcSelection targets;
1199
1200         get_selected_processors (targets);
1201
1202         if (targets.empty()) {
1203
1204                 int x, y;
1205                 processor_display.get_pointer (x, y);
1206
1207                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
1208
1209                 if (pointer.first && pointer.first->processor()) {
1210                         targets.push_back (pointer.first->processor ());
1211                 }
1212         }
1213
1214         switch (op) {
1215         case ProcessorsSelectAll:
1216                 processor_display.select_all ();
1217                 break;
1218
1219         case ProcessorsCopy:
1220                 copy_processors (targets);
1221                 break;
1222
1223         case ProcessorsCut:
1224                 cut_processors (targets);
1225                 break;
1226
1227         case ProcessorsPaste:
1228                 if (targets.empty()) {
1229                         paste_processors ();
1230                 } else {
1231                         paste_processors (targets.front());
1232                 }
1233                 break;
1234
1235         case ProcessorsDelete:
1236                 delete_processors (targets);
1237                 break;
1238
1239         case ProcessorsToggleActive:
1240                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
1241                         if ((*i)->active()) {
1242                                 (*i)->deactivate ();
1243                         } else {
1244                                 (*i)->activate ();
1245                         }
1246                 }
1247                 break;
1248
1249         case ProcessorsAB:
1250                 ab_plugins ();
1251                 break;
1252
1253         default:
1254                 break;
1255         }
1256 }
1257
1258 ProcessorWindowProxy* 
1259 ProcessorBox::find_window_proxy (boost::shared_ptr<Processor> processor) const
1260 {
1261         for (list<ProcessorWindowProxy*>::const_iterator i = _processor_window_info.begin(); i != _processor_window_info.end(); ++i) {
1262                 boost::shared_ptr<Processor> p = (*i)->processor().lock();
1263
1264                 if (p && p == processor) {
1265                         return (*i);
1266                 }
1267         }
1268
1269         return 0;
1270 }
1271
1272
1273
1274 bool
1275 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
1276 {
1277         boost::shared_ptr<Processor> processor;
1278         if (child) {
1279                 processor = child->processor ();
1280         }
1281
1282         int ret = false;
1283         bool selected = processor_display.selected (child);
1284
1285         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
1286
1287                 if (_session->engine().connected()) {
1288                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
1289
1290                         if (!one_processor_can_be_edited ()) return true;
1291
1292                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
1293                                 generic_edit_processor (processor);
1294                         } else {
1295                                 edit_processor (processor);
1296                         }
1297                 }
1298
1299                 ret = true;
1300
1301         } else if (processor && ev->button == 1 && selected) {
1302
1303                 // this is purely informational but necessary for route params UI
1304                 ProcessorSelected (processor); // emit
1305
1306         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
1307
1308                 choose_plugin ();
1309                 _get_plugin_selector()->show_manager ();
1310         }
1311
1312         return ret;
1313 }
1314
1315 bool
1316 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
1317 {
1318         boost::shared_ptr<Processor> processor;
1319         if (child) {
1320                 processor = child->processor ();
1321         }
1322
1323         if (processor && Keyboard::is_delete_event (ev)) {
1324
1325                 Glib::signal_idle().connect (sigc::bind (
1326                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
1327                                 boost::weak_ptr<Processor>(processor)));
1328
1329         } else if (Keyboard::is_context_menu_event (ev)) {
1330
1331                 show_processor_menu (ev->time);
1332
1333         } else if (processor && Keyboard::is_button2_event (ev)
1334 #ifndef GTKOSX
1335                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
1336 #endif
1337                 ) {
1338
1339                 /* button2-click with no/appropriate modifiers */
1340
1341                 if (processor->active()) {
1342                         processor->deactivate ();
1343                 } else {
1344                         processor->activate ();
1345                 }
1346         }
1347
1348         return false;
1349 }
1350
1351 Menu *
1352 ProcessorBox::build_processor_menu ()
1353 {
1354         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
1355         processor_menu->set_name ("ArdourContextMenu");
1356         return processor_menu;
1357 }
1358
1359 void
1360 ProcessorBox::select_all_processors ()
1361 {
1362         processor_display.select_all ();
1363 }
1364
1365 void
1366 ProcessorBox::deselect_all_processors ()
1367 {
1368         processor_display.select_none ();
1369 }
1370
1371 void
1372 ProcessorBox::choose_plugin ()
1373 {
1374         _get_plugin_selector()->set_interested_object (*this);
1375 }
1376
1377 /** @return true if an error occurred, otherwise false */
1378 bool
1379 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
1380 {
1381         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
1382
1383                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
1384
1385                 Route::ProcessorStreams err_streams;
1386
1387                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
1388                         weird_plugin_dialog (**p, err_streams);
1389                         return true;
1390                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
1391                 } else {
1392
1393                         if (Profile->get_sae()) {
1394                                 processor->activate ();
1395                         }
1396                 }
1397         }
1398
1399         return false;
1400 }
1401
1402 void
1403 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
1404 {
1405         ArdourDialog dialog (_("Plugin Incompatibility"));
1406         Label label;
1407
1408         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
1409                         p.name(), streams.index);
1410
1411         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
1412         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
1413
1414         text += _("\nThis plugin has:\n");
1415         if (has_midi) {
1416                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
1417                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
1418         }
1419         if (has_audio) {
1420                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
1421                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
1422         }
1423
1424         text += _("\nbut at the insertion point, there are:\n");
1425         if (has_midi) {
1426                 uint32_t const n = streams.count.n_midi ();
1427                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
1428         }
1429         if (has_audio) {
1430                 uint32_t const n = streams.count.n_audio ();
1431                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
1432         }
1433
1434         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
1435         label.set_text(text);
1436
1437         dialog.get_vbox()->pack_start (label);
1438         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1439
1440         dialog.set_name (X_("PluginIODialog"));
1441         dialog.set_modal (true);
1442         dialog.show_all ();
1443
1444         dialog.run ();
1445 }
1446
1447 void
1448 ProcessorBox::choose_insert ()
1449 {
1450         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
1451         _route->add_processor_by_index (processor, _placement);
1452 }
1453
1454 /* Caller must not hold process lock */
1455 void
1456 ProcessorBox::choose_send ()
1457 {
1458         boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
1459         boost::shared_ptr<Send> send (new Send (*_session, sendpan, _route->mute_master()));
1460
1461         /* make an educated guess at the initial number of outputs for the send */
1462         ChanCount outs = (_session->master_out())
1463                         ? _session->master_out()->n_outputs()
1464                         : _route->n_outputs();
1465
1466         /* XXX need processor lock on route */
1467         try {
1468                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1469                 send->output()->ensure_io (outs, false, this);
1470         } catch (AudioEngine::PortRegistrationFailure& err) {
1471                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
1472                 return;
1473         }
1474
1475         /* let the user adjust the IO setup before creation.
1476
1477            Note: this dialog is NOT modal - we just leave it to run and it will
1478            return when its Finished signal is emitted - typically when the window
1479            is closed.
1480          */
1481
1482         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
1483         ios->show ();
1484
1485         /* keep a reference to the send so it doesn't get deleted while
1486            the IOSelectorWindow is doing its stuff
1487         */
1488         _processor_being_created = send;
1489
1490         ios->selector().Finished.connect (sigc::bind (
1491                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
1492                         boost::weak_ptr<Processor>(send), ios));
1493
1494 }
1495
1496 void
1497 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1498 {
1499         boost::shared_ptr<Processor> processor (weak_processor.lock());
1500
1501         /* drop our temporary reference to the new send */
1502         _processor_being_created.reset ();
1503
1504         if (!processor) {
1505                 return;
1506         }
1507
1508         switch (r) {
1509         case IOSelector::Cancelled:
1510                 // processor will go away when all shared_ptrs to it vanish
1511                 break;
1512
1513         case IOSelector::Accepted:
1514                 _route->add_processor_by_index (processor, _placement);
1515                 if (Profile->get_sae()) {
1516                         processor->activate ();
1517                 }
1518                 break;
1519         }
1520
1521         delete_when_idle (ios);
1522 }
1523
1524 void
1525 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1526 {
1527         boost::shared_ptr<Processor> processor (weak_processor.lock());
1528
1529         /* drop our temporary reference to the new return */
1530         _processor_being_created.reset ();
1531
1532         if (!processor) {
1533                 return;
1534         }
1535
1536         switch (r) {
1537         case IOSelector::Cancelled:
1538                 // processor will go away when all shared_ptrs to it vanish
1539                 break;
1540
1541         case IOSelector::Accepted:
1542                 _route->add_processor_by_index (processor, _placement);
1543                 if (Profile->get_sae()) {
1544                         processor->activate ();
1545                 }
1546                 break;
1547         }
1548
1549         delete_when_idle (ios);
1550 }
1551
1552 void
1553 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
1554 {
1555         if (!_route) {
1556                 return;
1557         }
1558
1559         boost::shared_ptr<Route> target = wr.lock();
1560
1561         if (!target) {
1562                 return;
1563         }
1564
1565         _session->add_internal_send (target, _placement, _route);
1566 }
1567
1568 void
1569 ProcessorBox::route_processors_changed (RouteProcessorChange c)
1570 {
1571         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
1572                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
1573                 return;
1574         }
1575
1576         redisplay_processors ();
1577 }
1578
1579 void
1580 ProcessorBox::redisplay_processors ()
1581 {
1582         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
1583         bool     fader_seen;
1584
1585         if (no_processor_redisplay) {
1586                 return;
1587         }
1588
1589         processor_display.clear ();
1590
1591         _visible_prefader_processors = 0;
1592         fader_seen = false;
1593
1594         _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &ProcessorBox::help_count_visible_prefader_processors), 
1595                                                &_visible_prefader_processors, &fader_seen));
1596
1597         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
1598
1599         for (ProcessorWindowProxies::iterator i = _processor_window_info.begin(); i != _processor_window_info.end(); ++i) {
1600                 (*i)->marked = false;
1601         }
1602
1603         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
1604
1605         /* trim dead wood from the processor window proxy list */
1606
1607         ProcessorWindowProxies::iterator i = _processor_window_info.begin();
1608         while (i != _processor_window_info.end()) {
1609                 ProcessorWindowProxies::iterator j = i;
1610                 ++j;
1611
1612                 if (!(*i)->valid()) {
1613
1614                         WM::Manager::instance().remove (*i);
1615                         delete *i;
1616                         _processor_window_info.erase (i);
1617                         
1618                 } else if (!(*i)->marked) {
1619
1620                         /* this processor is no longer part of this processor
1621                          * box.
1622                          *
1623                          * that could be because it was deleted or it could be
1624                          * because the route being displayed in the parent
1625                          * strip changed.
1626                          *
1627                          * The latter only happens with the editor mixer strip.
1628                          */
1629
1630                         if (is_editor_mixer_strip()) {
1631                                 
1632                                 /* editor mixer strip .. DO NOTHING
1633                                  *
1634                                  * note: the processor window stays visible if
1635                                  * it is already visible
1636                                  */
1637                         } else {
1638                                 (*i)->hide ();
1639                                 WM::Manager::instance().remove (*i);
1640                                 delete *i;
1641                                 _processor_window_info.erase (i);
1642                         } 
1643                 } 
1644
1645                 i = j;
1646         }
1647
1648         setup_entry_positions ();
1649 }
1650
1651 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
1652  *  not already have one.
1653  */
1654 void
1655 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
1656 {
1657         boost::shared_ptr<Processor> p = w.lock ();
1658         if (!p) {
1659                 return;
1660         }
1661
1662         ProcessorWindowProxies::iterator i = _processor_window_info.begin ();
1663         while (i != _processor_window_info.end()) {
1664
1665                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1666
1667                 if (p == t) {
1668                         /* this processor is already on the list; done */
1669                         (*i)->marked = true;
1670                         return;
1671                 }
1672
1673                 ++i;
1674         }
1675
1676         /* not on the list; add it */
1677
1678         string loc;
1679         if (_parent_strip) {
1680                 if (_parent_strip->mixer_owned()) {
1681                         loc = X_("M");
1682                 } else {
1683                         loc = X_("R");
1684                 }
1685         } else {
1686                 loc = X_("P");
1687         }
1688
1689         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1690                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1691                 this,
1692                 w);
1693
1694         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
1695
1696         if (ui_xml) {
1697                 wp->set_state (*ui_xml);
1698         }
1699         
1700         wp->marked = true;
1701
1702         /* if the processor already has an existing UI,
1703            note that so that we don't recreate it
1704         */
1705
1706         void* existing_ui = p->get_ui ();
1707
1708         if (existing_ui) {
1709                 wp->use_window (*(reinterpret_cast<Gtk::Window*>(existing_ui)));
1710         }
1711
1712         _processor_window_info.push_back (wp);
1713         WM::Manager::instance().register_window (wp);
1714 }
1715
1716 void
1717 ProcessorBox::help_count_visible_prefader_processors (boost::weak_ptr<Processor> p, uint32_t* cnt, bool* amp_seen)
1718 {
1719         boost::shared_ptr<Processor> processor (p.lock ());
1720
1721         if (processor && processor->display_to_user()) {
1722
1723                 if (boost::dynamic_pointer_cast<Amp>(processor)) {
1724                         *amp_seen = true;
1725                 } else {
1726                         if (!*amp_seen) {
1727                                 (*cnt)++;
1728                         }
1729                 }
1730         }
1731 }
1732
1733 void
1734 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1735 {
1736         boost::shared_ptr<Processor> processor (p.lock ());
1737
1738         if (!processor || !processor->display_to_user()) {
1739                 return;
1740         }
1741
1742         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
1743         ProcessorEntry* e = 0;
1744         if (plugin_insert) {
1745                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
1746         } else {
1747                 e = new ProcessorEntry (this, processor, _width);
1748         }
1749
1750         /* Set up this entry's state from the GUIObjectState */
1751         XMLNode* proc = entry_gui_object_state (e);
1752         if (proc) {
1753                 e->set_control_state (proc);
1754         }
1755         
1756         if (boost::dynamic_pointer_cast<Send> (processor)) {
1757                 /* Always show send controls */
1758                 e->show_all_controls ();
1759         }
1760
1761         processor_display.add_child (e);
1762 }
1763
1764 void
1765 ProcessorBox::reordered ()
1766 {
1767         compute_processor_sort_keys ();
1768         setup_entry_positions ();
1769 }
1770
1771 void
1772 ProcessorBox::setup_entry_positions ()
1773 {
1774         list<ProcessorEntry*> children = processor_display.children ();
1775         bool pre_fader = true;
1776
1777         uint32_t num = 0;
1778         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1779                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor())) {
1780                         pre_fader = false;
1781                         (*i)->set_position (ProcessorEntry::Fader, num++);
1782                 } else {
1783                         if (pre_fader) {
1784                                 (*i)->set_position (ProcessorEntry::PreFader, num++);
1785                         } else {
1786                                 (*i)->set_position (ProcessorEntry::PostFader, num++);
1787                         }
1788                 }
1789         }
1790 }
1791
1792 void
1793 ProcessorBox::compute_processor_sort_keys ()
1794 {
1795         list<ProcessorEntry*> children = processor_display.children ();
1796         Route::ProcessorList our_processors;
1797
1798         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1799                 if ((*i)->processor()) {
1800                         our_processors.push_back ((*i)->processor ());
1801                 }
1802         }
1803
1804         if (_route->reorder_processors (our_processors)) {
1805                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1806                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1807                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1808                    when the drag is torn down after this handler function is finished.
1809                 */
1810                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1811         }
1812 }
1813
1814 void
1815 ProcessorBox::report_failed_reorder ()
1816 {
1817         /* reorder failed, so redisplay */
1818
1819         redisplay_processors ();
1820
1821         /* now tell them about the problem */
1822
1823         ArdourDialog dialog (_("Plugin Incompatibility"));
1824         Label label;
1825
1826         label.set_text (_("\
1827 You cannot reorder these plugins/sends/inserts\n\
1828 in that way because the inputs and\n\
1829 outputs will not work correctly."));
1830
1831         dialog.get_vbox()->set_border_width (12);
1832         dialog.get_vbox()->pack_start (label);
1833         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1834
1835         dialog.set_name (X_("PluginIODialog"));
1836         dialog.set_modal (true);
1837         dialog.show_all ();
1838
1839         dialog.run ();
1840 }
1841
1842 void
1843 ProcessorBox::rename_processors ()
1844 {
1845         ProcSelection to_be_renamed;
1846
1847         get_selected_processors (to_be_renamed);
1848
1849         if (to_be_renamed.empty()) {
1850                 return;
1851         }
1852
1853         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1854                 rename_processor (*i);
1855         }
1856 }
1857
1858 bool
1859 ProcessorBox::can_cut () const
1860 {
1861         vector<boost::shared_ptr<Processor> > sel;
1862
1863         get_selected_processors (sel);
1864
1865         /* cut_processors () does not cut inserts */
1866
1867         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1868
1869                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1870                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1871                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1872                         return true;
1873                 }
1874         }
1875
1876         return false;
1877 }
1878
1879 void
1880 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1881 {
1882         if (to_be_removed.empty()) {
1883                 return;
1884         }
1885
1886         XMLNode* node = new XMLNode (X_("cut"));
1887         Route::ProcessorList to_cut;
1888
1889         no_processor_redisplay = true;
1890         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1891                 // Cut only plugins, sends and returns
1892                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1893                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1894                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1895
1896                         Window* w = get_processor_ui (*i);
1897
1898                         if (w) {
1899                                 w->hide ();
1900                         }
1901
1902                         XMLNode& child ((*i)->get_state());
1903                         node->add_child_nocopy (child);
1904                         to_cut.push_back (*i);
1905                 }
1906         }
1907
1908         if (_route->remove_processors (to_cut) != 0) {
1909                 delete node;
1910                 no_processor_redisplay = false;
1911                 return;
1912         }
1913
1914         _rr_selection.set (node);
1915
1916         no_processor_redisplay = false;
1917         redisplay_processors ();
1918 }
1919
1920 void
1921 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1922 {
1923         if (to_be_copied.empty()) {
1924                 return;
1925         }
1926
1927         XMLNode* node = new XMLNode (X_("copy"));
1928
1929         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1930                 // Copy only plugins, sends, returns
1931                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1932                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1933                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1934                         node->add_child_nocopy ((*i)->get_state());
1935                 }
1936         }
1937
1938         _rr_selection.set (node);
1939 }
1940
1941 void
1942 ProcessorBox::delete_processors (const ProcSelection& targets)
1943 {
1944         if (targets.empty()) {
1945                 return;
1946         }
1947
1948         no_processor_redisplay = true;
1949
1950         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1951
1952                 Window* w = get_processor_ui (*i);
1953
1954                 if (w) {
1955                         w->hide ();
1956                 }
1957
1958                 _route->remove_processor(*i);
1959         }
1960
1961         no_processor_redisplay = false;
1962         redisplay_processors ();
1963 }
1964
1965 void
1966 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1967 {
1968         list<boost::shared_ptr<Processor> >::const_iterator x;
1969
1970         no_processor_redisplay = true;
1971         for (x = procs.begin(); x != procs.end(); ++x) {
1972
1973                 Window* w = get_processor_ui (*x);
1974
1975                 if (w) {
1976                         w->hide ();
1977                 }
1978
1979                 _route->remove_processor(*x);
1980         }
1981
1982         no_processor_redisplay = false;
1983         redisplay_processors ();
1984 }
1985
1986 gint
1987 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1988 {
1989         boost::shared_ptr<Processor> processor (weak_processor.lock());
1990
1991         if (!processor) {
1992                 return false;
1993         }
1994
1995         /* NOT copied to _mixer.selection() */
1996
1997         no_processor_redisplay = true;
1998         _route->remove_processor (processor);
1999         no_processor_redisplay = false;
2000         redisplay_processors ();
2001
2002         return false;
2003 }
2004
2005 void
2006 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
2007 {
2008         ArdourPrompter name_prompter (true);
2009         string result;
2010         name_prompter.set_title (_("Rename Processor"));
2011         name_prompter.set_prompt (_("New name:"));
2012         name_prompter.set_initial_text (processor->name());
2013         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
2014         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
2015         name_prompter.show_all ();
2016
2017         switch (name_prompter.run ()) {
2018
2019         case Gtk::RESPONSE_ACCEPT:
2020                 name_prompter.get_result (result);
2021                 if (result.length()) {
2022
2023                        int tries = 0;
2024                        string test = result;
2025
2026                        while (tries < 100) {
2027                                if (_session->io_name_is_legal (test)) {
2028                                        result = test;
2029                                        break;
2030                                }
2031                                tries++;
2032
2033                                test = string_compose ("%1-%2", result, tries);
2034                        }
2035
2036                        if (tries < 100) {
2037                                processor->set_name (result);
2038                        } else {
2039                                /* unlikely! */
2040                                ARDOUR_UI::instance()->popup_error
2041                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
2042                        }
2043                 }
2044                 break;
2045         }
2046
2047         return;
2048 }
2049
2050 void
2051 ProcessorBox::paste_processors ()
2052 {
2053         if (_rr_selection.processors.empty()) {
2054                 return;
2055         }
2056
2057         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
2058 }
2059
2060 void
2061 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
2062 {
2063
2064         if (_rr_selection.processors.empty()) {
2065                 return;
2066         }
2067
2068         paste_processor_state (_rr_selection.processors.get_node().children(), before);
2069 }
2070
2071 void
2072 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
2073 {
2074         XMLNodeConstIterator niter;
2075         list<boost::shared_ptr<Processor> > copies;
2076
2077         if (nlist.empty()) {
2078                 return;
2079         }
2080
2081         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2082
2083                 XMLProperty const * type = (*niter)->property ("type");
2084                 XMLProperty const * role = (*niter)->property ("role");
2085                 assert (type);
2086
2087                 boost::shared_ptr<Processor> p;
2088                 try {
2089                         if (type->value() == "meter" ||
2090                             type->value() == "main-outs" ||
2091                             type->value() == "amp" ||
2092                             type->value() == "intreturn") {
2093                                 /* do not paste meter, main outs, amp or internal returns */
2094                                 continue;
2095
2096                         } else if (type->value() == "intsend") {
2097                                 
2098                                 /* aux sends are OK, but those used for
2099                                  * other purposes, are not.
2100                                  */
2101                                 
2102                                 assert (role);
2103
2104                                 if (role->value() != "Aux") {
2105                                         continue;
2106                                 }
2107
2108                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
2109                                 XMLNode n (**niter);
2110                                 InternalSend* s = new InternalSend (*_session, sendpan, _route->mute_master(),
2111                                                                     boost::shared_ptr<Route>(), Delivery::Aux); 
2112
2113                                 IOProcessor::prepare_for_reset (n, s->name());
2114
2115                                 if (s->set_state (n, Stateful::loading_state_version)) {
2116                                         delete s;
2117                                         return;
2118                                 }
2119
2120                                 p.reset (s);
2121
2122                         } else if (type->value() == "send") {
2123
2124                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
2125                                 XMLNode n (**niter);
2126
2127                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
2128
2129                                 IOProcessor::prepare_for_reset (n, s->name());
2130                                 
2131                                 if (s->set_state (n, Stateful::loading_state_version)) {
2132                                         delete s;
2133                                         return;
2134                                 }
2135
2136                                 p.reset (s);
2137
2138                         } else if (type->value() == "return") {
2139
2140                                 XMLNode n (**niter);
2141                                 Return* r = new Return (*_session);
2142
2143                                 IOProcessor::prepare_for_reset (n, r->name());
2144
2145                                 if (r->set_state (n, Stateful::loading_state_version)) {
2146                                         delete r;
2147                                         return;
2148                                 }
2149
2150                                 p.reset (r);
2151
2152                         } else if (type->value() == "port") {
2153
2154                                 XMLNode n (**niter);
2155                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
2156                                 
2157                                 IOProcessor::prepare_for_reset (n, pi->name());
2158                                 
2159                                 if (pi->set_state (n, Stateful::loading_state_version)) {
2160                                         return;
2161                                 }
2162                                 
2163                                 p.reset (pi);
2164
2165                         } else {
2166                                 /* XXX its a bit limiting to assume that everything else
2167                                    is a plugin.
2168                                 */
2169
2170                                 p.reset (new PluginInsert (*_session));
2171                                 p->set_state (**niter, Stateful::current_state_version);
2172                         }
2173
2174                         copies.push_back (p);
2175                 }
2176
2177                 catch (...) {
2178                         error << _("plugin insert constructor failed") << endmsg;
2179                 }
2180         }
2181
2182         if (copies.empty()) {
2183                 return;
2184         }
2185
2186         if (_route->add_processors (copies, p)) {
2187
2188                 string msg = _(
2189                         "Copying the set of processors on the clipboard failed,\n\
2190 probably because the I/O configuration of the plugins\n\
2191 could not match the configuration of this track.");
2192                 MessageDialog am (msg);
2193                 am.run ();
2194         }
2195 }
2196
2197 void
2198 ProcessorBox::get_selected_processors (ProcSelection& processors) const
2199 {
2200         const list<ProcessorEntry*> selection = processor_display.selection ();
2201         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
2202                 processors.push_back ((*i)->processor ());
2203         }
2204 }
2205
2206 void
2207 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
2208 {
2209         list<ProcessorEntry*> selection = processor_display.selection ();
2210         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
2211                 (this->*method) ((*i)->processor ());
2212         }
2213 }
2214
2215 void
2216 ProcessorBox::all_visible_processors_active (bool state)
2217 {
2218         _route->all_visible_processors_active (state);
2219 }
2220
2221 void
2222 ProcessorBox::ab_plugins ()
2223 {
2224         _route->ab_plugins (ab_direction);
2225         ab_direction = !ab_direction;
2226 }
2227
2228
2229 void
2230 ProcessorBox::clear_processors ()
2231 {
2232         string prompt;
2233         vector<string> choices;
2234
2235         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
2236                                    "(this cannot be undone)"), _route->name());
2237
2238         choices.push_back (_("Cancel"));
2239         choices.push_back (_("Yes, remove them all"));
2240
2241         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
2242
2243         if (prompter.run () == 1) {
2244                 _route->clear_processors (PreFader);
2245                 _route->clear_processors (PostFader);
2246         }
2247 }
2248
2249 void
2250 ProcessorBox::clear_processors (Placement p)
2251 {
2252         string prompt;
2253         vector<string> choices;
2254
2255         if (p == PreFader) {
2256                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
2257                                            "(this cannot be undone)"), _route->name());
2258         } else {
2259                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
2260                                            "(this cannot be undone)"), _route->name());
2261         }
2262
2263         choices.push_back (_("Cancel"));
2264         choices.push_back (_("Yes, remove them all"));
2265
2266         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
2267
2268         if (prompter.run () == 1) {
2269                 _route->clear_processors (p);
2270         }
2271 }
2272
2273 bool
2274 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
2275 {
2276         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
2277         if (at && at->freeze_state() == AudioTrack::Frozen) {
2278                 return false;
2279         }
2280
2281         if (
2282                 boost::dynamic_pointer_cast<Send> (processor) ||
2283                 boost::dynamic_pointer_cast<Return> (processor) ||
2284                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
2285                 boost::dynamic_pointer_cast<PortInsert> (processor)
2286                 ) {
2287                 return true;
2288         }
2289
2290         return false;
2291 }
2292
2293 bool
2294 ProcessorBox::one_processor_can_be_edited ()
2295 {
2296         list<ProcessorEntry*> selection = processor_display.selection ();
2297         list<ProcessorEntry*>::iterator i = selection.begin();
2298         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
2299                 ++i;
2300         }
2301
2302         return (i != selection.end());
2303 }
2304
2305 Gtk::Window*
2306 ProcessorBox::get_editor_window (boost::shared_ptr<Processor> processor, bool use_custom)
2307 {
2308         boost::shared_ptr<Send> send;
2309         boost::shared_ptr<InternalSend> internal_send;
2310         boost::shared_ptr<Return> retrn;
2311         boost::shared_ptr<PluginInsert> plugin_insert;
2312         boost::shared_ptr<PortInsert> port_insert;
2313         Window* gidget = 0;
2314
2315         /* This method may or may not return a Window, but if it does not it
2316          * will modify the parent mixer strip appearance layout to allow
2317          * "editing" the @param processor that was passed in.
2318          *
2319          * So for example, if the processor is an Amp (gain), the parent strip
2320          * will be forced back into a model where the fader controls the main gain.
2321          * If the processor is a send, then we map the send controls onto the
2322          * strip.
2323          * 
2324          * Plugins and others will return a window for control.
2325          */
2326
2327         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
2328
2329                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
2330                         return 0;
2331                 }
2332         }
2333
2334         if (boost::dynamic_pointer_cast<Amp> (processor)) {
2335
2336                 if (_parent_strip) {
2337                         _parent_strip->revert_to_default_display ();
2338                 }
2339
2340         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2341
2342                 if (!_session->engine().connected()) {
2343                         return 0;
2344                 }
2345
2346                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2347
2348                         gidget = new SendUIWindow (send, _session);
2349                 }
2350
2351         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
2352
2353                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
2354                         /* no GUI for these */
2355                         return 0;
2356                 }
2357
2358                 if (!_session->engine().connected()) {
2359                         return 0;
2360                 }
2361
2362                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
2363
2364                 ReturnUIWindow *return_ui;
2365                 Window* w = get_processor_ui (retrn);
2366
2367                 if (w == 0) {
2368
2369                         return_ui = new ReturnUIWindow (retrn, _session);
2370                         return_ui->set_title (retrn->name ());
2371                         set_processor_ui (send, return_ui);
2372
2373                 } else {
2374                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
2375                 }
2376
2377                 gidget = return_ui;
2378
2379         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2380
2381                 PluginUIWindow *plugin_ui;
2382
2383                 /* these are both allowed to be null */
2384
2385                 Window* w = get_processor_ui (plugin_insert);
2386
2387                 if (w == 0) {
2388                         plugin_ui = new PluginUIWindow (plugin_insert, false, use_custom);
2389                         plugin_ui->set_title (generate_processor_title (plugin_insert));
2390                         set_processor_ui (plugin_insert, plugin_ui);
2391
2392                 } else {
2393                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
2394                 }
2395
2396                 gidget = plugin_ui;
2397
2398         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
2399
2400                 if (!_session->engine().connected()) {
2401                         MessageDialog msg ( _("Not connected to audio engine - no I/O changes are possible"));
2402                         msg.run ();
2403                         return 0;
2404                 }
2405
2406                 PortInsertWindow *io_selector;
2407
2408                 Window* w = get_processor_ui (port_insert);
2409
2410                 if (w == 0) {
2411                         io_selector = new PortInsertWindow (_session, port_insert);
2412                         set_processor_ui (port_insert, io_selector);
2413
2414                 } else {
2415                         io_selector = dynamic_cast<PortInsertWindow *> (w);
2416                 }
2417
2418                 gidget = io_selector;
2419         }
2420
2421         return gidget;
2422 }
2423
2424 Gtk::Window*
2425 ProcessorBox::get_generic_editor_window (boost::shared_ptr<Processor> processor)
2426 {
2427         boost::shared_ptr<PluginInsert> plugin_insert
2428                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
2429
2430         if (!plugin_insert) {
2431                 return 0;
2432         }
2433
2434         PluginUIWindow* win = new PluginUIWindow (plugin_insert, true, false);
2435         win->set_title (generate_processor_title (plugin_insert));
2436
2437         return win;
2438 }
2439
2440 void
2441 ProcessorBox::register_actions ()
2442 {
2443         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("ProcessorMenu"));
2444         Glib::RefPtr<Action> act;
2445
2446         /* new stuff */
2447         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
2448                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
2449
2450         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
2451                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
2452         ActionManager::engine_sensitive_actions.push_back (act);
2453         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New External Send ..."),
2454                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
2455         ActionManager::engine_sensitive_actions.push_back (act);
2456
2457         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
2458
2459         ActionManager::register_action (popup_act_grp, X_("controls"), _("Controls"));
2460         ActionManager::register_action (popup_act_grp, X_("send_options"), _("Send Options"));
2461
2462         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
2463                         sigc::ptr_fun (ProcessorBox::rb_clear));
2464         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
2465                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
2466         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
2467                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
2468
2469         /* standard editing stuff */
2470         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
2471                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
2472         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
2473         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
2474                         sigc::ptr_fun (ProcessorBox::rb_copy));
2475         ActionManager::plugin_selection_sensitive_actions.push_back(act);
2476
2477         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
2478                         sigc::ptr_fun (ProcessorBox::rb_delete));
2479         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
2480
2481         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
2482                         sigc::ptr_fun (ProcessorBox::rb_paste));
2483         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
2484                         sigc::ptr_fun (ProcessorBox::rb_rename));
2485         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
2486                         sigc::ptr_fun (ProcessorBox::rb_select_all));
2487         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
2488                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
2489
2490         /* activation etc. */
2491
2492         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate All"),
2493                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
2494         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate All"),
2495                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
2496         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
2497                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
2498
2499         /* show editors */
2500         edit_action = ActionManager::register_action (
2501                 popup_act_grp, X_("edit"), _("Edit..."),
2502                 sigc::ptr_fun (ProcessorBox::rb_edit));
2503
2504         edit_generic_action = ActionManager::register_action (
2505                 popup_act_grp, X_("edit-generic"), _("Edit with generic controls..."),
2506                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
2507
2508         ActionManager::add_action_group (popup_act_grp);
2509 }
2510
2511 void
2512 ProcessorBox::rb_edit_generic ()
2513 {
2514         if (_current_processor_box == 0) {
2515                 return;
2516         }
2517
2518         _current_processor_box->for_selected_processors (&ProcessorBox::generic_edit_processor);
2519 }
2520
2521 void
2522 ProcessorBox::rb_ab_plugins ()
2523 {
2524         if (_current_processor_box == 0) {
2525                 return;
2526         }
2527
2528         _current_processor_box->ab_plugins ();
2529 }
2530
2531 void
2532 ProcessorBox::rb_choose_plugin ()
2533 {
2534         if (_current_processor_box == 0) {
2535                 return;
2536         }
2537         _current_processor_box->choose_plugin ();
2538 }
2539
2540 void
2541 ProcessorBox::rb_choose_insert ()
2542 {
2543         if (_current_processor_box == 0) {
2544                 return;
2545         }
2546         _current_processor_box->choose_insert ();
2547 }
2548
2549 void
2550 ProcessorBox::rb_choose_send ()
2551 {
2552         if (_current_processor_box == 0) {
2553                 return;
2554         }
2555         _current_processor_box->choose_send ();
2556 }
2557
2558 void
2559 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
2560 {
2561         if (_current_processor_box == 0) {
2562                 return;
2563         }
2564
2565         _current_processor_box->choose_aux (wr);
2566 }
2567
2568 void
2569 ProcessorBox::rb_clear ()
2570 {
2571         if (_current_processor_box == 0) {
2572                 return;
2573         }
2574
2575         _current_processor_box->clear_processors ();
2576 }
2577
2578
2579 void
2580 ProcessorBox::rb_clear_pre ()
2581 {
2582         if (_current_processor_box == 0) {
2583                 return;
2584         }
2585
2586         _current_processor_box->clear_processors (PreFader);
2587 }
2588
2589
2590 void
2591 ProcessorBox::rb_clear_post ()
2592 {
2593         if (_current_processor_box == 0) {
2594                 return;
2595         }
2596
2597         _current_processor_box->clear_processors (PostFader);
2598 }
2599
2600 void
2601 ProcessorBox::rb_cut ()
2602 {
2603         if (_current_processor_box == 0) {
2604                 return;
2605         }
2606
2607         _current_processor_box->processor_operation (ProcessorsCut);
2608 }
2609
2610 void
2611 ProcessorBox::rb_delete ()
2612 {
2613         if (_current_processor_box == 0) {
2614                 return;
2615         }
2616
2617         _current_processor_box->processor_operation (ProcessorsDelete);
2618 }
2619
2620 void
2621 ProcessorBox::rb_copy ()
2622 {
2623         if (_current_processor_box == 0) {
2624                 return;
2625         }
2626         _current_processor_box->processor_operation (ProcessorsCopy);
2627 }
2628
2629 void
2630 ProcessorBox::rb_paste ()
2631 {
2632         if (_current_processor_box == 0) {
2633                 return;
2634         }
2635
2636         _current_processor_box->processor_operation (ProcessorsPaste);
2637 }
2638
2639 void
2640 ProcessorBox::rb_rename ()
2641 {
2642         if (_current_processor_box == 0) {
2643                 return;
2644         }
2645         _current_processor_box->rename_processors ();
2646 }
2647
2648 void
2649 ProcessorBox::rb_select_all ()
2650 {
2651         if (_current_processor_box == 0) {
2652                 return;
2653         }
2654
2655         _current_processor_box->processor_operation (ProcessorsSelectAll);
2656 }
2657
2658 void
2659 ProcessorBox::rb_deselect_all ()
2660 {
2661         if (_current_processor_box == 0) {
2662                 return;
2663         }
2664
2665         _current_processor_box->deselect_all_processors ();
2666 }
2667
2668 void
2669 ProcessorBox::rb_activate_all ()
2670 {
2671         if (_current_processor_box == 0) {
2672                 return;
2673         }
2674
2675         _current_processor_box->all_visible_processors_active (true);
2676 }
2677
2678 void
2679 ProcessorBox::rb_deactivate_all ()
2680 {
2681         if (_current_processor_box == 0) {
2682                 return;
2683         }
2684         _current_processor_box->all_visible_processors_active (false);
2685 }
2686
2687 void
2688 ProcessorBox::rb_edit ()
2689 {
2690         if (_current_processor_box == 0) {
2691                 return;
2692         }
2693
2694         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
2695 }
2696
2697 bool
2698 ProcessorBox::edit_aux_send (boost::shared_ptr<Processor> processor)
2699 {
2700         if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2701                 return false;
2702         }
2703
2704         if (_parent_strip) {
2705                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
2706                 if (_parent_strip->current_delivery() == send) {
2707                         _parent_strip->revert_to_default_display ();
2708                 } else {
2709                         _parent_strip->show_send(send);
2710                 }
2711         }
2712         return true;
2713 }
2714
2715 void
2716 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
2717 {
2718         if (!processor) {
2719                 return;
2720         }
2721         if (edit_aux_send (processor)) {
2722                 return;
2723         }
2724
2725         ProcessorWindowProxy* proxy = find_window_proxy (processor);
2726
2727         if (proxy) {
2728                 proxy->set_custom_ui_mode (true);
2729                 proxy->toggle ();
2730         }
2731 }
2732
2733 void
2734 ProcessorBox::generic_edit_processor (boost::shared_ptr<Processor> processor)
2735 {
2736         if (!processor) {
2737                 return;
2738         }
2739         if (edit_aux_send (processor)) {
2740                 return;
2741         }
2742
2743         ProcessorWindowProxy* proxy = find_window_proxy (processor);
2744
2745         if (proxy) {
2746                 proxy->set_custom_ui_mode (false);
2747                 proxy->toggle ();
2748         }
2749 }
2750
2751 void
2752 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
2753 {
2754         if (!what_changed.contains (ARDOUR::Properties::name)) {
2755                 return;
2756         }
2757
2758         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
2759
2760         boost::shared_ptr<Processor> processor;
2761         boost::shared_ptr<PluginInsert> plugin_insert;
2762         boost::shared_ptr<Send> send;
2763
2764         list<ProcessorEntry*> children = processor_display.children();
2765
2766         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
2767
2768                 processor = (*iter)->processor ();
2769
2770                 if (!processor) {
2771                         continue;
2772                 }
2773
2774                 Window* w = get_processor_ui (processor);
2775
2776                 if (!w) {
2777                         continue;
2778                 }
2779
2780                 /* rename editor windows for sends and plugins */
2781
2782                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2783                         w->set_title (send->name ());
2784                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2785                         w->set_title (generate_processor_title (plugin_insert));
2786                 }
2787         }
2788 }
2789
2790 string
2791 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
2792 {
2793         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
2794         string::size_type email_pos;
2795
2796         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
2797                 maker = maker.substr (0, email_pos - 1);
2798         }
2799
2800         if (maker.length() > 32) {
2801                 maker = maker.substr (0, 32);
2802                 maker += " ...";
2803         }
2804
2805         SessionObject* owner = pi->owner();
2806
2807         if (owner) {
2808                 return string_compose(_("%1: %2 (by %3)"), owner->name(), pi->name(), maker);
2809         } else {
2810                 return string_compose(_("%1 (by %2)"), pi->name(), maker);
2811         }
2812 }
2813
2814 /** @param p Processor.
2815  *  @return the UI window for \a p.
2816  */
2817 Window *
2818 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
2819 {
2820         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_info.begin ();
2821         while (i != _processor_window_info.end()) {
2822                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2823                 if (t && t == p) {
2824                         return (*i)->get ();
2825                 }
2826
2827                 ++i;
2828         }
2829
2830         return 0;
2831 }
2832
2833 /** Make a note of the UI window that a processor is using.
2834  *  @param p Processor.
2835  *  @param w UI window.
2836  */
2837 void
2838 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2839 {
2840         list<ProcessorWindowProxy*>::iterator i = _processor_window_info.begin ();
2841
2842         p->set_ui (w);
2843
2844         while (i != _processor_window_info.end()) {
2845                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2846                 if (t && t == p) {
2847                         (*i)->use_window (*w);
2848                         return;
2849                 }
2850
2851                 ++i;
2852         }
2853
2854         /* we shouldn't get here, because the ProcessorUIList should always contain
2855            an entry for each processor.
2856         */
2857         assert (false);
2858 }
2859
2860 void
2861 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
2862 {
2863         boost::shared_ptr<Delivery> d = w.lock ();
2864         if (!d) {
2865                 return;
2866         }
2867
2868         list<ProcessorEntry*> children = processor_display.children ();
2869         list<ProcessorEntry*>::const_iterator i = children.begin();
2870         while (i != children.end() && (*i)->processor() != d) {
2871                 ++i;
2872         }
2873
2874         if (i == children.end()) {
2875                 processor_display.set_active (0);
2876         } else {
2877                 processor_display.set_active (*i);
2878         }
2879 }
2880
2881 /** Called to repair the damage of Editor::show_window doing a show_all() */
2882 void
2883 ProcessorBox::hide_things ()
2884 {
2885         list<ProcessorEntry*> c = processor_display.children ();
2886         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
2887                 (*i)->hide_things ();
2888         }
2889 }
2890
2891 void
2892 ProcessorBox::processor_menu_unmapped ()
2893 {
2894         processor_display.remove_placeholder ();
2895 }
2896
2897 XMLNode *
2898 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
2899 {
2900         if (!_parent_strip) {
2901                 return 0;
2902         }
2903
2904         GUIObjectState& st = _parent_strip->gui_object_state ();
2905         
2906         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
2907         assert (strip);
2908         return st.get_or_add_node (strip, entry->state_id());
2909 }
2910
2911 void
2912 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
2913 {
2914         XMLNode* proc = entry_gui_object_state (entry);
2915         if (!proc) {
2916                 return;
2917         }
2918
2919         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
2920         proc->remove_nodes_and_delete (X_("Object"));
2921         entry->add_control_state (proc);
2922 }
2923
2924 bool
2925 ProcessorBox::is_editor_mixer_strip() const
2926 {
2927         return _parent_strip && !_parent_strip->mixer_owned();
2928 }
2929
2930 ProcessorWindowProxy::ProcessorWindowProxy (string const & name, ProcessorBox* box, boost::weak_ptr<Processor> processor)
2931         : WM::ProxyBase (name, string())
2932         , marked (false)
2933         , _processor_box (box)
2934         , _processor (processor)
2935         , is_custom (false)
2936         , want_custom (false)
2937         , _valid (true)
2938 {
2939         boost::shared_ptr<Processor> p = _processor.lock ();
2940         if (!p) {
2941                 return;
2942         }
2943         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&ProcessorWindowProxy::processor_going_away, this), gui_context());
2944 }
2945
2946 ProcessorWindowProxy::~ProcessorWindowProxy()
2947 {
2948         /* processor window proxies do not own the windows they create with
2949          * ::get(), so set _window to null before the normal WindowProxy method
2950          * deletes it.
2951          */
2952         _window = 0;
2953 }
2954
2955 void
2956 ProcessorWindowProxy::processor_going_away ()
2957 {
2958         delete _window;
2959         _window = 0;
2960         _valid = false;
2961         /* should be no real reason to do this, since the object that would
2962            send DropReferences is about to be deleted, but lets do it anyway.
2963         */
2964         going_away_connection.disconnect();
2965 }
2966
2967 ARDOUR::SessionHandlePtr*
2968 ProcessorWindowProxy::session_handle() 
2969 {
2970         /* we don't care */
2971         return 0;
2972 }
2973
2974 bool
2975 ProcessorWindowProxy::valid() const
2976 {
2977         return _valid;
2978 }
2979
2980 XMLNode&
2981 ProcessorWindowProxy::get_state () const
2982 {
2983         XMLNode *node;
2984         node = &ProxyBase::get_state();
2985         node->add_property (X_("custom-ui"), is_custom? X_("yes") : X_("no"));
2986         return *node;
2987 }
2988
2989 void
2990 ProcessorWindowProxy::set_state (const XMLNode& node)
2991 {
2992         XMLNodeList children = node.children ();
2993         XMLNodeList::const_iterator i = children.begin ();
2994         while (i != children.end()) {
2995                 XMLProperty* prop = (*i)->property (X_("name"));
2996                 if ((*i)->name() == X_("Window") && prop && prop->value() == _name) {
2997                         break;
2998                 }
2999                 ++i;
3000         }
3001
3002         if (i != children.end()) {
3003                 XMLProperty* prop;
3004                 if ((prop = (*i)->property (X_("custom-ui"))) != 0) {
3005                         want_custom = PBD::string_is_affirmative (prop->value ());
3006                 }
3007         }
3008
3009         ProxyBase::set_state(node);
3010 }
3011
3012 Gtk::Window*
3013 ProcessorWindowProxy::get (bool create)
3014 {
3015         boost::shared_ptr<Processor> p = _processor.lock ();
3016
3017         if (!p) {
3018                 return 0;
3019         }
3020         if (_window && (is_custom != want_custom)) {
3021                 /* drop existing window - wrong type */
3022                 drop_window ();
3023         }
3024
3025         if (!_window) {
3026                 if (!create) {
3027                         return 0;
3028                 }
3029                 
3030                 is_custom = want_custom;
3031                 _window = _processor_box->get_editor_window (p, is_custom);
3032
3033                 if (_window) {
3034                         setup ();
3035                 }
3036         }
3037
3038         return _window;
3039 }
3040
3041 void
3042 ProcessorWindowProxy::toggle ()
3043 {
3044         if (_window && (is_custom != want_custom)) {
3045                 /* drop existing window - wrong type */
3046                 drop_window ();
3047         }
3048         is_custom = want_custom;
3049
3050         WM::ProxyBase::toggle ();
3051 }