use ArdourCanvas::TimeRectangle for regions, notes, markers
[ardour.git] / gtk2_ardour / time_axis_view_item.cc
1 /*
2     Copyright (C) 2003 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <utility>
21
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
24
25 #include "ardour/types.h"
26 #include "ardour/ardour.h"
27
28 #include "gtkmm2ext/utils.h"
29 #include "gtkmm2ext/gui_thread.h"
30
31 #include "canvas/container.h"
32 #include "canvas/rectangle.h"
33 #include "canvas/debug.h"
34 #include "canvas/text.h"
35 #include "canvas/utils.h"
36
37 #include "ardour/profile.h"
38
39 #include "ardour_ui.h"
40 /*
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
44  */
45
46 #include "public_editor.h"
47 #include "time_axis_view_item.h"
48 #include "time_axis_view.h"
49 #include "utils.h"
50 #include "rgb_macros.h"
51
52 #include "i18n.h"
53
54 using namespace std;
55 using namespace Editing;
56 using namespace Glib;
57 using namespace PBD;
58 using namespace ARDOUR;
59 using namespace ARDOUR_UI_UTILS;
60 using namespace Gtkmm2ext;
61
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;
66 const double TimeAxisViewItem::RIGHT_EDGE_SHIFT = 0.0; // TODO remove, fixed in 3.5-3406-g90872c2, but may need further work.
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
74 TimeAxisViewItem::set_constant_heights ()
75 {
76         NAME_FONT = Pango::FontDescription (ARDOUR_UI::config()->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 (Config->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  */
122 TimeAxisViewItem::TimeAxisViewItem(
123         const string & it_name, ArdourCanvas::Item& parent, TimeAxisView& tv, double spu, uint32_t base_color,
124         framepos_t start, framecnt_t duration, bool recording, bool automation, Visibility vis
125         )
126         : trackview (tv)
127         , frame_position (-1)
128         , item_name (it_name)
129         , _height (1.0)
130         , _recregion (recording)
131         , _automation (automation)
132         , _dragging (false)
133         , _width (0.0)
134 {
135         init (&parent, spu, base_color, start, duration, vis, true, true);
136 }
137
138 TimeAxisViewItem::TimeAxisViewItem (const TimeAxisViewItem& other)
139         : trackable (other)
140         , Selectable (other)
141         , PBD::ScopedConnectionList()
142         , trackview (other.trackview)
143         , frame_position (-1)
144         , item_name (other.item_name)
145         , _height (1.0)
146         , _recregion (other._recregion)
147         , _automation (other._automation)
148         , _dragging (other._dragging)
149         , _width (0.0)
150 {
151         /* share the other's parent, but still create a new group */
152
153         ArdourCanvas::Item* parent = other.group->parent();
154         
155         _selected = other._selected;
156         
157         init (parent, other.samples_per_pixel, other.fill_color, other.frame_position,
158               other.item_duration, other.visibility, other.wide_enough_for_name, other.high_enough_for_name);
159 }
160
161 void
162 TimeAxisViewItem::init (ArdourCanvas::Item* parent, double fpp, uint32_t base_color, 
163                         framepos_t start, framepos_t duration, Visibility vis, 
164                         bool wide, bool high)
165 {
166         group = new ArdourCanvas::Container (parent);
167         CANVAS_DEBUG_NAME (group, string_compose ("TAVI group for %1", get_item_name()));
168         group->Event.connect (sigc::mem_fun (*this, &TimeAxisViewItem::canvas_group_event));
169
170         fill_color = base_color;
171         samples_per_pixel = fpp;
172         frame_position = start;
173         item_duration = duration;
174         name_connected = false;
175         position_locked = false;
176         max_item_duration = ARDOUR::max_framepos;
177         min_item_duration = 0;
178         show_vestigial = true;
179         visibility = vis;
180         _sensitive = true;
181         name_text_width = 0;
182         last_item_width = 0;
183         wide_enough_for_name = wide;
184         high_enough_for_name = high;
185         rect_visible = true;
186
187         if (duration == 0) {
188                 warning << "Time Axis Item Duration == 0" << endl;
189         }
190
191         vestigial_frame = new ArdourCanvas::TimeRectangle (group, ArdourCanvas::Rect (0.0, 1.0, 2.0, trackview.current_height()));
192         CANVAS_DEBUG_NAME (vestigial_frame, string_compose ("vestigial frame for %1", get_item_name()));
193         vestigial_frame->hide ();
194         vestigial_frame->set_outline_color (ARDOUR_UI::config()->get_VestigialFrame());
195         vestigial_frame->set_fill_color (ARDOUR_UI::config()->get_VestigialFrame());
196
197         if (visibility & ShowFrame) {
198                 frame = new ArdourCanvas::TimeRectangle (group, 
199                                                      ArdourCanvas::Rect (0.0, 0.0, 
200                                                                          trackview.editor().sample_to_pixel(duration) + RIGHT_EDGE_SHIFT, 
201                                                                          trackview.current_height() - 1.0));
202
203                 CANVAS_DEBUG_NAME (frame, string_compose ("frame for %1", get_item_name()));
204
205                 if (Config->get_show_name_highlight()) {
206                         frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
207                 } else {
208                         frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM));
209                 }
210
211                 if (_recregion) {
212                         frame->set_outline_color (ARDOUR_UI::config()->get_RecordingRect());
213                 } else {
214                         frame->set_outline_color (ARDOUR_UI::config()->get_TimeAxisFrame());
215                 }
216
217         } else {
218
219                 frame = 0;
220         }
221         
222         if (Config->get_show_name_highlight() && (visibility & ShowNameHighlight)) {
223
224                 double width;
225                 double start;
226
227                 if (visibility & FullWidthNameHighlight) {
228                         start = 0.0;
229                         width = trackview.editor().sample_to_pixel(item_duration) + RIGHT_EDGE_SHIFT;
230                 } else {
231                         start = 1.0;
232                         width = trackview.editor().sample_to_pixel(item_duration) - 2.0 + RIGHT_EDGE_SHIFT;
233                 }
234
235                 name_highlight = new ArdourCanvas::Rectangle (group, 
236                                                               ArdourCanvas::Rect (start, 
237                                                                                   trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE, 
238                                                                                   width - 2.0 + RIGHT_EDGE_SHIFT,
239                                                                                   trackview.current_height() - 1.0));
240                 CANVAS_DEBUG_NAME (name_highlight, string_compose ("name highlight for %1", get_item_name()));
241                 name_highlight->set_data ("timeaxisviewitem", this);
242                 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
243                 name_highlight->set_outline_color (RGBA_TO_UINT (0,0,0,255));
244
245         } else {
246                 name_highlight = 0;
247         }
248
249         if (visibility & ShowNameText) {
250                 name_text = new ArdourCanvas::Text (group);
251                 CANVAS_DEBUG_NAME (name_text, string_compose ("name text for %1", get_item_name()));
252                 if (Config->get_show_name_highlight()) {
253                         name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, trackview.current_height() - NAME_Y_OFFSET));
254                 } else {
255                         name_text->set_position (ArdourCanvas::Duple (NAME_X_OFFSET, NAME_Y_OFFSET));
256                 }
257                 name_text->set_font_description (NAME_FONT);
258                 name_text->set_ignore_events (true);
259         } else {
260                 name_text = 0;
261         }
262
263         /* create our grab handles used for trimming/duration etc */
264         if (!_recregion && !_automation) {
265                 double top   = TimeAxisViewItem::GRAB_HANDLE_TOP;
266                 double width = TimeAxisViewItem::GRAB_HANDLE_WIDTH;
267
268                 frame_handle_start = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
269                 CANVAS_DEBUG_NAME (frame_handle_start, "TAVI frame handle start");
270                 frame_handle_start->set_outline (false);
271                 frame_handle_start->set_fill (false);
272                 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_start));
273
274                 frame_handle_end = new ArdourCanvas::Rectangle (group, ArdourCanvas::Rect (0.0, top, width, trackview.current_height()));
275                 CANVAS_DEBUG_NAME (frame_handle_end, "TAVI frame handle end");
276                 frame_handle_end->set_outline (false);
277                 frame_handle_end->set_fill (false);
278                 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (*this, &TimeAxisViewItem::frame_handle_crossing), frame_handle_end));
279         } else {
280                 frame_handle_start = frame_handle_end = 0;
281         }
282
283         set_color (base_color);
284
285         set_duration (item_duration, this);
286         set_position (start, this);
287
288         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&TimeAxisViewItem::parameter_changed, this, _1), gui_context ());
289         ARDOUR_UI::config()->ParameterChanged.connect (sigc::mem_fun (*this, &TimeAxisViewItem::parameter_changed));
290 }
291
292 TimeAxisViewItem::~TimeAxisViewItem()
293 {
294         delete group;
295 }
296
297 bool
298 TimeAxisViewItem::canvas_group_event (GdkEvent* /*ev*/)
299 {
300         return false;
301 }
302
303 void
304 TimeAxisViewItem::hide_rect ()
305 {
306         rect_visible = false;
307         set_frame_color ();
308
309         if (name_highlight) {
310                 name_highlight->set_outline_what (ArdourCanvas::Rectangle::What (0));
311                 name_highlight->set_fill_color (UINT_RGBA_CHANGE_A (fill_color, 64));
312         }
313 }
314
315 void
316 TimeAxisViewItem::show_rect ()
317 {
318         rect_visible = true;
319         set_frame_color ();
320
321         if (name_highlight) {
322                 name_highlight->set_outline_what (ArdourCanvas::Rectangle::TOP);
323                 name_highlight->set_fill_color (fill_color);
324         }
325 }
326
327 /**
328  * Set the position of this item on the timeline.
329  *
330  * @param pos the new position
331  * @param src the identity of the object that initiated the change
332  * @return true on success
333  */
334
335 bool
336 TimeAxisViewItem::set_position(framepos_t pos, void* src, double* delta)
337 {
338         if (position_locked) {
339                 return false;
340         }
341
342         frame_position = pos;
343
344         double new_unit_pos = trackview.editor().sample_to_pixel (pos);
345
346         if (delta) {
347                 (*delta) = new_unit_pos - group->position().x;
348                 if (*delta == 0.0) {
349                         return true;
350                 }
351         } else {
352                 if (new_unit_pos == group->position().x) {
353                         return true;
354                 }
355         }
356
357         group->set_x_position (new_unit_pos);
358
359         PositionChanged (frame_position, src); /* EMIT_SIGNAL */
360
361         return true;
362 }
363
364 /** @return position of this item on the timeline */
365 framepos_t
366 TimeAxisViewItem::get_position() const
367 {
368         return frame_position;
369 }
370
371 /**
372  * Set the duration of this item.
373  *
374  * @param dur the new duration of this item
375  * @param src the identity of the object that initiated the change
376  * @return true on success
377  */
378
379 bool
380 TimeAxisViewItem::set_duration (framecnt_t dur, void* src)
381 {
382         if ((dur > max_item_duration) || (dur < min_item_duration)) {
383                 warning << string_compose (
384                                 P_("new duration %1 frame is out of bounds for %2", "new duration of %1 frames is out of bounds for %2", dur),
385                                 get_item_name(), dur)
386                         << endmsg;
387                 return false;
388         }
389
390         if (dur == 0) {
391                 group->hide();
392         }
393
394         item_duration = dur;
395
396         reset_width_dependent_items (trackview.editor().sample_to_pixel (dur));
397
398         DurationChanged (dur, src); /* EMIT_SIGNAL */
399         return true;
400 }
401
402 /** @return duration of this item */
403 framepos_t
404 TimeAxisViewItem::get_duration() const
405 {
406         return item_duration;
407 }
408
409 /**
410  * Set the maximum duration that this item can have.
411  *
412  * @param dur the new maximum duration
413  * @param src the identity of the object that initiated the change
414  */
415 void
416 TimeAxisViewItem::set_max_duration(framecnt_t dur, void* src)
417 {
418         max_item_duration = dur;
419         MaxDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
420 }
421
422 /** @return the maximum duration that this item may have */
423 framecnt_t
424 TimeAxisViewItem::get_max_duration() const
425 {
426         return max_item_duration;
427 }
428
429 /**
430  * Set the minimum duration that this item may have.
431  *
432  * @param the minimum duration that this item may be set to
433  * @param src the identity of the object that initiated the change
434  */
435 void
436 TimeAxisViewItem::set_min_duration(framecnt_t dur, void* src)
437 {
438         min_item_duration = dur;
439         MinDurationChanged(max_item_duration, src); /* EMIT_SIGNAL */
440 }
441
442 /** @return the minimum duration that this item mey have */
443 framecnt_t
444 TimeAxisViewItem::get_min_duration() const
445 {
446         return min_item_duration;
447 }
448
449 /**
450  * Set whether this item is locked to its current position.
451  * Locked items cannot be moved until the item is unlocked again.
452  *
453  * @param yn true to lock this item to its current position
454  * @param src the identity of the object that initiated the change
455  */
456 void
457 TimeAxisViewItem::set_position_locked(bool yn, void* src)
458 {
459         position_locked = yn;
460         set_trim_handle_colors();
461         PositionLockChanged (position_locked, src); /* EMIT_SIGNAL */
462 }
463
464 /** @return true if this item is locked to its current position */
465 bool
466 TimeAxisViewItem::get_position_locked() const
467 {
468         return position_locked;
469 }
470
471 /**
472  * Set whether the maximum duration constraint is active.
473  *
474  * @param active set true to enforce the max duration constraint
475  * @param src the identity of the object that initiated the change
476  */
477 void
478 TimeAxisViewItem::set_max_duration_active (bool active, void* /*src*/)
479 {
480         max_duration_active = active;
481 }
482
483 /** @return true if the maximum duration constraint is active */
484 bool
485 TimeAxisViewItem::get_max_duration_active() const
486 {
487         return max_duration_active;
488 }
489
490 /**
491  * Set whether the minimum duration constraint is active.
492  *
493  * @param active set true to enforce the min duration constraint
494  * @param src the identity of the object that initiated the change
495  */
496
497 void
498 TimeAxisViewItem::set_min_duration_active (bool active, void* /*src*/)
499 {
500         min_duration_active = active;
501 }
502
503 /** @return true if the maximum duration constraint is active */
504 bool
505 TimeAxisViewItem::get_min_duration_active() const
506 {
507         return min_duration_active;
508 }
509
510 /**
511  * Set the name of this item.
512  *
513  * @param new_name the new name of this item
514  * @param src the identity of the object that initiated the change
515  */
516
517 void
518 TimeAxisViewItem::set_item_name(std::string new_name, void* src)
519 {
520         if (new_name != item_name) {
521                 std::string temp_name = item_name;
522                 item_name = new_name;
523                 NameChanged (item_name, temp_name, src); /* EMIT_SIGNAL */
524         }
525 }
526
527 /** @return the name of this item */
528 std::string
529 TimeAxisViewItem::get_item_name() const
530 {
531         return item_name;
532 }
533
534 /**
535  * Set selection status.
536  *
537  * @param yn true if this item is currently selected
538  */
539 void
540 TimeAxisViewItem::set_selected(bool yn)
541 {
542         if (_selected != yn) {
543                 Selectable::set_selected (yn);
544                 set_frame_color ();
545                 set_name_text_color ();
546
547                 if (frame) {
548                         if (!Config->get_show_name_highlight() && yn) {
549                                 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM|ArdourCanvas::Rectangle::TOP));
550                                 frame->set_y0 (1.0);
551                                 frame->set_y1 (_height - 1.0);
552                         } else {
553                                 if (Config->get_show_name_highlight()) {
554                                         frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
555                                 } else {
556                                         frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM));
557                                 }
558                                 frame->set_y0 (0.0);
559                                 frame->set_y1 (_height);
560                         }
561                 }
562         }
563 }
564
565 /** @return the TimeAxisView that this item is on */
566 TimeAxisView&
567 TimeAxisViewItem::get_time_axis_view () const
568 {
569         return trackview;
570 }
571
572 /**
573  * Set the displayed item text.
574  * This item is the visual text name displayed on the canvas item, this can be different to the name of the item.
575  *
576  * @param new_name the new name text to display
577  */
578
579 void
580 TimeAxisViewItem::set_name_text(const string& new_name)
581 {
582         if (!name_text) {
583                 return;
584         }
585
586         name_text_width = pixel_width (new_name, NAME_FONT) + 2;
587         name_text->set (new_name);
588
589 }
590
591 /**
592  * Set the height of this item.
593  *
594  * @param h new height
595  */
596 void
597 TimeAxisViewItem::set_height (double height)
598 {
599         _height = height;
600
601         manage_name_highlight ();
602
603         if (visibility & ShowNameText) {
604                 if (Config->get_show_name_highlight()) {
605                         name_text->set_y_position (height - NAME_Y_OFFSET); 
606                 } else {
607                         name_text->set_y_position (NAME_Y_OFFSET); 
608                 }
609         }
610
611         if (frame) {
612                 if (!Config->get_show_name_highlight() && _selected) {
613                         frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM|ArdourCanvas::Rectangle::TOP));
614                         frame->set_y0 (1.0);
615                         frame->set_y1 (_height - 1.0);
616                 } else {
617                         if (Config->get_show_name_highlight()) {
618                                 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
619                         } else {
620                                 frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT|ArdourCanvas::Rectangle::BOTTOM));
621                         }
622                         frame->set_y0 (0.0);
623                         frame->set_y1 (_height);
624                 }
625
626                 if (frame_handle_start) {
627                         frame_handle_start->set_y1 (height);
628                         frame_handle_end->set_y1 (height);
629                 }
630         }
631
632         vestigial_frame->set_y1 (height - 1.0);
633
634         set_colors ();
635 }
636
637 void
638 TimeAxisViewItem::manage_name_highlight ()
639 {
640         if (!name_highlight) {
641                 return;
642         }
643
644         if (_height < NAME_HIGHLIGHT_THRESH) {
645                 high_enough_for_name = false;
646         } else {
647                 high_enough_for_name = true;
648         }
649
650         if (_width < 2.0) {
651                 wide_enough_for_name = false;
652         } else {
653                 wide_enough_for_name = true;
654         }
655
656         if (name_highlight && wide_enough_for_name && high_enough_for_name) {
657
658                 name_highlight->show();
659                 name_highlight->set (ArdourCanvas::Rect (0.0, (double) _height - NAME_HIGHLIGHT_SIZE,  _width+RIGHT_EDGE_SHIFT, (double) _height - 1.0));
660                         
661         } else {
662                 name_highlight->hide();
663         }
664
665         manage_name_text ();
666 }
667
668 void
669 TimeAxisViewItem::set_color (uint32_t base_color)
670 {
671         fill_color = base_color;
672         set_colors ();
673 }
674
675 ArdourCanvas::Item*
676 TimeAxisViewItem::get_canvas_frame()
677 {
678         return frame;
679 }
680
681 ArdourCanvas::Item*
682 TimeAxisViewItem::get_canvas_group()
683 {
684         return group;
685 }
686
687 ArdourCanvas::Item*
688 TimeAxisViewItem::get_name_highlight()
689 {
690         return name_highlight;
691 }
692
693 /**
694  * Convenience method to set the various canvas item colors
695  */
696 void
697 TimeAxisViewItem::set_colors()
698 {
699         set_frame_color ();
700
701         if (name_highlight) {
702                 name_highlight->set_fill_color (fill_color);
703         }
704
705         set_name_text_color ();
706         set_trim_handle_colors();
707 }
708
709 void
710 TimeAxisViewItem::set_name_text_color ()
711 {
712         if (!name_text) {
713                 return;
714         }
715         
716
717         uint32_t f;
718         
719         if (Config->get_show_name_highlight()) {
720                 /* name text will always be on top of name highlight, which
721                    will always use our fill color.
722                 */
723                 f = fill_color;
724         } else {
725                 /* name text will be on top of the item, whose color
726                    may vary depending on various conditions.
727                 */
728                 f = get_fill_color ();
729         }
730
731         name_text->set_color (ArdourCanvas::contrasting_text_color (f));
732 }
733
734 uint32_t
735 TimeAxisViewItem::fill_opacity () const
736 {
737         if (!rect_visible) {
738                 /* if the frame/rect is marked as invisible, then the
739                  * fill should be transparent. simplest: set
740                  
741                  * alpha/opacity to zero.
742                  */
743                 return 0;
744         }
745
746         if (_dragging) {
747                 return 130;
748         }
749
750         uint32_t col = ARDOUR_UI::config()->get_FrameBase();
751         return UINT_RGBA_A (col);
752 }
753
754 uint32_t
755 TimeAxisViewItem::get_fill_color () const
756 {
757         uint32_t f;
758         uint32_t o;
759
760         o = fill_opacity ();
761
762         if (_selected) {
763
764                 f = ARDOUR_UI::config()->get_SelectedFrameBase();
765
766                 if (o == 0) {
767                         /* some condition of this item has set fill opacity to
768                          * zero, but it has been selected, so use a mid-way
769                          * alpha value to make it reasonably visible.
770                          */
771                         o = 130;
772                 }
773                 
774         } else {
775
776                 if (_recregion) {
777                         f = ARDOUR_UI::config()->get_RecordingRect();
778                 } else {
779                         if ((!Config->get_show_name_highlight() || high_enough_for_name) && !ARDOUR_UI::config()->get_color_regions_using_track_color()) {
780                                 f = ARDOUR_UI::config()->get_FrameBase();
781                         } else {
782                                 f = fill_color;
783                         }
784                 }
785         }
786
787         return UINT_RGBA_CHANGE_A (f, o);
788 }
789
790 /**
791  * Sets the frame color depending on whether this item is selected
792  */
793 void
794 TimeAxisViewItem::set_frame_color()
795 {
796         if (!frame) {
797                 return;
798         }
799
800         frame->set_fill_color (get_fill_color());
801         set_frame_gradient ();
802
803         if (!_recregion) {
804                 uint32_t f;
805
806                 if (_selected) {
807                         f = ARDOUR_UI::config()->get_SelectedTimeAxisFrame();
808                 } else {
809                         f = ARDOUR_UI::config()->get_TimeAxisFrame();
810                 }
811
812                 if (!rect_visible) {
813                         /* make the frame outline be visible but rather transparent */
814                         f = UINT_RGBA_CHANGE_A (f, 64);
815                 }
816
817                 frame->set_outline_color (f);
818         }
819 }
820
821 void
822 TimeAxisViewItem::set_frame_gradient ()
823 {
824         if (ARDOUR_UI::config()->get_timeline_item_gradient_depth() == 0.0) {
825                 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
826                 return;
827         }
828                 
829         ArdourCanvas::Fill::StopList stops;
830         double r, g, b, a;
831         double h, s, v;
832         ArdourCanvas::Color f (get_fill_color());
833
834         /* need to get alpha value */
835         ArdourCanvas::color_to_rgba (f, r, g, b, a);
836         
837         stops.push_back (std::make_pair (0.0, f));
838         
839         /* now a darker version */
840         
841         ArdourCanvas::color_to_hsv (f, h, s, v);
842
843         v = min (1.0, v * (1.0 - ARDOUR_UI::config()->get_timeline_item_gradient_depth()));
844         
845         ArdourCanvas::Color darker = ArdourCanvas::hsv_to_color (h, s, v, a);
846         stops.push_back (std::make_pair (1.0, darker));
847         
848         frame->set_gradient (stops, true);
849 }
850
851 /**
852  * Set the colors of the start and end trim handle depending on object state
853  */
854 void
855 TimeAxisViewItem::set_trim_handle_colors()
856 {
857 #if 1
858         /* Leave them transparent for now */
859         if (frame_handle_start) {
860                 frame_handle_start->set_fill_color (0x00000000);
861                 frame_handle_end->set_fill_color (0x00000000);
862         }
863 #else
864         if (frame_handle_start) {
865                 if (position_locked) {
866                         frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
867                         frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
868                 } else {
869                         frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
870                         frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
871                 }
872         }
873 #endif
874 }
875
876 bool
877 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
878 {
879         switch (ev->type) {
880         case GDK_LEAVE_NOTIFY:
881                 /* always hide the handle whenever we leave, no matter what mode */
882                 item->set_fill (false);
883                 break;
884         case GDK_ENTER_NOTIFY:
885                 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject &&
886                     !trackview.editor().internal_editing()) {
887                         /* never set this to be visible in internal
888                            edit mode. Note, however, that we do need to
889                            undo visibility (LEAVE_NOTIFY case above) no
890                            matter what the mode is.
891                         */
892                         item->set_fill (true);
893                 }
894                 break;
895         default:
896                 break;
897         }
898         return false;
899 }
900
901 /** @return the frames per pixel */
902 double
903 TimeAxisViewItem::get_samples_per_pixel () const
904 {
905         return samples_per_pixel;
906 }
907
908 /** Set the frames per pixel of this item.
909  *  This item is used to determine the relative visual size and position of this item
910  *  based upon its duration and start value.
911  *
912  *  @param fpp the new frames per pixel
913  */
914 void
915 TimeAxisViewItem::set_samples_per_pixel (double fpp)
916 {
917         samples_per_pixel = fpp;
918         set_position (this->get_position(), this);
919         reset_width_dependent_items ((double) get_duration() / samples_per_pixel);
920 }
921
922 void
923 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
924 {
925         _width = pixel_width;
926
927         manage_name_highlight ();
928         manage_name_text ();
929
930         if (pixel_width < 2.0) {
931
932                 if (show_vestigial) {
933                         vestigial_frame->show();
934                 }
935
936                 if (frame) {
937                         frame->hide();
938                 }
939
940                 if (frame_handle_start) {
941                         frame_handle_start->hide();
942                         frame_handle_end->hide();
943                 }
944
945         } else {
946                 vestigial_frame->hide();
947
948                 if (frame) {
949                         frame->show();
950                         frame->set_x1 (pixel_width + RIGHT_EDGE_SHIFT);
951                 }
952
953                 if (frame_handle_start) {
954                         if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
955                                 /*
956                                  * there's less than GRAB_HANDLE_WIDTH of the region between 
957                                  * the right-hand end of frame_handle_start and the left-hand
958                                  * end of frame_handle_end, so disable the handles
959                                  */
960
961                                 frame_handle_start->hide();
962                                 frame_handle_end->hide();
963                         } else {
964                                 frame_handle_start->show();
965                                 frame_handle_end->set_x0 (pixel_width + RIGHT_EDGE_SHIFT - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
966                                 frame_handle_end->set_x1 (pixel_width + RIGHT_EDGE_SHIFT);
967                                 frame_handle_end->show();
968                         }
969                 }
970         }
971 }
972
973 void
974 TimeAxisViewItem::manage_name_text ()
975 {
976         int visible_name_width;
977
978         if (!name_text) {
979                 return;
980         }
981
982         if (!wide_enough_for_name || !high_enough_for_name) {
983                 name_text->hide ();
984                 return;
985         }
986                 
987         if (name_text->text().empty()) {
988                 name_text->hide ();
989         }
990
991         visible_name_width = name_text_width;
992
993         if (visible_name_width > _width - NAME_X_OFFSET) {
994                 visible_name_width = _width - NAME_X_OFFSET;
995         }
996
997         if (visible_name_width < 1) {
998                 name_text->hide ();
999         } else {
1000                 name_text->clamp_width (visible_name_width);
1001                 name_text->show ();
1002         }
1003 }
1004
1005 /**
1006  * Callback used to remove this time axis item during the gtk idle loop.
1007  * This is used to avoid deleting the obejct while inside the remove_this_item
1008  * method.
1009  *
1010  * @param item the TimeAxisViewItem to remove.
1011  * @param src the identity of the object that initiated the change.
1012  */
1013 gint
1014 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
1015 {
1016         item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
1017         delete item;
1018         item = 0;
1019         return false;
1020 }
1021
1022 void
1023 TimeAxisViewItem::set_y (double y)
1024 {
1025         group->set_y_position (y);
1026 }
1027
1028 void
1029 TimeAxisViewItem::parameter_changed (string p)
1030 {
1031         if (p == "color-regions-using-track-color") {
1032                 set_colors ();
1033         } else if (p == "timeline-item-gradient-depth") {
1034                 set_frame_gradient ();
1035         }
1036 }
1037
1038 void
1039 TimeAxisViewItem::drag_start ()
1040 {
1041         _dragging = true;
1042         set_frame_color ();
1043 }
1044
1045 void
1046 TimeAxisViewItem::drag_end ()
1047 {
1048         _dragging = false;
1049         set_frame_color ();
1050 }