Aux-sends do not have a GUI - fixes #5621
[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 <gtkmm2ext/utils.h>
25 #include <gtkmm2ext/window_title.h>
26
27 #include "ardour/audioengine.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 "ardour_ui.h"
38 #include "gui_thread.h"
39 #include "io_selector.h"
40 #include "keyboard.h"
41 #include "mixer_strip.h"
42 #include "port_insert_ui.h"
43 #include "plugin_selector.h"
44 #include "plugin_ui.h"
45 #include "return_ui.h"
46 #include "route_params_ui.h"
47 #include "send_ui.h"
48 #include "utils.h"
49
50 #include "i18n.h"
51
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtk;
55 using namespace Gtkmm2ext;
56
57 RouteParams_UI::RouteParams_UI ()
58         : ArdourWindow (_("Tracks and Busses")),
59           latency_apply_button (Stock::APPLY),
60           track_menu(0)
61 {
62         insert_box = 0;
63         _input_iosel = 0;
64         _output_iosel = 0;
65         _active_view = 0;
66         latency_widget = 0;
67
68         using namespace Notebook_Helpers;
69
70         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
71         output_frame.set_shadow_type(Gtk::SHADOW_NONE);
72         latency_frame.set_shadow_type (Gtk::SHADOW_NONE);
73
74         notebook.set_show_tabs (true);
75         notebook.set_show_border (true);
76         notebook.set_name ("RouteParamNotebook");
77
78         // create the tree model
79         route_display_model = ListStore::create(route_display_columns);
80
81         // setup the treeview
82         route_display.set_model(route_display_model);
83         route_display.append_column(_("Tracks/Busses"), route_display_columns.text);
84         route_display.set_name(X_("RouteParamsListDisplay"));
85         route_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE); // default
86         route_display.set_reorderable(false);
87         route_display.set_size_request(75, -1);
88         route_display.set_headers_visible(true);
89         route_display.set_headers_clickable(true);
90
91         dynamic_cast<Gtk::CellRendererText*>(route_display.get_column_cell_renderer(0))->property_ellipsize() = Pango::ELLIPSIZE_START;
92
93         route_select_scroller.add(route_display);
94         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
95
96         route_select_frame.set_name("RouteSelectBaseFrame");
97         route_select_frame.set_shadow_type (Gtk::SHADOW_IN);
98         route_select_frame.add(route_select_scroller);
99
100         list_vpacker.pack_start (route_select_frame, true, true);
101
102         notebook.pages().push_back (TabElem (input_frame, _("Inputs")));
103         notebook.pages().push_back (TabElem (output_frame, _("Outputs")));
104         notebook.pages().push_back (TabElem (redir_hpane, _("Plugins, Inserts & Sends")));
105         notebook.pages().push_back (TabElem (latency_frame, _("Latency")));
106
107         notebook.set_name ("InspectorNotebook");
108
109         title_label.set_name ("RouteParamsTitleLabel");
110         update_title();
111
112         latency_packer.set_spacing (18);
113         latency_button_box.pack_start (latency_apply_button);
114         delay_label.set_alignment (0, 0.5);
115
116         // changeable area
117         route_param_frame.set_name("RouteParamsBaseFrame");
118         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
119
120
121         route_hpacker.pack_start (notebook, true, true);
122
123         route_vpacker.pack_start (title_label, false, false);
124         route_vpacker.pack_start (route_hpacker, true, true);
125
126
127         list_hpane.pack1 (list_vpacker);
128         list_hpane.add2 (route_vpacker);
129
130         list_hpane.set_position(110);
131
132         redir_hpane.set_position(110);
133
134         //global_vpacker.pack_start (list_hpane, true, true);
135         //get_vbox()->pack_start (global_vpacker);
136         add (list_hpane);
137
138
139         set_name ("RouteParamsWindow");
140         set_default_size (620,370);
141         set_wmclass (X_("ardour_route_parameters"), PROGRAM_NAME);
142
143         // events
144         route_display.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &RouteParams_UI::route_selected));
145         route_display.get_column(0)->signal_clicked().connect(sigc::mem_fun(*this, &RouteParams_UI::show_track_menu));
146
147         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
148
149         _plugin_selector = new PluginSelector (PluginManager::instance());
150         show_all();
151 }
152
153 RouteParams_UI::~RouteParams_UI ()
154 {
155 }
156
157 void
158 RouteParams_UI::add_routes (RouteList& routes)
159 {
160         ENSURE_GUI_THREAD (*this, &RouteParams_UI::add_routes, routes)
161
162         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
163                 boost::shared_ptr<Route> route = (*x);
164
165                 if (route->is_auditioner()) {
166                         return;
167                 }
168
169                 TreeModel::Row row = *(route_display_model->append());
170                 row[route_display_columns.text] = route->name();
171                 row[route_display_columns.route] = route;
172
173                 //route_select_list.rows().back().select ();
174
175                 route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_property_changed, this, _1, boost::weak_ptr<Route>(route)), gui_context());
176                 route->DropReferences.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)), gui_context());
177         }
178 }
179
180
181 void
182 RouteParams_UI::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> wr)
183 {
184         if (!what_changed.contains (ARDOUR::Properties::name)) {
185                 return;
186         }
187
188         boost::shared_ptr<Route> route (wr.lock());
189
190         if (!route) {
191                 return;
192         }
193
194         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
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, boost::bind (&RouteParams_UI::plugin_selector, this), _rr_selection, 0);
227                 insert_box->set_route (_route);
228
229                 redir_hpane.pack1 (*insert_box);
230
231                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
232                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
233
234                 redir_hpane.show_all();
235         }
236 }
237
238 void
239 RouteParams_UI::cleanup_processor_boxes()
240 {
241         if (insert_box) {
242                 redir_hpane.remove(*insert_box);
243                 delete insert_box;
244                 insert_box = 0;
245         }
246 }
247
248 void
249 RouteParams_UI::refresh_latency ()
250 {
251         if (latency_widget) {
252                 latency_widget->refresh();
253
254                 char buf[128];
255                 snprintf (buf, sizeof (buf), _("Playback delay: %" PRId64 " samples"), _route->initial_delay());
256                 delay_label.set_text (buf);
257         }
258 }
259
260 void
261 RouteParams_UI::cleanup_latency_frame ()
262 {
263         if (latency_widget) {
264                 latency_frame.remove ();
265                 latency_packer.remove (*latency_widget);
266                 latency_packer.remove (latency_button_box);
267                 latency_packer.remove (delay_label);
268                 latency_connections.drop_connections ();
269                 latency_click_connection.disconnect ();
270
271                 delete latency_widget;
272                 latency_widget = 0;
273
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: %" PRId64 " 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_click_connection = latency_apply_button.signal_clicked().connect (sigc::mem_fun (*latency_widget, &LatencyGUI::finish));
291         _route->signal_latency_changed.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
292         _route->initial_delay_changed.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
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                 _processor_going_away_connection.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::weak_ptr<Route> wr)
354 {
355         boost::shared_ptr<Route> route (wr.lock());
356
357         if (!route) {
358                 return;
359         }
360
361         ENSURE_GUI_THREAD (*this, invalidator (*this), &RouteParams_UI::route_removed, wr)
362
363         TreeModel::Children rows = route_display_model->children();
364         TreeModel::Children::iterator ri;
365
366         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
367                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
368
369                 if (r == route) {
370                         route_display_model->erase(iter);
371                         break;
372                 }
373         }
374
375         if (route == _route) {
376                 cleanup_io_frames();
377                 cleanup_view();
378                 cleanup_processor_boxes();
379
380                 _route.reset ((Route*) 0);
381                 _processor.reset ((Processor*) 0);
382                 update_title();
383         }
384 }
385
386 void
387 RouteParams_UI::set_session (Session *sess)
388 {
389         ArdourWindow::set_session (sess);
390
391         route_display_model->clear();
392         _plugin_selector->set_session (_session);
393
394         if (_session) {
395                 boost::shared_ptr<RouteList> r = _session->get_routes();
396                 add_routes (*r);
397                 _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&RouteParams_UI::add_routes, this, _1), gui_context());
398                 start_updating ();
399         } else {
400                 stop_updating ();
401         }
402 }
403
404
405 void
406 RouteParams_UI::session_going_away ()
407 {
408         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
409
410         SessionHandlePtr::session_going_away ();
411
412         route_display_model->clear();
413
414         cleanup_io_frames();
415         cleanup_view();
416         cleanup_processor_boxes();
417         cleanup_latency_frame ();
418
419         _route.reset ((Route*) 0);
420         _processor.reset ((Processor*) 0);
421         update_title();
422 }
423
424 void
425 RouteParams_UI::route_selected()
426 {
427         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
428         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
429
430         if(iter) {
431                 //If anything is selected
432                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
433
434                 if (_route == route) {
435                         // do nothing
436                         return;
437                 }
438
439                 // remove event binding from previously selected
440                 if (_route) {
441                         _route_processors_connection.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                 route->processors_changed.connect (_route_processors_connection, invalidator (*this), boost::bind (&RouteParams_UI::processors_changed, this, _1), gui_context());
457
458                 track_input_label.set_text (_route->name());
459
460                 update_title();
461
462         } else {
463                 // no selection
464                 if (_route) {
465                         _route_processors_connection.disconnect ();
466
467                         // remove from view
468                         cleanup_io_frames();
469                         cleanup_view();
470                         cleanup_processor_boxes();
471                         cleanup_latency_frame ();
472
473                         _route.reset ((Route*) 0);
474                         _processor.reset ((Processor*) 0);
475                         track_input_label.set_text(_("NO TRACK"));
476                         update_title();
477                 }
478         }
479 }
480
481 void
482 RouteParams_UI::processors_changed (RouteProcessorChange)
483 {
484         cleanup_view();
485
486         _processor.reset ((Processor*) 0);
487
488         //update_title();
489 }
490
491 void
492 RouteParams_UI::show_track_menu()
493 {
494         using namespace Menu_Helpers;
495
496         if (track_menu == 0) {
497                 track_menu = new Menu;
498                 track_menu->set_name ("ArdourContextMenu");
499                 track_menu->items().push_back
500                                 (MenuElem (_("Add Track or Bus"),
501                                            sigc::bind (sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route), (Gtk::Window*) 0)));
502         }
503         track_menu->popup (1, gtk_get_current_event_time());
504 }
505
506 void
507 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
508 {
509         boost::shared_ptr<Send> send;
510         boost::shared_ptr<Return> retrn;
511         boost::shared_ptr<PluginInsert> plugin_insert;
512         boost::shared_ptr<PortInsert> port_insert;
513
514         if ((boost::dynamic_pointer_cast<InternalSend> (proc)) != 0) {
515                 cleanup_view();
516                 _processor.reset ((Processor*) 0);
517                 update_title();
518                 return;
519         } else if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
520
521                 SendUI *send_ui = new SendUI (this, send, _session);
522
523                 cleanup_view();
524                 send->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
525                 _active_view = send_ui;
526
527                 redir_hpane.add2 (*_active_view);
528                 redir_hpane.show_all();
529
530         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
531
532                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
533
534                 cleanup_view();
535                 retrn->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
536                 _active_view = return_ui;
537
538                 redir_hpane.add2 (*_active_view);
539                 redir_hpane.show_all();
540
541         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
542
543                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
544
545                 cleanup_view();
546                 plugin_insert->plugin()->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader), gui_context());
547                 plugin_ui->start_updating (0);
548                 _active_view = plugin_ui;
549
550                 redir_hpane.pack2 (*_active_view);
551                 redir_hpane.show_all();
552
553         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
554
555                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
556
557                 cleanup_view();
558                 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());
559                 _active_view = portinsert_ui;
560
561                 redir_hpane.pack2 (*_active_view);
562                 portinsert_ui->redisplay();
563                 redir_hpane.show_all();
564         }
565
566         _processor = proc;
567         update_title();
568
569 }
570
571 void
572 RouteParams_UI::plugin_going_away (Placement place)
573 {
574         ENSURE_GUI_THREAD (*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::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
586 {
587         boost::shared_ptr<Processor> proc = (wproc.lock());
588
589         if (!proc) {
590                 return;
591         }
592
593         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
594
595         printf ("redirect going away\n");
596         // delete the current view without calling finish
597         if (proc == _processor) {
598                 cleanup_view (false);
599                 _processor.reset ((Processor*) 0);
600         }
601 }
602
603 void
604 RouteParams_UI::update_title ()
605 {
606         WindowTitle title (_("Tracks and Busses"));
607
608         if (_route) {
609                 title_label.set_text(_route->name());
610                 title += _route->name();
611                 set_title(title.get_string());
612         } else {
613                 title_label.set_text(_("No Track or Bus Selected"));
614                 title += _("No Track or Bus Selected");
615                 set_title(title.get_string());
616         }
617 }
618
619 void
620 RouteParams_UI::start_updating ()
621 {
622         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
623                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
624 }
625
626 void
627 RouteParams_UI::stop_updating ()
628 {
629         update_connection.disconnect();
630 }
631
632 void
633 RouteParams_UI::update_views ()
634 {
635         SendUI *sui;
636         // TODO: only do it if correct tab is showing
637
638         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
639                 sui->update ();
640         }
641 }