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