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