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