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