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