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