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