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