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