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