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