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