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