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