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