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