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