Make send automation work (#4734).
[ardour.git] / gtk2_ardour / time_axis_view.cc
1 /*
2     Copyright (C) 2000 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
20 #include <cstdlib>
21 #include <cmath>
22 #include <algorithm>
23 #include <string>
24 #include <list>
25
26 #include <libgnomecanvasmm.h>
27 #include <libgnomecanvasmm/canvas.h>
28 #include <libgnomecanvasmm/item.h>
29
30 #include "pbd/error.h"
31 #include "pbd/convert.h"
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/selector.h>
35
36 #include "ardour_ui.h"
37 #include "global_signals.h"
38 #include "gui_thread.h"
39 #include "public_editor.h"
40 #include "time_axis_view.h"
41 #include "region_view.h"
42 #include "ghostregion.h"
43 #include "simplerect.h"
44 #include "simpleline.h"
45 #include "selection.h"
46 #include "keyboard.h"
47 #include "rgb_macros.h"
48 #include "utils.h"
49 #include "streamview.h"
50 #include "editor_drag.h"
51
52 #include "i18n.h"
53
54 using namespace std;
55 using namespace Gtk;
56 using namespace Gdk;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Editing;
60 using namespace ArdourCanvas;
61 using Gtkmm2ext::Keyboard;
62
63 const double trim_handle_size = 6.0; /* pixels */
64 uint32_t TimeAxisView::button_height = 0;
65 uint32_t TimeAxisView::extra_height = 0;
66 int const TimeAxisView::_max_order = 512;
67 PBD::Signal1<void,TimeAxisView*> TimeAxisView::CatchDeletion;
68
69 TimeAxisView::TimeAxisView (ARDOUR::Session* sess, PublicEditor& ed, TimeAxisView* rent, Canvas& /*canvas*/)
70         : AxisView (sess)
71         , controls_table (2, 8)
72         , _size_menu (0)
73         , _y_position (0)
74         , _editor (ed)
75         , _order (0)
76         , _preresize_cursor (0)
77         , _have_preresize_cursor (false)
78         , _ghost_group (0)
79 {
80         if (extra_height == 0) {
81                 compute_heights ();
82         }
83
84         _canvas_background = new Group (*ed.get_background_group (), 0.0, 0.0);
85         _canvas_display = new Group (*ed.get_trackview_group (), 0.0, 0.0);
86         _canvas_display->hide(); // reveal as needed
87
88         selection_group = new Group (*_canvas_display);
89         selection_group->set_data (X_("timeselection"), (void *) 1);
90         selection_group->hide();
91
92         _ghost_group = new Group (*_canvas_display);
93         _ghost_group->lower_to_bottom();
94         _ghost_group->show();
95
96         control_parent = 0;
97         display_menu = 0;
98         _hidden = false;
99         in_destructor = false;
100         height = 0;
101         _effective_height = 0;
102         parent = rent;
103         last_name_entry_key_press_event = 0;
104         name_packing = NamePackingBits (0);
105         _resize_drag_start = -1;
106
107         /*
108           Create the standard LHS Controls
109           We create the top-level container and name add the name label here,
110           subclasses can add to the layout as required
111         */
112
113         name_entry.set_name ("EditorTrackNameDisplay");
114         name_entry.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_release));
115         name_entry.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_button_press));
116         name_entry.signal_key_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_release));
117         name_entry.signal_activate().connect (sigc::mem_fun(*this, &TimeAxisView::name_entry_activated));
118         name_entry.signal_focus_in_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_in));
119         name_entry.signal_focus_out_event().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_focus_out));
120         Gtkmm2ext::set_size_request_to_display_given_text (name_entry, N_("gTortnam"), 10, 10); // just represents a short name
121
122         name_label.set_name ("TrackLabel");
123         name_label.set_alignment (0.0, 0.5);
124
125         /* typically, either name_label OR name_entry are visible,
126            but not both. its up to derived classes to show/hide them as they
127            wish.
128         */
129
130         name_hbox.show ();
131
132         controls_table.set_size_request (200);
133         controls_table.set_row_spacings (2);
134         controls_table.set_col_spacings (2);
135         controls_table.set_border_width (2);
136         controls_table.set_homogeneous (true);
137
138         controls_table.attach (name_hbox, 0, 5, 0, 1,  Gtk::FILL|Gtk::EXPAND,  Gtk::FILL|Gtk::EXPAND, 3, 0);
139         controls_table.show_all ();
140         controls_table.set_no_show_all ();
141
142         HSeparator* separator = manage (new HSeparator());
143
144         controls_vbox.pack_start (controls_table, false, false);
145         controls_vbox.show ();
146
147         //controls_ebox.set_name ("TimeAxisViewControlsBaseUnselected");
148         controls_ebox.add (controls_vbox);
149         controls_ebox.add_events (Gdk::BUTTON_PRESS_MASK|
150                                   Gdk::BUTTON_RELEASE_MASK|
151                                   Gdk::POINTER_MOTION_MASK|
152                                   Gdk::ENTER_NOTIFY_MASK|
153                                   Gdk::LEAVE_NOTIFY_MASK|
154                                   Gdk::SCROLL_MASK);
155         controls_ebox.set_flags (CAN_FOCUS);
156
157         /* note that this handler connects *before* the default handler */
158         controls_ebox.signal_scroll_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_scroll), true);
159         controls_ebox.signal_button_press_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_button_press));
160         controls_ebox.signal_button_release_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_button_release));
161         controls_ebox.signal_motion_notify_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_motion));
162         controls_ebox.signal_leave_notify_event().connect (sigc::mem_fun (*this, &TimeAxisView::controls_ebox_leave));
163         controls_ebox.show ();
164
165         controls_hbox.pack_start (controls_ebox, true, true);
166         controls_hbox.show ();
167
168         time_axis_vbox.pack_start (controls_hbox, true, true);
169         time_axis_vbox.pack_end (*separator, false, false);
170         time_axis_vbox.show();
171
172         ColorsChanged.connect (sigc::mem_fun (*this, &TimeAxisView::color_handler));
173
174         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&TimeAxisView::erase_ghost, this, _1), gui_context());
175 }
176
177 TimeAxisView::~TimeAxisView()
178 {
179         in_destructor = true;
180
181         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
182                 delete *i;
183         }
184
185         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
186                 delete (*i)->rect;
187                 delete (*i)->start_trim;
188                 delete (*i)->end_trim;
189
190         }
191
192         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
193                 delete (*i)->rect;
194                 delete (*i)->start_trim;
195                 delete (*i)->end_trim;
196         }
197
198         delete selection_group;
199         selection_group = 0;
200
201         delete _canvas_background;
202         _canvas_background = 0;
203
204         delete _canvas_display;
205         _canvas_display = 0;
206
207         delete display_menu;
208         display_menu = 0;
209
210         delete _size_menu;
211 }
212
213 void
214 TimeAxisView::hide ()
215 {
216         if (_hidden) {
217                 return;
218         }
219
220         _canvas_display->hide ();
221         _canvas_background->hide ();
222
223         if (control_parent) {
224                 control_parent->remove (time_axis_vbox);
225                 control_parent = 0;
226         }
227
228         _y_position = -1;
229         _hidden = true;
230
231         /* now hide children */
232
233         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
234                 (*i)->hide ();
235         }
236
237         /* if its hidden, it cannot be selected */
238         _editor.get_selection().remove (this);
239         /* and neither can its regions */
240         _editor.get_selection().remove_regions (this);
241
242         Hiding ();
243 }
244
245 /** Display this TimeAxisView as the nth component of the parent box, at y.
246 *
247 * @param y y position.
248 * @param nth index for this TimeAxisView, increased if this view has children.
249 * @param parent parent component.
250 * @return height of this TimeAxisView.
251 */
252 guint32
253 TimeAxisView::show_at (double y, int& nth, VBox *parent)
254 {
255         if (control_parent) {
256                 control_parent->reorder_child (time_axis_vbox, nth);
257         } else {
258                 control_parent = parent;
259                 parent->pack_start (time_axis_vbox, false, false);
260                 parent->reorder_child (time_axis_vbox, nth);
261         }
262
263         _order = nth;
264
265         if (_y_position != y) {
266                 _canvas_display->property_y () = y;
267                 _canvas_background->property_y () = y;
268                 /* silly canvas */
269                 _canvas_display->move (0.0, 0.0);
270                 _canvas_background->move (0.0, 0.0);
271                 _y_position = y;
272
273         }
274
275         _canvas_background->raise_to_top ();
276         _canvas_display->raise_to_top ();
277
278         _canvas_background->show ();
279         _canvas_display->show ();
280
281         _hidden = false;
282
283         _effective_height = current_height ();
284
285         /* now show relevant children */
286
287         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
288                 if ((*i)->marked_for_display()) {
289                         ++nth;
290                         _effective_height += (*i)->show_at (y + _effective_height, nth, parent);
291                 } else {
292                         (*i)->hide ();
293                 }
294         }
295
296         return _effective_height;
297 }
298
299 void
300 TimeAxisView::clip_to_viewport ()
301 {
302         if (marked_for_display()) {
303                 if (_y_position + _effective_height < _editor.get_trackview_group_vertical_offset () || _y_position > _editor.get_trackview_group_vertical_offset () + _canvas_display->get_canvas()->get_height()) {
304                         _canvas_background->hide ();
305                         _canvas_display->hide ();
306                         return;
307                 }
308                 _canvas_background->show ();
309                 _canvas_display->show ();
310         }
311         return;
312 }
313
314 bool
315 TimeAxisView::controls_ebox_scroll (GdkEventScroll* ev)
316 {
317         switch (ev->direction) {
318         case GDK_SCROLL_UP:
319                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
320                         step_height (false);
321                         return true;
322                 } else if (Keyboard::no_modifiers_active (ev->state)) {
323                         _editor.scroll_tracks_up_line();
324                         return true;
325                 }
326                 break;
327
328         case GDK_SCROLL_DOWN:
329                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
330                         step_height (true);
331                         return true;
332                 } else if (Keyboard::no_modifiers_active (ev->state)) {
333                         _editor.scroll_tracks_down_line();
334                         return true;
335                 }
336                 break;
337
338         default:
339                 /* no handling for left/right, yet */
340                 break;
341         }
342
343         return false;
344 }
345
346 bool
347 TimeAxisView::controls_ebox_button_press (GdkEventButton* event)
348 {
349         if (maybe_set_cursor (event->y) > 0) {
350                 _resize_drag_start = event->y_root;
351         }
352
353         return true;
354 }
355
356 void
357 TimeAxisView::idle_resize (uint32_t h)
358 {
359         set_height (h);
360 }
361
362
363 bool
364 TimeAxisView::controls_ebox_motion (GdkEventMotion* ev)
365 {
366         if (_resize_drag_start >= 0) {
367                 /* (ab)use the DragManager to do autoscrolling; adjust the event coordinates
368                    into the world coordinate space that DragManager::motion_handler is expecting,
369                    and then fake a DragManager motion event so that when maybe_autoscroll
370                    asks DragManager for the current pointer position it will get the correct
371                    answers.
372                 */
373                 int tx, ty;
374                 controls_ebox.translate_coordinates (*control_parent, ev->x, ev->y, tx, ty);
375                 ev->y = ty - _editor.get_trackview_group_vertical_offset();
376                 _editor.drags()->motion_handler ((GdkEvent *) ev, false);
377                 _editor.maybe_autoscroll (false, true, false, ev->y_root < _resize_drag_start);
378
379                 /* now do the actual TAV resize */
380                 int32_t const delta = (int32_t) floor (ev->y_root - _resize_drag_start);
381                 _editor.add_to_idle_resize (this, delta);
382                 _resize_drag_start = ev->y_root;
383         } else {
384                 /* not dragging but ... */
385                 maybe_set_cursor (ev->y);
386         }
387
388         return true;
389 }
390
391 bool
392 TimeAxisView::controls_ebox_leave (GdkEventCrossing*)
393 {
394         if (_have_preresize_cursor) {
395                 gdk_window_set_cursor (controls_ebox.get_window()->gobj(), _preresize_cursor);
396                 _have_preresize_cursor = false;
397         }
398         return true;
399 }
400
401 bool
402 TimeAxisView::maybe_set_cursor (int y)
403 {
404         /* XXX no Gtkmm Gdk::Window::get_cursor() */
405         Glib::RefPtr<Gdk::Window> win = controls_ebox.get_window();
406
407         if (y > (gint) floor (controls_ebox.get_height() * 0.75)) {
408
409                 /* y-coordinate in lower 25% */
410
411                 if (!_have_preresize_cursor) {
412                         _preresize_cursor = gdk_window_get_cursor (win->gobj());
413                         _have_preresize_cursor = true;
414                         win->set_cursor (Gdk::Cursor(Gdk::SB_V_DOUBLE_ARROW));
415                 }
416
417                 return 1;
418
419         } else if (_have_preresize_cursor) {
420                 gdk_window_set_cursor (win->gobj(), _preresize_cursor);
421                 _have_preresize_cursor = false;
422
423                 return -1;
424         }
425
426         return 0;
427 }
428
429 bool
430 TimeAxisView::controls_ebox_button_release (GdkEventButton* ev)
431 {
432         if (_resize_drag_start >= 0) {
433                 if (_have_preresize_cursor) {
434                         gdk_window_set_cursor (controls_ebox.get_window()->gobj(), _preresize_cursor);
435                         _preresize_cursor = 0;
436                         _have_preresize_cursor = false;
437                 }
438                 _editor.stop_canvas_autoscroll ();
439                 _resize_drag_start = -1;
440         }
441
442         switch (ev->button) {
443         case 1:
444                 selection_click (ev);
445                 break;
446
447         case 3:
448                 popup_display_menu (ev->time);
449                 break;
450         }
451
452         return true;
453 }
454
455 void
456 TimeAxisView::selection_click (GdkEventButton* ev)
457 {
458         Selection::Operation op = ArdourKeyboard::selection_type (ev->state);
459         _editor.set_selected_track (*this, op, false);
460 }
461
462
463 /** Steps through the defined heights for this TrackView.
464  *  @param coarser true if stepping should decrease in size, otherwise false.
465  */
466 void
467 TimeAxisView::step_height (bool coarser)
468 {
469         static const uint32_t step = 25;
470
471         if (coarser) {
472
473                 if (height <= preset_height (HeightSmall)) {
474                         return;
475                 } else if (height <= preset_height (HeightNormal) && height > preset_height (HeightSmall)) {
476                         set_height_enum (HeightSmall);
477                 } else {
478                         set_height (height - step);
479                 }
480
481         } else {
482
483                 if (height <= preset_height(HeightSmall)) {
484                         set_height_enum (HeightNormal);
485                 } else {
486                         set_height (height + step);
487                 }
488
489         }
490 }
491
492 void
493 TimeAxisView::set_height_enum (Height h, bool apply_to_selection)
494 {
495         if (apply_to_selection) {
496                 _editor.get_selection().tracks.foreach_time_axis (boost::bind (&TimeAxisView::set_height_enum, _1, h, false));
497         } else {
498                 set_height (preset_height (h));
499         }
500 }
501
502 void
503 TimeAxisView::set_height (uint32_t h)
504 {
505         if (h < preset_height (HeightSmall)) {
506                 h = preset_height (HeightSmall);
507         }
508
509         time_axis_vbox.property_height_request () = h;
510         height = h;
511
512         char buf[32];
513         snprintf (buf, sizeof (buf), "%u", height);
514         set_gui_property ("height", buf);
515
516         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
517                 (*i)->set_height ();
518         }
519
520         if (canvas_item_visible (selection_group)) {
521                 /* resize the selection rect */
522                 show_selection (_editor.get_selection().time);
523         }
524 }
525
526 bool
527 TimeAxisView::name_entry_key_release (GdkEventKey* ev)
528 {
529         TrackViewList::iterator i;
530
531         switch (ev->keyval) {
532         case GDK_Escape:
533                 name_entry.select_region (0,0);
534                 controls_ebox.grab_focus ();
535                 name_entry_changed ();
536                 return true;
537
538         /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
539          * generates a different ev->keyval, rather than setting
540          * ev->state.
541          */
542         case GDK_ISO_Left_Tab:
543         case GDK_Tab:
544         {
545                 name_entry_changed ();
546                 TrackViewList const & allviews = _editor.get_track_views ();
547                 TrackViewList::const_iterator i = find (allviews.begin(), allviews.end(), this);
548
549                 if (ev->keyval == GDK_Tab) {
550                         if (i != allviews.end()) {
551                                 do {
552                                         if (++i == allviews.end()) {
553                                                 return true;
554                                         }
555
556                                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*>(*i);
557
558                                         if (rtav && rtav->route()->record_enabled()) {
559                                                 continue;
560                                         }
561
562                                         if (!(*i)->hidden()) {
563                                                 break;
564                                         }
565
566                                 } while (true);
567                         }
568                 } else {
569                         if (i != allviews.begin()) {
570                                 do {
571                                         if (i == allviews.begin()) {
572                                                 return true;
573                                         }
574
575                                         --i;
576
577                                         RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*>(*i);
578
579                                         if (rtav && rtav->route()->record_enabled()) {
580                                                 continue;
581                                         }
582
583                                         if (!(*i)->hidden()) {
584                                                 break;
585                                         }
586
587                                 } while (true);
588                         }
589                 }
590
591                 if ((i != allviews.end()) && (*i != this) && !(*i)->hidden()) {
592                         (*i)->name_entry.grab_focus();
593                         _editor.ensure_time_axis_view_is_visible (**i);
594                 }
595         }
596         return true;
597
598         case GDK_Up:
599         case GDK_Down:
600                 name_entry_changed ();
601                 return true;
602
603         default:
604                 break;
605         }
606
607 #ifdef TIMEOUT_NAME_EDIT
608         /* adapt the timeout to reflect the user's typing speed */
609
610         guint32 name_entry_timeout;
611
612         if (last_name_entry_key_press_event) {
613                 /* timeout is 1/2 second or 5 times their current inter-char typing speed */
614                 name_entry_timeout = std::max (500U, (5 * (ev->time - last_name_entry_key_press_event)));
615         } else {
616                 /* start with a 1 second timeout */
617                 name_entry_timeout = 1000;
618         }
619
620         last_name_entry_key_press_event = ev->time;
621
622         /* wait 1 seconds and if no more keys are pressed, act as if they pressed enter */
623
624         name_entry_key_timeout.disconnect();
625         name_entry_key_timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &TimeAxisView::name_entry_key_timed_out), name_entry_timeout);
626 #endif
627
628         return false;
629 }
630
631 bool
632 TimeAxisView::name_entry_focus_in (GdkEventFocus*)
633 {
634         name_entry.select_region (0, -1);
635         name_entry.set_state (STATE_SELECTED);
636         return false;
637 }
638
639 bool
640 TimeAxisView::name_entry_focus_out (GdkEventFocus*)
641 {
642         /* clean up */
643
644         last_name_entry_key_press_event = 0;
645         name_entry_key_timeout.disconnect ();
646         name_entry.select_region (0,0);
647         name_entry.set_state (STATE_NORMAL);
648
649         /* do the real stuff */
650
651         name_entry_changed ();
652
653         return false;
654 }
655
656 bool
657 TimeAxisView::name_entry_key_timed_out ()
658 {
659         name_entry_activated();
660         return false;
661 }
662
663 void
664 TimeAxisView::name_entry_activated ()
665 {
666         controls_ebox.grab_focus();
667 }
668
669 void
670 TimeAxisView::name_entry_changed ()
671 {
672 }
673
674 bool
675 TimeAxisView::name_entry_button_press (GdkEventButton *ev)
676 {
677         if (ev->button == 3) {
678                 return true;
679         }
680         return false;
681 }
682
683 bool
684 TimeAxisView::name_entry_button_release (GdkEventButton *ev)
685 {
686         if (ev->button == 3) {
687                 popup_display_menu (ev->time);
688                 return true;
689         }
690         return false;
691 }
692
693 void
694 TimeAxisView::conditionally_add_to_selection ()
695 {
696         Selection& s (_editor.get_selection ());
697
698         if (!s.selected (this)) {
699                 _editor.set_selected_track (*this, Selection::Set);
700         }
701 }
702
703 void
704 TimeAxisView::popup_display_menu (guint32 when)
705 {
706         conditionally_add_to_selection ();
707
708         build_display_menu ();
709         display_menu->popup (1, when);
710 }
711
712 void
713 TimeAxisView::set_selected (bool yn)
714 {
715         if (yn == _selected) {
716                 return;
717         }
718
719         Selectable::set_selected (yn);
720
721         if (_selected) {
722                 controls_ebox.set_name (controls_base_selected_name);
723                 time_axis_vbox.set_name (controls_base_selected_name);
724                 controls_vbox.set_name (controls_base_selected_name);
725         } else {
726                 controls_ebox.set_name (controls_base_unselected_name);
727                 time_axis_vbox.set_name (controls_base_unselected_name);
728                 controls_vbox.set_name (controls_base_unselected_name);
729                 hide_selection ();
730
731                 /* children will be set for the yn=true case. but when deselecting
732                    the editor only has a list of top-level trackviews, so we
733                    have to do this here.
734                 */
735
736                 for (Children::iterator i = children.begin(); i != children.end(); ++i) {
737                         (*i)->set_selected (false);
738                 }
739         }
740 }
741
742 void
743 TimeAxisView::build_display_menu ()
744 {
745         using namespace Menu_Helpers;
746
747         delete display_menu;
748
749         display_menu = new Menu;
750         display_menu->set_name ("ArdourContextMenu");
751
752         // Just let implementing classes define what goes into the manu
753 }
754
755 void
756 TimeAxisView::set_samples_per_unit (double spu)
757 {
758         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
759                 (*i)->set_samples_per_unit (spu);
760         }
761
762         AnalysisFeatureList::const_iterator i;
763         list<ArdourCanvas::SimpleLine*>::iterator l;
764 }
765
766 void
767 TimeAxisView::show_timestretch (framepos_t start, framepos_t end, int layers, int layer)
768 {
769         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
770                 (*i)->show_timestretch (start, end, layers, layer);
771         }
772 }
773
774 void
775 TimeAxisView::hide_timestretch ()
776 {
777         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
778                 (*i)->hide_timestretch ();
779         }
780 }
781
782 void
783 TimeAxisView::show_selection (TimeSelection& ts)
784 {
785         double x1;
786         double x2;
787         double y2;
788         SelectionRect *rect;
789
790         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
791                 (*i)->show_selection (ts);
792         }
793
794         if (canvas_item_visible (selection_group)) {
795                 while (!used_selection_rects.empty()) {
796                         free_selection_rects.push_front (used_selection_rects.front());
797                         used_selection_rects.pop_front();
798                         free_selection_rects.front()->rect->hide();
799                         free_selection_rects.front()->start_trim->hide();
800                         free_selection_rects.front()->end_trim->hide();
801                 }
802                 selection_group->hide();
803         }
804
805         selection_group->show();
806         selection_group->raise_to_top();
807
808         for (list<AudioRange>::iterator i = ts.begin(); i != ts.end(); ++i) {
809                 framepos_t start, end;
810                 framecnt_t cnt;
811
812                 start = (*i).start;
813                 end = (*i).end;
814                 cnt = end - start + 1;
815
816                 rect = get_selection_rect ((*i).id);
817
818                 x1 = _editor.frame_to_unit (start);
819                 x2 = _editor.frame_to_unit (start + cnt - 1);
820                 y2 = current_height();
821
822                 rect->rect->property_x1() = x1;
823                 rect->rect->property_y1() = 1.0;
824                 rect->rect->property_x2() = x2;
825                 rect->rect->property_y2() = y2;
826
827                 // trim boxes are at the top for selections
828
829                 if (x2 > x1) {
830                         rect->start_trim->property_x1() = x1;
831                         rect->start_trim->property_y1() = 1.0;
832                         rect->start_trim->property_x2() = x1 + trim_handle_size;
833                         rect->start_trim->property_y2() = y2;
834
835                         rect->end_trim->property_x1() = x2 - trim_handle_size;
836                         rect->end_trim->property_y1() = 1.0;
837                         rect->end_trim->property_x2() = x2;
838                         rect->end_trim->property_y2() = y2;
839
840                         rect->start_trim->show();
841                         rect->end_trim->show();
842                 } else {
843                         rect->start_trim->hide();
844                         rect->end_trim->hide();
845                 }
846
847                 rect->rect->show ();
848                 used_selection_rects.push_back (rect);
849         }
850 }
851
852 void
853 TimeAxisView::reshow_selection (TimeSelection& ts)
854 {
855         show_selection (ts);
856
857         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
858                 (*i)->show_selection (ts);
859         }
860 }
861
862 void
863 TimeAxisView::hide_selection ()
864 {
865         if (canvas_item_visible (selection_group)) {
866                 while (!used_selection_rects.empty()) {
867                         free_selection_rects.push_front (used_selection_rects.front());
868                         used_selection_rects.pop_front();
869                         free_selection_rects.front()->rect->hide();
870                         free_selection_rects.front()->start_trim->hide();
871                         free_selection_rects.front()->end_trim->hide();
872                 }
873                 selection_group->hide();
874         }
875
876         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
877                 (*i)->hide_selection ();
878         }
879 }
880
881 void
882 TimeAxisView::order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top)
883 {
884         /* find the selection rect this is for. we have the item corresponding to one
885            of the trim handles.
886          */
887
888         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
889                 if ((*i)->start_trim == item || (*i)->end_trim == item) {
890
891                         /* make one trim handle be "above" the other so that if they overlap,
892                            the top one is the one last used.
893                         */
894
895                         (*i)->rect->raise_to_top ();
896                         (put_start_on_top ? (*i)->start_trim : (*i)->end_trim)->raise_to_top ();
897                         (put_start_on_top ? (*i)->end_trim : (*i)->start_trim)->raise_to_top ();
898
899                         break;
900                 }
901         }
902 }
903
904 SelectionRect *
905 TimeAxisView::get_selection_rect (uint32_t id)
906 {
907         SelectionRect *rect;
908
909         /* check to see if we already have a visible rect for this particular selection ID */
910
911         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
912                 if ((*i)->id == id) {
913                         return (*i);
914                 }
915         }
916
917         /* ditto for the free rect list */
918
919         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
920                 if ((*i)->id == id) {
921                         SelectionRect* ret = (*i);
922                         free_selection_rects.erase (i);
923                         return ret;
924                 }
925         }
926
927         /* no existing matching rect, so go get a new one from the free list, or create one if there are none */
928
929         if (free_selection_rects.empty()) {
930
931                 rect = new SelectionRect;
932
933                 rect->rect = new SimpleRect (*selection_group);
934                 rect->rect->property_outline_what() = 0x0;
935                 rect->rect->property_x1() = 0.0;
936                 rect->rect->property_y1() = 0.0;
937                 rect->rect->property_x2() = 0.0;
938                 rect->rect->property_y2() = 0.0;
939                 rect->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
940
941                 rect->start_trim = new SimpleRect (*selection_group);
942                 rect->start_trim->property_outline_what() = 0x0;
943                 rect->start_trim->property_x1() = 0.0;
944                 rect->start_trim->property_x2() = 0.0;
945
946                 rect->end_trim = new SimpleRect (*selection_group);
947                 rect->end_trim->property_outline_what() = 0x0;
948                 rect->end_trim->property_x1() = 0.0;
949                 rect->end_trim->property_x2() = 0.0;
950
951                 free_selection_rects.push_front (rect);
952
953                 rect->rect->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_rect_event), rect->rect, rect));
954                 rect->start_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_start_trim_event), rect->rect, rect));
955                 rect->end_trim->signal_event().connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_selection_end_trim_event), rect->rect, rect));
956         }
957
958         rect = free_selection_rects.front();
959         rect->id = id;
960         free_selection_rects.pop_front();
961         return rect;
962 }
963
964 struct null_deleter { void operator()(void const *) const {} };
965
966 bool
967 TimeAxisView::is_child (TimeAxisView* tav)
968 {
969         return find (children.begin(), children.end(), boost::shared_ptr<TimeAxisView>(tav, null_deleter())) != children.end();
970 }
971
972 void
973 TimeAxisView::add_child (boost::shared_ptr<TimeAxisView> child)
974 {
975         children.push_back (child);
976 }
977
978 void
979 TimeAxisView::remove_child (boost::shared_ptr<TimeAxisView> child)
980 {
981         Children::iterator i;
982
983         if ((i = find (children.begin(), children.end(), child)) != children.end()) {
984                 children.erase (i);
985         }
986 }
987
988 /** Get selectable things within a given range.
989  *  @param start Start time in session frames.
990  *  @param end End time in session frames.
991  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
992  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
993  *  @param result Filled in with selectable things.
994  */
995 void
996 TimeAxisView::get_selectables (framepos_t /*start*/, framepos_t /*end*/, double /*top*/, double /*bot*/, list<Selectable*>& /*result*/)
997 {
998         return;
999 }
1000
1001 void
1002 TimeAxisView::get_inverted_selectables (Selection& /*sel*/, list<Selectable*>& /*result*/)
1003 {
1004         return;
1005 }
1006
1007 void
1008 TimeAxisView::add_ghost (RegionView* rv)
1009 {
1010         GhostRegion* gr = rv->add_ghost (*this);
1011
1012         if (gr) {
1013                 ghosts.push_back(gr);
1014         }
1015 }
1016
1017 void
1018 TimeAxisView::remove_ghost (RegionView* rv)
1019 {
1020         rv->remove_ghost_in (*this);
1021 }
1022
1023 void
1024 TimeAxisView::erase_ghost (GhostRegion* gr)
1025 {
1026         if (in_destructor) {
1027                 return;
1028         }
1029
1030         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1031                 if ((*i) == gr) {
1032                         ghosts.erase (i);
1033                         break;
1034                 }
1035         }
1036 }
1037
1038 bool
1039 TimeAxisView::touched (double top, double bot)
1040 {
1041         /* remember: this is X Window - coordinate space starts in upper left and moves down.
1042           y_position is the "origin" or "top" of the track.
1043         */
1044
1045         double mybot = _y_position + current_height();
1046
1047         return ((_y_position <= bot && _y_position >= top) ||
1048                 ((mybot <= bot) && (top < mybot)) ||
1049                 (mybot >= bot && _y_position < top));
1050 }
1051
1052 void
1053 TimeAxisView::set_parent (TimeAxisView& p)
1054 {
1055         parent = &p;
1056 }
1057
1058 void
1059 TimeAxisView::reset_height ()
1060 {
1061         set_height (height);
1062
1063         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1064                 (*i)->set_height ((*i)->height);
1065         }
1066 }
1067
1068 void
1069 TimeAxisView::compute_heights ()
1070 {
1071         Gtk::Window window (Gtk::WINDOW_TOPLEVEL);
1072         Gtk::Table two_row_table (2, 8);
1073         Gtk::Table one_row_table (1, 8);
1074         Button* buttons[5];
1075         const int border_width = 2;
1076
1077         const int separator_height = 2;
1078         extra_height = (2 * border_width) + separator_height;
1079
1080         window.add (one_row_table);
1081
1082         one_row_table.set_border_width (border_width);
1083         one_row_table.set_row_spacings (0);
1084         one_row_table.set_col_spacings (0);
1085         one_row_table.set_homogeneous (true);
1086
1087         two_row_table.set_border_width (border_width);
1088         two_row_table.set_row_spacings (0);
1089         two_row_table.set_col_spacings (0);
1090         two_row_table.set_homogeneous (true);
1091
1092         for (int i = 0; i < 5; ++i) {
1093                 buttons[i] = manage (new Button (X_("f")));
1094                 buttons[i]->set_name ("TrackMuteButton");
1095         }
1096
1097         one_row_table.attach (*buttons[0], 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
1098
1099         one_row_table.show_all ();
1100         Gtk::Requisition req(one_row_table.size_request ());
1101
1102         // height required to show 1 row of buttons
1103         button_height = req.height;
1104 }
1105
1106 void
1107 TimeAxisView::show_name_label ()
1108 {
1109         if (!(name_packing & NameLabelPacked)) {
1110                 name_hbox.pack_start (name_label, true, true);
1111                 name_packing = NamePackingBits (name_packing | NameLabelPacked);
1112                 name_hbox.show ();
1113                 name_label.show ();
1114         }
1115 }
1116
1117 void
1118 TimeAxisView::show_name_entry ()
1119 {
1120         if (!(name_packing & NameEntryPacked)) {
1121                 name_hbox.pack_start (name_entry, true, true);
1122                 name_packing = NamePackingBits (name_packing | NameEntryPacked);
1123                 name_hbox.show ();
1124                 name_entry.show ();
1125         }
1126 }
1127
1128 void
1129 TimeAxisView::hide_name_label ()
1130 {
1131         if (name_packing & NameLabelPacked) {
1132                 name_hbox.remove (name_label);
1133                 name_packing = NamePackingBits (name_packing & ~NameLabelPacked);
1134         }
1135 }
1136
1137 void
1138 TimeAxisView::hide_name_entry ()
1139 {
1140         if (name_packing & NameEntryPacked) {
1141                 name_hbox.remove (name_entry);
1142                 name_packing = NamePackingBits (name_packing & ~NameEntryPacked);
1143         }
1144 }
1145
1146 void
1147 TimeAxisView::color_handler ()
1148 {
1149         for (list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); i++) {
1150                 (*i)->set_colors();
1151         }
1152
1153         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
1154
1155                 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1156                 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1157
1158                 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1159                 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1160
1161                 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1162                 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1163         }
1164
1165         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
1166
1167                 (*i)->rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SelectionRect.get();
1168                 (*i)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1169
1170                 (*i)->start_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1171                 (*i)->start_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1172
1173                 (*i)->end_trim->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1174                 (*i)->end_trim->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_Selection.get();
1175         }
1176 }
1177
1178 /** @return Pair: TimeAxisView, layer index.
1179  * TimeAxisView is non-0 if this object covers y, or one of its children does.
1180  * If the covering object is a child axis, then the child is returned.
1181  * TimeAxisView is 0 otherwise.
1182  * Layer index is the layer number (possibly fractional) if the TimeAxisView is valid
1183  * and is in stacked or expanded * region display mode, otherwise 0.
1184  */
1185 std::pair<TimeAxisView*, double>
1186 TimeAxisView::covers_y_position (double y)
1187 {
1188         if (hidden()) {
1189                 return std::make_pair ((TimeAxisView *) 0, 0);
1190         }
1191
1192         if (_y_position <= y && y < (_y_position + height)) {
1193
1194                 /* work out the layer index if appropriate */
1195                 double l = 0;
1196                 switch (layer_display ()) {
1197                 case Overlaid:
1198                         break;
1199                 case Stacked:
1200                         if (view ()) {
1201                                 /* compute layer */
1202                                 l = layer_t ((_y_position + height - y) / (view()->child_height ()));
1203                                 /* clamp to max layers to be on the safe side; sometimes the above calculation
1204                                    returns a too-high value */
1205                                 if (l >= view()->layers ()) {
1206                                         l = view()->layers() - 1;
1207                                 }
1208                         }
1209                         break;
1210                 case Expanded:
1211                         if (view ()) {
1212                                 int const n = floor ((_y_position + height - y) / (view()->child_height ()));
1213                                 l = n * 0.5 - 0.5;
1214                                 if (l >= (view()->layers() - 0.5)) {
1215                                         l = view()->layers() - 0.5;
1216                                 }
1217                         }
1218                         break;
1219                 }
1220
1221                 return std::make_pair (this, l);
1222         }
1223
1224         for (Children::const_iterator i = children.begin(); i != children.end(); ++i) {
1225
1226                 std::pair<TimeAxisView*, int> const r = (*i)->covers_y_position (y);
1227                 if (r.first) {
1228                         return r;
1229                 }
1230         }
1231
1232         return std::make_pair ((TimeAxisView *) 0, 0);
1233 }
1234
1235
1236 uint32_t
1237 TimeAxisView::preset_height (Height h)
1238 {
1239         switch (h) {
1240         case HeightLargest:
1241                 return (button_height * 2) + extra_height + 250;
1242         case HeightLarger:
1243                 return (button_height * 2) + extra_height + 150;
1244         case HeightLarge:
1245                 return (button_height * 2) + extra_height + 50;
1246         case HeightNormal:
1247                 return (button_height * 2) + extra_height;
1248         case HeightSmall:
1249                 return button_height + extra_height;
1250         }
1251
1252         /* NOTREACHED */
1253         return 0;
1254 }
1255
1256 /** @return Child time axis views that are not hidden */
1257 TimeAxisView::Children
1258 TimeAxisView::get_child_list ()
1259 {
1260         Children c;
1261
1262         for (Children::iterator i = children.begin(); i != children.end(); ++i) {
1263                 if (!(*i)->hidden()) {
1264                         c.push_back(*i);
1265                 }
1266         }
1267
1268         return c;
1269 }
1270
1271 void
1272 TimeAxisView::build_size_menu ()
1273 {
1274         if (_size_menu && _size_menu->gobj ()) {
1275                 return;
1276         }
1277
1278         delete _size_menu;
1279
1280         using namespace Menu_Helpers;
1281
1282         _size_menu = new Menu;
1283         _size_menu->set_name ("ArdourContextMenu");
1284         MenuList& items = _size_menu->items();
1285
1286         items.push_back (MenuElem (_("Largest"), sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLargest, true)));
1287         items.push_back (MenuElem (_("Larger"),  sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLarger, true)));
1288         items.push_back (MenuElem (_("Large"),   sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightLarge, true)));
1289         items.push_back (MenuElem (_("Normal"),  sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightNormal, true)));
1290         items.push_back (MenuElem (_("Small"),   sigc::bind (sigc::mem_fun (*this, &TimeAxisView::set_height_enum), HeightSmall, true)));
1291 }
1292
1293 void
1294 TimeAxisView::reset_visual_state ()
1295 {
1296         /* this method is not required to trigger a global redraw */
1297
1298         string str = gui_property ("height");
1299         
1300         if (!str.empty()) {
1301                 set_height (atoi (str));
1302         } else {
1303                 set_height (preset_height (HeightNormal));
1304         }
1305 }
1306
1307 TrackViewList
1308 TrackViewList::filter_to_unique_playlists ()
1309 {
1310         std::set<boost::shared_ptr<ARDOUR::Playlist> > playlists;
1311         TrackViewList ts;
1312
1313         for (iterator i = begin(); i != end(); ++i) {
1314                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
1315                 if (!rtav) {
1316                         /* not a route: include it anyway */
1317                         ts.push_back (*i);
1318                 } else {
1319                         boost::shared_ptr<ARDOUR::Track> t = rtav->track();
1320                         if (t) {
1321                                 if (playlists.insert (t->playlist()).second) {
1322                                         /* playlist not seen yet */
1323                                         ts.push_back (*i);
1324                                 }
1325                         } else {
1326                                 /* not a track: include it anyway */
1327                                 ts.push_back (*i);
1328                         }
1329                 }
1330         }
1331         return ts;
1332 }