Re-use the Mixer's PluginManager (singleton)
[ardour.git] / gtk2_ardour / route_params_ui.cc
1 /*
2     Copyright (C) 2000 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 <algorithm>
21 #include <inttypes.h>
22
23 #include <glibmm/threads.h>
24 #include <gtkmm/stock.h>
25
26 #include "ardour/audioengine.h"
27 #include "ardour/audio_track.h"
28 #include "ardour/plugin.h"
29 #include "ardour/plugin_insert.h"
30 #include "ardour/plugin_manager.h"
31 #include "ardour/port_insert.h"
32 #include "ardour/return.h"
33 #include "ardour/route.h"
34 #include "ardour/send.h"
35 #include "ardour/internal_send.h"
36
37 #include "gtkmm2ext/utils.h"
38 #include "gtkmm2ext/window_title.h"
39
40 #include "ardour_ui.h"
41 #include "gui_thread.h"
42 #include "io_selector.h"
43 #include "keyboard.h"
44 #include "mixer_ui.h"
45 #include "mixer_strip.h"
46 #include "port_insert_ui.h"
47 #include "plugin_selector.h"
48 #include "plugin_ui.h"
49 #include "return_ui.h"
50 #include "route_params_ui.h"
51 #include "send_ui.h"
52 #include "timers.h"
53
54 #include "pbd/i18n.h"
55
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace Gtk;
59 using namespace Gtkmm2ext;
60
61 RouteParams_UI::RouteParams_UI ()
62         : ArdourWindow (_("Tracks and Busses"))
63         , latency_apply_button (Stock::APPLY)
64         , track_menu(0)
65 {
66         insert_box = 0;
67         _input_iosel = 0;
68         _output_iosel = 0;
69         _active_view = 0;
70         latency_widget = 0;
71
72         using namespace Notebook_Helpers;
73
74         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
75         output_frame.set_shadow_type(Gtk::SHADOW_NONE);
76         latency_frame.set_shadow_type (Gtk::SHADOW_NONE);
77
78         notebook.set_show_tabs (true);
79         notebook.set_show_border (true);
80         notebook.set_name ("RouteParamNotebook");
81
82         // create the tree model
83         route_display_model = ListStore::create(route_display_columns);
84
85         // setup the treeview
86         route_display.set_model(route_display_model);
87         route_display.append_column(_("Tracks/Busses"), route_display_columns.text);
88         route_display.set_name(X_("RouteParamsListDisplay"));
89         route_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE); // default
90         route_display.set_reorderable(false);
91         route_display.set_size_request(75, -1);
92         route_display.set_headers_visible(true);
93         route_display.set_headers_clickable(true);
94
95         dynamic_cast<Gtk::CellRendererText*>(route_display.get_column_cell_renderer(0))->property_ellipsize() = Pango::ELLIPSIZE_START;
96
97         route_select_scroller.add(route_display);
98         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
99
100         route_select_frame.set_name("RouteSelectBaseFrame");
101         route_select_frame.set_shadow_type (Gtk::SHADOW_IN);
102         route_select_frame.add(route_select_scroller);
103
104         list_vpacker.pack_start (route_select_frame, true, true);
105
106         notebook.pages().push_back (TabElem (input_frame, _("Inputs")));
107         notebook.pages().push_back (TabElem (output_frame, _("Outputs")));
108         notebook.pages().push_back (TabElem (redir_hpane, _("Plugins, Inserts & Sends")));
109         notebook.pages().push_back (TabElem (latency_frame, _("Latency")));
110
111         notebook.set_name ("InspectorNotebook");
112
113         title_label.set_name ("RouteParamsTitleLabel");
114         update_title();
115
116         latency_packer.set_spacing (18);
117         latency_button_box.pack_start (latency_apply_button);
118         delay_label.set_alignment (0, 0.5);
119
120         // changeable area
121         route_param_frame.set_name("RouteParamsBaseFrame");
122         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
123
124         route_hpacker.pack_start (notebook, true, true);
125
126         route_vpacker.pack_start (title_label, false, false);
127         route_vpacker.pack_start (route_hpacker, true, true);
128
129         list_hpane.add (list_vpacker);
130         list_hpane.add (route_vpacker);
131
132         add (list_hpane);
133
134         set_name ("RouteParamsWindow");
135         set_default_size (620,370);
136         set_wmclass (X_("ardour_route_parameters"), PROGRAM_NAME);
137
138         // events
139         route_display.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &RouteParams_UI::route_selected));
140         route_display.get_column(0)->signal_clicked().connect(sigc::mem_fun(*this, &RouteParams_UI::show_track_menu));
141
142         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
143
144         show_all();
145 }
146
147 RouteParams_UI::~RouteParams_UI ()
148 {
149         delete track_menu;
150 }
151
152 void
153 RouteParams_UI::add_routes (RouteList& routes)
154 {
155         ENSURE_GUI_THREAD (*this, &RouteParams_UI::add_routes, routes)
156
157         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
158                 boost::shared_ptr<Route> route = (*x);
159
160                 if (route->is_auditioner()) {
161                         return;
162                 }
163
164                 TreeModel::Row row = *(route_display_model->append());
165                 row[route_display_columns.text] = route->name();
166                 row[route_display_columns.route] = route;
167
168                 //route_select_list.rows().back().select ();
169
170                 route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_property_changed, this, _1, boost::weak_ptr<Route>(route)), gui_context());
171                 route->DropReferences.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)), gui_context());
172         }
173 }
174
175
176 void
177 RouteParams_UI::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> wr)
178 {
179         if (!what_changed.contains (ARDOUR::Properties::name)) {
180                 return;
181         }
182
183         boost::shared_ptr<Route> route (wr.lock());
184
185         if (!route) {
186                 return;
187         }
188
189         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
190
191         bool found = false ;
192         TreeModel::Children rows = route_display_model->children();
193         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
194                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
195                 if (r == route) {
196                         (*iter)[route_display_columns.text] = route->name() ;
197                         found = true ;
198                         break;
199                 }
200         }
201
202         if(!found) {
203                 error << _("route display list item for renamed route not found!") << endmsg;
204         }
205
206         if (route == _route) {
207                 track_input_label.set_text (route->name());
208                 update_title();
209         }
210 }
211
212 void
213 RouteParams_UI::map_frozen()
214 {
215         ENSURE_GUI_THREAD (*this, &RouteParams_UI::map_frozen)
216         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
217         if (at && insert_box) {
218                 switch (at->freeze_state()) {
219                         case AudioTrack::Frozen:
220                                 insert_box->set_sensitive (false);
221                                 //hide_redirect_editors (); // TODO hide editor windows
222                                 break;
223                         default:
224                                 insert_box->set_sensitive (true);
225                                 // XXX need some way, maybe, to retoggle redirect editors
226                                 break;
227                 }
228         }
229 }
230
231 PluginSelector*
232 RouteParams_UI::plugin_selector() {
233         return Mixer_UI::instance()->plugin_selector ();
234 }
235
236 void
237 RouteParams_UI::setup_processor_boxes()
238 {
239         if (_session && _route) {
240
241                 // just in case... shouldn't need this
242                 cleanup_processor_boxes();
243
244                 // construct new redirect boxes
245                 insert_box = new ProcessorBox (_session, boost::bind (&RouteParams_UI::plugin_selector, this), _p_selection, 0);
246                 insert_box->set_route (_route);
247
248                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
249                 if (at) {
250                         at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&RouteParams_UI::map_frozen, this), gui_context());
251                 }
252                 redir_hpane.add (*insert_box);
253
254                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));  //note:  this indicates a double-click activation, not just a "selection"
255                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
256
257                 redir_hpane.show_all();
258         }
259 }
260
261 void
262 RouteParams_UI::cleanup_processor_boxes()
263 {
264         if (insert_box) {
265                 redir_hpane.remove(*insert_box);
266                 delete insert_box;
267                 insert_box = 0;
268         }
269 }
270
271 void
272 RouteParams_UI::refresh_latency ()
273 {
274         if (latency_widget) {
275                 latency_widget->refresh();
276
277                 char buf[128];
278                 snprintf (buf, sizeof (buf), _("Latency: %" PRId64 " samples"), _route->signal_latency ());
279                 delay_label.set_text (buf);
280         }
281 }
282
283 void
284 RouteParams_UI::cleanup_latency_frame ()
285 {
286         if (latency_widget) {
287                 latency_frame.remove ();
288                 latency_packer.remove (*latency_widget);
289                 latency_packer.remove (latency_button_box);
290                 latency_packer.remove (delay_label);
291                 latency_connections.drop_connections ();
292                 latency_click_connection.disconnect ();
293
294                 delete latency_widget;
295                 latency_widget = 0;
296
297         }
298 }
299
300 void
301 RouteParams_UI::setup_latency_frame ()
302 {
303         latency_widget = new LatencyGUI (*(_route->output()), _session->sample_rate(), AudioEngine::instance()->samples_per_cycle());
304
305         char buf[128];
306         snprintf (buf, sizeof (buf), _("Latency: %" PRId64 " samples"), _route->signal_latency());
307         delay_label.set_text (buf);
308
309         latency_packer.pack_start (*latency_widget, false, false);
310         latency_packer.pack_start (latency_button_box, false, false);
311         latency_packer.pack_start (delay_label);
312
313         latency_click_connection = latency_apply_button.signal_clicked().connect (sigc::mem_fun (*latency_widget, &LatencyGUI::finish));
314         _route->signal_latency_updated.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
315
316         latency_frame.add (latency_packer);
317         latency_frame.show_all ();
318 }
319
320 void
321 RouteParams_UI::setup_io_selector()
322 {
323         cleanup_io_selector();
324
325         // input
326         _input_iosel = new IOSelector (this, _session, _route->input());
327         _input_iosel->setup ();
328         input_frame.add (*_input_iosel);
329         input_frame.show_all();
330
331         // output
332         _output_iosel = new IOSelector (this, _session, _route->output());
333         _output_iosel->setup ();
334         output_frame.add (*_output_iosel);
335         output_frame.show_all();
336 }
337
338 void
339 RouteParams_UI::cleanup_io_selector()
340 {
341         if (_input_iosel) {
342                 _input_iosel->Finished (IOSelector::Cancelled);
343                 input_frame.remove();
344                 delete _input_iosel;
345                 _input_iosel = 0;
346         }
347
348         if (_output_iosel) {
349                 _output_iosel->Finished (IOSelector::Cancelled);
350
351                 output_frame.remove();
352                 delete _output_iosel;
353                 _output_iosel = 0;
354         }
355 }
356
357 void
358 RouteParams_UI::cleanup_view (bool stopupdate)
359 {
360         if (_active_view) {
361                 GenericPluginUI* plugui = 0;
362
363                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
364                         plugui->stop_updating (0);
365                 }
366
367                 _processor_going_away_connection.disconnect ();
368                 redir_hpane.remove(*_active_view);
369                 delete _active_view;
370                 _active_view = 0;
371         }
372 }
373
374 void
375 RouteParams_UI::route_removed (boost::weak_ptr<Route> wr)
376 {
377         boost::shared_ptr<Route> route (wr.lock());
378
379         if (!route) {
380                 return;
381         }
382
383         ENSURE_GUI_THREAD (*this, invalidator (*this), &RouteParams_UI::route_removed, wr)
384
385         TreeModel::Children rows = route_display_model->children();
386         TreeModel::Children::iterator ri;
387
388         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
389                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
390
391                 if (r == route) {
392                         route_display_model->erase(iter);
393                         break;
394                 }
395         }
396
397         if (route == _route) {
398                 cleanup_io_selector();
399                 cleanup_view();
400                 cleanup_processor_boxes();
401
402                 _route.reset ((Route*) 0);
403                 _processor.reset ((Processor*) 0);
404                 update_title();
405         }
406 }
407
408 void
409 RouteParams_UI::set_session (Session *sess)
410 {
411         ArdourWindow::set_session (sess);
412
413         route_display_model->clear();
414
415         if (_session) {
416                 boost::shared_ptr<RouteList> r = _session->get_routes();
417                 add_routes (*r);
418                 _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&RouteParams_UI::add_routes, this, _1), gui_context());
419                 start_updating ();
420         } else {
421                 stop_updating ();
422         }
423 }
424
425
426 void
427 RouteParams_UI::session_going_away ()
428 {
429         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
430
431         SessionHandlePtr::session_going_away ();
432
433         route_display_model->clear();
434
435         cleanup_io_selector();
436         cleanup_view();
437         cleanup_processor_boxes();
438         cleanup_latency_frame ();
439
440         _route.reset ((Route*) 0);
441         _processor.reset ((Processor*) 0);
442         update_title();
443 }
444
445 void
446 RouteParams_UI::route_selected()
447 {
448         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
449         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
450
451         if(iter) {
452                 //If anything is selected
453                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
454
455                 if (_route == route) {
456                         // do nothing
457                         return;
458                 }
459
460                 // remove event binding from previously selected
461                 if (_route) {
462                         _route_processors_connection.disconnect ();
463                         cleanup_processor_boxes();
464                         cleanup_view();
465                         cleanup_io_selector();
466                         cleanup_latency_frame ();
467                 }
468
469                 // update the other panes with the correct info
470                 _route = route;
471                 //update_routeinfo (route);
472
473                 setup_io_selector();
474                 setup_processor_boxes();
475                 setup_latency_frame ();
476
477                 route->processors_changed.connect (_route_processors_connection, invalidator (*this), boost::bind (&RouteParams_UI::processors_changed, this, _1), gui_context());
478
479                 track_input_label.set_text (_route->name());
480
481                 update_title();
482
483         } else {
484                 // no selection
485                 if (_route) {
486                         _route_processors_connection.disconnect ();
487
488                         // remove from view
489                         cleanup_io_selector();
490                         cleanup_view();
491                         cleanup_processor_boxes();
492                         cleanup_latency_frame ();
493
494                         _route.reset ((Route*) 0);
495                         _processor.reset ((Processor*) 0);
496                         track_input_label.set_text(_("NO TRACK"));
497                         update_title();
498                 }
499         }
500 }
501
502 void
503 RouteParams_UI::processors_changed (RouteProcessorChange)
504 {
505         cleanup_view();
506
507         _processor.reset ((Processor*) 0);
508
509         //update_title();
510 }
511
512 void
513 RouteParams_UI::show_track_menu()
514 {
515         using namespace Menu_Helpers;
516
517         if (track_menu == 0) {
518                 track_menu = new Menu;
519                 track_menu->set_name ("ArdourContextMenu");
520                 track_menu->items().push_back (MenuElem (_("Add Track or Bus"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route)));
521         }
522         track_menu->popup (1, gtk_get_current_event_time());
523 }
524
525 void
526 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
527 {
528         boost::shared_ptr<Send> send;
529         boost::shared_ptr<Return> retrn;
530         boost::shared_ptr<PluginInsert> plugin_insert;
531         boost::shared_ptr<PortInsert> port_insert;
532
533         if ((boost::dynamic_pointer_cast<InternalSend> (proc)) != 0) {
534                 cleanup_view();
535                 _processor.reset ((Processor*) 0);
536                 update_title();
537                 return;
538         } else if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
539
540                 SendUI *send_ui = new SendUI (this, send, _session);
541
542                 cleanup_view();
543                 send->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
544                 _active_view = send_ui;
545
546                 redir_hpane.add (*_active_view);
547                 redir_hpane.show_all();
548
549         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
550
551                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
552
553                 cleanup_view();
554                 retrn->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
555                 _active_view = return_ui;
556
557                 redir_hpane.add (*_active_view);
558                 redir_hpane.show_all();
559
560         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
561
562                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
563
564                 cleanup_view();
565                 plugin_insert->plugin()->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader), gui_context());
566                 plugin_ui->start_updating (0);
567                 _active_view = plugin_ui;
568
569                 redir_hpane.add (*_active_view);
570                 redir_hpane.show_all();
571
572         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
573
574                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
575
576                 cleanup_view();
577                 port_insert->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor> (proc)), gui_context());
578                 _active_view = portinsert_ui;
579
580                 redir_hpane.add (*_active_view);
581                 portinsert_ui->redisplay();
582                 redir_hpane.show_all();
583         }
584
585         _processor = proc;
586         update_title();
587
588 }
589
590 void
591 RouteParams_UI::plugin_going_away (Placement place)
592 {
593         ENSURE_GUI_THREAD (*this, &RouteParams_UI::plugin_going_away, place)
594
595         // delete the current view without calling finish
596
597         if (place == PreFader) {
598                 cleanup_view (false);
599                 _processor.reset ((Processor*) 0);
600         }
601 }
602
603 void
604 RouteParams_UI::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
605 {
606         boost::shared_ptr<Processor> proc = (wproc.lock());
607
608         if (!proc) {
609                 return;
610         }
611
612         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
613
614         printf ("redirect going away\n");
615         // delete the current view without calling finish
616         if (proc == _processor) {
617                 cleanup_view (false);
618                 _processor.reset ((Processor*) 0);
619         }
620 }
621
622 void
623 RouteParams_UI::update_title ()
624 {
625         WindowTitle title (_("Tracks and Busses"));
626
627         if (_route) {
628                 title_label.set_text(_route->name());
629                 title += _route->name();
630                 set_title(title.get_string());
631         } else {
632                 title_label.set_text(_("No Track or Bus Selected"));
633                 title += _("No Track or Bus Selected");
634                 set_title(title.get_string());
635         }
636 }
637
638 void
639 RouteParams_UI::start_updating ()
640 {
641         update_connection = Timers::rapid_connect
642                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
643 }
644
645 void
646 RouteParams_UI::stop_updating ()
647 {
648         update_connection.disconnect();
649 }
650
651 void
652 RouteParams_UI::update_views ()
653 {
654         SendUI *sui;
655         // TODO: only do it if correct tab is showing
656
657         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
658                 sui->update ();
659         }
660 }