Remove internal edit mode and add "content" tool.
[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 (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 = "region 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         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), 
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()->color ("recording rect"));
206                 } else {
207                         frame->set_outline_color (ARDOUR_UI::config()->color ("time axis frame"));
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);
218                 } else {
219                         width = trackview.editor().sample_to_pixel(item_duration) - 2.0;
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,
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()->color ("selected time axis frame"));
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, (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 translucent.parent.
725                 */
726                 return 64;
727         }
728
729         if (_dragging) {
730                 return 130;
731         }
732
733         uint32_t col = ARDOUR_UI::config()->color_mod (_fill_color_name, _fill_color_name);
734         return UINT_RGBA_A (col);
735 }
736
737 uint32_t
738 TimeAxisViewItem::get_fill_color () const
739 {
740         uint32_t f;
741         uint32_t o = fill_opacity ();
742
743         if (_selected) {
744
745                 f = ARDOUR_UI::config()->color ("selected region base");
746
747                 if (o == 0) {
748                         /* some condition of this item has set fill opacity to
749                          * zero, but it has been selected, so use a mid-way
750                          * alpha value to make it reasonably visible.
751                          */
752                         o = 130;
753                 }
754                 
755         } else {
756
757                 if (_recregion) {
758                         f = ARDOUR_UI::config()->color ("recording rect");
759                 } else {
760                         if ((!Config->get_show_name_highlight() || high_enough_for_name) && !ARDOUR_UI::config()->get_color_regions_using_track_color()) {
761                                 f = ARDOUR_UI::config()->color_mod (_fill_color_name, _fill_color_name);
762                         } else {
763                                 f = fill_color;
764                         }
765                 }
766         }
767
768         return UINT_RGBA_CHANGE_A (f, o);
769 }
770
771 /**
772  * Sets the frame color depending on whether this item is selected
773  */
774 void
775 TimeAxisViewItem::set_frame_color()
776 {
777         if (!frame) {
778                 return;
779         }
780
781         frame->set_fill_color (get_fill_color());
782         set_frame_gradient ();
783
784         if (!_recregion) {
785                 uint32_t f = ARDOUR_UI::config()->color ("time axis frame");
786
787                 if (!rect_visible) {
788                         /* make the frame outline be visible but rather transparent */
789                         f = UINT_RGBA_CHANGE_A (f, 64);
790                 }
791
792                 frame->set_outline_color (f);
793         }
794 }
795
796 void
797 TimeAxisViewItem::set_frame_gradient ()
798 {
799         if (ARDOUR_UI::config()->get_timeline_item_gradient_depth() == 0.0) {
800                 frame->set_gradient (ArdourCanvas::Fill::StopList (), 0);
801                 return;
802         }
803                 
804         ArdourCanvas::Fill::StopList stops;
805         double r, g, b, a;
806         double h, s, v;
807         ArdourCanvas::Color f (get_fill_color());
808
809         /* need to get alpha value */
810         ArdourCanvas::color_to_rgba (f, r, g, b, a);
811         
812         stops.push_back (std::make_pair (0.0, f));
813         
814         /* now a darker version */
815         
816         ArdourCanvas::color_to_hsv (f, h, s, v);
817
818         v = min (1.0, v * (1.0 - ARDOUR_UI::config()->get_timeline_item_gradient_depth()));
819         
820         ArdourCanvas::Color darker = ArdourCanvas::hsva_to_color (h, s, v, a);
821         stops.push_back (std::make_pair (1.0, darker));
822         
823         frame->set_gradient (stops, true);
824 }
825
826 /**
827  * Set the colors of the start and end trim handle depending on object state
828  */
829 void
830 TimeAxisViewItem::set_trim_handle_colors()
831 {
832 #if 1
833         /* Leave them transparent for now */
834         if (frame_handle_start) {
835                 frame_handle_start->set_fill_color (0x00000000);
836                 frame_handle_end->set_fill_color (0x00000000);
837         }
838 #else
839         if (frame_handle_start) {
840                 if (position_locked) {
841                         frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
842                         frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandleLocked());
843                 } else {
844                         frame_handle_start->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
845                         frame_handle_end->set_fill_color (ARDOUR_UI::config()->get_TrimHandle());
846                 }
847         }
848 #endif
849 }
850
851 bool
852 TimeAxisViewItem::frame_handle_crossing (GdkEvent* ev, ArdourCanvas::Rectangle* item)
853 {
854         switch (ev->type) {
855         case GDK_LEAVE_NOTIFY:
856                 /* always hide the handle whenever we leave, no matter what mode */
857                 item->set_fill (false);
858                 break;
859         case GDK_ENTER_NOTIFY:
860                 if (trackview.editor().effective_mouse_mode() == Editing::MouseObject) {
861                         /* Never set this to be visible in other modes.  Note, however,
862                            that we do need to undo visibility (LEAVE_NOTIFY case above) no
863                            matter what the mode is. */
864                         item->set_fill (true);
865                 }
866                 break;
867         default:
868                 break;
869         }
870         return false;
871 }
872
873 /** @return the frames per pixel */
874 double
875 TimeAxisViewItem::get_samples_per_pixel () const
876 {
877         return samples_per_pixel;
878 }
879
880 /** Set the frames per pixel of this item.
881  *  This item is used to determine the relative visual size and position of this item
882  *  based upon its duration and start value.
883  *
884  *  @param fpp the new frames per pixel
885  */
886 void
887 TimeAxisViewItem::set_samples_per_pixel (double fpp)
888 {
889         samples_per_pixel = fpp;
890         set_position (this->get_position(), this);
891         reset_width_dependent_items ((double) get_duration() / samples_per_pixel);
892 }
893
894 void
895 TimeAxisViewItem::reset_width_dependent_items (double pixel_width)
896 {
897         _width = pixel_width;
898
899         manage_name_highlight ();
900         manage_name_text ();
901
902         if (pixel_width < 2.0) {
903
904                 if (show_vestigial) {
905
906                         if (!vestigial_frame) {
907                                 vestigial_frame = new ArdourCanvas::TimeRectangle (group, ArdourCanvas::Rect (0.0, 0.0, 2.0, trackview.current_height()));
908                                 CANVAS_DEBUG_NAME (vestigial_frame, string_compose ("vestigial frame for %1", get_item_name()));
909                                 vestigial_frame->set_outline_color (ARDOUR_UI::config()->color ("vestigial frame"));
910                                 vestigial_frame->set_fill_color (ARDOUR_UI::config()->color ("vestigial frame"));
911                                 vestigial_frame->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT|ArdourCanvas::Rectangle::RIGHT));
912                         }
913
914                         vestigial_frame->show();
915                 }
916
917                 if (frame) {
918                         frame->hide();
919                 }
920
921                 if (frame_handle_start) {
922                         frame_handle_start->hide();
923                         frame_handle_end->hide();
924                 }
925
926         } else {
927                 if (vestigial_frame) {
928                         vestigial_frame->hide();
929                 }
930
931                 if (frame) {
932                         frame->show();
933                         frame->set_x1 (pixel_width);
934
935                         if (selection_frame) {
936                                 selection_frame->set (frame->get().shrink (1.0));
937                         }
938                 }
939
940                 if (frame_handle_start) {
941                         if (pixel_width < (3 * TimeAxisViewItem::GRAB_HANDLE_WIDTH)) {
942                                 /*
943                                  * there's less than GRAB_HANDLE_WIDTH of the region between 
944                                  * the right-hand end of frame_handle_start and the left-hand
945                                  * end of frame_handle_end, so disable the handles
946                                  */
947
948                                 frame_handle_start->hide();
949                                 frame_handle_end->hide();
950                         } else {
951                                 frame_handle_start->show();
952                                 frame_handle_end->set_x0 (pixel_width - (TimeAxisViewItem::GRAB_HANDLE_WIDTH));
953                                 frame_handle_end->set_x1 (pixel_width);
954                                 frame_handle_end->show();
955                         }
956                 }
957         }
958 }
959
960 void
961 TimeAxisViewItem::manage_name_text ()
962 {
963         int visible_name_width;
964
965         if (!name_text) {
966                 return;
967         }
968
969         if (!wide_enough_for_name || !high_enough_for_name) {
970                 name_text->hide ();
971                 return;
972         }
973                 
974         if (name_text->text().empty()) {
975                 name_text->hide ();
976         }
977
978         visible_name_width = name_text_width;
979
980         if (visible_name_width > _width - NAME_X_OFFSET) {
981                 visible_name_width = _width - NAME_X_OFFSET;
982         }
983
984         if (visible_name_width < 1) {
985                 name_text->hide ();
986         } else {
987                 name_text->clamp_width (visible_name_width);
988                 name_text->show ();
989         }
990 }
991
992 /**
993  * Callback used to remove this time axis item during the gtk idle loop.
994  * This is used to avoid deleting the obejct while inside the remove_this_item
995  * method.
996  *
997  * @param item the TimeAxisViewItem to remove.
998  * @param src the identity of the object that initiated the change.
999  */
1000 gint
1001 TimeAxisViewItem::idle_remove_this_item(TimeAxisViewItem* item, void* src)
1002 {
1003         item->ItemRemoved (item->get_item_name(), src); /* EMIT_SIGNAL */
1004         delete item;
1005         item = 0;
1006         return false;
1007 }
1008
1009 void
1010 TimeAxisViewItem::set_y (double y)
1011 {
1012         group->set_y_position (y);
1013 }
1014
1015 void
1016 TimeAxisViewItem::parameter_changed (string p)
1017 {
1018         if (p == "color-regions-using-track-color") {
1019                 set_colors ();
1020         } else if (p == "timeline-item-gradient-depth") {
1021                 set_frame_gradient ();
1022         }
1023 }
1024
1025 void
1026 TimeAxisViewItem::drag_start ()
1027 {
1028         _dragging = true;
1029         set_frame_color ();
1030 }
1031
1032 void
1033 TimeAxisViewItem::drag_end ()
1034 {
1035         _dragging = false;
1036         set_frame_color ();
1037 }