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