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