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