043395a0f1ddfb5756e29d22e7b3e01023d726f8
[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/stop_signal.h>
41 #include <gtkmm2ext/doi.h>
42
43 #include "ardour/amp.h"
44 #include "ardour/ardour.h"
45 #include "ardour/audio_diskstream.h"
46 #include "ardour/audio_track.h"
47 #include "ardour/audioengine.h"
48 #include "ardour/internal_send.h"
49 #include "ardour/ladspa_plugin.h"
50 #include "ardour/meter.h"
51 #include "ardour/plugin_insert.h"
52 #include "ardour/port_insert.h"
53 #include "ardour/profile.h"
54 #include "ardour/return.h"
55 #include "ardour/route.h"
56 #include "ardour/send.h"
57 #include "ardour/session.h"
58
59 #include "actions.h"
60 #include "ardour_dialog.h"
61 #include "ardour_ui.h"
62 #include "gui_thread.h"
63 #include "io_selector.h"
64 #include "keyboard.h"
65 #include "mixer_ui.h"
66 #include "mixer_strip.h"
67 #include "plugin_selector.h"
68 #include "plugin_ui.h"
69 #include "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 sigc;
84 using namespace ARDOUR;
85 using namespace PBD;
86 using namespace Gtk;
87 using namespace Glib;
88 using namespace Gtkmm2ext;
89
90 ProcessorBox* ProcessorBox::_current_processor_box = 0;
91 RefPtr<Action> ProcessorBox::paste_action;
92 bool ProcessorBox::get_colors = true;
93 Gdk::Color* ProcessorBox::active_processor_color;
94 Gdk::Color* ProcessorBox::inactive_processor_color;
95
96 ProcessorBox::ProcessorBox (ARDOUR::Session& sess, sigc::slot<PluginSelector*> get_plugin_selector,
97                         RouteRedirectSelection& rsel, MixerStrip* parent, bool owner_is_mixer)
98         : _session(sess)
99         , _parent_strip (parent)
100         , _owner_is_mixer (owner_is_mixer)
101         , _get_plugin_selector (get_plugin_selector)
102         , _placement(PreFader)
103         , _rr_selection(rsel)
104 {
105         if (get_colors) {
106                 active_processor_color = new Gdk::Color;
107                 inactive_processor_color = new Gdk::Color;
108                 set_color (*active_processor_color, rgba_from_style (
109                                 "ProcessorSelector", 0xff, 0, 0, 0, "fg", Gtk::STATE_ACTIVE, false ));
110                 set_color (*inactive_processor_color, rgba_from_style (
111                                 "ProcessorSelector", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
112                 get_colors = false;
113         }
114
115         _width = Wide;
116         processor_menu = 0;
117         send_action_menu = 0;
118         processor_drag_in_progress = false;
119         no_processor_redisplay = false;
120         ignore_delete = false;
121
122         model = ListStore::create(columns);
123
124         RefPtr<TreeSelection> selection = processor_display.get_selection();
125         selection->set_mode (Gtk::SELECTION_MULTIPLE);
126         selection->signal_changed().connect (mem_fun (*this, &ProcessorBox::selection_changed));
127
128         processor_display.set_data ("processorbox", this);
129         processor_display.set_model (model);
130         processor_display.append_column (X_("notshown"), columns.text);
131         processor_display.set_name ("ProcessorSelector");
132         processor_display.set_headers_visible (false);
133         processor_display.set_reorderable (true);
134         processor_display.set_size_request (-1, 40);
135         processor_display.get_column(0)->set_sizing(TREE_VIEW_COLUMN_FIXED);
136         processor_display.get_column(0)->set_fixed_width(48);
137         processor_display.add_object_drag (columns.processor.index(), "processors");
138         processor_display.set_enable_search (false);
139         processor_display.signal_drop.connect (mem_fun (*this, &ProcessorBox::object_drop));
140
141         TreeViewColumn* name_col = processor_display.get_column(0);
142         CellRendererText* renderer = dynamic_cast<CellRendererText*>(
143                         processor_display.get_column_cell_renderer (0));
144         name_col->add_attribute(renderer->property_foreground_gdk(), columns.color);
145
146         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
147
148         model->signal_row_deleted().connect (mem_fun (*this, &ProcessorBox::row_deleted));
149
150         processor_scroller.add (processor_display);
151         processor_eventbox.add (processor_scroller);
152
153         processor_scroller.set_size_request (-1, 40);
154
155         pack_start (processor_eventbox, true, true);
156
157         processor_display.signal_enter_notify_event().connect (mem_fun(*this, &ProcessorBox::enter_notify), false);
158         processor_display.signal_leave_notify_event().connect (mem_fun(*this, &ProcessorBox::leave_notify), false);
159
160         processor_display.signal_key_press_event().connect (mem_fun(*this, &ProcessorBox::processor_key_press_event));
161         processor_display.signal_key_release_event().connect (mem_fun(*this, &ProcessorBox::processor_key_release_event));
162
163         processor_display.signal_button_press_event().connect (
164                 mem_fun(*this, &ProcessorBox::processor_button_press_event), false);
165         processor_display.signal_button_release_event().connect (
166                 mem_fun(*this, &ProcessorBox::processor_button_release_event));
167 }
168
169 ProcessorBox::~ProcessorBox ()
170 {
171 }
172
173 void
174 ProcessorBox::set_route (boost::shared_ptr<Route> r)
175 {
176         connections.clear ();
177
178         /* new route: any existing block on processor redisplay must be meaningless */
179         no_processor_redisplay = false;
180         _route = r;
181
182         connections.push_back (_route->processors_changed.connect (
183                         mem_fun(*this, &ProcessorBox::redisplay_processors)));
184         connections.push_back (_route->GoingAway.connect (
185                         mem_fun (*this, &ProcessorBox::route_going_away)));
186         connections.push_back (_route->NameChanged.connect (
187                         mem_fun(*this, &ProcessorBox::route_name_changed)));
188
189         redisplay_processors ();
190 }
191
192 void
193 ProcessorBox::route_going_away ()
194 {
195         /* don't keep updating display as processors are deleted */
196         no_processor_redisplay = true;
197 }
198
199
200 void
201 ProcessorBox::object_drop (const list<boost::shared_ptr<Processor> >& procs, Gtk::TreeView* source, int x, int y, Glib::RefPtr<Gdk::DragContext>& context)
202 {
203         TreeIter iter;
204         TreeModel::Path path;
205         TreeViewColumn* column;
206         int cellx;
207         int celly;
208         boost::shared_ptr<Processor> p;
209
210         if (processor_display.get_path_at_pos (x, y, path, column, cellx, celly)) {
211                 if ((iter = model->get_iter (path))) {
212                         p = (*iter)[columns.processor];
213                 }
214         }
215
216         for (list<boost::shared_ptr<Processor> >::const_iterator i = procs.begin(); i != procs.end(); ++i) {
217                 XMLNode& state = (*i)->get_state ();
218                 XMLNodeList nlist;
219                 nlist.push_back (&state);
220                 paste_processor_state (nlist, p);
221                 delete &state;
222         }
223
224         /* since the treeview doesn't take care of this properly, we have to delete the originals
225            ourselves.
226         */
227
228         if ((context->get_suggested_action() == Gdk::ACTION_MOVE) && source) {
229                 ProcessorBox* other = reinterpret_cast<ProcessorBox*> (source->get_data ("processorbox"));
230                 if (other) {
231                         cerr << "source was another processor box, delete the selected items\n";
232                         other->delete_dragged_processors (procs);
233                 }
234         }
235
236         context->drag_finish (true, (context->get_suggested_action() == Gdk::ACTION_MOVE), 0);
237 }
238
239 void
240 ProcessorBox::update()
241 {
242         redisplay_processors ();
243 }
244
245
246 void
247 ProcessorBox::set_width (Width w)
248 {
249         if (_width == w) {
250                 return;
251         }
252         _width = w;
253
254         redisplay_processors ();
255 }
256
257 void
258 ProcessorBox::remove_processor_gui (boost::shared_ptr<Processor> processor)
259 {
260         boost::shared_ptr<Send> send;
261         boost::shared_ptr<Return> retrn;
262         boost::shared_ptr<PortInsert> port_insert;
263
264         if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
265                 PortInsertUI *io_selector = reinterpret_cast<PortInsertUI *> (port_insert->get_gui());
266                 port_insert->set_gui (0);
267                 delete io_selector;
268         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
269                 SendUIWindow *sui = reinterpret_cast<SendUIWindow*> (send->get_gui());
270                 send->set_gui (0);
271                 delete sui;
272         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
273                 ReturnUIWindow *rui = reinterpret_cast<ReturnUIWindow*> (retrn->get_gui());
274                 retrn->set_gui (0);
275                 delete rui;
276         }
277 }
278
279 void
280 ProcessorBox::build_send_action_menu ()
281 {
282         using namespace Menu_Helpers;
283
284         send_action_menu = new Menu;
285         send_action_menu->set_name ("ArdourContextMenu");
286         MenuList& items = send_action_menu->items();
287
288         items.push_back (MenuElem (_("New send"), mem_fun(*this, &ProcessorBox::new_send)));
289         items.push_back (MenuElem (_("Show send controls"), mem_fun(*this, &ProcessorBox::show_send_controls)));
290 }
291
292 void
293 ProcessorBox::show_send_controls ()
294 {
295 }
296
297 void
298 ProcessorBox::new_send ()
299 {
300 }
301
302 void
303 ProcessorBox::show_processor_menu (gint arg)
304 {
305         if (processor_menu == 0) {
306                 processor_menu = build_processor_menu ();
307         }
308
309         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/processormenu/newplugin"));
310
311         if (plugin_menu_item) {
312                 plugin_menu_item->set_submenu (*_get_plugin_selector()->plugin_menu());
313         }
314
315         paste_action->set_sensitive (!_rr_selection.processors.empty());
316
317         processor_menu->popup (1, arg);
318 }
319
320 void
321 ProcessorBox::processor_drag_begin (GdkDragContext *)
322 {
323         processor_drag_in_progress = true;
324 }
325
326 void
327 ProcessorBox::processor_drag_end (GdkDragContext *)
328 {
329         processor_drag_in_progress = false;
330 }
331
332 bool
333 ProcessorBox::enter_notify (GdkEventCrossing*)
334 {
335         _current_processor_box = this;
336         Keyboard::magic_widget_grab_focus ();
337         processor_display.grab_focus ();
338
339         return false;
340 }
341
342 bool
343 ProcessorBox::leave_notify (GdkEventCrossing* ev)
344 {
345         switch (ev->detail) {
346         case GDK_NOTIFY_INFERIOR:
347                 break;
348         default:
349                 Keyboard::magic_widget_drop_focus ();
350         }
351
352         return false;
353 }
354
355 bool
356 ProcessorBox::processor_key_press_event (GdkEventKey *)
357 {
358         /* do real stuff on key release */
359         return false;
360 }
361
362 bool
363 ProcessorBox::processor_key_release_event (GdkEventKey *ev)
364 {
365         bool ret = false;
366         ProcSelection targets;
367
368         get_selected_processors (targets);
369
370         if (targets.empty()) {
371
372                 int x, y;
373                 TreeIter iter;
374                 TreeModel::Path path;
375                 TreeViewColumn* column;
376                 int cellx;
377                 int celly;
378
379                 processor_display.get_pointer (x, y);
380
381                 if (processor_display.get_path_at_pos (x, y, path, column, cellx, celly)) {
382                         if ((iter = model->get_iter (path))) {
383                                 targets.push_back ((*iter)[columns.processor]);
384                         }
385                 }
386         }
387
388
389         switch (ev->keyval) {
390         case GDK_c:
391                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
392                         copy_processors (targets);
393                 }
394                 break;
395
396         case GDK_x:
397                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
398                         cut_processors (targets);
399                 }
400                 break;
401
402         case GDK_v:
403                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
404                         if (targets.empty()) {
405                                 paste_processors ();
406                         } else {
407                                 paste_processors (targets.front());
408                         }
409                 }
410                 break;
411
412         case GDK_Delete:
413         case GDK_BackSpace:
414                 delete_processors (targets);
415                 ret = true;
416                 break;
417
418         case GDK_slash:
419                 for (ProcSelection::iterator i = targets.begin(); i != targets.end(); ++i) {
420                         if ((*i)->active()) {
421                                 (*i)->deactivate ();
422                         } else {
423                                 (*i)->activate ();
424                         }
425                 }
426                 ret = true;
427                 break;
428
429         default:
430                 break;
431         }
432
433         return ret;
434 }
435
436 bool
437 ProcessorBox::processor_button_press_event (GdkEventButton *ev)
438 {
439         TreeIter iter;
440         TreeModel::Path path;
441         TreeViewColumn* column;
442         int cellx;
443         int celly;
444         boost::shared_ptr<Processor> processor;
445         int ret = false;
446         bool selected = false;
447
448         if (processor_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
449                 if ((iter = model->get_iter (path))) {
450                         processor = (*iter)[columns.processor];
451                         selected = processor_display.get_selection()->is_selected (iter);
452                 }
453
454         }
455
456         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
457
458                 if (_session.engine().connected()) {
459                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
460                         edit_processor (processor);
461                 }
462                 ret = true;
463
464         } else if (processor && ev->button == 1 && selected) {
465
466                 // this is purely informational but necessary for route params UI
467                 ProcessorSelected (processor); // emit
468
469         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
470
471                 choose_plugin ();
472                 _get_plugin_selector()->show_manager ();
473         }
474
475
476         return ret;
477 }
478
479 bool
480 ProcessorBox::processor_button_release_event (GdkEventButton *ev)
481 {
482         TreeIter iter;
483         TreeModel::Path path;
484         TreeViewColumn* column;
485         int cellx;
486         int celly;
487         boost::shared_ptr<Processor> processor;
488         int ret = false;
489
490         if (processor_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
491                 if ((iter = model->get_iter (path))) {
492                         processor = (*iter)[columns.processor];
493                 }
494         }
495
496         if (processor && Keyboard::is_delete_event (ev)) {
497
498                 Glib::signal_idle().connect (bind (
499                                 mem_fun(*this, &ProcessorBox::idle_delete_processor),
500                                 boost::weak_ptr<Processor>(processor)));
501                 ret = true;
502
503         } else if (Keyboard::is_context_menu_event (ev)) {
504
505                 /* figure out if we are above or below the fader/amp processor,
506                    and set the next insert position appropriately.
507                 */
508
509                 if (processor) {
510                         if (_route->processor_is_prefader (processor)) {
511                                 _placement = PreFader;
512                         } else {
513                                 _placement = PostFader;
514                         }
515                 } else {
516                         _placement = PostFader;
517                 }
518
519                 show_processor_menu (ev->time);
520                 ret = true;
521
522         } else if (processor && Keyboard::is_button2_event (ev)
523 #ifndef GTKOSX
524                    && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))
525 #endif
526                 ) {
527
528                 /* button2-click with no/appropriate modifiers */
529
530                 if (processor->active()) {
531                         processor->deactivate ();
532                 } else {
533                         processor->activate ();
534                 }
535                 ret = true;
536
537         }
538
539         return ret;
540 }
541
542 Menu *
543 ProcessorBox::build_processor_menu ()
544 {
545         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/processormenu") );
546         processor_menu->set_name ("ArdourContextMenu");
547
548         show_all_children();
549
550         return processor_menu;
551 }
552
553 void
554 ProcessorBox::selection_changed ()
555 {
556         bool sensitive = (processor_display.get_selection()->count_selected_rows()) ? true : false;
557         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
558 }
559
560 void
561 ProcessorBox::select_all_processors ()
562 {
563         processor_display.get_selection()->select_all();
564 }
565
566 void
567 ProcessorBox::deselect_all_processors ()
568 {
569         processor_display.get_selection()->unselect_all();
570 }
571
572 void
573 ProcessorBox::choose_plugin ()
574 {
575         _get_plugin_selector()->set_interested_object (*this);
576 }
577
578 void
579 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
580 {
581         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
582
583                 boost::shared_ptr<Processor> processor (new PluginInsert (_session, *p));
584
585                 Route::ProcessorStreams err_streams;
586
587                 if (Config->get_new_plugins_active()) {
588                         processor->activate ();
589                 }
590
591                 if (_route->add_processor (processor, _placement, &err_streams)) {
592                         weird_plugin_dialog (**p, err_streams);
593                         // XXX SHAREDPTR delete plugin here .. do we even need to care?
594                 } else {
595
596                         if (Profile->get_sae()) {
597                                 processor->activate ();
598                         }
599                         processor->ActiveChanged.connect (bind (
600                                         mem_fun (*this, &ProcessorBox::show_processor_active),
601                                         boost::weak_ptr<Processor>(processor)));
602                 }
603         }
604 }
605
606 void
607 ProcessorBox::weird_plugin_dialog (Plugin& p, Route::ProcessorStreams streams)
608 {
609         ArdourDialog dialog (_("ardour: weird plugin dialog"));
610         Label label;
611
612         string text = string_compose(_("You attempted to add the plugin \"%1\" at index %2.\n"),
613                         p.name(), streams.index);
614
615         bool has_midi  = streams.count.n_midi() > 0 || p.get_info()->n_inputs.n_midi() > 0;
616         bool has_audio = streams.count.n_audio() > 0 || p.get_info()->n_inputs.n_audio() > 0;
617
618         text += _("\nThis plugin has:\n");
619         if (has_midi) {
620                 text += string_compose("\t%1 ", p.get_info()->n_inputs.n_midi()) + _("MIDI input(s)\n");
621         }
622         if (has_audio) {
623                 text += string_compose("\t%1 ", p.get_info()->n_inputs.n_audio()) + _("audio input(s)\n");
624         }
625
626         text += _("\nBut at the insertion point, there are:\n");
627         if (has_midi) {
628                 text += string_compose("\t%1 ", streams.count.n_midi()) + _("MIDI channel(s)\n");
629         }
630         if (has_audio) {
631                 text += string_compose("\t%1 ", streams.count.n_audio()) + _("audio channel(s)\n");
632         }
633
634         text += _("\nArdour is unable to insert this plugin here.\n");
635         label.set_text(text);
636
637         dialog.get_vbox()->pack_start (label);
638         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
639
640         dialog.set_name (X_("PluginIODialog"));
641         dialog.set_position (Gtk::WIN_POS_MOUSE);
642         dialog.set_modal (true);
643         dialog.show_all ();
644
645         dialog.run ();
646 }
647
648 void
649 ProcessorBox::choose_insert ()
650 {
651         boost::shared_ptr<Processor> processor (new PortInsert (_session, _route->mute_master()));
652         processor->ActiveChanged.connect (bind (mem_fun(*this, &ProcessorBox::show_processor_active),
653                                                 boost::weak_ptr<Processor>(processor)));
654
655         _route->add_processor (processor, _placement);
656 }
657
658 void
659 ProcessorBox::choose_send ()
660 {
661         boost::shared_ptr<Send> send (new Send (_session, _route->mute_master()));
662
663         /* make an educated guess at the initial number of outputs for the send */
664         ChanCount outs = (_session.master_out())
665                         ? _session.master_out()->n_outputs()
666                         : _route->n_outputs();
667
668         /* XXX need processor lock on route */
669         try {
670                 send->output()->ensure_io (outs, false, this);
671         } catch (AudioEngine::PortRegistrationFailure& err) {
672                 error << string_compose (_("Cannot set up new send: %1"), err.what()) << endmsg;
673                 return;
674         }
675
676         /* let the user adjust the IO setup before creation.
677
678            Note: this dialog is NOT modal - we just leave it to run and it will
679            return when its Finished signal is emitted - typically when the window
680            is closed.
681          */
682
683         IOSelectorWindow *ios = new IOSelectorWindow (_session, send->output(), true);
684         ios->show_all ();
685
686         /* keep a reference to the send so it doesn't get deleted while
687            the IOSelectorWindow is doing its stuff
688         */
689         _processor_being_created = send;
690
691         ios->selector().Finished.connect (bind (
692                         mem_fun(*this, &ProcessorBox::send_io_finished),
693                         boost::weak_ptr<Processor>(send), ios));
694
695 }
696
697 void
698 ProcessorBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
699 {
700         boost::shared_ptr<Processor> processor (weak_processor.lock());
701
702         /* drop our temporary reference to the new send */
703         _processor_being_created.reset ();
704
705         if (!processor) {
706                 return;
707         }
708
709         switch (r) {
710         case IOSelector::Cancelled:
711                 // processor will go away when all shared_ptrs to it vanish
712                 break;
713
714         case IOSelector::Accepted:
715                 _route->add_processor (processor, _placement);
716                 if (Profile->get_sae()) {
717                         processor->activate ();
718                 }
719                 break;
720         }
721
722         delete_when_idle (ios);
723 }
724
725 void
726 ProcessorBox::choose_return ()
727 {
728         boost::shared_ptr<Return> retrn (new Return (_session));
729
730         /* assume user just wants a single audio input (sidechain) by default */
731         ChanCount ins(DataType::AUDIO, 1);
732
733         /* XXX need processor lock on route */
734         try {
735                 retrn->input()->ensure_io (ins, false, this);
736         } catch (AudioEngine::PortRegistrationFailure& err) {
737                 error << string_compose (_("Cannot set up new return: %1"), err.what()) << endmsg;
738                 return;
739         }
740
741         /* let the user adjust the IO setup before creation */
742         IOSelectorWindow *ios = new IOSelectorWindow (_session, retrn->input(), true);
743         ios->show_all ();
744
745         /* keep a reference to the return so it doesn't get deleted while
746            the IOSelectorWindow is doing its stuff */
747         _processor_being_created = retrn;
748
749         ios->selector().Finished.connect (bind (
750                         mem_fun(*this, &ProcessorBox::return_io_finished),
751                         boost::weak_ptr<Processor>(retrn), ios));
752 }
753
754 void
755 ProcessorBox::return_io_finished (IOSelector::Result r, boost::weak_ptr<Processor> weak_processor, IOSelectorWindow* ios)
756 {
757         boost::shared_ptr<Processor> processor (weak_processor.lock());
758
759         /* drop our temporary reference to the new return */
760         _processor_being_created.reset ();
761
762         if (!processor) {
763                 return;
764         }
765
766         switch (r) {
767         case IOSelector::Cancelled:
768                 // processor will go away when all shared_ptrs to it vanish
769                 break;
770
771         case IOSelector::Accepted:
772                 _route->add_processor (processor, _placement);
773                 if (Profile->get_sae()) {
774                         processor->activate ();
775                 }
776                 break;
777         }
778
779         delete_when_idle (ios);
780 }
781
782 void
783 ProcessorBox::redisplay_processors ()
784 {
785         ENSURE_GUI_THREAD (mem_fun(*this, &ProcessorBox::redisplay_processors));
786
787         if (no_processor_redisplay) {
788                 return;
789         }
790
791         ignore_delete = true;
792         model->clear ();
793         ignore_delete = false;
794
795         processor_active_connections.clear ();
796         processor_name_connections.clear ();
797
798         _route->foreach_processor (mem_fun (*this, &ProcessorBox::add_processor_to_display));
799
800         build_processor_tooltip (processor_eventbox, _("Inserts, sends & plugins:"));
801 }
802
803 void
804 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
805 {
806         boost::shared_ptr<Processor> processor (p.lock ());
807
808         if (!processor || !processor->display_to_user()) {
809                 return;
810         }
811
812         Gtk::TreeModel::Row row = *(model->append());
813         row[columns.text] = processor_name (processor);
814         row[columns.processor] = processor;
815
816         show_processor_active (processor);
817
818         processor_active_connections.push_back (processor->ActiveChanged.connect (bind (
819                         mem_fun(*this, &ProcessorBox::show_processor_active),
820                         boost::weak_ptr<Processor>(processor))));
821         processor_name_connections.push_back (processor->NameChanged.connect (bind (
822                         mem_fun(*this, &ProcessorBox::show_processor_name),
823                         boost::weak_ptr<Processor>(processor))));
824 }
825
826 string
827 ProcessorBox::processor_name (boost::weak_ptr<Processor> weak_processor)
828 {
829         boost::shared_ptr<Processor> processor (weak_processor.lock());
830
831         if (!processor) {
832                 return string();
833         }
834
835         boost::shared_ptr<Send> send;
836         string name_display;
837
838         if (!processor->active()) {
839                 name_display = " (";
840         }
841
842         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0 &&
843             !boost::dynamic_pointer_cast<InternalSend>(processor)) {
844
845                 name_display += '>';
846
847                 /* grab the send name out of its overall name */
848
849                 string::size_type lbracket, rbracket;
850                 lbracket = send->name().find ('[');
851                 rbracket = send->name().find (']');
852
853                 switch (_width) {
854                 case Wide:
855                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
856                         break;
857                 case Narrow:
858                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
859                         break;
860                 }
861
862         } else {
863
864                 switch (_width) {
865                 case Wide:
866                         name_display += processor->display_name();
867                         break;
868                 case Narrow:
869                         name_display += PBD::short_version (processor->display_name(), 5);
870                         break;
871                 }
872
873         }
874
875         if (!processor->active()) {
876                 name_display += ')';
877         }
878
879         return name_display;
880 }
881
882 void
883 ProcessorBox::build_processor_tooltip (EventBox& box, string start)
884 {
885         string tip(start);
886
887         Gtk::TreeModel::Children children = model->children();
888         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
889                 Gtk::TreeModel::Row row = *iter;
890                 tip += '\n';
891                 tip += row[columns.text];
892         }
893         ARDOUR_UI::instance()->tooltips().set_tip (box, tip);
894 }
895
896 void
897 ProcessorBox::show_processor_name (boost::weak_ptr<Processor> processor)
898 {
899         ENSURE_GUI_THREAD(bind (mem_fun(*this, &ProcessorBox::show_processor_name), processor));
900         show_processor_active (processor);
901 }
902
903 void
904 ProcessorBox::show_processor_active (boost::weak_ptr<Processor> weak_processor)
905 {
906         boost::shared_ptr<Processor> processor (weak_processor.lock());
907
908         if (!processor) {
909                 return;
910         }
911
912         ENSURE_GUI_THREAD(bind (mem_fun(*this, &ProcessorBox::show_processor_active), weak_processor));
913
914         Gtk::TreeModel::Children children = model->children();
915         Gtk::TreeModel::Children::iterator iter = children.begin();
916
917         while (iter != children.end()) {
918
919                 boost::shared_ptr<Processor> r = (*iter)[columns.processor];
920
921                 if (r == processor) {
922                         (*iter)[columns.text] = processor_name (r);
923
924                         if (processor->active()) {
925                                 (*iter)[columns.color] = *active_processor_color;
926                         } else {
927                                 (*iter)[columns.color] = *inactive_processor_color;
928                         }
929                         break;
930                 }
931
932                 iter++;
933         }
934 }
935
936 void
937 ProcessorBox::row_deleted (const Gtk::TreeModel::Path &)
938 {
939         if (!ignore_delete) {
940                 compute_processor_sort_keys ();
941         }
942 }
943
944 void
945 ProcessorBox::compute_processor_sort_keys ()
946 {
947         Gtk::TreeModel::Children children = model->children();
948         Route::ProcessorList our_processors;
949
950         for (Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
951                 boost::shared_ptr<Processor> p = (*iter)[columns.processor];
952                 our_processors.push_back ((*iter)[columns.processor]);
953         }
954
955         if (_route->reorder_processors (our_processors)) {
956
957                 /* reorder failed, so redisplay */
958
959                 redisplay_processors ();
960
961                 /* now tell them about the problem */
962
963                 ArdourDialog dialog (_("ardour: weird plugin dialog"));
964                 Label label;
965
966                 label.set_text (_("\
967 You cannot reorder this set of processors\n\
968 in that way because the inputs and\n\
969 outputs do not work correctly."));
970
971                 dialog.get_vbox()->pack_start (label);
972                 dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
973
974                 dialog.set_name (X_("PluginIODialog"));
975                 dialog.set_position (Gtk::WIN_POS_MOUSE);
976                 dialog.set_modal (true);
977                 dialog.show_all ();
978
979                 dialog.run ();
980         }
981 }
982
983 void
984 ProcessorBox::rename_processors ()
985 {
986         ProcSelection to_be_renamed;
987
988         get_selected_processors (to_be_renamed);
989
990         if (to_be_renamed.empty()) {
991                 return;
992         }
993
994         for (ProcSelection::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
995                 rename_processor (*i);
996         }
997 }
998
999 void
1000 ProcessorBox::cut_processors ()
1001 {
1002         ProcSelection to_be_removed;
1003
1004         get_selected_processors (to_be_removed);
1005 }
1006
1007 void
1008 ProcessorBox::cut_processors (const ProcSelection& to_be_removed)
1009 {
1010         if (to_be_removed.empty()) {
1011                 return;
1012         }
1013
1014         XMLNode* node = new XMLNode (X_("cut"));
1015         Route::ProcessorList to_cut;
1016
1017         no_processor_redisplay = true;
1018         for (ProcSelection::const_iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
1019                 // Cut only plugins, sends and returns
1020                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1021                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1022                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1023
1024                         void* gui = (*i)->get_gui ();
1025
1026                         if (gui) {
1027                                 static_cast<Gtk::Widget*>(gui)->hide ();
1028                         }
1029
1030                         XMLNode& child ((*i)->get_state());
1031                         node->add_child_nocopy (child);
1032                         to_cut.push_back (*i);
1033                 }
1034         }
1035
1036         if (_route->remove_processors (to_cut) != 0) {
1037                 delete node;
1038                 no_processor_redisplay = false;
1039                 return;
1040         }
1041
1042         _rr_selection.set (node);
1043
1044         no_processor_redisplay = false;
1045         redisplay_processors ();
1046 }
1047
1048 void
1049 ProcessorBox::copy_processors ()
1050 {
1051         ProcSelection to_be_copied;
1052         get_selected_processors (to_be_copied);
1053         copy_processors (to_be_copied);
1054 }
1055
1056 void
1057 ProcessorBox::copy_processors (const ProcSelection& to_be_copied)
1058 {
1059         if (to_be_copied.empty()) {
1060                 return;
1061         }
1062
1063         XMLNode* node = new XMLNode (X_("copy"));
1064
1065         for (ProcSelection::const_iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
1066                 // Copy only plugins, sends, returns
1067                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
1068                     (boost::dynamic_pointer_cast<Send>((*i)) != 0) ||
1069                     (boost::dynamic_pointer_cast<Return>((*i)) != 0)) {
1070                         node->add_child_nocopy ((*i)->get_state());
1071                 }
1072         }
1073
1074         _rr_selection.set (node);
1075 }
1076
1077 void
1078 ProcessorBox::delete_processors ()
1079 {
1080         ProcSelection to_be_deleted;
1081         get_selected_processors (to_be_deleted);
1082         delete_processors (to_be_deleted);
1083 }
1084
1085 void
1086 ProcessorBox::delete_processors (const ProcSelection& targets)
1087 {
1088         if (targets.empty()) {
1089                 return;
1090         }
1091
1092         no_processor_redisplay = true;
1093
1094         for (ProcSelection::const_iterator i = targets.begin(); i != targets.end(); ++i) {
1095
1096                 void* gui = (*i)->get_gui ();
1097
1098                 if (gui) {
1099                         static_cast<Gtk::Widget*>(gui)->hide ();
1100                 }
1101
1102                 _route->remove_processor(*i);
1103         }
1104
1105         no_processor_redisplay = false;
1106         redisplay_processors ();
1107 }
1108
1109 void
1110 ProcessorBox::delete_dragged_processors (const list<boost::shared_ptr<Processor> >& procs)
1111 {
1112         list<boost::shared_ptr<Processor> >::const_iterator x;
1113
1114         no_processor_redisplay = true;
1115         for (x = procs.begin(); x != procs.end(); ++x) {
1116
1117                 void* gui = (*x)->get_gui ();
1118
1119                 if (gui) {
1120                         static_cast<Gtk::Widget*>(gui)->hide ();
1121                 }
1122
1123                 _route->remove_processor(*x);
1124         }
1125
1126         no_processor_redisplay = false;
1127         redisplay_processors ();
1128 }
1129
1130 gint
1131 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
1132 {
1133         boost::shared_ptr<Processor> processor (weak_processor.lock());
1134
1135         if (!processor) {
1136                 return false;
1137         }
1138
1139         /* NOT copied to _mixer.selection() */
1140
1141         no_processor_redisplay = true;
1142         _route->remove_processor (processor);
1143         no_processor_redisplay = false;
1144         redisplay_processors ();
1145
1146         return false;
1147 }
1148
1149 void
1150 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
1151 {
1152         ArdourPrompter name_prompter (true);
1153         string result;
1154         name_prompter.set_prompt (_("rename processor"));
1155         name_prompter.set_initial_text (processor->name());
1156         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
1157         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1158         name_prompter.show_all ();
1159
1160         switch (name_prompter.run ()) {
1161
1162         case Gtk::RESPONSE_ACCEPT:
1163                 name_prompter.get_result (result);
1164                 if (result.length()) {
1165                         if (_session.route_by_name (result)) {
1166                                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1167                                 return;
1168                         }
1169                         processor->set_name (result);
1170                 }
1171                 break;
1172         }
1173
1174         return;
1175 }
1176
1177 void
1178 ProcessorBox::paste_processors ()
1179 {
1180         if (_rr_selection.processors.empty()) {
1181                 return;
1182         }
1183
1184         paste_processor_state (_rr_selection.processors.get_node().children(), boost::shared_ptr<Processor>());
1185 }
1186
1187 void
1188 ProcessorBox::paste_processors (boost::shared_ptr<Processor> before)
1189 {
1190
1191         if (_rr_selection.processors.empty()) {
1192                 return;
1193         }
1194
1195         paste_processor_state (_rr_selection.processors.get_node().children(), before);
1196 }
1197
1198 void
1199 ProcessorBox::paste_processor_state (const XMLNodeList& nlist, boost::shared_ptr<Processor> p)
1200 {
1201         XMLNodeConstIterator niter;
1202         list<boost::shared_ptr<Processor> > copies;
1203
1204         if (nlist.empty()) {
1205                 return;
1206         }
1207
1208         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1209
1210                 XMLProperty const * type = (*niter)->property ("type");
1211                 assert (type);
1212
1213                 boost::shared_ptr<Processor> p;
1214                 try {
1215                         if (type->value() == "meter" ||
1216                             type->value() == "main-outs" ||
1217                             type->value() == "amp" ||
1218                             type->value() == "intsend" || type->value() == "intreturn") {
1219                                 /* do not paste meter, main outs, amp or internal send/returns */
1220                                 continue;
1221
1222                         } else if (type->value() == "send") {
1223
1224                                 XMLNode n (**niter);
1225                                 Send::make_unique (n, _session);
1226                                 p.reset (new Send (_session, _route->mute_master(), n));
1227
1228                         } else if (type->value() == "return") {
1229
1230                                 XMLNode n (**niter);
1231                                 Return::make_unique (n, _session);
1232                                 p.reset (new Return (_session, **niter));
1233
1234                         } else {
1235                                 /* XXX its a bit limiting to assume that everything else
1236                                    is a plugin.
1237                                 */
1238                                 p.reset (new PluginInsert (_session, **niter));
1239                         }
1240
1241                         copies.push_back (p);
1242                 }
1243
1244                 catch (...) {
1245                         cerr << "plugin insert constructor failed\n";
1246                 }
1247         }
1248
1249         if (copies.empty()) {
1250                 return;
1251         }
1252
1253         if (_route->add_processors (copies, p)) {
1254
1255                 string msg = _(
1256                         "Copying the set of processors on the clipboard failed,\n\
1257 probably because the I/O configuration of the plugins\n\
1258 could not match the configuration of this track.");
1259                 MessageDialog am (msg);
1260                 am.run ();
1261         }
1262 }
1263
1264 void
1265 ProcessorBox::activate_processor (boost::shared_ptr<Processor> r)
1266 {
1267         r->activate ();
1268 }
1269
1270 void
1271 ProcessorBox::deactivate_processor (boost::shared_ptr<Processor> r)
1272 {
1273         r->deactivate ();
1274 }
1275
1276 void
1277 ProcessorBox::get_selected_processors (ProcSelection& processors)
1278 {
1279     vector<Gtk::TreeModel::Path> pathlist = processor_display.get_selection()->get_selected_rows();
1280
1281     for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
1282             processors.push_back ((*(model->get_iter(*iter)))[columns.processor]);
1283     }
1284 }
1285
1286 void
1287 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
1288 {
1289     vector<Gtk::TreeModel::Path> pathlist = processor_display.get_selection()->get_selected_rows();
1290
1291         for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
1292                 boost::shared_ptr<Processor> processor = (*(model->get_iter(*iter)))[columns.processor];
1293                 (this->*method)(processor);
1294         }
1295 }
1296
1297 void
1298 ProcessorBox::all_processors_active (bool state)
1299 {
1300         _route->all_processors_active (_placement, state);
1301 }
1302
1303 void
1304 ProcessorBox::ab_plugins ()
1305 {
1306         _route->ab_plugins (ab_direction);
1307         ab_direction = !ab_direction;
1308 }
1309
1310
1311 void
1312 ProcessorBox::clear_processors ()
1313 {
1314         string prompt;
1315         vector<string> choices;
1316
1317         prompt = string_compose (_("Do you really want to remove all processors from %1?\n"
1318                                    "(this cannot be undone)"), _route->name());
1319
1320         choices.push_back (_("Cancel"));
1321         choices.push_back (_("Yes, remove them all"));
1322
1323         Gtkmm2ext::Choice prompter (prompt, choices);
1324
1325         if (prompter.run () == 1) {
1326                 _route->clear_processors (PreFader);
1327                 _route->clear_processors (PostFader);
1328         }
1329 }
1330
1331 void
1332 ProcessorBox::clear_processors (Placement p)
1333 {
1334         string prompt;
1335         vector<string> choices;
1336
1337         if (p == PreFader) {
1338                 prompt = string_compose (_("Do you really want to remove all pre-fader processors from %1?\n"
1339                                            "(this cannot be undone)"), _route->name());
1340         } else {
1341                 prompt = string_compose (_("Do you really want to remove all post-fader processors from %1?\n"
1342                                            "(this cannot be undone)"), _route->name());
1343         }
1344
1345         choices.push_back (_("Cancel"));
1346         choices.push_back (_("Yes, remove them all"));
1347
1348         Gtkmm2ext::Choice prompter (prompt, choices);
1349
1350         if (prompter.run () == 1) {
1351                 _route->clear_processors (p);
1352         }
1353 }
1354
1355 void
1356 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
1357 {
1358         boost::shared_ptr<Send> send;
1359         boost::shared_ptr<Return> retrn;
1360         boost::shared_ptr<PluginInsert> plugin_insert;
1361         boost::shared_ptr<PortInsert> port_insert;
1362         Window* gidget = 0;
1363
1364         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1365
1366                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1367                         return;
1368                 }
1369         }
1370
1371         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1372
1373                 if (!_session.engine().connected()) {
1374                         return;
1375                 }
1376
1377 #ifdef OLD_SEND_EDITING
1378                 SendUIWindow *send_ui;
1379
1380                 if (send->get_gui() == 0) {
1381                         send_ui = new SendUIWindow (send, _session);
1382                         send_ui->set_title (send->name());
1383                         send->set_gui (send_ui);
1384
1385                 } else {
1386                         send_ui = reinterpret_cast<SendUIWindow *> (send->get_gui());
1387                 }
1388
1389                 gidget = send_ui;
1390 #else
1391                 if (_parent_strip) {
1392                         _parent_strip->gain_meter().set_controls (_route, send->meter(), send->amp());
1393                         _parent_strip->panner_ui().set_panner (send->panner());
1394                 }
1395 #endif
1396
1397         } else if ((retrn = boost::dynamic_pointer_cast<Return> (processor)) != 0) {
1398
1399                 if (!_session.engine().connected()) {
1400                         return;
1401                 }
1402
1403                 boost::shared_ptr<Return> retrn = boost::dynamic_pointer_cast<Return> (processor);
1404
1405                 ReturnUIWindow *return_ui;
1406
1407                 if (retrn->get_gui() == 0) {
1408
1409                         return_ui = new ReturnUIWindow (retrn, _session);
1410                         return_ui->set_title (retrn->name ());
1411                         send->set_gui (return_ui);
1412
1413                 } else {
1414                         return_ui = reinterpret_cast<ReturnUIWindow *> (retrn->get_gui());
1415                 }
1416
1417                 gidget = return_ui;
1418
1419         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1420
1421                 PluginUIWindow *plugin_ui;
1422
1423                 /* these are both allowed to be null */
1424
1425                 Container* toplevel = get_toplevel();
1426                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1427
1428                 if (plugin_insert->get_gui() == 0) {
1429
1430                         plugin_ui = new PluginUIWindow (win, plugin_insert);
1431                         plugin_ui->set_title (generate_processor_title (plugin_insert));
1432                         plugin_insert->set_gui (plugin_ui);
1433
1434                 } else {
1435                         plugin_ui = reinterpret_cast<PluginUIWindow *> (plugin_insert->get_gui());
1436                         plugin_ui->set_parent (win);
1437                 }
1438
1439                 gidget = plugin_ui;
1440
1441         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
1442
1443                 if (!_session.engine().connected()) {
1444                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1445                         msg.run ();
1446                         return;
1447                 }
1448
1449                 PortInsertWindow *io_selector;
1450
1451                 if (port_insert->get_gui() == 0) {
1452                         io_selector = new PortInsertWindow (_session, port_insert);
1453                         port_insert->set_gui (io_selector);
1454
1455                 } else {
1456                         io_selector = reinterpret_cast<PortInsertWindow *> (port_insert->get_gui());
1457                 }
1458
1459                 gidget = io_selector;
1460         }
1461
1462         if (gidget) {
1463                 if (gidget->is_visible()) {
1464                         gidget->get_window()->raise ();
1465                 } else {
1466                         gidget->show_all ();
1467                         gidget->present ();
1468                 }
1469         }
1470 }
1471
1472 void
1473 ProcessorBox::register_actions ()
1474 {
1475         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("processormenu"));
1476         Glib::RefPtr<Action> act;
1477
1478         /* new stuff */
1479         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),
1480                         sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
1481
1482         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),
1483                         sigc::ptr_fun (ProcessorBox::rb_choose_insert));
1484         ActionManager::jack_sensitive_actions.push_back (act);
1485         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New Send ..."),
1486                         sigc::ptr_fun (ProcessorBox::rb_choose_send));
1487         ActionManager::jack_sensitive_actions.push_back (act);
1488         act = ActionManager::register_action (popup_act_grp, X_("newreturn"), _("New Return ..."),
1489                         sigc::ptr_fun (ProcessorBox::rb_choose_return));
1490         ActionManager::jack_sensitive_actions.push_back (act);
1491
1492         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear (all)"),
1493                         sigc::ptr_fun (ProcessorBox::rb_clear));
1494         ActionManager::register_action (popup_act_grp, X_("clear_pre"), _("Clear (pre-fader)"),
1495                         sigc::ptr_fun (ProcessorBox::rb_clear_pre));
1496         ActionManager::register_action (popup_act_grp, X_("clear_post"), _("Clear (post-fader)"),
1497                         sigc::ptr_fun (ProcessorBox::rb_clear_post));
1498
1499         /* standard editing stuff */
1500         act = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),
1501                         sigc::ptr_fun (ProcessorBox::rb_cut));
1502         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1503         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),
1504                         sigc::ptr_fun (ProcessorBox::rb_copy));
1505         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1506
1507         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),
1508                         sigc::ptr_fun (ProcessorBox::rb_delete));
1509         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1510
1511         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),
1512                         sigc::ptr_fun (ProcessorBox::rb_paste));
1513         act = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),
1514                         sigc::ptr_fun (ProcessorBox::rb_rename));
1515         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1516         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),
1517                         sigc::ptr_fun (ProcessorBox::rb_select_all));
1518         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),
1519                         sigc::ptr_fun (ProcessorBox::rb_deselect_all));
1520
1521         /* activation */
1522         act = ActionManager::register_action (popup_act_grp, X_("activate"), _("Activate"),
1523                         sigc::ptr_fun (ProcessorBox::rb_activate));
1524         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1525         act = ActionManager::register_action (popup_act_grp, X_("deactivate"), _("Deactivate"),
1526                         sigc::ptr_fun (ProcessorBox::rb_deactivate));
1527         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1528         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate all"),
1529                         sigc::ptr_fun (ProcessorBox::rb_activate_all));
1530         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate all"),
1531                         sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
1532
1533         /* show editors */
1534         act = ActionManager::register_action (popup_act_grp, X_("edit"), _("Edit"),
1535                         sigc::ptr_fun (ProcessorBox::rb_edit));
1536         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1537
1538         ActionManager::add_action_group (popup_act_grp);
1539 }
1540
1541 void
1542 ProcessorBox::rb_choose_plugin ()
1543 {
1544         if (_current_processor_box == 0) {
1545                 return;
1546         }
1547         _current_processor_box->choose_plugin ();
1548 }
1549
1550 void
1551 ProcessorBox::rb_choose_insert ()
1552 {
1553         if (_current_processor_box == 0) {
1554                 return;
1555         }
1556         _current_processor_box->choose_insert ();
1557 }
1558
1559 void
1560 ProcessorBox::rb_choose_send ()
1561 {
1562         if (_current_processor_box == 0) {
1563                 return;
1564         }
1565         _current_processor_box->choose_send ();
1566 }
1567
1568 void
1569 ProcessorBox::rb_choose_return ()
1570 {
1571         if (_current_processor_box == 0) {
1572                 return;
1573         }
1574         _current_processor_box->choose_return ();
1575 }
1576
1577 void
1578 ProcessorBox::rb_clear ()
1579 {
1580         if (_current_processor_box == 0) {
1581                 return;
1582         }
1583
1584         _current_processor_box->clear_processors ();
1585 }
1586
1587
1588 void
1589 ProcessorBox::rb_clear_pre ()
1590 {
1591         if (_current_processor_box == 0) {
1592                 return;
1593         }
1594
1595         _current_processor_box->clear_processors (PreFader);
1596 }
1597
1598
1599 void
1600 ProcessorBox::rb_clear_post ()
1601 {
1602         if (_current_processor_box == 0) {
1603                 return;
1604         }
1605
1606         _current_processor_box->clear_processors (PostFader);
1607 }
1608
1609 void
1610 ProcessorBox::rb_cut ()
1611 {
1612         if (_current_processor_box == 0) {
1613                 return;
1614         }
1615
1616         _current_processor_box->cut_processors ();
1617 }
1618
1619 void
1620 ProcessorBox::rb_delete ()
1621 {
1622         if (_current_processor_box == 0) {
1623                 return;
1624         }
1625
1626         _current_processor_box->delete_processors ();
1627 }
1628
1629 void
1630 ProcessorBox::rb_copy ()
1631 {
1632         if (_current_processor_box == 0) {
1633                 return;
1634         }
1635         _current_processor_box->copy_processors ();
1636 }
1637
1638 void
1639 ProcessorBox::rb_paste ()
1640 {
1641         if (_current_processor_box == 0) {
1642                 return;
1643         }
1644
1645         _current_processor_box->paste_processors ();
1646 }
1647
1648 void
1649 ProcessorBox::rb_rename ()
1650 {
1651         if (_current_processor_box == 0) {
1652                 return;
1653         }
1654         _current_processor_box->rename_processors ();
1655 }
1656
1657 void
1658 ProcessorBox::rb_select_all ()
1659 {
1660         if (_current_processor_box == 0) {
1661                 return;
1662         }
1663
1664         _current_processor_box->select_all_processors ();
1665 }
1666
1667 void
1668 ProcessorBox::rb_deselect_all ()
1669 {
1670         if (_current_processor_box == 0) {
1671                 return;
1672         }
1673
1674         _current_processor_box->deselect_all_processors ();
1675 }
1676
1677 void
1678 ProcessorBox::rb_activate ()
1679 {
1680         if (_current_processor_box == 0) {
1681                 return;
1682         }
1683
1684         _current_processor_box->for_selected_processors (&ProcessorBox::activate_processor);
1685 }
1686
1687 void
1688 ProcessorBox::rb_deactivate ()
1689 {
1690         if (_current_processor_box == 0) {
1691                 return;
1692         }
1693         _current_processor_box->for_selected_processors (&ProcessorBox::deactivate_processor);
1694 }
1695
1696 void
1697 ProcessorBox::rb_activate_all ()
1698 {
1699         if (_current_processor_box == 0) {
1700                 return;
1701         }
1702
1703         _current_processor_box->all_processors_active (true);
1704 }
1705
1706 void
1707 ProcessorBox::rb_deactivate_all ()
1708 {
1709         if (_current_processor_box == 0) {
1710                 return;
1711         }
1712         _current_processor_box->all_processors_active (false);
1713 }
1714
1715 void
1716 ProcessorBox::rb_edit ()
1717 {
1718         if (_current_processor_box == 0) {
1719                 return;
1720         }
1721
1722         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
1723 }
1724
1725 void
1726 ProcessorBox::route_name_changed ()
1727 {
1728         ENSURE_GUI_THREAD (mem_fun (*this, &ProcessorBox::route_name_changed));
1729
1730         boost::shared_ptr<Processor> processor;
1731         boost::shared_ptr<PluginInsert> plugin_insert;
1732         boost::shared_ptr<Send> send;
1733
1734         Gtk::TreeModel::Children children = model->children();
1735
1736         for (Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
1737                 Gtk::TreeModel::Row row = *iter;
1738
1739                 processor= row[columns.processor];
1740
1741                 void* gui = processor->get_gui();
1742
1743                 if (!gui) {
1744                         continue;
1745                 }
1746
1747                 /* rename editor windows for sends and plugins */
1748
1749                 if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1750                         static_cast<Window*>(gui)->set_title (send->name ());
1751                 } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1752                         static_cast<Window*>(gui)->set_title (generate_processor_title (plugin_insert));
1753                 }
1754         }
1755 }
1756
1757 string
1758 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
1759 {
1760         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
1761         string::size_type email_pos;
1762
1763         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1764                 maker = maker.substr (0, email_pos - 1);
1765         }
1766
1767         if (maker.length() > 32) {
1768                 maker = maker.substr (0, 32);
1769                 maker += " ...";
1770         }
1771
1772         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);
1773 }
1774