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