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