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