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