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