use new syntax for connecting to backend signals that enforces explicit connection...
[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/stop_signal.h>
27 #include <gtkmm2ext/window_title.h>
28
29 #include "ardour/ardour.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/audio_track.h"
32 #include "ardour/plugin.h"
33 #include "ardour/plugin_insert.h"
34 #include "ardour/plugin_manager.h"
35 #include "ardour/port_insert.h"
36 #include "ardour/return.h"
37 #include "ardour/route.h"
38 #include "ardour/send.h"
39 #include "ardour/session.h"
40 #include "ardour/session.h"
41 #include "ardour/session_route.h"
42
43 #include "ardour_ui.h"
44 #include "gui_thread.h"
45 #include "io_selector.h"
46 #include "keyboard.h"
47 #include "mixer_strip.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 ("track/bus inspector"),
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         route_select_scroller.add(route_display);
97         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
98
99         route_select_frame.set_name("RouteSelectBaseFrame");
100         route_select_frame.set_shadow_type (Gtk::SHADOW_IN);
101         route_select_frame.add(route_select_scroller);
102
103         list_vpacker.pack_start (route_select_frame, true, true);
104
105         notebook.pages().push_back (TabElem (input_frame, _("Inputs")));
106         notebook.pages().push_back (TabElem (output_frame, _("Outputs")));
107         notebook.pages().push_back (TabElem (redir_hpane, _("Plugins, Inserts & Sends")));
108         notebook.pages().push_back (TabElem (latency_frame, _("Latency")));
109
110         notebook.set_name ("InspectorNotebook");
111
112         title_label.set_name ("RouteParamsTitleLabel");
113         update_title();
114
115         latency_packer.set_spacing (18);
116         latency_button_box.pack_start (latency_apply_button);
117         delay_label.set_alignment (0, 0.5);
118
119         // changeable area
120         route_param_frame.set_name("RouteParamsBaseFrame");
121         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
122
123
124         route_hpacker.pack_start (notebook, true, true);
125
126         route_vpacker.pack_start (title_label, false, false);
127         route_vpacker.pack_start (route_hpacker, true, true);
128
129
130         list_hpane.pack1 (list_vpacker);
131         list_hpane.add2 (route_vpacker);
132
133         list_hpane.set_position(110);
134
135         redir_hpane.set_position(110);
136
137         //global_vpacker.pack_start (list_hpane, true, true);
138         //get_vbox()->pack_start (global_vpacker);
139         get_vbox()->pack_start (list_hpane);
140
141
142         set_name ("RouteParamsWindow");
143         set_default_size (620,370);
144         set_wmclass (X_("ardour_route_parameters"), "Ardour");
145
146         set_title (_("Track/Bus Inspector"));
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->NameChanged.connect (*this, boost::bind (&RouteParams_UI::route_name_changed, this, boost::weak_ptr<Route>(route)));
185                 route->GoingAway.connect (*this, boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)));
186         }
187 }
188
189
190 void
191 RouteParams_UI::route_name_changed (boost::weak_ptr<Route> wr)
192 {
193         boost::shared_ptr<Route> route (wr.lock());
194
195         if (!route) { 
196                 return;
197         }
198
199         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
200
201         bool found = false ;
202         TreeModel::Children rows = route_display_model->children();
203         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
204                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
205                 if (r == route) {
206                         (*iter)[route_display_columns.text] = route->name() ;
207                         found = true ;
208                         break;
209                 }
210         }
211
212         if(!found) {
213                 error << _("route display list item for renamed route not found!") << endmsg;
214         }
215
216         if (route == _route) {
217                 track_input_label.set_text (route->name());
218                 update_title();
219         }
220 }
221
222 void
223 RouteParams_UI::setup_processor_boxes()
224 {
225         if (_session && _route) {
226
227                 // just in case... shouldn't need this
228                 cleanup_processor_boxes();
229
230                 // construct new redirect boxes
231                 insert_box = new ProcessorBox (_session, boost::bind (&RouteParams_UI::plugin_selector, this), _rr_selection, 0);
232                 insert_box->set_route (_route);
233
234                 redir_hpane.pack1 (*insert_box);
235
236                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
237                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
238
239                 redir_hpane.show_all();
240         }
241 }
242
243 void
244 RouteParams_UI::cleanup_processor_boxes()
245 {
246         if (insert_box) {
247                 redir_hpane.remove(*insert_box);
248                 delete insert_box;
249                 insert_box = 0;
250         }
251 }
252
253 void
254 RouteParams_UI::refresh_latency ()
255 {
256         if (latency_widget) {
257                 latency_widget->refresh();
258
259                 char buf[128];
260                 snprintf (buf, sizeof (buf), _("Playback delay: %u samples"), _route->initial_delay());
261                 delay_label.set_text (buf);
262         }
263 }
264
265 void
266 RouteParams_UI::cleanup_latency_frame ()
267 {
268         if (latency_widget) {
269                 latency_frame.remove ();
270                 latency_packer.remove (*latency_widget);
271                 latency_packer.remove (latency_button_box);
272                 latency_packer.remove (delay_label);
273                 latency_connections.drop_connections ();
274                 latency_click_connection.disconnect ();
275
276                 delete latency_widget;
277                 latency_widget = 0;
278                 
279         }
280 }
281
282 void
283 RouteParams_UI::setup_latency_frame ()
284 {
285         latency_widget = new LatencyGUI (*(_route->output()), _session->frame_rate(), _session->engine().frames_per_cycle());
286
287         char buf[128];
288         snprintf (buf, sizeof (buf), _("Playback delay: %u samples"), _route->initial_delay());
289         delay_label.set_text (buf);
290
291         latency_packer.pack_start (*latency_widget, false, false);
292         latency_packer.pack_start (latency_button_box, false, false);
293         latency_packer.pack_start (delay_label);
294
295         latency_click_connection = latency_apply_button.signal_clicked().connect (sigc::mem_fun (*latency_widget, &LatencyGUI::finish));
296         _route->signal_latency_changed.connect (latency_connections, sigc::mem_fun (*this, &RouteParams_UI::refresh_latency));
297         _route->initial_delay_changed.connect (latency_connections, sigc::mem_fun (*this, &RouteParams_UI::refresh_latency));
298
299         latency_frame.add (latency_packer);
300         latency_frame.show_all ();
301 }
302
303 void
304 RouteParams_UI::setup_io_frames()
305 {
306         cleanup_io_frames();
307
308         // input
309         _input_iosel = new IOSelector (this, _session, _route->input());
310         _input_iosel->setup ();
311         input_frame.add (*_input_iosel);
312         input_frame.show_all();
313
314         // output
315         _output_iosel = new IOSelector (this, _session, _route->output());
316         _output_iosel->setup ();
317         output_frame.add (*_output_iosel);
318         output_frame.show_all();
319 }
320
321 void
322 RouteParams_UI::cleanup_io_frames()
323 {
324         if (_input_iosel) {
325                 _input_iosel->Finished (IOSelector::Cancelled);
326                 input_frame.remove();
327                 delete _input_iosel;
328                 _input_iosel = 0;
329         }
330
331         if (_output_iosel) {
332                 _output_iosel->Finished (IOSelector::Cancelled);
333
334                 output_frame.remove();
335                 delete _output_iosel;
336                 _output_iosel = 0;
337         }
338 }
339
340 void
341 RouteParams_UI::cleanup_view (bool stopupdate)
342 {
343         if (_active_view) {
344                 GenericPluginUI *   plugui = 0;
345
346                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
347                           plugui->stop_updating (0);
348                 }
349
350                 _processor_going_away_connection.disconnect ();
351                 redir_hpane.remove(*_active_view);
352                 delete _active_view;
353                 _active_view = 0;
354         }
355 }
356
357 void
358 RouteParams_UI::route_removed (boost::weak_ptr<Route> wr)
359 {
360         boost::shared_ptr<Route> route (wr.lock());
361
362         if (!route) { 
363                 return;
364         }
365
366         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_removed, wr)
367
368         TreeModel::Children rows = route_display_model->children();
369         TreeModel::Children::iterator ri;
370
371         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
372                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
373
374                 if (r == route) {
375                         route_display_model->erase(iter);
376                         break;
377                 }
378         }
379
380         if (route == _route) {
381                 cleanup_io_frames();
382                 cleanup_view();
383                 cleanup_processor_boxes();
384
385                 _route.reset ((Route*) 0);
386                 _processor.reset ((Processor*) 0);
387                 update_title();
388         }
389 }
390
391 void
392 RouteParams_UI::set_session (Session *sess)
393 {
394         ArdourDialog::set_session (sess);
395         
396         route_display_model->clear();
397         _plugin_selector->set_session (_session);
398
399         if (_session) {
400                 boost::shared_ptr<RouteList> r = _session->get_routes();
401                 add_routes (*r);
402                 _session->RouteAdded.connect (_session_connections, sigc::mem_fun(*this, &RouteParams_UI::add_routes));
403                 start_updating ();
404         } else {
405                 stop_updating ();
406         }
407 }
408
409
410 void
411 RouteParams_UI::session_going_away ()
412 {
413         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
414
415         SessionHandlePtr::session_going_away ();
416
417         route_display_model->clear();
418
419         cleanup_io_frames();
420         cleanup_view();
421         cleanup_processor_boxes();
422         cleanup_latency_frame ();
423
424         _route.reset ((Route*) 0);
425         _processor.reset ((Processor*) 0);
426         update_title();
427 }
428
429 void
430 RouteParams_UI::route_selected()
431 {
432         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
433         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
434
435         if(iter) {
436                 //If anything is selected
437                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
438
439                 if (_route == route) {
440                         // do nothing
441                         return;
442                 }
443
444                 // remove event binding from previously selected
445                 if (_route) {
446                         _route_processors_connection.disconnect ();
447                         cleanup_processor_boxes();
448                         cleanup_view();
449                         cleanup_io_frames();
450                         cleanup_latency_frame ();
451                 }
452
453                 // update the other panes with the correct info
454                 _route = route;
455                 //update_routeinfo (route);
456
457                 setup_io_frames();
458                 setup_processor_boxes();
459                 setup_latency_frame ();
460
461                 route->processors_changed.connect (_route_processors_connection, boost::bind (&RouteParams_UI::processors_changed, this, _1));
462                 
463                 track_input_label.set_text (_route->name());
464
465                 update_title();
466
467         } else {
468                 // no selection
469                 if (_route) {
470                         _route_processors_connection.disconnect ();
471
472                         // remove from view
473                         cleanup_io_frames();
474                         cleanup_view();
475                         cleanup_processor_boxes();
476                         cleanup_latency_frame ();
477
478                         _route.reset ((Route*) 0);
479                         _processor.reset ((Processor*) 0);
480                         track_input_label.set_text(_("NO TRACK"));
481                         update_title();
482                 }
483         }
484 }
485
486 void
487 RouteParams_UI::processors_changed (RouteProcessorChange c)
488 {
489         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processors_changed, c)
490         cleanup_view();
491
492         _processor.reset ((Processor*) 0);
493
494         //update_title();
495 }
496
497 void
498 RouteParams_UI::show_track_menu()
499 {
500         using namespace Menu_Helpers;
501
502         if (track_menu == 0) {
503                 track_menu = new Menu;
504                 track_menu->set_name ("ArdourContextMenu");
505                 track_menu->items().push_back
506                                 (MenuElem (_("Add Track/Bus"),
507                                            sigc::bind (sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route), (Gtk::Window*) 0)));
508         }
509         track_menu->popup (1, gtk_get_current_event_time());
510 }
511
512 void
513 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
514 {
515         boost::shared_ptr<Send> send;
516         boost::shared_ptr<Return> retrn;
517         boost::shared_ptr<PluginInsert> plugin_insert;
518         boost::shared_ptr<PortInsert> port_insert;
519
520         if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
521
522                 SendUI *send_ui = new SendUI (this, send, _session);
523
524                 cleanup_view();
525                 send->GoingAway.connect (_processor_going_away_connection, boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)));
526                 _active_view = send_ui;
527
528                 redir_hpane.add2 (*_active_view);
529                 redir_hpane.show_all();
530
531         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
532
533                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
534
535                 cleanup_view();
536                 retrn->GoingAway.connect (_processor_going_away_connection, boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)));
537                 _active_view = return_ui;
538
539                 redir_hpane.add2 (*_active_view);
540                 redir_hpane.show_all();
541
542         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
543
544                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
545
546                 cleanup_view();
547                 plugin_insert->plugin()->GoingAway.connect (_processor_going_away_connection, boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader));
548                 plugin_ui->start_updating (0);
549                 _active_view = plugin_ui;
550
551                 redir_hpane.pack2 (*_active_view);
552                 redir_hpane.show_all();
553
554         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
555
556                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
557
558                 cleanup_view();
559                 port_insert->GoingAway.connect (_processor_going_away_connection, boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor> (proc)));
560                 _active_view = portinsert_ui;
561
562                 redir_hpane.pack2 (*_active_view);
563                 portinsert_ui->redisplay();
564                 redir_hpane.show_all();
565         }
566
567         _processor = proc;
568         update_title();
569
570 }
571
572 void
573 RouteParams_UI::plugin_going_away (Placement place)
574 {
575         ENSURE_GUI_THREAD (*this, &RouteParams_UI::plugin_going_away, place)
576
577         // delete the current view without calling finish
578
579         if (place == PreFader) {
580                 cleanup_view (false);
581                 _processor.reset ((Processor*) 0);
582         }
583 }
584
585 void
586 RouteParams_UI::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
587 {
588         boost::shared_ptr<Processor> proc = (wproc.lock());
589
590         if (!proc) {
591                 return;
592         }
593
594         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
595
596         printf ("redirect going away\n");
597         // delete the current view without calling finish
598         if (proc == _processor) {
599                 cleanup_view (false);
600                 _processor.reset ((Processor*) 0);
601         }
602 }
603
604 void
605 RouteParams_UI::update_title ()
606 {
607         WindowTitle title(_("Track/Bus Inspector"));
608
609         if (_route) {
610
611                 //              title += ": ";
612
613                 //              if (_redirect && (_current_view == PLUGIN_CONFIG_VIEW || _current_view == SEND_CONFIG_VIEW)) {
614                 //                      title += _redirect->name();
615                 //              }
616                 //              else if (_current_view == INPUT_CONFIG_VIEW) {
617                 //                      title += _("INPUT");
618                 //              }
619                 //              else if (_current_view == OUTPUT_CONFIG_VIEW) {
620                 //                      title += _("OUTPUT");
621                 //              }
622
623                 title_label.set_text(_route->name());
624
625                 title += _route->name();
626
627                 set_title(title.get_string());
628         }
629         else {
630                 title_label.set_text(_("No Route Selected"));
631                 title += _("No Route Selected");
632                 set_title(title.get_string());
633         }
634 }
635
636 void
637 RouteParams_UI::start_updating ()
638 {
639         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
640                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
641 }
642
643 void
644 RouteParams_UI::stop_updating ()
645 {
646         update_connection.disconnect();
647 }
648
649 void
650 RouteParams_UI::update_views ()
651 {
652         SendUI *sui;
653         // TODO: only do it if correct tab is showing
654
655         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
656                 sui->update ();
657         }
658 }