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