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