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