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