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