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