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