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