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