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