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