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