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