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