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