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