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