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