* fixed missing track color mode persistence
[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, PluginSelector &plugsel, 
86                             RouteRedirectSelection & rsel, bool owner_is_mixer)
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 ("ProcessorSelector", 0xff, 0, 0, 0, "fg", Gtk::STATE_ACTIVE, false ));
97                 set_color (*inactive_processor_color, rgba_from_style ("ProcessorSelector", 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
108         model = ListStore::create(columns);
109
110         RefPtr<TreeSelection> selection = processor_display.get_selection();
111         selection->set_mode (Gtk::SELECTION_MULTIPLE);
112         selection->signal_changed().connect (mem_fun (*this, &ProcessorBox::selection_changed));
113
114         processor_display.set_model (model);
115         processor_display.append_column (X_("notshown"), columns.text);
116         processor_display.set_name ("ProcessorSelector");
117         processor_display.set_headers_visible (false);
118         processor_display.set_reorderable (true);
119         processor_display.set_size_request (-1, 40);
120         processor_display.get_column(0)->set_sizing(TREE_VIEW_COLUMN_FIXED);
121         processor_display.get_column(0)->set_fixed_width(48);
122         processor_display.add_object_drag (columns.processor.index(), "processors");
123         processor_display.signal_drop.connect (mem_fun (*this, &ProcessorBox::object_drop));
124
125         TreeViewColumn* name_col = processor_display.get_column(0);
126         CellRendererText* renderer = dynamic_cast<CellRendererText*>(processor_display.get_column_cell_renderer (0));
127         name_col->add_attribute(renderer->property_foreground_gdk(), columns.color);
128
129         processor_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
130         
131         model->signal_row_deleted().connect (mem_fun (*this, &ProcessorBox::row_deleted));
132
133         processor_scroller.add (processor_display);
134         processor_eventbox.add (processor_scroller);
135         
136         processor_scroller.set_size_request (-1, 40);
137
138         pack_start (processor_eventbox, true, true);
139
140         processor_eventbox.signal_enter_notify_event().connect (bind (sigc::ptr_fun (ProcessorBox::enter_box), this));
141
142         processor_display.signal_button_press_event().connect (mem_fun(*this, &ProcessorBox::processor_button_press_event), false);
143         processor_display.signal_button_release_event().connect (mem_fun(*this, &ProcessorBox::processor_button_release_event));
144 }
145
146 ProcessorBox::~ProcessorBox ()
147 {
148 }
149
150 void
151 ProcessorBox::set_route (boost::shared_ptr<Route> r)
152 {
153         connections.clear ();
154
155         _route = r;
156
157         connections.push_back (_route->processors_changed.connect (mem_fun(*this, &ProcessorBox::redisplay_processors)));
158         connections.push_back (_route->GoingAway.connect (mem_fun (*this, &ProcessorBox::route_going_away)));
159
160         redisplay_processors ();
161 }
162
163 void
164 ProcessorBox::route_going_away ()
165 {
166         /* don't keep updating display as processors are deleted */
167         no_processor_redisplay = true;
168 }
169
170
171 void
172 ProcessorBox::object_drop (const list<boost::shared_ptr<Processor> >& procs)
173 {
174         paste_processor_list (procs);
175 }
176
177 void
178 ProcessorBox::update()
179 {
180         redisplay_processors ();
181 }
182
183
184 void
185 ProcessorBox::set_width (Width w)
186 {
187         if (_width == w) {
188                 return;
189         }
190         _width = w;
191
192         redisplay_processors ();
193 }
194
195 void
196 ProcessorBox::remove_processor_gui (boost::shared_ptr<Processor> processor)
197 {
198         boost::shared_ptr<Send> send;
199         boost::shared_ptr<PortInsert> port_insert;
200
201         if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
202
203                 PortInsertUI *io_selector = reinterpret_cast<PortInsertUI *> (port_insert->get_gui());
204                 port_insert->set_gui (0);
205                 delete io_selector;
206
207         } else if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
208                 SendUIWindow *sui = reinterpret_cast<SendUIWindow*> (send->get_gui());
209                 send->set_gui (0);
210                 delete sui;
211         }
212 }
213
214 void 
215 ProcessorBox::build_send_action_menu ()
216
217 {
218         using namespace Menu_Helpers;
219
220         send_action_menu = new Menu;
221         send_action_menu->set_name ("ArdourContextMenu");
222         MenuList& items = send_action_menu->items();
223
224         items.push_back (MenuElem (_("New send"), mem_fun(*this, &ProcessorBox::new_send)));
225         items.push_back (MenuElem (_("Show send controls"), mem_fun(*this, &ProcessorBox::show_send_controls)));
226 }
227
228 void
229 ProcessorBox::show_send_controls ()
230
231 {
232 }
233
234 void
235 ProcessorBox::new_send ()
236
237 {
238 }
239
240 void
241 ProcessorBox::show_processor_menu (gint arg)
242 {
243         if (processor_menu == 0) {
244                 processor_menu = build_processor_menu ();
245         }
246
247         Gtk::MenuItem* plugin_menu_item = dynamic_cast<Gtk::MenuItem*>(ActionManager::get_widget("/processormenu/newplugin"));
248
249         if (plugin_menu_item) {
250                 plugin_menu_item->set_submenu (_plugin_selector.plugin_menu());
251         }
252
253         paste_action->set_sensitive (!_rr_selection.processors.empty());
254
255         processor_menu->popup (1, arg);
256 }
257
258 void
259 ProcessorBox::processor_drag_begin (GdkDragContext *context)
260 {
261         processor_drag_in_progress = true;
262 }
263
264 void
265 ProcessorBox::processor_drag_end (GdkDragContext *context)
266 {
267         processor_drag_in_progress = false;
268 }
269
270 bool
271 ProcessorBox::processor_button_press_event (GdkEventButton *ev)
272 {
273         TreeIter iter;
274         TreeModel::Path path;
275         TreeViewColumn* column;
276         int cellx;
277         int celly;
278         boost::shared_ptr<Processor> processor;
279         int ret = false;
280         bool selected = false;
281
282         if (processor_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
283                 if ((iter = model->get_iter (path))) {
284                         processor = (*iter)[columns.processor];
285                         selected = processor_display.get_selection()->is_selected (iter);
286                 }
287                 
288         }
289
290         if (processor && (Keyboard::is_edit_event (ev) || (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS))) {
291                 
292                 if (_session.engine().connected()) {
293                         /* XXX giving an error message here is hard, because we may be in the midst of a button press */
294                         edit_processor (processor);
295                 }
296                 ret = true;
297                 
298         } else if (processor && ev->button == 1 && selected) {
299
300                 // this is purely informational but necessary
301                 ProcessorSelected (processor); // emit
302
303         } else if (!processor && ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
304
305                 choose_plugin ();
306                 _plugin_selector.show_manager ();
307         }
308
309         
310         return ret;
311 }
312
313 bool
314 ProcessorBox::processor_button_release_event (GdkEventButton *ev)
315 {
316         TreeIter iter;
317         TreeModel::Path path;
318         TreeViewColumn* column;
319         int cellx;
320         int celly;
321         boost::shared_ptr<Processor> processor;
322         int ret = false;
323
324
325         if (processor_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
326                 if ((iter = model->get_iter (path))) {
327                         processor = (*iter)[columns.processor];
328                 }
329         }
330
331         if (processor && Keyboard::is_delete_event (ev)) {
332                 
333                 Glib::signal_idle().connect (bind (mem_fun(*this, &ProcessorBox::idle_delete_processor), boost::weak_ptr<Processor>(processor)));
334                 ret = true;
335                 
336         } else if (Keyboard::is_context_menu_event (ev)) {
337
338                 show_processor_menu(ev->time);
339                 ret = true;
340
341         } else if (processor && Keyboard::is_button2_event (ev) && (Keyboard::no_modifier_keys_pressed (ev) && ((ev->state & Gdk::BUTTON2_MASK) == Gdk::BUTTON2_MASK))) {
342                 
343                 /* button2-click with no modifiers */
344
345                 processor->set_active (!processor->active());
346                 ret = true;
347
348         } 
349
350         return ret;
351 }
352
353 Menu *
354 ProcessorBox::build_processor_menu ()
355 {
356         processor_menu = dynamic_cast<Gtk::Menu*>(ActionManager::get_widget("/processormenu") );
357         processor_menu->set_name ("ArdourContextMenu");
358
359         show_all_children();
360
361         return processor_menu;
362 }
363
364 void
365 ProcessorBox::selection_changed ()
366 {
367         bool sensitive = (processor_display.get_selection()->count_selected_rows()) ? true : false;
368         ActionManager::set_sensitive (ActionManager::plugin_selection_sensitive_actions, sensitive);
369 }
370
371 void
372 ProcessorBox::select_all_processors ()
373 {
374         processor_display.get_selection()->select_all();
375 }
376
377 void
378 ProcessorBox::deselect_all_processors ()
379 {
380         processor_display.get_selection()->unselect_all();
381 }
382
383 void
384 ProcessorBox::choose_plugin ()
385 {
386         _plugin_selector.set_interested_object (*this);
387 }
388
389 void
390 ProcessorBox::use_plugins (const SelectedPlugins& plugins)
391 {
392         for (SelectedPlugins::const_iterator p = plugins.begin(); p != plugins.end(); ++p) {
393
394                 boost::shared_ptr<Processor> processor (new PluginInsert (_session, *p, _placement));
395
396                 Route::ProcessorStreams err_streams;
397
398                 if (Config->get_new_plugins_active()) {
399                         processor->set_active (true);
400                 }
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                 if (Profile->get_sae()) {
541                         processor->set_active (true);
542                 }
543                 break;
544         }
545
546         delete_when_idle (ios);
547 }
548
549 void
550 ProcessorBox::redisplay_processors ()
551 {
552         ENSURE_GUI_THREAD (mem_fun(*this, &ProcessorBox::redisplay_processors));
553
554         if (no_processor_redisplay) {
555                 return;
556         }
557         
558         ignore_delete = true;
559         model->clear ();
560         ignore_delete = false;
561
562         processor_active_connections.clear ();
563         processor_name_connections.clear ();
564
565         _route->foreach_processor (mem_fun (*this, &ProcessorBox::add_processor_to_display));
566
567         switch (_placement) {
568         case PreFader:
569                 build_processor_tooltip(processor_eventbox, _("Pre-fader inserts, sends & plugins:"));
570                 break;
571         case PostFader:
572                 build_processor_tooltip(processor_eventbox, _("Post-fader inserts, sends & plugins:"));
573                 break;
574         }
575 }
576
577 void
578 ProcessorBox::add_processor_to_display (boost::weak_ptr<Processor> p)
579 {
580         boost::shared_ptr<Processor> processor (p.lock ());
581         if (!processor) {
582                 return;
583         }
584         
585         if (processor->placement() != _placement) {
586                 return;
587         }
588         
589         Gtk::TreeModel::Row row = *(model->append());
590         row[columns.text] = processor_name (processor);
591         row[columns.processor] = processor;
592
593         show_processor_active (processor);
594
595         processor_active_connections.push_back (processor->ActiveChanged.connect (bind (mem_fun(*this, &ProcessorBox::show_processor_active), boost::weak_ptr<Processor>(processor))));
596         processor_name_connections.push_back (processor->NameChanged.connect (bind (mem_fun(*this, &ProcessorBox::show_processor_name), boost::weak_ptr<Processor>(processor))));
597 }
598
599 string
600 ProcessorBox::processor_name (boost::weak_ptr<Processor> weak_processor)
601 {
602         boost::shared_ptr<Processor> processor (weak_processor.lock());
603
604         if (!processor) {
605                 return string();
606         }
607
608         boost::shared_ptr<Send> send;
609         string name_display;
610
611         if (!processor->active()) {
612                 name_display = " (";
613         }
614
615         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
616
617                 name_display += '>';
618
619                 /* grab the send name out of its overall name */
620
621                 string::size_type lbracket, rbracket;
622                 lbracket = send->name().find ('[');
623                 rbracket = send->name().find (']');
624
625                 switch (_width) {
626                 case Wide:
627                         name_display += send->name().substr (lbracket+1, lbracket-rbracket-1);
628                         break;
629                 case Narrow:
630                         name_display += PBD::short_version (send->name().substr (lbracket+1, lbracket-rbracket-1), 4);
631                         break;
632                 }
633
634         } else {
635
636                 switch (_width) {
637                 case Wide:
638                         name_display += processor->name();
639                         break;
640                 case Narrow:
641                         name_display += PBD::short_version (processor->name(), 5);
642                         break;
643                 }
644
645         }
646
647         if (!processor->active()) {
648                 name_display += ')';
649         }
650
651         return name_display;
652 }
653
654 void
655 ProcessorBox::build_processor_tooltip (EventBox& box, string start)
656 {
657         string tip(start);
658
659         Gtk::TreeModel::Children children = model->children();
660         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
661                 Gtk::TreeModel::Row row = *iter;
662                 tip += '\n';
663                 tip += row[columns.text];
664         }
665         ARDOUR_UI::instance()->tooltips().set_tip (box, tip);
666 }
667
668 void
669 ProcessorBox::show_processor_name (boost::weak_ptr<Processor> processor)
670 {
671         ENSURE_GUI_THREAD(bind (mem_fun(*this, &ProcessorBox::show_processor_name), processor));
672         show_processor_active (processor);
673 }
674
675 void
676 ProcessorBox::show_processor_active (boost::weak_ptr<Processor> weak_processor)
677 {
678         boost::shared_ptr<Processor> processor (weak_processor.lock());
679         
680         if (!processor) {
681                 return;
682         }
683
684         ENSURE_GUI_THREAD(bind (mem_fun(*this, &ProcessorBox::show_processor_active), weak_processor));
685         
686         Gtk::TreeModel::Children children = model->children();
687         Gtk::TreeModel::Children::iterator iter = children.begin();
688
689         while (iter != children.end()) {
690
691                 boost::shared_ptr<Processor> r = (*iter)[columns.processor];
692
693                 if (r == processor) {
694                         (*iter)[columns.text] = processor_name (r);
695                         
696                         if (processor->active()) {
697                                 (*iter)[columns.color] = *active_processor_color;
698                         } else {
699                                 (*iter)[columns.color] = *inactive_processor_color;
700                         }
701                         break;
702                 }
703
704                 iter++;
705         }
706 }
707
708 void
709 ProcessorBox::row_deleted (const Gtk::TreeModel::Path& path)
710 {
711         if (!ignore_delete) {
712                 compute_processor_sort_keys ();
713         }
714 }
715
716 void
717 ProcessorBox::compute_processor_sort_keys ()
718 {
719         uint32_t sort_key = 0;
720         Gtk::TreeModel::Children children = model->children();
721
722         for (Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
723                 boost::shared_ptr<Processor> r = (*iter)[columns.processor];
724                 r->set_sort_key (sort_key);
725                 sort_key++;
726         }
727
728         if (_route->sort_processors ()) {
729
730                 redisplay_processors ();
731
732                 /* now tell them about the problem */
733
734                 ArdourDialog dialog (_("ardour: weird plugin dialog"));
735                 Label label;
736
737                 label.set_text (_("\
738 You cannot reorder this set of processors\n\
739 in that way because the inputs and\n\
740 outputs do not work correctly."));
741
742                 dialog.get_vbox()->pack_start (label);
743                 dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
744
745                 dialog.set_name (X_("PluginIODialog"));
746                 dialog.set_position (Gtk::WIN_POS_MOUSE);
747                 dialog.set_modal (true);
748                 dialog.show_all ();
749
750                 dialog.run ();
751         }
752 }
753
754 void
755 ProcessorBox::rename_processors ()
756 {
757         vector<boost::shared_ptr<Processor> > to_be_renamed;
758         
759         get_selected_processors (to_be_renamed);
760
761         if (to_be_renamed.empty()) {
762                 return;
763         }
764
765         for (vector<boost::shared_ptr<Processor> >::iterator i = to_be_renamed.begin(); i != to_be_renamed.end(); ++i) {
766                 rename_processor (*i);
767         }
768 }
769
770 void
771 ProcessorBox::cut_processors ()
772 {
773         vector<boost::shared_ptr<Processor> > to_be_removed;
774         XMLNode* node = new XMLNode (X_("cut"));
775
776         get_selected_processors (to_be_removed);
777
778         if (to_be_removed.empty()) {
779                 return;
780         }
781
782         no_processor_redisplay = true;
783         for (vector<boost::shared_ptr<Processor> >::iterator i = to_be_removed.begin(); i != to_be_removed.end(); ++i) {
784                 // Do not cut inserts
785                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
786                     (boost::dynamic_pointer_cast<Send>((*i)) != 0)) {
787
788                         void* gui = (*i)->get_gui ();
789                 
790                         if (gui) {
791                                 static_cast<Gtk::Widget*>(gui)->hide ();
792                         }
793                         
794                         XMLNode& child ((*i)->get_state());
795
796                         if (_route->remove_processor (*i) == 0) {
797                                 /* success */
798                                 node->add_child_nocopy (child);
799                         } else {
800                                 delete &child;
801                         }
802                 } 
803         }
804
805         _rr_selection.set (node);
806
807         no_processor_redisplay = false;
808         redisplay_processors ();
809 }
810
811 void
812 ProcessorBox::copy_processors ()
813 {
814         vector<boost::shared_ptr<Processor> > to_be_copied;
815         XMLNode* node = new XMLNode (X_("copy"));
816
817         get_selected_processors (to_be_copied);
818
819         if (to_be_copied.empty()) {
820                 return;
821         }
822
823         for (vector<boost::shared_ptr<Processor> >::iterator i = to_be_copied.begin(); i != to_be_copied.end(); ++i) {
824                 // Do not copy inserts
825                 if (boost::dynamic_pointer_cast<PluginInsert>((*i)) != 0 ||
826                     (boost::dynamic_pointer_cast<Send>((*i)) != 0)) {
827                         node->add_child_nocopy ((*i)->get_state());
828                 }
829         }
830
831         _rr_selection.set (node);
832 }
833
834 void
835 ProcessorBox::delete_processors ()
836 {
837         vector<boost::shared_ptr<Processor> > to_be_deleted;
838         
839         get_selected_processors (to_be_deleted);
840
841         if (to_be_deleted.empty()) {
842                 return;
843         }
844
845         for (vector<boost::shared_ptr<Processor> >::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
846                 
847                 void* gui = (*i)->get_gui ();
848                 
849                 if (gui) {
850                         static_cast<Gtk::Widget*>(gui)->hide ();
851                 }
852
853                 _route->remove_processor(*i);
854         }
855
856         no_processor_redisplay = false;
857         redisplay_processors ();
858 }
859
860 gint
861 ProcessorBox::idle_delete_processor (boost::weak_ptr<Processor> weak_processor)
862 {
863         boost::shared_ptr<Processor> processor (weak_processor.lock());
864
865         if (!processor) {
866                 return false;
867         }
868
869         /* NOT copied to _mixer.selection() */
870
871         no_processor_redisplay = true;
872         _route->remove_processor (processor);
873         no_processor_redisplay = false;
874         redisplay_processors ();
875
876         return false;
877 }
878
879 void
880 ProcessorBox::rename_processor (boost::shared_ptr<Processor> processor)
881 {
882         ArdourPrompter name_prompter (true);
883         string result;
884         name_prompter.set_prompt (_("rename processor"));
885         name_prompter.set_initial_text (processor->name());
886         name_prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
887         name_prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
888         name_prompter.show_all ();
889
890         switch (name_prompter.run ()) {
891
892         case Gtk::RESPONSE_ACCEPT:
893         name_prompter.get_result (result);
894         if (result.length()) {
895                         processor->set_name (result);
896                 }       
897                 break;
898         }
899
900         return;
901 }
902
903 void
904 ProcessorBox::paste_processors ()
905 {
906         if (_rr_selection.processors.empty()) {
907                 return;
908         }
909
910         cerr << "paste from node called " << _rr_selection.processors.get_node().name() << endl;
911
912         paste_processor_state (_rr_selection.processors.get_node());
913 }
914
915 void
916 ProcessorBox::paste_processor_list (const list<boost::shared_ptr<Processor> >& processors)
917 {
918         list<boost::shared_ptr<Processor> > copies;
919         
920         for (list<boost::shared_ptr<Processor> >::const_iterator i = processors.begin(); i != processors.end(); ++i) {
921                 
922                 boost::shared_ptr<Processor> copy = Processor::clone (*i);
923                 
924                 copy->set_placement (_placement);
925                 copies.push_back (copy);
926         }
927
928         if (_route->add_processors (copies)) {
929
930                 string msg = _(
931                         "Copying the set of processors on the clipboard failed,\n\
932 probably because the I/O configuration of the plugins\n\
933 could not match the configuration of this track.");
934                 MessageDialog am (msg);
935                 am.run ();
936         }
937 }
938
939 void
940 ProcessorBox::paste_processor_state (const XMLNode& node)
941 {
942         XMLNodeList nlist;
943         XMLNodeConstIterator niter;
944         list<boost::shared_ptr<Processor> > copies;
945
946         nlist = node.children();
947
948         cerr << "Pasting processor selection containing " << nlist.size() << endl;
949
950         if (nlist.empty()) {
951                 return;
952         }
953
954         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
955                 cerr << "try using " << (*niter)->name() << endl;
956                 try {
957                         copies.push_back (boost::shared_ptr<Processor> (new PluginInsert (_session, **niter)));
958                 }
959                 catch (...) {
960                         cerr << "plugin insert constructor failed\n";
961                 }
962         }
963
964         if (_route->add_processors (copies)) {
965
966                 string msg = _(
967                         "Copying the set of processors on the clipboard failed,\n\
968 probably because the I/O configuration of the plugins\n\
969 could not match the configuration of this track.");
970                 MessageDialog am (msg);
971                 am.run ();
972         }
973 }
974
975 void
976 ProcessorBox::activate_processor (boost::shared_ptr<Processor> r)
977 {
978         r->set_active (true);
979 }
980
981 void
982 ProcessorBox::deactivate_processor (boost::shared_ptr<Processor> r)
983 {
984         r->set_active (false);
985 }
986
987 void
988 ProcessorBox::get_selected_processors (vector<boost::shared_ptr<Processor> >& processors)
989 {
990     vector<Gtk::TreeModel::Path> pathlist = processor_display.get_selection()->get_selected_rows();
991  
992     for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
993             processors.push_back ((*(model->get_iter(*iter)))[columns.processor]);
994     }
995 }
996
997 void
998 ProcessorBox::for_selected_processors (void (ProcessorBox::*method)(boost::shared_ptr<Processor>))
999 {
1000     vector<Gtk::TreeModel::Path> pathlist = processor_display.get_selection()->get_selected_rows();
1001
1002         for (vector<Gtk::TreeModel::Path>::iterator iter = pathlist.begin(); iter != pathlist.end(); ++iter) {
1003                 boost::shared_ptr<Processor> processor = (*(model->get_iter(*iter)))[columns.processor];
1004                 (this->*method)(processor);
1005         }
1006 }
1007
1008 void
1009 ProcessorBox::clone_processors ()
1010 {
1011         RouteSelection& routes (_rr_selection.routes);
1012
1013         if (!routes.empty()) {
1014                 if (_route->copy_processors (*routes.front(), _placement)) {
1015                         string msg = _(
1016 "Copying the set of processors on the clipboard failed,\n\
1017 probably because the I/O configuration of the plugins\n\
1018 could not match the configuration of this track.");
1019                         MessageDialog am (msg);
1020                         am.run ();
1021                 }
1022         }
1023 }
1024
1025 void
1026 ProcessorBox::all_processors_active (bool state)
1027 {
1028         _route->all_processors_active (_placement, state);
1029 }
1030
1031 void
1032 ProcessorBox::ab_plugins ()
1033 {
1034         _route->ab_plugins (ab_direction);
1035         ab_direction = !ab_direction;
1036 }
1037
1038
1039 void
1040 ProcessorBox::clear_processors ()
1041 {
1042         string prompt;
1043         vector<string> choices;
1044
1045         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1046                 if (_placement == PreFader) {
1047                         prompt = _("Do you really want to remove all pre-fader processors from this track?\n"
1048                                    "(this cannot be undone)");
1049                 } else {
1050                         prompt = _("Do you really want to remove all post-fader processors from this track?\n"
1051                                    "(this cannot be undone)");
1052                 }
1053         } else {
1054                 if (_placement == PreFader) {
1055                         prompt = _("Do you really want to remove all pre-fader processors from this bus?\n"
1056                                    "(this cannot be undone)");
1057                 } else {
1058                         prompt = _("Do you really want to remove all post-fader processors from this bus?\n"
1059                                    "(this cannot be undone)");
1060                 }
1061         }
1062
1063         choices.push_back (_("Cancel"));
1064         choices.push_back (_("Yes, remove them all"));
1065
1066         Gtkmm2ext::Choice prompter (prompt, choices);
1067
1068         if (prompter.run () == 1) {
1069                 _route->clear_processors (_placement);
1070         }
1071 }
1072
1073 void
1074 ProcessorBox::edit_processor (boost::shared_ptr<Processor> processor)
1075 {
1076         boost::shared_ptr<Send> send;
1077         boost::shared_ptr<PluginInsert> plugin_insert;
1078         boost::shared_ptr<PortInsert> port_insert;
1079         Window* gidget = 0;
1080
1081         if (boost::dynamic_pointer_cast<AudioTrack>(_route) != 0) {
1082
1083                 if (boost::dynamic_pointer_cast<AudioTrack> (_route)->freeze_state() == AudioTrack::Frozen) {
1084                         return;
1085                 }
1086         }
1087         
1088         if ((send = boost::dynamic_pointer_cast<Send> (processor)) != 0) {
1089                 
1090                 if (!_session.engine().connected()) {
1091                         return;
1092                 }
1093
1094                 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
1095                 
1096                 SendUIWindow *send_ui;
1097                 
1098                 if (send->get_gui() == 0) {
1099                         
1100                         send_ui = new SendUIWindow (send, _session);
1101
1102                         WindowTitle title(Glib::get_application_name());
1103                         title += send->name();
1104                         send_ui->set_title (title.get_string());
1105
1106                         send->set_gui (send_ui);
1107                         
1108                 } else {
1109                         send_ui = reinterpret_cast<SendUIWindow *> (send->get_gui());
1110                 }
1111                 
1112                 gidget = send_ui;
1113                 
1114         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (processor)) != 0) {
1115                 
1116                 PluginUIWindow *plugin_ui;
1117                 
1118                 /* these are both allowed to be null */
1119                 
1120                 Container* toplevel = get_toplevel();
1121                 Window* win = dynamic_cast<Gtk::Window*>(toplevel);
1122                 
1123                 if (plugin_insert->get_gui() == 0) {
1124                         
1125                         plugin_ui = new PluginUIWindow (win, plugin_insert);
1126                         
1127                         WindowTitle title(Glib::get_application_name());
1128                         title += generate_processor_title (plugin_insert);
1129                         plugin_ui->set_title (title.get_string());
1130                         
1131                         plugin_insert->set_gui (plugin_ui);
1132                         
1133                         // change window title when route name is changed
1134                         _route->NameChanged.connect (bind (mem_fun(*this, &ProcessorBox::route_name_changed), plugin_ui, boost::weak_ptr<PluginInsert> (plugin_insert)));
1135                         
1136                 } else {
1137                         plugin_ui = reinterpret_cast<PluginUIWindow *> (plugin_insert->get_gui());
1138                         plugin_ui->set_parent (win);
1139                 }
1140                 
1141                 gidget = plugin_ui;
1142                 
1143         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (processor)) != 0) {
1144                 
1145                 if (!_session.engine().connected()) {
1146                         MessageDialog msg ( _("Not connected to JACK - no I/O changes are possible"));
1147                         msg.run ();
1148                         return;
1149                 }
1150                 
1151                 PortInsertWindow *io_selector;
1152                 
1153                 if (port_insert->get_gui() == 0) {
1154                         io_selector = new PortInsertWindow (_session, port_insert);
1155                         port_insert->set_gui (io_selector);
1156                         
1157                 } else {
1158                         io_selector = reinterpret_cast<PortInsertWindow *> (port_insert->get_gui());
1159                 }
1160                 
1161                 gidget = io_selector;
1162         }
1163
1164         if (gidget) {
1165                 if (gidget->is_visible()) {
1166                         gidget->get_window()->raise ();
1167                 } else {
1168                         gidget->show_all ();
1169                         gidget->present ();
1170                 }
1171         }
1172 }
1173
1174 bool
1175 ProcessorBox::enter_box (GdkEventCrossing *ev, ProcessorBox* rb)
1176 {
1177         switch (ev->detail) {
1178         case GDK_NOTIFY_INFERIOR:
1179                 break;
1180
1181         case GDK_NOTIFY_VIRTUAL:
1182                 /* fallthru */
1183
1184         default:
1185                 _current_processor_box = rb;
1186         }
1187
1188         return false;
1189 }
1190
1191 void
1192 ProcessorBox::register_actions ()
1193 {
1194         Glib::RefPtr<Gtk::ActionGroup> popup_act_grp = Gtk::ActionGroup::create(X_("processormenu"));
1195         Glib::RefPtr<Action> act;
1196
1197         /* new stuff */
1198         ActionManager::register_action (popup_act_grp, X_("newplugin"), _("New Plugin"),  sigc::ptr_fun (ProcessorBox::rb_choose_plugin));
1199
1200         act = ActionManager::register_action (popup_act_grp, X_("newinsert"), _("New Insert"),  sigc::ptr_fun (ProcessorBox::rb_choose_insert));
1201         ActionManager::jack_sensitive_actions.push_back (act);
1202         act = ActionManager::register_action (popup_act_grp, X_("newsend"), _("New Send ..."),  sigc::ptr_fun (ProcessorBox::rb_choose_send));
1203         ActionManager::jack_sensitive_actions.push_back (act);
1204
1205         ActionManager::register_action (popup_act_grp, X_("clear"), _("Clear"),  sigc::ptr_fun (ProcessorBox::rb_clear));
1206
1207         /* standard editing stuff */
1208         act = ActionManager::register_action (popup_act_grp, X_("cut"), _("Cut"),  sigc::ptr_fun (ProcessorBox::rb_cut));
1209         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1210         act = ActionManager::register_action (popup_act_grp, X_("copy"), _("Copy"),  sigc::ptr_fun (ProcessorBox::rb_copy));
1211         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1212
1213         act = ActionManager::register_action (popup_act_grp, X_("delete"), _("Delete"),  sigc::ptr_fun (ProcessorBox::rb_delete));
1214         ActionManager::plugin_selection_sensitive_actions.push_back(act); // ??
1215
1216         paste_action = ActionManager::register_action (popup_act_grp, X_("paste"), _("Paste"),  sigc::ptr_fun (ProcessorBox::rb_paste));
1217         act = ActionManager::register_action (popup_act_grp, X_("rename"), _("Rename"),  sigc::ptr_fun (ProcessorBox::rb_rename));
1218         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1219         ActionManager::register_action (popup_act_grp, X_("selectall"), _("Select All"),  sigc::ptr_fun (ProcessorBox::rb_select_all));
1220         ActionManager::register_action (popup_act_grp, X_("deselectall"), _("Deselect All"),  sigc::ptr_fun (ProcessorBox::rb_deselect_all));
1221                 
1222         /* activation */
1223         act = ActionManager::register_action (popup_act_grp, X_("activate"), _("Activate"),  sigc::ptr_fun (ProcessorBox::rb_activate));
1224         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1225         act = ActionManager::register_action (popup_act_grp, X_("deactivate"), _("Deactivate"),  sigc::ptr_fun (ProcessorBox::rb_deactivate));
1226         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1227         ActionManager::register_action (popup_act_grp, X_("activate_all"), _("Activate all"),  sigc::ptr_fun (ProcessorBox::rb_activate_all));
1228         ActionManager::register_action (popup_act_grp, X_("deactivate_all"), _("Deactivate all"),  sigc::ptr_fun (ProcessorBox::rb_deactivate_all));
1229
1230         /* show editors */
1231         act = ActionManager::register_action (popup_act_grp, X_("edit"), _("Edit"),  sigc::ptr_fun (ProcessorBox::rb_edit));
1232         ActionManager::plugin_selection_sensitive_actions.push_back(act);
1233
1234         ActionManager::add_action_group (popup_act_grp);
1235
1236
1237 }
1238
1239 void
1240 ProcessorBox::rb_choose_plugin ()
1241 {
1242         if (_current_processor_box == 0) {
1243                 return;
1244         }
1245         _current_processor_box->choose_plugin ();
1246 }
1247
1248 void
1249 ProcessorBox::rb_choose_insert ()
1250 {
1251         if (_current_processor_box == 0) {
1252                 return;
1253         }
1254         _current_processor_box->choose_insert ();
1255 }
1256
1257 void
1258 ProcessorBox::rb_choose_send ()
1259 {
1260         if (_current_processor_box == 0) {
1261                 return;
1262         }
1263         _current_processor_box->choose_send ();
1264 }
1265
1266 void
1267 ProcessorBox::rb_clear ()
1268 {
1269         if (_current_processor_box == 0) {
1270                 return;
1271         }
1272
1273         _current_processor_box->clear_processors ();
1274 }
1275
1276 void
1277 ProcessorBox::rb_cut ()
1278 {
1279         if (_current_processor_box == 0) {
1280                 return;
1281         }
1282
1283         _current_processor_box->cut_processors ();
1284 }
1285
1286 void
1287 ProcessorBox::rb_delete ()
1288 {
1289         if (_current_processor_box == 0) {
1290                 return;
1291         }
1292
1293         _current_processor_box->delete_processors ();
1294 }
1295
1296 void
1297 ProcessorBox::rb_copy ()
1298 {
1299         if (_current_processor_box == 0) {
1300                 return;
1301         }
1302         _current_processor_box->copy_processors ();
1303 }
1304
1305 void
1306 ProcessorBox::rb_paste ()
1307 {
1308         if (_current_processor_box == 0) {
1309                 return;
1310         }
1311
1312         _current_processor_box->paste_processors ();
1313 }
1314
1315 void
1316 ProcessorBox::rb_rename ()
1317 {
1318         if (_current_processor_box == 0) {
1319                 return;
1320         }
1321         _current_processor_box->rename_processors ();
1322 }
1323
1324 void
1325 ProcessorBox::rb_select_all ()
1326 {
1327         if (_current_processor_box == 0) {
1328                 return;
1329         }
1330
1331         _current_processor_box->select_all_processors ();
1332 }
1333
1334 void
1335 ProcessorBox::rb_deselect_all ()
1336 {
1337         if (_current_processor_box == 0) {
1338                 return;
1339         }
1340
1341         _current_processor_box->deselect_all_processors ();
1342 }
1343
1344 void
1345 ProcessorBox::rb_activate ()
1346 {
1347         if (_current_processor_box == 0) {
1348                 return;
1349         }
1350
1351         _current_processor_box->for_selected_processors (&ProcessorBox::activate_processor);
1352 }
1353
1354 void
1355 ProcessorBox::rb_deactivate ()
1356 {
1357         if (_current_processor_box == 0) {
1358                 return;
1359         }
1360         _current_processor_box->for_selected_processors (&ProcessorBox::deactivate_processor);
1361 }
1362
1363 void
1364 ProcessorBox::rb_activate_all ()
1365 {
1366         if (_current_processor_box == 0) {
1367                 return;
1368         }
1369
1370         _current_processor_box->all_processors_active (true);
1371 }
1372
1373 void
1374 ProcessorBox::rb_deactivate_all ()
1375 {
1376         if (_current_processor_box == 0) {
1377                 return;
1378         }
1379         _current_processor_box->all_processors_active (false);
1380 }
1381
1382 void
1383 ProcessorBox::rb_edit ()
1384 {
1385         if (_current_processor_box == 0) {
1386                 return;
1387         }
1388
1389         _current_processor_box->for_selected_processors (&ProcessorBox::edit_processor);
1390 }
1391
1392 void
1393 ProcessorBox::route_name_changed (PluginUIWindow* plugin_ui, boost::weak_ptr<PluginInsert> wpi)
1394 {
1395         ENSURE_GUI_THREAD(bind (mem_fun (*this, &ProcessorBox::route_name_changed), plugin_ui, wpi));
1396
1397         boost::shared_ptr<PluginInsert> pi (wpi.lock());
1398
1399         if (pi) {
1400                 WindowTitle title(Glib::get_application_name());
1401                 title += generate_processor_title (pi);
1402                 plugin_ui->set_title (title.get_string());
1403         }
1404 }
1405
1406 string 
1407 ProcessorBox::generate_processor_title (boost::shared_ptr<PluginInsert> pi)
1408 {
1409         string maker = pi->plugin()->maker() ? pi->plugin()->maker() : "";
1410         string::size_type email_pos;
1411
1412         if ((email_pos = maker.find_first_of ('<')) != string::npos) {
1413                 maker = maker.substr (0, email_pos - 1);
1414         }
1415
1416         if (maker.length() > 32) {
1417                 maker = maker.substr (0, 32);
1418                 maker += " ...";
1419         }
1420
1421         return string_compose(_("%1: %2 (by %3)"), _route->name(), pi->name(), maker);  
1422 }
1423