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