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