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