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