Added take system from 2.0. Thanks to torben for the patch.
[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 <evoral/Parameter.hpp>
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 int
419 RouteTimeAxisView::set_state (const XMLNode& node)
420 {
421         TimeAxisView::set_state (node);
422
423         XMLNodeList kids = node.children();
424         XMLNodeConstIterator iter;
425         const XMLProperty* prop;
426         
427         for (iter = kids.begin(); iter != kids.end(); ++iter) {
428                 if ((*iter)->name() == AutomationTimeAxisView::state_node_name) {
429                         if ((prop = (*iter)->property ("automation-id")) != 0) {
430                                 Evoral::Parameter param = ARDOUR::EventTypeMap::instance().new_parameter(prop->value());
431                                 bool show = ((prop = (*iter)->property ("shown")) != 0) && prop->value() == "yes";
432                                 create_automation_child(param, show);
433                         } else {
434                                 warning << "Automation child has no ID" << endmsg;
435                         }
436                 }
437         }
438         return 0;
439 }
440
441 void
442 RouteTimeAxisView::build_automation_action_menu ()
443 {
444         using namespace Menu_Helpers;
445
446         automation_action_menu = manage (new Menu);
447         MenuList& automation_items = automation_action_menu->items();
448         automation_action_menu->set_name ("ArdourContextMenu");
449         
450         automation_items.push_back (MenuElem (_("Show all automation"),
451                                               mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
452
453         automation_items.push_back (MenuElem (_("Show existing automation"),
454                                               mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
455
456         automation_items.push_back (MenuElem (_("Hide all automation"),
457                                               mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
458
459         if (subplugin_menu.get_attach_widget())
460                 subplugin_menu.detach();
461
462         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
463         
464         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
465         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
466
467                 automation_items.push_back (SeparatorElem());
468
469                 if (i->second->menu_item)
470                         delete i->second->menu_item;
471
472                 automation_items.push_back(CheckMenuElem (_route->describe_parameter(i->second->param), 
473                                 bind (mem_fun(*this, &RouteTimeAxisView::toggle_automation_track), i->second->param)));
474
475                 i->second->menu_item = static_cast<Gtk::CheckMenuItem*>(&automation_items.back());
476
477                 i->second->menu_item->set_active(show_automation(i->second->param));
478                 //i->second->menu_item->set_active(false);
479         }
480 }
481
482 void
483 RouteTimeAxisView::build_display_menu ()
484 {
485         using namespace Menu_Helpers;
486
487         /* get the size menu ready */
488
489         build_size_menu ();
490
491         /* prepare it */
492
493         TimeAxisView::build_display_menu ();
494
495         /* now fill it with our stuff */
496
497         MenuList& items = display_menu->items();
498         display_menu->set_name ("ArdourContextMenu");
499         
500         items.push_back (MenuElem (_("Height"), *size_menu));
501         items.push_back (MenuElem (_("Color"), mem_fun(*this, &RouteTimeAxisView::select_track_color)));
502
503         items.push_back (SeparatorElem());
504
505         build_remote_control_menu ();
506         build_automation_action_menu ();
507
508         if (!Profile->get_sae()) {
509                 items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
510                 items.push_back (MenuElem (_("Automation"), *automation_action_menu));
511                 items.push_back (SeparatorElem());
512         }
513
514         // Hook for derived classes to add type specific stuff
515         append_extra_display_menu_items ();
516         items.push_back (SeparatorElem());
517         
518         if (is_track()) {
519
520                 Menu *layers_menu = manage(new Menu);
521                 MenuList &layers_items = layers_menu->items();
522                 layers_menu->set_name("ArdourContextMenu");
523
524                 RadioMenuItem::Group layers_group;
525
526                 layers_items.push_back(RadioMenuElem (layers_group, _("Overlaid"),
527                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Overlaid)));
528                 layers_items.push_back(RadioMenuElem (layers_group, _("Stacked"),
529                                 bind (mem_fun (*this, &RouteTimeAxisView::set_layer_display), Stacked)));
530
531                 items.push_back (MenuElem (_("Layers"), *layers_menu));
532
533                 Menu* alignment_menu = manage (new Menu);
534                 MenuList& alignment_items = alignment_menu->items();
535                 alignment_menu->set_name ("ArdourContextMenu");
536
537                 RadioMenuItem::Group align_group;
538
539                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"),
540                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
541                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
542                 if (get_diskstream()->alignment_style() == ExistingMaterial)
543                         align_existing_item->set_active();
544
545                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"),
546                                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
547                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
548                 if (get_diskstream()->alignment_style() == CaptureTime)
549                         align_capture_item->set_active();
550
551                 if (!Profile->get_sae()) {
552                         items.push_back (MenuElem (_("Alignment"), *alignment_menu));
553                         get_diskstream()->AlignmentStyleChanged.connect (mem_fun(*this, &RouteTimeAxisView::align_style_changed));
554                         
555                         RadioMenuItem::Group mode_group;
556                         items.push_back (RadioMenuElem (mode_group, _("Normal mode"),
557                                                         bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Normal)));
558                         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
559                         items.push_back (RadioMenuElem (mode_group, _("Tape mode"),
560                                                         bind (mem_fun (*this, &RouteTimeAxisView::set_track_mode), ARDOUR::Destructive)));
561                         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
562                         
563                         switch (track()->mode()) {
564                         case ARDOUR::Destructive:
565                                 destructive_track_mode_item->set_active ();
566                                 break;
567                         case ARDOUR::Normal:
568                                 normal_track_mode_item->set_active ();
569                                 break;
570                         }
571
572                 get_diskstream()->AlignmentStyleChanged.connect (
573                                 mem_fun(*this, &RouteTimeAxisView::align_style_changed));
574
575                 mode_menu = build_mode_menu();
576                 if (mode_menu)
577                         items.push_back (MenuElem (_("Mode"), *mode_menu));
578                         
579                 items.push_back (SeparatorElem());
580                 }
581         }
582
583         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
584         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
585         route_active_menu_item->set_active (_route->active());
586
587         items.push_back (SeparatorElem());
588         items.push_back (MenuElem (_("Hide"), mem_fun(*this, &RouteTimeAxisView::hide_click)));
589         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
590 }
591
592 static bool __reset_item (RadioMenuItem* item)
593 {
594         item->set_active ();
595         return false;
596 }
597
598 void
599 RouteTimeAxisView::set_track_mode (TrackMode mode)
600 {
601         RadioMenuItem* item;
602         RadioMenuItem* other_item;
603
604         switch (mode) {
605         case ARDOUR::Normal:
606                 item = normal_track_mode_item;
607                 other_item = destructive_track_mode_item;
608                 break;
609         case ARDOUR::Destructive:
610                 item = destructive_track_mode_item;
611                 other_item = normal_track_mode_item;
612                 break;
613         default:
614                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", mode) << endmsg;
615                 /*NOTREACHED*/
616                 return;
617         }
618         
619         if (item && other_item && item->get_active () && track()->mode() != mode) {
620                 _set_track_mode (track().get(), mode, other_item);
621         }
622 }
623
624 void
625 RouteTimeAxisView::_set_track_mode (Track* track, TrackMode mode, RadioMenuItem* reset_item)
626 {
627         bool needs_bounce;
628
629         if (!track->can_use_mode (mode, needs_bounce)) {
630
631                 if (!needs_bounce) {
632                         /* cannot be done */
633                         Glib::signal_idle().connect (bind (sigc::ptr_fun (__reset_item), reset_item));
634                         return;
635                 } else {
636                         cerr << "would bounce this one\n";
637                         return;
638                 }
639         }
640
641         track->set_mode (mode);
642
643         rec_enable_button->remove ();
644         switch (mode) {
645         case ARDOUR::Normal:
646                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_normal_red"))))));
647                 break;
648         case ARDOUR::Destructive:
649                 rec_enable_button->add (*(manage (new Image (::get_icon (X_("record_tape_red"))))));
650                 break;
651         }
652         rec_enable_button->show_all ();
653
654 }
655
656 void
657 RouteTimeAxisView::track_mode_changed ()
658 {
659         RadioMenuItem* item;
660         
661         switch (track()->mode()) {
662         case ARDOUR::Normal:
663                 item = normal_track_mode_item;
664                 break;
665         case ARDOUR::Destructive:
666                 item = destructive_track_mode_item;
667                 break;
668         default:
669                 fatal << string_compose (_("programming error: %1 %2"), "illegal track mode in RouteTimeAxisView::set_track_mode", track()->mode()) << endmsg;
670                 /*NOTREACHED*/
671                 return;
672         }
673
674         item->set_active ();
675 }
676
677 void
678 RouteTimeAxisView::show_timestretch (nframes_t start, nframes_t end)
679 {
680         double x1;
681         double x2;
682         double y2;
683         
684         TimeAxisView::show_timestretch (start, end);
685
686         hide_timestretch ();
687
688 #if 0   
689         if (ts.empty()) {
690                 return;
691         }
692
693
694         /* check that the time selection was made in our route, or our edit group.
695            remember that edit_group() == 0 implies the route is *not* in a edit group.
696         */
697
698         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->edit_group()))) {
699                 /* this doesn't apply to us */
700                 return;
701         }
702
703         /* ignore it if our edit group is not active */
704         
705         if ((ts.track != this) && _route->edit_group() && !_route->edit_group()->is_active()) {
706                 return;
707         }
708 #endif
709
710         if (timestretch_rect == 0) {
711                 timestretch_rect = new SimpleRect (*canvas_display);
712                 timestretch_rect->property_x1() =  0.0;
713                 timestretch_rect->property_y1() =  0.0;
714                 timestretch_rect->property_x2() =  0.0;
715                 timestretch_rect->property_y2() =  0.0;
716                 timestretch_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
717                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
718         }
719
720         timestretch_rect->show ();
721         timestretch_rect->raise_to_top ();
722
723         x1 = start / editor.get_current_zoom();
724         x2 = (end - 1) / editor.get_current_zoom();
725         y2 = current_height() - 2;
726         
727         timestretch_rect->property_x1() = x1;
728         timestretch_rect->property_y1() = 1.0;
729         timestretch_rect->property_x2() = x2;
730         timestretch_rect->property_y2() = y2;
731 }
732
733 void
734 RouteTimeAxisView::hide_timestretch ()
735 {
736         TimeAxisView::hide_timestretch ();
737
738         if (timestretch_rect) {
739                 timestretch_rect->hide ();
740         }
741 }
742
743 void
744 RouteTimeAxisView::show_selection (TimeSelection& ts)
745 {
746
747 #if 0
748         /* ignore it if our edit group is not active or if the selection was started
749            in some other track or edit group (remember that edit_group() == 0 means
750            that the track is not in an edit group).
751         */
752
753         if (((ts.track != this && !is_child (ts.track)) && _route->edit_group() && !_route->edit_group()->is_active()) ||
754             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->edit_group())))) {
755                 hide_selection ();
756                 return;
757         }
758 #endif
759
760         TimeAxisView::show_selection (ts);
761 }
762
763 void
764 RouteTimeAxisView::set_height (uint32_t h)
765 {
766         int gmlen = h - 5;
767         bool height_changed = (height == 0) || (h != height);
768         gm.get_level_meter().setup_meters (gmlen);
769
770         TimeAxisView::set_height (h);
771
772         ensure_xml_node ();
773
774         if (_view) {
775                 _view->set_height ((double) current_height());
776         }
777
778         char buf[32];
779         snprintf (buf, sizeof (buf), "%u", height);
780         xml_node->add_property ("height", buf);
781
782         if (height >= hNormal) {
783                 reset_meter();
784                 show_name_entry ();
785                 hide_name_label ();
786
787                 gm.get_gain_slider().show();
788                 mute_button->show();
789                 solo_button->show();
790                 if (rec_enable_button)
791                         rec_enable_button->show();
792
793                 edit_group_button.show();
794                 hide_button.show();
795                 visual_button.show();
796                 size_button.show();
797                 automation_button.show();
798                 
799                 if (is_track() && track()->mode() == ARDOUR::Normal) {
800                         playlist_button.show();
801                 }
802
803         } else if (height >= hSmaller) {
804
805                 reset_meter();
806                 show_name_entry ();
807                 hide_name_label ();
808
809                 gm.get_gain_slider().hide();
810                 mute_button->show();
811                 solo_button->show();
812                 if (rec_enable_button)
813                         rec_enable_button->show();
814
815                 edit_group_button.hide ();
816                 hide_button.hide ();
817                 visual_button.hide ();
818                 size_button.hide ();
819                 automation_button.hide ();
820                 
821                 if (is_track() && track()->mode() == ARDOUR::Normal) {
822                         playlist_button.hide ();
823                 }
824
825         } else {
826
827                 hide_name_entry ();
828                 show_name_label ();
829                 
830                 gm.get_gain_slider().hide();
831                 mute_button->hide();
832                 solo_button->hide();
833                 if (rec_enable_button)
834                         rec_enable_button->hide();
835
836                 edit_group_button.hide ();
837                 hide_button.hide ();
838                 visual_button.hide ();
839                 size_button.hide ();
840                 automation_button.hide ();
841                 playlist_button.hide ();
842                 name_label.set_text (_route->name());
843         }
844
845         if (height_changed) {
846                 /* only emit the signal if the height really changed */
847                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
848         }
849 }
850
851 void
852 RouteTimeAxisView::select_track_color ()
853 {
854         if (RouteUI::choose_color ()) {
855
856                 if (_view) {
857                         _view->apply_color (_color, StreamView::RegionColor);
858                 }
859         }
860 }
861
862 void
863 RouteTimeAxisView::reset_samples_per_unit ()
864 {
865         set_samples_per_unit (editor.get_current_zoom());
866 }
867
868 void
869 RouteTimeAxisView::set_samples_per_unit (double spu)
870 {
871         double speed = 1.0;
872
873         if (get_diskstream() != 0) {
874                 speed = get_diskstream()->speed();
875         }
876         
877         if (_view) {
878                 _view->set_samples_per_unit (spu * speed);
879         }
880
881         TimeAxisView::set_samples_per_unit (spu * speed);
882 }
883
884 void
885 RouteTimeAxisView::align_style_changed ()
886 {
887         switch (get_diskstream()->alignment_style()) {
888         case ExistingMaterial:
889                 if (!align_existing_item->get_active()) {
890                         align_existing_item->set_active();
891                 }
892                 break;
893         case CaptureTime:
894                 if (!align_capture_item->get_active()) {
895                         align_capture_item->set_active();
896                 }
897                 break;
898         }
899 }
900
901 void
902 RouteTimeAxisView::set_align_style (AlignStyle style)
903 {
904         RadioMenuItem* item;
905
906         switch (style) {
907         case ExistingMaterial:
908                 item = align_existing_item;
909                 break;
910         case CaptureTime:
911                 item = align_capture_item;
912                 break;
913         default:
914                 fatal << string_compose (_("programming error: %1 %2"), "illegal align style in RouteTimeAxisView::set_align_style", style) << endmsg;
915                 /*NOTREACHED*/
916                 return;
917         }
918
919         if (item->get_active()) {
920                 get_diskstream()->set_align_style (style);
921         }
922 }
923
924 void
925 RouteTimeAxisView::rename_current_playlist ()
926 {
927         ArdourPrompter prompter (true);
928         string name;
929
930         boost::shared_ptr<Diskstream> ds = get_diskstream();
931         if (!ds || ds->destructive())
932                 return;
933
934         boost::shared_ptr<Playlist> pl = ds->playlist();
935         if (!pl)
936                 return;
937
938         prompter.set_prompt (_("Name for playlist"));
939         prompter.set_initial_text (pl->name());
940         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
941         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
942
943         switch (prompter.run ()) {
944         case Gtk::RESPONSE_ACCEPT:
945                 prompter.get_result (name);
946                 if (name.length()) {
947                         pl->set_name (name);
948                 }
949                 break;
950
951         default:
952                 break;
953         }
954 }
955
956 std::string 
957 RouteTimeAxisView::resolve_new_group_playlist_name(std::string &basename, vector<boost::shared_ptr<Playlist> > const & playlists)
958 {
959         std::string ret(basename);
960
961         std::string group_string = "."+edit_group()->name()+".";
962
963         if (basename.find(group_string) == string::npos) {
964                 int maxnumber = 0;
965
966                 // iterate through all playlists
967                 for (vector<boost::shared_ptr<Playlist> >::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
968                         std::string tmp = (*i)->name();
969
970                         std::string::size_type idx = tmp.find(group_string);                    
971                         // find those which belong to this group
972                         if (idx != string::npos) {
973                                 tmp = tmp.substr(idx + group_string.length());
974
975                                 // and find the largest current number
976                                 int x = atoi(tmp.c_str());
977                                 if (x > maxnumber) {
978                                         maxnumber = x;
979                                 }
980                         }
981                 }
982
983                 maxnumber++;
984
985                 char buf[32];
986                 snprintf (buf, sizeof(buf), "%d", maxnumber);
987                
988                 ret = this->name()+"."+edit_group()->name()+"."+buf;
989         }
990
991         return ret;
992 }
993
994 void
995 RouteTimeAxisView::use_copy_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
996 {
997         string name;
998         
999         boost::shared_ptr<Diskstream> ds = get_diskstream();
1000         if (!ds || ds->destructive())
1001                 return;
1002
1003         boost::shared_ptr<const Playlist> pl = ds->playlist();
1004         if (!pl)
1005                 return;
1006
1007         name = pl->name();
1008         
1009         if (edit_group() && edit_group()->is_active()) {
1010                 name = resolve_new_group_playlist_name(name, playlists_before_op);
1011         }
1012
1013         while (_session.playlist_by_name(name)) {
1014                 name = Playlist::bump_name (name, _session);
1015         }
1016
1017         // TODO: The prompter "new" button should be de-activated if the user
1018         // specifies a playlist name which already exists in the session.
1019
1020         if (prompt) {
1021
1022                 ArdourPrompter prompter (true);
1023                 
1024                 prompter.set_prompt (_("Name for Playlist"));
1025                 prompter.set_initial_text (name);
1026                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1027                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1028                 prompter.show_all ();
1029                 
1030                 switch (prompter.run ()) {
1031                 case Gtk::RESPONSE_ACCEPT:
1032                         prompter.get_result (name);
1033                         break;
1034                         
1035                 default:
1036                         return;
1037                 }
1038         }
1039
1040         if (name.length()) {
1041                 ds->use_copy_playlist ();
1042                 ds->playlist()->set_name (name);
1043         }
1044 }
1045
1046 void
1047 RouteTimeAxisView::use_new_playlist (bool prompt, vector<boost::shared_ptr<Playlist> > const & playlists_before_op)
1048 {
1049         string name;
1050         
1051         boost::shared_ptr<Diskstream> ds = get_diskstream();
1052         if (!ds || ds->destructive())
1053                 return;
1054
1055         boost::shared_ptr<const Playlist> pl = ds->playlist();
1056         if (!pl)
1057                 return;
1058
1059         name = pl->name();
1060         
1061         if (edit_group() && edit_group()->is_active()) {
1062                 name = resolve_new_group_playlist_name(name,playlists_before_op);
1063         }
1064
1065         while (_session.playlist_by_name(name)) {
1066                 name = Playlist::bump_name (name, _session);
1067         }
1068
1069
1070         if (prompt) {
1071                 
1072                 ArdourPrompter prompter (true);
1073                 
1074                 prompter.set_prompt (_("Name for Playlist"));
1075                 prompter.set_initial_text (name);
1076                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
1077                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
1078
1079                 switch (prompter.run ()) {
1080                 case Gtk::RESPONSE_ACCEPT:
1081                         prompter.get_result (name);
1082                         break;
1083                         
1084                 default:
1085                         return;
1086                 }
1087         }
1088
1089         if (name.length()) {
1090                 ds->use_new_playlist ();
1091                 ds->playlist()->set_name (name);
1092         }
1093 }
1094
1095 void
1096 RouteTimeAxisView::clear_playlist ()
1097 {
1098         boost::shared_ptr<Diskstream> ds = get_diskstream();
1099         if (!ds || ds->destructive())
1100                 return;
1101
1102         boost::shared_ptr<Playlist> pl = ds->playlist();
1103         if (!pl)
1104                 return;
1105
1106         editor.clear_playlist (pl);
1107 }
1108
1109 void
1110 RouteTimeAxisView::speed_changed ()
1111 {
1112         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
1113 }
1114
1115 void
1116 RouteTimeAxisView::diskstream_changed ()
1117 {
1118         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::update_diskstream_display));
1119 }       
1120
1121 void
1122 RouteTimeAxisView::update_diskstream_display ()
1123 {
1124         if (!get_diskstream()) // bus
1125                 return;
1126
1127         set_playlist (get_diskstream()->playlist());
1128         map_frozen ();
1129 }       
1130
1131 void
1132 RouteTimeAxisView::selection_click (GdkEventButton* ev)
1133 {
1134         if (Keyboard::modifier_state_equals (ev->state, (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier))) {
1135
1136                 /* special case: select/deselect all tracks */
1137                 if (editor.get_selection().selected (this)) {
1138                         editor.get_selection().clear_tracks ();
1139                 } else {
1140                         editor.select_all_tracks ();
1141                 }
1142
1143                 return;
1144         } 
1145
1146         PublicEditor::TrackViewList* tracks = editor.get_valid_views (this, _route->edit_group());
1147
1148         switch (Keyboard::selection_type (ev->state)) {
1149         case Selection::Toggle:
1150                 editor.get_selection().toggle (*tracks);
1151                 break;
1152                 
1153         case Selection::Set:
1154                 editor.get_selection().set (*tracks);
1155                 break;
1156
1157         case Selection::Extend:
1158                 if (tracks->size() > 1) {
1159                         /* add each one, do not "extend" */
1160                         editor.get_selection().add (*tracks);
1161                 } else {
1162                         /* extend to the single track */
1163                         editor.extend_selection_to_track (*tracks->front());
1164                 }
1165                 break;
1166
1167         case Selection::Add:
1168                 editor.get_selection().add (*tracks);
1169                 break;
1170         }
1171
1172         delete tracks;
1173 }
1174
1175 void
1176 RouteTimeAxisView::set_selected_points (PointSelection& points)
1177 {
1178         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1179                 (*i)->set_selected_points (points);
1180         }
1181 }
1182
1183 void
1184 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
1185 {
1186         if (_view) {
1187                 _view->set_selected_regionviews (regions);
1188         }
1189 }
1190
1191 /** Add the selectable things that we have to a list.
1192  * @param results List to add things to.
1193  */
1194 void
1195 RouteTimeAxisView::get_selectables (nframes_t start, nframes_t end, double top, double bot, list<Selectable*>& results)
1196 {
1197         double speed = 1.0;
1198         
1199         if (get_diskstream() != 0) {
1200                 speed = get_diskstream()->speed();
1201         }
1202         
1203         nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
1204         nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
1205
1206         if ((_view && ((top < 0.0 && bot < 0.0))) || touched (top, bot)) {
1207                 _view->get_selectables (start_adjusted, end_adjusted, results);
1208         }
1209
1210         /* pick up visible automation tracks */
1211         
1212         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1213                 if (!(*i)->hidden()) {
1214                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1215                 }
1216         }
1217 }
1218
1219 void
1220 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1221 {
1222         if (_view) {
1223                 _view->get_inverted_selectables (sel, results);
1224         }
1225
1226         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1227                 if (!(*i)->hidden()) {
1228                         (*i)->get_inverted_selectables (sel, results);
1229                 }
1230         }
1231
1232         return;
1233 }
1234
1235 bool
1236 RouteTimeAxisView::show_automation(Evoral::Parameter param)
1237 {
1238         return (_show_automation.find(param) != _show_automation.end());
1239 }
1240
1241 /** Retuns NULL if track for \a param doesn't exist.
1242  */
1243 RouteTimeAxisView::RouteAutomationNode*
1244 RouteTimeAxisView::automation_track(Evoral::Parameter param)
1245 {
1246         map<Evoral::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.find(param);
1247
1248         if (i != _automation_tracks.end())
1249                 return i->second;
1250         else
1251                 return NULL;
1252 }
1253
1254 /** Shorthand for GainAutomation, etc.
1255  */     
1256 RouteTimeAxisView::RouteAutomationNode*
1257 RouteTimeAxisView::automation_track(AutomationType type)
1258 {
1259         return automation_track(Evoral::Parameter(type));
1260 }
1261
1262 RouteGroup*
1263 RouteTimeAxisView::edit_group() const
1264 {
1265         return _route->edit_group();
1266 }
1267
1268 string
1269 RouteTimeAxisView::name() const
1270 {
1271         return _route->name();
1272 }
1273
1274 boost::shared_ptr<Playlist>
1275 RouteTimeAxisView::playlist () const 
1276 {
1277         boost::shared_ptr<Diskstream> ds;
1278
1279         if ((ds = get_diskstream()) != 0) {
1280                 return ds->playlist(); 
1281         } else {
1282                 return boost::shared_ptr<Playlist> ();
1283         }
1284 }
1285
1286 void
1287 RouteTimeAxisView::name_entry_changed ()
1288 {
1289         string x;
1290
1291         x = name_entry.get_text ();
1292         
1293         if (x == _route->name()) {
1294                 return;
1295         }
1296
1297         strip_whitespace_edges(x);
1298
1299         if (x.length() == 0) {
1300                 name_entry.set_text (_route->name());
1301                 return;
1302         }
1303
1304         if (_session.route_name_unique (x)) {
1305                 _route->set_name (x);
1306         } else {
1307                 ARDOUR_UI::instance()->popup_error (_("A track already exists with that name"));
1308                 name_entry.set_text (_route->name());
1309         }
1310 }
1311
1312 void
1313 RouteTimeAxisView::visual_click ()
1314 {
1315         popup_display_menu (0);
1316 }
1317
1318 void
1319 RouteTimeAxisView::hide_click ()
1320 {
1321         // LAME fix for hide_button refresh fix
1322         hide_button.set_sensitive(false);
1323         
1324         editor.hide_track_in_display (*this);
1325         
1326         hide_button.set_sensitive(true);
1327 }
1328
1329 boost::shared_ptr<Region>
1330 RouteTimeAxisView::find_next_region (nframes_t pos, RegionPoint point, int32_t dir)
1331 {
1332         boost::shared_ptr<Diskstream> stream;
1333         boost::shared_ptr<Playlist> playlist;
1334
1335         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1336                 return playlist->find_next_region (pos, point, dir);
1337         }
1338
1339         return boost::shared_ptr<Region> ();
1340 }
1341
1342 nframes64_t 
1343 RouteTimeAxisView::find_next_region_boundary (nframes64_t pos, int32_t dir)
1344 {
1345         boost::shared_ptr<Diskstream> stream;
1346         boost::shared_ptr<Playlist> playlist;
1347
1348         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1349                 return playlist->find_next_region_boundary (pos, dir);
1350         }
1351
1352         return -1;
1353 }
1354
1355 bool
1356 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1357 {
1358         boost::shared_ptr<Playlist> what_we_got;
1359         boost::shared_ptr<Diskstream> ds = get_diskstream();
1360         boost::shared_ptr<Playlist> playlist;
1361         bool ret = false;
1362
1363         if (ds == 0) {
1364                 /* route is a bus, not a track */
1365                 return false;
1366         }
1367
1368         playlist = ds->playlist();
1369
1370         TimeSelection time (selection.time);
1371         float speed = ds->speed();
1372         if (speed != 1.0f) {
1373                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1374                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1375                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1376                 }
1377         }
1378         
1379         XMLNode &before = playlist->get_state();
1380         switch (op) {
1381         case Cut:
1382                 if ((what_we_got = playlist->cut (time)) != 0) {
1383                         editor.get_cut_buffer().add (what_we_got);
1384                         _session.add_command( new MementoCommand<Playlist>(*playlist.get(), &before, &playlist->get_state()));
1385                         ret = true;
1386                 }
1387                 break;
1388         case Copy:
1389                 if ((what_we_got = playlist->copy (time)) != 0) {
1390                         editor.get_cut_buffer().add (what_we_got);
1391                 }
1392                 break;
1393
1394         case Clear:
1395                 if ((what_we_got = playlist->cut (time)) != 0) {
1396                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1397                         what_we_got->release ();
1398                         ret = true;
1399                 }
1400                 break;
1401         }
1402
1403         return ret;
1404 }
1405
1406 bool
1407 RouteTimeAxisView::paste (nframes_t pos, float times, Selection& selection, size_t nth)
1408 {
1409         if (!is_track()) {
1410                 return false;
1411         }
1412
1413         boost::shared_ptr<Playlist> playlist = get_diskstream()->playlist();
1414         PlaylistSelection::iterator p;
1415         
1416         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1417
1418         if (p == selection.playlists.end()) {
1419                 return false;
1420         }
1421
1422         if (get_diskstream()->speed() != 1.0f)
1423                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1424         
1425         XMLNode &before = playlist->get_state();
1426         playlist->paste (*p, pos, times);
1427         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1428
1429         return true;
1430 }
1431
1432
1433 TimeAxisView::Children
1434 RouteTimeAxisView::get_child_list()
1435 {
1436         TimeAxisView::Children redirect_children;
1437         
1438         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1439                 if (!(*i)->hidden()) {
1440                         redirect_children.push_back(*i);
1441                 }
1442         }
1443         return redirect_children;
1444 }
1445
1446
1447 void
1448 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1449 {
1450         using namespace Menu_Helpers;
1451
1452         if (!menu || !is_track()) {
1453                 return;
1454         }
1455
1456         MenuList& playlist_items = menu->items();
1457         menu->set_name ("ArdourContextMenu");
1458         playlist_items.clear();
1459
1460         if (playlist_menu) {
1461                 delete playlist_menu;
1462         }
1463
1464         playlist_menu = new Menu;
1465         playlist_menu->set_name ("ArdourContextMenu");
1466
1467         vector<boost::shared_ptr<Playlist> > playlists;
1468         boost::shared_ptr<Diskstream> ds = get_diskstream();
1469         RadioMenuItem::Group playlist_group;
1470
1471         _session.get_playlists (playlists);
1472         
1473         for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
1474
1475                 if ((*i)->get_orig_diskstream_id() == ds->id()) {
1476                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist),
1477                                                                                                      boost::weak_ptr<Playlist> (*i))));
1478
1479                         if (ds->playlist()->id() == (*i)->id()) {
1480                                 static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1481                         }
1482                 } else if (ds->playlist()->id() == (*i)->id()) {
1483                         playlist_items.push_back (RadioMenuElem (playlist_group, (*i)->name(), bind (mem_fun (*this, &RouteTimeAxisView::use_playlist), 
1484                                                                                                      boost::weak_ptr<Playlist>(*i))));
1485                         static_cast<RadioMenuItem*>(&playlist_items.back())->set_active();
1486                         
1487                 }
1488         }
1489
1490         playlist_items.push_back (SeparatorElem());
1491         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1492         playlist_items.push_back (SeparatorElem());
1493
1494         if (!edit_group() || !edit_group()->is_active()) {
1495                 playlist_items.push_back (MenuElem (_("New"), bind(mem_fun(editor, &PublicEditor::new_playlists), this)));
1496                 playlist_items.push_back (MenuElem (_("New Copy"), bind(mem_fun(editor, &PublicEditor::copy_playlists), this)));
1497
1498         } else {
1499                 // Use a label which tells the user what is happening
1500                 playlist_items.push_back (MenuElem (_("New Take"), bind(mem_fun(editor, &PublicEditor::new_playlists), this)));
1501                 playlist_items.push_back (MenuElem (_("Copy Take"), bind(mem_fun(editor, &PublicEditor::copy_playlists), this)));
1502                 
1503         }
1504
1505         playlist_items.push_back (SeparatorElem());
1506         playlist_items.push_back (MenuElem (_("Clear Current"), bind(mem_fun(editor, &PublicEditor::clear_playlists), this)));
1507         playlist_items.push_back (SeparatorElem());
1508
1509         playlist_items.push_back (MenuElem(_("Select from all ..."), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1510 }
1511
1512 void
1513 RouteTimeAxisView::use_playlist (boost::weak_ptr<Playlist> wpl)
1514 {
1515         assert (is_track());
1516
1517         boost::shared_ptr<Playlist> pl (wpl.lock());
1518
1519         if (!pl) {
1520                 return;
1521         }
1522
1523         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl);
1524         
1525         if (apl) {
1526                 if (get_diskstream()->playlist() == apl) {
1527                         // radio button cotnrols mean this function is called for both the 
1528                         // old and new playlist
1529                         return;
1530                 }
1531                 get_diskstream()->use_playlist (apl);
1532
1533
1534                 if (edit_group() && edit_group()->is_active()) {
1535                         //PBD::stacktrace(cerr, 20);
1536                         std::string group_string = "."+edit_group()->name()+".";
1537
1538                         std::string take_name = apl->name();
1539                         std::string::size_type idx = take_name.find(group_string);
1540
1541                         if (idx == std::string::npos)
1542                                 return;
1543
1544                         take_name = take_name.substr(idx + group_string.length()); // find the bit containing the take number / name
1545                         
1546                         for (list<Route*>::const_iterator i = edit_group()->route_list().begin(); i != edit_group()->route_list().end(); ++i) {
1547                                 if ( (*i) == this->route().get()) {
1548                                         continue;
1549                                 }
1550                                 
1551                                 std::string playlist_name = (*i)->name()+group_string+take_name;
1552
1553                                 Track *track = dynamic_cast<Track *>(*i);
1554                                 if (!track) {
1555                                         std::cerr << "route " << (*i)->name() << " is not a Track" << std::endl;
1556                                         continue;
1557                                 }
1558
1559                                 boost::shared_ptr<Playlist> ipl = session().playlist_by_name(playlist_name);
1560                                 if (!ipl) {
1561                                         // No playlist for this track for this take yet, make it
1562                                         track->diskstream()->use_new_playlist();
1563                                         track->diskstream()->playlist()->set_name(playlist_name);
1564                                 } else {
1565                                         track->diskstream()->use_playlist(ipl);
1566                                 }
1567                                 
1568                                 //(*i)->get_dis
1569                         }
1570                 }
1571         }
1572 }
1573
1574 void
1575 RouteTimeAxisView::show_playlist_selector ()
1576 {
1577         editor.playlist_selector().show_for (this);
1578 }
1579
1580 void
1581 RouteTimeAxisView::map_frozen ()
1582 {
1583         if (!is_track()) {
1584                 return;
1585         }
1586
1587         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1588
1589         switch (track()->freeze_state()) {
1590         case Track::Frozen:
1591                 playlist_button.set_sensitive (false);
1592                 rec_enable_button->set_sensitive (false);
1593                 break;
1594         default:
1595                 playlist_button.set_sensitive (true);
1596                 rec_enable_button->set_sensitive (true);
1597                 break;
1598         }
1599 }
1600
1601 void
1602 RouteTimeAxisView::color_handler ()
1603 {
1604         //case cTimeStretchOutline:
1605         if (timestretch_rect) {
1606                 timestretch_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchOutline.get();
1607         }
1608         //case cTimeStretchFill:
1609         if (timestretch_rect) {
1610                 timestretch_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeStretchFill.get();
1611         }
1612
1613         reset_meter();
1614 }
1615
1616 void
1617 RouteTimeAxisView::toggle_automation_track (Evoral::Parameter param)
1618 {
1619         RouteAutomationNode* node = automation_track(param);
1620
1621         if (!node)
1622                 return;
1623
1624         bool showit = node->menu_item->get_active();
1625
1626         if (showit != node->track->marked_for_display()) {
1627                 if (showit) {
1628                         node->track->set_marked_for_display (true);
1629                         node->track->canvas_display->show();
1630                         node->track->get_state_node()->add_property ("shown", X_("yes"));
1631                 } else {
1632                         node->track->set_marked_for_display (false);
1633                         node->track->hide ();
1634                         node->track->get_state_node()->add_property ("shown", X_("no"));
1635                 }
1636
1637                 /* now trigger a redisplay */
1638                 
1639                 if (!no_redraw) {
1640                          _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
1641                 }
1642         }
1643 }
1644
1645 void
1646 RouteTimeAxisView::automation_track_hidden (Evoral::Parameter param)
1647 {
1648         RouteAutomationNode* ran = automation_track(param);
1649         if (!ran) {
1650                 return;
1651         }
1652
1653         _show_automation.erase(param);
1654         ran->track->get_state_node()->add_property (X_("shown"), X_("no"));
1655
1656         if (ran->menu_item && !_hidden) {
1657                 ran->menu_item->set_active (false);
1658         }
1659
1660          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1661 }
1662
1663
1664 void
1665 RouteTimeAxisView::show_all_automation ()
1666 {
1667         no_redraw = true;
1668         
1669         /* Show our automation */
1670
1671         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
1672         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1673                 i->second->track->set_marked_for_display (true);
1674                 i->second->track->canvas_display->show();
1675                 i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1676                 i->second->menu_item->set_active(true);
1677         }
1678
1679
1680         /* Show processor automation */
1681
1682         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1683                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1684                         if ((*ii)->view == 0) {
1685                                 add_processor_automation_curve ((*i)->processor, (*ii)->what);
1686                         } 
1687
1688                         (*ii)->menu_item->set_active (true);
1689                 }
1690         }
1691
1692         no_redraw = false;
1693
1694         /* Redraw */
1695
1696          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1697 }
1698
1699 void
1700 RouteTimeAxisView::show_existing_automation ()
1701 {
1702         no_redraw = true;
1703         
1704         /* Show our automation */
1705
1706         map<Evoral::Parameter, RouteAutomationNode*>::iterator i;
1707         for (i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1708                 if (i->second->track->line() && i->second->track->line()->npoints() > 0) {
1709                         i->second->track->set_marked_for_display (true);
1710                         i->second->track->canvas_display->show();
1711                         i->second->track->get_state_node()->add_property ("shown", X_("yes"));
1712                         i->second->menu_item->set_active(true);
1713                 }
1714         }
1715
1716
1717         /* Show processor automation */
1718
1719         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1720                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1721                         if ((*ii)->view != 0 && (*i)->processor->data().control((*ii)->what)->list()->size() > 0) {
1722                                 (*ii)->menu_item->set_active (true);
1723                         }
1724                 }
1725         }
1726
1727         no_redraw = false;
1728         
1729         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1730 }
1731
1732 void
1733 RouteTimeAxisView::hide_all_automation ()
1734 {
1735         no_redraw = true;
1736
1737         /* Hide our automation */
1738
1739         for (map<Evoral::Parameter, RouteAutomationNode*>::iterator i = _automation_tracks.begin(); i != _automation_tracks.end(); ++i) {
1740                 i->second->track->set_marked_for_display (false);
1741                 i->second->track->hide ();
1742                 i->second->track->get_state_node()->add_property ("shown", X_("no"));
1743                 i->second->menu_item->set_active (false);
1744         }
1745
1746         /* Hide processor automation */
1747
1748         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1749                 for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1750                         (*ii)->menu_item->set_active (false);
1751                 }
1752         }
1753
1754         _show_automation.clear();
1755
1756         no_redraw = false;
1757          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1758 }
1759
1760
1761 void
1762 RouteTimeAxisView::region_view_added (RegionView* rv)
1763 {
1764         /* XXX need to find out if automation children have automationstreamviews. If yes, no ghosts */
1765         if(is_audio_track()) {
1766                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1767                         boost::shared_ptr<AutomationTimeAxisView> atv;
1768                         
1769                         if ((atv = boost::dynamic_pointer_cast<AutomationTimeAxisView> (*i)) != 0) {
1770                                 atv->add_ghost(rv);
1771                         }
1772                 }
1773         }
1774
1775         for (UnderlayMirrorList::iterator i = _underlay_mirrors.begin(); i != _underlay_mirrors.end(); ++i) {
1776                 (*i)->add_ghost(rv);
1777         }
1778 }
1779
1780 RouteTimeAxisView::ProcessorAutomationInfo::~ProcessorAutomationInfo ()
1781 {
1782         for (vector<ProcessorAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1783                 delete *i;
1784         }
1785 }
1786
1787
1788 RouteTimeAxisView::ProcessorAutomationNode::~ProcessorAutomationNode ()
1789 {
1790         parent.remove_processor_automation_node (this);
1791 }
1792
1793 void
1794 RouteTimeAxisView::remove_processor_automation_node (ProcessorAutomationNode* pan)
1795 {
1796         if (pan->view) {
1797                 remove_child (pan->view);
1798         }
1799 }
1800
1801 RouteTimeAxisView::ProcessorAutomationNode*
1802 RouteTimeAxisView::find_processor_automation_node (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1803 {
1804         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
1805
1806                 if ((*i)->processor == processor) {
1807
1808                         for (vector<ProcessorAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1809                                 if ((*ii)->what == what) {
1810                                         return *ii;
1811                                 }
1812                         }
1813                 }
1814         }
1815
1816         return 0;
1817 }
1818
1819 static string 
1820 legalize_for_xml_node (string str)
1821 {
1822         string::size_type pos;
1823         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=:";
1824         string legal;
1825
1826         legal = str;
1827         pos = 0;
1828
1829         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1830                 legal.replace (pos, 1, "_");
1831                 pos += 1;
1832         }
1833
1834         return legal;
1835 }
1836
1837
1838 void
1839 RouteTimeAxisView::add_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
1840 {
1841         string name;
1842         ProcessorAutomationNode* pan;
1843
1844         if ((pan = find_processor_automation_node (processor, what)) == 0) {
1845                 fatal << _("programming error: ")
1846                       << string_compose (X_("processor automation curve for %1:%2 not registered with track!"),
1847                                   processor->name(), what)
1848                       << endmsg;
1849                 /*NOTREACHED*/
1850                 return;
1851         }
1852
1853         if (pan->view) {
1854                 return;
1855         }
1856
1857         name = processor->describe_parameter (what);
1858
1859         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1860
1861         /* FIXME: ew */
1862
1863         char state_name[256];
1864         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (processor->name()).c_str(), what.id());
1865
1866         boost::shared_ptr<AutomationControl> control
1867                         = boost::dynamic_pointer_cast<AutomationControl>(processor->data().control(what, true));
1868
1869         pan->view = boost::shared_ptr<AutomationTimeAxisView>(
1870                         new AutomationTimeAxisView (_session, _route, processor, control,
1871                                 editor, *this, false, parent_canvas, name, state_name));
1872
1873         pan->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_automation_track_hidden), pan, processor));
1874
1875         if (!pan->view->marked_for_display()) {
1876                 pan->view->hide ();
1877         } else {
1878                 pan->menu_item->set_active (true);
1879         }
1880
1881         add_child (pan->view);
1882
1883         if (_view) {
1884                 _view->foreach_regionview (mem_fun(*pan->view.get(), &TimeAxisView::add_ghost));
1885         }
1886
1887         processor->mark_automation_visible (what, true);
1888 }
1889
1890 void
1891 RouteTimeAxisView::processor_automation_track_hidden (RouteTimeAxisView::ProcessorAutomationNode* pan, boost::shared_ptr<Processor> i)
1892 {
1893         if (!_hidden) {
1894                 pan->menu_item->set_active (false);
1895         }
1896
1897         i->mark_automation_visible (pan->what, false);
1898
1899          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1900 }
1901
1902 void
1903 RouteTimeAxisView::add_existing_processor_automation_curves (boost::shared_ptr<Processor> processor)
1904 {
1905         set<Evoral::Parameter> s;
1906         boost::shared_ptr<AutomationLine> al;
1907
1908         processor->what_has_visible_data (s);
1909
1910         for (set<Evoral::Parameter>::iterator i = s.begin(); i != s.end(); ++i) {
1911                 
1912                 if ((al = find_processor_automation_curve (processor, *i)) != 0) {
1913                         al->queue_reset ();
1914                 } else {
1915                         add_processor_automation_curve (processor, (*i));
1916                 }
1917         }
1918 }
1919
1920 void
1921 RouteTimeAxisView::add_automation_child(Evoral::Parameter param, boost::shared_ptr<AutomationTimeAxisView> track, bool show)
1922 {
1923         using namespace Menu_Helpers;
1924
1925         XMLProperty* prop;
1926         XMLNode* node;
1927
1928         add_child (track);
1929
1930         track->Hiding.connect (bind (mem_fun (*this, &RouteTimeAxisView::automation_track_hidden), param));
1931
1932         bool hideit = (!show);
1933
1934         if ((node = track->get_state_node()) != 0) {
1935                 if  ((prop = node->property ("shown")) != 0) {
1936                         if (prop->value() == "yes") {
1937                                 hideit = false;
1938                         }
1939                 } 
1940         }
1941
1942         _automation_tracks.insert(std::make_pair(param, new RouteAutomationNode(param, NULL, track)));
1943
1944         if (hideit) {
1945                 track->hide ();
1946         } else {
1947                 _show_automation.insert (param);
1948
1949
1950                 if (!no_redraw) {
1951                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
1952                 }
1953         }
1954
1955         build_display_menu();
1956 }
1957
1958
1959 void
1960 RouteTimeAxisView::add_processor_to_subplugin_menu (boost::shared_ptr<Processor> processor)
1961 {
1962         using namespace Menu_Helpers;
1963         ProcessorAutomationInfo *rai;
1964         list<ProcessorAutomationInfo*>::iterator x;
1965         
1966         const std::set<Evoral::Parameter>& automatable = processor->what_can_be_automated ();
1967         std::set<Evoral::Parameter> has_visible_automation;
1968
1969         processor->what_has_visible_data(has_visible_automation);
1970
1971         if (automatable.empty()) {
1972                 return;
1973         }
1974
1975         for (x = processor_automation.begin(); x != processor_automation.end(); ++x) {
1976                 if ((*x)->processor == processor) {
1977                         break;
1978                 }
1979         }
1980
1981         if (x == processor_automation.end()) {
1982
1983                 rai = new ProcessorAutomationInfo (processor);
1984                 processor_automation.push_back (rai);
1985
1986         } else {
1987
1988                 rai = *x;
1989
1990         }
1991
1992         /* any older menu was deleted at the top of processors_changed()
1993            when we cleared the subplugin menu.
1994         */
1995
1996         rai->menu = manage (new Menu);
1997         MenuList& items = rai->menu->items();
1998         rai->menu->set_name ("ArdourContextMenu");
1999
2000         items.clear ();
2001
2002         for (std::set<Evoral::Parameter>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
2003
2004                 ProcessorAutomationNode* pan;
2005                 CheckMenuItem* mitem;
2006                 
2007                 string name = processor->describe_parameter (*i);
2008                 
2009                 items.push_back (CheckMenuElem (name));
2010                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
2011
2012                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
2013                         mitem->set_active(true);
2014                 }
2015
2016                 if ((pan = find_processor_automation_node (processor, *i)) == 0) {
2017
2018                         /* new item */
2019                         
2020                         pan = new ProcessorAutomationNode (*i, mitem, *this);
2021                         
2022                         rai->lines.push_back (pan);
2023
2024                 } else {
2025
2026                         pan->menu_item = mitem;
2027
2028                 }
2029
2030                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::processor_menu_item_toggled), rai, pan));
2031         }
2032
2033         /* add the menu for this processor, because the subplugin
2034            menu is always cleared at the top of processors_changed().
2035            this is the result of some poor design in gtkmm and/or
2036            GTK+.
2037         */
2038
2039         subplugin_menu.items().push_back (MenuElem (processor->name(), *rai->menu));
2040         rai->valid = true;
2041 }
2042
2043 void
2044 RouteTimeAxisView::processor_menu_item_toggled (RouteTimeAxisView::ProcessorAutomationInfo* rai,
2045                                                RouteTimeAxisView::ProcessorAutomationNode* pan)
2046 {
2047         bool showit = pan->menu_item->get_active();
2048         bool redraw = false;
2049
2050         if (pan->view == 0 && showit) {
2051                 add_processor_automation_curve (rai->processor, pan->what);
2052                 redraw = true;
2053         }
2054
2055         if (pan->view && showit != pan->view->marked_for_display()) {
2056
2057                 if (showit) {
2058                         pan->view->set_marked_for_display (true);
2059                         pan->view->canvas_display->show();
2060                 } else {
2061                         rai->processor->mark_automation_visible (pan->what, true);
2062                         pan->view->set_marked_for_display (false);
2063                         pan->view->hide ();
2064                 }
2065
2066                 redraw = true;
2067
2068         }
2069
2070         if (redraw && !no_redraw) {
2071
2072                 /* now trigger a redisplay */
2073                 
2074                  _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
2075
2076         }
2077 }
2078
2079 void
2080 RouteTimeAxisView::processors_changed ()
2081 {
2082         using namespace Menu_Helpers;
2083         
2084         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ++i) {
2085                 (*i)->valid = false;
2086         }
2087
2088         subplugin_menu.items().clear ();
2089
2090         _route->foreach_processor (this, &RouteTimeAxisView::add_processor_to_subplugin_menu);
2091         _route->foreach_processor (this, &RouteTimeAxisView::add_existing_processor_automation_curves);
2092
2093         for (list<ProcessorAutomationInfo*>::iterator i = processor_automation.begin(); i != processor_automation.end(); ) {
2094
2095                 list<ProcessorAutomationInfo*>::iterator tmp;
2096
2097                 tmp = i;
2098                 ++tmp;
2099
2100                 if (!(*i)->valid) {
2101
2102                         delete *i;
2103                         processor_automation.erase (i);
2104
2105                 } 
2106
2107                 i = tmp;
2108         }
2109
2110         /* change in visibility was possible */
2111
2112         _route->gui_changed ("visible_tracks", this);
2113 }
2114
2115 boost::shared_ptr<AutomationLine>
2116 RouteTimeAxisView::find_processor_automation_curve (boost::shared_ptr<Processor> processor, Evoral::Parameter what)
2117 {
2118         ProcessorAutomationNode* pan;
2119
2120         if ((pan = find_processor_automation_node (processor, what)) != 0) {
2121                 if (pan->view) {
2122                         pan->view->line();
2123                 } 
2124         }
2125
2126         return boost::shared_ptr<AutomationLine>();
2127 }
2128
2129 void
2130 RouteTimeAxisView::reset_processor_automation_curves ()
2131 {
2132         for (ProcessorAutomationCurves::iterator i = processor_automation_curves.begin(); i != processor_automation_curves.end(); ++i) {
2133                 (*i)->reset();
2134         }
2135 }
2136
2137 void
2138 RouteTimeAxisView::update_rec_display ()
2139 {
2140         RouteUI::update_rec_display ();
2141         name_entry.set_sensitive (!_route->record_enabled());
2142 }
2143                 
2144 void
2145 RouteTimeAxisView::set_layer_display (LayerDisplay d)
2146 {
2147         _view->set_layer_display (d);
2148 }
2149         
2150
2151 boost::shared_ptr<AutomationTimeAxisView>
2152 RouteTimeAxisView::automation_child(Evoral::Parameter param)
2153 {
2154         AutomationTracks::iterator i = _automation_tracks.find(param);
2155         if (i != _automation_tracks.end())
2156                 return i->second->track;
2157         else
2158                 return boost::shared_ptr<AutomationTimeAxisView>();
2159 }
2160
2161 void
2162 RouteTimeAxisView::fast_update ()
2163 {
2164         gm.get_level_meter().update_meters ();
2165 }
2166
2167 void
2168 RouteTimeAxisView::hide_meter ()
2169 {
2170         clear_meter ();
2171         gm.get_level_meter().hide_meters ();
2172 }
2173
2174 void
2175 RouteTimeAxisView::show_meter ()
2176 {
2177         reset_meter ();
2178 }
2179
2180 void
2181 RouteTimeAxisView::reset_meter ()
2182 {
2183         if (Config->get_show_track_meters()) {
2184                 gm.get_level_meter().setup_meters (height-5);
2185         } else {
2186                 hide_meter ();
2187         }
2188 }
2189
2190 void
2191 RouteTimeAxisView::clear_meter ()
2192 {
2193         gm.get_level_meter().clear_meters ();
2194 }
2195
2196 void
2197 RouteTimeAxisView::meter_changed (void *src)
2198 {
2199         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RouteTimeAxisView::meter_changed), src));
2200         reset_meter();
2201 }
2202
2203 void
2204 RouteTimeAxisView::io_changed (IOChange change, void *src)
2205 {
2206         reset_meter ();
2207 }
2208
2209 void
2210 RouteTimeAxisView::build_underlay_menu(Gtk::Menu* parent_menu) {
2211         using namespace Menu_Helpers;
2212
2213         if(!_underlay_streams.empty()) {
2214                 MenuList& parent_items = parent_menu->items();
2215                 Menu* gs_menu = manage (new Menu);
2216                 gs_menu->set_name ("ArdourContextMenu");
2217                 MenuList& gs_items = gs_menu->items();
2218                 
2219                 parent_items.push_back (MenuElem (_("Underlays"), *gs_menu));
2220                 
2221                 for(UnderlayList::iterator it = _underlay_streams.begin(); it != _underlay_streams.end(); ++it) {
2222                         gs_items.push_back(MenuElem(string_compose(_("Remove \"%1\""), (*it)->trackview().name()),
2223                                                     bind(mem_fun(*this, &RouteTimeAxisView::remove_underlay), *it)));
2224                 }
2225         }
2226 }
2227
2228 bool
2229 RouteTimeAxisView::set_underlay_state() 
2230 {
2231         if(!underlay_xml_node) {
2232                 return false;
2233         }
2234
2235         XMLNodeList nlist = underlay_xml_node->children();
2236         XMLNodeConstIterator niter;
2237         XMLNode *child_node;
2238         
2239         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2240                 child_node = *niter;
2241
2242                 if(child_node->name() != "Underlay") {
2243                         continue;
2244                 }
2245
2246                 XMLProperty* prop = child_node->property ("id");
2247                 if(prop) {
2248                         PBD::ID id(prop->value());
2249
2250                         RouteTimeAxisView* v = editor.get_route_view_by_id(id);
2251
2252                         if(v) {
2253                                 add_underlay(v->view(), false);
2254                         }
2255                 }
2256         }
2257
2258         return false;
2259 }
2260
2261 void
2262 RouteTimeAxisView::add_underlay(StreamView* v, bool update_xml) 
2263 {
2264         if(!v) {
2265                 return;
2266         }
2267
2268         RouteTimeAxisView& other = v->trackview();
2269
2270         if(find(_underlay_streams.begin(), _underlay_streams.end(), v) == _underlay_streams.end()) {
2271                 if(find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this) != other._underlay_mirrors.end()) {
2272                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2273                         /*NOTREACHED*/
2274                 }
2275
2276                 _underlay_streams.push_back(v);
2277                 other._underlay_mirrors.push_back(this);
2278
2279                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::add_ghost));
2280
2281                 if(update_xml) {
2282                         if(!underlay_xml_node) {
2283                                 ensure_xml_node();
2284                                 underlay_xml_node = xml_node->add_child("Underlays");
2285                         }
2286
2287                         XMLNode* node = underlay_xml_node->add_child("Underlay");
2288                         XMLProperty* prop = node->add_property("id");
2289                         prop->set_value(v->trackview().route()->id().to_s());
2290                 }
2291         }
2292 }
2293
2294 void
2295 RouteTimeAxisView::remove_underlay(StreamView* v) 
2296 {
2297         if(!v) {
2298                 return;
2299         }
2300
2301         UnderlayList::iterator it = find(_underlay_streams.begin(), _underlay_streams.end(), v);
2302         RouteTimeAxisView& other = v->trackview();
2303
2304         if(it != _underlay_streams.end()) {
2305                 UnderlayMirrorList::iterator gm = find(other._underlay_mirrors.begin(), other._underlay_mirrors.end(), this);
2306
2307                 if(gm == other._underlay_mirrors.end()) {
2308                         fatal << _("programming error: underlay reference pointer pairs are inconsistent!") << endmsg;
2309                         /*NOTREACHED*/
2310                 }
2311
2312                 v->foreach_regionview(mem_fun(*this, &RouteTimeAxisView::remove_ghost));
2313
2314                 _underlay_streams.erase(it);
2315                 other._underlay_mirrors.erase(gm);
2316
2317                 if(underlay_xml_node) {
2318                         underlay_xml_node->remove_nodes_and_delete("id", v->trackview().route()->id().to_s());
2319                 }
2320         }
2321 }
2322