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