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