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