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