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