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