Various fixups to bundle manager. Add a separator between the benign and more seriou...
[ardour.git] / gtk2_ardour / route_params_ui.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <algorithm>
21 #define __STDC_FORMAT_MACROS
22 #include <inttypes.h>
23
24 #include <glibmm/thread.h>
25 #include <gtkmm2ext/utils.h>
26 #include <gtkmm2ext/stop_signal.h>
27 #include <gtkmm2ext/window_title.h>
28
29 #include "ardour/ardour.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/audio_track.h"
32 #include "ardour/plugin.h"
33 #include "ardour/plugin_insert.h"
34 #include "ardour/plugin_manager.h"
35 #include "ardour/port_insert.h"
36 #include "ardour/return.h"
37 #include "ardour/route.h"
38 #include "ardour/send.h"
39 #include "ardour/session.h"
40 #include "ardour/session.h"
41 #include "ardour/session_route.h"
42
43 #include "ardour_ui.h"
44 #include "gui_thread.h"
45 #include "io_selector.h"
46 #include "keyboard.h"
47 #include "mixer_strip.h"
48 #include "plugin_selector.h"
49 #include "plugin_ui.h"
50 #include "return_ui.h"
51 #include "route_params_ui.h"
52 #include "send_ui.h"
53 #include "utils.h"
54
55 #include "i18n.h"
56
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61 using namespace sigc;
62
63 RouteParams_UI::RouteParams_UI ()
64         : ArdourDialog ("track/bus inspector"),
65           latency_apply_button (Stock::APPLY),
66           track_menu(0)
67         
68 {
69         insert_box = 0;
70         _input_iosel = 0;
71         _output_iosel = 0;
72         _active_view = 0;
73         latency_widget = 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         latency_frame.set_shadow_type (Gtk::SHADOW_NONE);
80
81         notebook.set_show_tabs (true);
82         notebook.set_show_border (true);
83         notebook.set_name ("RouteParamNotebook");
84
85         // create the tree model
86         route_display_model = ListStore::create(route_display_columns);
87
88         // setup the treeview
89         route_display.set_model(route_display_model);
90         route_display.append_column(_("Tracks/Busses"), route_display_columns.text);
91         route_display.set_name(X_("RouteParamsListDisplay"));
92         route_display.get_selection()->set_mode(Gtk::SELECTION_SINGLE); // default
93         route_display.set_reorderable(false);
94         route_display.set_size_request(75, -1);
95         route_display.set_headers_visible(true);
96         route_display.set_headers_clickable(true);
97
98         route_select_scroller.add(route_display);
99         route_select_scroller.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
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 (redir_hpane, _("Plugins, Inserts & Sends")));
110         notebook.pages().push_back (TabElem (latency_frame, _("Latency")));
111
112         notebook.set_name ("InspectorNotebook");
113         
114         title_label.set_name ("RouteParamsTitleLabel");
115         update_title();
116         
117         latency_packer.set_spacing (18);
118         latency_button_box.pack_start (latency_apply_button);
119         delay_label.set_alignment (0, 0.5);
120
121         // changeable area
122         route_param_frame.set_name("RouteParamsBaseFrame");
123         route_param_frame.set_shadow_type (Gtk::SHADOW_IN);
124         
125         
126         route_hpacker.pack_start (notebook, true, true);
127         
128         route_vpacker.pack_start (title_label, false, false);
129         route_vpacker.pack_start (route_hpacker, true, true);
130
131         
132         list_hpane.pack1 (list_vpacker);
133         list_hpane.add2 (route_vpacker);
134
135         list_hpane.set_position(110);
136
137         redir_hpane.set_position(110);
138         
139         //global_vpacker.pack_start (list_hpane, true, true);
140         //get_vbox()->pack_start (global_vpacker);
141         get_vbox()->pack_start (list_hpane);
142         
143         
144         set_name ("RouteParamsWindow");
145         set_default_size (620,370);
146         set_wmclass (X_("ardour_route_parameters"), "Ardour");
147
148         WindowTitle title(Glib::get_application_name());
149         title += _("Track/Bus Inspector"); 
150         set_title (title.get_string());
151
152
153         // events
154         route_display.get_selection()->signal_changed().connect(mem_fun(*this, &RouteParams_UI::route_selected));
155         route_display.get_column(0)->signal_clicked().connect(mem_fun(*this, &RouteParams_UI::show_track_menu));
156
157         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
158         
159         _plugin_selector = new PluginSelector (PluginManager::the_manager());
160         _plugin_selector->signal_delete_event().connect (bind (ptr_fun (just_hide_it), 
161                                                      static_cast<Window *> (_plugin_selector)));
162
163
164         signal_delete_event().connect(bind(ptr_fun(just_hide_it), static_cast<Gtk::Window *>(this)));
165 }
166
167 RouteParams_UI::~RouteParams_UI ()
168 {
169 }
170
171 void
172 RouteParams_UI::add_routes (RouteList& routes)
173 {
174         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::add_routes), routes));
175         
176         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
177                 boost::shared_ptr<Route> route = (*x);
178
179                 if (route->is_hidden()) {
180                         return;
181                 }
182                 
183                 TreeModel::Row row = *(route_display_model->append());
184                 row[route_display_columns.text] = route->name();
185                 row[route_display_columns.route] = route;
186                 
187                 //route_select_list.rows().back().select ();
188                 
189                 route->NameChanged.connect (bind (mem_fun(*this, &RouteParams_UI::route_name_changed), route));
190                 route->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
191         }
192 }
193
194
195 void
196 RouteParams_UI::route_name_changed (boost::shared_ptr<Route> route)
197 {
198         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::route_name_changed), route));
199
200         bool found = false ;
201         TreeModel::Children rows = route_display_model->children();
202         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
203                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
204                 if (r == route) {
205                         (*iter)[route_display_columns.text] = route->name() ;
206                         found = true ;
207                         break;
208                 }
209         }
210
211         if(!found) {
212                 error << _("route display list item for renamed route not found!") << endmsg;
213         }
214
215         if (route == _route) {
216                 track_input_label.set_text (route->name());
217                 update_title();
218         }
219 }
220
221 void
222 RouteParams_UI::setup_processor_boxes()
223 {
224         if (session && _route) {
225
226                 // just in case... shouldn't need this
227                 cleanup_processor_boxes();
228                 
229                 // construct new redirect boxes
230                 insert_box = new ProcessorBox(*session, *_plugin_selector, _rr_selection, 0);
231                 insert_box->set_route (_route);
232
233                 redir_hpane.pack1 (*insert_box);
234
235                 insert_box->ProcessorSelected.connect (mem_fun(*this, &RouteParams_UI::redirect_selected));
236                 insert_box->ProcessorUnselected.connect (mem_fun(*this, &RouteParams_UI::redirect_selected));
237
238                 redir_hpane.show_all();
239         }
240 }
241
242 void
243 RouteParams_UI::cleanup_processor_boxes()
244 {
245         if (insert_box) {
246                 redir_hpane.remove(*insert_box);
247                 delete insert_box;
248                 insert_box = 0;
249         }
250 }
251
252 void
253 RouteParams_UI::refresh_latency ()
254 {
255         if (latency_widget) {
256                 latency_widget->refresh();
257
258                 char buf[128];
259                 snprintf (buf, sizeof (buf), _("Playback delay: %u samples"), _route->initial_delay());
260                 delay_label.set_text (buf);
261         }
262 }
263
264 void
265 RouteParams_UI::cleanup_latency_frame ()
266 {
267         if (latency_widget) {
268                 latency_frame.remove ();
269                 latency_packer.remove (*latency_widget);
270                 latency_packer.remove (latency_button_box);
271                 latency_packer.remove (delay_label);
272                 delete latency_widget;
273                 latency_widget = 0;
274                 latency_conn.disconnect ();
275                 delay_conn.disconnect ();
276                 latency_apply_conn.disconnect ();
277         }
278 }
279
280 void
281 RouteParams_UI::setup_latency_frame ()
282 {
283         latency_widget = new LatencyGUI (*(_route->output()), session->frame_rate(), session->engine().frames_per_cycle());
284
285         char buf[128];
286         snprintf (buf, sizeof (buf), _("Playback delay: %u samples"), _route->initial_delay());
287         delay_label.set_text (buf);
288
289         latency_packer.pack_start (*latency_widget, false, false);
290         latency_packer.pack_start (latency_button_box, false, false);
291         latency_packer.pack_start (delay_label);
292
293         latency_apply_conn = latency_apply_button.signal_clicked().connect (mem_fun (*latency_widget, &LatencyGUI::finish));
294         latency_conn = _route->signal_latency_changed.connect (mem_fun (*this, &RouteParams_UI::refresh_latency));
295         delay_conn = _route->initial_delay_changed.connect (mem_fun (*this, &RouteParams_UI::refresh_latency));
296         
297         latency_frame.add (latency_packer);
298         latency_frame.show_all ();
299 }
300
301 void
302 RouteParams_UI::setup_io_frames()
303 {
304         cleanup_io_frames();
305         
306         // input
307         _input_iosel = new IOSelector (this, *session, _route->input());
308         _input_iosel->setup ();
309         input_frame.add (*_input_iosel);
310         input_frame.show_all();
311         
312         // output
313         _output_iosel = new IOSelector (this, *session, _route->output());
314         _output_iosel->setup ();
315         output_frame.add (*_output_iosel);
316         output_frame.show_all();
317 }
318
319 void
320 RouteParams_UI::cleanup_io_frames()
321 {
322         if (_input_iosel) {
323                 _input_iosel->Finished (IOSelector::Cancelled);
324                 input_frame.remove();
325                 delete _input_iosel;
326                 _input_iosel = 0;
327         }
328
329         if (_output_iosel) {
330                 _output_iosel->Finished (IOSelector::Cancelled);
331
332                 output_frame.remove();
333                 delete _output_iosel;
334                 _output_iosel = 0;
335         }
336 }
337
338 void
339 RouteParams_UI::cleanup_view (bool stopupdate)
340 {
341         if (_active_view) {
342                 GenericPluginUI *   plugui = 0;
343                 
344                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
345                           plugui->stop_updating (0);
346                 }
347
348                 _plugin_conn.disconnect();
349                 redir_hpane.remove(*_active_view);
350                 delete _active_view;
351                 _active_view = 0;
352         }
353 }
354
355 void
356 RouteParams_UI::route_removed (boost::shared_ptr<Route> route)
357 {
358         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::route_removed), route));
359
360         TreeModel::Children rows = route_display_model->children();
361         TreeModel::Children::iterator ri;
362
363         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
364                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
365
366                 if (r == route) {
367                         route_display_model->erase(iter);
368                         break;
369                 }
370         }
371
372         if (route == _route) {
373                 cleanup_io_frames();
374                 cleanup_view();
375                 cleanup_processor_boxes();
376                 
377                 _route.reset ((Route*) 0);
378                 _processor.reset ((Processor*) 0);
379                 update_title();
380         }
381 }
382
383 void
384 RouteParams_UI::set_session (Session *sess)
385 {
386         ArdourDialog::set_session (sess);
387
388         route_display_model->clear();
389
390         if (session) {
391                 boost::shared_ptr<RouteList> r = session->get_routes();
392                 add_routes (*r);
393                 session->GoingAway.connect (mem_fun(*this, &ArdourDialog::session_gone));
394                 session->RouteAdded.connect (mem_fun(*this, &RouteParams_UI::add_routes));
395                 start_updating ();
396         } else {
397                 stop_updating ();
398         }
399
400         //route_select_list.thaw ();
401
402         _plugin_selector->set_session (session);
403 }       
404
405
406 void
407 RouteParams_UI::session_gone ()
408 {
409         ENSURE_GUI_THREAD(mem_fun(*this, &RouteParams_UI::session_gone));
410
411         route_display_model->clear();
412
413         cleanup_io_frames();
414         cleanup_view();
415         cleanup_processor_boxes();
416         cleanup_latency_frame ();
417
418         _route.reset ((Route*) 0);
419         _processor.reset ((Processor*) 0);
420         update_title();
421
422         ArdourDialog::session_gone();
423
424 }
425
426 void
427 RouteParams_UI::route_selected()
428 {
429         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
430         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
431
432         if(iter) {
433                 //If anything is selected
434                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
435
436                 if (_route == route) {
437                         // do nothing
438                         return;
439                 }
440
441                 // remove event binding from previously selected
442                 if (_route) {
443                         _route_conn.disconnect();
444                         _route_ds_conn.disconnect();
445                         cleanup_processor_boxes();
446                         cleanup_view();
447                         cleanup_io_frames();
448                         cleanup_latency_frame ();
449                 }
450
451                 // update the other panes with the correct info
452                 _route = route;
453                 //update_routeinfo (route);
454
455                 setup_io_frames();
456                 setup_processor_boxes();
457                 setup_latency_frame ();
458
459                 // bind to redirects changed event for this route
460                 _route_conn = route->processors_changed.connect (mem_fun(*this, &RouteParams_UI::processors_changed));
461
462                 track_input_label.set_text (_route->name());
463
464                 update_title();
465
466         } else {
467                 // no selection
468                 if (_route) {
469                         _route_conn.disconnect();
470
471                         // remove from view
472                         cleanup_io_frames();
473                         cleanup_view();
474                         cleanup_processor_boxes();
475                         cleanup_latency_frame ();
476
477                         _route.reset ((Route*) 0);
478                         _processor.reset ((Processor*) 0);
479                         track_input_label.set_text(_("NO TRACK"));
480                         update_title();
481                 }
482         }
483 }
484
485 void
486 RouteParams_UI::processors_changed ()
487 {
488         ENSURE_GUI_THREAD(mem_fun(*this, &RouteParams_UI::processors_changed));
489         cleanup_view();
490         
491         _processor.reset ((Processor*) 0);
492
493         //update_title();
494 }
495
496 void
497 RouteParams_UI::show_track_menu()
498 {
499         using namespace Menu_Helpers;
500         
501         if (track_menu == 0) {
502                 track_menu = new Menu;
503                 track_menu->set_name ("ArdourContextMenu");
504                 track_menu->items().push_back 
505                                 (MenuElem (_("Add Track/Bus"), 
506                                            bind (mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route), (Gtk::Window*) 0)));
507         }
508         track_menu->popup (1, gtk_get_current_event_time());
509 }
510
511 void
512 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> insert)
513 {
514         boost::shared_ptr<Send> send;
515         boost::shared_ptr<Return> retrn;
516         boost::shared_ptr<PluginInsert> plugin_insert;
517         boost::shared_ptr<PortInsert> port_insert;
518         
519         if ((send = boost::dynamic_pointer_cast<Send> (insert)) != 0) {
520
521                 SendUI *send_ui = new SendUI (this, send, *session);
522
523                 cleanup_view();
524                 _plugin_conn = send->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_going_away),
525                                                               insert));
526                 _active_view = send_ui;
527                 
528                 redir_hpane.add2 (*_active_view);
529                 redir_hpane.show_all();
530
531         } else if ((retrn = boost::dynamic_pointer_cast<Return> (insert)) != 0) {
532
533                 ReturnUI *return_ui = new ReturnUI (this, retrn, *session);
534
535                 cleanup_view();
536                 _plugin_conn = retrn->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_going_away),
537                                                                insert));
538                 _active_view = return_ui;
539                 
540                 redir_hpane.add2 (*_active_view);
541                 redir_hpane.show_all();
542
543         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (insert)) != 0) {                         
544
545                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
546
547                 cleanup_view();
548                 _plugin_conn = plugin_insert->plugin()->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::plugin_going_away),
549                                                                                  PreFader));
550                 plugin_ui->start_updating (0);
551                 _active_view = plugin_ui;
552                 redir_hpane.pack2 (*_active_view);
553                 redir_hpane.show_all();
554
555         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (insert)) != 0) {
556
557                 PortInsertUI *portinsert_ui = new PortInsertUI (this, *session, port_insert);
558                                 
559                 cleanup_view();
560                 _plugin_conn = port_insert->GoingAway.connect (bind (mem_fun(*this, &RouteParams_UI::redirect_going_away),
561                                                                      insert));
562                 _active_view = portinsert_ui;
563                 redir_hpane.pack2 (*_active_view);
564                 portinsert_ui->redisplay();
565                 redir_hpane.show_all();
566         }
567                                 
568         _processor = insert;
569         
570         update_title();
571                 
572 }
573
574 void
575 RouteParams_UI::plugin_going_away (Placement place)
576 {
577         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::plugin_going_away), place));
578         
579         // delete the current view without calling finish
580
581         if (place == PreFader) {
582                 cleanup_view (false);
583                 _processor.reset ((Processor*) 0);
584         }
585 }
586
587 void
588 RouteParams_UI::redirect_going_away (boost::shared_ptr<ARDOUR::Processor> insert)
589
590 {
591         ENSURE_GUI_THREAD(bind (mem_fun(*this, &RouteParams_UI::redirect_going_away), insert));
592         
593         printf ("redirect going away\n");
594         // delete the current view without calling finish
595         if (insert == _processor) {
596                 cleanup_view (false);
597                 _processor.reset ((Processor*) 0);
598         } 
599 }
600
601 void
602 RouteParams_UI::update_title ()
603 {
604         WindowTitle title(Glib::get_application_name());
605         title += _("Track/Bus Inspector");
606
607         if (_route) {
608
609                 //              title += ": ";
610
611                 //              if (_redirect && (_current_view == PLUGIN_CONFIG_VIEW || _current_view == SEND_CONFIG_VIEW)) {
612                 //                      title += _redirect->name();
613                 //              }
614                 //              else if (_current_view == INPUT_CONFIG_VIEW) {
615                 //                      title += _("INPUT");
616                 //              }
617                 //              else if (_current_view == OUTPUT_CONFIG_VIEW) {
618                 //                      title += _("OUTPUT");
619                 //              }
620
621                 title_label.set_text(_route->name());
622
623                 title += _route->name();
624
625                 set_title(title.get_string());
626         }
627         else {
628                 title_label.set_text(_("No Route Selected"));
629                 title += _("No Route Selected");
630                 set_title(title.get_string());
631         }       
632 }
633
634 void
635 RouteParams_UI::start_updating ()
636 {
637         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
638                 (mem_fun(*this, &RouteParams_UI::update_views));
639 }
640
641 void
642 RouteParams_UI::stop_updating ()
643 {
644         update_connection.disconnect();
645 }
646
647 void
648 RouteParams_UI::update_views ()
649 {
650         SendUI *sui;
651         // TODO: only do it if correct tab is showing
652         
653         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
654                 sui->update ();
655         }
656 }