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