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