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