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