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