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