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