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