Factor out sequencing related things into an independant new library: "evoral".
[ardour.git] / gtk2_ardour / route_time_axis.cc
1 /*
2     Copyright (C) 2006 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 #include <cstdlib>
20 #include <cmath>
21 #include <cassert>
22
23 #include <algorithm>
24 #include <string>
25 #include <vector>
26 #include <utility>
27
28 #include <sigc++/bind.h>
29
30 #include <pbd/error.h>
31 #include <pbd/stl_delete.h>
32 #include <pbd/whitespace.h>
33 #include <pbd/memento_command.h>
34
35 #include <gtkmm/menu.h>
36 #include <gtkmm/menuitem.h>
37 #include <gtkmm2ext/gtk_ui.h>
38 #include <gtkmm2ext/selector.h>
39 #include <gtkmm2ext/stop_signal.h>
40 #include <gtkmm2ext/bindable_button.h>
41 #include <gtkmm2ext/utils.h>
42
43 #include <ardour/playlist.h>
44 #include <ardour/audioplaylist.h>
45 #include <ardour/diskstream.h>
46 #include <ardour/processor.h>
47 #include <ardour/ladspa_plugin.h>
48 #include <ardour/location.h>
49 #include <ardour/panner.h>
50 #include <ardour/playlist.h>
51 #include <ardour/session.h>
52 #include <ardour/session_playlist.h>
53 #include <ardour/utils.h>
54 #include <ardour/parameter.h>
55 #include <ardour/profile.h>
56
57 #include "ardour_ui.h"
58 #include "route_time_axis.h"
59 #include "automation_time_axis.h"
60 #include "canvas_impl.h"
61 #include "crossfade_view.h"
62 #include "enums.h"
63 #include "gui_thread.h"
64 #include "keyboard.h"
65 #include "playlist_selector.h"
66 #include "point_selection.h"
67 #include "prompter.h"
68 #include "public_editor.h"
69 #include "region_view.h"
70 #include "rgb_macros.h"
71 #include "selection.h"
72 #include "simplerect.h"
73 #include "streamview.h"
74 #include "utils.h"
75
76 #include <ardour/track.h>
77
78 #include "i18n.h"
79
80 using namespace ARDOUR;
81 using namespace PBD;
82 using namespace Gtkmm2ext;
83 using namespace Gtk;
84 using namespace Editing;
85 using namespace sigc;
86 using namespace std;
87
88 Glib::RefPtr<Gdk::Pixbuf> RouteTimeAxisView::slider;
89
90 void
91 RouteTimeAxisView::setup_slider_pix ()
92 {
93         if ((slider = ::get_icon ("fader_belt_h")) == 0) {
94                 throw failed_constructor ();
95         }
96 }
97
98 RouteTimeAxisView::RouteTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
99         : AxisView(sess),
100           RouteUI(rt, sess, _("m"), _("s"), _("r")), // mute, solo, and record
101           TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas),
102           parent_canvas (canvas),
103           button_table (3, 3),
104           edit_group_button (_("g")), // group
105           playlist_button (_("p")), 
106           size_button (_("h")), // height
107           automation_button (_("a")),
108           visual_button (_("v")),
109           gm (rt, sess, slider, true)
110
111 {
112         gm.get_level_meter().set_no_show_all();
113         gm.get_level_meter().setup_meters(50);
114
115         _has_state = true;
116         playlist_menu = 0;
117         playlist_action_menu = 0;
118         automation_action_menu = 0;
119         _view = 0;
120
121         if (!_route->is_hidden()) {
122                 _marked_for_display = true;
123         }
124
125         timestretch_rect = 0;
126         no_redraw = false;
127         destructive_track_mode_item = 0;
128         normal_track_mode_item = 0;
129
130         ignore_toggle = false;
131
132         edit_group_button.set_name ("TrackGroupButton");
133         playlist_button.set_name ("TrackPlaylistButton");
134         automation_button.set_name ("TrackAutomationButton");
135         size_button.set_name ("TrackSizeButton");
136         visual_button.set_name ("TrackVisualButton");
137         hide_button.set_name ("TrackRemoveButton");
138
139         hide_button.add (*(manage (new Image (::get_icon("hide")))));
140         hide_button.show_all ();
141
142         edit_group_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::edit_click), false);
143         playlist_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::playlist_click));
144         automation_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::automation_click));
145         size_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::size_click), false);
146         visual_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::visual_click));
147         hide_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::hide_click));
148
149         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
150         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release));
151         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
152         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release));
153
154         if (is_track()) {
155
156                 /* use icon */
157
158                 rec_enable_button->remove ();
159                 switch (track()->mode()) {
160                 case ARDOUR::Normal:
161                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
162                         break;
163                 case ARDOUR::Destructive:
164                         rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
165                         break;
166                 }
167                 rec_enable_button->show_all ();
168
169                 rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press), false);
170                 rec_enable_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::rec_enable_release));
171                 controls_table.attach (*rec_enable_button, 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
172                 ARDOUR_UI::instance()->tooltips().set_tip(*rec_enable_button, _("Record"));
173
174         }
175
176         controls_hbox.pack_start(gm.get_level_meter(), false, false);
177         _route->meter_change.connect (mem_fun(*this, &RouteTimeAxisView::meter_changed));
178         _route->input_changed.connect (mem_fun(*this, &RouteTimeAxisView::io_changed));
179         _route->output_changed.connect (mem_fun(*this, &RouteTimeAxisView::io_changed));
180
181         controls_table.attach (*mute_button, 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
182         controls_table.attach (*solo_button, 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
183
184         controls_table.attach (edit_group_button, 7, 8, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
185         controls_table.attach (gm.get_gain_slider(), 0, 5, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
186
187         ARDOUR_UI::instance()->tooltips().set_tip(*solo_button,_("Solo"));
188         ARDOUR_UI::instance()->tooltips().set_tip(*mute_button,_("Mute"));
189         ARDOUR_UI::instance()->tooltips().set_tip(edit_group_button,_("Edit Group"));
190         ARDOUR_UI::instance()->tooltips().set_tip(size_button,_("Display Height"));
191         ARDOUR_UI::instance()->tooltips().set_tip(playlist_button,_("Playlist"));
192         ARDOUR_UI::instance()->tooltips().set_tip(automation_button, _("Automation"));
193         ARDOUR_UI::instance()->tooltips().set_tip(visual_button, _("Visual options"));
194         ARDOUR_UI::instance()->tooltips().set_tip(hide_button, _("Hide this track"));
195         
196         label_view ();
197
198         if (0) {
199
200                 /* old school - when we used to put an extra row of buttons in place */
201
202                 controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
203                 controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
204                 controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
205                 controls_table.attach (automation_button, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
206
207         } else {
208
209                 controls_table.attach (automation_button, 6, 7, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
210         }
211
212         if (is_track() && track()->mode() == ARDOUR::Normal) {
213                 controls_table.attach (playlist_button, 5, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
214         }
215
216         y_position = -1;
217
218         _route->mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed));
219         _route->solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
220         _route->processors_changed.connect (mem_fun(*this, &RouteTimeAxisView::processors_changed));
221         _route->NameChanged.connect (mem_fun(*this, &RouteTimeAxisView::route_name_changed));
222         _route->solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
223
224
225         if (is_track()) {
226
227                 track()->TrackModeChanged.connect (mem_fun(*this, &RouteTimeAxisView::track_mode_changed));
228                 track()->FreezeChange.connect (mem_fun(*this, &RouteTimeAxisView::map_frozen));
229                 track()->DiskstreamChanged.connect (mem_fun(*this, &RouteTimeAxisView::diskstream_changed));
230                 get_diskstream()->SpeedChanged.connect (mem_fun(*this, &RouteTimeAxisView::speed_changed));
231
232                 /* pick up the correct freeze state */
233                 map_frozen ();
234
235         }
236
237         editor.ZoomChanged.connect (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
238         ColorsChanged.connect (mem_fun (*this, &RouteTimeAxisView::color_handler));
239
240         gm.get_gain_slider().signal_scroll_event().connect(mem_fun(*this, &RouteTimeAxisView::controls_ebox_scroll), false);
241         gm.get_gain_slider().set_name ("TrackGainFader");
242 }
243
244 RouteTimeAxisView::~RouteTimeAxisView ()
245 {
246         GoingAway (); /* EMIT_SIGNAL */
247
248         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
249                 delete *i;
250         }
251
252         if (playlist_menu) {
253                 delete playlist_menu;
254                 playlist_menu = 0;
255         }
256   
257         if (playlist_action_menu) {
258                 delete playlist_action_menu;
259                 playlist_action_menu = 0;
260         }
261
262         if (_view) {
263                 delete _view;
264                 _view = 0;
265         }
266
267         for (AutomationTracks::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
268                 delete i->second;
269         }
270         
271         _automation_tracks.clear ();
272 }
273
274 void
275 RouteTimeAxisView::post_construct ()
276 {
277         /* map current state of the route */
278
279         update_diskstream_display ();
280
281         subplugin_menu.items().clear ();
282         _route->foreach_processor (this, &RouteTimeAxisView::add_processor_to_subplugin_menu);
283         _route->foreach_processor (this, &RouteTimeAxisView::add_existing_processor_automation_curves);
284         reset_processor_automation_curves ();
285 }
286
287 void
288 RouteTimeAxisView::set_playlist (boost::shared_ptr<Playlist> newplaylist)
289 {
290         boost::shared_ptr<Playlist> pl = playlist();
291         assert(pl);
292
293         modified_connection.disconnect ();
294         modified_connection = pl->Modified.connect (mem_fun(*this, &RouteTimeAxisView::playlist_modified));
295 }
296
297 void
298 RouteTimeAxisView::playlist_modified ()
299 {
300 }
301
302 gint
303 RouteTimeAxisView::edit_click (GdkEventButton *ev)
304 {
305         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
306                 _route->set_edit_group (0, this);
307                 return FALSE;
308         } 
309
310         using namespace Menu_Helpers;
311
312         MenuList& items = edit_group_menu.items ();
313         RadioMenuItem::Group group;
314
315         items.clear ();
316         items.push_back (RadioMenuElem (group, _("No group"), 
317                                         bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), (RouteGroup *) 0)));
318         
319         if (_route->edit_group() == 0) {
320                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
321         }
322         
323         _session.foreach_edit_group (bind (mem_fun (*this, &RouteTimeAxisView::add_edit_group_menu_item), &group));
324         edit_group_menu.popup (ev->button, ev->time);
325
326         return FALSE;
327 }
328
329 void
330 RouteTimeAxisView::add_edit_group_menu_item (RouteGroup *eg, RadioMenuItem::Group* group)
331 {
332         using namespace Menu_Helpers;
333
334         MenuList &items = edit_group_menu.items();
335
336         items.push_back (RadioMenuElem (*group, eg->name(), bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), eg)));
337         if (_route->edit_group() == eg) {
338                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
339         }
340 }
341
342 void
343 RouteTimeAxisView::set_edit_group_from_menu (RouteGroup *eg)
344 {
345         _route->set_edit_group (eg, this);
346 }
347
348 void
349 RouteTimeAxisView::playlist_changed ()
350
351 {
352         label_view ();
353
354         if (is_track()) {
355                 set_playlist (get_diskstream()->playlist());
356         }
357 }
358
359 void
360 RouteTimeAxisView::label_view ()
361 {
362         string x = _route->name();
363
364         if (x != name_entry.get_text()) {
365                 name_entry.set_text (x);
366         }
367
368         ARDOUR_UI::instance()->tooltips().set_tip (name_entry, x);
369 }
370
371 void
372 RouteTimeAxisView::route_name_changed ()
373 {
374         editor.route_name_changed (this);
375         label_view ();
376 }
377
378 void
379 RouteTimeAxisView::take_name_changed (void *src)
380
381 {
382         if (src != this) {
383                 label_view ();
384         }
385 }
386
387 void
388 RouteTimeAxisView::playlist_click ()
389 {
390         // always build a new action menu
391         
392         if (playlist_action_menu != 0) {
393                 delete playlist_action_menu;
394         } 
395
396         playlist_action_menu = new Menu;
397         playlist_action_menu->set_name ("ArdourContextMenu");
398         
399         build_playlist_menu (playlist_action_menu);
400
401         conditionally_add_to_selection ();
402         playlist_action_menu->popup (1, gtk_get_current_event_time());
403 }
404
405 void
406 RouteTimeAxisView::automation_click ()
407 {
408         if (automation_action_menu == 0) {
409                 /* this seems odd, but the automation action
410                    menu is built as part of the display menu.
411                 */
412                 build_display_menu ();
413         }
414         conditionally_add_to_selection ();
415         automation_action_menu->popup (1, gtk_get_current_event_time());
416 }
417
418 void
419 RouteTimeAxisView::build_automation_action_menu ()
420 {
421         using namespace Menu_Helpers;
422
423         automation_action_menu = manage (new Menu);
424         MenuList& automation_items = automation_action_menu->items();
425         automation_action_menu->set_name ("ArdourContextMenu");
426         
427         automation_items.push_back (MenuElem (_("Show all automation"),
428                                               mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
429
430         automation_items.push_back (MenuElem (_("Show existing automation"),
431                                               mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
432
433         automation_items.push_back (MenuElem (_("Hide all automation"),
434                                               mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
435
436         if (subplugin_menu.get_attach_widget())
437                 subplugin_menu.detach();
438
439         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
440         
441         map<ARDOUR::Parameter, RouteAutomationNode*>::iterator i;
442         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
443
444                 automation_items.push_back (SeparatorElem());
445
446                 if (i->second->menu_item)
447                         delete i->second->menu_item;
448
449                 automation_items.push_back(CheckMenuElem (_route->describe_parameter(i->second->param), 
450                                 bind (mem_fun(*this, &RouteTimeAxisView::toggle_automation_track), i->second->param)));
451
452                 i->second->menu_item = static_cast<Gtk::CheckMenuItem*>(&automation_items.back());
453
454                 i->second->menu_item->set_active(show_automation(i->second->param));
455                 //i->second->menu_item->set_active(false);
456         }
457 }
458
459 void
460 RouteTimeAxisView::build_display_menu ()
461 {
462         using namespace Menu_Helpers;
463
464         /* get the size menu ready */
465
466         build_size_menu ();
467
468         /* prepare it */
469
470         TimeAxisView::build_display_menu ();
471
472         /* now fill it with our stuff */
473
474         MenuList& items = display_menu->items();
475         display_menu->set_name ("ArdourContextMenu");
476         
477         items.push_back (MenuElem (_("Height"), *size_menu));
478         items.push_back (MenuElem (_("Color"), mem_fun(*this, &RouteTimeAxisView::select_track_color)));
479
480         items.push_back (SeparatorElem());
481
482         build_remote_control_menu ();
483         build_automation_action_menu ();
484
485         if (!Profile->get_sae()) {
486                 items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
487                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
488                 items.push_back (SeparatorElem());
489         }
490
491         // Hook for derived classes to add type specific stuff
492         append_extra_display_menu_items ();
493         items.push_back (SeparatorElem());
494         
495         if (is_track()) {
496
497                 Menu *layers_menu = manage(new Menu);
498                 MenuList &layers_items = layers_menu->items();
499                 layers_menu->set_name("ArdourContextMenu");
500
501                 RadioMenuItem::Group layers_group;
502
503                 layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"),
504                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Overlaid)));
505                 layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"),
506                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Stacked)));
507
508                 items.push_back (MenuElem (_("Layers"), *layers_menu));
509
510                 Menu* alignment_menu = manage (new Menu);
511                 MenuList& alignment_items = alignment_menu->items();
512                 alignment_menu->set_name ("ArdourContextMenu");
513
514                 RadioMenuItem::Group align_group;
515
516                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"),
517                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
518                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
519                 if (get_diskstream()->alignment_style() == ExistingMaterial)
520                         align_existing_item->set_active();
521
522                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"),
523                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
524                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
525                 if (get_diskstream()->alignment_style() == CaptureTime)
526                         align_capture_item->set_active();
527
528                 if (!Profile->get_sae()) {
529                         items.push_back (MenuElem (_("Alignment"), *alignment_menu));
530                         get_diskstream()->AlignmentStyleChanged.connect (mem_fun(*this, &RouteTimeAxisView::align_style_changed));
531                         
532                         RadioMenuItem::Group mode_group;
533                         items.push_back (RadioMenuElem (mode_group, _("Normal mode"),
534                                                         bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Normal)));
535                         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
536                         items.push_back (RadioMenuElem (mode_group, _("Tape mode"),
537                                                         bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Destructive)));
538                         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
539                         
540                         switch (track()->mode()) {
541                         case ARDOUR::Destructive:
542                                 destructive_track_mode_item->set_active ();
543                                 break;
544                         case ARDOUR::Normal:
545                                 normal_track_mode_item->set_active ();
546                                 break;
547                         }
548
549                 get_diskstream()->AlignmentStyleChanged.connect (
550                                 mem_fun(*this, &RouteTimeAxisView::align_style_changed));
551
552                 mode_menu = build_mode_menu();
553                 if (mode_menu)
554                         items.push_back (MenuElem (_("Mode"), *mode_menu));
555                         
556                 items.push_back (SeparatorElem());
557                 }
558         }
559
560         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
561         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
562         route_active_menu_item->set_active (_route->active());
563
564         items.push_back (SeparatorElem());
565         items.push_back (MenuElem (_("Hide"), mem_fun(*this, &RouteTimeAxisView::hide_click)));
566         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
567 }
568
569 static bool __reset_item (RadioMenuItem* item)
570 {
571         item->set_active ();
572         return false;
573 }
574
575 void
576 RouteTimeAxisView::set_track_mode (TrackMode mode)
577 {
578         RadioMenuItem* item;
579         RadioMenuItem* other_item;
580
581         switch (mode) {
582         case ARDOUR::Normal:
583                 item = normal_track_mode_item;
584                 other_item = destructive_track_mode_item;
585                 break;
586         case ARDOUR::Destructive:
587                 item = destructive_track_mode_item;
588                 other_item = normal_track_mode_item;
589                 break;
590         default:
591                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", mode) << endmsg;
592                 /*NOTREACHED*/
593                 return;
594         }
595         
596         if (item && other_item && item->get_active () && track()->mode() != mode) {
597                 _set_track_mode (track().get(), mode, other_item);
598         }
599 }
600
601 void
602 RouteTimeAxisView::_set_track_mode (Track* track, TrackMode mode, RadioMenuItem* reset_item)
603 {
604         bool needs_bounce;
605
606         if (!track->can_use_mode (mode, needs_bounce)) {
607
608                 if (!needs_bounce) {
609                         /* cannot be done */
610                         Glib::signal_idle().connect (bind (sigc::ptr_fun (__reset_item), reset_item));
611                         return;
612                 } else {
613                         cerr << "would bounce this one\n";
614                         return;
615                 }
616         }
617
618         track->set_mode (mode);
619
620         rec_enable_button->remove ();
621         switch (mode) {
622         case ARDOUR::Normal:
623                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
624                 break;
625         case ARDOUR::Destructive:
626                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
627                 break;
628         }
629         rec_enable_button->show_all ();
630
631 }
632
633 void
634 RouteTimeAxisView::track_mode_changed ()
635 {
636         RadioMenuItem* item;
637         
638         switch (track()->mode()) {
639         case ARDOUR::Normal:
640                 item = normal_track_mode_item;
641                 break;
642         case ARDOUR::Destructive:
643                 item = destructive_track_mode_item;
644                 break;
645         default:
646                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", track()->mode()) << endmsg;
647                 /*NOTREACHED*/
648                 return;
649         }
650
651         item->set_active ();
652 }
653
654 void
655 RouteTimeAxisView::show_timestretch (nframes_t start, nframes_t end)
656 {
657         double x1;
658         double x2;
659         double y2;
660         
661         TimeAxisView::show_timestretch (start, end);
662
663         hide_timestretch ();
664
665 #if 0   
666         if (ts.empty()) {
667                 return;
668         }
669
670
671         /* check that the time selection was made in our route, or our edit group.
672            remember that edit_group() == 0 implies the route is *not* in a edit group.
673         */
674
675         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->edit_group()))) {
676                 /* this doesn't apply to us */
677                 return;
678         }
679
680         /* ignore it if our edit group is not active */
681         
682         if ((ts.track != this) && _route->edit_group() && !_route->edit_group()->is_active()) {
683                 return;
684         }
685 #endif
686
687         if (timestretch_rect == 0) {
688                 timestretch_rect = new SimpleRect (*canvas_display);
689                 timestretch_rect->property_x1() =  0.0;
690                 timestretch_rect->property_y1() =  0.0;
691                 timestretch_rect->property_x2() =  0.0;
692                 timestretch_rect->property_y2() =  0.0;
693                 timestretch_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
694                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
695         }
696
697         timestretch_rect->show ();
698         timestretch_rect->raise_to_top ();
699
700         x1 = start / editor.get_current_zoom();
701         x2 = (end - 1) / editor.get_current_zoom();
702         y2 = current_height() - 2;
703         
704         timestretch_rect->property_x1() = x1;
705         timestretch_rect->property_y1() = 1.0;
706         timestretch_rect->property_x2() = x2;
707         timestretch_rect->property_y2() = y2;
708 }
709
710 void
711 RouteTimeAxisView::hide_timestretch ()
712 {
713         TimeAxisView::hide_timestretch ();
714
715         if (timestretch_rect) {
716                 timestretch_rect->hide ();
717         }
718 }
719
720 void
721 RouteTimeAxisView::show_selection (TimeSelection& ts)
722 {
723
724 #if 0
725         /* ignore it if our edit group is not active or if the selection was started
726            in some other track or edit group (remember that edit_group() == 0 means
727            that the track is not in an edit group).
728         */
729
730         if (((ts.track != this && !is_child (ts.track)) && _route->edit_group() && !_route->edit_group()->is_active()) ||
731             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->edit_group())))) {
732                 hide_selection ();
733                 return;
734         }
735 #endif
736
737         TimeAxisView::show_selection (ts);
738 }
739
740 void
741 RouteTimeAxisView::set_height (uint32_t h)
742 {
743         int gmlen = h - 5;
744         bool height_changed = (height == 0) || (h != height);
745         gm.get_level_meter().setup_meters (gmlen);
746
747         TimeAxisView::set_height (h);
748
749         ensure_xml_node ();
750
751         if (_view) {
752                 _view->set_height ((double) current_height());
753         }
754
755         char buf[32];
756         snprintf (buf, sizeof (buf), "%u", height);
757         xml_node->add_property ("height", buf);
758
759         if (height >= hNormal) {
760                 reset_meter();
761                 show_name_entry ();
762                 hide_name_label ();
763
764                 gm.get_gain_slider().show();
765                 mute_button->show();
766                 solo_button->show();
767                 if (rec_enable_button)
768                         rec_enable_button->show();
769
770                 edit_group_button.show();
771                 hide_button.show();
772                 visual_button.show();
773                 size_button.show();
774                 automation_button.show();
775                 
776                 if (is_track() && track()->mode() == ARDOUR::Normal) {
777                         playlist_button.show();
778                 }
779
780         } else if (height >= hSmaller) {
781
782                 reset_meter();
783                 show_name_entry ();
784                 hide_name_label ();
785
786                 gm.get_gain_slider().hide();
787                 mute_button->show();
788                 solo_button->show();
789                 if (rec_enable_button)
790                         rec_enable_button->show();
791
792                 edit_group_button.hide ();
793                 hide_button.hide ();
794                 visual_button.hide ();
795                 size_button.hide ();
796                 automation_button.hide ();
797                 
798                 if (is_track() && track()->mode() == ARDOUR::Normal) {
799                         playlist_button.hide ();
800                 }
801
802         } else {
803
804                 hide_name_entry ();
805                 show_name_label ();
806                 
807                 gm.get_gain_slider().hide();
808                 mute_button->hide();
809                 solo_button->hide();
810                 if (rec_enable_button)
811                         rec_enable_button->hide();
812
813                 edit_group_button.hide ();
814                 hide_button.hide ();
815                 visual_button.hide ();
816                 size_button.hide ();
817                 automation_button.hide ();
818                 playlist_button.hide ();
819                 name_label.set_text (_route->name());
820         }
821
822         if (height_changed) {
823                 /* only emit the signal if the height really changed */
824                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
825         }
826 }
827
828 void
829 RouteTimeAxisView::select_track_color ()
830 {
831         if (RouteUI::choose_color ()) {
832
833                 if (_view) {
834                         _view->apply_color (_color, StreamView::RegionColor);
835                 }
836         }
837 }
838
839 void
840 RouteTimeAxisView::reset_samples_per_unit ()
841 {
842         set_samples_per_unit (editor.get_current_zoom());
843 }
844
845 void
846 RouteTimeAxisView::set_samples_per_unit (double spu)
847 {
848         double speed = 1.0;
849
850         if (get_diskstream() != 0) {
851                 speed = get_diskstream()->speed();
852         }
853         
854         if (_view) {
855                 _view->set_samples_per_unit (spu * speed);
856         }
857
858         TimeAxisView::set_samples_per_unit (spu * speed);
859 }
860
861 void
862 RouteTimeAxisView::align_style_changed ()
863 {
864         switch (get_diskstream()->alignment_style()) {
865         case ExistingMaterial:
866                 if (!align_existing_item->get_active()) {
867                         align_existing_item->set_active();
868                 }
869                 break;
870         case CaptureTime:
871                 if (!align_capture_item->get_active()) {
872                         align_capture_item->set_active();
873                 }
874                 break;
875         }
876 }
877
878 void
879 RouteTimeAxisView::set_align_style (AlignStyle style)
880 {
881         RadioMenuItem* item;
882
883         switch (style) {
884         case ExistingMaterial:
885                 item = align_existing_item;
886                 break;
887         case CaptureTime:
888                 item = align_capture_item;
889                 break;
890         default:
891                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
892                 /*NOTREACHED*/
893                 return;
894         }
895
896         if (item->get_active()) {
897                 get_diskstream()->set_align_style (style);
898         }
899 }
900
901 void
902 RouteTimeAxisView::rename_current_playlist ()
903 {
904         ArdourPrompter prompter (true);
905         string name;
906
907         boost::shared_ptr<Diskstream> ds = get_diskstream();
908         if (!ds || ds->destructive())
909                 return;
910
911         boost::shared_ptr<Playlist> pl = ds->playlist();
912         if (!pl)
913                 return;
914
915         prompter.set_prompt (_("Name for playlist"));
916         prompter.set_initial_text (pl->name());
917         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
918         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
919
920         switch (prompter.run ()) {
921         case Gtk::RESPONSE_ACCEPT:
922                 prompter.get_result (name);
923                 if (name.length()) {
924                         pl->set_name (name);
925                 }
926                 break;
927
928         default:
929                 break;
930         }
931 }
932
933 void
934 RouteTimeAxisView::use_copy_playlist (bool prompt)
935 {
936         string name;
937         
938         boost::shared_ptr<Diskstream> ds = get_diskstream();
939         if (!ds || ds->destructive())
940                 return;
941
942         boost::shared_ptr<const Playlist> pl = ds->playlist();
943         if (!pl)
944                 return;
945
946         name = pl->name();
947
948         do {
949                 name = Playlist::bump_name (name, _session);
950         } while (_session.playlist_by_name(name));
951
952         // TODO: The prompter "new" button should be de-activated if the user
953         // specifies a playlist name which already exists in the session.
954
955         if (prompt) {
956
957                 ArdourPrompter prompter (true);
958                 
959                 prompter.set_prompt (_("Name for Playlist"));
960                 prompter.set_initial_text (name);
961                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
962                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
963                 prompter.show_all ();
964                 
965                 switch (prompter.run ()) {
966                 case Gtk::RESPONSE_ACCEPT:
967                         prompter.get_result (name);
968                         break;
969                         
970                 default:
971                         return;
972                 }
973         }
974
975         if (name.length()) {
976                 ds->use_copy_playlist ();
977                 ds->playlist()->set_name (name);
978         }
979 }
980
981 void
982 RouteTimeAxisView::use_new_playlist (bool prompt)
983 {
984         string name;
985         
986         boost::shared_ptr<Diskstream> ds = get_diskstream();
987         if (!ds || ds->destructive())
988                 return;
989
990         boost::shared_ptr<const Playlist> pl = ds->playlist();
991         if (!pl)
992                 return;
993
994         name = pl->name();
995
996         do {
997                 name = Playlist::bump_name (name, _session);
998         } while (_session.playlist_by_name(name));
999
1000
1001         if (prompt) {
1002                 
1003                 ArdourPrompter prompter (true);
1004                 
1005                 prompter.set_prompt (_("Name for Playlist"));
1006                 prompter.set_initial_text (name);
1007                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1008                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1009
1010                 switch (prompter.run ()) {
1011                 case Gtk::RESPONSE_ACCEPT:
1012                         prompter.get_result (name);
1013                         break;
1014                         
1015                 default:
1016                         return;
1017                 }
1018         }
1019
1020         if (name.length()) {
1021                 ds->use_new_playlist ();
1022                 ds->playlist()->set_name (name);
1023         }
1024 }
1025
1026 void
1027 RouteTimeAxisView::clear_playlist ()
1028 {
1029         boost::shared_ptr<Diskstream> ds = get_diskstream();
1030         if (!ds || ds->destructive())
1031                 return;
1032
1033         boost::shared_ptr<Playlist> pl = ds->playlist();
1034         if (!pl)
1035                 return;
1036
1037         editor.clear_playlist (pl);
1038 }
1039
1040 void
1041 RouteTimeAxisView::speed_changed ()
1042 {
1043         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
1044 }
1045
1046 void
1047 RouteTimeAxisView::diskstream_changed ()
1048 {
1049         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::update_diskstream_display));
1050 }       
1051
1052 void
1053 RouteTimeAxisView::update_diskstream_display ()
1054 {
1055         if (!get_diskstream()) // bus
1056                 return;
1057
1058         set_playlist (get_diskstream()->playlist());
1059         map_frozen ();
1060 }       
1061
1062 void
1063 RouteTimeAxisView::selection_click (GdkEventButton* ev)
1064 {
1065         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier))) {
1066
1067                 /* special case: select/deselect all tracks */
1068                 if (editor.get_selection().selected (this)) {
1069                         editor.get_selection().clear_tracks ();
1070                 } else {
1071                         editor.select_all_tracks ();
1072                 }
1073
1074                 return;
1075         } 
1076
1077         PublicEditor::TrackViewList* tracks = editor.get_valid_views (this, _route->edit_group());
1078
1079         switch (Keyboard::selection_type (ev->state)) {
1080         case Selection::Toggle:
1081                 editor.get_selection().toggle (*tracks);
1082                 break;
1083                 
1084         case Selection::Set:
1085                 editor.get_selection().set (*tracks);
1086                 break;
1087
1088         case Selection::Extend:
1089                 if (tracks->size() > 1) {
1090                         /* add each one, do not "extend" */
1091                         editor.get_selection().add (*tracks);
1092                 } else {
1093                         /* extend to the single track */
1094                         editor.extend_selection_to_track (*tracks->front());
1095                 }
1096                 break;
1097
1098         case Selection::Add:
1099                 editor.get_selection().add (*tracks);
1100                 break;
1101         }
1102
1103         delete tracks;
1104 }
1105
1106 void
1107 RouteTimeAxisView::set_selected_points (PointSelection& points)
1108 {
1109         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1110                 (*i)->set_selected_points (points);
1111         }
1112 }
1113
1114 void
1115 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1116 {
1117         if (_view) {
1118                 _view->set_selected_regionviews (regions);
1119         }
1120 }
1121
1122 /** Add the selectable things that we have to a list.
1123  * @param results List to add things to.
1124  */
1125 void
1126 RouteTimeAxisView::get_selectables (nframes_t start, nframes_t end, double top, double bot, list<Selectable*>& results)
1127 {
1128         double speed = 1.0;
1129         
1130         if (get_diskstream() != 0) {
1131                 speed = get_diskstream()->speed();
1132         }
1133         
1134         nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
1135         nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
1136
1137         if ((_view && ((top < 0.0 && bot < 0.0))) || touched (top, bot)) {
1138                 _view->get_selectables (start_adjusted, end_adjusted, results);
1139         }
1140
1141         /* pick up visible automation tracks */
1142         
1143         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1144                 if (!(*i)->hidden()) {
1145                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1146                 }
1147         }
1148 }
1149
1150 void
1151 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1152 {
1153         if (_view) {
1154                 _view->get_inverted_selectables (sel, results);
1155         }
1156
1157         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1158                 if (!(*i)->hidden()) {
1159                         (*i)->get_inverted_selectables (sel, results);
1160                 }
1161         }
1162
1163         return;
1164 }
1165
1166 bool
1167 RouteTimeAxisView::show_automation(Parameter param)
1168 {
1169         return (_show_automation.find(param) != _show_automation.end());
1170 }
1171
1172 /** Retuns NULL if track for \a param doesn't exist.
1173  */
1174 RouteTimeAxisView::RouteAutomationNode*
1175 RouteTimeAxisView::automation_track(Parameter param)
1176 {
1177         map<ARDOUR::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.find(param);
1178
1179         if (i != _automation_tracks.end())
1180                 return i->second;
1181         else
1182                 return NULL;
1183 }
1184
1185 /** Shorthand for GainAutomation, etc.
1186  */     
1187 RouteTimeAxisView::RouteAutomationNode*
1188 RouteTimeAxisView::automation_track(AutomationType type)
1189 {
1190         return automation_track(Parameter(type));
1191 }
1192
1193 RouteGroup*
1194 RouteTimeAxisView::edit_group() const
1195 {
1196         return _route->edit_group();
1197 }
1198
1199 string
1200 RouteTimeAxisView::name() const
1201 {
1202         return _route->name();
1203 }
1204
1205 boost::shared_ptr<Playlist>
1206 RouteTimeAxisView::playlist () const 
1207 {
1208         boost::shared_ptr<Diskstream> ds;
1209
1210         if ((ds = get_diskstream()) != 0) {
1211                 return ds->playlist(); 
1212         } else {
1213                 return boost::shared_ptr<Playlist> ();
1214         }
1215 }
1216
1217 void
1218 RouteTimeAxisView::name_entry_changed ()
1219 {
1220         string x;
1221
1222         x = name_entry.get_text ();
1223         
1224         if (x == _route->name()) {
1225                 return;
1226         }
1227
1228         strip_whitespace_edges(x);
1229
1230         if (x.length() == 0) {
1231                 name_entry.set_text (_route->name());
1232                 return;
1233         }
1234
1235         if (_session.route_name_unique (x)) {
1236                 _route->set_name (x);
1237         } else {
1238                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1239                 name_entry.set_text (_route->name());
1240         }
1241 }
1242
1243 void
1244 RouteTimeAxisView::visual_click ()
1245 {
1246         popup_display_menu (0);
1247 }
1248
1249 void
1250 RouteTimeAxisView::hide_click ()
1251 {
1252         // LAME fix for hide_button refresh fix
1253         hide_button.set_sensitive(false);
1254         
1255         editor.hide_track_in_display (*this);
1256         
1257         hide_button.set_sensitive(true);
1258 }
1259
1260 boost::shared_ptr<Region>
1261 RouteTimeAxisView::find_next_region (nframes_t pos, RegionPoint point, int32_t dir)
1262 {
1263         boost::shared_ptr<Diskstream> stream;
1264         boost::shared_ptr<Playlist> playlist;
1265
1266         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1267                 return playlist->find_next_region (pos, point, dir);
1268         }
1269
1270         return boost::shared_ptr<Region> ();
1271 }
1272
1273 nframes64_t 
1274 RouteTimeAxisView::find_next_region_boundary (nframes64_t pos, int32_t dir)
1275 {
1276         boost::shared_ptr<Diskstream> stream;
1277         boost::shared_ptr<Playlist> playlist;
1278
1279         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1280                 return playlist->find_next_region_boundary (pos, dir);
1281         }
1282
1283         return -1;
1284 }
1285
1286 bool
1287 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1288 {
1289         boost::shared_ptr<Playlist> what_we_got;
1290         boost::shared_ptr<Diskstream> ds = get_diskstream();
1291         boost::shared_ptr<Playlist> playlist;
1292         bool ret = false;
1293
1294         if (ds == 0) {
1295                 /* route is a bus, not a track */
1296                 return false;
1297         }
1298
1299         playlist = ds->playlist();
1300
1301         TimeSelection time (selection.time);
1302         float speed = ds->speed();
1303         if (speed != 1.0f) {
1304                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1305                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1306                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1307                 }
1308         }
1309         
1310         XMLNode &before = playlist->get_state();
1311         switch (op) {
1312         case Cut:
1313                 if ((what_we_got = playlist->cut (time)) != 0) {
1314                         editor.get_cut_buffer().add (what_we_got);
1315                         _session.add_command( new MementoCommand<Playlist>(*playlist.get(), &before, &playlist->get_state()));
1316                         ret = true;
1317                 }
1318                 break;
1319         case Copy:
1320                 if ((what_we_got = playlist->copy (time)) != 0) {
1321                         editor.get_cut_buffer().add (what_we_got);
1322                 }
1323                 break;
1324
1325         case Clear:
1326                 if ((what_we_got = playlist->cut (time)) != 0) {
1327                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1328                         what_we_got->release ();
1329                         ret = true;
1330                 }
1331                 break;
1332         }
1333
1334         return ret;
1335 }
1336
1337 bool
1338 RouteTimeAxisView::paste (nframes_t pos, float times, Selection& selection, size_t nth)
1339 {
1340         if (!is_track()) {
1341                 return false;
1342         }
1343
1344         boost::shared_ptr<Playlist> playlist = get_diskstream()->playlist();
1345         PlaylistSelection::iterator p;
1346         
1347         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1348
1349         if (p == selection.playlists.end()) {
1350                 return false;
1351         }
1352
1353         if (get_diskstream()->speed() != 1.0f)
1354                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1355         
1356         XMLNode &before = playlist->get_state();
1357         playlist->paste (*p, pos, times);
1358         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1359
1360         return true;
1361 }
1362
1363
1364 TimeAxisView::Children
1365 RouteTimeAxisView::get_child_list()
1366 {
1367         TimeAxisView::Children redirect_children;
1368         
1369         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1370                 if (!(*i)->hidden()) {
1371                         redirect_children.push_back(*i);
1372                 }
1373         }
1374         return redirect_children;
1375 }
1376
1377
1378 void
1379 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1380 {
1381         using namespace Menu_Helpers;
1382
1383         if (!menu || !is_track()) {
1384                 return;
1385         }
1386
1387         MenuList& playlist_items = menu->items();
1388         menu->set_name ("ArdourContextMenu");
1389         playlist_items.clear();
1390
1391         if (playlist_menu) {
1392                 delete playlist_menu;
1393         }
1394
1395         playlist_menu = new Menu;
1396         playlist_menu->set_name ("ArdourContextMenu");
1397
1398         vector<boost::shared_ptr<Playlist> > playlists;
1399         boost::shared_ptr<Diskstream> ds = get_diskstream();
1400         RadioMenuItem::Group playlist_group;
1401
1402         _session.get_playlists (playlists);
1403         
1404         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1405
1406                 if ((*i)->get_orig_diskstream_id() == ds->id()) {
1407                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist),
1408                                                                                                      boost::weak_ptr<Playlist> (*i))));
1409
1410                         if (ds->playlist()->id() == (*i)->id()) {
1411                                 static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1412                         }
1413                 } else if (ds->playlist()->id() == (*i)->id()) {
1414                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist), 
1415                                                                                                      boost::weak_ptr<Playlist>(*i))));
1416                         static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1417                         
1418                 }
1419         }
1420
1421         playlist_items.push_back (SeparatorElem());
1422         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1423         playlist_items.push_back (SeparatorElem());
1424
1425         playlist_items.push_back (MenuElem (_("New"), bind(mem_fun(editor, &PublicEditor::new_playlists), this)));
1426         playlist_items.push_back (MenuElem (_("New Copy"), bind(mem_fun(editor, &PublicEditor::copy_playlists), this)));
1427         playlist_items.push_back (SeparatorElem());
1428         playlist_items.push_back (MenuElem (_("Clear Current"), bind(mem_fun(editor, &PublicEditor::clear_playlists), this)));
1429         playlist_items.push_back (SeparatorElem());
1430
1431         playlist_items.push_back (MenuElem(_("Select from all ..."), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1432 }
1433
1434 void
1435 RouteTimeAxisView::use_playlist (boost::weak_ptr<Playlist> wpl)
1436 {
1437         assert (is_track());
1438
1439         boost::shared_ptr<Playlist> pl (wpl.lock());
1440
1441         if (!pl) {
1442                 return;
1443         }
1444
1445         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
1446         
1447         if (apl) {
1448                 get_diskstream()->use_playlist (apl);
1449         }
1450 }
1451
1452 void
1453 RouteTimeAxisView::show_playlist_selector ()
1454 {
1455         editor.playlist_selector().show_for (this);
1456 }
1457
1458 void
1459 RouteTimeAxisView::map_frozen ()
1460 {
1461         if (!is_track()) {
1462                 return;
1463         }
1464
1465         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1466
1467         switch (track()->freeze_state()) {
1468         case Track::Frozen:
1469                 playlist_button.set_sensitive (false);
1470                 rec_enable_button->set_sensitive (false);
1471                 break;
1472         default:
1473                 playlist_button.set_sensitive (true);
1474                 rec_enable_button->set_sensitive (true);
1475                 break;
1476         }
1477 }
1478
1479 void
1480 RouteTimeAxisView::color_handler ()
1481 {
1482         //case cTimeStretchOutline:
1483         if (timestretch_rect) {
1484                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
1485         }
1486         //case cTimeStretchFill:
1487         if (timestretch_rect) {
1488                 timestretch_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
1489         }
1490
1491         reset_meter();
1492 }
1493
1494 void
1495 RouteTimeAxisView::toggle_automation_track (Parameter param)
1496 {
1497         RouteAutomationNode* node = automation_track(param);
1498
1499         if (!node)
1500                 return;
1501
1502         bool showit = node->menu_item->get_active();
1503
1504         if (showit != node->track->marked_for_display()) {
1505                 if (showit) {
1506                         node->track->set_marked_for_display (true);
1507                         node->track->canvas_display->show();
1508                         node->track->get_state_node()->add_property ("shown", X_("yes"));
1509                 } else {
1510                         node->track->set_marked_for_display (false);
1511                         node->track->hide ();
1512                         node->track->get_state_node()->add_property ("shown", X_("no"));
1513                 }
1514
1515                 /* now trigger a redisplay */
1516                 
1517                 if (!no_redraw) {
1518                          _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
1519                 }
1520         }
1521 }
1522
1523 void
1524 RouteTimeAxisView::automation_track_hidden (Parameter param)
1525 {
1526         RouteAutomationNode* ran = automation_track(param);
1527         if (!ran) {
1528                 return;
1529         }
1530
1531         _show_automation.erase(param);
1532         ran->track->get_state_node()->add_property (X_("shown"), X_("no"));
1533
1534         if (ran->menu_item && !_hidden) {
1535                 ran->menu_item->set_active (false);
1536         }
1537
1538          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1539 }
1540
1541
1542 void
1543 RouteTimeAxisView::show_all_automation ()
1544 {
1545         no_redraw = true;
1546         
1547         /* Show our automation */
1548
1549         map<ARDOUR::Parameter, RouteAutomationNode*>::iterator i;
1550         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1551                 i->second->track->set_marked_for_display (true);
1552                 i->second->track->canvas_display->show();
1553                 i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1554                 i->second->menu_item->set_active(true);
1555         }
1556
1557
1558         /* Show processor automation */
1559
1560         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1561                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1562                         if ((*ii)->view == 0) {
1563                                 add_processor_automation_curve ((*i)->processor, (*ii)->what);
1564                         } 
1565
1566                         (*ii)->menu_item->set_active (true);
1567                 }
1568         }
1569
1570         no_redraw = false;
1571
1572         /* Redraw */
1573
1574          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1575 }
1576
1577 void
1578 RouteTimeAxisView::show_existing_automation ()
1579 {
1580         no_redraw = true;
1581         
1582         /* Show our automation */
1583
1584         map<ARDOUR::Parameter, RouteAutomationNode*>::iterator i;
1585         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1586                 if (i->second->track->line() && i->second->track->line()->npoints() > 0) {
1587                         i->second->track->set_marked_for_display (true);
1588                         i->second->track->canvas_display->show();
1589                         i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1590                         i->second->menu_item->set_active(true);
1591                 }
1592         }
1593
1594
1595         /* Show processor automation */
1596
1597         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1598                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1599                         if ((*ii)->view != 0 && (*i)->processor->control((*ii)->what)->list()->size() > 0) {
1600                                 (*ii)->menu_item->set_active (true);
1601                         }
1602                 }
1603         }
1604
1605         no_redraw = false;
1606         
1607         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1608 }
1609
1610 void
1611 RouteTimeAxisView::hide_all_automation ()
1612 {
1613         no_redraw = true;
1614
1615         /* Hide our automation */
1616
1617         for (map<ARDOUR::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1618                 i->second->track->set_marked_for_display (false);
1619                 i->second->track->hide ();
1620                 i->second->track->get_state_node()->add_property ("shown", X_("no"));
1621                 i->second->menu_item->set_active (false);
1622         }
1623
1624         /* Hide processor automation */
1625
1626         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1627                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1628                         (*ii)->menu_item->set_active (false);
1629                 }
1630         }
1631
1632         _show_automation.clear();
1633
1634         no_redraw = false;
1635          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1636 }
1637
1638
1639 void
1640 RouteTimeAxisView::region_view_added (RegionView* rv)
1641 {
1642         /* XXX need to find out if automation children have automationstreamviews. If yes, no ghosts */
1643         if(is_audio_track()) {
1644                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1645                         boost::shared_ptr<AutomationTimeAxisView> atv;
1646                         
1647                         if ((atv = boost::dynamic_pointer_cast<AutomationTimeAxisView> (*i)) != 0) {
1648                                 atv->add_ghost(rv);
1649                         }
1650                 }
1651         }
1652
1653         for (UnderlayMirrorList::iterator i = _underlay_mirrors.begin(); i != _underlay_mirrors.end(); ++i) {
1654                 (*i)->add_ghost(rv);
1655         }
1656 }
1657
1658 RouteTimeAxisView::ProcessorAutomationInfo::~ProcessorAutomationInfo ()
1659 {
1660         for (vector<ProcessorAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1661                 delete *i;
1662         }
1663 }
1664
1665
1666 RouteTimeAxisView::ProcessorAutomationNode::~ProcessorAutomationNode ()
1667 {
1668         parent.remove_processor_automation_node (this);
1669 }
1670
1671 void
1672 RouteTimeAxisView::remove_processor_automation_node (ProcessorAutomationNode* pan)
1673 {
1674         if (pan->view) {
1675                 remove_child (pan->view);
1676         }
1677 }
1678
1679 RouteTimeAxisView::ProcessorAutomationNode*
1680 RouteTimeAxisView::find_processor_automation_node (boost::shared_ptr<Processor> processor, Parameter what)
1681 {
1682         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1683
1684                 if ((*i)->processor == processor) {
1685
1686                         for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1687                                 if ((*ii)->what == what) {
1688                                         return *ii;
1689                                 }
1690                         }
1691                 }
1692         }
1693
1694         return 0;
1695 }
1696
1697 static string 
1698 legalize_for_xml_node (string str)
1699 {
1700         string::size_type pos;
1701         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=:";
1702         string legal;
1703
1704         legal = str;
1705         pos = 0;
1706
1707         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1708                 legal.replace (pos, 1, "_");
1709                 pos += 1;
1710         }
1711
1712         return legal;
1713 }
1714
1715
1716 void
1717 RouteTimeAxisView::add_processor_automation_curve (boost::shared_ptr<Processor> processor, Parameter what)
1718 {
1719         string name;
1720         ProcessorAutomationNode* pan;
1721
1722         if ((pan = find_processor_automation_node (processor, what)) == 0) {
1723                 fatal << _("programming error: ")
1724                       << string_compose (X_("processor automation curve for %1:%2 not registered with track!"),
1725                                   processor->name(), what)
1726                       << endmsg;
1727                 /*NOTREACHED*/
1728                 return;
1729         }
1730
1731         if (pan->view) {
1732                 return;
1733         }
1734
1735         name = processor->describe_parameter (what);
1736
1737         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1738
1739         /* FIXME: ew */
1740
1741         char state_name[256];
1742         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (processor->name()).c_str(), what.id());
1743
1744         boost::shared_ptr<AutomationControl> control
1745                         = boost::dynamic_pointer_cast<AutomationControl>(processor->control(what, true));
1746
1747         pan->view = boost::shared_ptr<AutomationTimeAxisView>(
1748                         new AutomationTimeAxisView (_session, _route, processor, control,
1749                                 editor, *this, false, parent_canvas, name, state_name));
1750
1751         pan->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_automation_track_hidden), pan, processor));
1752
1753         if (!pan->view->marked_for_display()) {
1754                 pan->view->hide ();
1755         } else {
1756                 pan->menu_item->set_active (true);
1757         }
1758
1759         add_child (pan->view);
1760
1761         if (_view) {
1762                 _view->foreach_regionview (mem_fun(*pan->view.get(), &TimeAxisView::add_ghost));
1763         }
1764
1765         processor->mark_automation_visible (what, true);
1766 }
1767
1768 void
1769 RouteTimeAxisView::processor_automation_track_hidden (RouteTimeAxisView::ProcessorAutomationNode* pan, boost::shared_ptr<Processor> i)
1770 {
1771         if (!_hidden) {
1772                 pan->menu_item->set_active (false);
1773         }
1774
1775         i->mark_automation_visible (pan->what, false);
1776
1777          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1778 }
1779
1780 void
1781 RouteTimeAxisView::add_existing_processor_automation_curves (boost::shared_ptr<Processor> processor)
1782 {
1783         set<Parameter> s;
1784         boost::shared_ptr<AutomationLine> al;
1785
1786         processor->what_has_visible_data (s);
1787
1788         for (set<Parameter>::iterator i = s.begin(); i != s.end(); ++i) {
1789                 
1790                 if ((al = find_processor_automation_curve (processor, *i)) != 0) {
1791                         al->queue_reset ();
1792                 } else {
1793                         add_processor_automation_curve (processor, (*i));
1794                 }
1795         }
1796 }
1797
1798 void
1799 RouteTimeAxisView::add_automation_child(Parameter param, boost::shared_ptr<AutomationTimeAxisView> track, bool show)
1800 {
1801         using namespace Menu_Helpers;
1802
1803         XMLProperty* prop;
1804         XMLNode* node;
1805
1806         add_child (track);
1807
1808         track->Hiding.connect (bind (mem_fun (*this, &RouteTimeAxisView::automation_track_hidden), param));
1809
1810         bool hideit = (!show);
1811
1812         if ((node = track->get_state_node()) != 0) {
1813                 if  ((prop = node->property ("shown")) != 0) {
1814                         if (prop->value() == "yes") {
1815                                 hideit = false;
1816                         }
1817                 } 
1818         }
1819
1820         _automation_tracks.insert(std::make_pair(param, new RouteAutomationNode(param, NULL, track)));
1821
1822         if (hideit) {
1823                 track->hide ();
1824         } else {
1825                 _show_automation.insert (param);
1826
1827
1828                 if (!no_redraw) {
1829                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1830                 }
1831         }
1832
1833         build_display_menu();
1834 }
1835
1836
1837 void
1838 RouteTimeAxisView::add_processor_to_subplugin_menu (boost::shared_ptr<Processor> processor)
1839 {
1840         using namespace Menu_Helpers;
1841         ProcessorAutomationInfo *rai;
1842         list<ProcessorAutomationInfo*>::iterator x;
1843         
1844         const std::set<Parameter>& automatable = processor->what_can_be_automated ();
1845         std::set<Parameter> has_visible_automation;
1846
1847         processor->what_has_visible_data(has_visible_automation);
1848
1849         if (automatable.empty()) {
1850                 return;
1851         }
1852
1853         for (x = processor_automation.begin(); x != processor_automation.end(); ++x) {
1854                 if ((*x)->processor == processor) {
1855                         break;
1856                 }
1857         }
1858
1859         if (x == processor_automation.end()) {
1860
1861                 rai = new ProcessorAutomationInfo (processor);
1862                 processor_automation.push_back (rai);
1863
1864         } else {
1865
1866                 rai = *x;
1867
1868         }
1869
1870         /* any older menu was deleted at the top of processors_changed()
1871            when we cleared the subplugin menu.
1872         */
1873
1874         rai->menu = manage (new Menu);
1875         MenuList& items = rai->menu->items();
1876         rai->menu->set_name ("ArdourContextMenu");
1877
1878         items.clear ();
1879
1880         for (std::set<Parameter>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
1881
1882                 ProcessorAutomationNode* pan;
1883                 CheckMenuItem* mitem;
1884                 
1885                 string name = processor->describe_parameter (*i);
1886                 
1887                 items.push_back (CheckMenuElem (name));
1888                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
1889
1890                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
1891                         mitem->set_active(true);
1892                 }
1893
1894                 if ((pan = find_processor_automation_node (processor, *i)) == 0) {
1895
1896                         /* new item */
1897                         
1898                         pan = new ProcessorAutomationNode (*i, mitem, *this);
1899                         
1900                         rai->lines.push_back (pan);
1901
1902                 } else {
1903
1904                         pan->menu_item = mitem;
1905
1906                 }
1907
1908                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_menu_item_toggled), rai, pan));
1909         }
1910
1911         /* add the menu for this processor, because the subplugin
1912            menu is always cleared at the top of processors_changed().
1913            this is the result of some poor design in gtkmm and/or
1914            GTK+.
1915         */
1916
1917         subplugin_menu.items().push_back (MenuElem (processor->name(), *rai->menu));
1918         rai->valid = true;
1919 }
1920
1921 void
1922 RouteTimeAxisView::processor_menu_item_toggled (RouteTimeAxisView::ProcessorAutomationInfo* rai,
1923                                                RouteTimeAxisView::ProcessorAutomationNode* pan)
1924 {
1925         bool showit = pan->menu_item->get_active();
1926         bool redraw = false;
1927
1928         if (pan->view == 0 && showit) {
1929                 add_processor_automation_curve (rai->processor, pan->what);
1930                 redraw = true;
1931         }
1932
1933         if (pan->view && showit != pan->view->marked_for_display()) {
1934
1935                 if (showit) {
1936                         pan->view->set_marked_for_display (true);
1937                         pan->view->canvas_display->show();
1938                 } else {
1939                         rai->processor->mark_automation_visible (pan->what, true);
1940                         pan->view->set_marked_for_display (false);
1941                         pan->view->hide ();
1942                 }
1943
1944                 redraw = true;
1945
1946         }
1947
1948         if (redraw && !no_redraw) {
1949
1950                 /* now trigger a redisplay */
1951                 
1952                  _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1953
1954         }
1955 }
1956
1957 void
1958 RouteTimeAxisView::processors_changed ()
1959 {
1960         using namespace Menu_Helpers;
1961         
1962         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1963                 (*i)->valid = false;
1964         }
1965
1966         subplugin_menu.items().clear ();
1967
1968         _route->foreach_processor (this, &RouteTimeAxisView::add_processor_to_subplugin_menu);
1969         _route->foreach_processor (this, &RouteTimeAxisView::add_existing_processor_automation_curves);
1970
1971         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ) {
1972
1973                 list<ProcessorAutomationInfo*>::iterator tmp;
1974
1975                 tmp = i;
1976                 ++tmp;
1977
1978                 if (!(*i)->valid) {
1979
1980                         delete *i;
1981                         processor_automation.erase (i);
1982
1983                 } 
1984
1985                 i = tmp;
1986         }
1987
1988         /* change in visibility was possible */
1989
1990         _route->gui_changed ("visible_tracks", this);
1991 }
1992
1993 boost::shared_ptr<AutomationLine>
1994 RouteTimeAxisView::find_processor_automation_curve (boost::shared_ptr<Processor> processor, Parameter what)
1995 {
1996         ProcessorAutomationNode* pan;
1997
1998         if ((pan = find_processor_automation_node (processor, what)) != 0) {
1999                 if (pan->view) {
2000                         pan->view->line();
2001                 } 
2002         }
2003
2004         return boost::shared_ptr<AutomationLine>();
2005 }
2006
2007 void
2008 RouteTimeAxisView::reset_processor_automation_curves ()
2009 {
2010         for (ProcessorAutomationCurves::iterator i = processor_automation_curves.begin(); i != processor_automation_curves.end(); ++i) {
2011                 (*i)->reset();
2012         }
2013 }
2014
2015 void
2016 RouteTimeAxisView::update_rec_display ()
2017 {
2018         RouteUI::update_rec_display ();
2019         name_entry.set_sensitive (!_route->record_enabled());
2020 }
2021                 
2022 void
2023 RouteTimeAxisView::set_layer_display (LayerDisplay d)
2024 {
2025         _view->set_layer_display (d);
2026 }
2027         
2028
2029 boost::shared_ptr<AutomationTimeAxisView>
2030 RouteTimeAxisView::automation_child(ARDOUR::Parameter param)
2031 {
2032         AutomationTracks::iterator i = _automation_tracks.find(param);
2033         if (i != _automation_tracks.end())
2034                 return i->second->track;
2035         else
2036                 return boost::shared_ptr<AutomationTimeAxisView>();
2037 }
2038
2039 void
2040 RouteTimeAxisView::fast_update ()
2041 {
2042         gm.get_level_meter().update_meters ();
2043 }
2044
2045 void
2046 RouteTimeAxisView::hide_meter ()
2047 {
2048         clear_meter ();
2049         gm.get_level_meter().hide_meters ();
2050 }
2051
2052 void
2053 RouteTimeAxisView::show_meter ()
2054 {
2055         reset_meter ();
2056 }
2057
2058 void
2059 RouteTimeAxisView::reset_meter ()
2060 {
2061         if (Config->get_show_track_meters()) {
2062                 gm.get_level_meter().setup_meters (height-5);
2063         } else {
2064                 hide_meter ();
2065         }
2066 }
2067
2068 void
2069 RouteTimeAxisView::clear_meter ()
2070 {
2071         gm.get_level_meter().clear_meters ();
2072 }
2073
2074 void
2075 RouteTimeAxisView::meter_changed (void *src)
2076 {
2077         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RouteTimeAxisView::meter_changed), src));
2078         reset_meter();
2079 }
2080
2081 void
2082 RouteTimeAxisView::io_changed (IOChange change, void *src)
2083 {
2084         reset_meter ();
2085 }
2086
2087 void
2088 RouteTimeAxisView::build_underlay_menu(Gtk::Menu* parent_menu) {
2089         using namespace Menu_Helpers;
2090
2091         if(!_underlay_streams.empty()) {
2092                 MenuList& parent_items = parent_menu->items();
2093                 Menu* gs_menu = manage (new Menu);
2094                 gs_menu->set_name ("ArdourContextMenu");
2095                 MenuList& gs_items = gs_menu->items();
2096                 
2097                 parent_items.push_back (MenuElem (_("Underlays"), *gs_menu));
2098                 
2099                 for(UnderlayList::iterator it = _underlay_streams.begin(); it != _underlay_streams.end(); ++it) {
2100                         gs_items.push_back(MenuElem(string_compose(_("Remove \"%1\""), (*it)->trackview().name()),
2101                                                     bind(mem_fun(*this, &RouteTimeAxisView::remove_underlay), *it)));
2102                 }
2103         }
2104 }
2105
2106 bool
2107 RouteTimeAxisView::set_underlay_state() 
2108 {
2109         if(!underlay_xml_node) {
2110                 return false;
2111         }
2112
2113         XMLNodeList nlist = underlay_xml_node->children();
2114         XMLNodeConstIterator niter;
2115         XMLNode *child_node;
2116         
2117         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2118                 child_node = *niter;
2119
2120                 if(child_node->name() != "Underlay") {
2121                         continue;
2122                 }
2123
2124                 XMLProperty* prop = child_node->property ("id");
2125                 if(prop) {
2126                         PBD::ID id(prop->value());
2127
2128                         RouteTimeAxisView* v = editor.get_route_view_by_id(id);
2129
2130                         if(v) {
2131                                 add_underlay(v->view(), false);
2132                         }
2133                 }
2134         }
2135
2136         return false;
2137 }
2138
2139 void
2140 RouteTimeAxisView::add_underlay(StreamView* v, bool update_xml) 
2141 {
2142         if(!v) {
2143                 return;
2144         }
2145
2146         RouteTimeAxisView& other = v->trackview();
2147
2148         if(find(_underlay_streams.begin(), _underlay_streams.end(), v) == _underlay_streams.end()) {
2149                 if(find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this) != other._underlay_mirrors.end()) {
2150                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2151                         /*NOTREACHED*/
2152                 }
2153
2154                 _underlay_streams.push_back(v);
2155                 other._underlay_mirrors.push_back(this);
2156
2157                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::add_ghost));
2158
2159                 if(update_xml) {
2160                         if(!underlay_xml_node) {
2161                                 ensure_xml_node();
2162                                 underlay_xml_node = xml_node->add_child("Underlays");
2163                         }
2164
2165                         XMLNode* node = underlay_xml_node->add_child("Underlay");
2166                         XMLProperty* prop = node->add_property("id");
2167                         prop->set_value(v->trackview().route()->id().to_s());
2168                 }
2169         }
2170 }
2171
2172 void
2173 RouteTimeAxisView::remove_underlay(StreamView* v) 
2174 {
2175         if(!v) {
2176                 return;
2177         }
2178
2179         UnderlayList::iterator it = find(_underlay_streams.begin(), _underlay_streams.end(), v);
2180         RouteTimeAxisView& other = v->trackview();
2181
2182         if(it != _underlay_streams.end()) {
2183                 UnderlayMirrorList::iterator gm = find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this);
2184
2185                 if(gm == other._underlay_mirrors.end()) {
2186                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2187                         /*NOTREACHED*/
2188                 }
2189
2190                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::remove_ghost));
2191
2192                 _underlay_streams.erase(it);
2193                 other._underlay_mirrors.erase(gm);
2194
2195                 if(underlay_xml_node) {
2196                         underlay_xml_node->remove_nodes_and_delete("id", v->trackview().route()->id().to_s());
2197                 }
2198         }
2199 }
2200