a52cf79f3fd44fc2b3ab550b679013ae09d9ca28
[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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <pbd/lockmonitor.h>
24 #include <gtkmm2ext/utils.h>
25 #include <gtkmm2ext/stop_signal.h>
26
27 #include <ardour/session.h>
28 #include <ardour/session_route.h>
29 #include <ardour/diskstream.h>
30 #include <ardour/plugin.h>
31 #include <ardour/plugin_manager.h>
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/route.h>
35 #include <ardour/audio_track.h>
36 #include <ardour/send.h>
37 #include <ardour/insert.h>
38 #include <ardour/connection.h>
39 #include <ardour/session_connection.h>
40
41 #include "route_params_ui.h"
42 #include "keyboard.h"
43 #include "mixer_strip.h"
44 #include "plugin_selector.h"
45 #include "ardour_ui.h"
46 #include "plugin_ui.h"
47 #include "io_selector.h"
48 #include "send_ui.h"
49 #include "utils.h"
50 #include "gui_thread.h"
51
52 #include "i18n.h"
53
54 using namespace ARDOUR;
55 using namespace Gtk;
56 using namespace sigc;
57
58 RouteParams_UI::RouteParams_UI (AudioEngine& eng)
59         : ArdourDialog ("track/bus inspector"),
60           engine (eng),
61           _route(0), 
62           track_menu(0)
63 {
64         pre_redirect_box = 0;
65         post_redirect_box = 0;
66         _route = 0;
67         _pre_redirect = 0;
68         _post_redirect = 0;
69         _input_iosel = 0;
70         _output_iosel = 0;
71         _active_pre_view = 0;
72         _active_post_view = 0;
73         
74         using namespace Notebook_Helpers;
75
76         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
77         output_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/Buses"), 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
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 (pre_redir_hpane, _("Pre-fader Redirects")));
109         notebook.pages().push_back (TabElem (post_redir_hpane, _("Post-fader Redirects")));
110
111         notebook.set_name ("InspectorNotebook");
112         
113         title_label.set_name ("RouteParamsTitleLabel");
114         update_title();
115         
116         // changeable area
117         route_param_frame.set_name("RouteParamsBaseFrame");
118         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
119         
120         
121         route_hpacker.pack_start (notebook, true, true);
122         
123         
124         route_vpacker.pack_start (title_label, false, false);
125         route_vpacker.pack_start (route_hpacker, true, true);
126
127         
128         list_hpane.pack1 (list_vpacker);
129         list_hpane.add2 (route_vpacker);
130
131         list_hpane.set_position(110);
132
133         pre_redir_hpane.set_position(110);
134         post_redir_hpane.set_position(110);
135         
136         //global_vpacker.pack_start (list_hpane, true, true);
137         //get_vbox()->pack_start (global_vpacker);
138         get_vbox()->pack_start (list_hpane);
139         
140         
141         set_name ("RouteParamsWindow");
142         set_default_size (620,370);
143         set_title (_("ardour: track/bus inspector"));
144         set_wmclass (_("ardour_route_parameters"), "Ardour");
145
146         // events
147         route_display.get_selection()->signal_changed().connect(mem_fun(*this, &RouteParams_UI::route_selected));
148         route_display.get_column(0)->signal_clicked().connect(mem_fun(*this, &RouteParams_UI::show_track_menu));
149
150         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
151         
152         _plugin_selector = new PluginSelector (PluginManager::the_manager());
153         _plugin_selector->signal_delete_event().connect (bind (ptr_fun (just_hide_it), 
154                                                      static_cast<Window *> (_plugin_selector)));
155
156
157         signal_delete_event().connect(bind(ptr_fun(just_hide_it), static_cast<Gtk::Window *>(this)));
158 }
159
160 RouteParams_UI::~RouteParams_UI ()
161 {
162 }
163
164 void
165 RouteParams_UI::add_route (Route* route)
166 {
167         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::add_route), route));
168         
169         if (route->hidden()) {
170                 return;
171         }
172
173         TreeModel::Row row = *(route_display_model->append());
174         row[route_display_columns.text] = route->name();
175         row[route_display_columns.route] = route;
176
177         //route_select_list.rows().back().select ();
178         
179         route->name_changed.connect (bind (mem_fun(*this, &RouteParams_UI::route_name_changed), route));
180         route->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
181 }
182
183
184 void
185 RouteParams_UI::route_name_changed (void *src, Route *route)
186 {
187         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::route_name_changed), src, route));
188
189         bool found = false ;
190         TreeModel::Children rows = route_display_model->children();
191         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
192                 if((*iter)[route_display_columns.route] == route) {
193                         (*iter)[route_display_columns.text] = route->name() ;
194                         found = true ;
195                         break;
196                 }
197         }
198
199         if(!found)
200         {
201                 error << _("route display list item for renamed route not found!") << endmsg;
202         }
203
204         if (route == _route) {
205                 track_input_label.set_text (route->name());
206                 update_title();
207         }
208 }
209
210 void
211 RouteParams_UI::setup_redirect_boxes()
212 {
213         if (session && _route) {
214
215                 // just in case... shouldn't need this
216                 cleanup_redirect_boxes();
217                 
218                 // construct new redirect boxes
219                 pre_redirect_box = new RedirectBox(PreFader, *session, *_route, *_plugin_selector, _rr_selection);
220                 post_redirect_box = new RedirectBox(PostFader, *session, *_route, *_plugin_selector, _rr_selection);
221
222                 pre_redir_hpane.pack1 (*pre_redirect_box);
223                 post_redir_hpane.pack1 (*post_redirect_box);
224
225                 pre_redirect_box->RedirectSelected.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_selected), PreFader));
226                 pre_redirect_box->RedirectUnselected.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_selected), PreFader));
227                 post_redirect_box->RedirectSelected.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_selected), PostFader));
228                 post_redirect_box->RedirectUnselected.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_selected), PostFader));
229
230                 pre_redir_hpane.show_all();
231                 post_redir_hpane.show_all();
232         }
233         
234 }
235
236 void
237 RouteParams_UI::cleanup_redirect_boxes()
238 {
239         if (pre_redirect_box) {
240                 pre_redir_hpane.remove(*pre_redirect_box);
241                 delete pre_redirect_box;
242                 pre_redirect_box = 0;
243         }
244
245         if (post_redirect_box) {
246                 post_redir_hpane.remove(*post_redirect_box);
247                 delete post_redirect_box;
248                 post_redirect_box = 0;
249         }
250 }
251
252 void
253 RouteParams_UI::setup_io_frames()
254 {
255         cleanup_io_frames();
256         
257         // input
258         _input_iosel = new IOSelector (*session, *_route, true);
259         _input_iosel->redisplay ();
260         input_frame.add (*_input_iosel);
261         input_frame.show_all();
262         
263         // output
264         _output_iosel = new IOSelector (*session, *_route, false);
265         _output_iosel->redisplay ();
266         output_frame.add (*_output_iosel);
267         output_frame.show_all();
268 }
269
270 void
271 RouteParams_UI::cleanup_io_frames()
272 {
273         if (_input_iosel) {
274                 _input_iosel->Finished (IOSelector::Cancelled);
275                 input_frame.remove();
276                 delete _input_iosel;
277                 _input_iosel = 0;
278         }
279
280         if (_output_iosel) {
281                 _output_iosel->Finished (IOSelector::Cancelled);
282
283                 output_frame.remove();
284                 delete _output_iosel;
285                 _output_iosel = 0;
286         }
287 }
288
289 void
290 RouteParams_UI::cleanup_pre_view (bool stopupdate)
291 {
292         if (_active_pre_view) {
293                 PluginUI *   plugui = 0;
294                 
295                 if (stopupdate && (plugui = dynamic_cast<PluginUI*>(_active_pre_view)) != 0) {
296                           plugui->stop_updating (0);
297                 }
298
299                 _pre_plugin_conn.disconnect();
300                 pre_redir_hpane.remove(*_active_pre_view);
301                 delete _active_pre_view;
302                 _active_pre_view = 0;
303         }
304 }
305
306 void
307 RouteParams_UI::cleanup_post_view (bool stopupdate)
308 {
309         if (_active_post_view) {
310                 PluginUI *   plugui = 0;
311                 
312                 if (stopupdate && (plugui = dynamic_cast<PluginUI*>(_active_post_view)) != 0) {
313                           plugui->stop_updating (0);
314                 }
315                 _post_plugin_conn.disconnect();
316                 post_redir_hpane.remove(*_active_post_view);
317                 delete _active_post_view;
318                 _active_post_view = 0;
319         }
320 }
321
322
323 void
324 RouteParams_UI::route_removed (Route *route)
325 {
326         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
327         /*
328         route_select_list.freeze ();
329         route_select_list.clear ();
330         session->foreach_route (this, &RouteParams_UI::add_route);
331         route_select_list.thaw ();
332         */
333
334         TreeModel::Children rows = route_display_model->children();
335         TreeModel::Children::iterator ri;
336
337         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
338                 if((*iter)[route_display_columns.route] == route) {
339                         route_display_model->erase(iter);
340                         break;
341                 }
342         }
343
344         if (route == _route)
345         {
346                 cleanup_io_frames();
347                 cleanup_pre_view();
348                 cleanup_post_view();
349                 cleanup_redirect_boxes();
350                 
351                 _route = 0;
352                 _pre_redirect = 0;
353                 _post_redirect = 0;
354                 update_title();
355         }
356 }
357
358 void
359 RouteParams_UI::set_session (Session *sess)
360 {
361         ArdourDialog::set_session (sess);
362
363         route_display_model->clear();
364
365         if (session) {
366                 session->foreach_route (this, &RouteParams_UI::add_route);
367                 session->going_away.connect (mem_fun(*this, &ArdourDialog::session_gone));
368                 session->RouteAdded.connect (mem_fun(*this, &RouteParams_UI::add_route));
369                 start_updating ();
370         } else {
371                 stop_updating ();
372         }
373
374         //route_select_list.thaw ();
375
376         _plugin_selector->set_session (session);
377 }       
378
379
380 void
381 RouteParams_UI::session_gone ()
382 {
383         ENSURE_GUI_THREAD(mem_fun(*this, &RouteParams_UI::session_gone));
384
385         route_display_model->clear();
386
387         cleanup_io_frames();
388         cleanup_pre_view();
389         cleanup_post_view();
390         cleanup_redirect_boxes();
391
392         _route = 0;
393         _pre_redirect = 0;
394         _post_redirect = 0;
395         update_title();
396
397         ArdourDialog::session_gone();
398
399 }
400
401 void
402 RouteParams_UI::route_selected()
403 {
404         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
405         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
406         if(iter) {
407                 //If anything is selected
408                 Route* route = (*iter)[route_display_columns.route] ;
409
410                 if (_route == route) {
411                         // do nothing
412                         return;
413                 }
414
415                 // remove event binding from previously selected
416                 if (_route) {
417                         _route_conn.disconnect();
418                         _route_ds_conn.disconnect();
419                         cleanup_redirect_boxes();
420                         cleanup_pre_view();
421                         cleanup_post_view();
422                         cleanup_io_frames();
423                 }
424
425                 // update the other panes with the correct info
426                 _route = route;
427                 //update_routeinfo (route);
428
429                 setup_io_frames();
430                 setup_redirect_boxes();
431
432                 // bind to redirects changed event for this route
433                 _route_conn = route->redirects_changed.connect (mem_fun(*this, &RouteParams_UI::redirects_changed));
434
435                 track_input_label.set_text (_route->name());
436
437                 update_title();
438         } else {
439                 // no selection
440                 if (_route) {
441                         _route_conn.disconnect();
442
443                         // remove from view
444                         cleanup_io_frames();
445                         cleanup_pre_view();
446                         cleanup_post_view();
447                         cleanup_redirect_boxes();
448
449                         _route = 0;
450                         _pre_redirect = 0;
451                         _post_redirect = 0;
452                         track_input_label.set_text(_("NO TRACK"));
453                         update_title();
454                 }
455         }
456 }
457
458 //void
459 //RouteParams_UI::route_unselected (gint row, gint col, GdkEvent *ev)
460 //{
461 //      if (_route) {
462 //              _route_conn.disconnect();
463
464                 // remove from view
465 //              cleanup_io_frames();
466 //              cleanup_pre_view();
467 //              cleanup_post_view();
468 //              cleanup_redirect_boxes();
469                 
470 //              _route = 0;
471 //              _pre_redirect = 0;
472 //              _post_redirect = 0;
473 //              track_input_label.set_text(_("NO TRACK"));
474 //              update_title();
475 //      }
476 //}
477
478 void
479 RouteParams_UI::redirects_changed (void *src)
480
481 {
482         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::redirects_changed), src));
483         
484 //      pre_redirect_list.freeze ();
485 //      pre_redirect_list.clear ();
486 //      post_redirect_list.freeze ();
487 //      post_redirect_list.clear ();
488 //      if (_route) {
489 //              _route->foreach_redirect (this, &RouteParams_UI::add_redirect_to_display);
490 //      }
491 //      pre_redirect_list.thaw ();
492 //      post_redirect_list.thaw ();
493
494         cleanup_pre_view();
495         cleanup_post_view();
496         
497         _pre_redirect = 0;
498         _post_redirect = 0;
499         //update_title();
500 }
501
502
503
504 void
505 RouteParams_UI::show_track_menu()
506 {
507         using namespace Menu_Helpers;
508         
509         if (track_menu == 0) {
510                 track_menu = new Menu;
511                 track_menu->set_name ("ArdourContextMenu");
512                 track_menu->items().push_back 
513                                 (MenuElem (_("Add Track/Bus"), 
514                                            mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route)));
515         }
516         track_menu->popup (1, 0);
517 }
518
519
520
521 void
522 RouteParams_UI::redirect_selected (ARDOUR::Redirect *redirect, ARDOUR::Placement place)
523 {
524         Insert *insert;
525
526         if ((place == PreFader && _pre_redirect == redirect)
527             || (place == PostFader && _post_redirect == redirect)){
528                 return;
529         }
530         
531         if ((insert = dynamic_cast<Insert *> (redirect)) == 0) {
532
533                 Send *send;
534
535                 if ((send = dynamic_cast<Send *> (redirect)) != 0) {
536
537                         /* its a send */
538
539                         SendUI *send_ui = new SendUI (*send, *session);
540
541                         if (place == PreFader) {
542                                 cleanup_pre_view();
543                                 _pre_plugin_conn = send->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
544                                 _active_pre_view = send_ui;
545                                 
546                                 pre_redir_hpane.add2 (*_active_pre_view);
547                                 pre_redir_hpane.show_all();
548                         }
549                         else {
550                                 cleanup_post_view();
551                                 _post_plugin_conn = send->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
552                                 _active_post_view = send_ui;
553                                 
554                                 post_redir_hpane.add2 (*_active_post_view);
555                                 post_redir_hpane.show_all();
556                         }
557                 }
558
559         } else {
560                 /* its an insert, though we don't know what kind yet. */
561
562                 PluginInsert *plugin_insert;
563                 PortInsert *port_insert;
564                                 
565                 if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {                             
566
567                         PluginUI *plugin_ui = new PluginUI (session->engine(), *plugin_insert, true);
568
569                         if (place == PreFader) {
570                                 cleanup_pre_view();
571                                 _pre_plugin_conn = plugin_insert->plugin().GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), PreFader));
572                                 plugin_ui->start_updating (0);
573                                 _active_pre_view = plugin_ui;
574                                 pre_redir_hpane.pack2 (*_active_pre_view);
575                                 pre_redir_hpane.show_all();
576                         }
577                         else {
578                                 cleanup_post_view();
579                                 _post_plugin_conn = plugin_insert->plugin().GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), PostFader));
580                                 plugin_ui->start_updating (0);
581                                 _active_post_view = plugin_ui;
582                                 post_redir_hpane.pack2 (*_active_post_view);
583                                 post_redir_hpane.show_all();
584                         }
585
586                 } else if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
587
588                         PortInsertUI *portinsert_ui = new PortInsertUI (*session, *port_insert);
589                                         
590                         if (place == PreFader) {
591                                 cleanup_pre_view();
592                                 _pre_plugin_conn = port_insert->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
593                                 _active_pre_view = portinsert_ui;
594                                 pre_redir_hpane.pack2 (*_active_pre_view);
595                                 portinsert_ui->redisplay();
596                                 pre_redir_hpane.show_all();
597                         }
598                         else {
599                                 cleanup_post_view();
600                                 _post_plugin_conn = port_insert->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
601                                 _active_post_view = portinsert_ui;
602                                 post_redir_hpane.pack2 (*_active_post_view);
603                                 portinsert_ui->redisplay();
604                                 post_redir_hpane.show_all();
605                         }
606                 }
607                                 
608         }
609
610         if (place == PreFader) {
611                 _pre_redirect = redirect;
612         }
613         else {
614                 _post_redirect = redirect;
615         }
616         
617         update_title();
618                 
619 }
620
621 void
622 RouteParams_UI::redirect_unselected (ARDOUR::Redirect *redirect)
623 {
624         // not called anymore
625         
626         if (redirect == _pre_redirect) {
627                 cleanup_pre_view();
628                 _pre_redirect = 0;
629         }
630         else if (redirect == _post_redirect) {
631                 cleanup_post_view();
632                 _post_redirect = 0;
633         }
634 }
635
636
637
638 void
639 RouteParams_UI::plugin_going_away (Plugin *plugin, Placement place)
640 {
641         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), plugin, place));
642         
643         // delete the current view without calling finish
644
645         if (place == PreFader) {
646                 cleanup_pre_view (false);
647                 _pre_redirect = 0;
648         }
649         else {
650                 cleanup_post_view (false);
651                 _post_redirect = 0;
652         }
653 }
654
655 void
656 RouteParams_UI::redirect_going_away (ARDOUR::Redirect *plugin)
657
658 {
659         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::redirect_going_away), plugin));
660         
661         printf ("redirect going away\n");
662         // delete the current view without calling finish
663         if (plugin == _pre_redirect) {
664                 cleanup_pre_view (false);
665                 _pre_redirect = 0;
666         }
667         else if (plugin == _post_redirect) {
668                 cleanup_post_view (false);
669                 _post_redirect = 0;
670         }
671 }
672
673
674 void
675 RouteParams_UI::update_title ()
676 {
677         if (_route) {
678                 string title;
679                 title += _route->name();
680 //              title += ": ";
681
682 //              if (_redirect && (_current_view == PLUGIN_CONFIG_VIEW || _current_view == SEND_CONFIG_VIEW)) {
683 //                      title += _redirect->name();
684 //              }
685 //              else if (_current_view == INPUT_CONFIG_VIEW) {
686 //                      title += _("INPUT");
687 //              }
688 //              else if (_current_view == OUTPUT_CONFIG_VIEW) {
689 //                      title += _("OUTPUT");
690 //              }
691                 
692                 title_label.set_text(title);
693
694                 title = _("ardour: track/bus inspector: ") + title;
695                 set_title(title);
696         }
697         else {
698                 title_label.set_text(_("No Route Selected"));
699                 set_title(_("ardour: track/bus/inspector: no route selected"));
700         }       
701 }
702
703
704 void
705 RouteParams_UI::start_updating ()
706 {
707         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
708                 (mem_fun(*this, &RouteParams_UI::update_views));
709 }
710
711 void
712 RouteParams_UI::stop_updating ()
713 {
714         update_connection.disconnect();
715 }
716
717 void
718 RouteParams_UI::update_views ()
719 {
720         SendUI *sui;
721         // TODO: only do it if correct tab is showing
722         
723         if ((sui = dynamic_cast<SendUI*> (_active_pre_view)) != 0) {
724                 sui->update ();
725         }
726         if ((sui = dynamic_cast<SendUI*> (_active_post_view)) != 0) {
727                 sui->update ();
728         }
729
730 }