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