1 /*
2  * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5  * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
7  * Copyright (C) 2007 Doug McLain <doug@nostar.net>
8  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
9  * Copyright (C) 2014-2016 Nick Mainsbridge <mainsbridge@gmail.com>
10  * Copyright (C) 2014 Ben Loftis <ben@harrisonconsoles.com>
11  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
28 #include <utility>
29 
30 #include "pbd/error.h"
31 
32 #include "ardour/types.h"
33 #include "ardour/ardour.h"
34 
35 #include "gtkmm2ext/utils.h"
36 #include "gtkmm2ext/gui_thread.h"
37 
38 #include "canvas/container.h"
39 #include "canvas/rectangle.h"
40 #include "canvas/debug.h"
41 #include "canvas/text.h"
42 #include "gtkmm2ext/colors.h"
43 
44 #include "ardour/profile.h"
45 
46 #include "public_editor.h"
47 #include "time_axis_view_item.h"
48 #include "time_axis_view.h"
49 #include "ui_config.h"
50 #include "utils.h"
51 #include "rgb_macros.h"
52 
53 #include "pbd/i18n.h"
54 
55 using namespace std;
56 using namespace Editing;
57 using namespace Glib;
58 using namespace PBD;
59 using namespace ARDOUR;
60 using namespace ARDOUR_UI_UTILS;
61 using namespace Gtkmm2ext;
62 
63 Pango::FontDescription TimeAxisViewItem::NAME_FONT;
64 const double TimeAxisViewItem::NAME_X_OFFSET = 15.0;
65 const double TimeAxisViewItem::GRAB_HANDLE_TOP = 0.0;
66 const double TimeAxisViewItem::GRAB_HANDLE_WIDTH = 10.0;
67 
68 int    TimeAxisViewItem::NAME_HEIGHT;
69 double TimeAxisViewItem::NAME_Y_OFFSET;
70 double TimeAxisViewItem::NAME_HIGHLIGHT_SIZE;
71 double TimeAxisViewItem::NAME_HIGHLIGHT_THRESH;
72 
73 void
set_constant_heights()74 TimeAxisViewItem::set_constant_heights ()
75 {
76 	NAME_FONT = Pango::FontDescription (UIConfiguration::instance().get_SmallFont());
77 
78 	Gtk::Window win;
79 	Gtk::Label foo;
80 	win.add (foo);
81 
82 	Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
83 	int width = 0;
84 	int height = 0;
85 
86 	layout->set_font_description (NAME_FONT);
87 	get_pixel_size (layout, width, height);
88 
89 	layout = foo.create_pango_layout (X_("H")); /* just the ascender */
90 
91 	NAME_HEIGHT = height;
92 
93 	/* Config->get_show_name_highlight) == true:
94 	        Y_OFFSET is measured from bottom of the time axis view item.
95 	   Config->get_show_name_highlight) == false:
96 	        Y_OFFSET is measured from the top of the time axis view item.
97 	*/
98 
99 	if (UIConfiguration::instance().get_show_name_highlight()) {
100 		NAME_Y_OFFSET = height + 1;
101 		NAME_HIGHLIGHT_SIZE = height + 2;
102 	} else {
103 		NAME_Y_OFFSET = 3;
104 		NAME_HIGHLIGHT_SIZE = 0;
105 	}
106 	NAME_HIGHLIGHT_THRESH = NAME_HIGHLIGHT_SIZE * 3;
107 }
108 
109 /**
110  * Construct a new TimeAxisViewItem.
111  *
112  * @param it_name the unique name of this item
113  * @param parent the parent canvas group
114  * @param tv the TimeAxisView we are going to be added to
115  * @param spu samples per unit
116  * @param base_color
117  * @param start the start point of this item
118  * @param duration the duration of this item
119  * @param recording true if this is a recording region view
120  * @param automation true if this is an automation region view
121  */
TimeAxisViewItem(const string & it_name,ArdourCanvas::Item & parent,TimeAxisView & tv,double spu,uint32_t base_color,samplepos_t start,samplecnt_t duration,bool recording,bool automation,Visibility vis)122 TimeAxisViewItem::TimeAxisViewItem(
123 	const string & it_name, ArdourCanvas::Item& parent, TimeAxisView& tv, double spu, uint32_t base_color,
124 	samplepos_t start, samplecnt_t duration, bool recording, bool automation, Visibility vis
125 	)
126 	: trackview (tv)
127 	, sample_position (-1)
128 	, item_name (it_name)
129 	, selection_frame (0)
130 	, _height (1.0)
131 	, _recregion (recording)
132 	, _automation (automation)
133 	, _dragging (false)
134 	, _width (0.0)
135 {
136 	init (&parent, spu, base_color, start, duration, vis, true, true);
137 }
138 
TimeAxisViewItem(const TimeAxisViewItem & other)139 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
140 	: trackable (other)
141 	, Selectable (other)
142 	, PBD::ScopedConnectionList()
143 	, trackview (other.trackview)
144 	, sample_position (-1)
145 	, item_name (other.item_name)
146 	, selection_frame (0)
147 	, _height (1.0)
148 	, _recregion (other._recregion)
149 	, _automation (other._automation)
150 	, _dragging (other._dragging)
151 	, _width (0.0)
152 {
153 	/* share the other's parent, but still create a new group */
154 
155 	ArdourCanvas::Item* parent = other.group->parent();
156 
157 	_selected = other._selected;
158 
159 	init (parent, other.samples_per_pixel, other.fill_color, other.sample_position,
160 	      other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
161 }
162 
163 void
init(ArdourCanvas::Item * parent,double fpp,uint32_t base_color,samplepos_t start,samplepos_t duration,Visibility vis,bool wide,bool high)164 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color,
165 			samplepos_t start, samplepos_t duration, Visibility vis,
166 			bool wide, bool high)
167 {
168 	group = new ArdourCanvas::Container (parent);
169 	CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
170 
171 	fill_color = base_color;
172 	fill_color_name = "time axis view item base";
173 	samples_per_pixel = fpp;
174 	sample_position = start;
175 	item_duration = duration;
176 	name_connected = false;
177 	position_locked = false;
178 	max_item_duration = Temporal::max_samplepos;
179 	min_item_duration = 0;
180 	visibility = vis;
181 	_sensitive = true;
182 	name_text_width = 0;
183 	last_item_width = 0;
184 	wide_enough_for_name = wide;
185 	high_enough_for_name = high;
186 
187 	if (duration == 0) {
188 		warning << "Time Axis Item Duration == 0" << endl;
189 	}
190 
191 	if (visibility & ShowFrame) {
192 		frame = new ArdourCanvas::Rectangle (group,
193 		                                     ArdourCanvas::Rect (0.0, 0.0,
194 		                                                         trackview.editor().sample_to_pixel(duration),
195 		                                                         trackview.current_height()));
196 
197 		frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
198 		frame->show ();
199 
200 		CANVAS_DEBUG_NAME (frame, string_compose ("frame for %1", get_item_name()));
201 
202 		if (_recregion) {
203 			frame->set_outline_color (UIConfiguration::instance().color ("recording rect"));
204 		} else {
205 			frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
206 		}
207 	}
208 
209 	if (UIConfiguration::instance().get_show_name_highlight() && (visibility & ShowNameHighlight)) {
210 
211 		/* rectangle size will be set in ::manage_name_highlight() */
212 		name_highlight = new ArdourCanvas::Rectangle (group);
213 		CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
214 		name_highlight->set_data ("timeaxisviewitem", this);
215 		name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
216 		name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255)); // this should use a theme color
217 
218 	} else {
219 		name_highlight = 0;
220 	}
221 
222 	if (visibility & ShowNameText) {
223 		name_text = new ArdourCanvas::Text (group);
224 		CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
225 		if (UIConfiguration::instance().get_show_name_highlight()) {
226 			name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
227 		} else {
228 			name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
229 		}
230 		name_text->set_font_description (NAME_FONT);
231 		name_text->set_ignore_events (true);
232 	} else {
233 		name_text = 0;
234 	}
235 
236 	/* create our grab handles used for trimming/duration etc */
237 	if (!_recregion && !_automation) {
238 		double top   = TimeAxisViewItem::GRAB_HANDLE_TOP;
239 		double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
240 
241 		frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
242 		CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
243 		frame_handle_start->set_outline (false);
244 		frame_handle_start->set_fill (false);
245 		frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
246 
247 		frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
248 		CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
249 		frame_handle_end->set_outline (false);
250 		frame_handle_end->set_fill (false);
251 		frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
252 	} else {
253 		frame_handle_start = frame_handle_end = 0;
254 	}
255 
256 	//set_color (base_color);
257 
258 	//set_duration (item_duration, this);
259 	//set_position (start, this);
260 
261 	group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
262 	//Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
263 	UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
264 }
265 
~TimeAxisViewItem()266 TimeAxisViewItem::~TimeAxisViewItem()
267 {
268 	delete group;
269 }
270 
271 bool
canvas_group_event(GdkEvent *)272 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
273 {
274 	return false;
275 }
276 
277 /**
278  * Set the position of this item on the timeline.
279  *
280  * @param pos the new position
281  * @param src the identity of the object that initiated the change
282  * @return true on success
283  */
284 
285 bool
set_position(samplepos_t pos,void * src,double * delta)286 TimeAxisViewItem::set_position(samplepos_t pos, void* src, double* delta)
287 {
288 	if (position_locked) {
289 		return false;
290 	}
291 
292 	sample_position = pos;
293 
294 	double new_unit_pos = trackview.editor().sample_to_pixel (pos);
295 
296 	if (delta) {
297 		(*delta) = new_unit_pos - group->position().x;
298 		if (*delta == 0.0) {
299 			return true;
300 		}
301 	} else {
302 		if (new_unit_pos == group->position().x) {
303 			return true;
304 		}
305 	}
306 
307 	group->set_x_position (new_unit_pos);
308 	PositionChanged (sample_position, src); /* EMIT_SIGNAL */
309 
310 	return true;
311 }
312 
313 /** @return position of this item on the timeline */
314 samplepos_t
get_position() const315 TimeAxisViewItem::get_position() const
316 {
317 	return sample_position;
318 }
319 
320 /**
321  * Set the duration of this item.
322  *
323  * @param dur the new duration of this item
324  * @param src the identity of the object that initiated the change
325  * @return true on success
326  */
327 
328 bool
set_duration(samplecnt_t dur,void * src)329 TimeAxisViewItem::set_duration (samplecnt_t dur, void* src)
330 {
331 	if ((dur > max_item_duration) || (dur < min_item_duration)) {
332 		warning << string_compose (
333 				P_("new duration %1 frame is out of bounds for %2", "new duration of %1 samples is out of bounds for %2", dur),
334 				get_item_name(), dur)
335 			<< endmsg;
336 		return false;
337 	}
338 
339 	if (dur == 0) {
340 		group->hide();
341 	}
342 
343 	item_duration = dur;
344 
345 	double end_pixel = trackview.editor().sample_to_pixel (sample_position + dur);
346 	double first_pixel = trackview.editor().sample_to_pixel (sample_position);
347 
348 	reset_width_dependent_items (end_pixel - first_pixel);
349 
350 	DurationChanged (dur, src); /* EMIT_SIGNAL */
351 	return true;
352 }
353 
354 /** @return duration of this item */
355 samplepos_t
get_duration() const356 TimeAxisViewItem::get_duration() const
357 {
358 	return item_duration;
359 }
360 
361 /**
362  * Set the maximum duration that this item can have.
363  *
364  * @param dur the new maximum duration
365  * @param src the identity of the object that initiated the change
366  */
367 void
set_max_duration(samplecnt_t dur,void * src)368 TimeAxisViewItem::set_max_duration(samplecnt_t dur, void* src)
369 {
370 	max_item_duration = dur;
371 	MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
372 }
373 
374 /** @return the maximum duration that this item may have */
375 samplecnt_t
get_max_duration() const376 TimeAxisViewItem::get_max_duration() const
377 {
378 	return max_item_duration;
379 }
380 
381 /**
382  * Set the minimum duration that this item may have.
383  *
384  * @param the minimum duration that this item may be set to
385  * @param src the identity of the object that initiated the change
386  */
387 void
set_min_duration(samplecnt_t dur,void * src)388 TimeAxisViewItem::set_min_duration(samplecnt_t dur, void* src)
389 {
390 	min_item_duration = dur;
391 	MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
392 }
393 
394 /** @return the minimum duration that this item mey have */
395 samplecnt_t
get_min_duration() const396 TimeAxisViewItem::get_min_duration() const
397 {
398 	return min_item_duration;
399 }
400 
401 /**
402  * Set whether this item is locked to its current position.
403  * Locked items cannot be moved until the item is unlocked again.
404  *
405  * @param yn true to lock this item to its current position
406  * @param src the identity of the object that initiated the change
407  */
408 void
set_position_locked(bool yn,void * src)409 TimeAxisViewItem::set_position_locked(bool yn, void* src)
410 {
411 	position_locked = yn;
412 	set_trim_handle_colors();
413 	PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
414 }
415 
416 /** @return true if this item is locked to its current position */
417 bool
get_position_locked() const418 TimeAxisViewItem::get_position_locked() const
419 {
420 	return position_locked;
421 }
422 
423 /**
424  * Set whether the maximum duration constraint is active.
425  *
426  * @param active set true to enforce the max duration constraint
427  * @param src the identity of the object that initiated the change
428  */
429 void
set_max_duration_active(bool active,void *)430 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
431 {
432 	max_duration_active = active;
433 }
434 
435 /** @return true if the maximum duration constraint is active */
436 bool
get_max_duration_active() const437 TimeAxisViewItem::get_max_duration_active() const
438 {
439 	return max_duration_active;
440 }
441 
442 /**
443  * Set whether the minimum duration constraint is active.
444  *
445  * @param active set true to enforce the min duration constraint
446  * @param src the identity of the object that initiated the change
447  */
448 
449 void
set_min_duration_active(bool active,void *)450 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
451 {
452 	min_duration_active = active;
453 }
454 
455 /** @return true if the maximum duration constraint is active */
456 bool
get_min_duration_active() const457 TimeAxisViewItem::get_min_duration_active() const
458 {
459 	return min_duration_active;
460 }
461 
462 /**
463  * Set the name of this item.
464  *
465  * @param new_name the new name of this item
466  * @param src the identity of the object that initiated the change
467  */
468 
469 void
set_item_name(std::string new_name,void * src)470 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
471 {
472 	if (new_name != item_name) {
473 		std::string temp_name = item_name;
474 		item_name = new_name;
475 		NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
476 	}
477 }
478 
479 /** @return the name of this item */
480 std::string
get_item_name() const481 TimeAxisViewItem::get_item_name() const
482 {
483 	return item_name;
484 }
485 
486 /**
487  * Set selection status.
488  *
489  * @param yn true if this item is currently selected
490  */
491 void
set_selected(bool yn)492 TimeAxisViewItem::set_selected(bool yn)
493 {
494 	if (_selected == yn) {
495 		return;
496 	}
497 
498 	Selectable::set_selected (yn);
499 	set_frame_color ();
500 	set_name_text_color ();
501 
502 	if (_selected && frame) {
503 		if (!selection_frame) {
504 			selection_frame = new ArdourCanvas::Rectangle (group);
505 			selection_frame->set_fill (false);
506 			selection_frame->set_outline_color (UIConfiguration::instance().color ("selected time axis frame"));
507 			selection_frame->set_ignore_events (true);
508 		}
509 		selection_frame->set (frame->get().shrink (1.0));
510 		selection_frame->show ();
511 	} else {
512 		if (selection_frame) {
513 			selection_frame->hide ();
514 		}
515 	}
516 }
517 
518 /** @return the TimeAxisView that this item is on */
519 TimeAxisView&
get_time_axis_view() const520 TimeAxisViewItem::get_time_axis_view () const
521 {
522 	return trackview;
523 }
524 
525 /**
526  * Set the displayed item text.
527  * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
528  *
529  * @param new_name the new name text to display
530  */
531 
532 void
set_name_text(const string & new_name)533 TimeAxisViewItem::set_name_text(const string& new_name)
534 {
535 	if (!name_text) {
536 		return;
537 	}
538 
539 	name_text_width = pixel_width (new_name, NAME_FONT) + 2;
540 	name_text->set (new_name);
541 	manage_name_text ();
542 	manage_name_highlight ();
543 }
544 
545 /**
546  * Set the height of this item.
547  *
548  * @param h new height
549  */
550 void
set_height(double height)551 TimeAxisViewItem::set_height (double height)
552 {
553 	_height = height;
554 
555 	manage_name_highlight ();
556 
557 	if (visibility & ShowNameText) {
558 		if (UIConfiguration::instance().get_show_name_highlight()) {
559 			name_text->set_y_position (height - NAME_Y_OFFSET);
560 		} else {
561 			name_text->set_y_position (NAME_Y_OFFSET);
562 		}
563 	}
564 
565 	if (frame) {
566 
567 		frame->set_y0 (0.0);
568 		frame->set_y1 (height);
569 
570 		if (frame_handle_start) {
571 			frame_handle_start->set_y1 (height);
572 			frame_handle_end->set_y1 (height);
573 		}
574 
575 		if (selection_frame) {
576 			selection_frame->set (frame->get().shrink (1.0));
577 		}
578 	}
579 }
580 
581 void
manage_name_highlight()582 TimeAxisViewItem::manage_name_highlight ()
583 {
584 	if (!name_highlight) {
585 		return;
586 	}
587 
588 	if (_height < NAME_HIGHLIGHT_THRESH) {
589 		high_enough_for_name = false;
590 	} else {
591 		high_enough_for_name = true;
592 	}
593 
594 	if (_width < 2.0) {
595 		wide_enough_for_name = false;
596 	} else {
597 		wide_enough_for_name = true;
598 	}
599 
600 	if (name_highlight && wide_enough_for_name && high_enough_for_name) {
601 
602 		name_highlight->show();
603 		// name_highlight->set_x_position (1.0);
604 		name_highlight->set (ArdourCanvas::Rect (0.0, (double) _height - NAME_HIGHLIGHT_SIZE,  _width - 2.0, _height));
605 
606 	} else {
607 		name_highlight->hide();
608 	}
609 
610 	manage_name_text ();
611 }
612 
613 void
set_color(uint32_t base_color)614 TimeAxisViewItem::set_color (uint32_t base_color)
615 {
616 	fill_color = base_color;
617 	set_colors ();
618 }
619 
620 ArdourCanvas::Item*
get_canvas_frame()621 TimeAxisViewItem::get_canvas_frame()
622 {
623 	return frame;
624 }
625 
626 ArdourCanvas::Item*
get_canvas_group()627 TimeAxisViewItem::get_canvas_group()
628 {
629 	return group;
630 }
631 
632 ArdourCanvas::Item*
get_name_highlight()633 TimeAxisViewItem::get_name_highlight()
634 {
635 	return name_highlight;
636 }
637 
638 /**
639  * Convenience method to set the various canvas item colors
640  */
641 void
set_colors()642 TimeAxisViewItem::set_colors()
643 {
644 	set_frame_color ();
645 
646 	if (name_highlight) {
647 		name_highlight->set_fill_color (fill_color);
648 	}
649 
650 	set_name_text_color ();
651 	set_trim_handle_colors();
652 }
653 
654 void
set_name_text_color()655 TimeAxisViewItem::set_name_text_color ()
656 {
657 	if (!name_text) {
658 		return;
659 	}
660 
661 
662 	uint32_t f;
663 
664 	if (UIConfiguration::instance().get_show_name_highlight()) {
665 		/* name text will always be on top of name highlight, which
666 		   will always use our fill color.
667 		*/
668 		f = fill_color;
669 	} else {
670 		/* name text will be on top of the item, whose color
671 		   may vary depending on various conditions.
672 		*/
673 		f = get_fill_color ();
674 	}
675 
676 	name_text->set_color (contrasting_text_color (f));
677 }
678 
679 Gtkmm2ext::Color
get_fill_color() const680 TimeAxisViewItem::get_fill_color () const
681 {
682 	const std::string mod_name = (_dragging ? "dragging region" : fill_color_name);
683 
684 	if (_selected) {
685 		return UIConfiguration::instance().color ("selected region base");
686 	} else if (_recregion) {
687 		return UIConfiguration::instance().color ("recording rect");
688 	} else if ((!UIConfiguration::instance().get_show_name_highlight() || high_enough_for_name) &&
689 	           !UIConfiguration::instance().get_color_regions_using_track_color()) {
690 		return UIConfiguration::instance().color_mod (fill_color_name, mod_name);
691 	}
692 	return UIConfiguration::instance().color_mod (fill_color, mod_name);
693 }
694 
695 /**
696  * Sets the frame color depending on whether this item is selected
697  */
698 void
set_frame_color()699 TimeAxisViewItem::set_frame_color()
700 {
701 	if (!frame) {
702 		return;
703 	}
704 
705 	frame->set_fill_color (get_fill_color());
706 	set_frame_gradient ();
707 
708 	if (!_recregion) {
709 		frame->set_outline_color (UIConfiguration::instance().color ("time axis frame"));
710 	}
711 }
712 
713 void
set_frame_gradient()714 TimeAxisViewItem::set_frame_gradient ()
715 {
716 	if (UIConfiguration::instance().get_timeline_item_gradient_depth() == 0.0) {
717 		frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
718 		return;
719 	}
720 
721 	ArdourCanvas::Fill::StopList stops;
722 	double r, g, b, a;
723 	double h, s, v;
724 	Color f (get_fill_color());
725 
726 	/* need to get alpha value */
727 	color_to_rgba (f, r, g, b, a);
728 
729 	stops.push_back (std::make_pair (0.0, f));
730 
731 	/* now a darker version */
732 
733 	color_to_hsv (f, h, s, v);
734 
735 	v = min (1.0, v * (1.0 - UIConfiguration::instance().get_timeline_item_gradient_depth()));
736 
737 	Color darker = hsva_to_color (h, s, v, a);
738 	stops.push_back (std::make_pair (1.0, darker));
739 
740 	frame->set_gradient (stops, true);
741 }
742 
743 /**
744  * Set the colors of the start and end trim handle depending on object state
745  */
746 void
set_trim_handle_colors()747 TimeAxisViewItem::set_trim_handle_colors()
748 {
749 #if 1
750 	/* Leave them transparent for now */
751 	if (frame_handle_start) {
752 		frame_handle_start->set_fill_color (0x00000000);
753 		frame_handle_end->set_fill_color (0x00000000);
754 	}
755 #else
756 	if (frame_handle_start) {
757 		if (position_locked) {
758 			frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
759 			frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandleLocked());
760 		} else {
761 			frame_handle_start->set_fill_color (UIConfiguration::instance().get_TrimHandle());
762 			frame_handle_end->set_fill_color (UIConfiguration::instance().get_TrimHandle());
763 		}
764 	}
765 #endif
766 }
767 
768 bool
frame_handle_crossing(GdkEvent * ev,ArdourCanvas::Rectangle * item)769 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
770 {
771 	switch (ev->type) {
772 	case GDK_LEAVE_NOTIFY:
773 		/* always hide the handle whenever we leave, no matter what mode */
774 		item->set_fill (false);
775 		break;
776 	case GDK_ENTER_NOTIFY:
777 		if (trackview.editor().effective_mouse_mode() == Editing::MouseObject) {
778 			/* Never set this to be visible in other modes.  Note, however,
779 			   that we do need to undo visibility (LEAVE_NOTIFY case above) no
780 			   matter what the mode is. */
781 			item->set_fill (true);
782 		}
783 		break;
784 	default:
785 		break;
786 	}
787 	return false;
788 }
789 
790 /** @return the samples per pixel */
791 double
get_samples_per_pixel() const792 TimeAxisViewItem::get_samples_per_pixel () const
793 {
794 	return samples_per_pixel;
795 }
796 
797 /** Set the samples per pixel 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.
800  *
801  *  @param fpp the new samples per pixel
802  */
803 void
set_samples_per_pixel(double fpp)804 TimeAxisViewItem::set_samples_per_pixel (double fpp)
805 {
806 	samples_per_pixel = fpp;
807 	set_position (this->get_position(), this);
808 
809 	double end_pixel = trackview.editor().sample_to_pixel (sample_position + get_duration());
810 	double first_pixel = trackview.editor().sample_to_pixel (sample_position);
811 
812 	reset_width_dependent_items (end_pixel - first_pixel);
813 }
814 
815 void
reset_width_dependent_items(double pixel_width)816 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
817 {
818 	_width = pixel_width;
819 
820 	manage_name_highlight ();
821 	manage_name_text ();
822 
823 	if (pixel_width < 2.0) {
824 
825 		if (frame) {
826 			frame->set_outline (false);
827 			frame->set_x1 (std::max(1.0, pixel_width));
828 		}
829 
830 		if (frame_handle_start) {
831 			frame_handle_start->hide();
832 			frame_handle_end->hide();
833 		}
834 
835 	} else {
836 		if (frame) {
837 			frame->set_outline (true);
838 			/* Note: x0 is always zero - the position is defined by
839 			 * the position of the group, not the frame.
840 			 */
841 			frame->set_x1 (pixel_width);
842 
843 			if (selection_frame) {
844 				selection_frame->set (frame->get().shrink (1.0));
845 			}
846 		}
847 
848 		if (frame_handle_start) {
849 			if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
850 				/*
851 				 * there's less than GRAB_HANDLE_WIDTH of the region between
852 				 * the right-hand end of frame_handle_start and the left-hand
853 				 * end of frame_handle_end, so disable the handles
854 				 */
855 
856 				frame_handle_start->hide();
857 				frame_handle_end->hide();
858 			} else {
859 				frame_handle_start->show();
860 				frame_handle_end->set_x0 (pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
861 				frame_handle_end->set_x1 (pixel_width);
862 				frame_handle_end->show();
863 
864 				frame_handle_start->raise_to_top ();
865 				frame_handle_end->raise_to_top ();
866 			}
867 		}
868 	}
869 }
870 
871 void
manage_name_text()872 TimeAxisViewItem::manage_name_text ()
873 {
874 	int visible_name_width;
875 
876 	if (!name_text) {
877 		return;
878 	}
879 
880 	if (!(visibility & ShowNameText) || (!wide_enough_for_name || !high_enough_for_name)) {
881 		name_text->hide ();
882 		return;
883 	}
884 
885 	if (name_text->text().empty()) {
886 		name_text->hide ();
887 	}
888 
889 	visible_name_width = name_text_width;
890 
891 	if (visible_name_width > _width - NAME_X_OFFSET) {
892 		visible_name_width = _width - NAME_X_OFFSET;
893 	}
894 
895 	if (visible_name_width < 1) {
896 		name_text->hide ();
897 	} else {
898 		name_text->clamp_width (visible_name_width);
899 		name_text->show ();
900 	}
901 }
902 
903 /**
904  * Callback used to remove this time axis item during the gtk idle loop.
905  * This is used to avoid deleting the obejct while inside the remove_this_item
906  * method.
907  *
908  * @param item the TimeAxisViewItem to remove.
909  * @param src the identity of the object that initiated the change.
910  */
911 gint
idle_remove_this_item(TimeAxisViewItem * item,void * src)912 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
913 {
914 	item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
915 	delete item;
916 	item = 0;
917 	return false;
918 }
919 
920 void
set_y(double y)921 TimeAxisViewItem::set_y (double y)
922 {
923 	group->set_y_position (y);
924 }
925 
926 void
parameter_changed(string p)927 TimeAxisViewItem::parameter_changed (string p)
928 {
929 	if (p == "color-regions-using-track-color") {
930 		set_colors ();
931 	} else if (p == "timeline-item-gradient-depth") {
932 		set_frame_gradient ();
933 	}
934 }
935 
936 void
drag_start()937 TimeAxisViewItem::drag_start ()
938 {
939 	_dragging = true;
940 	set_frame_color ();
941 }
942 
943 void
drag_end()944 TimeAxisViewItem::drag_end ()
945 {
946 	_dragging = false;
947 	set_frame_color ();
948 }
949