add note onset detection to the ferret, c/o the aubio-based Onset VAMP plugin (REQUIR...
[ardour.git] / gtk2_ardour / redirect_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 #include <cmath>
21 #include <iostream>
22
23 #include <sigc++/bind.h>
24
25 #include <pbd/convert.h>
26
27 #include <glibmm/miscutils.h>
28
29 #include <gtkmm/messagedialog.h>
30
31 #include <gtkmm2ext/gtk_ui.h>
32 #include <gtkmm2ext/utils.h>
33 #include <gtkmm2ext/choice.h>
34 #include <gtkmm2ext/utils.h>
35 #include <gtkmm2ext/stop_signal.h>
36 #include <gtkmm2ext/doi.h>
37 #include <gtkmm2ext/window_title.h>
38
39 #include <ardour/ardour.h>
40 #include <ardour/session.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/route.h>
43 #include <ardour/audio_track.h>
44 #include <ardour/audio_diskstream.h>
45 #include <ardour/send.h>
46 #include <ardour/insert.h>
47 #include <ardour/ladspa_plugin.h>
48 #include <ardour/connection.h>
49 #include <ardour/session_connection.h>
50 #include <ardour/profile.h>
51
52 #include "ardour_ui.h"
53 #include "ardour_dialog.h"
54 #include "public_editor.h"
55 #include "redirect_box.h"
56 #include "keyboard.h"
57 #include "plugin_selector.h"
58 #include "route_redirect_selection.h"
59 #include "mixer_ui.h"
60 #include "actions.h"
61 #include "plugin_ui.h"
62 #include "send_ui.h"
63 #include "io_selector.h"
64 #include "utils.h"
65 #include "gui_thread.h"
66
67 #include "i18n.h"
68
69 #ifdef HAVE_AUDIOUNITS
70 class AUPluginUI;
71 #endif
72
73 using namespace sigc;
74 using namespace ARDOUR;
75 using namespace PBD;
76 using namespace Gtk;
77 using namespace Glib;
78 using namespace Gtkmm2ext;
79
80 RedirectBox* RedirectBox::_current_redirect_box = 0;
81 RefPtr<Action> RedirectBox::paste_action;
82 bool RedirectBox::get_colors = true;
83 Gdk::Color* RedirectBox::active_redirect_color;
84 Gdk::Color* RedirectBox::inactive_redirect_color;
85
86 RedirectBox::RedirectBox (Placement pcmnt, Session& sess, boost::shared_ptr<Route> rt, PluginSelector &plugsel, 
87                           RouteRedirectSelection & rsel, bool owner_is_mixer)
88         : _route(rt), 
89           _session(sess), 
90           _owner_is_mixer (owner_is_mixer), 
91           _placement(pcmnt), 
92           _plugin_selector(plugsel),
93           _rr_selection(rsel)
94 {
95         if (get_colors) {
96                 active_redirect_color = new Gdk::Color;
97                 inactive_redirect_color = new Gdk::Color;
98                 set_color (*active_redirect_color, rgba_from_style ("RedirectSelector", 0xff, 0, 0, 0, "fg", Gtk::STATE_ACTIVE, false ));
99                 set_color (*inactive_redirect_color, rgba_from_style ("RedirectSelector", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
100                 get_colors = false;
101         }
102
103         _width = Wide;
104         redirect_menu = 0;
105         send_action_menu = 0;
106         redirect_drag_in_progress = false;
107         no_redirect_redisplay = false;
108         ignore_delete = false;
109
110         model = ListStore::create(columns);
111
112         RefPtr<TreeSelection> selection = redirect_display.get_selection();
113         selection->set_mode (Gtk::SELECTION_MULTIPLE);
114         selection->signal_changed().connect (mem_fun (*this, &RedirectBox::selection_changed));
115
116         redirect_display.set_model (model);
117         redirect_display.append_column (X_("notshown"), columns.text);
118         redirect_display.set_name ("RedirectSelector");
119         redirect_display.set_headers_visible (false);
120         redirect_display.set_reorderable (true);
121         redirect_display.set_size_request (-1, 40);
122         redirect_display.get_column(0)->set_sizing(TREE_VIEW_COLUMN_FIXED);
123         redirect_display.get_column(0)->set_fixed_width(48);
124         redirect_display.add_object_drag (columns.redirect.index(), "redirects");
125         redirect_display.signal_object_drop.connect (mem_fun (*this, &RedirectBox::object_drop));
126
127         TreeViewColumn* name_col = redirect_display.get_column(0);
128         CellRendererText* renderer = dynamic_cast<CellRendererText*>(redirect_display.get_column_cell_renderer (0));
129         name_col->add_attribute(renderer->property_foreground_gdk(), columns.color);
130
131         redirect_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
132         
133         model->signal_row_deleted().connect (mem_fun (*this, &RedirectBox::row_deleted));
134
135         redirect_scroller.add (redirect_display);
136         redirect_eventbox.add (redirect_scroller);
137         
138         redirect_scroller.set_size_request (-1, 40);
139
140         pack_start (redirect_eventbox, true, true);
141
142         _route->redirects_changed.connect (mem_fun(*this, &RedirectBox::redisplay_redirects));
143         _route->GoingAway.connect (mem_fun (*this, &RedirectBox::route_going_away));
144
145         redirect_eventbox.signal_enter_notify_event().connect (bind (sigc::ptr_fun (RedirectBox::enter_box), this));
146
147         redirect_display.signal_button_press_event().connect (mem_fun(*this, &RedirectBox::redirect_button_press_event), false);
148         redirect_display.signal_button_release_event().connect (mem_fun(*this, &RedirectBox::redirect_button_release_event));
149
150         using_plugin_selector = false;
151         _plugin_selector.signal_hide().connect (mem_fun (*this, &RedirectBox::plugin_selector_hidden));
152         _plugin_selector.signal_show().connect (mem_fun (*this, &RedirectBox::plugin_selector_shown));
153
154         /* start off as a passthru strip. we'll correct this, if necessary,
155            in update_diskstream_display().
156         */
157
158         /* now force an update of all the various elements */
159
160         redisplay_redirects (0);
161 }
162
163 RedirectBox::~RedirectBox ()
164 {
165 }
166
167 void
168 RedirectBox::route_going_away ()
169 {
170         /* don't keep updating display as redirects are deleted */
171         no_redirect_redisplay = true;
172 }
173
174 void
175 RedirectBox::object_drop (string type, uint32_t cnt, const boost::shared_ptr<Redirect>* ptr)
176 {
177         if (type != "redirects" || cnt == 0 || !ptr) {
178                 return;
179         }
180
181         /* do something with the dropped redirects */
182
183         list<boost::shared_ptr<Redirect> > redirects;
184         
185         for (uint32_t n = 0; n < cnt; ++n) {
186                 redirects.push_back (ptr[n]);
187         }
188         
189         paste_redirect_list (redirects);
190 }
191
192 void
193 RedirectBox::update()
194 {
195         redisplay_redirects (0);
196 }
197
198
199 void
200 RedirectBox::set_width (Width w)
201 {
202         if (_width == w) {
203                 return;
204         }
205         _width = w;
206
207         redisplay_redirects (0);
208 }
209
210 void
211 RedirectBox::remove_redirect_gui (boost::shared_ptr<Redirect> redirect)
212 {
213         boost::shared_ptr<Insert> insert;
214         boost::shared_ptr<Send> send;
215         boost::shared_ptr<PortInsert> port_insert;
216
217         if ((insert = boost::dynamic_pointer_cast<Insert> (redirect)) != 0) {
218
219                 if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (insert)) != 0) {
220                         PortInsertUI *io_selector = reinterpret_cast<PortInsertUI *> (port_insert->get_gui());
221                         port_insert->set_gui (0);
222                         delete io_selector;
223                 } 
224
225         } else if ((send = boost::dynamic_pointer_cast<Send> (insert)) != 0) {
226                 SendUIWindow *sui = reinterpret_cast<SendUIWindow*> (send->get_gui());
227                 send->set_gui (0);
228                 delete sui;
229         }
230 }
231
232 void 
233 RedirectBox::build_send_action_menu ()
234
235 {
236         using namespace Menu_Helpers;
237
238         send_action_menu = new Menu;
239         send_action_menu->set_name ("ArdourContextMenu");
240         MenuList& items = send_action_menu->items();
241
242         items.push_back (MenuElem (_("New send"), mem_fun(*this, &RedirectBox::new_send)));
243         items.push_back (MenuElem (_("Show send controls"), mem_fun(*this, &RedirectBox::show_send_controls)));
244 }
245
246 void
247 RedirectBox::show_send_controls ()
248
249 {
250 }
251
252 void
253 RedirectBox::new_send ()
254
255 {
256 }
257
258 void
259 RedirectBox::show_redirect_menu (gint arg)
260 {
261         if (redirect_menu == 0) {
262                 redirect_menu = build_redirect_menu ();
263         }
264
265         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/redirectmenu/newplugin"));
266
267         if (plugin_menu_item) {
268                 plugin_menu_item->set_submenu (_plugin_selector.plugin_menu());
269         }
270
271         paste_action->set_sensitive (!_rr_selection.redirects.empty());
272
273         redirect_menu->popup (1, arg);
274 }
275
276 void
277 RedirectBox::redirect_drag_begin (GdkDragContext *context)
278 {
279         redirect_drag_in_progress = true;
280 }
281
282 void
283 RedirectBox::redirect_drag_end (GdkDragContext *context)
284 {
285         redirect_drag_in_progress = false;
286 }
287
288 bool
289 RedirectBox::redirect_button_press_event (GdkEventButton *ev)
290 {
291         TreeIter iter;
292         TreeModel::Path path;
293         TreeViewColumn* column;
294         int cellx;
295         int celly;
296         boost::shared_ptr<Redirect> redirect;
297         int ret = false;
298         bool selected = false;
299
300         if (redirect_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
301                 if ((iter = model->get_iter (path))) {
302                         redirect = (*iter)[columns.redirect];
303                         selected = redirect_display.get_selection()->is_selected (iter);
304                 }
305                 
306         }
307
308         if (redirect && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
309                 
310                 if (_session.engine().connected()) {
311                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
312                         edit_redirect (redirect);
313                 }
314                 ret = true;
315                 
316         } else if (redirect && ev->button == 1 && selected) {
317
318                 // this is purely informational but necessary
319                 RedirectSelected (redirect); // emit
320         }
321         
322         return ret;
323 }
324
325 bool
326 RedirectBox::redirect_button_release_event (GdkEventButton *ev)
327 {
328         TreeIter iter;
329         TreeModel::Path path;
330         TreeViewColumn* column;
331         int cellx;
332         int celly;
333         boost::shared_ptr<Redirect> redirect;
334         int ret = false;
335
336
337         if (redirect_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
338                 if ((iter = model->get_iter (path))) {
339                         redirect = (*iter)[columns.redirect];
340                 }
341         }
342
343         if (redirect && Keyboard::is_delete_event (ev)) {
344                 
345                 Glib::signal_idle().connect (bind (mem_fun(*this, &RedirectBox::idle_delete_redirect), boost::weak_ptr<Redirect>(redirect)));
346                 ret = true;
347                 
348         } else if (Keyboard::is_context_menu_event (ev)) {
349
350                 show_redirect_menu(ev->time);
351                 ret = true;
352
353         } else if (redirect && (ev->button == 2) && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))) {
354                 
355                 /* button2-click with no modifiers */
356
357                 redirect->set_active (!redirect->active(), this);
358                 ret = true;
359
360         } 
361
362         return ret;
363 }
364
365 Menu *
366 RedirectBox::build_redirect_menu ()
367 {
368         redirect_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/redirectmenu") );
369         redirect_menu->set_name ("ArdourContextMenu");
370
371         show_all_children();
372
373         return redirect_menu;
374 }
375
376 void
377 RedirectBox::selection_changed ()
378 {
379         bool sensitive = (redirect_display.get_selection()->count_selected_rows()) ? true : false;
380         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
381 }
382
383 void
384 RedirectBox::select_all_redirects ()
385 {
386         redirect_display.get_selection()->select_all();
387 }
388
389 void
390 RedirectBox::deselect_all_redirects ()
391 {
392         redirect_display.get_selection()->unselect_all();
393 }
394
395 void
396 RedirectBox::choose_plugin ()
397 {
398         newplug_connection = _plugin_selector.PluginCreated.connect (mem_fun(*this,&RedirectBox::insert_plugin_chosen));
399         // Gtk::Menu& m = _plugin_selector.plugin_menu();
400         // m.popup (1, 0);
401 }
402
403 void
404 RedirectBox::insert_plugin_chosen (boost::shared_ptr<Plugin> plugin)
405 {
406         if (plugin) {
407
408                 boost::shared_ptr<Redirect> redirect (new PluginInsert (_session, plugin, _placement));
409                 
410                 uint32_t err_streams;
411
412                 if (_route->add_redirect (redirect, this, &err_streams)) {
413                         weird_plugin_dialog (*plugin, err_streams, _route);
414                 } else {
415                         if (Profile->get_sae()) {
416                                 redirect->set_active (true, 0);
417                         }
418                         redirect->active_changed.connect (bind (mem_fun (*this, &RedirectBox::show_redirect_active_r), boost::weak_ptr<Redirect>(redirect)));
419                 }
420         }
421
422         if (!using_plugin_selector) {
423                 newplug_connection.disconnect();
424         }
425 }
426
427 void
428 RedirectBox::weird_plugin_dialog (Plugin& p, uint32_t streams, boost::shared_ptr<IO> io)
429 {
430         ArdourDialog dialog (_("ardour: weird plugin dialog"));
431         Label label;
432
433         /* i hate this kind of code */
434
435         if (streams > (unsigned)p.get_info()->n_inputs) {
436                 label.set_text (string_compose (_(
437 "You attempted to add a plugin (%1).\n"
438 "The plugin has %2 inputs\n"
439 "but at the insertion point, there are\n"
440 "%3 active signal streams.\n"
441 "\n"
442 "This makes no sense - you are throwing away\n"
443 "part of the signal."),
444                                          p.name(),
445                                          p.get_info()->n_inputs,
446                                          streams));
447         } else if (streams < (unsigned)p.get_info()->n_inputs) {
448                 label.set_text (string_compose (_(
449 "You attempted to add a plugin (%1).\n"
450 "The plugin has %2 inputs\n"
451 "but at the insertion point there are\n"
452 "only %3 active signal streams.\n"
453 "\n"
454 "This makes no sense - unless the plugin supports\n"
455 "side-chain inputs. A future version of Ardour will\n"
456 "support this type of configuration."),
457                                          p.name(),
458                                          p.get_info()->n_inputs,
459                                          streams));
460         } else {
461                 label.set_text (string_compose (_(
462 "You attempted to add a plugin (%1).\n"
463 "\n"
464 "The I/O configuration doesn't make sense:\n"
465 "\n" 
466 "The plugin has %2 inputs and %3 outputs.\n"
467 "The track/bus has %4 inputs and %5 outputs.\n"
468 "The insertion point, has %6 active signals.\n"
469 "\n"
470 "Ardour does not understand what to do in such situations.\n"),
471                                          p.name(),
472                                          p.get_info()->n_inputs,
473                                          p.get_info()->n_outputs,
474                                          io->n_inputs(),
475                                          io->n_outputs(),
476                                          streams));
477         }
478
479         dialog.set_border_width (PublicEditor::window_border_width);
480
481         label.set_alignment (0.5, 0.5);
482         dialog.get_vbox()->pack_start (label);
483         dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
484
485         dialog.set_name (X_("PluginIODialog"));
486         dialog.set_position (Gtk::WIN_POS_MOUSE);
487         dialog.set_modal (true);
488         dialog.show_all ();
489
490         dialog.run ();
491 }
492
493 void
494 RedirectBox::choose_insert ()
495 {
496         boost::shared_ptr<Redirect> redirect (new PortInsert (_session, _placement));
497         redirect->active_changed.connect (bind (mem_fun(*this, &RedirectBox::show_redirect_active_r), boost::weak_ptr<Redirect>(redirect)));
498         _route->add_redirect (redirect, this);
499 }
500
501 void
502 RedirectBox::choose_send ()
503 {
504         boost::shared_ptr<Send> send (new Send (_session, _placement));
505
506         /* XXX need redirect lock on route */
507
508         send->ensure_io (0, _route->max_redirect_outs(), false, this);
509         
510         IOSelectorWindow *ios = new IOSelectorWindow (_session, send, false, true);
511         
512         ios->show_all ();
513
514         boost::shared_ptr<Redirect> r = boost::static_pointer_cast<Redirect>(send);
515
516         ios->selector().Finished.connect (bind (mem_fun(*this, &RedirectBox::send_io_finished), boost::weak_ptr<Redirect>(r), ios));
517 }
518
519 void
520 RedirectBox::send_io_finished (IOSelector::Result r, boost::weak_ptr<Redirect> weak_redirect, IOSelectorWindow* ios)
521 {
522         boost::shared_ptr<Redirect> redirect (weak_redirect.lock());
523
524         if (!redirect) {
525                 return;
526         }
527
528         switch (r) {
529         case IOSelector::Cancelled:
530                 // redirect will go away when all shared_ptrs to it vanish
531                 break;
532
533         case IOSelector::Accepted:
534                 _route->add_redirect (redirect, this);
535                 break;
536         }
537
538         delete_when_idle (ios);
539 }
540
541 void
542 RedirectBox::redisplay_redirects (void *src)
543 {
544         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::redisplay_redirects), src));
545
546         if (no_redirect_redisplay) {
547                 return;
548         }
549         
550         ignore_delete = true;
551         model->clear ();
552         ignore_delete = false;
553
554         redirect_active_connections.clear ();
555         redirect_name_connections.clear ();
556
557         void (RedirectBox::*pmf)(boost::shared_ptr<Redirect>) = &RedirectBox::add_redirect_to_display;
558         _route->foreach_redirect (this, pmf);
559
560         switch (_placement) {
561         case PreFader:
562                 build_redirect_tooltip(redirect_eventbox, _("Pre-fader inserts, sends & plugins:"));
563                 break;
564         case PostFader:
565                 build_redirect_tooltip(redirect_eventbox, _("Post-fader inserts, sends & plugins:"));
566                 break;
567         }
568 }
569
570 void
571 RedirectBox::add_redirect_to_display (boost::shared_ptr<Redirect> redirect)
572 {
573         if (redirect->placement() != _placement) {
574                 return;
575         }
576         
577         Gtk::TreeModel::Row row = *(model->append());
578         row[columns.text] = redirect_name (redirect);
579         row[columns.redirect] = redirect;
580
581         show_redirect_active (redirect);
582
583         redirect_active_connections.push_back (redirect->active_changed.connect (bind (mem_fun(*this, &RedirectBox::show_redirect_active_r), boost::weak_ptr<Redirect>(redirect))));
584         redirect_name_connections.push_back (redirect->name_changed.connect (bind (mem_fun(*this, &RedirectBox::show_redirect_name), boost::weak_ptr<Redirect>(redirect))));
585 }
586
587 string
588 RedirectBox::redirect_name (boost::weak_ptr<Redirect> weak_redirect)
589 {
590         boost::shared_ptr<Redirect> redirect (weak_redirect.lock());
591
592         if (!redirect) {
593                 return string();
594         }
595
596         boost::shared_ptr<Send> send;
597         string name_display;
598
599         if (!redirect->active()) {
600                 name_display = " (";
601         }
602
603         if ((send = boost::dynamic_pointer_cast<Send> (redirect)) != 0) {
604
605                 name_display += '>';
606
607                 /* grab the send name out of its overall name */
608
609                 string::size_type lbracket, rbracket;
610                 lbracket = send->name().find ('[');
611                 rbracket = send->name().find (']');
612
613                 switch (_width) {
614                 case Wide:
615                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
616                         break;
617                 case Narrow:
618                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
619                         break;
620                 }
621
622         } else {
623
624                 switch (_width) {
625                 case Wide:
626                         name_display += redirect->name();
627                         break;
628                 case Narrow:
629                         name_display += PBD::short_version (redirect->name(), 5);
630                         break;
631                 }
632
633         }
634
635         if (!redirect->active()) {
636                 name_display += ')';
637         }
638
639         return name_display;
640 }
641
642 void
643 RedirectBox::build_redirect_tooltip (EventBox& box, string start)
644 {
645         string tip(start);
646
647         Gtk::TreeModel::Children children = model->children();
648         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
649                 Gtk::TreeModel::Row row = *iter;
650                 tip += '\n';
651                 tip += row[columns.text];
652         }
653         ARDOUR_UI::instance()->tooltips().set_tip (box, tip);
654 }
655
656 void
657 RedirectBox::show_redirect_name (void* src, boost::weak_ptr<Redirect> redirect)
658 {
659         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::show_redirect_name), src, redirect));
660         show_redirect_active (redirect);
661 }
662
663 void
664 RedirectBox::show_redirect_active_r (Redirect* r, void *src, boost::weak_ptr<Redirect> weak_redirect)
665 {
666         show_redirect_active (weak_redirect);
667 }
668
669 void
670 RedirectBox::show_redirect_active (boost::weak_ptr<Redirect> weak_redirect)
671 {
672         boost::shared_ptr<Redirect> redirect (weak_redirect.lock());
673         
674         if (!redirect) {
675                 return;
676         }
677
678         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RedirectBox::show_redirect_active), weak_redirect));
679         
680         Gtk::TreeModel::Children children = model->children();
681         Gtk::TreeModel::Children::iterator iter = children.begin();
682
683         while (iter != children.end()) {
684
685                 boost::shared_ptr<Redirect> r = (*iter)[columns.redirect];
686
687                 if (r == redirect) {
688                         (*iter)[columns.text] = redirect_name (r);
689                         
690                         if (redirect->active()) {
691                                 (*iter)[columns.color] = *active_redirect_color;
692                         } else {
693                                 (*iter)[columns.color] = *inactive_redirect_color;
694                         }
695                         break;
696                 }
697
698                 iter++;
699         }
700 }
701
702 void
703 RedirectBox::row_deleted (const Gtk::TreeModel::Path& path)
704 {
705         if (!ignore_delete) {
706                 compute_redirect_sort_keys ();
707         }
708 }
709
710 void
711 RedirectBox::compute_redirect_sort_keys ()
712 {
713         uint32_t sort_key = 0;
714         Gtk::TreeModel::Children children = model->children();
715
716         for (Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
717                 boost::shared_ptr<Redirect> r = (*iter)[columns.redirect];
718                 r->set_sort_key (sort_key);
719                 sort_key++;
720         }
721
722         if (_route->sort_redirects ()) {
723
724                 redisplay_redirects (0);
725
726                 /* now tell them about the problem */
727
728                 ArdourDialog dialog (_("ardour: weird plugin dialog"));
729                 Label label;
730
731                 label.set_text (_("\
732 You cannot reorder this set of redirects\n\
733 in that way because the inputs and\n\
734 outputs do not work correctly."));
735
736                 dialog.get_vbox()->pack_start (label);
737                 dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
738
739                 dialog.set_name (X_("PluginIODialog"));
740                 dialog.set_position (Gtk::WIN_POS_MOUSE);
741                 dialog.set_modal (true);
742                 dialog.show_all ();
743
744                 dialog.run ();
745         }
746 }
747
748 void
749 RedirectBox::rename_redirects ()
750 {
751         vector<boost::shared_ptr<Redirect> > to_be_renamed;
752         
753         get_selected_redirects (to_be_renamed);
754
755         if (to_be_renamed.empty()) {
756                 return;
757         }
758
759         for (vector<boost::shared_ptr<Redirect> >::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
760                 rename_redirect (*i);
761         }
762 }
763
764 void
765 RedirectBox::cut_redirects ()
766 {
767         vector<boost::shared_ptr<Redirect> > to_be_removed;
768         
769         get_selected_redirects (to_be_removed);
770
771         if (to_be_removed.empty()) {
772                 return;
773         }
774
775         /* this essentially transfers ownership of the redirect
776            of the redirect from the route to the mixer
777            selection.
778         */
779         
780         _rr_selection.set (to_be_removed);
781
782         no_redirect_redisplay = true;
783         for (vector<boost::shared_ptr<Redirect> >::iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
784                 // Do not cut inserts or sends
785                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0) {
786                         void* gui = (*i)->get_gui ();
787                 
788                         if (gui) {
789                                 static_cast<Gtk::Widget*>(gui)->hide ();
790                         }
791                 
792                         if (_route->remove_redirect (*i, this)) {
793                                 /* removal failed */
794                                 _rr_selection.remove (*i);
795                         }
796                 } else {
797                         _rr_selection.remove (*i);
798                 }
799
800         }
801         no_redirect_redisplay = false;
802         redisplay_redirects (this);
803 }
804
805 void
806 RedirectBox::copy_redirects ()
807 {
808         vector<boost::shared_ptr<Redirect> > to_be_copied;
809         vector<boost::shared_ptr<Redirect> > copies;
810
811         get_selected_redirects (to_be_copied);
812
813         if (to_be_copied.empty()) {
814                 return;
815         }
816
817         for (vector<boost::shared_ptr<Redirect> >::iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
818                 // Do not copy inserts or sends
819                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0) {
820                         copies.push_back (Redirect::clone (*i));
821                 }
822         }
823
824         _rr_selection.set (copies);
825
826 }
827
828 void
829 RedirectBox::delete_redirects ()
830 {
831         vector<boost::shared_ptr<Redirect> > to_be_deleted;
832         
833         get_selected_redirects (to_be_deleted);
834
835         if (to_be_deleted.empty()) {
836                 return;
837         }
838
839         for (vector<boost::shared_ptr<Redirect> >::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
840                 
841                 void* gui = (*i)->get_gui ();
842                 
843                 if (gui) {
844                         static_cast<Gtk::Widget*>(gui)->hide ();
845                 }
846
847                 _route->remove_redirect( *i, this);
848         }
849
850         no_redirect_redisplay = false;
851         redisplay_redirects (this);
852 }
853
854 gint
855 RedirectBox::idle_delete_redirect (boost::weak_ptr<Redirect> weak_redirect)
856 {
857         boost::shared_ptr<Redirect> redirect (weak_redirect.lock());
858
859         if (!redirect) {
860                 return false;
861         }
862
863         /* NOT copied to _mixer.selection() */
864
865         no_redirect_redisplay = true;
866         _route->remove_redirect (redirect, this);
867         no_redirect_redisplay = false;
868         redisplay_redirects (this);
869
870         return false;
871 }
872
873 void
874 RedirectBox::rename_redirect (boost::shared_ptr<Redirect> redirect)
875 {
876         ArdourPrompter name_prompter (true);
877         string result;
878         name_prompter.set_prompt (_("rename redirect"));
879         name_prompter.set_initial_text (redirect->name());
880         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
881         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
882         name_prompter.show_all ();
883
884         switch (name_prompter.run ()) {
885
886         case Gtk::RESPONSE_ACCEPT:
887         name_prompter.get_result (result);
888         if (result.length()) {
889                         redirect->set_name (result, this);
890                 }       
891                 break;
892         }
893
894         return;
895 }
896
897 void
898 RedirectBox::cut_redirect (boost::shared_ptr<Redirect> redirect)
899 {
900         /* this essentially transfers ownership of the redirect
901            of the redirect from the route to the mixer
902            selection.
903         */
904
905         _rr_selection.add (redirect);
906         
907         void* gui = redirect->get_gui ();
908
909         if (gui) {
910                 static_cast<Gtk::Widget*>(gui)->hide ();
911         }
912         
913         no_redirect_redisplay = true;
914         if (_route->remove_redirect (redirect, this)) {
915                 _rr_selection.remove (redirect);
916         }
917         no_redirect_redisplay = false;
918         redisplay_redirects (this);
919 }
920
921 void
922 RedirectBox::copy_redirect (boost::shared_ptr<Redirect> redirect)
923 {
924         boost::shared_ptr<Redirect> copy = Redirect::clone (redirect);
925         _rr_selection.add (copy);
926 }
927
928 void
929 RedirectBox::paste_redirects ()
930 {
931         if (_rr_selection.redirects.empty()) {
932                 return;
933         }
934
935         paste_redirect_list (_rr_selection.redirects);
936 }
937
938 void
939 RedirectBox::paste_redirect_list (list<boost::shared_ptr<Redirect> >& redirects)
940 {
941         list<boost::shared_ptr<Redirect> > copies;
942
943         for (list<boost::shared_ptr<Redirect> >::iterator i = redirects.begin(); i != redirects.end(); ++i) {
944
945                 boost::shared_ptr<Redirect> copy = Redirect::clone (*i);
946
947                 copy->set_placement (_placement, this);
948                 copies.push_back (copy);
949         }
950
951         if (_route->add_redirects (copies, this)) {
952
953                 string msg = _(
954                         "Copying the set of redirects on the clipboard failed,\n\
955 probably because the I/O configuration of the plugins\n\
956 could not match the configuration of this track.");
957                 MessageDialog am (msg);
958                 am.run ();
959         }
960 }
961
962 void
963 RedirectBox::activate_redirect (boost::shared_ptr<Redirect> r)
964 {
965         r->set_active (true, 0);
966 }
967
968 void
969 RedirectBox::deactivate_redirect (boost::shared_ptr<Redirect> r)
970 {
971         r->set_active (false, 0);
972 }
973
974 void
975 RedirectBox::get_selected_redirects (vector<boost::shared_ptr<Redirect> >& redirects)
976 {
977     vector<Gtk::TreeModel::Path> pathlist = redirect_display.get_selection()->get_selected_rows();
978  
979     for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
980             redirects.push_back ((*(model->get_iter(*iter)))[columns.redirect]);
981     }
982 }
983
984 void
985 RedirectBox::for_selected_redirects (void (RedirectBox::*pmf)(boost::shared_ptr<Redirect>))
986 {
987     vector<Gtk::TreeModel::Path> pathlist = redirect_display.get_selection()->get_selected_rows();
988
989         for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
990                 boost::shared_ptr<Redirect> redirect = (*(model->get_iter(*iter)))[columns.redirect];
991                 (this->*pmf)(redirect);
992         }
993 }
994
995 void
996 RedirectBox::clone_redirects ()
997 {
998         RouteSelection& routes (_rr_selection.routes);
999
1000         if (!routes.empty()) {
1001                 if (_route->copy_redirects (*routes.front(), _placement)) {
1002                         string msg = _(
1003 "Copying the set of redirects on the clipboard failed,\n\
1004 probably because the I/O configuration of the plugins\n\
1005 could not match the configuration of this track.");
1006                         MessageDialog am (msg);
1007                         am.run ();
1008                 }
1009         }
1010 }
1011
1012 void
1013 RedirectBox::all_redirects_active (bool state)
1014 {
1015         _route->all_redirects_active (_placement, state);
1016 }
1017
1018 void
1019 RedirectBox::clear_redirects ()
1020 {
1021         string prompt;
1022         vector<string> choices;
1023
1024         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1025                 if (_placement == PreFader) {
1026                         prompt = _("Do you really want to remove all pre-fader redirects from this track?\n"
1027                                    "(this cannot be undone)");
1028                 } else {
1029                         prompt = _("Do you really want to remove all post-fader redirects from this track?\n"
1030                                    "(this cannot be undone)");
1031                 }
1032         } else {
1033                 if (_placement == PreFader) {
1034                         prompt = _("Do you really want to remove all pre-fader redirects from this bus?\n"
1035                                    "(this cannot be undone)");
1036                 } else {
1037                         prompt = _("Do you really want to remove all post-fader redirects from this bus?\n"
1038                                    "(this cannot be undone)");
1039                 }
1040         }
1041
1042         choices.push_back (_("Cancel"));
1043         choices.push_back (_("Yes, remove them all"));
1044
1045         Gtkmm2ext::Choice prompter (prompt, choices);
1046
1047         if (prompter.run () == 1) {
1048                 _route->clear_redirects (_placement, this);
1049         }
1050 }
1051
1052 void
1053 RedirectBox::edit_redirect (boost::shared_ptr<Redirect> redirect)
1054 {
1055         boost::shared_ptr<Insert> insert;
1056
1057         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1058
1059                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1060                         return;
1061                 }
1062         }
1063         
1064         if ((insert = boost::dynamic_pointer_cast<Insert> (redirect)) == 0) {
1065                 
1066                 /* it's a send */
1067                 
1068                 if (!_session.engine().connected()) {
1069                         return;
1070                 }
1071
1072                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (redirect);
1073                 
1074                 SendUIWindow *send_ui;
1075                 
1076                 if (send->get_gui() == 0) {
1077                         
1078                         send_ui = new SendUIWindow (send, _session);
1079
1080                         WindowTitle title(Glib::get_application_name());
1081                         title += send->name();
1082                         send_ui->set_title (title.get_string());
1083
1084                         send->set_gui (send_ui);
1085                         
1086                 } else {
1087                         send_ui = reinterpret_cast<SendUIWindow *> (send->get_gui());
1088                 }
1089                 
1090                 if (send_ui->is_visible()) {
1091                         send_ui->get_window()->raise ();
1092                 } else {
1093                         send_ui->show_all ();
1094                         send_ui->present ();
1095                 }
1096                 
1097         } else {
1098                 
1099                 /* it's an insert */
1100
1101                 boost::shared_ptr<PluginInsert> plugin_insert;
1102                 boost::shared_ptr<PortInsert> port_insert;
1103                 
1104                 if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (insert)) != 0) {
1105                         
1106                         PluginUIWindow *plugin_ui;
1107
1108                         /* these are both allowed to be null */
1109                         
1110                         Container* toplevel = get_toplevel();
1111                         Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1112                         
1113                         if (plugin_insert->get_gui() == 0) {
1114
1115                                 plugin_ui = new PluginUIWindow (win, plugin_insert);
1116                                 
1117                                 WindowTitle title(Glib::get_application_name());
1118                                 title += generate_redirect_title (plugin_insert);
1119                                 plugin_ui->set_title (title.get_string());
1120                                 
1121                                 plugin_insert->set_gui (plugin_ui);
1122                                 
1123                                 // change window title when route name is changed
1124                                 _route->name_changed.connect (bind (mem_fun(*this, &RedirectBox::route_name_changed), plugin_ui, boost::weak_ptr<PluginInsert> (plugin_insert)));
1125                                 
1126                         } else {
1127                                 plugin_ui = reinterpret_cast<PluginUIWindow *> (plugin_insert->get_gui());
1128                                 plugin_ui->set_parent (win);
1129                         }
1130                         
1131                         if (plugin_ui->is_visible()) {
1132                                 plugin_ui->get_window()->raise ();
1133                         } else {
1134                                 plugin_ui->show_all ();
1135                                 plugin_ui->present ();
1136                         }
1137
1138                 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (insert)) != 0) {
1139                         
1140                         if (!_session.engine().connected()) {
1141                                 MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1142                                 msg.run ();
1143                                 return;
1144                         }
1145
1146                         PortInsertWindow *io_selector;
1147
1148                         if (port_insert->get_gui() == 0) {
1149                                 io_selector = new PortInsertWindow (_session, port_insert);
1150                                 port_insert->set_gui (io_selector);
1151                                 
1152                         } else {
1153                                 io_selector = reinterpret_cast<PortInsertWindow *> (port_insert->get_gui());
1154                         }
1155                         
1156                         if (io_selector->is_visible()) {
1157                                 io_selector->get_window()->raise ();
1158                         } else {
1159                                 io_selector->show_all ();
1160                                 io_selector->present ();
1161                         }
1162                 }
1163         }
1164 }
1165
1166 bool
1167 RedirectBox::enter_box (GdkEventCrossing *ev, RedirectBox* rb)
1168 {
1169         switch (ev->detail) {
1170         case GDK_NOTIFY_INFERIOR:
1171                 break;
1172
1173         case GDK_NOTIFY_VIRTUAL:
1174                 /* fallthru */
1175
1176         default:
1177                 _current_redirect_box = rb;
1178         }
1179
1180         return false;
1181 }
1182
1183 void
1184 RedirectBox::register_actions ()
1185 {
1186         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("redirectmenu"));
1187         Glib::RefPtr<Action> act;
1188
1189         /* new stuff */
1190         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),  sigc::ptr_fun (RedirectBox::rb_choose_plugin));
1191
1192         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),  sigc::ptr_fun (RedirectBox::rb_choose_insert));
1193         ActionManager::jack_sensitive_actions.push_back (act);
1194         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New Send ..."),  sigc::ptr_fun (RedirectBox::rb_choose_send));
1195         ActionManager::jack_sensitive_actions.push_back (act);
1196
1197         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear"),  sigc::ptr_fun (RedirectBox::rb_clear));
1198
1199         /* standard editing stuff */
1200         act = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),  sigc::ptr_fun (RedirectBox::rb_cut));
1201         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1202         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),  sigc::ptr_fun (RedirectBox::rb_copy));
1203         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1204
1205         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),  sigc::ptr_fun (RedirectBox::rb_delete));
1206         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1207
1208         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),  sigc::ptr_fun (RedirectBox::rb_paste));
1209         act = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),  sigc::ptr_fun (RedirectBox::rb_rename));
1210         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1211         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),  sigc::ptr_fun (RedirectBox::rb_select_all));
1212         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),  sigc::ptr_fun (RedirectBox::rb_deselect_all));
1213                 
1214         /* activation */
1215         act = ActionManager::register_action (popup_act_grp, X_("activate"), _("Activate"),  sigc::ptr_fun (RedirectBox::rb_activate));
1216         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1217         act = ActionManager::register_action (popup_act_grp, X_("deactivate"), _("Deactivate"),  sigc::ptr_fun (RedirectBox::rb_deactivate));
1218         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1219         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate all"),  sigc::ptr_fun (RedirectBox::rb_activate_all));
1220         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate all"),  sigc::ptr_fun (RedirectBox::rb_deactivate_all));
1221
1222         /* show editors */
1223         act = ActionManager::register_action (popup_act_grp, X_("edit"), _("Edit"),  sigc::ptr_fun (RedirectBox::rb_edit));
1224         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1225
1226         ActionManager::add_action_group (popup_act_grp);
1227
1228
1229 }
1230
1231 void
1232 RedirectBox::rb_choose_plugin ()
1233 {
1234         if (_current_redirect_box == 0) {
1235                 return;
1236         }
1237         _current_redirect_box->choose_plugin ();
1238 }
1239
1240 void
1241 RedirectBox::rb_choose_insert ()
1242 {
1243         if (_current_redirect_box == 0) {
1244                 return;
1245         }
1246         _current_redirect_box->choose_insert ();
1247 }
1248
1249 void
1250 RedirectBox::rb_choose_send ()
1251 {
1252         if (_current_redirect_box == 0) {
1253                 return;
1254         }
1255         _current_redirect_box->choose_send ();
1256 }
1257
1258 void
1259 RedirectBox::rb_clear ()
1260 {
1261         if (_current_redirect_box == 0) {
1262                 return;
1263         }
1264
1265         _current_redirect_box->clear_redirects ();
1266 }
1267
1268 void
1269 RedirectBox::rb_cut ()
1270 {
1271         if (_current_redirect_box == 0) {
1272                 return;
1273         }
1274
1275         _current_redirect_box->cut_redirects ();
1276 }
1277
1278 void
1279 RedirectBox::rb_delete ()
1280 {
1281         if (_current_redirect_box == 0) {
1282                 return;
1283         }
1284
1285         _current_redirect_box->delete_redirects ();
1286 }
1287
1288 void
1289 RedirectBox::rb_copy ()
1290 {
1291         if (_current_redirect_box == 0) {
1292                 return;
1293         }
1294         _current_redirect_box->copy_redirects ();
1295 }
1296
1297 void
1298 RedirectBox::rb_paste ()
1299 {
1300         if (_current_redirect_box == 0) {
1301                 return;
1302         }
1303
1304         _current_redirect_box->paste_redirects ();
1305 }
1306
1307 void
1308 RedirectBox::rb_rename ()
1309 {
1310         if (_current_redirect_box == 0) {
1311                 return;
1312         }
1313         _current_redirect_box->rename_redirects ();
1314 }
1315
1316 void
1317 RedirectBox::rb_select_all ()
1318 {
1319         if (_current_redirect_box == 0) {
1320                 return;
1321         }
1322
1323         _current_redirect_box->select_all_redirects ();
1324 }
1325
1326 void
1327 RedirectBox::rb_deselect_all ()
1328 {
1329         if (_current_redirect_box == 0) {
1330                 return;
1331         }
1332
1333         _current_redirect_box->deselect_all_redirects ();
1334 }
1335
1336 void
1337 RedirectBox::rb_activate ()
1338 {
1339         if (_current_redirect_box == 0) {
1340                 return;
1341         }
1342
1343         _current_redirect_box->for_selected_redirects (&RedirectBox::activate_redirect);
1344 }
1345
1346 void
1347 RedirectBox::rb_deactivate ()
1348 {
1349         if (_current_redirect_box == 0) {
1350                 return;
1351         }
1352         _current_redirect_box->for_selected_redirects (&RedirectBox::deactivate_redirect);
1353 }
1354
1355 void
1356 RedirectBox::rb_activate_all ()
1357 {
1358         if (_current_redirect_box == 0) {
1359                 return;
1360         }
1361
1362         _current_redirect_box->all_redirects_active (true);
1363 }
1364
1365 void
1366 RedirectBox::rb_deactivate_all ()
1367 {
1368         if (_current_redirect_box == 0) {
1369                 return;
1370         }
1371         _current_redirect_box->all_redirects_active (false);
1372 }
1373
1374 void
1375 RedirectBox::rb_edit ()
1376 {
1377         if (_current_redirect_box == 0) {
1378                 return;
1379         }
1380
1381         _current_redirect_box->for_selected_redirects (&RedirectBox::edit_redirect);
1382 }
1383
1384 void
1385 RedirectBox::route_name_changed (void* src, PluginUIWindow* plugin_ui, boost::weak_ptr<PluginInsert> wpi)
1386 {
1387         ENSURE_GUI_THREAD(bind (mem_fun (*this, &RedirectBox::route_name_changed), src, plugin_ui, wpi));
1388         boost::shared_ptr<PluginInsert> pi (wpi.lock());
1389         
1390
1391         if (pi) {
1392                 WindowTitle title(Glib::get_application_name());
1393                 title += generate_redirect_title (pi);
1394                 plugin_ui->set_title (title.get_string());
1395         }
1396 }
1397
1398 string 
1399 RedirectBox::generate_redirect_title (boost::shared_ptr<PluginInsert> pi)
1400 {
1401         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
1402         string::size_type email_pos;
1403
1404         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1405                 maker = maker.substr (0, email_pos - 1);
1406         }
1407
1408         if (maker.length() > 32) {
1409                 maker = maker.substr (0, 32);
1410                 maker += " ...";
1411         }
1412
1413         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);  
1414 }
1415
1416 void
1417 RedirectBox::plugin_selector_hidden ()
1418 {
1419         newplug_connection.disconnect();
1420         using_plugin_selector = false;
1421 }
1422
1423 void
1424 RedirectBox::plugin_selector_shown ()
1425 {
1426         using_plugin_selector = true;
1427 }