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