patch from colinf to make dbl-click on send "box" map its controls to the strip;...
[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_processor_menu (int arg)
661 {
662         if (processor_menu == 0) {
663                 processor_menu = build_processor_menu ();
664                 processor_menu->signal_unmap().connect (sigc::mem_fun (*this, &ProcessorBox::processor_menu_unmapped));
665         }
666
667         /* Sort out the plugin submenu */
668
669         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newplugin"));
670
671         if (plugin_menu_item) {
672                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
673         }
674
675         /* And the aux submenu */
676
677         Gtk::MenuItem* aux_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/ProcessorMenu/newaux"));
678
679         if (aux_menu_item) {
680                 Menu* m = build_possible_aux_menu();
681                 if (m && !m->items().empty()) {
682                         aux_menu_item->set_submenu (*m);
683                         aux_menu_item->set_sensitive (true);
684                 } else {
685                         /* stupid gtkmm: we need to pass a null reference here */
686                         gtk_menu_item_set_submenu (aux_menu_item->gobj(), 0);
687                         aux_menu_item->set_sensitive (false);
688                 }
689         }
690
691         /* Sensitise actions as approprioate */
692
693         cut_action->set_sensitive (can_cut());
694         paste_action->set_sensitive (!_rr_selection.processors.empty());
695
696         const bool sensitive = !processor_display.selection().empty();
697         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
698         edit_action->set_sensitive (one_processor_can_be_edited ());
699
700         boost::shared_ptr<Processor> single_selection;
701         if (processor_display.selection().size() == 1) {
702                 single_selection = processor_display.selection().front()->processor ();
703         }
704
705         boost::shared_ptr<PluginInsert> pi;
706         if (single_selection) {
707                 pi = boost::dynamic_pointer_cast<PluginInsert> (single_selection);
708         }
709
710         /* enable gui for plugin inserts with editors */
711         controls_action->set_sensitive(pi && pi->plugin()->has_editor());
712
713         /* disallow rename for multiple selections, for plugin inserts and for the fader */
714         rename_action->set_sensitive (single_selection && !pi && !boost::dynamic_pointer_cast<Amp> (single_selection));
715
716         processor_menu->popup (1, arg);
717
718         /* Add a placeholder gap to the processor list to indicate where a processor would be
719            inserted were one chosen from the menu.
720         */
721         int x, y;
722         processor_display.get_pointer (x, y);
723         _placement = processor_display.add_placeholder (y);
724
725         if (_visible_prefader_processors == 0) {
726                 if (_placement == 1) {
727                         _placement = 0;
728                 }
729         }
730 }
731
732 bool
733 ProcessorBox::enter_notify (GdkEventCrossing*)
734 {
735         _current_processor_box = this;
736         return false;
737 }
738
739 bool
740 ProcessorBox::leave_notify (GdkEventCrossing* ev)
741 {
742         return false;
743 }
744
745 void
746 ProcessorBox::processor_operation (ProcessorOperation op) 
747 {
748         ProcSelection targets;
749
750         get_selected_processors (targets);
751
752         if (targets.empty()) {
753
754                 int x, y;
755                 processor_display.get_pointer (x, y);
756
757                 pair<ProcessorEntry *, double> const pointer = processor_display.get_child_at_position (y);
758
759                 if (pointer.first && pointer.first->processor()) {
760                         targets.push_back (pointer.first->processor ());
761                 }
762         }
763
764         switch (op) {
765         case ProcessorsSelectAll:
766                 processor_display.select_all ();
767                 break;
768
769         case ProcessorsCopy:
770                 copy_processors (targets);
771                 break;
772
773         case ProcessorsCut:
774                 cut_processors (targets);
775                 break;
776
777         case ProcessorsPaste:
778                 if (targets.empty()) {
779                         paste_processors ();
780                 } else {
781                         paste_processors (targets.front());
782                 }
783                 break;
784
785         case ProcessorsDelete:
786                 delete_processors (targets);
787                 break;
788
789         case ProcessorsToggleActive:
790                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
791                         if ((*i)->active()) {
792                                 (*i)->deactivate ();
793                         } else {
794                                 (*i)->activate ();
795                         }
796                 }
797                 break;
798
799         case ProcessorsAB:
800                 ab_plugins ();
801                 break;
802
803         default:
804                 break;
805         }
806 }
807
808 bool
809 ProcessorBox::processor_button_press_event (GdkEventButton *ev, ProcessorEntry* child)
810 {
811         boost::shared_ptr<Processor> processor;
812         if (child) {
813                 processor = child->processor ();
814         }
815
816         int ret = false;
817         bool selected = processor_display.selected (child);
818
819         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
820
821                 if (_session->engine().connected()) {
822                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
823                         toggle_edit_processor (processor);
824                 }
825                 ret = true;
826
827         } else if (processor && ev->button == 1 && selected) {
828
829                 // this is purely informational but necessary for route params UI
830                 ProcessorSelected (processor); // emit
831
832         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
833
834                 choose_plugin ();
835                 _get_plugin_selector()->show_manager ();
836         }
837
838         return ret;
839 }
840
841 bool
842 ProcessorBox::processor_button_release_event (GdkEventButton *ev, ProcessorEntry* child)
843 {
844         boost::shared_ptr<Processor> processor;
845         if (child) {
846                 processor = child->processor ();
847         }
848
849         if (processor && Keyboard::is_delete_event (ev)) {
850
851                 Glib::signal_idle().connect (sigc::bind (
852                                 sigc::mem_fun(*this, &ProcessorBox::idle_delete_processor),
853                                 boost::weak_ptr<Processor>(processor)));
854
855         } else if (Keyboard::is_context_menu_event (ev)) {
856
857                 show_processor_menu (ev->time);
858
859         } else if (processor && Keyboard::is_button2_event (ev)
860 #ifndef GTKOSX
861                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
862 #endif
863                 ) {
864
865                 /* button2-click with no/appropriate modifiers */
866
867                 if (processor->active()) {
868                         processor->deactivate ();
869                 } else {
870                         processor->activate ();
871                 }
872         }
873
874         return false;
875 }
876
877 Menu *
878 ProcessorBox::build_processor_menu ()
879 {
880         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/ProcessorMenu") );
881         processor_menu->set_name ("ArdourContextMenu");
882         return processor_menu;
883 }
884
885 void
886 ProcessorBox::select_all_processors ()
887 {
888         processor_display.select_all ();
889 }
890
891 void
892 ProcessorBox::deselect_all_processors ()
893 {
894         processor_display.select_none ();
895 }
896
897 void
898 ProcessorBox::choose_plugin ()
899 {
900         _get_plugin_selector()->set_interested_object (*this);
901 }
902
903 /** @return true if an error occurred, otherwise false */
904 bool
905 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
906 {
907         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
908
909                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, *p));
910
911                 Route::ProcessorStreams err_streams;
912
913                 if (_route->add_processor_by_index (processor, _placement, &err_streams, Config->get_new_plugins_active ())) {
914                         weird_plugin_dialog (**p, err_streams);
915                         return true;
916                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
917                 } else {
918
919                         if (Profile->get_sae()) {
920                                 processor->activate ();
921                         }
922                 }
923         }
924
925         return false;
926 }
927
928 void
929 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
930 {
931         ArdourDialog dialog (_("Plugin Incompatibility"));
932         Label label;
933
934         string text = string_compose(_("You attempted to add the plugin \"%1\" in slot %2.\n"),
935                         p.name(), streams.index);
936
937         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
938         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
939
940         text += _("\nThis plugin has:\n");
941         if (has_midi) {
942                 uint32_t const n = p.get_info()->n_inputs.n_midi ();
943                 text += string_compose (ngettext ("\t%1 MIDI input\n", "\t%1 MIDI inputs\n", n), n);
944         }
945         if (has_audio) {
946                 uint32_t const n = p.get_info()->n_inputs.n_audio ();
947                 text += string_compose (ngettext ("\t%1 audio input\n", "\t%1 audio inputs\n", n), n);
948         }
949
950         text += _("\nbut at the insertion point, there are:\n");
951         if (has_midi) {
952                 uint32_t const n = streams.count.n_midi ();
953                 text += string_compose (ngettext ("\t%1 MIDI channel\n", "\t%1 MIDI channels\n", n), n);
954         }
955         if (has_audio) {
956                 uint32_t const n = streams.count.n_audio ();
957                 text += string_compose (ngettext ("\t%1 audio channel\n", "\t%1 audio channels\n", n), n);
958         }
959
960         text += string_compose (_("\n%1 is unable to insert this plugin here.\n"), PROGRAM_NAME);
961         label.set_text(text);
962
963         dialog.get_vbox()->pack_start (label);
964         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
965
966         dialog.set_name (X_("PluginIODialog"));
967         dialog.set_position (Gtk::WIN_POS_MOUSE);
968         dialog.set_modal (true);
969         dialog.show_all ();
970
971         dialog.run ();
972 }
973
974 void
975 ProcessorBox::choose_insert ()
976 {
977         boost::shared_ptr<Processor> processor (new PortInsert (*_session, _route->pannable(), _route->mute_master()));
978         _route->add_processor_by_index (processor, _placement);
979 }
980
981 /* Caller must not hold process lock */
982 void
983 ProcessorBox::choose_send ()
984 {
985         boost::shared_ptr<Send> send (new Send (*_session, _route->pannable(), _route->mute_master()));
986
987         /* make an educated guess at the initial number of outputs for the send */
988         ChanCount outs = (_session->master_out())
989                         ? _session->master_out()->n_outputs()
990                         : _route->n_outputs();
991
992         /* XXX need processor lock on route */
993         try {
994                 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
995                 send->output()->ensure_io (outs, false, this);
996         } catch (AudioEngine::PortRegistrationFailure& err) {
997                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
998                 return;
999         }
1000
1001         /* let the user adjust the IO setup before creation.
1002
1003            Note: this dialog is NOT modal - we just leave it to run and it will
1004            return when its Finished signal is emitted - typically when the window
1005            is closed.
1006          */
1007
1008         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
1009         ios->show ();
1010
1011         /* keep a reference to the send so it doesn't get deleted while
1012            the IOSelectorWindow is doing its stuff
1013         */
1014         _processor_being_created = send;
1015
1016         ios->selector().Finished.connect (sigc::bind (
1017                         sigc::mem_fun(*this, &ProcessorBox::send_io_finished),
1018                         boost::weak_ptr<Processor>(send), ios));
1019
1020 }
1021
1022 void
1023 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1024 {
1025         boost::shared_ptr<Processor> processor (weak_processor.lock());
1026
1027         /* drop our temporary reference to the new send */
1028         _processor_being_created.reset ();
1029
1030         if (!processor) {
1031                 return;
1032         }
1033
1034         switch (r) {
1035         case IOSelector::Cancelled:
1036                 // processor will go away when all shared_ptrs to it vanish
1037                 break;
1038
1039         case IOSelector::Accepted:
1040                 _route->add_processor_by_index (processor, _placement);
1041                 if (Profile->get_sae()) {
1042                         processor->activate ();
1043                 }
1044                 break;
1045         }
1046
1047         delete_when_idle (ios);
1048 }
1049
1050 void
1051 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
1052 {
1053         boost::shared_ptr<Processor> processor (weak_processor.lock());
1054
1055         /* drop our temporary reference to the new return */
1056         _processor_being_created.reset ();
1057
1058         if (!processor) {
1059                 return;
1060         }
1061
1062         switch (r) {
1063         case IOSelector::Cancelled:
1064                 // processor will go away when all shared_ptrs to it vanish
1065                 break;
1066
1067         case IOSelector::Accepted:
1068                 _route->add_processor_by_index (processor, _placement);
1069                 if (Profile->get_sae()) {
1070                         processor->activate ();
1071                 }
1072                 break;
1073         }
1074
1075         delete_when_idle (ios);
1076 }
1077
1078 void
1079 ProcessorBox::choose_aux (boost::weak_ptr<Route> wr)
1080 {
1081         if (!_route) {
1082                 return;
1083         }
1084
1085         boost::shared_ptr<Route> target = wr.lock();
1086
1087         if (!target) {
1088                 return;
1089         }
1090
1091         boost::shared_ptr<RouteList> rlist (new RouteList);
1092         rlist->push_back (_route);
1093
1094         _session->add_internal_sends (target, PreFader, rlist);
1095 }
1096
1097 void
1098 ProcessorBox::route_processors_changed (RouteProcessorChange c)
1099 {
1100         if (c.type == RouteProcessorChange::MeterPointChange && c.meter_visibly_changed == false) {
1101                 /* the meter has moved, but it was and still is invisible to the user, so nothing to do */
1102                 return;
1103         }
1104
1105         redisplay_processors ();
1106 }
1107
1108 void
1109 ProcessorBox::redisplay_processors ()
1110 {
1111         ENSURE_GUI_THREAD (*this, &ProcessorBox::redisplay_processors);
1112         bool     fader_seen;
1113
1114         if (no_processor_redisplay) {
1115                 return;
1116         }
1117
1118         processor_display.clear ();
1119
1120         _visible_prefader_processors = 0;
1121         fader_seen = false;
1122
1123         _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &ProcessorBox::help_count_visible_prefader_processors), 
1124                                                &_visible_prefader_processors, &fader_seen));
1125
1126         if (_visible_prefader_processors == 0) { // fader only
1127                 BlankProcessorEntry* bpe = new BlankProcessorEntry (_width);
1128                 bpe->set_pixel_width (get_allocation().get_width());
1129                 processor_display.add_child (bpe);
1130         }
1131
1132         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::add_processor_to_display));
1133
1134         for (list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin(); i != _processor_window_proxies.end(); ++i) {
1135                 (*i)->marked = false;
1136         }
1137
1138         _route->foreach_processor (sigc::mem_fun (*this, &ProcessorBox::maybe_add_processor_to_ui_list));
1139
1140         /* trim dead wood from the processor window proxy list */
1141
1142         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin();
1143         while (i != _processor_window_proxies.end()) {
1144                 list<ProcessorWindowProxy*>::iterator j = i;
1145                 ++j;
1146
1147                 if (!(*i)->marked) {
1148                         ARDOUR_UI::instance()->remove_window_proxy (*i);
1149                         delete *i;
1150                         _processor_window_proxies.erase (i);
1151                 }
1152
1153                 i = j;
1154         }
1155
1156         setup_entry_positions ();
1157 }
1158
1159 /** Add a ProcessorWindowProxy for a processor to our list, if that processor does
1160  *  not already have one.
1161  */
1162 void
1163 ProcessorBox::maybe_add_processor_to_ui_list (boost::weak_ptr<Processor> w)
1164 {
1165         boost::shared_ptr<Processor> p = w.lock ();
1166         if (!p) {
1167                 return;
1168         }
1169
1170         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
1171         while (i != _processor_window_proxies.end()) {
1172
1173                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
1174
1175                 if (p == t) {
1176                         /* this processor is already on the list; done */
1177                         (*i)->marked = true;
1178                         return;
1179                 }
1180
1181                 ++i;
1182         }
1183
1184         /* not on the list; add it */
1185
1186         string loc;
1187         if (_parent_strip) {
1188                 if (_parent_strip->mixer_owned()) {
1189                         loc = X_("M");
1190                 } else {
1191                         loc = X_("R");
1192                 }
1193         } else {
1194                 loc = X_("P");
1195         }
1196
1197         ProcessorWindowProxy* wp = new ProcessorWindowProxy (
1198                 string_compose ("%1-%2-%3", loc, _route->id(), p->id()),
1199                 _session->extra_xml (X_("UI")),
1200                 this,
1201                 w);
1202
1203         wp->marked = true;
1204
1205         /* if the processor already has an existing UI,
1206            note that so that we don't recreate it
1207         */
1208
1209         void* existing_ui = p->get_ui ();
1210
1211         if (existing_ui) {
1212                 wp->set (static_cast<Gtk::Window*>(existing_ui));
1213         }
1214
1215         _processor_window_proxies.push_back (wp);
1216         ARDOUR_UI::instance()->add_window_proxy (wp);
1217 }
1218
1219 void
1220 ProcessorBox::help_count_visible_prefader_processors (boost::weak_ptr<Processor> p, uint32_t* cnt, bool* amp_seen)
1221 {
1222         boost::shared_ptr<Processor> processor (p.lock ());
1223
1224         if (processor && processor->display_to_user()) {
1225
1226                 if (boost::dynamic_pointer_cast<Amp>(processor)) {
1227                         *amp_seen = true;
1228                 } else {
1229                         if (!*amp_seen) {
1230                                 (*cnt)++;
1231                         }
1232                 }
1233         }
1234 }
1235
1236 void
1237 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
1238 {
1239         boost::shared_ptr<Processor> processor (p.lock ());
1240
1241         if (!processor || !processor->display_to_user()) {
1242                 return;
1243         }
1244
1245
1246         boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
1247         boost::shared_ptr<PluginInsert> plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor);
1248         ProcessorEntry* e = 0;
1249         if (send) {
1250                 e = new SendProcessorEntry (send, _width);
1251         } else if (plugin_insert) {
1252                 e = new PluginInsertProcessorEntry (plugin_insert, _width);
1253         } else {
1254                 e = new ProcessorEntry (processor, _width);
1255         }
1256
1257         e->set_pixel_width (get_allocation().get_width());
1258         processor_display.add_child (e);
1259 }
1260
1261 void
1262 ProcessorBox::reordered ()
1263 {
1264         compute_processor_sort_keys ();
1265         setup_entry_positions ();
1266 }
1267
1268 void
1269 ProcessorBox::setup_entry_positions ()
1270 {
1271         list<ProcessorEntry*> children = processor_display.children ();
1272         bool pre_fader = true;
1273
1274         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1275                 if (boost::dynamic_pointer_cast<Amp>((*i)->processor())) {
1276                         pre_fader = false;
1277                         (*i)->set_position (ProcessorEntry::Fader);
1278                 } else {
1279                         if (pre_fader) {
1280                                 (*i)->set_position (ProcessorEntry::PreFader);
1281                         } else {
1282                                 (*i)->set_position (ProcessorEntry::PostFader);
1283                         }
1284                 }
1285         }
1286 }
1287
1288 void
1289 ProcessorBox::compute_processor_sort_keys ()
1290 {
1291         list<ProcessorEntry*> children = processor_display.children ();
1292         Route::ProcessorList our_processors;
1293
1294         for (list<ProcessorEntry*>::iterator i = children.begin(); i != children.end(); ++i) {
1295                 if ((*i)->processor()) {
1296                         our_processors.push_back ((*i)->processor ());
1297                 }
1298         }
1299
1300         if (_route->reorder_processors (our_processors)) {
1301                 /* Reorder failed, so report this to the user.  As far as I can see this must be done
1302                    in an idle handler: it seems that the redisplay_processors() that happens below destroys
1303                    widgets that were involved in the drag-and-drop on the processor list, which causes problems
1304                    when the drag is torn down after this handler function is finished.
1305                 */
1306                 Glib::signal_idle().connect_once (sigc::mem_fun (*this, &ProcessorBox::report_failed_reorder));
1307         }
1308 }
1309
1310 void
1311 ProcessorBox::report_failed_reorder ()
1312 {
1313         /* reorder failed, so redisplay */
1314
1315         redisplay_processors ();
1316
1317         /* now tell them about the problem */
1318
1319         ArdourDialog dialog (_("Plugin Incompatibility"));
1320         Label label;
1321
1322         label.set_text (_("\
1323 You cannot reorder these plugins/sends/inserts\n\
1324 in that way because the inputs and\n\
1325 outputs will not work correctly."));
1326
1327         dialog.get_vbox()->set_border_width (12);
1328         dialog.get_vbox()->pack_start (label);
1329         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
1330
1331         dialog.set_name (X_("PluginIODialog"));
1332         dialog.set_position (Gtk::WIN_POS_MOUSE);
1333         dialog.set_modal (true);
1334         dialog.show_all ();
1335
1336         dialog.run ();
1337 }
1338
1339 void
1340 ProcessorBox::rename_processors ()
1341 {
1342         ProcSelection to_be_renamed;
1343
1344         get_selected_processors (to_be_renamed);
1345
1346         if (to_be_renamed.empty()) {
1347                 return;
1348         }
1349
1350         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
1351                 rename_processor (*i);
1352         }
1353 }
1354
1355 bool
1356 ProcessorBox::can_cut () const
1357 {
1358         vector<boost::shared_ptr<Processor> > sel;
1359
1360         get_selected_processors (sel);
1361
1362         /* cut_processors () does not cut inserts */
1363
1364         for (vector<boost::shared_ptr<Processor> >::const_iterator i = sel.begin (); i != sel.end (); ++i) {
1365
1366                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1367                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1368                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1369                         return true;
1370                 }
1371         }
1372
1373         return false;
1374 }
1375
1376 void
1377 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1378 {
1379         if (to_be_removed.empty()) {
1380                 return;
1381         }
1382
1383         XMLNode* node = new XMLNode (X_("cut"));
1384         Route::ProcessorList to_cut;
1385
1386         no_processor_redisplay = true;
1387         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1388                 // Cut only plugins, sends and returns
1389                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1390                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1391                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1392
1393                         Window* w = get_processor_ui (*i);
1394
1395                         if (w) {
1396                                 w->hide ();
1397                         }
1398
1399                         XMLNode& child ((*i)->get_state());
1400                         node->add_child_nocopy (child);
1401                         to_cut.push_back (*i);
1402                 }
1403         }
1404
1405         if (_route->remove_processors (to_cut) != 0) {
1406                 delete node;
1407                 no_processor_redisplay = false;
1408                 return;
1409         }
1410
1411         _rr_selection.set (node);
1412
1413         no_processor_redisplay = false;
1414         redisplay_processors ();
1415 }
1416
1417 void
1418 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1419 {
1420         if (to_be_copied.empty()) {
1421                 return;
1422         }
1423
1424         XMLNode* node = new XMLNode (X_("copy"));
1425
1426         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1427                 // Copy only plugins, sends, returns
1428                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1429                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1430                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1431                         node->add_child_nocopy ((*i)->get_state());
1432                 }
1433         }
1434
1435         _rr_selection.set (node);
1436 }
1437
1438 void
1439 ProcessorBox::processors_up ()
1440 {
1441         /* unimplemented */
1442 }
1443
1444 void
1445 ProcessorBox::processors_down ()
1446 {
1447         /* unimplemented */
1448 }
1449         
1450
1451 void
1452 ProcessorBox::delete_processors (const ProcSelection& targets)
1453 {
1454         if (targets.empty()) {
1455                 return;
1456         }
1457
1458         no_processor_redisplay = true;
1459
1460         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1461
1462                 Window* w = get_processor_ui (*i);
1463
1464                 if (w) {
1465                         w->hide ();
1466                 }
1467
1468                 _route->remove_processor(*i);
1469         }
1470
1471         no_processor_redisplay = false;
1472         redisplay_processors ();
1473 }
1474
1475 void
1476 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1477 {
1478         list<boost::shared_ptr<Processor> >::const_iterator x;
1479
1480         no_processor_redisplay = true;
1481         for (x = procs.begin(); x != procs.end(); ++x) {
1482
1483                 Window* w = get_processor_ui (*x);
1484
1485                 if (w) {
1486                         w->hide ();
1487                 }
1488
1489                 _route->remove_processor(*x);
1490         }
1491
1492         no_processor_redisplay = false;
1493         redisplay_processors ();
1494 }
1495
1496 gint
1497 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1498 {
1499         boost::shared_ptr<Processor> processor (weak_processor.lock());
1500
1501         if (!processor) {
1502                 return false;
1503         }
1504
1505         /* NOT copied to _mixer.selection() */
1506
1507         no_processor_redisplay = true;
1508         _route->remove_processor (processor);
1509         no_processor_redisplay = false;
1510         redisplay_processors ();
1511
1512         return false;
1513 }
1514
1515 void
1516 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1517 {
1518         ArdourPrompter name_prompter (true);
1519         string result;
1520         name_prompter.set_title (_("Rename Processor"));
1521         name_prompter.set_prompt (_("New name:"));
1522         name_prompter.set_initial_text (processor->name());
1523         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1524         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1525         name_prompter.show_all ();
1526
1527         switch (name_prompter.run ()) {
1528
1529         case Gtk::RESPONSE_ACCEPT:
1530                 name_prompter.get_result (result);
1531                 if (result.length()) {
1532
1533                        int tries = 0;
1534                        string test = result;
1535
1536                        while (tries < 100) {
1537                                if (_session->io_name_is_legal (test)) {
1538                                        result = test;
1539                                        break;
1540                                }
1541                                tries++;
1542
1543                                test = string_compose ("%1-%2", result, tries);
1544                        }
1545
1546                        if (tries < 100) {
1547                                processor->set_name (result);
1548                        } else {
1549                                /* unlikely! */
1550                                ARDOUR_UI::instance()->popup_error
1551                                        (string_compose (_("At least 100 IO objects exist with a name like %1 - name not changed"), result));
1552                        }
1553                 }
1554                 break;
1555         }
1556
1557         return;
1558 }
1559
1560 void
1561 ProcessorBox::paste_processors ()
1562 {
1563         if (_rr_selection.processors.empty()) {
1564                 return;
1565         }
1566
1567         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1568 }
1569
1570 void
1571 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1572 {
1573
1574         if (_rr_selection.processors.empty()) {
1575                 return;
1576         }
1577
1578         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1579 }
1580
1581 void
1582 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1583 {
1584         XMLNodeConstIterator niter;
1585         list<boost::shared_ptr<Processor> > copies;
1586
1587         if (nlist.empty()) {
1588                 return;
1589         }
1590
1591         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1592
1593                 XMLProperty const * type = (*niter)->property ("type");
1594                 assert (type);
1595
1596                 boost::shared_ptr<Processor> p;
1597                 try {
1598                         if (type->value() == "meter" ||
1599                             type->value() == "main-outs" ||
1600                             type->value() == "amp" ||
1601                             type->value() == "intsend" || type->value() == "intreturn") {
1602                                 /* do not paste meter, main outs, amp or internal send/returns */
1603                                 continue;
1604
1605                         } else if (type->value() == "send") {
1606
1607                                 XMLNode n (**niter);
1608                                 Send::make_unique (n, *_session);
1609                                 Send* s = new Send (*_session, _route->pannable(), _route->mute_master());
1610                                 if (s->set_state (n, Stateful::loading_state_version)) {
1611                                         delete s;
1612                                         return;
1613                                 }
1614
1615                                 p.reset (s);
1616
1617
1618                         } else if (type->value() == "return") {
1619
1620                                 XMLNode n (**niter);
1621                                 Return::make_unique (n, *_session);
1622                                 Return* r = new Return (*_session);
1623
1624                                 if (r->set_state (n, Stateful::loading_state_version)) {
1625                                         delete r;
1626                                         return;
1627                                 }
1628
1629                                 p.reset (r);
1630
1631                         } else if (type->value() == "port") {
1632
1633                                 XMLNode n (**niter);
1634                                 p.reset (new PortInsert (*_session, _route->pannable (), _route->mute_master ()));
1635                                 if (p->set_state (n, Stateful::loading_state_version)) {
1636                                         return;
1637                                 }
1638
1639                         } else {
1640                                 /* XXX its a bit limiting to assume that everything else
1641                                    is a plugin.
1642                                 */
1643
1644                                 p.reset (new PluginInsert (*_session));
1645                                 p->set_state (**niter, Stateful::current_state_version);
1646                         }
1647
1648                         copies.push_back (p);
1649                 }
1650
1651                 catch (...) {
1652                         cerr << "plugin insert constructor failed\n";
1653                 }
1654         }
1655
1656         if (copies.empty()) {
1657                 return;
1658         }
1659
1660         if (_route->add_processors (copies, p)) {
1661
1662                 string msg = _(
1663                         "Copying the set of processors on the clipboard failed,\n\
1664 probably because the I/O configuration of the plugins\n\
1665 could not match the configuration of this track.");
1666                 MessageDialog am (msg);
1667                 am.run ();
1668         }
1669 }
1670
1671 void
1672 ProcessorBox::activate_processor (boost::shared_ptr<Processor> r)
1673 {
1674         r->activate ();
1675 }
1676
1677 void
1678 ProcessorBox::deactivate_processor (boost::shared_ptr<Processor> r)
1679 {
1680         r->deactivate ();
1681 }
1682
1683 void
1684 ProcessorBox::get_selected_processors (ProcSelection& processors) const
1685 {
1686         const list<ProcessorEntry*> selection = processor_display.selection ();
1687         for (list<ProcessorEntry*>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
1688                 processors.push_back ((*i)->processor ());
1689         }
1690 }
1691
1692 void
1693 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1694 {
1695         list<ProcessorEntry*> selection = processor_display.selection ();
1696         for (list<ProcessorEntry*>::iterator i = selection.begin(); i != selection.end(); ++i) {
1697                 (this->*method) ((*i)->processor ());
1698         }
1699 }
1700
1701 void
1702 ProcessorBox::all_visible_processors_active (bool state)
1703 {
1704         _route->all_visible_processors_active (state);
1705 }
1706
1707 void
1708 ProcessorBox::ab_plugins ()
1709 {
1710         _route->ab_plugins (ab_direction);
1711         ab_direction = !ab_direction;
1712 }
1713
1714
1715 void
1716 ProcessorBox::clear_processors ()
1717 {
1718         string prompt;
1719         vector<string> choices;
1720
1721         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1722                                    "(this cannot be undone)"), _route->name());
1723
1724         choices.push_back (_("Cancel"));
1725         choices.push_back (_("Yes, remove them all"));
1726
1727         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1728
1729         if (prompter.run () == 1) {
1730                 _route->clear_processors (PreFader);
1731                 _route->clear_processors (PostFader);
1732         }
1733 }
1734
1735 void
1736 ProcessorBox::clear_processors (Placement p)
1737 {
1738         string prompt;
1739         vector<string> choices;
1740
1741         if (p == PreFader) {
1742                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1743                                            "(this cannot be undone)"), _route->name());
1744         } else {
1745                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1746                                            "(this cannot be undone)"), _route->name());
1747         }
1748
1749         choices.push_back (_("Cancel"));
1750         choices.push_back (_("Yes, remove them all"));
1751
1752         Gtkmm2ext::Choice prompter (_("Remove processors"), prompt, choices);
1753
1754         if (prompter.run () == 1) {
1755                 _route->clear_processors (p);
1756         }
1757 }
1758
1759 bool
1760 ProcessorBox::processor_can_be_edited (boost::shared_ptr<Processor> processor)
1761 {
1762         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (_route);
1763         if (at && at->freeze_state() == AudioTrack::Frozen) {
1764                 return false;
1765         }
1766
1767         if (
1768                 (boost::dynamic_pointer_cast<Send> (processor) && !boost::dynamic_pointer_cast<InternalSend> (processor))||
1769                 boost::dynamic_pointer_cast<Return> (processor) ||
1770                 boost::dynamic_pointer_cast<PluginInsert> (processor) ||
1771                 boost::dynamic_pointer_cast<PortInsert> (processor)
1772                 ) {
1773                 return true;
1774         }
1775
1776         return false;
1777 }
1778
1779 bool
1780 ProcessorBox::one_processor_can_be_edited ()
1781 {
1782         list<ProcessorEntry*> selection = processor_display.selection ();
1783         list<ProcessorEntry*>::iterator i = selection.begin();
1784         while (i != selection.end() && processor_can_be_edited ((*i)->processor()) == false) {
1785                 ++i;
1786         }
1787
1788         return (i != selection.end());
1789 }
1790
1791 void
1792 ProcessorBox::toggle_edit_processor (boost::shared_ptr<Processor> processor)
1793 {
1794         boost::shared_ptr<Send> send;
1795         boost::shared_ptr<InternalSend> internal_send;
1796         boost::shared_ptr<Return> retrn;
1797         boost::shared_ptr<PluginInsert> plugin_insert;
1798         boost::shared_ptr<PortInsert> port_insert;
1799         Window* gidget = 0;
1800
1801         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1802
1803                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1804                         return;
1805                 }
1806         }
1807
1808         if (boost::dynamic_pointer_cast<Amp> (processor)) {
1809
1810                 if (_parent_strip) {
1811                         _parent_strip->revert_to_default_display ();
1812                 }
1813
1814         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1815
1816                 if (!_session->engine().connected()) {
1817                         return;
1818                 }
1819
1820                 if (boost::dynamic_pointer_cast<InternalSend> (processor) == 0) {
1821                         SendUIWindow* w = new SendUIWindow (send, _session);
1822                         w->show ();
1823                 } else {
1824                         /* assign internal send to main fader */
1825                         if (_parent_strip) {
1826                                 _parent_strip->show_send(send);
1827                         }
1828                 }
1829
1830         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
1831
1832                 if (boost::dynamic_pointer_cast<InternalReturn> (retrn)) {
1833                         /* no GUI for these */
1834                         return;
1835                 }
1836
1837                 if (!_session->engine().connected()) {
1838                         return;
1839                 }
1840
1841                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
1842
1843                 ReturnUIWindow *return_ui;
1844                 Window* w = get_processor_ui (retrn);
1845
1846                 if (w == 0) {
1847
1848                         return_ui = new ReturnUIWindow (retrn, _session);
1849                         return_ui->set_title (retrn->name ());
1850                         set_processor_ui (send, return_ui);
1851
1852                 } else {
1853                         return_ui = dynamic_cast<ReturnUIWindow *> (w);
1854                 }
1855
1856                 gidget = return_ui;
1857
1858         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1859
1860                 PluginUIWindow *plugin_ui;
1861
1862                 /* these are both allowed to be null */
1863
1864                 Container* toplevel = get_toplevel();
1865                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1866
1867                 Window* w = get_processor_ui (plugin_insert);
1868
1869                 if (w == 0) {
1870
1871                         plugin_ui = new PluginUIWindow (win, plugin_insert);
1872                         plugin_ui->set_title (generate_processor_title (plugin_insert));
1873                         set_processor_ui (plugin_insert, plugin_ui);
1874
1875                 } else {
1876                         plugin_ui = dynamic_cast<PluginUIWindow *> (w);
1877                         plugin_ui->set_parent (win);
1878                 }
1879
1880                 gidget = plugin_ui;
1881
1882         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
1883
1884                 if (!_session->engine().connected()) {
1885                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1886                         msg.run ();
1887                         return;
1888                 }
1889
1890                 PortInsertWindow *io_selector;
1891
1892                 Window* w = get_processor_ui (port_insert);
1893
1894                 if (w == 0) {
1895                         io_selector = new PortInsertWindow (_session, port_insert);
1896                         set_processor_ui (port_insert, io_selector);
1897
1898                 } else {
1899                         io_selector = dynamic_cast<PortInsertWindow *> (w);
1900                 }
1901
1902                 gidget = io_selector;
1903         }
1904
1905         if (gidget) {
1906                 if (gidget->is_visible()) {
1907                         gidget->hide ();
1908                 } else {
1909                         gidget->show_all ();
1910                         gidget->present ();
1911                 }
1912         }
1913 }
1914
1915 void
1916 ProcessorBox::toggle_processor_controls (boost::shared_ptr<Processor> processor)
1917 {
1918         boost::shared_ptr<PluginInsert> plugin_insert
1919                 = boost::dynamic_pointer_cast<PluginInsert>(processor);
1920         if (!plugin_insert) {
1921                 return;
1922         }
1923
1924         Container*      toplevel  = get_toplevel();
1925         Window*         win       = dynamic_cast<Gtk::Window*>(toplevel);
1926         PluginUIWindow* plugin_ui = new PluginUIWindow(win, plugin_insert, true, false);
1927         plugin_ui->set_title(generate_processor_title (plugin_insert));
1928
1929         if (plugin_ui->is_visible()) {
1930                 plugin_ui->hide();
1931         } else {
1932                 plugin_ui->show_all();
1933                 plugin_ui->present();
1934         }
1935 }
1936
1937 void
1938 ProcessorBox::register_actions ()
1939 {
1940         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("ProcessorMenu"));
1941         Glib::RefPtr<Action> act;
1942
1943         /* new stuff */
1944         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
1945                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
1946
1947         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
1948                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
1949         ActionManager::jack_sensitive_actions.push_back (act);
1950         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New External Send ..."),
1951                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
1952         ActionManager::jack_sensitive_actions.push_back (act);
1953
1954         ActionManager::register_action (popup_act_grp, X_("newaux"), _("New Aux Send ..."));
1955
1956         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
1957                         sigc::ptr_fun (ProcessorBox::rb_clear));
1958         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
1959                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
1960         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
1961                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
1962
1963         /* standard editing stuff */
1964         cut_action = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
1965                                                      sigc::ptr_fun (ProcessorBox::rb_cut));
1966         ActionManager::plugin_selection_sensitive_actions.push_back(cut_action);
1967         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
1968                         sigc::ptr_fun (ProcessorBox::rb_copy));
1969         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1970
1971         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
1972                         sigc::ptr_fun (ProcessorBox::rb_delete));
1973         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1974
1975         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
1976                         sigc::ptr_fun (ProcessorBox::rb_paste));
1977         rename_action = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
1978                         sigc::ptr_fun (ProcessorBox::rb_rename));
1979         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
1980                         sigc::ptr_fun (ProcessorBox::rb_select_all));
1981         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
1982                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
1983
1984         /* activation etc. */
1985
1986         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate All"),
1987                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
1988         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate All"),
1989                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
1990         ActionManager::register_action (popup_act_grp, X_("ab_plugins"), _("A/B Plugins"),
1991                         sigc::ptr_fun (ProcessorBox::rb_ab_plugins));
1992
1993         /* show editors */
1994         edit_action = ActionManager::register_action (
1995                 popup_act_grp, X_("edit"), _("Edit..."),
1996                 sigc::ptr_fun (ProcessorBox::rb_edit));
1997
1998         /* show plugin GUI */
1999         controls_action = ActionManager::register_action (
2000                         popup_act_grp, X_("controls"), _("Controls..."),
2001                         sigc::ptr_fun (ProcessorBox::rb_controls));
2002
2003         ActionManager::add_action_group (popup_act_grp);
2004 }
2005
2006 void
2007 ProcessorBox::rb_ab_plugins ()
2008 {
2009         if (_current_processor_box == 0) {
2010                 return;
2011         }
2012
2013         _current_processor_box->ab_plugins ();
2014 }
2015
2016 void
2017 ProcessorBox::rb_choose_plugin ()
2018 {
2019         if (_current_processor_box == 0) {
2020                 return;
2021         }
2022         _current_processor_box->choose_plugin ();
2023 }
2024
2025 void
2026 ProcessorBox::rb_choose_insert ()
2027 {
2028         if (_current_processor_box == 0) {
2029                 return;
2030         }
2031         _current_processor_box->choose_insert ();
2032 }
2033
2034 void
2035 ProcessorBox::rb_choose_send ()
2036 {
2037         if (_current_processor_box == 0) {
2038                 return;
2039         }
2040         _current_processor_box->choose_send ();
2041 }
2042
2043 void
2044 ProcessorBox::rb_choose_aux (boost::weak_ptr<Route> wr)
2045 {
2046         if (_current_processor_box == 0) {
2047                 return;
2048         }
2049
2050         _current_processor_box->choose_aux (wr);
2051 }
2052
2053 void
2054 ProcessorBox::rb_clear ()
2055 {
2056         if (_current_processor_box == 0) {
2057                 return;
2058         }
2059
2060         _current_processor_box->clear_processors ();
2061 }
2062
2063
2064 void
2065 ProcessorBox::rb_clear_pre ()
2066 {
2067         if (_current_processor_box == 0) {
2068                 return;
2069         }
2070
2071         _current_processor_box->clear_processors (PreFader);
2072 }
2073
2074
2075 void
2076 ProcessorBox::rb_clear_post ()
2077 {
2078         if (_current_processor_box == 0) {
2079                 return;
2080         }
2081
2082         _current_processor_box->clear_processors (PostFader);
2083 }
2084
2085 void
2086 ProcessorBox::rb_cut ()
2087 {
2088         if (_current_processor_box == 0) {
2089                 return;
2090         }
2091
2092         _current_processor_box->processor_operation (ProcessorsCut);
2093 }
2094
2095 void
2096 ProcessorBox::rb_delete ()
2097 {
2098         if (_current_processor_box == 0) {
2099                 return;
2100         }
2101
2102         _current_processor_box->processor_operation (ProcessorsDelete);
2103 }
2104
2105 void
2106 ProcessorBox::rb_copy ()
2107 {
2108         if (_current_processor_box == 0) {
2109                 return;
2110         }
2111         _current_processor_box->processor_operation (ProcessorsCopy);
2112 }
2113
2114 void
2115 ProcessorBox::rb_paste ()
2116 {
2117         if (_current_processor_box == 0) {
2118                 return;
2119         }
2120
2121         _current_processor_box->processor_operation (ProcessorsPaste);
2122 }
2123
2124 void
2125 ProcessorBox::rb_rename ()
2126 {
2127         if (_current_processor_box == 0) {
2128                 return;
2129         }
2130         _current_processor_box->rename_processors ();
2131 }
2132
2133 void
2134 ProcessorBox::rb_select_all ()
2135 {
2136         if (_current_processor_box == 0) {
2137                 return;
2138         }
2139
2140         _current_processor_box->processor_operation (ProcessorsSelectAll);
2141 }
2142
2143 void
2144 ProcessorBox::rb_deselect_all ()
2145 {
2146         if (_current_processor_box == 0) {
2147                 return;
2148         }
2149
2150         _current_processor_box->deselect_all_processors ();
2151 }
2152
2153 void
2154 ProcessorBox::rb_activate_all ()
2155 {
2156         if (_current_processor_box == 0) {
2157                 return;
2158         }
2159
2160         _current_processor_box->all_visible_processors_active (true);
2161 }
2162
2163 void
2164 ProcessorBox::rb_deactivate_all ()
2165 {
2166         if (_current_processor_box == 0) {
2167                 return;
2168         }
2169         _current_processor_box->all_visible_processors_active (false);
2170 }
2171
2172 void
2173 ProcessorBox::rb_edit ()
2174 {
2175         if (_current_processor_box == 0) {
2176                 return;
2177         }
2178
2179         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_edit_processor);
2180 }
2181
2182 void
2183 ProcessorBox::rb_controls ()
2184 {
2185         if (_current_processor_box == 0) {
2186                 return;
2187         }
2188
2189         _current_processor_box->for_selected_processors (&ProcessorBox::toggle_processor_controls);
2190 }
2191
2192 void
2193 ProcessorBox::route_property_changed (const PropertyChange& what_changed)
2194 {
2195         if (!what_changed.contains (ARDOUR::Properties::name)) {
2196                 return;
2197         }
2198
2199         ENSURE_GUI_THREAD (*this, &ProcessorBox::route_property_changed, what_changed);
2200
2201         boost::shared_ptr<Processor> processor;
2202         boost::shared_ptr<PluginInsert> plugin_insert;
2203         boost::shared_ptr<Send> send;
2204
2205         list<ProcessorEntry*> children = processor_display.children();
2206
2207         for (list<ProcessorEntry*>::iterator iter = children.begin(); iter != children.end(); ++iter) {
2208
2209                 processor = (*iter)->processor ();
2210
2211                 if (!processor) {
2212                         continue;
2213                 }
2214
2215                 Window* w = get_processor_ui (processor);
2216
2217                 if (!w) {
2218                         continue;
2219                 }
2220
2221                 /* rename editor windows for sends and plugins */
2222
2223                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
2224                         w->set_title (send->name ());
2225                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
2226                         w->set_title (generate_processor_title (plugin_insert));
2227                 }
2228         }
2229 }
2230
2231 string
2232 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
2233 {
2234         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
2235         string::size_type email_pos;
2236
2237         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
2238                 maker = maker.substr (0, email_pos - 1);
2239         }
2240
2241         if (maker.length() > 32) {
2242                 maker = maker.substr (0, 32);
2243                 maker += " ...";
2244         }
2245
2246         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
2247 }
2248
2249 void
2250 ProcessorBox::on_size_allocate (Allocation& a)
2251 {
2252         HBox::on_size_allocate (a);
2253
2254         list<ProcessorEntry*> children = processor_display.children ();
2255         for (list<ProcessorEntry*>::const_iterator i = children.begin(); i != children.end(); ++i) {
2256                 (*i)->set_pixel_width (a.get_width ());
2257         }
2258 }
2259
2260 /** @param p Processor.
2261  *  @return the UI window for \a p.
2262  */
2263 Window *
2264 ProcessorBox::get_processor_ui (boost::shared_ptr<Processor> p) const
2265 {
2266         list<ProcessorWindowProxy*>::const_iterator i = _processor_window_proxies.begin ();
2267         while (i != _processor_window_proxies.end()) {
2268                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2269                 if (t && t == p) {
2270                         return (*i)->get ();
2271                 }
2272
2273                 ++i;
2274         }
2275
2276         return 0;
2277 }
2278
2279 /** Make a note of the UI window that a processor is using.
2280  *  @param p Processor.
2281  *  @param w UI window.
2282  */
2283 void
2284 ProcessorBox::set_processor_ui (boost::shared_ptr<Processor> p, Gtk::Window* w)
2285 {
2286         list<ProcessorWindowProxy*>::iterator i = _processor_window_proxies.begin ();
2287
2288         p->set_ui (w);
2289
2290         while (i != _processor_window_proxies.end()) {
2291                 boost::shared_ptr<Processor> t = (*i)->processor().lock ();
2292                 if (t && t == p) {
2293                         (*i)->set (w);
2294                         return;
2295                 }
2296
2297                 ++i;
2298         }
2299
2300         /* we shouldn't get here, because the ProcessorUIList should always contain
2301            an entry for each processor.
2302         */
2303         assert (false);
2304 }
2305
2306 void
2307 ProcessorBox::mixer_strip_delivery_changed (boost::weak_ptr<Delivery> w)
2308 {
2309         boost::shared_ptr<Delivery> d = w.lock ();
2310         if (!d) {
2311                 return;
2312         }
2313
2314         list<ProcessorEntry*> children = processor_display.children ();
2315         list<ProcessorEntry*>::const_iterator i = children.begin();
2316         while (i != children.end() && (*i)->processor() != d) {
2317                 ++i;
2318         }
2319
2320         if (i == children.end()) {
2321                 processor_display.set_active (0);
2322         } else {
2323                 processor_display.set_active (*i);
2324         }
2325 }
2326
2327 void
2328 ProcessorBox::hide_things ()
2329 {
2330         list<ProcessorEntry*> c = processor_display.children ();
2331         for (list<ProcessorEntry*>::iterator i = c.begin(); i != c.end(); ++i) {
2332                 (*i)->hide_things ();
2333         }
2334 }
2335
2336 void
2337 ProcessorBox::processor_menu_unmapped ()
2338 {
2339         processor_display.remove_placeholder ();
2340 }
2341
2342 ProcessorWindowProxy::ProcessorWindowProxy (
2343         string const & name,
2344         XMLNode const * node,
2345         ProcessorBox* box,
2346         boost::weak_ptr<Processor> processor
2347         )
2348         : WindowProxy<Gtk::Window> (name, node)
2349         , marked (false)
2350         , _processor_box (box)
2351         , _processor (processor)
2352 {
2353
2354 }
2355
2356
2357 void
2358 ProcessorWindowProxy::show ()
2359 {
2360         boost::shared_ptr<Processor> p = _processor.lock ();
2361         if (!p) {
2362                 return;
2363         }
2364
2365         _processor_box->toggle_edit_processor (p);
2366 }
2367