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.
21 #include <pbd/error.h>
23 #include <ardour/types.h>
24 #include <ardour/ardour.h>
26 #include <gtkmm2ext/utils.h>
28 #include "public_editor.h"
29 #include "time_axis_view_item.h"
30 #include "time_axis_view.h"
31 #include "simplerect.h"
33 #include "canvas_impl.h"
34 #include "rgb_macros.h"
39 using namespace Editing;
42 //------------------------------------------------------------------------------
43 /** Initialize const static memeber data */
45 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
46 bool TimeAxisViewItem::have_name_font = false;
47 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
48 const double TimeAxisViewItem::GRAB_HANDLE_LENGTH = 6 ;
50 double TimeAxisViewItem::NAME_Y_OFFSET;
51 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
52 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
55 //---------------------------------------------------------------------------------------//
56 // Constructor / Desctructor
59 * Constructs a new TimeAxisViewItem.
61 * @param it_name the unique name/Id of this item
62 * @param parant the parent canvas group
63 * @param tv the TimeAxisView we are going to be added to
64 * @param spu samples per unit
66 * @param start the start point of this item
67 * @param duration the duration of this item
69 TimeAxisViewItem::TimeAxisViewItem(const string & it_name, ArdourCanvas::Group& parent, TimeAxisView& tv, double spu, Gdk::Color& base_color,
70 jack_nframes_t start, jack_nframes_t duration,
74 if (!have_name_font) {
76 /* first constructed item sets up font info */
78 NAME_FONT = get_font_for_style (N_("TimeAxisViewItemName"));
84 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
88 layout->set_font_description (NAME_FONT);
89 Gtkmm2ext::get_ink_pixel_size (layout, width, height);
91 NAME_Y_OFFSET = height + 4;
92 NAME_HIGHLIGHT_SIZE = height + 6;
93 NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 2;
95 have_name_font = true;
99 samples_per_unit = spu ;
100 should_show_selection = true;
101 frame_position = start ;
102 item_duration = duration ;
103 name_connected = false;
105 position_locked = false ;
106 max_item_duration = ARDOUR::max_frames;
107 min_item_duration = 0 ;
108 show_vestigial = true;
112 warning << "Time Axis Item Duration == 0" << endl ;
115 group = new ArdourCanvas::Group (parent);
117 vestigial_frame = new ArdourCanvas::SimpleRect (*group);
118 vestigial_frame->property_x1() = (double) 0.0;
119 vestigial_frame->property_y1() = (double) 1.0;
120 vestigial_frame->property_x2() = 2.0;
121 vestigial_frame->property_y2() = (double) trackview.height;
122 vestigial_frame->property_outline_color_rgba() = color_map[cVestigialFrameOutline];
123 vestigial_frame->property_fill_color_rgba() = color_map[cVestigialFrameFill];
124 vestigial_frame->hide ();
126 if (visibility & ShowFrame) {
127 frame = new ArdourCanvas::SimpleRect (*group);
128 frame->property_x1() = (double) 0.0;
129 frame->property_y1() = (double) 1.0;
130 frame->property_x2() = (double) trackview.editor.frame_to_pixel(duration);
131 frame->property_y2() = (double) trackview.height;
132 frame->property_outline_color_rgba() = color_map[cTimeAxisFrameOutline];
133 frame->property_fill_color_rgba() = color_map[cTimeAxisFrameFill];
135 /* by default draw all 4 edges */
137 uint32_t outline_what = 0x1|0x2|0x4|0x8;
139 if (visibility & HideFrameLR) {
140 outline_what &= ~(0x1 | 0x2);
143 if (visibility & HideFrameTB) {
144 outline_what &= ~(0x4 | 0x8);
147 frame->property_outline_what() = outline_what;
153 if (visibility & ShowNameHighlight) {
154 name_highlight = new ArdourCanvas::SimpleRect (*group);
155 if (visibility & FullWidthNameHighlight) {
156 name_highlight->property_x1() = (double) 0.0;
157 name_highlight->property_x2() = (double) (trackview.editor.frame_to_pixel(item_duration));
159 name_highlight->property_x1() = (double) 1.0;
160 name_highlight->property_x2() = (double) (trackview.editor.frame_to_pixel(item_duration)) - 1;
162 name_highlight->property_y1() = (double) (trackview.height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE);
163 name_highlight->property_y2() = (double) (trackview.height - 1);
164 name_highlight->property_outline_color_rgba() = color_map[cNameHighlightFill];
165 name_highlight->property_fill_color_rgba() = color_map[cNameHighlightOutline];
167 name_highlight->set_data ("timeaxisviewitem", this);
173 if (visibility & ShowNameText) {
174 name_text = new ArdourCanvas::Text (*group);
175 name_text->property_x() = (double) TimeAxisViewItem::NAME_X_OFFSET;
176 /* trackview.height is the bottom of the trackview. subtract 1 to get back to the bottom of the highlight,
177 then NAME_Y_OFFSET to position the text in the vertical center of the highlight
179 name_text->property_y() = (double) trackview.height - 1.0 - TimeAxisViewItem::NAME_Y_OFFSET;
180 name_text->property_font_desc() = NAME_FONT;
181 name_text->property_anchor() = Gtk::ANCHOR_NW;
183 name_text->set_data ("timeaxisviewitem", this);
189 /* create our grab handles used for trimming/duration etc */
191 if (visibility & ShowHandles) {
192 frame_handle_start = new ArdourCanvas::SimpleRect (*group);
193 frame_handle_start->property_x1() = (double) 0.0;
194 frame_handle_start->property_x2() = (double) TimeAxisViewItem::GRAB_HANDLE_LENGTH;
195 frame_handle_start->property_y1() = (double) 1.0;
196 frame_handle_start->property_y2() = (double) TimeAxisViewItem::GRAB_HANDLE_LENGTH+1;
197 frame_handle_start->property_outline_color_rgba() = color_map[cFrameHandleStartOutline];
198 frame_handle_start->property_fill_color_rgba() = color_map[cFrameHandleStartFill];
200 frame_handle_end = new ArdourCanvas::SimpleRect (*group);
201 frame_handle_end->property_x1() = (double) (trackview.editor.frame_to_pixel(get_duration())) - (TimeAxisViewItem::GRAB_HANDLE_LENGTH);
202 frame_handle_end->property_x2() = (double) trackview.editor.frame_to_pixel(get_duration());
203 frame_handle_end->property_y1() = (double) 1;
204 frame_handle_end->property_y2() = (double) TimeAxisViewItem::GRAB_HANDLE_LENGTH + 1;
205 frame_handle_end->property_outline_color_rgba() = color_map[cFrameHandleEndOutline];
206 frame_handle_end->property_fill_color_rgba() = color_map[cFrameHandleEndFill];
209 frame_handle_start = 0;
210 frame_handle_end = 0;
213 set_color (base_color) ;
215 set_duration (item_duration, this) ;
216 set_position (start, this) ;
223 TimeAxisViewItem::~TimeAxisViewItem()
229 //---------------------------------------------------------------------------------------//
230 // Position and duration Accessors/Mutators
233 * Set the position of this item upon the timeline to the specified value
235 * @param pos the new position
236 * @param src the identity of the object that initiated the change
237 * @return true if the position change was a success, false otherwise
240 TimeAxisViewItem::set_position(jack_nframes_t pos, void* src, double* delta)
242 if (position_locked) {
246 frame_position = pos;
248 /* This sucks. The GnomeCanvas version I am using
249 doesn't correctly implement gnome_canvas_group_set_arg(),
250 so that simply setting the "x" arg of the group
251 fails to move the group. Instead, we have to
252 use gnome_canvas_item_move(), which does the right
253 thing. I see that in GNOME CVS, the current (Sept 2001)
254 version of GNOME Canvas rectifies this issue cleanly.
257 double old_unit_pos ;
258 double new_unit_pos = pos / samples_per_unit ;
260 old_unit_pos = group->property_x();
262 if (new_unit_pos != old_unit_pos) {
263 group->move (new_unit_pos - old_unit_pos, 0.0);
267 (*delta) = new_unit_pos - old_unit_pos;
270 PositionChanged (frame_position, src) ; /* EMIT_SIGNAL */
276 * Return the position of this item upon the timeline
278 * @return the position of this item
281 TimeAxisViewItem::get_position() const
283 return frame_position;
287 * Sets the duration of this item
289 * @param dur the new duration of this item
290 * @param src the identity of the object that initiated the change
291 * @return true if the duration change was succesful, false otherwise
294 TimeAxisViewItem::set_duration (jack_nframes_t dur, void* src)
296 if ((dur > max_item_duration) || (dur < min_item_duration)) {
297 warning << string_compose (_("new duration %1 frames is out of bounds for %2"), get_item_name(), dur)
308 double pixel_width = trackview.editor.frame_to_pixel (dur);
310 reset_width_dependent_items (pixel_width);
312 DurationChanged (dur, src) ; /* EMIT_SIGNAL */
317 * Returns the duration of this item
321 TimeAxisViewItem::get_duration() const
323 return (item_duration);
327 * Sets the maximum duration that this item make have.
329 * @param dur the new maximum duration
330 * @param src the identity of the object that initiated the change
333 TimeAxisViewItem::set_max_duration(jack_nframes_t dur, void* src)
335 max_item_duration = dur ;
336 MaxDurationChanged(max_item_duration, src) ; /* EMIT_SIGNAL */
340 * Returns the maxmimum duration that this item may be set to
342 * @return the maximum duration that this item may be set to
345 TimeAxisViewItem::get_max_duration() const
347 return (max_item_duration) ;
351 * Sets the minimu duration that this item may be set to
353 * @param the minimum duration that this item may be set to
354 * @param src the identity of the object that initiated the change
357 TimeAxisViewItem::set_min_duration(jack_nframes_t dur, void* src)
359 min_item_duration = dur ;
360 MinDurationChanged(max_item_duration, src) ; /* EMIT_SIGNAL */
364 * Returns the minimum duration that this item mey be set to
366 * @return the nimum duration that this item mey be set to
369 TimeAxisViewItem::get_min_duration() const
371 return(min_item_duration) ;
375 * Sets whether the position of this Item is locked to its current position
376 * Locked items cannot be moved until the item is unlocked again.
378 * @param yn set to true to lock this item to its current position
379 * @param src the identity of the object that initiated the change
382 TimeAxisViewItem::set_position_locked(bool yn, void* src)
384 position_locked = yn ;
385 set_trim_handle_colors() ;
386 PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
390 * Returns whether this item is locked to its current position
392 * @return true if this item is locked to its current posotion
396 TimeAxisViewItem::get_position_locked() const
398 return (position_locked);
402 * Sets whether the Maximum Duration constraint is active and should be enforced
404 * @param active set true to enforce the max duration constraint
405 * @param src the identity of the object that initiated the change
408 TimeAxisViewItem::set_max_duration_active(bool active, void* src)
410 max_duration_active = active ;
414 * Returns whether the Maximum Duration constraint is active and should be enforced
416 * @return true if the maximum duration constraint is active, false otherwise
419 TimeAxisViewItem::get_max_duration_active() const
421 return(max_duration_active) ;
425 * Sets whether the Minimum Duration constraint is active and should be enforced
427 * @param active set true to enforce the min duration constraint
428 * @param src the identity of the object that initiated the change
431 TimeAxisViewItem::set_min_duration_active(bool active, void* src)
433 min_duration_active = active ;
437 * Returns whether the Maximum Duration constraint is active and should be enforced
439 * @return true if the maximum duration constraint is active, false otherwise
442 TimeAxisViewItem::get_min_duration_active() const
444 return(min_duration_active) ;
447 //---------------------------------------------------------------------------------------//
448 // Name/Id Accessors/Mutators
451 * Set the name/Id of this item.
453 * @param new_name the new name of this item
454 * @param src the identity of the object that initiated the change
457 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
459 if (new_name != item_name) {
460 std::string temp_name = item_name ;
461 item_name = new_name ;
462 NameChanged (item_name, temp_name, src) ; /* EMIT_SIGNAL */
467 * Returns the name/id of this item
469 * @return the name/id of this item
472 TimeAxisViewItem::get_item_name() const
477 //---------------------------------------------------------------------------------------//
481 * Set to true to indicate that this item is currently selected
483 * @param yn true if this item is currently selected
484 * @param src the identity of the object that initiated the change
487 TimeAxisViewItem::set_selected(bool yn)
489 if (_selected != yn) {
490 Selectable::set_selected (yn);
496 TimeAxisViewItem::set_should_show_selection (bool yn)
498 if (should_show_selection != yn) {
499 should_show_selection = yn;
504 //---------------------------------------------------------------------------------------//
505 // Parent Componenet Methods
508 * Returns the TimeAxisView that this item is upon
510 * @return the timeAxisView that this item is placed upon
513 TimeAxisViewItem::get_time_axis_view()
517 //---------------------------------------------------------------------------------------//
521 * Sets the displayed item text
522 * This item is the visual text name displayed on the canvas item, this can be different to the name of the item
524 * @param new_name the new name text to display
527 TimeAxisViewItem::set_name_text(std::string new_name)
530 name_text->property_text() = new_name.c_str();
535 * Set the height of this item
537 * @param h the new height
540 TimeAxisViewItem::set_height(double height)
542 if (name_highlight) {
543 if (height < NAME_HIGHLIGHT_THRESH) {
544 name_highlight->hide();
549 name_highlight->show();
555 if (height > NAME_HIGHLIGHT_SIZE) {
556 name_highlight->property_y1() = (double) height+1 - NAME_HIGHLIGHT_SIZE;
557 name_highlight->property_y2() = (double) height;
560 /* it gets hidden now anyway */
561 name_highlight->property_y1() = (double) 1.0;
562 name_highlight->property_y2() = (double) height;
567 name_text->property_y() = height+1 - NAME_Y_OFFSET;
568 if (height < NAME_HIGHLIGHT_THRESH) {
569 name_text->property_fill_color_rgba() = fill_color;
572 name_text->property_fill_color_rgba() = label_color;
577 frame->property_y2() = height+1;
580 vestigial_frame->property_y2() = height+1;
587 TimeAxisViewItem::set_color(Gdk::Color& base_color)
589 compute_colors (base_color);
597 TimeAxisViewItem::get_canvas_frame()
606 TimeAxisViewItem::get_canvas_group()
615 TimeAxisViewItem::get_name_highlight()
617 return (name_highlight) ;
624 TimeAxisViewItem::get_name_text()
630 * Calculates some contrasting color for displaying various parts of this item, based upon the base color
632 * @param color the base color of the item
635 TimeAxisViewItem::compute_colors(Gdk::Color& base_color)
637 unsigned char radius ;
640 unsigned char r,g,b ;
642 /* FILL: this is simple */
643 r = base_color.get_red()/256 ;
644 g = base_color.get_green()/256 ;
645 b = base_color.get_blue()/256 ;
646 fill_color = RGBA_TO_UINT(r,g,b,255) ;
649 if the overall saturation is strong, make the minor colors light.
650 if its weak, make them dark.
652 we do this by moving an equal distance to the other side of the
653 central circle in the color wheel from where we started.
656 radius = (unsigned char) rint (floor (sqrt (static_cast<double>(r*r + g*g + b+b))/3.0f)) ;
657 minor_shift = 125 - radius ;
659 /* LABEL: rotate around color wheel by 120 degrees anti-clockwise */
661 r = base_color.get_red()/256;
662 g = base_color.get_green()/256;
663 b = base_color.get_blue()/256;
669 /* red sector => green */
674 /* green sector => blue */
682 /* blue sector => red */
687 /* green sector => blue */
696 label_color = RGBA_TO_UINT(r,g,b,255);
697 r = (base_color.get_red()/256) + 127 ;
698 g = (base_color.get_green()/256) + 127 ;
699 b = (base_color.get_blue()/256) + 127 ;
701 label_color = RGBA_TO_UINT(r,g,b,255);
703 /* XXX can we do better than this ? */
704 /* We're trying ;) */
707 //frame_color_r = 192;
708 //frame_color_g = 192;
709 //frame_color_b = 194;
711 //selected_frame_color_r = 182;
712 //selected_frame_color_g = 145;
713 //selected_frame_color_b = 168;
715 //handle_color_r = 25 ;
716 //handle_color_g = 0 ;
717 //handle_color_b = 255 ;
718 //lock_handle_color_r = 235 ;
719 //lock_handle_color_g = 16;
720 //lock_handle_color_b = 16;
724 * Convenience method to set the various canvas item colors
727 TimeAxisViewItem::set_colors()
731 double height = NAME_HIGHLIGHT_THRESH;
734 height = frame->property_y2();
737 if (height < NAME_HIGHLIGHT_THRESH) {
738 name_text->property_fill_color_rgba() = fill_color;
741 name_text->property_fill_color_rgba() = label_color;
745 if (name_highlight) {
746 name_highlight->property_fill_color_rgba() = fill_color;
747 name_highlight->property_outline_color_rgba() = fill_color;
749 set_trim_handle_colors() ;
753 * Sets the frame color depending on whether this item is selected
756 TimeAxisViewItem::set_frame_color()
761 if (_selected && should_show_selection) {
762 UINT_TO_RGBA(color_map[cSelectedFrameBase], &r, &g, &b, &a);
763 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity);
765 UINT_TO_RGBA(color_map[cFrameBase], &r, &g, &b, &a);
766 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity);
772 * Sets 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() = color_map[cTrimHandleLockedStart];
781 frame_handle_end->property_fill_color_rgba() = color_map[cTrimHandleLockedEnd];
783 frame_handle_start->property_fill_color_rgba() = color_map[cTrimHandleStart];
784 frame_handle_end->property_fill_color_rgba() = color_map[cTrimHandleEnd];
790 TimeAxisViewItem::get_samples_per_unit()
792 return(samples_per_unit) ;
796 TimeAxisViewItem::set_samples_per_unit (double spu)
798 samples_per_unit = spu ;
799 set_position (this->get_position(), this);
800 reset_width_dependent_items ((double)get_duration() / samples_per_unit);
804 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
806 if (pixel_width < GRAB_HANDLE_LENGTH * 2) {
808 if (frame_handle_start) {
809 frame_handle_start->hide();
810 frame_handle_end->hide();
813 } if (pixel_width < 2.0) {
815 if (show_vestigial) {
816 vestigial_frame->show();
819 if (name_highlight) {
820 name_highlight->hide();
830 if (frame_handle_start) {
831 frame_handle_start->hide();
832 frame_handle_end->hide();
836 vestigial_frame->hide();
838 if (name_highlight) {
840 double height = name_highlight->property_y2 ();
842 if (height < NAME_HIGHLIGHT_THRESH) {
843 name_highlight->hide();
848 name_highlight->show();
851 reset_name_width (pixel_width);
855 if (visibility & FullWidthNameHighlight) {
856 name_highlight->property_x2() = pixel_width;
858 name_highlight->property_x2() = pixel_width - 1.0;
865 frame->property_x2() = pixel_width;
868 if (frame_handle_start) {
869 if (pixel_width < (2*TimeAxisViewItem::GRAB_HANDLE_LENGTH)) {
870 frame_handle_start->hide();
871 frame_handle_end->hide();
873 frame_handle_start->show();
874 frame_handle_end->property_x1() = pixel_width - (TimeAxisViewItem::GRAB_HANDLE_LENGTH);
875 frame_handle_end->show();
876 frame_handle_end->property_x2() = pixel_width;
882 TimeAxisViewItem::reset_name_width (double pixel_width)
884 if (name_text == 0) {
890 ustring ustr = fit_to_pixels (item_name, (int) floor (pixel_width - NAME_X_OFFSET), NAME_FONT, width);
898 /* don't use name for event handling if it leaves no room
899 for trimming to work.
902 if (pixel_width - width < (NAME_X_OFFSET * 2.0)) {
903 if (name_connected) {
904 name_connected = false;
907 if (!name_connected) {
908 name_connected = true;
912 name_text->property_text() = ustr;
918 //---------------------------------------------------------------------------------------//
919 // Handle time axis removal
922 * Handles the Removal of this time axis item
923 * This _needs_ to be called to alert others of the removal properly, ie where the source
924 * of the removal came from.
926 * XXX Although im not too happy about this method of doing things, I cant think of a cleaner method
927 * just now to capture the source of the removal
929 * @param src the identity of the object that initiated the change
932 TimeAxisViewItem::remove_this_item(void* src)
935 defer to idle loop, otherwise we'll delete this object
936 while we're still inside this function ...
938 Glib::signal_idle().connect(bind (sigc::ptr_fun (&TimeAxisViewItem::idle_remove_this_item), this, src));
942 * Callback used to remove this time axis item during the gtk idle loop
943 * This is used to avoid deleting the obejct while inside the remove_this_item
946 * @param item the TimeAxisViewItem to remove
947 * @param src the identity of the object that initiated the change
950 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
952 item->ItemRemoved (item->get_item_name(), src) ; /* EMIT_SIGNAL */