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