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