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