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