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