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.
20 #include "pbd/error.h"
21 #include "pbd/stacktrace.h"
23 #include "ardour/types.h"
24 #include "ardour/ardour.h"
26 #include "gtkmm2ext/utils.h"
27 #include "gtkmm2ext/gui_thread.h"
29 #include "ardour_ui.h"
31 * ardour_ui.h was moved up in the include list
32 * due to a conflicting definition of 'Rect' between
33 * Apple's MacTypes.h file and GTK
36 #include "public_editor.h"
37 #include "time_axis_view_item.h"
38 #include "time_axis_view.h"
39 #include "simplerect.h"
41 #include "canvas_impl.h"
42 #include "rgb_macros.h"
47 using namespace Editing;
50 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
53 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
54 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
55 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 6;
56 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 5;
58 int TimeAxisViewItem::NAME_HEIGHT;
59 double TimeAxisViewItem::NAME_Y_OFFSET;
60 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
61 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
64 TimeAxisViewItem::set_constant_heights ()
66 NAME_FONT = get_font_for_style (X_("TimeAxisViewItemName"));
72 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
76 layout->set_font_description (NAME_FONT);
77 Gtkmm2ext::get_ink_pixel_size (layout, width, height);
80 NAME_Y_OFFSET = height + 3;
81 NAME_HIGHLIGHT_SIZE = height + 2;
82 NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 3;
86 * Construct a new TimeAxisViewItem.
88 * @param it_name the unique name of this item
89 * @param parent the parent canvas group
90 * @param tv the TimeAxisView we are going to be added to
91 * @param spu samples per unit
93 * @param start the start point of this item
94 * @param duration the duration of this item
95 * @param recording true if this is a recording region view
96 * @param automation true if this is an automation region view
98 TimeAxisViewItem::TimeAxisViewItem(
99 const string & it_name, ArdourCanvas::Group& parent, TimeAxisView& tv, double spu, Gdk::Color const & base_color,
100 framepos_t start, framecnt_t duration, bool recording, bool automation, Visibility vis
104 , _recregion (recording)
105 , _automation (automation)
108 group = new ArdourCanvas::Group (parent);
110 init (it_name, spu, base_color, start, duration, vis, true, true);
113 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
116 , PBD::ScopedConnectionList()
117 , trackview (other.trackview)
118 , _recregion (other._recregion)
119 , _automation (other._automation)
120 , _dragging (other._dragging)
126 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
127 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
129 /* share the other's parent, but still create a new group */
131 Gnome::Canvas::Group* parent = other.group->property_parent();
133 group = new ArdourCanvas::Group (*parent);
135 _selected = other._selected;
137 init (other.item_name, other.samples_per_unit, c, other.frame_position,
138 other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
142 TimeAxisViewItem::init (
143 const string& it_name, double spu, Gdk::Color const & base_color, framepos_t start, framepos_t duration, Visibility vis, bool wide, bool high)
146 samples_per_unit = spu;
147 frame_position = start;
148 item_duration = duration;
149 name_connected = false;
151 position_locked = false;
152 max_item_duration = ARDOUR::max_framepos;
153 min_item_duration = 0;
154 show_vestigial = true;
157 name_pixbuf_width = 0;
159 wide_enough_for_name = wide;
160 high_enough_for_name = high;
164 warning << "Time Axis Item Duration == 0" << endl;
167 vestigial_frame = new ArdourCanvas::SimpleRect (*group, 0.0, 1.0, 2.0, trackview.current_height());
168 vestigial_frame->hide ();
169 vestigial_frame->property_outline_what() = 0xF;
170 vestigial_frame->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_VestigialFrame.get();
171 vestigial_frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_VestigialFrame.get();
173 if (visibility & ShowFrame) {
174 frame = new ArdourCanvas::SimpleRect (*group, 0.0, 1.0, trackview.editor().frame_to_pixel(duration), trackview.current_height());
176 frame->property_outline_pixels() = 1;
177 frame->property_outline_what() = 0xF;
180 frame->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
182 frame->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
185 frame->property_outline_what() = 0x1|0x2|0x4|0x8;
191 if (visibility & ShowNameHighlight) {
193 if (visibility & FullWidthNameHighlight) {
194 name_highlight = new ArdourCanvas::SimpleRect (*group, 0.0, trackview.editor().frame_to_pixel(item_duration), trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE, trackview.current_height());
196 name_highlight = new ArdourCanvas::SimpleRect (*group, 1.0, trackview.editor().frame_to_pixel(item_duration) - 1, trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE, trackview.current_height());
199 name_highlight->set_data ("timeaxisviewitem", this);
200 name_highlight->property_outline_what() = 0x4;
201 /* we should really use a canvas color property here */
202 name_highlight->property_outline_color_rgba() = RGBA_TO_UINT (0,0,0,255);
208 if (visibility & ShowNameText) {
209 name_pixbuf = new ArdourCanvas::Pixbuf(*group);
210 name_pixbuf->property_x() = NAME_X_OFFSET;
211 name_pixbuf->property_y() = trackview.current_height() + 1 - NAME_Y_OFFSET;
217 /* create our grab handles used for trimming/duration etc */
218 if (!_recregion && !_automation) {
219 double top = TimeAxisViewItem::GRAB_HANDLE_TOP;
220 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
222 frame_handle_start = new ArdourCanvas::SimpleRect (*group, 0.0, top, width, trackview.current_height());
223 frame_handle_start->property_outline_what() = 0x0;
224 frame_handle_end = new ArdourCanvas::SimpleRect (*group, 0.0, top, width, trackview.current_height());
225 frame_handle_end->property_outline_what() = 0x0;
227 frame_handle_start = frame_handle_end = 0;
230 set_color (base_color);
232 set_duration (item_duration, this);
233 set_position (start, this);
235 Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
238 TimeAxisViewItem::~TimeAxisViewItem()
244 TimeAxisViewItem::hide_rect ()
246 rect_visible = false;
249 if (name_highlight) {
250 name_highlight->property_outline_what() = 0x0;
251 name_highlight->property_fill_color_rgba() = UINT_RGBA_CHANGE_A(fill_color,64);
256 TimeAxisViewItem::show_rect ()
261 if (name_highlight) {
262 name_highlight->property_outline_what() = 0x4;
263 name_highlight->property_fill_color_rgba() = fill_color;
269 * Set the position of this item on the timeline.
271 * @param pos the new position
272 * @param src the identity of the object that initiated the change
273 * @return true on success
277 TimeAxisViewItem::set_position(framepos_t pos, void* src, double* delta)
279 if (position_locked) {
283 frame_position = pos;
285 /* This sucks. The GnomeCanvas version I am using
286 doesn't correctly implement gnome_canvas_group_set_arg(),
287 so that simply setting the "x" arg of the group
288 fails to move the group. Instead, we have to
289 use gnome_canvas_item_move(), which does the right
290 thing. I see that in GNOME CVS, the current (Sept 2001)
291 version of GNOME Canvas rectifies this issue cleanly.
295 double new_unit_pos = pos / samples_per_unit;
297 old_unit_pos = group->property_x();
299 if (new_unit_pos != old_unit_pos) {
300 group->move (new_unit_pos - old_unit_pos, 0.0);
304 (*delta) = new_unit_pos - old_unit_pos;
307 PositionChanged (frame_position, src); /* EMIT_SIGNAL */
312 /** @return position of this item on the timeline */
314 TimeAxisViewItem::get_position() const
316 return frame_position;
320 * Set the duration of this item.
322 * @param dur the new duration of this item
323 * @param src the identity of the object that initiated the change
324 * @return true on success
328 TimeAxisViewItem::set_duration (framecnt_t dur, void* src)
330 if ((dur > max_item_duration) || (dur < min_item_duration)) {
331 warning << string_compose (_("new duration %1 frames is out of bounds for %2"), get_item_name(), dur)
342 reset_width_dependent_items (trackview.editor().frame_to_pixel (dur));
344 DurationChanged (dur, src); /* EMIT_SIGNAL */
348 /** @return duration of this item */
350 TimeAxisViewItem::get_duration() const
352 return item_duration;
356 * Set the maximum duration that this item can have.
358 * @param dur the new maximum duration
359 * @param src the identity of the object that initiated the change
362 TimeAxisViewItem::set_max_duration(framecnt_t dur, void* src)
364 max_item_duration = dur;
365 MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
368 /** @return the maximum duration that this item may have */
370 TimeAxisViewItem::get_max_duration() const
372 return max_item_duration;
376 * Set the minimum duration that this item may have.
378 * @param the minimum duration that this item may be set to
379 * @param src the identity of the object that initiated the change
382 TimeAxisViewItem::set_min_duration(framecnt_t dur, void* src)
384 min_item_duration = dur;
385 MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
388 /** @return the minimum duration that this item mey have */
390 TimeAxisViewItem::get_min_duration() const
392 return min_item_duration;
396 * Set whether this item is locked to its current position.
397 * Locked items cannot be moved until the item is unlocked again.
399 * @param yn true to lock this item to its current position
400 * @param src the identity of the object that initiated the change
403 TimeAxisViewItem::set_position_locked(bool yn, void* src)
405 position_locked = yn;
406 set_trim_handle_colors();
407 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
410 /** @return true if this item is locked to its current position */
412 TimeAxisViewItem::get_position_locked() const
414 return position_locked;
418 * Set whether the maximum duration constraint is active.
420 * @param active set true to enforce the max duration constraint
421 * @param src the identity of the object that initiated the change
424 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
426 max_duration_active = active;
429 /** @return true if the maximum duration constraint is active */
431 TimeAxisViewItem::get_max_duration_active() const
433 return max_duration_active;
437 * Set whether the minimum duration constraint is active.
439 * @param active set true to enforce the min duration constraint
440 * @param src the identity of the object that initiated the change
444 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
446 min_duration_active = active;
449 /** @return true if the maximum duration constraint is active */
451 TimeAxisViewItem::get_min_duration_active() const
453 return min_duration_active;
457 * Set the name of this item.
459 * @param new_name the new name of this item
460 * @param src the identity of the object that initiated the change
464 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
466 if (new_name != item_name) {
467 std::string temp_name = item_name;
468 item_name = new_name;
469 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
473 /** @return the name of this item */
475 TimeAxisViewItem::get_item_name() const
481 * Set selection status.
483 * @param yn true if this item is currently selected
486 TimeAxisViewItem::set_selected(bool yn)
488 if (_selected != yn) {
489 Selectable::set_selected (yn);
494 /** @return the TimeAxisView that this item is on */
496 TimeAxisViewItem::get_time_axis_view () const
502 * Set the displayed item text.
503 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
505 * @param new_name the new name text to display
509 TimeAxisViewItem::set_name_text(const string& new_name)
515 last_item_width = trackview.editor().frame_to_pixel(item_duration);
516 name_pixbuf_width = pixel_width (new_name, NAME_FONT) + 2;
517 name_pixbuf->property_pixbuf() = pixbuf_from_string(new_name, NAME_FONT, name_pixbuf_width, NAME_HEIGHT, Gdk::Color ("#000000"));
522 * Set the height of this item.
524 * @param h new height
527 TimeAxisViewItem::set_height (double height)
531 if (name_highlight) {
532 if (height < NAME_HIGHLIGHT_THRESH) {
533 name_highlight->hide ();
534 high_enough_for_name = false;
537 name_highlight->show();
538 high_enough_for_name = true;
541 if (height > NAME_HIGHLIGHT_SIZE) {
542 name_highlight->property_y1() = (double) height - 1 - NAME_HIGHLIGHT_SIZE;
543 name_highlight->property_y2() = (double) height - 1;
546 /* it gets hidden now anyway */
547 name_highlight->property_y1() = (double) 1.0;
548 name_highlight->property_y2() = (double) height;
552 if (visibility & ShowNameText) {
553 name_pixbuf->property_y() = height + 1 - NAME_Y_OFFSET;
557 frame->property_y2() = height - 1;
558 if (frame_handle_start) {
559 frame_handle_start->property_y2() = height - 1;
560 frame_handle_end->property_y2() = height - 1;
564 vestigial_frame->property_y2() = height - 1;
566 update_name_pixbuf_visibility ();
571 TimeAxisViewItem::set_color (Gdk::Color const & base_color)
573 compute_colors (base_color);
578 TimeAxisViewItem::get_canvas_frame()
584 TimeAxisViewItem::get_canvas_group()
590 TimeAxisViewItem::get_name_highlight()
592 return name_highlight;
595 ArdourCanvas::Pixbuf*
596 TimeAxisViewItem::get_name_pixbuf()
602 * Calculate some contrasting color for displaying various parts of this item, based upon the base color.
604 * @param color the base color of the item
607 TimeAxisViewItem::compute_colors (Gdk::Color const & base_color)
609 unsigned char radius;
614 /* FILL: this is simple */
615 r = base_color.get_red()/256;
616 g = base_color.get_green()/256;
617 b = base_color.get_blue()/256;
618 fill_color = RGBA_TO_UINT(r,g,b,160);
621 if the overall saturation is strong, make the minor colors light.
622 if its weak, make them dark.
624 we do this by moving an equal distance to the other side of the
625 central circle in the color wheel from where we started.
628 radius = (unsigned char) rint (floor (sqrt (static_cast<double>(r*r + g*g + b+b))/3.0f));
629 minor_shift = 125 - radius;
631 /* LABEL: rotate around color wheel by 120 degrees anti-clockwise */
633 r = base_color.get_red()/256;
634 g = base_color.get_green()/256;
635 b = base_color.get_blue()/256;
641 /* red sector => green */
646 /* green sector => blue */
654 /* blue sector => red */
659 /* green sector => blue */
668 label_color = RGBA_TO_UINT(r,g,b,255);
669 r = (base_color.get_red()/256) + 127;
670 g = (base_color.get_green()/256) + 127;
671 b = (base_color.get_blue()/256) + 127;
673 label_color = RGBA_TO_UINT(r,g,b,255);
675 /* XXX can we do better than this ? */
679 //frame_color_r = 192;
680 //frame_color_g = 192;
681 //frame_color_b = 194;
683 //selected_frame_color_r = 182;
684 //selected_frame_color_g = 145;
685 //selected_frame_color_b = 168;
687 //handle_color_r = 25;
688 //handle_color_g = 0;
689 //handle_color_b = 255;
690 //lock_handle_color_r = 235;
691 //lock_handle_color_g = 16;
692 //lock_handle_color_b = 16;
696 * Convenience method to set the various canvas item colors
699 TimeAxisViewItem::set_colors()
703 if (name_highlight) {
704 name_highlight->property_fill_color_rgba() = fill_color;
706 set_trim_handle_colors();
710 * Sets the frame color depending on whether this item is selected
713 TimeAxisViewItem::set_frame_color()
723 f = ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get();
726 f = UINT_RGBA_CHANGE_A (f, fill_opacity);
730 f = UINT_RGBA_CHANGE_A (f, 0);
736 f = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
739 if (high_enough_for_name && !Config->get_color_regions_using_track_color()) {
740 f = ARDOUR_UI::config()->canvasvar_FrameBase.get();
746 f = UINT_RGBA_CHANGE_A (f, fill_opacity);
750 f = UINT_RGBA_CHANGE_A (f, 0);
755 frame->property_fill_color_rgba() = f;
759 f = ARDOUR_UI::config()->canvasvar_SelectedTimeAxisFrame.get();
761 f = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
765 f = UINT_RGBA_CHANGE_A (f, 64);
768 frame->property_outline_color_rgba() = f;
773 * Set the colors of the start and end trim handle depending on object state
776 TimeAxisViewItem::set_trim_handle_colors()
778 if (frame_handle_start) {
779 if (position_locked) {
780 frame_handle_start->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TrimHandleLocked.get();
781 frame_handle_end->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_TrimHandleLocked.get();
783 frame_handle_start->property_fill_color_rgba() = RGBA_TO_UINT(1, 1, 1, 0); //ARDOUR_UI::config()->canvasvar_TrimHandle.get();
784 frame_handle_end->property_fill_color_rgba() = RGBA_TO_UINT(1, 1, 1, 0); //ARDOUR_UI::config()->canvasvar_TrimHandle.get();
789 /** @return the samples per unit of this item */
791 TimeAxisViewItem::get_samples_per_unit()
793 return samples_per_unit;
797 * Set the samples per unit of this item.
798 * This item is used to determine the relative visual size and position of this item
799 * based upon its duration and start value.
801 * @param spu the new samples per unit value
804 TimeAxisViewItem::set_samples_per_unit (double spu)
806 samples_per_unit = spu;
807 set_position (this->get_position(), this);
808 reset_width_dependent_items ((double)get_duration() / samples_per_unit);
812 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
815 if (pixel_width < 2.0) {
817 if (show_vestigial) {
818 vestigial_frame->show();
821 if (name_highlight) {
822 name_highlight->hide();
829 if (frame_handle_start) {
830 frame_handle_start->hide();
831 frame_handle_end->hide();
834 wide_enough_for_name = false;
837 vestigial_frame->hide();
839 if (name_highlight) {
841 if (_height < NAME_HIGHLIGHT_THRESH) {
842 name_highlight->hide();
843 high_enough_for_name = false;
845 name_highlight->show();
846 if (!get_item_name().empty()) {
847 reset_name_width (pixel_width);
849 high_enough_for_name = true;
852 name_highlight->property_x2() = pixel_width;
857 frame->property_x2() = pixel_width;
860 if (frame_handle_start) {
861 if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
863 * there's less than GRAB_HANDLE_WIDTH of the region between
864 * the right-hand end of frame_handle_start and the left-hand
865 * end of frame_handle_end, so disable the handles
867 frame_handle_start->hide();
868 frame_handle_end->hide();
870 frame_handle_start->show();
871 frame_handle_end->property_x1() = pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH);
872 frame_handle_end->property_x2() = pixel_width;
873 frame_handle_end->show();
877 wide_enough_for_name = true;
880 update_name_pixbuf_visibility ();
884 TimeAxisViewItem::reset_name_width (double /*pixel_width*/)
888 bool pixbuf_holds_full_name;
894 it_width = trackview.editor().frame_to_pixel(item_duration);
895 pb_width = name_pixbuf_width;
897 pixbuf_holds_full_name = last_item_width > pb_width + NAME_X_OFFSET;
898 last_item_width = it_width;
900 if (pixbuf_holds_full_name && (it_width >= pb_width + NAME_X_OFFSET)) {
902 we've previously had the full name length showing
903 and its still showing.
908 if (pb_width > it_width - NAME_X_OFFSET) {
909 pb_width = it_width - NAME_X_OFFSET;
912 if (it_width <= NAME_X_OFFSET) {
913 wide_enough_for_name = false;
915 wide_enough_for_name = true;
918 update_name_pixbuf_visibility ();
924 name_pixbuf->property_pixbuf() = pixbuf_from_string(item_name, NAME_FONT, pb_width, NAME_HEIGHT, Gdk::Color ("#000000"));
928 * Callback used to remove this time axis item during the gtk idle loop.
929 * This is used to avoid deleting the obejct while inside the remove_this_item
932 * @param item the TimeAxisViewItem to remove.
933 * @param src the identity of the object that initiated the change.
936 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
938 item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
945 TimeAxisViewItem::set_y (double y)
947 double const old = group->property_y ();
949 group->move (0, y - old);
954 TimeAxisViewItem::update_name_pixbuf_visibility ()
960 if (wide_enough_for_name && high_enough_for_name) {
961 name_pixbuf->show ();
963 name_pixbuf->hide ();
968 TimeAxisViewItem::parameter_changed (string p)
970 if (p == "color-regions-using-track-color") {