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