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