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