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