2 Copyright (C) 2003 Paul Davis
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.
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.
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.
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
25 #include "ardour/types.h"
26 #include "ardour/ardour.h"
28 #include "gtkmm2ext/utils.h"
29 #include "gtkmm2ext/gui_thread.h"
31 #include "canvas/container.h"
32 #include "canvas/rectangle.h"
33 #include "canvas/debug.h"
34 #include "canvas/text.h"
35 #include "canvas/utils.h"
37 #include "ardour/profile.h"
39 #include "ardour_ui.h"
41 * ardour_ui.h was moved up in the include list
42 * due to a conflicting definition of 'Rect' between
43 * Apple's MacTypes.h file and GTK
46 #include "public_editor.h"
47 #include "time_axis_view_item.h"
48 #include "time_axis_view.h"
50 #include "rgb_macros.h"
55 using namespace Editing;
58 using namespace ARDOUR;
59 using namespace Gtkmm2ext;
61 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
62 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
63 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 0.0;
64 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 10.0;
65 const double TimeAxisViewItem::RIGHT_EDGE_SHIFT = 1.0;
67 int TimeAxisViewItem::NAME_HEIGHT;
68 double TimeAxisViewItem::NAME_Y_OFFSET;
69 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
70 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
73 TimeAxisViewItem::set_constant_heights ()
75 NAME_FONT = Pango::FontDescription (ARDOUR_UI::config()->get_canvasvar_SmallFont());
81 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
85 layout->set_font_description (NAME_FONT);
86 get_pixel_size (layout, width, height);
88 layout = foo.create_pango_layout (X_("H")); /* just the ascender */
92 /* Config->get_show_name_highlight) == true:
93 Y_OFFSET is measured from bottom of the time axis view item.
94 Config->get_show_name_highlight) == false:
95 Y_OFFSET is measured from the top of the time axis view item.
98 if (Config->get_show_name_highlight()) {
99 NAME_Y_OFFSET = height + 1;
100 NAME_HIGHLIGHT_SIZE = height + 2;
103 NAME_HIGHLIGHT_SIZE = 0;
105 NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 3;
109 * Construct a new TimeAxisViewItem.
111 * @param it_name the unique name of this item
112 * @param parent the parent canvas group
113 * @param tv the TimeAxisView we are going to be added to
114 * @param spu samples per unit
116 * @param start the start point of this item
117 * @param duration the duration of this item
118 * @param recording true if this is a recording region view
119 * @param automation true if this is an automation region view
121 TimeAxisViewItem::TimeAxisViewItem(
122 const string & it_name, ArdourCanvas::Item& parent, TimeAxisView& tv, double spu, uint32_t base_color,
123 framepos_t start, framecnt_t duration, bool recording, bool automation, Visibility vis
126 , frame_position (-1)
127 , item_name (it_name)
129 , _recregion (recording)
130 , _automation (automation)
134 init (&parent, spu, base_color, start, duration, vis, true, true);
137 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
140 , PBD::ScopedConnectionList()
141 , trackview (other.trackview)
142 , frame_position (-1)
143 , item_name (other.item_name)
145 , _recregion (other._recregion)
146 , _automation (other._automation)
147 , _dragging (other._dragging)
150 /* share the other's parent, but still create a new group */
152 ArdourCanvas::Item* parent = other.group->parent();
154 _selected = other._selected;
156 init (parent, other.samples_per_pixel, other.fill_color, other.frame_position,
157 other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
161 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color,
162 framepos_t start, framepos_t duration, Visibility vis,
163 bool wide, bool high)
165 group = new ArdourCanvas::Container (parent);
166 CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
167 group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
169 fill_color = base_color;
170 samples_per_pixel = fpp;
171 frame_position = start;
172 item_duration = duration;
173 name_connected = false;
174 position_locked = false;
175 max_item_duration = ARDOUR::max_framepos;
176 min_item_duration = 0;
177 show_vestigial = true;
182 wide_enough_for_name = wide;
183 high_enough_for_name = high;
187 warning << "Time Axis Item Duration == 0" << endl;
190 vestigial_frame = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, 1.0, 2.0, trackview.current_height()));
191 CANVAS_DEBUG_NAME (vestigial_frame, string_compose ("vestigial frame for %1", get_item_name()));
192 vestigial_frame->hide ();
193 vestigial_frame->set_outline_color (ARDOUR_UI::config()->get_canvasvar_VestigialFrame());
194 vestigial_frame->set_fill_color (ARDOUR_UI::config()->get_canvasvar_VestigialFrame());
196 if (visibility & ShowFrame) {
197 frame = new ArdourCanvas::Rectangle (group,
198 ArdourCanvas::Rect (0.0, 0.0,
199 trackview.editor().sample_to_pixel(duration) + RIGHT_EDGE_SHIFT,
200 trackview.current_height() - 1.0));
202 CANVAS_DEBUG_NAME (frame, string_compose ("frame for %1", get_item_name()));
204 if (Config->get_show_name_highlight()) {
205 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
207 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM));
211 frame->set_outline_color (ARDOUR_UI::config()->get_canvasvar_RecordingRect());
213 frame->set_outline_color (ARDOUR_UI::config()->get_canvasvar_TimeAxisFrame());
221 if (Config->get_show_name_highlight() && (visibility & ShowNameHighlight)) {
226 if (visibility & FullWidthNameHighlight) {
228 width = trackview.editor().sample_to_pixel(item_duration) + RIGHT_EDGE_SHIFT;
231 width = trackview.editor().sample_to_pixel(item_duration) - 2.0 + RIGHT_EDGE_SHIFT;
234 name_highlight = new ArdourCanvas::Rectangle (group,
235 ArdourCanvas::Rect (start,
236 trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE,
237 width - 2.0 + RIGHT_EDGE_SHIFT,
238 trackview.current_height() - 1.0));
239 CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
240 name_highlight->set_data ("timeaxisviewitem", this);
241 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
242 name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255));
248 if (visibility & ShowNameText) {
249 name_text = new ArdourCanvas::Text (group);
250 CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
251 if (Config->get_show_name_highlight()) {
252 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
254 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
256 name_text->set_font_description (NAME_FONT);
261 /* create our grab handles used for trimming/duration etc */
262 if (!_recregion && !_automation) {
263 double top = TimeAxisViewItem::GRAB_HANDLE_TOP;
264 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
266 frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
267 CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
268 frame_handle_start->set_outline (false);
269 frame_handle_start->set_fill (false);
270 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
272 frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
273 CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
274 frame_handle_end->set_outline (false);
275 frame_handle_end->set_fill (false);
276 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
278 frame_handle_start = frame_handle_end = 0;
281 set_color (base_color);
283 set_duration (item_duration, this);
284 set_position (start, this);
286 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
287 ARDOUR_UI::config()->ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
290 TimeAxisViewItem::~TimeAxisViewItem()
296 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
302 TimeAxisViewItem::hide_rect ()
304 rect_visible = false;
307 if (name_highlight) {
308 name_highlight->set_outline_what (ArdourCanvas::Rectangle::What (0));
309 name_highlight->set_fill_color (UINT_RGBA_CHANGE_A (fill_color, 64));
314 TimeAxisViewItem::show_rect ()
319 if (name_highlight) {
320 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
321 name_highlight->set_fill_color (fill_color);
326 * Set the position of this item on the timeline.
328 * @param pos the new position
329 * @param src the identity of the object that initiated the change
330 * @return true on success
334 TimeAxisViewItem::set_position(framepos_t pos, void* src, double* delta)
336 if (position_locked) {
340 frame_position = pos;
342 double new_unit_pos = trackview.editor().sample_to_pixel (pos);
345 (*delta) = new_unit_pos - group->position().x;
350 if (new_unit_pos == group->position().x) {
355 group->set_x_position (new_unit_pos);
357 PositionChanged (frame_position, src); /* EMIT_SIGNAL */
362 /** @return position of this item on the timeline */
364 TimeAxisViewItem::get_position() const
366 return frame_position;
370 * Set the duration of this item.
372 * @param dur the new duration of this item
373 * @param src the identity of the object that initiated the change
374 * @return true on success
378 TimeAxisViewItem::set_duration (framecnt_t dur, void* src)
380 if ((dur > max_item_duration) || (dur < min_item_duration)) {
381 warning << string_compose (
382 P_("new duration %1 frame is out of bounds for %2", "new duration of %1 frames is out of bounds for %2", dur),
383 get_item_name(), dur)
394 reset_width_dependent_items (trackview.editor().sample_to_pixel (dur));
396 DurationChanged (dur, src); /* EMIT_SIGNAL */
400 /** @return duration of this item */
402 TimeAxisViewItem::get_duration() const
404 return item_duration;
408 * Set the maximum duration that this item can have.
410 * @param dur the new maximum duration
411 * @param src the identity of the object that initiated the change
414 TimeAxisViewItem::set_max_duration(framecnt_t dur, void* src)
416 max_item_duration = dur;
417 MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
420 /** @return the maximum duration that this item may have */
422 TimeAxisViewItem::get_max_duration() const
424 return max_item_duration;
428 * Set the minimum duration that this item may have.
430 * @param the minimum duration that this item may be set to
431 * @param src the identity of the object that initiated the change
434 TimeAxisViewItem::set_min_duration(framecnt_t dur, void* src)
436 min_item_duration = dur;
437 MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
440 /** @return the minimum duration that this item mey have */
442 TimeAxisViewItem::get_min_duration() const
444 return min_item_duration;
448 * Set whether this item is locked to its current position.
449 * Locked items cannot be moved until the item is unlocked again.
451 * @param yn true to lock this item to its current position
452 * @param src the identity of the object that initiated the change
455 TimeAxisViewItem::set_position_locked(bool yn, void* src)
457 position_locked = yn;
458 set_trim_handle_colors();
459 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
462 /** @return true if this item is locked to its current position */
464 TimeAxisViewItem::get_position_locked() const
466 return position_locked;
470 * Set whether the maximum duration constraint is active.
472 * @param active set true to enforce the max duration constraint
473 * @param src the identity of the object that initiated the change
476 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
478 max_duration_active = active;
481 /** @return true if the maximum duration constraint is active */
483 TimeAxisViewItem::get_max_duration_active() const
485 return max_duration_active;
489 * Set whether the minimum duration constraint is active.
491 * @param active set true to enforce the min duration constraint
492 * @param src the identity of the object that initiated the change
496 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
498 min_duration_active = active;
501 /** @return true if the maximum duration constraint is active */
503 TimeAxisViewItem::get_min_duration_active() const
505 return min_duration_active;
509 * Set the name of this item.
511 * @param new_name the new name of this item
512 * @param src the identity of the object that initiated the change
516 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
518 if (new_name != item_name) {
519 std::string temp_name = item_name;
520 item_name = new_name;
521 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
525 /** @return the name of this item */
527 TimeAxisViewItem::get_item_name() const
533 * Set selection status.
535 * @param yn true if this item is currently selected
538 TimeAxisViewItem::set_selected(bool yn)
540 if (_selected != yn) {
541 Selectable::set_selected (yn);
543 set_name_text_color ();
547 /** @return the TimeAxisView that this item is on */
549 TimeAxisViewItem::get_time_axis_view () const
555 * Set the displayed item text.
556 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
558 * @param new_name the new name text to display
562 TimeAxisViewItem::set_name_text(const string& new_name)
568 name_text_width = pixel_width (new_name, NAME_FONT) + 2;
569 name_text->set (new_name);
574 * Set the height of this item.
576 * @param h new height
579 TimeAxisViewItem::set_height (double height)
583 manage_name_highlight ();
585 if (visibility & ShowNameText) {
586 if (Config->get_show_name_highlight()) {
587 name_text->set_y_position (height - NAME_Y_OFFSET);
589 name_text->set_y_position (NAME_Y_OFFSET);
594 frame->set_y1 (height);
595 if (frame_handle_start) {
596 frame_handle_start->set_y1 (height);
597 frame_handle_end->set_y1 (height);
601 vestigial_frame->set_y1 (height - 1.0);
607 TimeAxisViewItem::manage_name_highlight ()
609 if (!name_highlight) {
613 if (_height < NAME_HIGHLIGHT_THRESH) {
614 high_enough_for_name = false;
616 high_enough_for_name = true;
620 wide_enough_for_name = false;
622 wide_enough_for_name = true;
625 if (name_highlight && wide_enough_for_name && high_enough_for_name) {
627 name_highlight->show();
628 name_highlight->set (ArdourCanvas::Rect (0.0, (double) _height - NAME_HIGHLIGHT_SIZE, _width+RIGHT_EDGE_SHIFT, (double) _height - 1.0));
631 name_highlight->hide();
638 TimeAxisViewItem::set_color (uint32_t base_color)
640 fill_color = base_color;
645 TimeAxisViewItem::get_canvas_frame()
651 TimeAxisViewItem::get_canvas_group()
657 TimeAxisViewItem::get_name_highlight()
659 return name_highlight;
663 * Convenience method to set the various canvas item colors
666 TimeAxisViewItem::set_colors()
670 if (name_highlight) {
671 name_highlight->set_fill_color (fill_color);
674 set_name_text_color ();
675 set_trim_handle_colors();
679 TimeAxisViewItem::set_name_text_color ()
688 if (Config->get_show_name_highlight()) {
689 /* name text will always be on top of name highlight, which
690 will always use our fill color.
694 /* name text will be on top of the item, whose color
695 may vary depending on various conditions.
697 f = get_fill_color ();
700 name_text->set_color (contrasting_text_color (f));
704 TimeAxisViewItem::fill_opacity () const
707 /* if the frame/rect is marked as invisible, then the
708 * fill should be transparent. simplest: set
710 * alpha/opacity to zero.
719 uint32_t col = ARDOUR_UI::config()->get_canvasvar_FrameBase();
720 return UINT_RGBA_A (col);
724 TimeAxisViewItem::get_fill_color () const
733 f = ARDOUR_UI::config()->get_canvasvar_SelectedFrameBase();
736 /* some condition of this item has set fill opacity to
737 * zero, but it has been selected, so use a mid-way
738 * alpha value to make it reasonably visible.
746 f = ARDOUR_UI::config()->get_canvasvar_RecordingRect();
748 if ((!Config->get_show_name_highlight() || high_enough_for_name) && !ARDOUR_UI::config()->get_color_regions_using_track_color()) {
749 f = ARDOUR_UI::config()->get_canvasvar_FrameBase();
756 return UINT_RGBA_CHANGE_A (f, o);
760 * Sets the frame color depending on whether this item is selected
763 TimeAxisViewItem::set_frame_color()
769 frame->set_fill_color (get_fill_color());
770 set_frame_gradient ();
776 f = ARDOUR_UI::config()->get_canvasvar_SelectedTimeAxisFrame();
778 f = ARDOUR_UI::config()->get_canvasvar_TimeAxisFrame();
782 /* make the frame outline be visible but rather transparent */
783 f = UINT_RGBA_CHANGE_A (f, 64);
786 frame->set_outline_color (f);
791 TimeAxisViewItem::set_frame_gradient ()
793 if (ARDOUR_UI::config()->get_timeline_item_gradient_depth() == 0.0) {
794 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
798 ArdourCanvas::Fill::StopList stops;
801 ArdourCanvas::Color f (get_fill_color());
803 /* need to get alpha value */
804 ArdourCanvas::color_to_rgba (f, r, g, b, a);
806 stops.push_back (std::make_pair (0.0, f));
808 /* now a darker version */
810 ArdourCanvas::color_to_hsv (f, h, s, v);
812 v = min (1.0, v * (1.0 - ARDOUR_UI::config()->get_timeline_item_gradient_depth()));
814 ArdourCanvas::Color darker = ArdourCanvas::hsv_to_color (h, s, v, a);
815 stops.push_back (std::make_pair (1.0, darker));
817 frame->set_gradient (stops, true);
821 * Set the colors of the start and end trim handle depending on object state
824 TimeAxisViewItem::set_trim_handle_colors()
827 /* Leave them transparent for now */
828 if (frame_handle_start) {
829 frame_handle_start->set_fill_color (0x00000000);
830 frame_handle_end->set_fill_color (0x00000000);
833 if (frame_handle_start) {
834 if (position_locked) {
835 frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TrimHandleLocked());
836 frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TrimHandleLocked());
838 frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TrimHandle());
839 frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_canvasvar_TrimHandle());
846 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
849 case GDK_LEAVE_NOTIFY:
850 /* always hide the handle whenever we leave, no matter what mode */
851 item->set_fill (false);
853 case GDK_ENTER_NOTIFY:
854 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject &&
855 !trackview.editor().internal_editing()) {
856 /* never set this to be visible in internal
857 edit mode. Note, however, that we do need to
858 undo visibility (LEAVE_NOTIFY case above) no
859 matter what the mode is.
861 item->set_fill (true);
870 /** @return the frames per pixel */
872 TimeAxisViewItem::get_samples_per_pixel () const
874 return samples_per_pixel;
877 /** Set the frames per pixel of this item.
878 * This item is used to determine the relative visual size and position of this item
879 * based upon its duration and start value.
881 * @param fpp the new frames per pixel
884 TimeAxisViewItem::set_samples_per_pixel (double fpp)
886 samples_per_pixel = fpp;
887 set_position (this->get_position(), this);
888 reset_width_dependent_items ((double) get_duration() / samples_per_pixel);
892 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
894 _width = pixel_width;
896 manage_name_highlight ();
899 if (pixel_width < 2.0) {
901 if (show_vestigial) {
902 vestigial_frame->show();
909 if (frame_handle_start) {
910 frame_handle_start->hide();
911 frame_handle_end->hide();
915 vestigial_frame->hide();
919 frame->set_x1 (pixel_width + RIGHT_EDGE_SHIFT);
922 if (frame_handle_start) {
923 if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
925 * there's less than GRAB_HANDLE_WIDTH of the region between
926 * the right-hand end of frame_handle_start and the left-hand
927 * end of frame_handle_end, so disable the handles
930 frame_handle_start->hide();
931 frame_handle_end->hide();
933 frame_handle_start->show();
934 frame_handle_end->set_x0 (pixel_width + RIGHT_EDGE_SHIFT - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
935 frame_handle_end->set_x1 (pixel_width + RIGHT_EDGE_SHIFT);
936 frame_handle_end->show();
943 TimeAxisViewItem::manage_name_text ()
945 int visible_name_width;
951 if (!wide_enough_for_name || !high_enough_for_name) {
956 if (name_text->text().empty()) {
960 visible_name_width = name_text_width;
962 if (visible_name_width > _width - NAME_X_OFFSET) {
963 visible_name_width = _width - NAME_X_OFFSET;
966 if (visible_name_width < 1) {
969 name_text->clamp_width (visible_name_width);
975 * Callback used to remove this time axis item during the gtk idle loop.
976 * This is used to avoid deleting the obejct while inside the remove_this_item
979 * @param item the TimeAxisViewItem to remove.
980 * @param src the identity of the object that initiated the change.
983 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
985 item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
992 TimeAxisViewItem::set_y (double y)
994 group->set_y_position (y);
998 TimeAxisViewItem::parameter_changed (string p)
1000 if (p == "color-regions-using-track-color") {
1002 } else if (p == "timeline-item-gradient-depth") {
1003 set_frame_gradient ();
1008 TimeAxisViewItem::drag_start ()
1015 TimeAxisViewItem::drag_end ()