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