Fixed compile warnings.
[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           _route(0), 
63           track_menu(0)
64 {
65         pre_redirect_box = 0;
66         post_redirect_box = 0;
67         _route = 0;
68         _pre_redirect = 0;
69         _post_redirect = 0;
70         _input_iosel = 0;
71         _output_iosel = 0;
72         _active_pre_view = 0;
73         _active_post_view = 0;
74         
75         using namespace Notebook_Helpers;
76
77         input_frame.set_shadow_type(Gtk::SHADOW_NONE);
78         output_frame.set_shadow_type(Gtk::SHADOW_NONE);
79         
80         notebook.set_show_tabs (true);
81         notebook.set_show_border (true);
82         notebook.set_name ("RouteParamNotebook");
83
84         // create the tree model
85         route_display_model = ListStore::create(route_display_columns);
86
87         // setup the treeview
88         route_display.set_model(route_display_model);
89         route_display.append_column(_("Tracks/Buses"), route_display_columns.text);
90         route_display.set_name(X_("RouteParamsListDisplay"));
91         route_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE); // default
92         route_display.set_reorderable(false);
93         route_display.set_size_request(75, -1);
94         route_display.set_headers_visible(true);
95         route_display.set_headers_clickable(true);
96
97         route_select_scroller.add(route_display);
98         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
99
100
101         route_select_frame.set_name("RouteSelectBaseFrame");
102         route_select_frame.set_shadow_type (Gtk::SHADOW_IN);
103         route_select_frame.add(route_select_scroller);
104
105         list_vpacker.pack_start (route_select_frame, true, true);
106         
107         notebook.pages().push_back (TabElem (input_frame, _("Inputs")));
108         notebook.pages().push_back (TabElem (output_frame, _("Outputs")));
109         notebook.pages().push_back (TabElem (pre_redir_hpane, _("Pre-fader Redirects")));
110         notebook.pages().push_back (TabElem (post_redir_hpane, _("Post-fader Redirects")));
111
112         notebook.set_name ("InspectorNotebook");
113         
114         title_label.set_name ("RouteParamsTitleLabel");
115         update_title();
116         
117         // changeable area
118         route_param_frame.set_name("RouteParamsBaseFrame");
119         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
120         
121         
122         route_hpacker.pack_start (notebook, true, true);
123         
124         
125         route_vpacker.pack_start (title_label, false, false);
126         route_vpacker.pack_start (route_hpacker, true, true);
127
128         
129         list_hpane.pack1 (list_vpacker);
130         list_hpane.add2 (route_vpacker);
131
132         list_hpane.set_position(110);
133
134         pre_redir_hpane.set_position(110);
135         post_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_title (_("ardour: track/bus inspector"));
145         set_wmclass (_("ardour_route_parameters"), "Ardour");
146
147         // events
148         route_display.get_selection()->signal_changed().connect(mem_fun(*this, &RouteParams_UI::route_selected));
149         route_display.get_column(0)->signal_clicked().connect(mem_fun(*this, &RouteParams_UI::show_track_menu));
150
151         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
152         
153         _plugin_selector = new PluginSelector (PluginManager::the_manager());
154         _plugin_selector->signal_delete_event().connect (bind (ptr_fun (just_hide_it), 
155                                                      static_cast<Window *> (_plugin_selector)));
156
157
158         signal_delete_event().connect(bind(ptr_fun(just_hide_it), static_cast<Gtk::Window *>(this)));
159 }
160
161 RouteParams_UI::~RouteParams_UI ()
162 {
163 }
164
165 void
166 RouteParams_UI::add_route (Route* route)
167 {
168         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::add_route), route));
169         
170         if (route->hidden()) {
171                 return;
172         }
173
174         TreeModel::Row row = *(route_display_model->append());
175         row[route_display_columns.text] = route->name();
176         row[route_display_columns.route] = route;
177
178         //route_select_list.rows().back().select ();
179         
180         route->name_changed.connect (bind (mem_fun(*this, &RouteParams_UI::route_name_changed), route));
181         route->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
182 }
183
184
185 void
186 RouteParams_UI::route_name_changed (void *src, 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                 if((*iter)[route_display_columns.route] == route) {
194                         (*iter)[route_display_columns.text] = route->name() ;
195                         found = true ;
196                         break;
197                 }
198         }
199
200         if(!found)
201         {
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                 PluginUI *   plugui = 0;
295                 
296                 if (stopupdate && (plugui = dynamic_cast<PluginUI*>(_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                 PluginUI *   plugui = 0;
312                 
313                 if (stopupdate && (plugui = dynamic_cast<PluginUI*>(_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 (Route *route)
326 {
327         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
328         /*
329         route_select_list.freeze ();
330         route_select_list.clear ();
331         session->foreach_route (this, &RouteParams_UI::add_route);
332         route_select_list.thaw ();
333         */
334
335         TreeModel::Children rows = route_display_model->children();
336         TreeModel::Children::iterator ri;
337
338         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
339                 if((*iter)[route_display_columns.route] == route) {
340                         route_display_model->erase(iter);
341                         break;
342                 }
343         }
344
345         if (route == _route)
346         {
347                 cleanup_io_frames();
348                 cleanup_pre_view();
349                 cleanup_post_view();
350                 cleanup_redirect_boxes();
351                 
352                 _route = 0;
353                 _pre_redirect = 0;
354                 _post_redirect = 0;
355                 update_title();
356         }
357 }
358
359 void
360 RouteParams_UI::set_session (Session *sess)
361 {
362         ArdourDialog::set_session (sess);
363
364         route_display_model->clear();
365
366         if (session) {
367                 session->foreach_route (this, &RouteParams_UI::add_route);
368                 session->going_away.connect (mem_fun(*this, &ArdourDialog::session_gone));
369                 session->RouteAdded.connect (mem_fun(*this, &RouteParams_UI::add_route));
370                 start_updating ();
371         } else {
372                 stop_updating ();
373         }
374
375         //route_select_list.thaw ();
376
377         _plugin_selector->set_session (session);
378 }       
379
380
381 void
382 RouteParams_UI::session_gone ()
383 {
384         ENSURE_GUI_THREAD(mem_fun(*this, &RouteParams_UI::session_gone));
385
386         route_display_model->clear();
387
388         cleanup_io_frames();
389         cleanup_pre_view();
390         cleanup_post_view();
391         cleanup_redirect_boxes();
392
393         _route = 0;
394         _pre_redirect = 0;
395         _post_redirect = 0;
396         update_title();
397
398         ArdourDialog::session_gone();
399
400 }
401
402 void
403 RouteParams_UI::route_selected()
404 {
405         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
406         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
407         if(iter) {
408                 //If anything is selected
409                 Route* route = (*iter)[route_display_columns.route] ;
410
411                 if (_route == route) {
412                         // do nothing
413                         return;
414                 }
415
416                 // remove event binding from previously selected
417                 if (_route) {
418                         _route_conn.disconnect();
419                         _route_ds_conn.disconnect();
420                         cleanup_redirect_boxes();
421                         cleanup_pre_view();
422                         cleanup_post_view();
423                         cleanup_io_frames();
424                 }
425
426                 // update the other panes with the correct info
427                 _route = route;
428                 //update_routeinfo (route);
429
430                 setup_io_frames();
431                 setup_redirect_boxes();
432
433                 // bind to redirects changed event for this route
434                 _route_conn = route->redirects_changed.connect (mem_fun(*this, &RouteParams_UI::redirects_changed));
435
436                 track_input_label.set_text (_route->name());
437
438                 update_title();
439         } else {
440                 // no selection
441                 if (_route) {
442                         _route_conn.disconnect();
443
444                         // remove from view
445                         cleanup_io_frames();
446                         cleanup_pre_view();
447                         cleanup_post_view();
448                         cleanup_redirect_boxes();
449
450                         _route = 0;
451                         _pre_redirect = 0;
452                         _post_redirect = 0;
453                         track_input_label.set_text(_("NO TRACK"));
454                         update_title();
455                 }
456         }
457 }
458
459 //void
460 //RouteParams_UI::route_unselected (gint row, gint col, GdkEvent *ev)
461 //{
462 //      if (_route) {
463 //              _route_conn.disconnect();
464
465                 // remove from view
466 //              cleanup_io_frames();
467 //              cleanup_pre_view();
468 //              cleanup_post_view();
469 //              cleanup_redirect_boxes();
470                 
471 //              _route = 0;
472 //              _pre_redirect = 0;
473 //              _post_redirect = 0;
474 //              track_input_label.set_text(_("NO TRACK"));
475 //              update_title();
476 //      }
477 //}
478
479 void
480 RouteParams_UI::redirects_changed (void *src)
481
482 {
483         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::redirects_changed), src));
484         
485 //      pre_redirect_list.freeze ();
486 //      pre_redirect_list.clear ();
487 //      post_redirect_list.freeze ();
488 //      post_redirect_list.clear ();
489 //      if (_route) {
490 //              _route->foreach_redirect (this, &RouteParams_UI::add_redirect_to_display);
491 //      }
492 //      pre_redirect_list.thaw ();
493 //      post_redirect_list.thaw ();
494
495         cleanup_pre_view();
496         cleanup_post_view();
497         
498         _pre_redirect = 0;
499         _post_redirect = 0;
500         //update_title();
501 }
502
503
504
505 void
506 RouteParams_UI::show_track_menu()
507 {
508         using namespace Menu_Helpers;
509         
510         if (track_menu == 0) {
511                 track_menu = new Menu;
512                 track_menu->set_name ("ArdourContextMenu");
513                 track_menu->items().push_back 
514                                 (MenuElem (_("Add Track/Bus"), 
515                                            mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route)));
516         }
517         track_menu->popup (1, 0);
518 }
519
520
521
522 void
523 RouteParams_UI::redirect_selected (ARDOUR::Redirect *redirect, ARDOUR::Placement place)
524 {
525         Insert *insert;
526
527         if ((place == PreFader && _pre_redirect == redirect)
528             || (place == PostFader && _post_redirect == redirect)){
529                 return;
530         }
531         
532         if ((insert = dynamic_cast<Insert *> (redirect)) == 0) {
533
534                 Send *send;
535
536                 if ((send = dynamic_cast<Send *> (redirect)) != 0) {
537
538                         /* its a send */
539
540                         SendUI *send_ui = new SendUI (*send, *session);
541
542                         if (place == PreFader) {
543                                 cleanup_pre_view();
544                                 _pre_plugin_conn = send->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
545                                 _active_pre_view = send_ui;
546                                 
547                                 pre_redir_hpane.add2 (*_active_pre_view);
548                                 pre_redir_hpane.show_all();
549                         }
550                         else {
551                                 cleanup_post_view();
552                                 _post_plugin_conn = send->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
553                                 _active_post_view = send_ui;
554                                 
555                                 post_redir_hpane.add2 (*_active_post_view);
556                                 post_redir_hpane.show_all();
557                         }
558                 }
559
560         } else {
561                 /* its an insert, though we don't know what kind yet. */
562
563                 PluginInsert *plugin_insert;
564                 PortInsert *port_insert;
565                                 
566                 if ((plugin_insert = dynamic_cast<PluginInsert *> (insert)) != 0) {                             
567
568                         PluginUI *plugin_ui = new PluginUI (session->engine(), *plugin_insert, true);
569
570                         if (place == PreFader) {
571                                 cleanup_pre_view();
572                                 _pre_plugin_conn = plugin_insert->plugin().GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), PreFader));
573                                 plugin_ui->start_updating (0);
574                                 _active_pre_view = plugin_ui;
575                                 pre_redir_hpane.pack2 (*_active_pre_view);
576                                 pre_redir_hpane.show_all();
577                         }
578                         else {
579                                 cleanup_post_view();
580                                 _post_plugin_conn = plugin_insert->plugin().GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), PostFader));
581                                 plugin_ui->start_updating (0);
582                                 _active_post_view = plugin_ui;
583                                 post_redir_hpane.pack2 (*_active_post_view);
584                                 post_redir_hpane.show_all();
585                         }
586
587                 } else if ((port_insert = dynamic_cast<PortInsert *> (insert)) != 0) {
588
589                         PortInsertUI *portinsert_ui = new PortInsertUI (*session, *port_insert);
590                                         
591                         if (place == PreFader) {
592                                 cleanup_pre_view();
593                                 _pre_plugin_conn = port_insert->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
594                                 _active_pre_view = portinsert_ui;
595                                 pre_redir_hpane.pack2 (*_active_pre_view);
596                                 portinsert_ui->redisplay();
597                                 pre_redir_hpane.show_all();
598                         }
599                         else {
600                                 cleanup_post_view();
601                                 _post_plugin_conn = port_insert->GoingAway.connect (mem_fun(*this, &RouteParams_UI::redirect_going_away));
602                                 _active_post_view = portinsert_ui;
603                                 post_redir_hpane.pack2 (*_active_post_view);
604                                 portinsert_ui->redisplay();
605                                 post_redir_hpane.show_all();
606                         }
607                 }
608                                 
609         }
610
611         if (place == PreFader) {
612                 _pre_redirect = redirect;
613         }
614         else {
615                 _post_redirect = redirect;
616         }
617         
618         update_title();
619                 
620 }
621
622 void
623 RouteParams_UI::redirect_unselected (ARDOUR::Redirect *redirect)
624 {
625         // not called anymore
626         
627         if (redirect == _pre_redirect) {
628                 cleanup_pre_view();
629                 _pre_redirect = 0;
630         }
631         else if (redirect == _post_redirect) {
632                 cleanup_post_view();
633                 _post_redirect = 0;
634         }
635 }
636
637
638
639 void
640 RouteParams_UI::plugin_going_away (Plugin *plugin, Placement place)
641 {
642         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), plugin, place));
643         
644         // delete the current view without calling finish
645
646         if (place == PreFader) {
647                 cleanup_pre_view (false);
648                 _pre_redirect = 0;
649         }
650         else {
651                 cleanup_post_view (false);
652                 _post_redirect = 0;
653         }
654 }
655
656 void
657 RouteParams_UI::redirect_going_away (ARDOUR::Redirect *plugin)
658
659 {
660         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::redirect_going_away), plugin));
661         
662         printf ("redirect going away\n");
663         // delete the current view without calling finish
664         if (plugin == _pre_redirect) {
665                 cleanup_pre_view (false);
666                 _pre_redirect = 0;
667         }
668         else if (plugin == _post_redirect) {
669                 cleanup_post_view (false);
670                 _post_redirect = 0;
671         }
672 }
673
674
675 void
676 RouteParams_UI::update_title ()
677 {
678         if (_route) {
679                 string title;
680                 title += _route->name();
681 //              title += ": ";
682
683 //              if (_redirect && (_current_view == PLUGIN_CONFIG_VIEW || _current_view == SEND_CONFIG_VIEW)) {
684 //                      title += _redirect->name();
685 //              }
686 //              else if (_current_view == INPUT_CONFIG_VIEW) {
687 //                      title += _("INPUT");
688 //              }
689 //              else if (_current_view == OUTPUT_CONFIG_VIEW) {
690 //                      title += _("OUTPUT");
691 //              }
692                 
693                 title_label.set_text(title);
694
695                 title = _("ardour: track/bus inspector: ") + title;
696                 set_title(title);
697         }
698         else {
699                 title_label.set_text(_("No Route Selected"));
700                 set_title(_("ardour: track/bus/inspector: no route selected"));
701         }       
702 }
703
704
705 void
706 RouteParams_UI::start_updating ()
707 {
708         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
709                 (mem_fun(*this, &RouteParams_UI::update_views));
710 }
711
712 void
713 RouteParams_UI::stop_updating ()
714 {
715         update_connection.disconnect();
716 }
717
718 void
719 RouteParams_UI::update_views ()
720 {
721         SendUI *sui;
722         // TODO: only do it if correct tab is showing
723         
724         if ((sui = dynamic_cast<SendUI*> (_active_pre_view)) != 0) {
725                 sui->update ();
726         }
727         if ((sui = dynamic_cast<SendUI*> (_active_post_view)) != 0) {
728                 sui->update ();
729         }
730
731 }