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