2 * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
7 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
8 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2014-2016 Nick Mainsbridge <mainsbridge@gmail.com>
10 * Copyright (C) 2014 Ben Loftis <ben@harrisonconsoles.com>
11 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "pbd/error.h"
31 #include "pbd/stacktrace.h"
33 #include "ardour/types.h"
34 #include "ardour/ardour.h"
36 #include "gtkmm2ext/utils.h"
37 #include "gtkmm2ext/gui_thread.h"
39 #include "canvas/container.h"
40 #include "canvas/rectangle.h"
41 #include "canvas/debug.h"
42 #include "canvas/text.h"
43 #include "gtkmm2ext/colors.h"
45 #include "ardour/profile.h"
47 #include "public_editor.h"
48 #include "time_axis_view_item.h"
49 #include "time_axis_view.h"
50 #include "ui_config.h"
52 #include "rgb_macros.h"
57 using namespace Editing;
60 using namespace ARDOUR;
61 using namespace ARDOUR_UI_UTILS;
62 using namespace Gtkmm2ext;
64 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
65 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
66 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 0.0;
67 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 10.0;
69 int TimeAxisViewItem::NAME_HEIGHT;
70 double TimeAxisViewItem::NAME_Y_OFFSET;
71 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
72 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
75 TimeAxisViewItem::set_constant_heights ()
77 NAME_FONT = Pango::FontDescription (UIConfiguration::instance().get_SmallFont());
83 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
87 layout->set_font_description (NAME_FONT);
88 get_pixel_size (layout, width, height);
90 layout = foo.create_pango_layout (X_("H")); /* just the ascender */
94 /* Config->get_show_name_highlight) == true:
95 Y_OFFSET is measured from bottom of the time axis view item.
96 Config->get_show_name_highlight) == false:
97 Y_OFFSET is measured from the top of the time axis view item.
100 if (UIConfiguration::instance().get_show_name_highlight()) {
101 NAME_Y_OFFSET = height + 1;
102 NAME_HIGHLIGHT_SIZE = height + 2;
105 NAME_HIGHLIGHT_SIZE = 0;
107 NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 3;
111 * Construct a new TimeAxisViewItem.
113 * @param it_name the unique name of this item
114 * @param parent the parent canvas group
115 * @param tv the TimeAxisView we are going to be added to
116 * @param spu samples per unit
118 * @param start the start point of this item
119 * @param duration the duration of this item
120 * @param recording true if this is a recording region view
121 * @param automation true if this is an automation region view
123 TimeAxisViewItem::TimeAxisViewItem(
124 const string & it_name, ArdourCanvas::Item& parent, TimeAxisView& tv, double spu, uint32_t base_color,
125 samplepos_t start, samplecnt_t duration, bool recording, bool automation, Visibility vis
128 , sample_position (-1)
129 , item_name (it_name)
130 , selection_frame (0)
132 , _recregion (recording)
133 , _automation (automation)
137 init (&parent, spu, base_color, start, duration, vis, true, true);
140 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
143 , PBD::ScopedConnectionList()
144 , trackview (other.trackview)
145 , sample_position (-1)
146 , item_name (other.item_name)
147 , selection_frame (0)
149 , _recregion (other._recregion)
150 , _automation (other._automation)
151 , _dragging (other._dragging)
154 /* share the other's parent, but still create a new group */
156 ArdourCanvas::Item* parent = other.group->parent();
158 _selected = other._selected;
160 init (parent, other.samples_per_pixel, other.fill_color, other.sample_position,
161 other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
165 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color,
166 samplepos_t start, samplepos_t duration, Visibility vis,
167 bool wide, bool high)
169 group = new ArdourCanvas::Container (parent);
170 CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
172 fill_color = base_color;
173 fill_color_name = "time axis view item base";
174 samples_per_pixel = fpp;
175 sample_position = start;
176 item_duration = duration;
177 name_connected = false;
178 position_locked = false;
179 max_item_duration = Temporal::max_samplepos;
180 min_item_duration = 0;
185 wide_enough_for_name = wide;
186 high_enough_for_name = high;
189 warning << "Time Axis Item Duration == 0" << endl;
192 if (visibility & ShowFrame) {
193 frame = new ArdourCanvas::Rectangle (group,
194 ArdourCanvas::Rect (0.0, 0.0,
195 trackview.editor().sample_to_pixel(duration),
196 trackview.current_height()));
198 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
201 CANVAS_DEBUG_NAME (frame, string_compose ("frame for %1", get_item_name()));
204 frame->set_outline_color (UIConfiguration::instance().color ("recording rect"));
206 frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
210 if (UIConfiguration::instance().get_show_name_highlight() && (visibility & ShowNameHighlight)) {
212 /* rectangle size will be set in ::manage_name_highlight() */
213 name_highlight = new ArdourCanvas::Rectangle (group);
214 CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
215 name_highlight->set_data ("timeaxisviewitem", this);
216 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
217 name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255)); // this should use a theme color
223 if (visibility & ShowNameText) {
224 name_text = new ArdourCanvas::Text (group);
225 CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
226 if (UIConfiguration::instance().get_show_name_highlight()) {
227 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
229 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
231 name_text->set_font_description (NAME_FONT);
232 name_text->set_ignore_events (true);
237 /* create our grab handles used for trimming/duration etc */
238 if (!_recregion && !_automation) {
239 double top = TimeAxisViewItem::GRAB_HANDLE_TOP;
240 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
242 frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
243 CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
244 frame_handle_start->set_outline (false);
245 frame_handle_start->set_fill (false);
246 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
248 frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
249 CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
250 frame_handle_end->set_outline (false);
251 frame_handle_end->set_fill (false);
252 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
254 frame_handle_start = frame_handle_end = 0;
257 //set_color (base_color);
259 //set_duration (item_duration, this);
260 //set_position (start, this);
262 group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
263 //Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
264 UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
267 TimeAxisViewItem::~TimeAxisViewItem()
273 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
279 * Set the position of this item on the timeline.
281 * @param pos the new position
282 * @param src the identity of the object that initiated the change
283 * @return true on success
287 TimeAxisViewItem::set_position(samplepos_t pos, void* src, double* delta)
289 if (position_locked) {
293 sample_position = pos;
295 double new_unit_pos = trackview.editor().sample_to_pixel (pos);
298 (*delta) = new_unit_pos - group->position().x;
303 if (new_unit_pos == group->position().x) {
308 group->set_x_position (new_unit_pos);
309 PositionChanged (sample_position, src); /* EMIT_SIGNAL */
314 /** @return position of this item on the timeline */
316 TimeAxisViewItem::get_position() const
318 return sample_position;
322 * Set the duration of this item.
324 * @param dur the new duration of this item
325 * @param src the identity of the object that initiated the change
326 * @return true on success
330 TimeAxisViewItem::set_duration (samplecnt_t dur, void* src)
332 if ((dur > max_item_duration) || (dur < min_item_duration)) {
333 warning << string_compose (
334 P_("new duration %1 frame is out of bounds for %2", "new duration of %1 samples is out of bounds for %2", dur),
335 get_item_name(), dur)
346 double end_pixel = trackview.editor().sample_to_pixel (sample_position + dur);
347 double first_pixel = trackview.editor().sample_to_pixel (sample_position);
349 reset_width_dependent_items (end_pixel - first_pixel);
351 DurationChanged (dur, src); /* EMIT_SIGNAL */
355 /** @return duration of this item */
357 TimeAxisViewItem::get_duration() const
359 return item_duration;
363 * Set the maximum duration that this item can have.
365 * @param dur the new maximum duration
366 * @param src the identity of the object that initiated the change
369 TimeAxisViewItem::set_max_duration(samplecnt_t dur, void* src)
371 max_item_duration = dur;
372 MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
375 /** @return the maximum duration that this item may have */
377 TimeAxisViewItem::get_max_duration() const
379 return max_item_duration;
383 * Set the minimum duration that this item may have.
385 * @param the minimum duration that this item may be set to
386 * @param src the identity of the object that initiated the change
389 TimeAxisViewItem::set_min_duration(samplecnt_t dur, void* src)
391 min_item_duration = dur;
392 MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
395 /** @return the minimum duration that this item mey have */
397 TimeAxisViewItem::get_min_duration() const
399 return min_item_duration;
403 * Set whether this item is locked to its current position.
404 * Locked items cannot be moved until the item is unlocked again.
406 * @param yn true to lock this item to its current position
407 * @param src the identity of the object that initiated the change
410 TimeAxisViewItem::set_position_locked(bool yn, void* src)
412 position_locked = yn;
413 set_trim_handle_colors();
414 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
417 /** @return true if this item is locked to its current position */
419 TimeAxisViewItem::get_position_locked() const
421 return position_locked;
425 * Set whether the maximum duration constraint is active.
427 * @param active set true to enforce the max duration constraint
428 * @param src the identity of the object that initiated the change
431 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
433 max_duration_active = active;
436 /** @return true if the maximum duration constraint is active */
438 TimeAxisViewItem::get_max_duration_active() const
440 return max_duration_active;
444 * Set whether the minimum duration constraint is active.
446 * @param active set true to enforce the min duration constraint
447 * @param src the identity of the object that initiated the change
451 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
453 min_duration_active = active;
456 /** @return true if the maximum duration constraint is active */
458 TimeAxisViewItem::get_min_duration_active() const
460 return min_duration_active;
464 * Set the name of this item.
466 * @param new_name the new name of this item
467 * @param src the identity of the object that initiated the change
471 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
473 if (new_name != item_name) {
474 std::string temp_name = item_name;
475 item_name = new_name;
476 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
480 /** @return the name of this item */
482 TimeAxisViewItem::get_item_name() const
488 * Set selection status.
490 * @param yn true if this item is currently selected
493 TimeAxisViewItem::set_selected(bool yn)
495 if (_selected == yn) {
499 Selectable::set_selected (yn);
501 set_name_text_color ();
503 if (_selected && frame) {
504 if (!selection_frame) {
505 selection_frame = new ArdourCanvas::Rectangle (group);
506 selection_frame->set_fill (false);
507 selection_frame->set_outline_color (UIConfiguration::instance().color ("selected time axis frame"));
508 selection_frame->set_ignore_events (true);
510 selection_frame->set (frame->get().shrink (1.0));
511 selection_frame->show ();
513 if (selection_frame) {
514 selection_frame->hide ();
519 /** @return the TimeAxisView that this item is on */
521 TimeAxisViewItem::get_time_axis_view () const
527 * Set the displayed item text.
528 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
530 * @param new_name the new name text to display
534 TimeAxisViewItem::set_name_text(const string& new_name)
540 name_text_width = pixel_width (new_name, NAME_FONT) + 2;
541 name_text->set (new_name);
543 manage_name_highlight ();
547 * Set the height of this item.
549 * @param h new height
552 TimeAxisViewItem::set_height (double height)
556 manage_name_highlight ();
558 if (visibility & ShowNameText) {
559 if (UIConfiguration::instance().get_show_name_highlight()) {
560 name_text->set_y_position (height - NAME_Y_OFFSET);
562 name_text->set_y_position (NAME_Y_OFFSET);
569 frame->set_y1 (height);
571 if (frame_handle_start) {
572 frame_handle_start->set_y1 (height);
573 frame_handle_end->set_y1 (height);
576 if (selection_frame) {
577 selection_frame->set (frame->get().shrink (1.0));
583 TimeAxisViewItem::manage_name_highlight ()
585 if (!name_highlight) {
589 if (_height < NAME_HIGHLIGHT_THRESH) {
590 high_enough_for_name = false;
592 high_enough_for_name = true;
596 wide_enough_for_name = false;
598 wide_enough_for_name = true;
601 if (name_highlight && wide_enough_for_name && high_enough_for_name) {
603 name_highlight->show();
604 // name_highlight->set_x_position (1.0);
605 name_highlight->set (ArdourCanvas::Rect (0.0, (double) _height - NAME_HIGHLIGHT_SIZE, _width - 2.0, _height));
608 name_highlight->hide();
615 TimeAxisViewItem::set_color (uint32_t base_color)
617 fill_color = base_color;
622 TimeAxisViewItem::get_canvas_frame()
628 TimeAxisViewItem::get_canvas_group()
634 TimeAxisViewItem::get_name_highlight()
636 return name_highlight;
640 * Convenience method to set the various canvas item colors
643 TimeAxisViewItem::set_colors()
647 if (name_highlight) {
648 name_highlight->set_fill_color (fill_color);
651 set_name_text_color ();
652 set_trim_handle_colors();
656 TimeAxisViewItem::set_name_text_color ()
665 if (UIConfiguration::instance().get_show_name_highlight()) {
666 /* name text will always be on top of name highlight, which
667 will always use our fill color.
671 /* name text will be on top of the item, whose color
672 may vary depending on various conditions.
674 f = get_fill_color ();
677 name_text->set_color (contrasting_text_color (f));
681 TimeAxisViewItem::get_fill_color () const
683 const std::string mod_name = (_dragging ? "dragging region" : fill_color_name);
686 return UIConfiguration::instance().color ("selected region base");
687 } else if (_recregion) {
688 return UIConfiguration::instance().color ("recording rect");
689 } else if ((!UIConfiguration::instance().get_show_name_highlight() || high_enough_for_name) &&
690 !UIConfiguration::instance().get_color_regions_using_track_color()) {
691 return UIConfiguration::instance().color_mod (fill_color_name, mod_name);
693 return UIConfiguration::instance().color_mod (fill_color, mod_name);
697 * Sets the frame color depending on whether this item is selected
700 TimeAxisViewItem::set_frame_color()
706 frame->set_fill_color (get_fill_color());
707 set_frame_gradient ();
710 frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
715 TimeAxisViewItem::set_frame_gradient ()
717 if (UIConfiguration::instance().get_timeline_item_gradient_depth() == 0.0) {
718 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
722 ArdourCanvas::Fill::StopList stops;
725 Color f (get_fill_color());
727 /* need to get alpha value */
728 color_to_rgba (f, r, g, b, a);
730 stops.push_back (std::make_pair (0.0, f));
732 /* now a darker version */
734 color_to_hsv (f, h, s, v);
736 v = min (1.0, v * (1.0 - UIConfiguration::instance().get_timeline_item_gradient_depth()));
738 Color darker = hsva_to_color (h, s, v, a);
739 stops.push_back (std::make_pair (1.0, darker));
741 frame->set_gradient (stops, true);
745 * Set the colors of the start and end trim handle depending on object state
748 TimeAxisViewItem::set_trim_handle_colors()
751 /* Leave them transparent for now */
752 if (frame_handle_start) {
753 frame_handle_start->set_fill_color (0x00000000);
754 frame_handle_end->set_fill_color (0x00000000);
757 if (frame_handle_start) {
758 if (position_locked) {
759 frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
760 frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
762 frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandle());
763 frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandle());
770 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
773 case GDK_LEAVE_NOTIFY:
774 /* always hide the handle whenever we leave, no matter what mode */
775 item->set_fill (false);
777 case GDK_ENTER_NOTIFY:
778 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject) {
779 /* Never set this to be visible in other modes. Note, however,
780 that we do need to undo visibility (LEAVE_NOTIFY case above) no
781 matter what the mode is. */
782 item->set_fill (true);
791 /** @return the samples per pixel */
793 TimeAxisViewItem::get_samples_per_pixel () const
795 return samples_per_pixel;
798 /** Set the samples per pixel of this item.
799 * This item is used to determine the relative visual size and position of this item
800 * based upon its duration and start value.
802 * @param fpp the new samples per pixel
805 TimeAxisViewItem::set_samples_per_pixel (double fpp)
807 samples_per_pixel = fpp;
808 set_position (this->get_position(), this);
810 double end_pixel = trackview.editor().sample_to_pixel (sample_position + get_duration());
811 double first_pixel = trackview.editor().sample_to_pixel (sample_position);
813 reset_width_dependent_items (end_pixel - first_pixel);
817 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
819 _width = pixel_width;
821 manage_name_highlight ();
824 if (pixel_width < 2.0) {
827 frame->set_outline (false);
828 frame->set_x1 (std::max(1.0, pixel_width));
831 if (frame_handle_start) {
832 frame_handle_start->hide();
833 frame_handle_end->hide();
838 frame->set_outline (true);
839 /* Note: x0 is always zero - the position is defined by
840 * the position of the group, not the frame.
842 frame->set_x1 (pixel_width);
844 if (selection_frame) {
845 selection_frame->set (frame->get().shrink (1.0));
849 if (frame_handle_start) {
850 if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
852 * there's less than GRAB_HANDLE_WIDTH of the region between
853 * the right-hand end of frame_handle_start and the left-hand
854 * end of frame_handle_end, so disable the handles
857 frame_handle_start->hide();
858 frame_handle_end->hide();
860 frame_handle_start->show();
861 frame_handle_end->set_x0 (pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
862 frame_handle_end->set_x1 (pixel_width);
863 frame_handle_end->show();
865 frame_handle_start->raise_to_top ();
866 frame_handle_end->raise_to_top ();
873 TimeAxisViewItem::manage_name_text ()
875 int visible_name_width;
881 if (!(visibility & ShowNameText) || (!wide_enough_for_name || !high_enough_for_name)) {
886 if (name_text->text().empty()) {
890 visible_name_width = name_text_width;
892 if (visible_name_width > _width - NAME_X_OFFSET) {
893 visible_name_width = _width - NAME_X_OFFSET;
896 if (visible_name_width < 1) {
899 name_text->clamp_width (visible_name_width);
905 * Callback used to remove this time axis item during the gtk idle loop.
906 * This is used to avoid deleting the obejct while inside the remove_this_item
909 * @param item the TimeAxisViewItem to remove.
910 * @param src the identity of the object that initiated the change.
913 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
915 item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
922 TimeAxisViewItem::set_y (double y)
924 group->set_y_position (y);
928 TimeAxisViewItem::parameter_changed (string p)
930 if (p == "color-regions-using-track-color") {
932 } else if (p == "timeline-item-gradient-depth") {
933 set_frame_gradient ();
938 TimeAxisViewItem::drag_start ()
945 TimeAxisViewItem::drag_end ()