visibility macros and flush() added to SrcFileSource; merge with master
[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         }
849
850         /* AUDIO */
851         const uint32_t audio_sources = _sources.n_audio();
852         const uint32_t audio_sinks = _sinks.n_audio();
853         cairo_set_source_rgb (cr,
854                         UINT_RGBA_R_FLT(audio_port_color),
855                         UINT_RGBA_G_FLT(audio_port_color),
856                         UINT_RGBA_B_FLT(audio_port_color));
857
858         if (_splitting) {
859                 assert(audio_sinks > 1);
860                 const float si_x0 = rintf(width * .5f) + .5f;
861                 for (uint32_t i = midi_sinks; i < sinks; ++i) {
862                         const float si_x = rintf(width * (.2f + .6f * i / (sinks - 1.f))) + .5f;
863                         cairo_move_to (cr, si_x, height);
864                         cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
865                         cairo_stroke (cr);
866                 }
867         } else if (audio_sources > 1) {
868                 for (uint32_t i = 0 ; i < audio_sources; ++i) {
869                         const float si_x = rintf(width * (.2f + .6f * (i + midi_sinks) / (sinks - 1.f))) + .5f;
870                         const float si_x0 = rintf(width * (.2f + .6f * (i + midi_sources) / (sources - 1.f))) + .5f;
871                         cairo_move_to (cr, si_x, height);
872                         cairo_curve_to (cr, si_x, 0, si_x0, height, si_x0, 0);
873                         cairo_stroke (cr);
874                 }
875         } else if (audio_sources == 1 && audio_sinks == 1) {
876                 const float si_x = rintf(width * .5f) + .5f;
877                 cairo_move_to (cr, si_x, height);
878                 cairo_line_to (cr, si_x, 0);
879                 cairo_stroke (cr);
880         }
881         cairo_destroy(cr);
882         return true;
883 }
884
885 ProcessorBox::ProcessorBox (ARDOUR::Session* sess, boost::function<PluginSelector*()> get_plugin_selector,
886                             RouteProcessorSelection& rsel, MixerStrip* parent, bool owner_is_mixer)
887         : _parent_strip (parent)
888         , _owner_is_mixer (owner_is_mixer)
889         , ab_direction (true)
890         , _get_plugin_selector (get_plugin_selector)
891         , _placement (-1)
892         , _visible_prefader_processors (0)
893         , _rr_selection(rsel)
894         , _redisplay_pending (false)
895
896 {
897         set_session (sess);
898
899         _width = Wide;
900         processor_menu = 0;
901         no_processor_redisplay = false;
902
903         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
904         processor_scroller.add (processor_display);
905         pack_start (processor_scroller, true, true);
906
907         processor_display.set_flags (CAN_FOCUS);
908         processor_display.set_name ("ProcessorList");
909         processor_display.set_data ("processorbox", this);
910         processor_display.set_size_request (48, -1);
911         processor_display.set_spacing (0);
912
913         processor_display.signal_enter_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::enter_notify), false);
914         processor_display.signal_leave_notify_event().connect (sigc::mem_fun(*this, &ProcessorBox::leave_notify), false);
915
916         processor_display.ButtonPress.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_press_event));
917         processor_display.ButtonRelease.connect (sigc::mem_fun (*this, &ProcessorBox::processor_button_release_event));
918
919         processor_display.Reordered.connect (sigc::mem_fun (*this, &ProcessorBox::reordered));
920         processor_display.DropFromAnotherBox.connect (sigc::mem_fun (*this, &ProcessorBox::object_drop));
921
922         processor_scroller.show ();
923         processor_display.show ();
924
925         if (parent) {
926                 parent->DeliveryChanged.connect (
927                         _mixer_strip_connections, invalidator (*this), boost::bind (&ProcessorBox::mixer_strip_delivery_changed, this, _1), gui_context ()
928                         );
929         }
930
931         ARDOUR_UI::instance()->set_tip (processor_display, _("Right-click to add/remove/edit\nplugins,inserts,sends and more"));
932 }
933
934 ProcessorBox::~ProcessorBox ()
935 {
936         /* it may appear as if we should delete processor_menu but that is a
937          * pointer to a widget owned by the UI Manager, and has potentially
938          * be returned to many other ProcessorBoxes. GTK doesn't really make
939          * clear the ownership of this widget, which is a menu and thus is
940          * never packed into any container other than an implict GtkWindow.
941          *
942          * For now, until or if we ever get clarification over the ownership
943          * story just let it continue to exist. At worst, its a small memory leak.
944          */
945 }
946
947 void
948 ProcessorBox::set_route (boost::shared_ptr<Route> r)
949 {
950         if (_route == r) {
951                 return;
952         }
953
954         _route_connections.drop_connections();
955
956         /* new route: any existing block on processor redisplay must be meaningless */
957         no_processor_redisplay = false;
958         _route = r;
959
960         _route->processors_changed.connect (
961                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_processors_changed, this, _1), gui_context()
962                 );
963
964         _route->DropReferences.connect (
965                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_going_away, this), gui_context()
966                 );
967
968         _route->PropertyChanged.connect (
969                 _route_connections, invalidator (*this), boost::bind (&ProcessorBox::route_property_changed, this, _1), gui_context()
970                 );
971
972         redisplay_processors ();
973 }
974
975 void
976 ProcessorBox::route_going_away ()
977 {
978         /* don't keep updating display as processors are deleted */
979         no_processor_redisplay = true;
980         _route.reset ();
981 }
982
983 void
984 ProcessorBox::object_drop(DnDVBox<ProcessorEntry>* source, ProcessorEntry* position, Glib::RefPtr<Gdk::DragContext> const & context)
985 {
986         boost::shared_ptr<Processor> p;
987         if (position) {
988                 p = position->processor ();
989                 if (!p) {
990                         /* dropped on the blank entry (which will be before the
991                            fader), so use the first non-blank child as our
992                            `dropped on' processor */
993                         list<ProcessorEntry*> c = processor_display.children ();
994                         list<ProcessorEntry*>::iterator i = c.begin ();
995
996                         assert (i != c.end ());
997                         p = (*i)->processor ();
998                         assert (p);
999                 }
1000         }
1001
1002         list<ProcessorEntry*> children = source->selection ();
1003         list<boost::shared_ptr<Processor> > procs;
1004         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
1005                 if ((*i)->processor ()) {
1006                         procs.push_back ((*i)->processor ());
1007                 }
1008         }
1009
1010         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
1011                 XMLNode& state = (*i)->get_state ();
1012                 XMLNodeList nlist;
1013                 nlist.push_back (&state);
1014                 paste_processor_state (nlist, p);
1015                 delete &state;
1016         }
1017
1018         /* since the dndvbox doesn't take care of this properly, we have to delete the originals
1019            ourselves.
1020         */
1021
1022         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
1023                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
1024                 if (other) {
1025                         other->delete_dragged_processors (procs);
1026                 }
1027         }
1028 }
1029
1030 void
1031 ProcessorBox::set_width (Width w)
1032 {
1033         if (_width == w) {
1034                 return;
1035         }
1036
1037         _width = w;
1038
1039         list<ProcessorEntry*> children = processor_display.children ();
1040         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1041                 (*i)->set_enum_width (w);
1042         }
1043
1044         queue_resize ();
1045 }
1046
1047 Gtk::Menu*
1048 ProcessorBox::build_possible_aux_menu ()
1049 {
1050         boost::shared_ptr<RouteList> rl = _session->get_routes_with_internal_returns();
1051
1052         if (rl->empty()) {
1053                 /* No aux sends if there are no busses */
1054                 return 0;
1055         }
1056
1057         using namespace Menu_Helpers;
1058         Menu* menu = manage (new Menu);
1059         MenuList& items = menu->items();
1060
1061         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
1062                 if (!_route->internal_send_for (*r) && *r != _route) {
1063                         items.push_back (MenuElem ((*r)->name(), sigc::bind (sigc::ptr_fun (ProcessorBox::rb_choose_aux), boost::weak_ptr<Route>(*r))));
1064                 }
1065         }
1066
1067         return menu;
1068 }
1069
1070 void
1071 ProcessorBox::show_processor_menu (int arg)
1072 {
1073         if (processor_menu == 0) {
1074                 processor_menu = build_processor_menu ();
1075                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
1076         }
1077
1078         /* Sort out the plugin submenu */
1079
1080         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
1081
1082         if (plugin_menu_item) {
1083                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
1084         }
1085
1086         /* And the aux submenu */
1087
1088         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
1089
1090         if (aux_menu_item) {
1091                 Menu* m = build_possible_aux_menu();
1092                 if (m && !m->items().empty()) {
1093                         aux_menu_item->set_submenu (*m);
1094                         aux_menu_item->set_sensitive (true);
1095                 } else {
1096                         /* stupid gtkmm: we need to pass a null reference here */
1097                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
1098                         aux_menu_item->set_sensitive (false);
1099                 }
1100         }
1101
1102         ProcessorEntry* single_selection = 0;
1103         if (processor_display.selection().size() == 1) {
1104                 single_selection = processor_display.selection().front();
1105         }
1106
1107         /* And the controls submenu */
1108
1109         Gtk::MenuItem* controls_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/controls"));
1110
1111         if (controls_menu_item) {
1112                 if (single_selection) {
1113                         Menu* m = single_selection->build_controls_menu ();
1114                         if (m && !m->items().empty()) {
1115                                 controls_menu_item->set_submenu (*m);
1116                                 controls_menu_item->set_sensitive (true);
1117                         } else {
1118                                 gtk_menu_item_set_submenu (controls_menu_item->gobj(), 0);
1119                                 controls_menu_item->set_sensitive (false);
1120                         }
1121                 }
1122         }
1123
1124         Gtk::MenuItem* send_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/send_options"));
1125         if (send_menu_item) {
1126                 if (single_selection) {
1127                         Menu* m = single_selection->build_send_options_menu ();
1128                         if (m && !m->items().empty()) {
1129                                 send_menu_item->set_submenu (*m);
1130                                 send_menu_item->set_sensitive (true);
1131                         } else {
1132                                 gtk_menu_item_set_submenu (send_menu_item->gobj(), 0);
1133                                 send_menu_item->set_sensitive (false);
1134                         }
1135                 }
1136         }
1137
1138         /* Sensitise actions as approprioate */
1139
1140         cut_action->set_sensitive (can_cut());
1141         paste_action->set_sensitive (!_rr_selection.processors.empty());
1142
1143         const bool sensitive = !processor_display.selection().empty();
1144         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
1145         edit_action->set_sensitive (one_processor_can_be_edited ());
1146         edit_generic_action->set_sensitive (one_processor_can_be_edited ());
1147
1148         boost::shared_ptr<PluginInsert> pi;
1149         if (single_selection) {
1150                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection->processor ());
1151         }
1152
1153         /* allow editing with an Ardour-generated UI for plugin inserts with editors */
1154         edit_action->set_sensitive (pi && pi->plugin()->has_editor ());
1155
1156         /* disallow rename for multiple selections, for plugin inserts and for the fader */
1157         rename_action->set_sensitive (single_selection && !pi && !boost::dynamic_pointer_cast<Amp> (single_selection->processor ()));
1158
1159         processor_menu->popup (1, arg);
1160
1161         /* Add a placeholder gap to the processor list to indicate where a processor would be
1162            inserted were one chosen from the menu.
1163         */
1164         int x, y;
1165         processor_display.get_pointer (x, y);
1166         _placement = processor_display.add_placeholder (y);
1167
1168         if (_visible_prefader_processors == 0 && _placement > 0) {
1169                 --_placement;
1170         }
1171 }
1172
1173 bool
1174 ProcessorBox::enter_notify (GdkEventCrossing*)
1175 {
1176         _current_processor_box = this;
1177         return false;
1178 }
1179
1180 bool
1181 ProcessorBox::leave_notify (GdkEventCrossing*)
1182 {
1183         return false;
1184 }
1185
1186 void
1187 ProcessorBox::processor_operation (ProcessorOperation op) 
1188 {
1189         ProcSelection targets;
1190
1191         get_selected_processors (targets);
1192
1193         if (targets.empty()) {
1194
1195                 int x, y;
1196                 processor_display.get_pointer (x, y);
1197
1198                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
1199
1200                 if (pointer.first && pointer.first->processor()) {
1201                         targets.push_back (pointer.first->processor ());
1202                 }
1203         }
1204
1205         switch (op) {
1206         case ProcessorsSelectAll:
1207                 processor_display.select_all ();
1208                 break;
1209
1210         case ProcessorsCopy:
1211                 copy_processors (targets);
1212                 break;
1213
1214         case ProcessorsCut:
1215                 cut_processors (targets);
1216                 break;
1217
1218         case ProcessorsPaste:
1219                 if (targets.empty()) {
1220                         paste_processors ();
1221                 } else {
1222                         paste_processors (targets.front());
1223                 }
1224                 break;
1225
1226         case ProcessorsDelete:
1227                 delete_processors (targets);
1228                 break;
1229
1230         case ProcessorsToggleActive:
1231                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
1232                         if ((*i)->active()) {
1233                                 (*i)->deactivate ();
1234                         } else {
1235                                 (*i)->activate ();
1236                         }
1237                 }
1238                 break;
1239
1240         case ProcessorsAB:
1241                 ab_plugins ();
1242                 break;
1243
1244         default:
1245                 break;
1246         }
1247 }
1248
1249 ProcessorWindowProxy* 
1250 ProcessorBox::find_window_proxy (boost::shared_ptr<Processor> processor) const
1251 {
1252         for (list<ProcessorWindowProxy*>::const_iterator i = _processor_window_info.begin(); i != _processor_window_info.end(); ++i) {
1253                 boost::shared_ptr<Processor> p = (*i)->processor().lock();
1254
1255                 if (p && p == processor) {
1256                         return (*i);
1257                 }
1258         }
1259
1260         return 0;
1261 }
1262
1263
1264
1265 bool
1266 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
1267 {
1268         boost::shared_ptr<Processor> processor;
1269         if (child) {
1270                 processor = child->processor ();
1271         }
1272
1273         int ret = false;
1274         bool selected = processor_display.selected (child);
1275
1276         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
1277
1278                 if (_session->engine().connected()) {
1279                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
1280
1281                         if (!one_processor_can_be_edited ()) return true;
1282
1283                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::SecondaryModifier)) {
1284                                 generic_edit_processor (processor);
1285                         } else {
1286                                 edit_processor (processor);
1287                         }
1288                 }
1289
1290                 ret = true;
1291
1292         } else if (processor && ev->button == 1 && selected) {
1293
1294                 // this is purely informational but necessary for route params UI
1295                 ProcessorSelected (processor); // emit
1296
1297         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
1298
1299                 choose_plugin ();
1300                 _get_plugin_selector()->show_manager ();
1301         }
1302
1303         return ret;
1304 }
1305
1306 bool
1307 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
1308 {
1309         boost::shared_ptr<Processor> processor;
1310         if (child) {
1311                 processor = child->processor ();
1312         }
1313
1314         if (processor && Keyboard::is_delete_event (ev)) {
1315
1316                 Glib::signal_idle().connect (sigc::bind (
1317                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
1318                                 boost::weak_ptr<Processor>(processor)));
1319
1320         } else if (Keyboard::is_context_menu_event (ev)) {
1321
1322                 show_processor_menu (ev->time);
1323
1324         } else if (processor && Keyboard::is_button2_event (ev)
1325 #ifndef GTKOSX
1326                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
1327 #endif
1328                 ) {
1329
1330                 /* button2-click with no/appropriate modifiers */
1331
1332                 if (processor->active()) {
1333                         processor->deactivate ();
1334                 } else {
1335                         processor->activate ();
1336                 }
1337         }
1338
1339         return false;
1340 }
1341
1342 Menu *
1343 ProcessorBox::build_processor_menu ()
1344 {
1345         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
1346         processor_menu->set_name ("ArdourContextMenu");
1347         return processor_menu;
1348 }
1349
1350 void
1351 ProcessorBox::select_all_processors ()
1352 {
1353         processor_display.select_all ();
1354 }
1355
1356 void
1357 ProcessorBox::deselect_all_processors ()
1358 {
1359         processor_display.select_none ();
1360 }
1361
1362 void
1363 ProcessorBox::choose_plugin ()
1364 {
1365         _get_plugin_selector()->set_interested_object (*this);
1366 }
1367
1368 /** @return true if an error occurred, otherwise false */
1369 bool
1370 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
1371 {
1372         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
1373
1374                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
1375
1376                 Route::ProcessorStreams err_streams;
1377
1378                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
1379                         weird_plugin_dialog (**p, err_streams);
1380                         return true;
1381                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
1382                 } else {
1383
1384                         if (Profile->get_sae()) {
1385                                 processor->activate ();
1386                         }
1387                 }
1388         }
1389
1390         return false;
1391 }
1392
1393 void
1394 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
1395 {
1396         ArdourDialog dialog (_("Plugin Incompatibility"));
1397         Label label;
1398
1399         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
1400                         p.name(), streams.index);
1401
1402         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
1403         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
1404
1405         text += _("\nThis plugin has:\n");
1406         if (has_midi) {
1407                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
1408                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
1409         }
1410         if (has_audio) {
1411                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
1412                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
1413         }
1414
1415         text += _("\nbut at the insertion point, there are:\n");
1416         if (has_midi) {
1417                 uint32_t const n = streams.count.n_midi ();
1418                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
1419         }
1420         if (has_audio) {
1421                 uint32_t const n = streams.count.n_audio ();
1422                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
1423         }
1424
1425         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
1426         label.set_text(text);
1427
1428         dialog.get_vbox()->pack_start (label);
1429         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1430
1431         dialog.set_name (X_("PluginIODialog"));
1432         dialog.set_modal (true);
1433         dialog.show_all ();
1434
1435         dialog.run ();
1436 }
1437
1438 void
1439 ProcessorBox::choose_insert ()
1440 {
1441         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
1442         _route->add_processor_by_index (processor, _placement);
1443 }
1444
1445 /* Caller must not hold process lock */
1446 void
1447 ProcessorBox::choose_send ()
1448 {
1449         boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
1450         boost::shared_ptr<Send> send (new Send (*_session, sendpan, _route->mute_master()));
1451
1452         /* make an educated guess at the initial number of outputs for the send */
1453         ChanCount outs = (_session->master_out())
1454                         ? _session->master_out()->n_outputs()
1455                         : _route->n_outputs();
1456
1457         /* XXX need processor lock on route */
1458         try {
1459                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1460                 send->output()->ensure_io (outs, false, this);
1461         } catch (AudioEngine::PortRegistrationFailure& err) {
1462                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
1463                 return;
1464         }
1465
1466         /* let the user adjust the IO setup before creation.
1467
1468            Note: this dialog is NOT modal - we just leave it to run and it will
1469            return when its Finished signal is emitted - typically when the window
1470            is closed.
1471          */
1472
1473         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
1474         ios->show ();
1475
1476         /* keep a reference to the send so it doesn't get deleted while
1477            the IOSelectorWindow is doing its stuff
1478         */
1479         _processor_being_created = send;
1480
1481         ios->selector().Finished.connect (sigc::bind (
1482                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
1483                         boost::weak_ptr<Processor>(send), ios));
1484
1485 }
1486
1487 void
1488 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1489 {
1490         boost::shared_ptr<Processor> processor (weak_processor.lock());
1491
1492         /* drop our temporary reference to the new send */
1493         _processor_being_created.reset ();
1494
1495         if (!processor) {
1496                 return;
1497         }
1498
1499         switch (r) {
1500         case IOSelector::Cancelled:
1501                 // processor will go away when all shared_ptrs to it vanish
1502                 break;
1503
1504         case IOSelector::Accepted:
1505                 _route->add_processor_by_index (processor, _placement);
1506                 if (Profile->get_sae()) {
1507                         processor->activate ();
1508                 }
1509                 break;
1510         }
1511
1512         delete_when_idle (ios);
1513 }
1514
1515 void
1516 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1517 {
1518         boost::shared_ptr<Processor> processor (weak_processor.lock());
1519
1520         /* drop our temporary reference to the new return */
1521         _processor_being_created.reset ();
1522
1523         if (!processor) {
1524                 return;
1525         }
1526
1527         switch (r) {
1528         case IOSelector::Cancelled:
1529                 // processor will go away when all shared_ptrs to it vanish
1530                 break;
1531
1532         case IOSelector::Accepted:
1533                 _route->add_processor_by_index (processor, _placement);
1534                 if (Profile->get_sae()) {
1535                         processor->activate ();
1536                 }
1537                 break;
1538         }
1539
1540         delete_when_idle (ios);
1541 }
1542
1543 void
1544 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
1545 {
1546         if (!_route) {
1547                 return;
1548         }
1549
1550         boost::shared_ptr<Route> target = wr.lock();
1551
1552         if (!target) {
1553                 return;
1554         }
1555
1556         _session->add_internal_send (target, _placement, _route);
1557 }
1558
1559 void
1560 ProcessorBox::route_processors_changed (RouteProcessorChange c)
1561 {
1562         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
1563                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
1564                 return;
1565         }
1566
1567         redisplay_processors ();
1568 }
1569
1570 void
1571 ProcessorBox::redisplay_processors ()
1572 {
1573         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
1574         bool     fader_seen;
1575
1576         if (no_processor_redisplay) {
1577                 return;
1578         }
1579
1580         processor_display.clear ();
1581
1582         _visible_prefader_processors = 0;
1583         fader_seen = false;
1584
1585         _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &ProcessorBox::help_count_visible_prefader_processors), 
1586                                                &_visible_prefader_processors, &fader_seen));
1587
1588         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
1589
1590         for (ProcessorWindowProxies::iterator i = _processor_window_info.begin(); i != _processor_window_info.end(); ++i) {
1591                 (*i)->marked = false;
1592         }
1593
1594         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
1595
1596         /* trim dead wood from the processor window proxy list */
1597
1598         ProcessorWindowProxies::iterator i = _processor_window_info.begin();
1599         while (i != _processor_window_info.end()) {
1600                 ProcessorWindowProxies::iterator j = i;
1601                 ++j;
1602
1603                 if (!(*i)->valid()) {
1604
1605                         WM::Manager::instance().remove (*i);
1606                         delete *i;
1607                         _processor_window_info.erase (i);
1608                         
1609                 } else if (!(*i)->marked) {
1610
1611                         /* this processor is no longer part of this processor
1612                          * box.
1613                          *
1614                          * that could be because it was deleted or it could be
1615                          * because the route being displayed in the parent
1616                          * strip changed.
1617                          *
1618                          * The latter only happens with the editor mixer strip.
1619                          */
1620
1621                         if (is_editor_mixer_strip()) {
1622                                 
1623                                 /* editor mixer strip .. DO NOTHING
1624                                  *
1625                                  * note: the processor window stays visible if
1626                                  * it is already visible
1627                                  */
1628                         } else {
1629                                 (*i)->hide ();
1630                                 WM::Manager::instance().remove (*i);
1631                                 delete *i;
1632                                 _processor_window_info.erase (i);
1633                         } 
1634                 } 
1635
1636                 i = j;
1637         }
1638
1639         setup_entry_positions ();
1640 }
1641
1642 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
1643  *  not already have one.
1644  */
1645 void
1646 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
1647 {
1648         boost::shared_ptr<Processor> p = w.lock ();
1649         if (!p) {
1650                 return;
1651         }
1652
1653         ProcessorWindowProxies::iterator i = _processor_window_info.begin ();
1654         while (i != _processor_window_info.end()) {
1655
1656                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1657
1658                 if (p == t) {
1659                         /* this processor is already on the list; done */
1660                         (*i)->marked = true;
1661                         return;
1662                 }
1663
1664                 ++i;
1665         }
1666
1667         /* not on the list; add it */
1668
1669         string loc;
1670         if (_parent_strip) {
1671                 if (_parent_strip->mixer_owned()) {
1672                         loc = X_("M");
1673                 } else {
1674                         loc = X_("R");
1675                 }
1676         } else {
1677                 loc = X_("P");
1678         }
1679
1680         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1681                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1682                 this,
1683                 w);
1684
1685         const XMLNode* ui_xml = _session->extra_xml (X_("UI"));
1686
1687         if (ui_xml) {
1688                 wp->set_state (*ui_xml);
1689         }
1690         
1691         wp->marked = true;
1692
1693         /* if the processor already has an existing UI,
1694            note that so that we don't recreate it
1695         */
1696
1697         void* existing_ui = p->get_ui ();
1698
1699         if (existing_ui) {
1700                 wp->use_window (*(reinterpret_cast<Gtk::Window*>(existing_ui)));
1701         }
1702
1703         _processor_window_info.push_back (wp);
1704         WM::Manager::instance().register_window (wp);
1705 }
1706
1707 void
1708 ProcessorBox::help_count_visible_prefader_processors (boost::weak_ptr<Processor> p, uint32_t* cnt, bool* amp_seen)
1709 {
1710         boost::shared_ptr<Processor> processor (p.lock ());
1711
1712         if (processor && processor->display_to_user()) {
1713
1714                 if (boost::dynamic_pointer_cast<Amp>(processor)) {
1715                         *amp_seen = true;
1716                 } else {
1717                         if (!*amp_seen) {
1718                                 (*cnt)++;
1719                         }
1720                 }
1721         }
1722 }
1723
1724 void
1725 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1726 {
1727         boost::shared_ptr<Processor> processor (p.lock ());
1728
1729         if (!processor || !processor->display_to_user()) {
1730                 return;
1731         }
1732
1733         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
1734         ProcessorEntry* e = 0;
1735         if (plugin_insert) {
1736                 e = new PluginInsertProcessorEntry (this, plugin_insert, _width);
1737         } else {
1738                 e = new ProcessorEntry (this, processor, _width);
1739         }
1740
1741         /* Set up this entry's state from the GUIObjectState */
1742         XMLNode* proc = entry_gui_object_state (e);
1743         if (proc) {
1744                 e->set_control_state (proc);
1745         }
1746         
1747         if (boost::dynamic_pointer_cast<Send> (processor)) {
1748                 /* Always show send controls */
1749                 e->show_all_controls ();
1750         }
1751
1752         processor_display.add_child (e);
1753 }
1754
1755 void
1756 ProcessorBox::reordered ()
1757 {
1758         compute_processor_sort_keys ();
1759         setup_entry_positions ();
1760 }
1761
1762 void
1763 ProcessorBox::setup_entry_positions ()
1764 {
1765         list<ProcessorEntry*> children = processor_display.children ();
1766         bool pre_fader = true;
1767
1768         uint32_t num = 0;
1769         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1770                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor())) {
1771                         pre_fader = false;
1772                         (*i)->set_position (ProcessorEntry::Fader, num++);
1773                 } else {
1774                         if (pre_fader) {
1775                                 (*i)->set_position (ProcessorEntry::PreFader, num++);
1776                         } else {
1777                                 (*i)->set_position (ProcessorEntry::PostFader, num++);
1778                         }
1779                 }
1780         }
1781 }
1782
1783 void
1784 ProcessorBox::compute_processor_sort_keys ()
1785 {
1786         list<ProcessorEntry*> children = processor_display.children ();
1787         Route::ProcessorList our_processors;
1788
1789         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1790                 if ((*i)->processor()) {
1791                         our_processors.push_back ((*i)->processor ());
1792                 }
1793         }
1794
1795         if (_route->reorder_processors (our_processors)) {
1796                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1797                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1798                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1799                    when the drag is torn down after this handler function is finished.
1800                 */
1801                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1802         }
1803 }
1804
1805 void
1806 ProcessorBox::report_failed_reorder ()
1807 {
1808         /* reorder failed, so redisplay */
1809
1810         redisplay_processors ();
1811
1812         /* now tell them about the problem */
1813
1814         ArdourDialog dialog (_("Plugin Incompatibility"));
1815         Label label;
1816
1817         label.set_text (_("\
1818 You cannot reorder these plugins/sends/inserts\n\
1819 in that way because the inputs and\n\
1820 outputs will not work correctly."));
1821
1822         dialog.get_vbox()->set_border_width (12);
1823         dialog.get_vbox()->pack_start (label);
1824         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1825
1826         dialog.set_name (X_("PluginIODialog"));
1827         dialog.set_modal (true);
1828         dialog.show_all ();
1829
1830         dialog.run ();
1831 }
1832
1833 void
1834 ProcessorBox::rename_processors ()
1835 {
1836         ProcSelection to_be_renamed;
1837
1838         get_selected_processors (to_be_renamed);
1839
1840         if (to_be_renamed.empty()) {
1841                 return;
1842         }
1843
1844         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1845                 rename_processor (*i);
1846         }
1847 }
1848
1849 bool
1850 ProcessorBox::can_cut () const
1851 {
1852         vector<boost::shared_ptr<Processor> > sel;
1853
1854         get_selected_processors (sel);
1855
1856         /* cut_processors () does not cut inserts */
1857
1858         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1859
1860                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1861                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1862                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1863                         return true;
1864                 }
1865         }
1866
1867         return false;
1868 }
1869
1870 void
1871 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1872 {
1873         if (to_be_removed.empty()) {
1874                 return;
1875         }
1876
1877         XMLNode* node = new XMLNode (X_("cut"));
1878         Route::ProcessorList to_cut;
1879
1880         no_processor_redisplay = true;
1881         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1882                 // Cut only plugins, sends and returns
1883                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1884                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1885                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1886
1887                         Window* w = get_processor_ui (*i);
1888
1889                         if (w) {
1890                                 w->hide ();
1891                         }
1892
1893                         XMLNode& child ((*i)->get_state());
1894                         node->add_child_nocopy (child);
1895                         to_cut.push_back (*i);
1896                 }
1897         }
1898
1899         if (_route->remove_processors (to_cut) != 0) {
1900                 delete node;
1901                 no_processor_redisplay = false;
1902                 return;
1903         }
1904
1905         _rr_selection.set (node);
1906
1907         no_processor_redisplay = false;
1908         redisplay_processors ();
1909 }
1910
1911 void
1912 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1913 {
1914         if (to_be_copied.empty()) {
1915                 return;
1916         }
1917
1918         XMLNode* node = new XMLNode (X_("copy"));
1919
1920         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1921                 // Copy only plugins, sends, returns
1922                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1923                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1924                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1925                         node->add_child_nocopy ((*i)->get_state());
1926                 }
1927         }
1928
1929         _rr_selection.set (node);
1930 }
1931
1932 void
1933 ProcessorBox::delete_processors (const ProcSelection& targets)
1934 {
1935         if (targets.empty()) {
1936                 return;
1937         }
1938
1939         no_processor_redisplay = true;
1940
1941         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1942
1943                 Window* w = get_processor_ui (*i);
1944
1945                 if (w) {
1946                         w->hide ();
1947                 }
1948
1949                 _route->remove_processor(*i);
1950         }
1951
1952         no_processor_redisplay = false;
1953         redisplay_processors ();
1954 }
1955
1956 void
1957 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1958 {
1959         list<boost::shared_ptr<Processor> >::const_iterator x;
1960
1961         no_processor_redisplay = true;
1962         for (x = procs.begin(); x != procs.end(); ++x) {
1963
1964                 Window* w = get_processor_ui (*x);
1965
1966                 if (w) {
1967                         w->hide ();
1968                 }
1969
1970                 _route->remove_processor(*x);
1971         }
1972
1973         no_processor_redisplay = false;
1974         redisplay_processors ();
1975 }
1976
1977 gint
1978 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1979 {
1980         boost::shared_ptr<Processor> processor (weak_processor.lock());
1981
1982         if (!processor) {
1983                 return false;
1984         }
1985
1986         /* NOT copied to _mixer.selection() */
1987
1988         no_processor_redisplay = true;
1989         _route->remove_processor (processor);
1990         no_processor_redisplay = false;
1991         redisplay_processors ();
1992
1993         return false;
1994 }
1995
1996 void
1997 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1998 {
1999         ArdourPrompter name_prompter (true);
2000         string result;
2001         name_prompter.set_title (_("Rename Processor"));
2002         name_prompter.set_prompt (_("New name:"));
2003         name_prompter.set_initial_text (processor->name());
2004         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
2005         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
2006         name_prompter.show_all ();
2007
2008         switch (name_prompter.run ()) {
2009
2010         case Gtk::RESPONSE_ACCEPT:
2011                 name_prompter.get_result (result);
2012                 if (result.length()) {
2013
2014                        int tries = 0;
2015                        string test = result;
2016
2017                        while (tries < 100) {
2018                                if (_session->io_name_is_legal (test)) {
2019                                        result = test;
2020                                        break;
2021                                }
2022                                tries++;
2023
2024                                test = string_compose ("%1-%2", result, tries);
2025                        }
2026
2027                        if (tries < 100) {
2028                                processor->set_name (result);
2029                        } else {
2030                                /* unlikely! */
2031                                ARDOUR_UI::instance()->popup_error
2032                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
2033                        }
2034                 }
2035                 break;
2036         }
2037
2038         return;
2039 }
2040
2041 void
2042 ProcessorBox::paste_processors ()
2043 {
2044         if (_rr_selection.processors.empty()) {
2045                 return;
2046         }
2047
2048         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
2049 }
2050
2051 void
2052 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
2053 {
2054
2055         if (_rr_selection.processors.empty()) {
2056                 return;
2057         }
2058
2059         paste_processor_state (_rr_selection.processors.get_node().children(), before);
2060 }
2061
2062 void
2063 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
2064 {
2065         XMLNodeConstIterator niter;
2066         list<boost::shared_ptr<Processor> > copies;
2067
2068         if (nlist.empty()) {
2069                 return;
2070         }
2071
2072         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2073
2074                 XMLProperty const * type = (*niter)->property ("type");
2075                 XMLProperty const * role = (*niter)->property ("role");
2076                 assert (type);
2077
2078                 boost::shared_ptr<Processor> p;
2079                 try {
2080                         if (type->value() == "meter" ||
2081                             type->value() == "main-outs" ||
2082                             type->value() == "amp" ||
2083                             type->value() == "intreturn") {
2084                                 /* do not paste meter, main outs, amp or internal returns */
2085                                 continue;
2086
2087                         } else if (type->value() == "intsend") {
2088                                 
2089                                 /* aux sends are OK, but those used for
2090                                  * other purposes, are not.
2091                                  */
2092                                 
2093                                 assert (role);
2094
2095                                 if (role->value() != "Aux") {
2096                                         continue;
2097                                 }
2098
2099                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
2100                                 XMLNode n (**niter);
2101                                 InternalSend* s = new InternalSend (*_session, sendpan, _route->mute_master(),
2102                                                                     boost::shared_ptr<Route>(), Delivery::Aux); 
2103
2104                                 IOProcessor::prepare_for_reset (n, s->name());
2105
2106                                 if (s->set_state (n, Stateful::loading_state_version)) {
2107                                         delete s;
2108                                         return;
2109                                 }
2110
2111                                 p.reset (s);
2112
2113                         } else if (type->value() == "send") {
2114
2115                                 boost::shared_ptr<Pannable> sendpan(new Pannable (*_session));
2116                                 XMLNode n (**niter);
2117
2118                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
2119
2120                                 IOProcessor::prepare_for_reset (n, s->name());
2121                                 
2122                                 if (s->set_state (n, Stateful::loading_state_version)) {
2123                                         delete s;
2124                                         return;
2125                                 }
2126
2127                                 p.reset (s);
2128
2129                         } else if (type->value() == "return") {
2130
2131                                 XMLNode n (**niter);
2132                                 Return* r = new Return (*_session);
2133
2134                                 IOProcessor::prepare_for_reset (n, r->name());
2135
2136                                 if (r->set_state (n, Stateful::loading_state_version)) {
2137                                         delete r;
2138                                         return;
2139                                 }
2140
2141                                 p.reset (r);
2142
2143                         } else if (type->value() == "port") {
2144
2145                                 XMLNode n (**niter);
2146                                 PortInsert* pi = new PortInsert (*_session, _route->pannable (), _route->mute_master ());
2147                                 
2148                                 IOProcessor::prepare_for_reset (n, pi->name());
2149                                 
2150                                 if (pi->set_state (n, Stateful::loading_state_version)) {
2151                                         return;
2152                                 }
2153                                 
2154                                 p.reset (pi);
2155
2156                         } else {
2157                                 /* XXX its a bit limiting to assume that everything else
2158                                    is a plugin.
2159                                 */
2160
2161                                 p.reset (new PluginInsert (*_session));
2162                                 p->set_state (**niter, Stateful::current_state_version);
2163                         }
2164
2165                         copies.push_back (p);
2166                 }
2167
2168                 catch (...) {
2169                         error << _("plugin insert constructor failed") << endmsg;
2170                 }
2171         }
2172
2173         if (copies.empty()) {
2174                 return;
2175         }
2176
2177         if (_route->add_processors (copies, p)) {
2178
2179                 string msg = _(
2180                         "Copying the set of processors on the clipboard failed,\n\
2181 probably because the I/O configuration of the plugins\n\
2182 could not match the configuration of this track.");
2183                 MessageDialog am (msg);
2184                 am.run ();
2185         }
2186 }
2187
2188 void
2189 ProcessorBox::get_selected_processors (ProcSelection& processors) const
2190 {
2191         const list<ProcessorEntry*> selection = processor_display.selection ();
2192         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
2193                 processors.push_back ((*i)->processor ());
2194         }
2195 }
2196
2197 void
2198 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
2199 {
2200         list<ProcessorEntry*> selection = processor_display.selection ();
2201         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
2202                 (this->*method) ((*i)->processor ());
2203         }
2204 }
2205
2206 void
2207 ProcessorBox::all_visible_processors_active (bool state)
2208 {
2209         _route->all_visible_processors_active (state);
2210 }
2211
2212 void
2213 ProcessorBox::ab_plugins ()
2214 {
2215         _route->ab_plugins (ab_direction);
2216         ab_direction = !ab_direction;
2217 }
2218
2219
2220 void
2221 ProcessorBox::clear_processors ()
2222 {
2223         string prompt;
2224         vector<string> choices;
2225
2226         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
2227                                    "(this cannot be undone)"), _route->name());
2228
2229         choices.push_back (_("Cancel"));
2230         choices.push_back (_("Yes, remove them all"));
2231
2232         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
2233
2234         if (prompter.run () == 1) {
2235                 _route->clear_processors (PreFader);
2236                 _route->clear_processors (PostFader);
2237         }
2238 }
2239
2240 void
2241 ProcessorBox::clear_processors (Placement p)
2242 {
2243         string prompt;
2244         vector<string> choices;
2245
2246         if (p == PreFader) {
2247                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
2248                                            "(this cannot be undone)"), _route->name());
2249         } else {
2250                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
2251                                            "(this cannot be undone)"), _route->name());
2252         }
2253
2254         choices.push_back (_("Cancel"));
2255         choices.push_back (_("Yes, remove them all"));
2256
2257         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
2258
2259         if (prompter.run () == 1) {
2260                 _route->clear_processors (p);
2261         }
2262 }
2263
2264 bool
2265 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
2266 {
2267         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
2268         if (at && at->freeze_state() == AudioTrack::Frozen) {
2269                 return false;
2270         }
2271
2272         if (
2273                 boost::dynamic_pointer_cast<Send> (processor) ||
2274                 boost::dynamic_pointer_cast<Return> (processor) ||
2275                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
2276                 boost::dynamic_pointer_cast<PortInsert> (processor)
2277                 ) {
2278                 return true;
2279         }
2280
2281         return false;
2282 }
2283
2284 bool
2285 ProcessorBox::one_processor_can_be_edited ()
2286 {
2287         list<ProcessorEntry*> selection = processor_display.selection ();
2288         list<ProcessorEntry*>::iterator i = selection.begin();
2289         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
2290                 ++i;
2291         }
2292
2293         return (i != selection.end());
2294 }
2295
2296 Gtk::Window*
2297 ProcessorBox::get_editor_window (boost::shared_ptr<Processor> processor, bool use_custom)
2298 {
2299         boost::shared_ptr<Send> send;
2300         boost::shared_ptr<InternalSend> internal_send;
2301         boost::shared_ptr<Return> retrn;
2302         boost::shared_ptr<PluginInsert> plugin_insert;
2303         boost::shared_ptr<PortInsert> port_insert;
2304         Window* gidget = 0;
2305
2306         /* This method may or may not return a Window, but if it does not it
2307          * will modify the parent mixer strip appearance layout to allow
2308          * "editing" the @param processor that was passed in.
2309          *
2310          * So for example, if the processor is an Amp (gain), the parent strip
2311          * will be forced back into a model where the fader controls the main gain.
2312          * If the processor is a send, then we map the send controls onto the
2313          * strip.
2314          * 
2315          * Plugins and others will return a window for control.
2316          */
2317
2318         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
2319
2320                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
2321                         return 0;
2322                 }
2323         }
2324
2325         if (boost::dynamic_pointer_cast<Amp> (processor)) {
2326
2327                 if (_parent_strip) {
2328                         _parent_strip->revert_to_default_display ();
2329                 }
2330
2331         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2332
2333                 if (!_session->engine().connected()) {
2334                         return 0;
2335                 }
2336
2337                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2338
2339                         gidget = new SendUIWindow (send, _session);
2340                 }
2341
2342         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
2343
2344                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
2345                         /* no GUI for these */
2346                         return 0;
2347                 }
2348
2349                 if (!_session->engine().connected()) {
2350                         return 0;
2351                 }
2352
2353                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
2354
2355                 ReturnUIWindow *return_ui;
2356                 Window* w = get_processor_ui (retrn);
2357
2358                 if (w == 0) {
2359
2360                         return_ui = new ReturnUIWindow (retrn, _session);
2361                         return_ui->set_title (retrn->name ());
2362                         set_processor_ui (send, return_ui);
2363
2364                 } else {
2365                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
2366                 }
2367
2368                 gidget = return_ui;
2369
2370         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2371
2372                 PluginUIWindow *plugin_ui;
2373
2374                 /* these are both allowed to be null */
2375
2376                 Window* w = get_processor_ui (plugin_insert);
2377
2378                 if (w == 0) {
2379                         plugin_ui = new PluginUIWindow (plugin_insert, false, use_custom);
2380                         plugin_ui->set_title (generate_processor_title (plugin_insert));
2381                         set_processor_ui (plugin_insert, plugin_ui);
2382
2383                 } else {
2384                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
2385                 }
2386
2387                 gidget = plugin_ui;
2388
2389         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
2390
2391                 if (!_session->engine().connected()) {
2392                         MessageDialog msg ( _("Not connected to audio engine - no I/O changes are possible"));
2393                         msg.run ();
2394                         return 0;
2395                 }
2396
2397                 PortInsertWindow *io_selector;
2398
2399                 Window* w = get_processor_ui (port_insert);
2400
2401                 if (w == 0) {
2402                         io_selector = new PortInsertWindow (_session, port_insert);
2403                         set_processor_ui (port_insert, io_selector);
2404
2405                 } else {
2406                         io_selector = dynamic_cast<PortInsertWindow *> (w);
2407                 }
2408
2409                 gidget = io_selector;
2410         }
2411
2412         return gidget;
2413 }
2414
2415 Gtk::Window*
2416 ProcessorBox::get_generic_editor_window (boost::shared_ptr<Processor> processor)
2417 {
2418         boost::shared_ptr<PluginInsert> plugin_insert
2419                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
2420
2421         if (!plugin_insert) {
2422                 return 0;
2423         }
2424
2425         PluginUIWindow* win = new PluginUIWindow (plugin_insert, true, false);
2426         win->set_title (generate_processor_title (plugin_insert));
2427
2428         return win;
2429 }
2430
2431 void
2432 ProcessorBox::register_actions ()
2433 {
2434         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("ProcessorMenu"));
2435         Glib::RefPtr<Action> act;
2436
2437         /* new stuff */
2438         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
2439                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
2440
2441         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
2442                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
2443         ActionManager::engine_sensitive_actions.push_back (act);
2444         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New External Send ..."),
2445                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
2446         ActionManager::engine_sensitive_actions.push_back (act);
2447
2448         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
2449
2450         ActionManager::register_action (popup_act_grp, X_("controls"), _("Controls"));
2451         ActionManager::register_action (popup_act_grp, X_("send_options"), _("Send Options"));
2452
2453         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
2454                         sigc::ptr_fun (ProcessorBox::rb_clear));
2455         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
2456                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
2457         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
2458                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
2459
2460         /* standard editing stuff */
2461         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
2462                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
2463         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
2464         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
2465                         sigc::ptr_fun (ProcessorBox::rb_copy));
2466         ActionManager::plugin_selection_sensitive_actions.push_back(act);
2467
2468         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
2469                         sigc::ptr_fun (ProcessorBox::rb_delete));
2470         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
2471
2472         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
2473                         sigc::ptr_fun (ProcessorBox::rb_paste));
2474         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
2475                         sigc::ptr_fun (ProcessorBox::rb_rename));
2476         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
2477                         sigc::ptr_fun (ProcessorBox::rb_select_all));
2478         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
2479                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
2480
2481         /* activation etc. */
2482
2483         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate All"),
2484                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
2485         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate All"),
2486                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
2487         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
2488                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
2489
2490         /* show editors */
2491         edit_action = ActionManager::register_action (
2492                 popup_act_grp, X_("edit"), _("Edit..."),
2493                 sigc::ptr_fun (ProcessorBox::rb_edit));
2494
2495         edit_generic_action = ActionManager::register_action (
2496                 popup_act_grp, X_("edit-generic"), _("Edit with generic controls..."),
2497                 sigc::ptr_fun (ProcessorBox::rb_edit_generic));
2498
2499         ActionManager::add_action_group (popup_act_grp);
2500 }
2501
2502 void
2503 ProcessorBox::rb_edit_generic ()
2504 {
2505         if (_current_processor_box == 0) {
2506                 return;
2507         }
2508
2509         _current_processor_box->for_selected_processors (&ProcessorBox::generic_edit_processor);
2510 }
2511
2512 void
2513 ProcessorBox::rb_ab_plugins ()
2514 {
2515         if (_current_processor_box == 0) {
2516                 return;
2517         }
2518
2519         _current_processor_box->ab_plugins ();
2520 }
2521
2522 void
2523 ProcessorBox::rb_choose_plugin ()
2524 {
2525         if (_current_processor_box == 0) {
2526                 return;
2527         }
2528         _current_processor_box->choose_plugin ();
2529 }
2530
2531 void
2532 ProcessorBox::rb_choose_insert ()
2533 {
2534         if (_current_processor_box == 0) {
2535                 return;
2536         }
2537         _current_processor_box->choose_insert ();
2538 }
2539
2540 void
2541 ProcessorBox::rb_choose_send ()
2542 {
2543         if (_current_processor_box == 0) {
2544                 return;
2545         }
2546         _current_processor_box->choose_send ();
2547 }
2548
2549 void
2550 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
2551 {
2552         if (_current_processor_box == 0) {
2553                 return;
2554         }
2555
2556         _current_processor_box->choose_aux (wr);
2557 }
2558
2559 void
2560 ProcessorBox::rb_clear ()
2561 {
2562         if (_current_processor_box == 0) {
2563                 return;
2564         }
2565
2566         _current_processor_box->clear_processors ();
2567 }
2568
2569
2570 void
2571 ProcessorBox::rb_clear_pre ()
2572 {
2573         if (_current_processor_box == 0) {
2574                 return;
2575         }
2576
2577         _current_processor_box->clear_processors (PreFader);
2578 }
2579
2580
2581 void
2582 ProcessorBox::rb_clear_post ()
2583 {
2584         if (_current_processor_box == 0) {
2585                 return;
2586         }
2587
2588         _current_processor_box->clear_processors (PostFader);
2589 }
2590
2591 void
2592 ProcessorBox::rb_cut ()
2593 {
2594         if (_current_processor_box == 0) {
2595                 return;
2596         }
2597
2598         _current_processor_box->processor_operation (ProcessorsCut);
2599 }
2600
2601 void
2602 ProcessorBox::rb_delete ()
2603 {
2604         if (_current_processor_box == 0) {
2605                 return;
2606         }
2607
2608         _current_processor_box->processor_operation (ProcessorsDelete);
2609 }
2610
2611 void
2612 ProcessorBox::rb_copy ()
2613 {
2614         if (_current_processor_box == 0) {
2615                 return;
2616         }
2617         _current_processor_box->processor_operation (ProcessorsCopy);
2618 }
2619
2620 void
2621 ProcessorBox::rb_paste ()
2622 {
2623         if (_current_processor_box == 0) {
2624                 return;
2625         }
2626
2627         _current_processor_box->processor_operation (ProcessorsPaste);
2628 }
2629
2630 void
2631 ProcessorBox::rb_rename ()
2632 {
2633         if (_current_processor_box == 0) {
2634                 return;
2635         }
2636         _current_processor_box->rename_processors ();
2637 }
2638
2639 void
2640 ProcessorBox::rb_select_all ()
2641 {
2642         if (_current_processor_box == 0) {
2643                 return;
2644         }
2645
2646         _current_processor_box->processor_operation (ProcessorsSelectAll);
2647 }
2648
2649 void
2650 ProcessorBox::rb_deselect_all ()
2651 {
2652         if (_current_processor_box == 0) {
2653                 return;
2654         }
2655
2656         _current_processor_box->deselect_all_processors ();
2657 }
2658
2659 void
2660 ProcessorBox::rb_activate_all ()
2661 {
2662         if (_current_processor_box == 0) {
2663                 return;
2664         }
2665
2666         _current_processor_box->all_visible_processors_active (true);
2667 }
2668
2669 void
2670 ProcessorBox::rb_deactivate_all ()
2671 {
2672         if (_current_processor_box == 0) {
2673                 return;
2674         }
2675         _current_processor_box->all_visible_processors_active (false);
2676 }
2677
2678 void
2679 ProcessorBox::rb_edit ()
2680 {
2681         if (_current_processor_box == 0) {
2682                 return;
2683         }
2684
2685         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
2686 }
2687
2688 bool
2689 ProcessorBox::edit_aux_send (boost::shared_ptr<Processor> processor)
2690 {
2691         if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
2692                 return false;
2693         }
2694
2695         if (_parent_strip) {
2696                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
2697                 if (_parent_strip->current_delivery() == send) {
2698                         _parent_strip->revert_to_default_display ();
2699                 } else {
2700                         _parent_strip->show_send(send);
2701                 }
2702         }
2703         return true;
2704 }
2705
2706 void
2707 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
2708 {
2709         if (!processor) {
2710                 return;
2711         }
2712         if (edit_aux_send (processor)) {
2713                 return;
2714         }
2715
2716         ProcessorWindowProxy* proxy = find_window_proxy (processor);
2717
2718         if (proxy) {
2719                 proxy->set_custom_ui_mode (true);
2720                 proxy->toggle ();
2721         }
2722 }
2723
2724 void
2725 ProcessorBox::generic_edit_processor (boost::shared_ptr<Processor> processor)
2726 {
2727         if (!processor) {
2728                 return;
2729         }
2730         if (edit_aux_send (processor)) {
2731                 return;
2732         }
2733
2734         ProcessorWindowProxy* proxy = find_window_proxy (processor);
2735
2736         if (proxy) {
2737                 proxy->set_custom_ui_mode (false);
2738                 proxy->toggle ();
2739         }
2740 }
2741
2742 void
2743 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
2744 {
2745         if (!what_changed.contains (ARDOUR::Properties::name)) {
2746                 return;
2747         }
2748
2749         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
2750
2751         boost::shared_ptr<Processor> processor;
2752         boost::shared_ptr<PluginInsert> plugin_insert;
2753         boost::shared_ptr<Send> send;
2754
2755         list<ProcessorEntry*> children = processor_display.children();
2756
2757         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
2758
2759                 processor = (*iter)->processor ();
2760
2761                 if (!processor) {
2762                         continue;
2763                 }
2764
2765                 Window* w = get_processor_ui (processor);
2766
2767                 if (!w) {
2768                         continue;
2769                 }
2770
2771                 /* rename editor windows for sends and plugins */
2772
2773                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2774                         w->set_title (send->name ());
2775                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2776                         w->set_title (generate_processor_title (plugin_insert));
2777                 }
2778         }
2779 }
2780
2781 string
2782 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
2783 {
2784         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
2785         string::size_type email_pos;
2786
2787         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
2788                 maker = maker.substr (0, email_pos - 1);
2789         }
2790
2791         if (maker.length() > 32) {
2792                 maker = maker.substr (0, 32);
2793                 maker += " ...";
2794         }
2795
2796         SessionObject* owner = pi->owner();
2797
2798         if (owner) {
2799                 return string_compose(_("%1: %2 (by %3)"), owner->name(), pi->name(), maker);
2800         } else {
2801                 return string_compose(_("%1 (by %2)"), pi->name(), maker);
2802         }
2803 }
2804
2805 /** @param p Processor.
2806  *  @return the UI window for \a p.
2807  */
2808 Window *
2809 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
2810 {
2811         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_info.begin ();
2812         while (i != _processor_window_info.end()) {
2813                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2814                 if (t && t == p) {
2815                         return (*i)->get ();
2816                 }
2817
2818                 ++i;
2819         }
2820
2821         return 0;
2822 }
2823
2824 /** Make a note of the UI window that a processor is using.
2825  *  @param p Processor.
2826  *  @param w UI window.
2827  */
2828 void
2829 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2830 {
2831         list<ProcessorWindowProxy*>::iterator i = _processor_window_info.begin ();
2832
2833         p->set_ui (w);
2834
2835         while (i != _processor_window_info.end()) {
2836                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2837                 if (t && t == p) {
2838                         (*i)->use_window (*w);
2839                         return;
2840                 }
2841
2842                 ++i;
2843         }
2844
2845         /* we shouldn't get here, because the ProcessorUIList should always contain
2846            an entry for each processor.
2847         */
2848         assert (false);
2849 }
2850
2851 void
2852 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
2853 {
2854         boost::shared_ptr<Delivery> d = w.lock ();
2855         if (!d) {
2856                 return;
2857         }
2858
2859         list<ProcessorEntry*> children = processor_display.children ();
2860         list<ProcessorEntry*>::const_iterator i = children.begin();
2861         while (i != children.end() && (*i)->processor() != d) {
2862                 ++i;
2863         }
2864
2865         if (i == children.end()) {
2866                 processor_display.set_active (0);
2867         } else {
2868                 processor_display.set_active (*i);
2869         }
2870 }
2871
2872 /** Called to repair the damage of Editor::show_window doing a show_all() */
2873 void
2874 ProcessorBox::hide_things ()
2875 {
2876         list<ProcessorEntry*> c = processor_display.children ();
2877         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
2878                 (*i)->hide_things ();
2879         }
2880 }
2881
2882 void
2883 ProcessorBox::processor_menu_unmapped ()
2884 {
2885         processor_display.remove_placeholder ();
2886 }
2887
2888 XMLNode *
2889 ProcessorBox::entry_gui_object_state (ProcessorEntry* entry)
2890 {
2891         if (!_parent_strip) {
2892                 return 0;
2893         }
2894
2895         GUIObjectState& st = _parent_strip->gui_object_state ();
2896         
2897         XMLNode* strip = st.get_or_add_node (_parent_strip->state_id ());
2898         assert (strip);
2899         return st.get_or_add_node (strip, entry->state_id());
2900 }
2901
2902 void
2903 ProcessorBox::update_gui_object_state (ProcessorEntry* entry)
2904 {
2905         XMLNode* proc = entry_gui_object_state (entry);
2906         if (!proc) {
2907                 return;
2908         }
2909
2910         /* XXX: this is a bit inefficient; we just remove all child nodes and re-add them */
2911         proc->remove_nodes_and_delete (X_("Object"));
2912         entry->add_control_state (proc);
2913 }
2914
2915 bool
2916 ProcessorBox::is_editor_mixer_strip() const
2917 {
2918         return _parent_strip && !_parent_strip->mixer_owned();
2919 }
2920
2921 ProcessorWindowProxy::ProcessorWindowProxy (string const & name, ProcessorBox* box, boost::weak_ptr<Processor> processor)
2922         : WM::ProxyBase (name, string())
2923         , marked (false)
2924         , _processor_box (box)
2925         , _processor (processor)
2926         , is_custom (false)
2927         , want_custom (false)
2928         , _valid (true)
2929 {
2930         boost::shared_ptr<Processor> p = _processor.lock ();
2931         if (!p) {
2932                 return;
2933         }
2934         p->DropReferences.connect (going_away_connection, MISSING_INVALIDATOR, boost::bind (&ProcessorWindowProxy::processor_going_away, this), gui_context());
2935 }
2936
2937 ProcessorWindowProxy::~ProcessorWindowProxy()
2938 {
2939         /* processor window proxies do not own the windows they create with
2940          * ::get(), so set _window to null before the normal WindowProxy method
2941          * deletes it.
2942          */
2943         _window = 0;
2944 }
2945
2946 void
2947 ProcessorWindowProxy::processor_going_away ()
2948 {
2949         delete _window;
2950         _window = 0;
2951         _valid = false;
2952         /* should be no real reason to do this, since the object that would
2953            send DropReferences is about to be deleted, but lets do it anyway.
2954         */
2955         going_away_connection.disconnect();
2956 }
2957
2958 ARDOUR::SessionHandlePtr*
2959 ProcessorWindowProxy::session_handle() 
2960 {
2961         /* we don't care */
2962         return 0;
2963 }
2964
2965 bool
2966 ProcessorWindowProxy::valid() const
2967 {
2968         return _valid;
2969 }
2970
2971 XMLNode&
2972 ProcessorWindowProxy::get_state () const
2973 {
2974         XMLNode *node;
2975         node = &ProxyBase::get_state();
2976         node->add_property (X_("custom-ui"), is_custom? X_("yes") : X_("no"));
2977         return *node;
2978 }
2979
2980 void
2981 ProcessorWindowProxy::set_state (const XMLNode& node)
2982 {
2983         XMLNodeList children = node.children ();
2984         XMLNodeList::const_iterator i = children.begin ();
2985         while (i != children.end()) {
2986                 XMLProperty* prop = (*i)->property (X_("name"));
2987                 if ((*i)->name() == X_("Window") && prop && prop->value() == _name) {
2988                         break;
2989                 }
2990                 ++i;
2991         }
2992
2993         if (i != children.end()) {
2994                 XMLProperty* prop;
2995                 if ((prop = (*i)->property (X_("custom-ui"))) != 0) {
2996                         want_custom = PBD::string_is_affirmative (prop->value ());
2997                 }
2998         }
2999
3000         ProxyBase::set_state(node);
3001 }
3002
3003 Gtk::Window*
3004 ProcessorWindowProxy::get (bool create)
3005 {
3006         boost::shared_ptr<Processor> p = _processor.lock ();
3007
3008         if (!p) {
3009                 return 0;
3010         }
3011         if (_window && (is_custom != want_custom)) {
3012                 /* drop existing window - wrong type */
3013                 drop_window ();
3014         }
3015
3016         if (!_window) {
3017                 if (!create) {
3018                         return 0;
3019                 }
3020                 
3021                 is_custom = want_custom;
3022                 _window = _processor_box->get_editor_window (p, is_custom);
3023
3024                 if (_window) {
3025                         setup ();
3026                 }
3027         }
3028
3029         return _window;
3030 }
3031
3032 void
3033 ProcessorWindowProxy::toggle ()
3034 {
3035         if (_window && (is_custom != want_custom)) {
3036                 /* drop existing window - wrong type */
3037                 drop_window ();
3038         }
3039         is_custom = want_custom;
3040
3041         WM::ProxyBase::toggle ();
3042 }