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 "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 ARDOUR_UI_UTILS;
60 using namespace Gtkmm2ext;
62 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
63 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
64 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 0.0;
65 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 10.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_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 (ARDOUR_UI::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)
128 , selection_frame (0)
130 , _recregion (recording)
131 , _automation (automation)
135 init (&parent, spu, base_color, start, duration, vis, true, true);
138 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
141 , PBD::ScopedConnectionList()
142 , trackview (other.trackview)
143 , frame_position (-1)
144 , item_name (other.item_name)
145 , selection_frame (0)
147 , _recregion (other._recregion)
148 , _automation (other._automation)
149 , _dragging (other._dragging)
152 /* share the other's parent, but still create a new group */
154 ArdourCanvas::Item* parent = other.group->parent();
156 _selected = other._selected;
158 init (parent, other.samples_per_pixel, other.fill_color, other.frame_position,
159 other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
163 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color,
164 framepos_t start, framepos_t duration, Visibility vis,
165 bool wide, bool high)
167 group = new ArdourCanvas::Container (parent);
168 CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
169 group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
171 fill_color = base_color;
172 fill_color_name = "time axis view item base";
173 samples_per_pixel = fpp;
174 frame_position = start;
175 item_duration = duration;
176 name_connected = false;
177 position_locked = false;
178 max_item_duration = ARDOUR::max_framepos;
179 min_item_duration = 0;
180 show_vestigial = true;
185 wide_enough_for_name = wide;
186 high_enough_for_name = high;
190 warning << "Time Axis Item Duration == 0" << endl;
193 if (visibility & ShowFrame) {
194 frame = new ArdourCanvas::Rectangle (group,
195 ArdourCanvas::Rect (0.0, 0.0,
196 trackview.editor().sample_to_pixel(duration),
197 trackview.current_height()));
199 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 (ARDOUR_UI::config()->color ("recording rect"));
206 frame->set_outline_color (ARDOUR_UI::config()->color ("time axis frame"));
210 if (ARDOUR_UI::config()->get_show_name_highlight() && (visibility & ShowNameHighlight)) {
215 if (visibility & FullWidthNameHighlight) {
216 width = trackview.editor().sample_to_pixel(item_duration);
218 width = trackview.editor().sample_to_pixel(item_duration) - 2.0;
221 name_highlight = new ArdourCanvas::Rectangle (group,
222 ArdourCanvas::Rect (start,
223 trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE,
225 trackview.current_height() - 1.0));
226 CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
227 name_highlight->set_data ("timeaxisviewitem", this);
228 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
229 name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255));
235 if (visibility & ShowNameText) {
236 name_text = new ArdourCanvas::Text (group);
237 CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
238 if (ARDOUR_UI::config()->get_show_name_highlight()) {
239 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
241 name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
243 name_text->set_font_description (NAME_FONT);
244 name_text->set_ignore_events (true);
249 /* create our grab handles used for trimming/duration etc */
250 if (!_recregion && !_automation) {
251 double top = TimeAxisViewItem::GRAB_HANDLE_TOP;
252 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
254 frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
255 CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
256 frame_handle_start->set_outline (false);
257 frame_handle_start->set_fill (false);
258 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
260 frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
261 CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
262 frame_handle_end->set_outline (false);
263 frame_handle_end->set_fill (false);
264 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
266 frame_handle_start = frame_handle_end = 0;
269 set_color (base_color);
271 set_duration (item_duration, this);
272 set_position (start, this);
274 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
275 ARDOUR_UI::config()->ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
278 TimeAxisViewItem::~TimeAxisViewItem()
284 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
290 * Set the position of this item on the timeline.
292 * @param pos the new position
293 * @param src the identity of the object that initiated the change
294 * @return true on success
298 TimeAxisViewItem::set_position(framepos_t pos, void* src, double* delta)
300 if (position_locked) {
304 frame_position = pos;
306 double new_unit_pos = trackview.editor().sample_to_pixel (pos);
309 (*delta) = new_unit_pos - group->position().x;
314 if (new_unit_pos == group->position().x) {
319 group->set_x_position (new_unit_pos);
320 PositionChanged (frame_position, src); /* EMIT_SIGNAL */
325 /** @return position of this item on the timeline */
327 TimeAxisViewItem::get_position() const
329 return frame_position;
333 * Set the duration of this item.
335 * @param dur the new duration of this item
336 * @param src the identity of the object that initiated the change
337 * @return true on success
341 TimeAxisViewItem::set_duration (framecnt_t dur, void* src)
343 if ((dur > max_item_duration) || (dur < min_item_duration)) {
344 warning << string_compose (
345 P_("new duration %1 frame is out of bounds for %2", "new duration of %1 frames is out of bounds for %2", dur),
346 get_item_name(), dur)
357 double end_pixel = trackview.editor().sample_to_pixel (frame_position + dur);
358 double first_pixel = trackview.editor().sample_to_pixel (frame_position);
360 reset_width_dependent_items (end_pixel - first_pixel);
362 DurationChanged (dur, src); /* EMIT_SIGNAL */
366 /** @return duration of this item */
368 TimeAxisViewItem::get_duration() const
370 return item_duration;
374 * Set the maximum duration that this item can have.
376 * @param dur the new maximum duration
377 * @param src the identity of the object that initiated the change
380 TimeAxisViewItem::set_max_duration(framecnt_t dur, void* src)
382 max_item_duration = dur;
383 MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
386 /** @return the maximum duration that this item may have */
388 TimeAxisViewItem::get_max_duration() const
390 return max_item_duration;
394 * Set the minimum duration that this item may have.
396 * @param the minimum duration that this item may be set to
397 * @param src the identity of the object that initiated the change
400 TimeAxisViewItem::set_min_duration(framecnt_t dur, void* src)
402 min_item_duration = dur;
403 MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
406 /** @return the minimum duration that this item mey have */
408 TimeAxisViewItem::get_min_duration() const
410 return min_item_duration;
414 * Set whether this item is locked to its current position.
415 * Locked items cannot be moved until the item is unlocked again.
417 * @param yn true to lock this item to its current position
418 * @param src the identity of the object that initiated the change
421 TimeAxisViewItem::set_position_locked(bool yn, void* src)
423 position_locked = yn;
424 set_trim_handle_colors();
425 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
428 /** @return true if this item is locked to its current position */
430 TimeAxisViewItem::get_position_locked() const
432 return position_locked;
436 * Set whether the maximum duration constraint is active.
438 * @param active set true to enforce the max duration constraint
439 * @param src the identity of the object that initiated the change
442 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
444 max_duration_active = active;
447 /** @return true if the maximum duration constraint is active */
449 TimeAxisViewItem::get_max_duration_active() const
451 return max_duration_active;
455 * Set whether the minimum duration constraint is active.
457 * @param active set true to enforce the min duration constraint
458 * @param src the identity of the object that initiated the change
462 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
464 min_duration_active = active;
467 /** @return true if the maximum duration constraint is active */
469 TimeAxisViewItem::get_min_duration_active() const
471 return min_duration_active;
475 * Set the name of this item.
477 * @param new_name the new name of this item
478 * @param src the identity of the object that initiated the change
482 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
484 if (new_name != item_name) {
485 std::string temp_name = item_name;
486 item_name = new_name;
487 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
491 /** @return the name of this item */
493 TimeAxisViewItem::get_item_name() const
499 * Set selection status.
501 * @param yn true if this item is currently selected
504 TimeAxisViewItem::set_selected(bool yn)
506 if (_selected == yn) {
510 Selectable::set_selected (yn);
512 set_name_text_color ();
514 if (_selected && frame) {
515 if (!selection_frame) {
516 selection_frame = new ArdourCanvas::Rectangle (group);
517 selection_frame->set_fill (false);
518 selection_frame->set_outline_color (ARDOUR_UI::config()->color ("selected time axis frame"));
519 selection_frame->set_ignore_events (true);
521 selection_frame->set (frame->get().shrink (1.0));
522 selection_frame->show ();
524 if (selection_frame) {
525 selection_frame->hide ();
530 /** @return the TimeAxisView that this item is on */
532 TimeAxisViewItem::get_time_axis_view () const
538 * Set the displayed item text.
539 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
541 * @param new_name the new name text to display
545 TimeAxisViewItem::set_name_text(const string& new_name)
551 name_text_width = pixel_width (new_name, NAME_FONT) + 2;
552 name_text->set (new_name);
557 * Set the height of this item.
559 * @param h new height
562 TimeAxisViewItem::set_height (double height)
566 manage_name_highlight ();
568 if (visibility & ShowNameText) {
569 if (ARDOUR_UI::config()->get_show_name_highlight()) {
570 name_text->set_y_position (height - NAME_Y_OFFSET);
572 name_text->set_y_position (NAME_Y_OFFSET);
579 frame->set_y1 (height);
581 if (frame_handle_start) {
582 frame_handle_start->set_y1 (height);
583 frame_handle_end->set_y1 (height);
586 if (selection_frame) {
587 selection_frame->set (frame->get().shrink (1.0));
591 if (vestigial_frame) {
592 vestigial_frame->set_y0 (1.0);
593 vestigial_frame->set_y1 (height);
600 TimeAxisViewItem::manage_name_highlight ()
602 if (!name_highlight) {
606 if (_height < NAME_HIGHLIGHT_THRESH) {
607 high_enough_for_name = false;
609 high_enough_for_name = true;
613 wide_enough_for_name = false;
615 wide_enough_for_name = true;
618 if (name_highlight && wide_enough_for_name && high_enough_for_name) {
620 name_highlight->show();
621 name_highlight->set (ArdourCanvas::Rect (1.0, (double) _height - NAME_HIGHLIGHT_SIZE, _width, (double) _height - 1.0));
624 name_highlight->hide();
631 TimeAxisViewItem::set_color (uint32_t base_color)
633 fill_color = base_color;
638 TimeAxisViewItem::get_canvas_frame()
644 TimeAxisViewItem::get_canvas_group()
650 TimeAxisViewItem::get_name_highlight()
652 return name_highlight;
656 * Convenience method to set the various canvas item colors
659 TimeAxisViewItem::set_colors()
663 if (name_highlight) {
664 name_highlight->set_fill_color (fill_color);
667 set_name_text_color ();
668 set_trim_handle_colors();
672 TimeAxisViewItem::set_name_text_color ()
681 if (ARDOUR_UI::config()->get_show_name_highlight()) {
682 /* name text will always be on top of name highlight, which
683 will always use our fill color.
687 /* name text will be on top of the item, whose color
688 may vary depending on various conditions.
690 f = get_fill_color ();
693 name_text->set_color (ArdourCanvas::contrasting_text_color (f));
697 TimeAxisViewItem::get_fill_color () const
699 const std::string mod_name = (_dragging ? "dragging region" : fill_color_name);
702 return ARDOUR_UI::config()->color_mod ("selected region base", mod_name);
703 } else if (_recregion) {
704 return ARDOUR_UI::config()->color ("recording rect");
705 } else if ((!ARDOUR_UI::config()->get_show_name_highlight() || high_enough_for_name) &&
706 !ARDOUR_UI::config()->get_color_regions_using_track_color()) {
707 return ARDOUR_UI::config()->color_mod (fill_color_name, mod_name);
709 return ARDOUR_UI::config()->color_mod (fill_color, mod_name);
713 * Sets the frame color depending on whether this item is selected
716 TimeAxisViewItem::set_frame_color()
722 frame->set_fill_color (get_fill_color());
723 set_frame_gradient ();
726 frame->set_outline_color (ARDOUR_UI::config()->color ("time axis frame"));
731 TimeAxisViewItem::set_frame_gradient ()
733 if (ARDOUR_UI::config()->get_timeline_item_gradient_depth() == 0.0) {
734 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
738 ArdourCanvas::Fill::StopList stops;
741 ArdourCanvas::Color f (get_fill_color());
743 /* need to get alpha value */
744 ArdourCanvas::color_to_rgba (f, r, g, b, a);
746 stops.push_back (std::make_pair (0.0, f));
748 /* now a darker version */
750 ArdourCanvas::color_to_hsv (f, h, s, v);
752 v = min (1.0, v * (1.0 - ARDOUR_UI::config()->get_timeline_item_gradient_depth()));
754 ArdourCanvas::Color darker = ArdourCanvas::hsva_to_color (h, s, v, a);
755 stops.push_back (std::make_pair (1.0, darker));
757 frame->set_gradient (stops, true);
761 * Set the colors of the start and end trim handle depending on object state
764 TimeAxisViewItem::set_trim_handle_colors()
767 /* Leave them transparent for now */
768 if (frame_handle_start) {
769 frame_handle_start->set_fill_color (0x00000000);
770 frame_handle_end->set_fill_color (0x00000000);
773 if (frame_handle_start) {
774 if (position_locked) {
775 frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
776 frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
778 frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
779 frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
786 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
789 case GDK_LEAVE_NOTIFY:
790 /* always hide the handle whenever we leave, no matter what mode */
791 item->set_fill (false);
793 case GDK_ENTER_NOTIFY:
794 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject) {
795 /* Never set this to be visible in other modes. Note, however,
796 that we do need to undo visibility (LEAVE_NOTIFY case above) no
797 matter what the mode is. */
798 item->set_fill (true);
807 /** @return the frames per pixel */
809 TimeAxisViewItem::get_samples_per_pixel () const
811 return samples_per_pixel;
814 /** Set the frames per pixel of this item.
815 * This item is used to determine the relative visual size and position of this item
816 * based upon its duration and start value.
818 * @param fpp the new frames per pixel
821 TimeAxisViewItem::set_samples_per_pixel (double fpp)
823 samples_per_pixel = fpp;
824 set_position (this->get_position(), this);
826 double end_pixel = trackview.editor().sample_to_pixel (frame_position + get_duration());
827 double first_pixel = trackview.editor().sample_to_pixel (frame_position);
829 reset_width_dependent_items (end_pixel - first_pixel);
833 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
835 _width = pixel_width;
837 manage_name_highlight ();
840 if (pixel_width < 2.0) {
842 if (show_vestigial) {
844 if (!vestigial_frame) {
845 vestigial_frame = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, 0.0, 2.0, trackview.current_height()));
846 CANVAS_DEBUG_NAME (vestigial_frame, string_compose ("vestigial frame for %1", get_item_name()));
847 vestigial_frame->set_outline_color (ARDOUR_UI::config()->color ("vestigial frame"));
848 vestigial_frame->set_fill_color (ARDOUR_UI::config()->color ("vestigial frame"));
849 vestigial_frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
852 vestigial_frame->show();
859 if (frame_handle_start) {
860 frame_handle_start->hide();
861 frame_handle_end->hide();
865 if (vestigial_frame) {
866 vestigial_frame->hide();
871 /* Note: x0 is always zero - the position is defined by
872 * the position of the group, not the frame.
874 frame->set_x1 (pixel_width);
876 if (selection_frame) {
877 selection_frame->set (frame->get().shrink (1.0));
881 if (frame_handle_start) {
882 if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
884 * there's less than GRAB_HANDLE_WIDTH of the region between
885 * the right-hand end of frame_handle_start and the left-hand
886 * end of frame_handle_end, so disable the handles
889 frame_handle_start->hide();
890 frame_handle_end->hide();
892 frame_handle_start->show();
893 frame_handle_end->set_x0 (pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
894 frame_handle_end->set_x1 (pixel_width);
895 frame_handle_end->show();
902 TimeAxisViewItem::manage_name_text ()
904 int visible_name_width;
910 if (!wide_enough_for_name || !high_enough_for_name) {
915 if (name_text->text().empty()) {
919 visible_name_width = name_text_width;
921 if (visible_name_width > _width - NAME_X_OFFSET) {
922 visible_name_width = _width - NAME_X_OFFSET;
925 if (visible_name_width < 1) {
928 name_text->clamp_width (visible_name_width);
934 * Callback used to remove this time axis item during the gtk idle loop.
935 * This is used to avoid deleting the obejct while inside the remove_this_item
938 * @param item the TimeAxisViewItem to remove.
939 * @param src the identity of the object that initiated the change.
942 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
944 item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
951 TimeAxisViewItem::set_y (double y)
953 group->set_y_position (y);
957 TimeAxisViewItem::parameter_changed (string p)
959 if (p == "color-regions-using-track-color") {
961 } else if (p == "timeline-item-gradient-depth") {
962 set_frame_gradient ();
967 TimeAxisViewItem::drag_start ()
974 TimeAxisViewItem::drag_end ()