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/colors.h"
37 #include "ardour/profile.h"
39 #include "public_editor.h"
40 #include "time_axis_view_item.h"
41 #include "time_axis_view.h"
42 #include "ui_config.h"
44 #include "rgb_macros.h"
49 using namespace Editing;
52 using namespace ARDOUR;
53 using namespace ARDOUR_UI_UTILS;
54 using namespace Gtkmm2ext;
56 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
57 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
58 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 0.0;
59 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 10.0;
61 int TimeAxisViewItem::NAME_HEIGHT;
62 double TimeAxisViewItem::NAME_Y_OFFSET;
63 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
64 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
67 TimeAxisViewItem::set_constant_heights ()
69 NAME_FONT = Pango::FontDescription (UIConfiguration::instance().get_SmallFont());
75 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
79 layout->set_font_description (NAME_FONT);
80 get_pixel_size (layout, width, height);
82 layout = foo.create_pango_layout (X_("H")); /* just the ascender */
86 /* Config->get_show_name_highlight) == true:
87 Y_OFFSET is measured from bottom of the time axis view item.
88 Config->get_show_name_highlight) == false:
89 Y_OFFSET is measured from the top of the time axis view item.
92 if (UIConfiguration::instance().get_show_name_highlight()) {
93 NAME_Y_OFFSET = height + 1;
94 NAME_HIGHLIGHT_SIZE = height + 2;
97 NAME_HIGHLIGHT_SIZE = 0;
99 NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 3;
103 * Construct a new TimeAxisViewItem.
105 * @param it_name the unique name of this item
106 * @param parent the parent canvas group
107 * @param tv the TimeAxisView we are going to be added to
108 * @param spu samples per unit
110 * @param start the start point of this item
111 * @param duration the duration of this item
112 * @param recording true if this is a recording region view
113 * @param automation true if this is an automation region view
115 TimeAxisViewItem::TimeAxisViewItem(
116 const string & it_name, ArdourCanvas::Item& parent, TimeAxisView& tv, double spu, uint32_t base_color,
117 framepos_t start, framecnt_t duration, bool recording, bool automation, Visibility vis
120 , frame_position (-1)
121 , item_name (it_name)
122 , selection_frame (0)
124 , _recregion (recording)
125 , _automation (automation)
129 init (&parent, spu, base_color, start, duration, vis, true, true);
132 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
135 , PBD::ScopedConnectionList()
136 , trackview (other.trackview)
137 , frame_position (-1)
138 , item_name (other.item_name)
139 , selection_frame (0)
141 , _recregion (other._recregion)
142 , _automation (other._automation)
143 , _dragging (other._dragging)
146 /* share the other's parent, but still create a new group */
148 ArdourCanvas::Item* parent = other.group->parent();
150 _selected = other._selected;
152 init (parent, other.samples_per_pixel, other.fill_color, other.frame_position,
153 other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
157 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color,
158 framepos_t start, framepos_t duration, Visibility vis,
159 bool wide, bool high)
161 group = new ArdourCanvas::Container (parent);
162 CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
163 group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
165 fill_color = base_color;
166 fill_color_name = "time axis view item base";
167 samples_per_pixel = fpp;
168 frame_position = start;
169 item_duration = duration;
170 name_connected = false;
171 position_locked = false;
172 max_item_duration = ARDOUR::max_framepos;
173 min_item_duration = 0;
178 wide_enough_for_name = wide;
179 high_enough_for_name = high;
182 warning << "Time Axis Item Duration == 0" << endl;
185 if (visibility & ShowFrame) {
186 frame = new ArdourCanvas::Rectangle (group,
187 ArdourCanvas::Rect (0.0, 0.0,
188 trackview.editor().sample_to_pixel(duration),
189 trackview.current_height()));
191 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
194 CANVAS_DEBUG_NAME (frame, string_compose ("frame for %1", get_item_name()));
197 frame->set_outline_color (UIConfiguration::instance().color ("recording rect"));
199 frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
203 if (UIConfiguration::instance().get_show_name_highlight() && (visibility & ShowNameHighlight)) {
205 /* rectangle size will be set in ::manage_name_highlight() */
206 name_highlight = new ArdourCanvas::Rectangle (group);
207 CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
208 name_highlight->set_data ("timeaxisviewitem", this);
209 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
210 name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255)); // this should use a theme color
216 if (visibility & ShowNameText) {
217 name_text = new ArdourCanvas::Text (group);
218 CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
219 if (UIConfiguration::instance().get_show_name_highlight()) {
220 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
222 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
224 name_text->set_font_description (NAME_FONT);
225 name_text->set_ignore_events (true);
230 /* create our grab handles used for trimming/duration etc */
231 if (!_recregion && !_automation) {
232 double top = TimeAxisViewItem::GRAB_HANDLE_TOP;
233 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
235 frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
236 CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
237 frame_handle_start->set_outline (false);
238 frame_handle_start->set_fill (false);
239 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
241 frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
242 CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
243 frame_handle_end->set_outline (false);
244 frame_handle_end->set_fill (false);
245 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
247 frame_handle_start = frame_handle_end = 0;
250 set_color (base_color);
252 set_duration (item_duration, this);
253 set_position (start, this);
255 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
256 UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
259 TimeAxisViewItem::~TimeAxisViewItem()
265 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
271 * Set the position of this item on the timeline.
273 * @param pos the new position
274 * @param src the identity of the object that initiated the change
275 * @return true on success
279 TimeAxisViewItem::set_position(framepos_t pos, void* src, double* delta)
281 if (position_locked) {
285 frame_position = pos;
287 double new_unit_pos = trackview.editor().sample_to_pixel (pos);
290 (*delta) = new_unit_pos - group->position().x;
295 if (new_unit_pos == group->position().x) {
300 group->set_x_position (new_unit_pos);
301 PositionChanged (frame_position, src); /* EMIT_SIGNAL */
306 /** @return position of this item on the timeline */
308 TimeAxisViewItem::get_position() const
310 return frame_position;
314 * Set the duration of this item.
316 * @param dur the new duration of this item
317 * @param src the identity of the object that initiated the change
318 * @return true on success
322 TimeAxisViewItem::set_duration (framecnt_t dur, void* src)
324 if ((dur > max_item_duration) || (dur < min_item_duration)) {
325 warning << string_compose (
326 P_("new duration %1 frame is out of bounds for %2", "new duration of %1 frames is out of bounds for %2", dur),
327 get_item_name(), dur)
338 double end_pixel = trackview.editor().sample_to_pixel (frame_position + dur);
339 double first_pixel = trackview.editor().sample_to_pixel (frame_position);
341 reset_width_dependent_items (end_pixel - first_pixel);
343 DurationChanged (dur, src); /* EMIT_SIGNAL */
347 /** @return duration of this item */
349 TimeAxisViewItem::get_duration() const
351 return item_duration;
355 * Set the maximum duration that this item can have.
357 * @param dur the new maximum duration
358 * @param src the identity of the object that initiated the change
361 TimeAxisViewItem::set_max_duration(framecnt_t dur, void* src)
363 max_item_duration = dur;
364 MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
367 /** @return the maximum duration that this item may have */
369 TimeAxisViewItem::get_max_duration() const
371 return max_item_duration;
375 * Set the minimum duration that this item may have.
377 * @param the minimum duration that this item may be set to
378 * @param src the identity of the object that initiated the change
381 TimeAxisViewItem::set_min_duration(framecnt_t dur, void* src)
383 min_item_duration = dur;
384 MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
387 /** @return the minimum duration that this item mey have */
389 TimeAxisViewItem::get_min_duration() const
391 return min_item_duration;
395 * Set whether this item is locked to its current position.
396 * Locked items cannot be moved until the item is unlocked again.
398 * @param yn true to lock this item to its current position
399 * @param src the identity of the object that initiated the change
402 TimeAxisViewItem::set_position_locked(bool yn, void* src)
404 position_locked = yn;
405 set_trim_handle_colors();
406 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
409 /** @return true if this item is locked to its current position */
411 TimeAxisViewItem::get_position_locked() const
413 return position_locked;
417 * Set whether the maximum duration constraint is active.
419 * @param active set true to enforce the max duration constraint
420 * @param src the identity of the object that initiated the change
423 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
425 max_duration_active = active;
428 /** @return true if the maximum duration constraint is active */
430 TimeAxisViewItem::get_max_duration_active() const
432 return max_duration_active;
436 * Set whether the minimum duration constraint is active.
438 * @param active set true to enforce the min duration constraint
439 * @param src the identity of the object that initiated the change
443 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
445 min_duration_active = active;
448 /** @return true if the maximum duration constraint is active */
450 TimeAxisViewItem::get_min_duration_active() const
452 return min_duration_active;
456 * Set the name of this item.
458 * @param new_name the new name of this item
459 * @param src the identity of the object that initiated the change
463 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
465 if (new_name != item_name) {
466 std::string temp_name = item_name;
467 item_name = new_name;
468 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
472 /** @return the name of this item */
474 TimeAxisViewItem::get_item_name() const
480 * Set selection status.
482 * @param yn true if this item is currently selected
485 TimeAxisViewItem::set_selected(bool yn)
487 if (_selected == yn) {
491 Selectable::set_selected (yn);
493 set_name_text_color ();
495 if (_selected && frame) {
496 if (!selection_frame) {
497 selection_frame = new ArdourCanvas::Rectangle (group);
498 selection_frame->set_fill (false);
499 selection_frame->set_outline_color (UIConfiguration::instance().color ("selected time axis frame"));
500 selection_frame->set_ignore_events (true);
502 selection_frame->set (frame->get().shrink (1.0));
503 selection_frame->show ();
505 if (selection_frame) {
506 selection_frame->hide ();
511 /** @return the TimeAxisView that this item is on */
513 TimeAxisViewItem::get_time_axis_view () const
519 * Set the displayed item text.
520 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
522 * @param new_name the new name text to display
526 TimeAxisViewItem::set_name_text(const string& new_name)
532 name_text_width = pixel_width (new_name, NAME_FONT) + 2;
533 name_text->set (new_name);
539 * Set the height of this item.
541 * @param h new height
544 TimeAxisViewItem::set_height (double height)
548 manage_name_highlight ();
550 if (visibility & ShowNameText) {
551 if (UIConfiguration::instance().get_show_name_highlight()) {
552 name_text->set_y_position (height - NAME_Y_OFFSET);
554 name_text->set_y_position (NAME_Y_OFFSET);
561 frame->set_y1 (height);
563 if (frame_handle_start) {
564 frame_handle_start->set_y1 (height);
565 frame_handle_end->set_y1 (height);
568 if (selection_frame) {
569 selection_frame->set (frame->get().shrink (1.0));
577 TimeAxisViewItem::manage_name_highlight ()
579 if (!name_highlight) {
583 if (_height < NAME_HIGHLIGHT_THRESH) {
584 high_enough_for_name = false;
586 high_enough_for_name = true;
590 wide_enough_for_name = false;
592 wide_enough_for_name = true;
595 if (name_highlight && wide_enough_for_name && high_enough_for_name) {
597 name_highlight->show();
598 // name_highlight->set_x_position (1.0);
599 name_highlight->set (ArdourCanvas::Rect (0.0, (double) _height - NAME_HIGHLIGHT_SIZE, _width - 2.0, _height));
602 name_highlight->hide();
609 TimeAxisViewItem::set_color (uint32_t base_color)
611 fill_color = base_color;
616 TimeAxisViewItem::get_canvas_frame()
622 TimeAxisViewItem::get_canvas_group()
628 TimeAxisViewItem::get_name_highlight()
630 return name_highlight;
634 * Convenience method to set the various canvas item colors
637 TimeAxisViewItem::set_colors()
641 if (name_highlight) {
642 name_highlight->set_fill_color (fill_color);
645 set_name_text_color ();
646 set_trim_handle_colors();
650 TimeAxisViewItem::set_name_text_color ()
659 if (UIConfiguration::instance().get_show_name_highlight()) {
660 /* name text will always be on top of name highlight, which
661 will always use our fill color.
665 /* name text will be on top of the item, whose color
666 may vary depending on various conditions.
668 f = get_fill_color ();
671 name_text->set_color (ArdourCanvas::contrasting_text_color (f));
675 TimeAxisViewItem::get_fill_color () const
677 const std::string mod_name = (_dragging ? "dragging region" : fill_color_name);
680 return UIConfiguration::instance().color_mod ("selected region base", mod_name);
681 } else if (_recregion) {
682 return UIConfiguration::instance().color ("recording rect");
683 } else if ((!UIConfiguration::instance().get_show_name_highlight() || high_enough_for_name) &&
684 !UIConfiguration::instance().get_color_regions_using_track_color()) {
685 return UIConfiguration::instance().color_mod (fill_color_name, mod_name);
687 return UIConfiguration::instance().color_mod (fill_color, mod_name);
691 * Sets the frame color depending on whether this item is selected
694 TimeAxisViewItem::set_frame_color()
700 frame->set_fill_color (get_fill_color());
701 set_frame_gradient ();
704 frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
709 TimeAxisViewItem::set_frame_gradient ()
711 if (UIConfiguration::instance().get_timeline_item_gradient_depth() == 0.0) {
712 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
716 ArdourCanvas::Fill::StopList stops;
719 ArdourCanvas::Color f (get_fill_color());
721 /* need to get alpha value */
722 ArdourCanvas::color_to_rgba (f, r, g, b, a);
724 stops.push_back (std::make_pair (0.0, f));
726 /* now a darker version */
728 ArdourCanvas::color_to_hsv (f, h, s, v);
730 v = min (1.0, v * (1.0 - UIConfiguration::instance().get_timeline_item_gradient_depth()));
732 ArdourCanvas::Color darker = ArdourCanvas::hsva_to_color (h, s, v, a);
733 stops.push_back (std::make_pair (1.0, darker));
735 frame->set_gradient (stops, true);
739 * Set the colors of the start and end trim handle depending on object state
742 TimeAxisViewItem::set_trim_handle_colors()
745 /* Leave them transparent for now */
746 if (frame_handle_start) {
747 frame_handle_start->set_fill_color (0x00000000);
748 frame_handle_end->set_fill_color (0x00000000);
751 if (frame_handle_start) {
752 if (position_locked) {
753 frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
754 frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
756 frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandle());
757 frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandle());
764 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
767 case GDK_LEAVE_NOTIFY:
768 /* always hide the handle whenever we leave, no matter what mode */
769 item->set_fill (false);
771 case GDK_ENTER_NOTIFY:
772 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject) {
773 /* Never set this to be visible in other modes. Note, however,
774 that we do need to undo visibility (LEAVE_NOTIFY case above) no
775 matter what the mode is. */
776 item->set_fill (true);
785 /** @return the frames per pixel */
787 TimeAxisViewItem::get_samples_per_pixel () const
789 return samples_per_pixel;
792 /** Set the frames per pixel of this item.
793 * This item is used to determine the relative visual size and position of this item
794 * based upon its duration and start value.
796 * @param fpp the new frames per pixel
799 TimeAxisViewItem::set_samples_per_pixel (double fpp)
801 samples_per_pixel = fpp;
802 set_position (this->get_position(), this);
804 double end_pixel = trackview.editor().sample_to_pixel (frame_position + get_duration());
805 double first_pixel = trackview.editor().sample_to_pixel (frame_position);
807 reset_width_dependent_items (end_pixel - first_pixel);
811 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
813 _width = pixel_width;
815 manage_name_highlight ();
818 if (pixel_width < 2.0) {
821 frame->set_outline (false);
822 frame->set_x1 (std::max(1.0, pixel_width));
825 if (frame_handle_start) {
826 frame_handle_start->hide();
827 frame_handle_end->hide();
832 frame->set_outline (true);
833 /* Note: x0 is always zero - the position is defined by
834 * the position of the group, not the frame.
836 frame->set_x1 (pixel_width);
838 if (selection_frame) {
839 selection_frame->set (frame->get().shrink (1.0));
843 if (frame_handle_start) {
844 if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
846 * there's less than GRAB_HANDLE_WIDTH of the region between
847 * the right-hand end of frame_handle_start and the left-hand
848 * end of frame_handle_end, so disable the handles
851 frame_handle_start->hide();
852 frame_handle_end->hide();
854 frame_handle_start->show();
855 frame_handle_end->set_x0 (pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
856 frame_handle_end->set_x1 (pixel_width);
857 frame_handle_end->show();
864 TimeAxisViewItem::manage_name_text ()
866 int visible_name_width;
872 if (!wide_enough_for_name || !high_enough_for_name) {
877 if (name_text->text().empty()) {
881 visible_name_width = name_text_width;
883 if (visible_name_width > _width - NAME_X_OFFSET) {
884 visible_name_width = _width - NAME_X_OFFSET;
887 if (visible_name_width < 1) {
890 name_text->clamp_width (visible_name_width);
896 * Callback used to remove this time axis item during the gtk idle loop.
897 * This is used to avoid deleting the obejct while inside the remove_this_item
900 * @param item the TimeAxisViewItem to remove.
901 * @param src the identity of the object that initiated the change.
904 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
906 item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
913 TimeAxisViewItem::set_y (double y)
915 group->set_y_position (y);
919 TimeAxisViewItem::parameter_changed (string p)
921 if (p == "color-regions-using-track-color") {
923 } else if (p == "timeline-item-gradient-depth") {
924 set_frame_gradient ();
929 TimeAxisViewItem::drag_start ()
936 TimeAxisViewItem::drag_end ()