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