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