Fix MIDI disk-writer flush
[ardour.git] / gtk2_ardour / route_params_ui.cc
1 /*
2  * Copyright (C) 2005-2007 Taybin Rutkin <taybin@taybin.com>
3  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2007-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
6  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <algorithm>
24 #include <inttypes.h>
25
26 #include <glibmm/threads.h>
27 #include <gtkmm/stock.h>
28
29 #include "ardour/audioengine.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/plugin.h"
32 #include "ardour/plugin_insert.h"
33 #include "ardour/plugin_manager.h"
34 #include "ardour/port_insert.h"
35 #include "ardour/return.h"
36 #include "ardour/route.h"
37 #include "ardour/send.h"
38 #include "ardour/internal_send.h"
39
40 #include "gtkmm2ext/utils.h"
41 #include "gtkmm2ext/window_title.h"
42
43 #include "ardour_ui.h"
44 #include "gui_thread.h"
45 #include "io_selector.h"
46 #include "keyboard.h"
47 #include "mixer_ui.h"
48 #include "mixer_strip.h"
49 #include "port_insert_ui.h"
50 #include "plugin_selector.h"
51 #include "plugin_ui.h"
52 #include "return_ui.h"
53 #include "route_params_ui.h"
54 #include "send_ui.h"
55 #include "timers.h"
56
57 #include "pbd/i18n.h"
58
59 using namespace ARDOUR;
60 using namespace PBD;
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63
64 RouteParams_UI::RouteParams_UI ()
65         : ArdourWindow (_("Tracks and Busses"))
66         , track_menu(0)
67 {
68         insert_box = 0;
69         _input_iosel = 0;
70         _output_iosel = 0;
71         _active_view = 0;
72
73         using namespace Notebook_Helpers;
74
75         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
76         output_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
110         notebook.set_name ("InspectorNotebook");
111
112         title_label.set_name ("RouteParamsTitleLabel");
113         update_title();
114
115         // changeable area
116         route_param_frame.set_name("RouteParamsBaseFrame");
117         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
118
119         route_hpacker.pack_start (notebook, true, true);
120
121         route_vpacker.pack_start (title_label, false, false);
122         route_vpacker.pack_start (route_hpacker, true, true);
123
124         list_hpane.add (list_vpacker);
125         list_hpane.add (route_vpacker);
126
127         add (list_hpane);
128
129         set_name ("RouteParamsWindow");
130         set_default_size (620,370);
131         set_wmclass (X_("ardour_route_parameters"), PROGRAM_NAME);
132
133         // events
134         route_display.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &RouteParams_UI::route_selected));
135         route_display.get_column(0)->signal_clicked().connect(sigc::mem_fun(*this, &RouteParams_UI::show_track_menu));
136
137         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
138
139         show_all();
140 }
141
142 RouteParams_UI::~RouteParams_UI ()
143 {
144         delete track_menu;
145 }
146
147 void
148 RouteParams_UI::add_routes (RouteList& routes)
149 {
150         ENSURE_GUI_THREAD (*this, &RouteParams_UI::add_routes, routes)
151
152         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
153                 boost::shared_ptr<Route> route = (*x);
154
155                 if (route->is_auditioner()) {
156                         return;
157                 }
158
159                 TreeModel::Row row = *(route_display_model->append());
160                 row[route_display_columns.text] = route->name();
161                 row[route_display_columns.route] = route;
162
163                 //route_select_list.rows().back().select ();
164
165                 route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_property_changed, this, _1, boost::weak_ptr<Route>(route)), gui_context());
166                 route->DropReferences.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)), gui_context());
167         }
168 }
169
170
171 void
172 RouteParams_UI::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> wr)
173 {
174         if (!what_changed.contains (ARDOUR::Properties::name)) {
175                 return;
176         }
177
178         boost::shared_ptr<Route> route (wr.lock());
179
180         if (!route) {
181                 return;
182         }
183
184         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
185
186         bool found = false ;
187         TreeModel::Children rows = route_display_model->children();
188         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
189                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
190                 if (r == route) {
191                         (*iter)[route_display_columns.text] = route->name() ;
192                         found = true ;
193                         break;
194                 }
195         }
196
197         if(!found) {
198                 error << _("route display list item for renamed route not found!") << endmsg;
199         }
200
201         if (route == _route) {
202                 track_input_label.set_text (route->name());
203                 update_title();
204         }
205 }
206
207 void
208 RouteParams_UI::map_frozen()
209 {
210         ENSURE_GUI_THREAD (*this, &RouteParams_UI::map_frozen)
211         boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
212         if (at && insert_box) {
213                 switch (at->freeze_state()) {
214                         case AudioTrack::Frozen:
215                                 insert_box->set_sensitive (false);
216                                 //hide_redirect_editors (); // TODO hide editor windows
217                                 break;
218                         default:
219                                 insert_box->set_sensitive (true);
220                                 // XXX need some way, maybe, to retoggle redirect editors
221                                 break;
222                 }
223         }
224 }
225
226 PluginSelector*
227 RouteParams_UI::plugin_selector() {
228         return Mixer_UI::instance()->plugin_selector ();
229 }
230
231 void
232 RouteParams_UI::setup_processor_boxes()
233 {
234         if (_session && _route) {
235
236                 // just in case... shouldn't need this
237                 cleanup_processor_boxes();
238
239                 // construct new redirect boxes
240                 insert_box = new ProcessorBox (_session, boost::bind (&RouteParams_UI::plugin_selector, this), _p_selection, 0);
241                 insert_box->set_route (_route);
242
243                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(_route);
244                 if (at) {
245                         at->FreezeChange.connect (route_connections, invalidator (*this), boost::bind (&RouteParams_UI::map_frozen, this), gui_context());
246                 }
247                 redir_hpane.add (*insert_box);
248
249                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));  //note:  this indicates a double-click activation, not just a "selection"
250                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
251
252                 redir_hpane.show_all();
253         }
254 }
255
256 void
257 RouteParams_UI::cleanup_processor_boxes()
258 {
259         if (insert_box) {
260                 redir_hpane.remove(*insert_box);
261                 delete insert_box;
262                 insert_box = 0;
263         }
264 }
265
266 void
267 RouteParams_UI::setup_io_selector()
268 {
269         cleanup_io_selector();
270
271         // input
272         _input_iosel = new IOSelector (this, _session, _route->input());
273         _input_iosel->setup ();
274         input_frame.add (*_input_iosel);
275         input_frame.show_all();
276
277         // output
278         _output_iosel = new IOSelector (this, _session, _route->output());
279         _output_iosel->setup ();
280         output_frame.add (*_output_iosel);
281         output_frame.show_all();
282 }
283
284 void
285 RouteParams_UI::cleanup_io_selector()
286 {
287         if (_input_iosel) {
288                 _input_iosel->Finished (IOSelector::Cancelled);
289                 input_frame.remove();
290                 delete _input_iosel;
291                 _input_iosel = 0;
292         }
293
294         if (_output_iosel) {
295                 _output_iosel->Finished (IOSelector::Cancelled);
296
297                 output_frame.remove();
298                 delete _output_iosel;
299                 _output_iosel = 0;
300         }
301 }
302
303 void
304 RouteParams_UI::cleanup_view (bool stopupdate)
305 {
306         if (_active_view) {
307                 GenericPluginUI* plugui = 0;
308
309                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
310                         plugui->stop_updating (0);
311                 }
312
313                 _processor_going_away_connection.disconnect ();
314                 redir_hpane.remove(*_active_view);
315                 delete _active_view;
316                 _active_view = 0;
317         }
318 }
319
320 void
321 RouteParams_UI::route_removed (boost::weak_ptr<Route> wr)
322 {
323         boost::shared_ptr<Route> route (wr.lock());
324
325         if (!route) {
326                 return;
327         }
328
329         ENSURE_GUI_THREAD (*this, invalidator (*this), &RouteParams_UI::route_removed, wr)
330
331         TreeModel::Children rows = route_display_model->children();
332         TreeModel::Children::iterator ri;
333
334         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
335                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
336
337                 if (r == route) {
338                         route_display_model->erase(iter);
339                         break;
340                 }
341         }
342
343         if (route == _route) {
344                 cleanup_io_selector();
345                 cleanup_view();
346                 cleanup_processor_boxes();
347
348                 _route.reset ((Route*) 0);
349                 _processor.reset ((Processor*) 0);
350                 update_title();
351         }
352 }
353
354 void
355 RouteParams_UI::set_session (Session *sess)
356 {
357         ArdourWindow::set_session (sess);
358
359         route_display_model->clear();
360
361         if (_session) {
362                 boost::shared_ptr<RouteList> r = _session->get_routes();
363                 add_routes (*r);
364                 _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&RouteParams_UI::add_routes, this, _1), gui_context());
365                 start_updating ();
366         } else {
367                 stop_updating ();
368         }
369 }
370
371
372 void
373 RouteParams_UI::session_going_away ()
374 {
375         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
376
377         SessionHandlePtr::session_going_away ();
378
379         route_display_model->clear();
380
381         cleanup_io_selector();
382         cleanup_view();
383         cleanup_processor_boxes();
384
385         _route.reset ((Route*) 0);
386         _processor.reset ((Processor*) 0);
387         update_title();
388 }
389
390 void
391 RouteParams_UI::route_selected()
392 {
393         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
394         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
395
396         if(iter) {
397                 //If anything is selected
398                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
399
400                 if (_route == route) {
401                         // do nothing
402                         return;
403                 }
404
405                 // remove event binding from previously selected
406                 if (_route) {
407                         _route_processors_connection.disconnect ();
408                         cleanup_processor_boxes();
409                         cleanup_view();
410                         cleanup_io_selector();
411                 }
412
413                 // update the other panes with the correct info
414                 _route = route;
415                 //update_routeinfo (route);
416
417                 setup_io_selector();
418                 setup_processor_boxes();
419
420                 route->processors_changed.connect (_route_processors_connection, invalidator (*this), boost::bind (&RouteParams_UI::processors_changed, this, _1), gui_context());
421
422                 track_input_label.set_text (_route->name());
423
424                 update_title();
425
426         } else {
427                 // no selection
428                 if (_route) {
429                         _route_processors_connection.disconnect ();
430
431                         // remove from view
432                         cleanup_io_selector();
433                         cleanup_view();
434                         cleanup_processor_boxes();
435
436                         _route.reset ((Route*) 0);
437                         _processor.reset ((Processor*) 0);
438                         track_input_label.set_text(_("NO TRACK"));
439                         update_title();
440                 }
441         }
442 }
443
444 void
445 RouteParams_UI::processors_changed (RouteProcessorChange)
446 {
447         cleanup_view();
448
449         _processor.reset ((Processor*) 0);
450
451         //update_title();
452 }
453
454 void
455 RouteParams_UI::show_track_menu()
456 {
457         using namespace Menu_Helpers;
458
459         if (track_menu == 0) {
460                 track_menu = new Menu;
461                 track_menu->set_name ("ArdourContextMenu");
462                 track_menu->items().push_back (MenuElem (_("Add Track or Bus"), sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route)));
463         }
464         track_menu->popup (1, gtk_get_current_event_time());
465 }
466
467 void
468 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
469 {
470         boost::shared_ptr<Send> send;
471         boost::shared_ptr<Return> retrn;
472         boost::shared_ptr<PluginInsert> plugin_insert;
473         boost::shared_ptr<PortInsert> port_insert;
474
475         if ((boost::dynamic_pointer_cast<InternalSend> (proc)) != 0) {
476                 cleanup_view();
477                 _processor.reset ((Processor*) 0);
478                 update_title();
479                 return;
480         } else if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
481
482                 SendUI *send_ui = new SendUI (this, send, _session);
483
484                 cleanup_view();
485                 send->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
486                 _active_view = send_ui;
487
488                 redir_hpane.add (*_active_view);
489                 redir_hpane.show_all();
490
491         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
492
493                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
494
495                 cleanup_view();
496                 retrn->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
497                 _active_view = return_ui;
498
499                 redir_hpane.add (*_active_view);
500                 redir_hpane.show_all();
501
502         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
503
504                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
505
506                 cleanup_view();
507                 plugin_insert->plugin()->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader), gui_context());
508                 plugin_ui->start_updating (0);
509                 _active_view = plugin_ui;
510
511                 redir_hpane.add (*_active_view);
512                 redir_hpane.show_all();
513
514         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
515
516                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
517
518                 cleanup_view();
519                 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());
520                 _active_view = portinsert_ui;
521
522                 redir_hpane.add (*_active_view);
523                 portinsert_ui->redisplay();
524                 redir_hpane.show_all();
525         }
526
527         _processor = proc;
528         update_title();
529
530 }
531
532 void
533 RouteParams_UI::plugin_going_away (Placement place)
534 {
535         ENSURE_GUI_THREAD (*this, &RouteParams_UI::plugin_going_away, place)
536
537         // delete the current view without calling finish
538
539         if (place == PreFader) {
540                 cleanup_view (false);
541                 _processor.reset ((Processor*) 0);
542         }
543 }
544
545 void
546 RouteParams_UI::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
547 {
548         boost::shared_ptr<Processor> proc = (wproc.lock());
549
550         if (!proc) {
551                 return;
552         }
553
554         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
555
556         printf ("redirect going away\n");
557         // delete the current view without calling finish
558         if (proc == _processor) {
559                 cleanup_view (false);
560                 _processor.reset ((Processor*) 0);
561         }
562 }
563
564 void
565 RouteParams_UI::update_title ()
566 {
567         WindowTitle title (_("Tracks and Busses"));
568
569         if (_route) {
570                 title_label.set_text(_route->name());
571                 title += _route->name();
572                 set_title(title.get_string());
573         } else {
574                 title_label.set_text(_("No Track or Bus Selected"));
575                 title += _("No Track or Bus Selected");
576                 set_title(title.get_string());
577         }
578 }
579
580 void
581 RouteParams_UI::start_updating ()
582 {
583         update_connection = Timers::rapid_connect
584                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
585 }
586
587 void
588 RouteParams_UI::stop_updating ()
589 {
590         update_connection.disconnect();
591 }
592
593 void
594 RouteParams_UI::update_views ()
595 {
596         SendUI *sui;
597         // TODO: only do it if correct tab is showing
598
599         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
600                 sui->update ();
601         }
602 }