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