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