fix conflicts caused by meterbridge merge
[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         show_all();
150 }
151
152 RouteParams_UI::~RouteParams_UI ()
153 {
154 }
155
156 void
157 RouteParams_UI::add_routes (RouteList& routes)
158 {
159         ENSURE_GUI_THREAD (*this, &RouteParams_UI::add_routes, routes)
160
161         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
162                 boost::shared_ptr<Route> route = (*x);
163
164                 if (route->is_auditioner()) {
165                         return;
166                 }
167
168                 TreeModel::Row row = *(route_display_model->append());
169                 row[route_display_columns.text] = route->name();
170                 row[route_display_columns.route] = route;
171
172                 //route_select_list.rows().back().select ();
173
174                 route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_property_changed, this, _1, boost::weak_ptr<Route>(route)), gui_context());
175                 route->DropReferences.connect (*this, invalidator (*this), boost::bind (&RouteParams_UI::route_removed, this, boost::weak_ptr<Route>(route)), gui_context());
176         }
177 }
178
179
180 void
181 RouteParams_UI::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Route> wr)
182 {
183         if (!what_changed.contains (ARDOUR::Properties::name)) {
184                 return;
185         }
186
187         boost::shared_ptr<Route> route (wr.lock());
188
189         if (!route) {
190                 return;
191         }
192
193         ENSURE_GUI_THREAD (*this, &RouteParams_UI::route_name_changed, wr)
194
195         bool found = false ;
196         TreeModel::Children rows = route_display_model->children();
197         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
198                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
199                 if (r == route) {
200                         (*iter)[route_display_columns.text] = route->name() ;
201                         found = true ;
202                         break;
203                 }
204         }
205
206         if(!found) {
207                 error << _("route display list item for renamed route not found!") << endmsg;
208         }
209
210         if (route == _route) {
211                 track_input_label.set_text (route->name());
212                 update_title();
213         }
214 }
215
216 void
217 RouteParams_UI::setup_processor_boxes()
218 {
219         if (_session && _route) {
220
221                 // just in case... shouldn't need this
222                 cleanup_processor_boxes();
223
224                 // construct new redirect boxes
225                 insert_box = new ProcessorBox (_session, boost::bind (&RouteParams_UI::plugin_selector, this), _rr_selection, 0);
226                 insert_box->set_route (_route);
227
228                 redir_hpane.pack1 (*insert_box);
229
230                 insert_box->ProcessorSelected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
231                 insert_box->ProcessorUnselected.connect (sigc::mem_fun(*this, &RouteParams_UI::redirect_selected));
232
233                 redir_hpane.show_all();
234         }
235 }
236
237 void
238 RouteParams_UI::cleanup_processor_boxes()
239 {
240         if (insert_box) {
241                 redir_hpane.remove(*insert_box);
242                 delete insert_box;
243                 insert_box = 0;
244         }
245 }
246
247 void
248 RouteParams_UI::refresh_latency ()
249 {
250         if (latency_widget) {
251                 latency_widget->refresh();
252
253                 char buf[128];
254                 snprintf (buf, sizeof (buf), _("Playback delay: %" PRId64 " samples"), _route->initial_delay());
255                 delay_label.set_text (buf);
256         }
257 }
258
259 void
260 RouteParams_UI::cleanup_latency_frame ()
261 {
262         if (latency_widget) {
263                 latency_frame.remove ();
264                 latency_packer.remove (*latency_widget);
265                 latency_packer.remove (latency_button_box);
266                 latency_packer.remove (delay_label);
267                 latency_connections.drop_connections ();
268                 latency_click_connection.disconnect ();
269
270                 delete latency_widget;
271                 latency_widget = 0;
272
273         }
274 }
275
276 void
277 RouteParams_UI::setup_latency_frame ()
278 {
279         latency_widget = new LatencyGUI (*(_route->output()), _session->frame_rate(), _session->engine().frames_per_cycle());
280
281         char buf[128];
282         snprintf (buf, sizeof (buf), _("Playback delay: %" PRId64 " samples"), _route->initial_delay());
283         delay_label.set_text (buf);
284
285         latency_packer.pack_start (*latency_widget, false, false);
286         latency_packer.pack_start (latency_button_box, false, false);
287         latency_packer.pack_start (delay_label);
288
289         latency_click_connection = latency_apply_button.signal_clicked().connect (sigc::mem_fun (*latency_widget, &LatencyGUI::finish));
290         _route->signal_latency_changed.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
291         _route->initial_delay_changed.connect (latency_connections, invalidator (*this), boost::bind (&RouteParams_UI::refresh_latency, this), gui_context());
292
293         latency_frame.add (latency_packer);
294         latency_frame.show_all ();
295 }
296
297 void
298 RouteParams_UI::setup_io_frames()
299 {
300         cleanup_io_frames();
301
302         // input
303         _input_iosel = new IOSelector (this, _session, _route->input());
304         _input_iosel->setup ();
305         input_frame.add (*_input_iosel);
306         input_frame.show_all();
307
308         // output
309         _output_iosel = new IOSelector (this, _session, _route->output());
310         _output_iosel->setup ();
311         output_frame.add (*_output_iosel);
312         output_frame.show_all();
313 }
314
315 void
316 RouteParams_UI::cleanup_io_frames()
317 {
318         if (_input_iosel) {
319                 _input_iosel->Finished (IOSelector::Cancelled);
320                 input_frame.remove();
321                 delete _input_iosel;
322                 _input_iosel = 0;
323         }
324
325         if (_output_iosel) {
326                 _output_iosel->Finished (IOSelector::Cancelled);
327
328                 output_frame.remove();
329                 delete _output_iosel;
330                 _output_iosel = 0;
331         }
332 }
333
334 void
335 RouteParams_UI::cleanup_view (bool stopupdate)
336 {
337         if (_active_view) {
338                 GenericPluginUI *   plugui = 0;
339
340                 if (stopupdate && (plugui = dynamic_cast<GenericPluginUI*>(_active_view)) != 0) {
341                           plugui->stop_updating (0);
342                 }
343
344                 _processor_going_away_connection.disconnect ();
345                 redir_hpane.remove(*_active_view);
346                 delete _active_view;
347                 _active_view = 0;
348         }
349 }
350
351 void
352 RouteParams_UI::route_removed (boost::weak_ptr<Route> wr)
353 {
354         boost::shared_ptr<Route> route (wr.lock());
355
356         if (!route) {
357                 return;
358         }
359
360         ENSURE_GUI_THREAD (*this, invalidator (*this), &RouteParams_UI::route_removed, wr)
361
362         TreeModel::Children rows = route_display_model->children();
363         TreeModel::Children::iterator ri;
364
365         for(TreeModel::Children::iterator iter = rows.begin(); iter != rows.end(); ++iter) {
366                 boost::shared_ptr<Route> r =(*iter)[route_display_columns.route];
367
368                 if (r == route) {
369                         route_display_model->erase(iter);
370                         break;
371                 }
372         }
373
374         if (route == _route) {
375                 cleanup_io_frames();
376                 cleanup_view();
377                 cleanup_processor_boxes();
378
379                 _route.reset ((Route*) 0);
380                 _processor.reset ((Processor*) 0);
381                 update_title();
382         }
383 }
384
385 void
386 RouteParams_UI::set_session (Session *sess)
387 {
388         ArdourWindow::set_session (sess);
389
390         route_display_model->clear();
391         _plugin_selector->set_session (_session);
392
393         if (_session) {
394                 boost::shared_ptr<RouteList> r = _session->get_routes();
395                 add_routes (*r);
396                 _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&RouteParams_UI::add_routes, this, _1), gui_context());
397                 start_updating ();
398         } else {
399                 stop_updating ();
400         }
401 }
402
403
404 void
405 RouteParams_UI::session_going_away ()
406 {
407         ENSURE_GUI_THREAD (*this, &RouteParams_UI::session_going_away);
408
409         SessionHandlePtr::session_going_away ();
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
423 void
424 RouteParams_UI::route_selected()
425 {
426         Glib::RefPtr<TreeSelection> selection = route_display.get_selection();
427         TreeModel::iterator iter = selection->get_selected(); // only used with Gtk::SELECTION_SINGLE
428
429         if(iter) {
430                 //If anything is selected
431                 boost::shared_ptr<Route> route = (*iter)[route_display_columns.route] ;
432
433                 if (_route == route) {
434                         // do nothing
435                         return;
436                 }
437
438                 // remove event binding from previously selected
439                 if (_route) {
440                         _route_processors_connection.disconnect ();
441                         cleanup_processor_boxes();
442                         cleanup_view();
443                         cleanup_io_frames();
444                         cleanup_latency_frame ();
445                 }
446
447                 // update the other panes with the correct info
448                 _route = route;
449                 //update_routeinfo (route);
450
451                 setup_io_frames();
452                 setup_processor_boxes();
453                 setup_latency_frame ();
454
455                 route->processors_changed.connect (_route_processors_connection, invalidator (*this), boost::bind (&RouteParams_UI::processors_changed, this, _1), gui_context());
456
457                 track_input_label.set_text (_route->name());
458
459                 update_title();
460
461         } else {
462                 // no selection
463                 if (_route) {
464                         _route_processors_connection.disconnect ();
465
466                         // remove from view
467                         cleanup_io_frames();
468                         cleanup_view();
469                         cleanup_processor_boxes();
470                         cleanup_latency_frame ();
471
472                         _route.reset ((Route*) 0);
473                         _processor.reset ((Processor*) 0);
474                         track_input_label.set_text(_("NO TRACK"));
475                         update_title();
476                 }
477         }
478 }
479
480 void
481 RouteParams_UI::processors_changed (RouteProcessorChange)
482 {
483         cleanup_view();
484
485         _processor.reset ((Processor*) 0);
486
487         //update_title();
488 }
489
490 void
491 RouteParams_UI::show_track_menu()
492 {
493         using namespace Menu_Helpers;
494
495         if (track_menu == 0) {
496                 track_menu = new Menu;
497                 track_menu->set_name ("ArdourContextMenu");
498                 track_menu->items().push_back
499                                 (MenuElem (_("Add Track or Bus"),
500                                            sigc::bind (sigc::mem_fun (*(ARDOUR_UI::instance()), &ARDOUR_UI::add_route), (Gtk::Window*) 0)));
501         }
502         track_menu->popup (1, gtk_get_current_event_time());
503 }
504
505 void
506 RouteParams_UI::redirect_selected (boost::shared_ptr<ARDOUR::Processor> proc)
507 {
508         boost::shared_ptr<Send> send;
509         boost::shared_ptr<Return> retrn;
510         boost::shared_ptr<PluginInsert> plugin_insert;
511         boost::shared_ptr<PortInsert> port_insert;
512
513         if ((send = boost::dynamic_pointer_cast<Send> (proc)) != 0) {
514
515                 SendUI *send_ui = new SendUI (this, send, _session);
516
517                 cleanup_view();
518                 send->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
519                 _active_view = send_ui;
520
521                 redir_hpane.add2 (*_active_view);
522                 redir_hpane.show_all();
523
524         } else if ((retrn = boost::dynamic_pointer_cast<Return> (proc)) != 0) {
525
526                 ReturnUI *return_ui = new ReturnUI (this, retrn, _session);
527
528                 cleanup_view();
529                 retrn->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::processor_going_away, this, boost::weak_ptr<Processor>(proc)), gui_context());
530                 _active_view = return_ui;
531
532                 redir_hpane.add2 (*_active_view);
533                 redir_hpane.show_all();
534
535         } else if ((plugin_insert = boost::dynamic_pointer_cast<PluginInsert> (proc)) != 0) {
536
537                 GenericPluginUI *plugin_ui = new GenericPluginUI (plugin_insert, true);
538
539                 cleanup_view();
540                 plugin_insert->plugin()->DropReferences.connect (_processor_going_away_connection, invalidator (*this), boost::bind (&RouteParams_UI::plugin_going_away, this, PreFader), gui_context());
541                 plugin_ui->start_updating (0);
542                 _active_view = plugin_ui;
543
544                 redir_hpane.pack2 (*_active_view);
545                 redir_hpane.show_all();
546
547         } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (proc)) != 0) {
548
549                 PortInsertUI *portinsert_ui = new PortInsertUI (this, _session, port_insert);
550
551                 cleanup_view();
552                 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());
553                 _active_view = portinsert_ui;
554
555                 redir_hpane.pack2 (*_active_view);
556                 portinsert_ui->redisplay();
557                 redir_hpane.show_all();
558         }
559
560         _processor = proc;
561         update_title();
562
563 }
564
565 void
566 RouteParams_UI::plugin_going_away (Placement place)
567 {
568         ENSURE_GUI_THREAD (*this, &RouteParams_UI::plugin_going_away, place)
569
570         // delete the current view without calling finish
571
572         if (place == PreFader) {
573                 cleanup_view (false);
574                 _processor.reset ((Processor*) 0);
575         }
576 }
577
578 void
579 RouteParams_UI::processor_going_away (boost::weak_ptr<ARDOUR::Processor> wproc)
580 {
581         boost::shared_ptr<Processor> proc = (wproc.lock());
582
583         if (!proc) {
584                 return;
585         }
586
587         ENSURE_GUI_THREAD (*this, &RouteParams_UI::processor_going_away, wproc)
588
589         printf ("redirect going away\n");
590         // delete the current view without calling finish
591         if (proc == _processor) {
592                 cleanup_view (false);
593                 _processor.reset ((Processor*) 0);
594         }
595 }
596
597 void
598 RouteParams_UI::update_title ()
599 {
600         WindowTitle title (_("Tracks and Busses"));
601
602         if (_route) {
603                 title_label.set_text(_route->name());
604                 title += _route->name();
605                 set_title(title.get_string());
606         } else {
607                 title_label.set_text(_("No Track or Bus Selected"));
608                 title += _("No Track or Bus Selected");
609                 set_title(title.get_string());
610         }
611 }
612
613 void
614 RouteParams_UI::start_updating ()
615 {
616         update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
617                 (sigc::mem_fun(*this, &RouteParams_UI::update_views));
618 }
619
620 void
621 RouteParams_UI::stop_updating ()
622 {
623         update_connection.disconnect();
624 }
625
626 void
627 RouteParams_UI::update_views ()
628 {
629         SendUI *sui;
630         // TODO: only do it if correct tab is showing
631
632         if ((sui = dynamic_cast<SendUI*> (_active_view)) != 0) {
633                 sui->update ();
634         }
635 }