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