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