add left/right side trim cursors and use them for region trimming, as appropriate
[ardour.git] / gtk2_ardour / region_view.cc
1 /*
2     Copyright (C) 2001-2006 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 <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27 #include "pbd/stacktrace.h"
28
29 #include "ardour/playlist.h"
30 #include "ardour/audioregion.h"
31 #include "ardour/audiosource.h"
32 #include "ardour/audio_diskstream.h"
33 #include "ardour/session.h"
34
35 #include "ardour_ui.h"
36 #include "streamview.h"
37 #include "region_view.h"
38 #include "automation_region_view.h"
39 #include "route_time_axis.h"
40 #include "simplerect.h"
41 #include "simpleline.h"
42 #include "waveview.h"
43 #include "public_editor.h"
44 #include "region_editor.h"
45 #include "ghostregion.h"
46 #include "route_time_axis.h"
47 #include "utils.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Editing;
57 using namespace ArdourCanvas;
58
59 static const int32_t sync_mark_width = 9;
60
61 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
62
63 RegionView::RegionView (ArdourCanvas::Group*              parent,
64                         TimeAxisView&                     tv,
65                         boost::shared_ptr<ARDOUR::Region> r,
66                         double                            spu,
67                         Gdk::Color const &                basic_color)
68         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false,
69                             TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
70                                                           TimeAxisViewItem::ShowNameHighlight| TimeAxisViewItem::ShowFrame))
71         , _region (r)
72         , sync_mark(0)
73         , sync_line(0)
74         , editor(0)
75         , current_visible_sync_position(0.0)
76         , valid(false)
77         , _enable_display(false)
78         , _pixel_width(1.0)
79         , in_destructor(false)
80         , wait_for_data(false)
81         , _time_converter(r->session().tempo_map(), r->position())
82 {
83         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
84 }
85
86 RegionView::RegionView (const RegionView& other)
87         : sigc::trackable(other)
88         , TimeAxisViewItem (other)
89         , _time_converter(other._time_converter)
90 {
91         /* derived concrete type will call init () */
92
93         _region = other._region;
94         current_visible_sync_position = other.current_visible_sync_position;
95         valid = false;
96         _pixel_width = other._pixel_width;
97
98         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
99 }
100
101 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
102         : sigc::trackable(other)
103         , TimeAxisViewItem (other)
104         , _time_converter(other._time_converter)
105 {
106         /* this is a pseudo-copy constructor used when dragging regions
107            around on the canvas.
108         */
109
110         /* derived concrete type will call init () */
111
112         _region = other_region;
113         current_visible_sync_position = other.current_visible_sync_position;
114         valid = false;
115         _pixel_width = other._pixel_width;
116
117         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
118 }
119
120 RegionView::RegionView (ArdourCanvas::Group*         parent,
121                         TimeAxisView&                tv,
122                         boost::shared_ptr<ARDOUR::Region> r,
123                         double                       spu,
124                         Gdk::Color const &           basic_color,
125                                                 bool recording,
126                         TimeAxisViewItem::Visibility visibility)
127         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, visibility)
128         , _region (r)
129         , sync_mark(0)
130         , sync_line(0)
131         , editor(0)
132         , current_visible_sync_position(0.0)
133         , valid(false)
134         , _enable_display(false)
135         , _pixel_width(1.0)
136         , in_destructor(false)
137         , wait_for_data(false)
138         , _time_converter(r->session().tempo_map(), r->position())
139 {
140 }
141
142 void
143 RegionView::init (Gdk::Color const & basic_color, bool wfd)
144 {
145         editor        = 0;
146         valid         = true;
147         in_destructor = false;
148         wait_for_data = wfd;
149         sync_mark     = 0;
150         sync_line     = 0;
151         sync_mark     = 0;
152         sync_line     = 0;
153
154         compute_colors (basic_color);
155
156         if (name_highlight) {
157                 name_highlight->set_data ("regionview", this);
158                 name_highlight->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
159
160                 frame_handle_start->set_data ("regionview", this);
161                 frame_handle_start->set_data ("isleft", (void*) 1);
162                 frame_handle_start->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
163
164                 frame_handle_end->set_data ("regionview", this);
165                 frame_handle_end->set_data ("isleft", (void*) 0);
166                 frame_handle_end->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
167
168                 frame_handle_start->raise_to_top();
169                 frame_handle_end->raise_to_top();
170         }
171
172         if (name_pixbuf) {
173                 name_pixbuf->set_data ("regionview", this);
174                 name_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_pixbuf, this));
175         }
176
177         if (wfd) {
178                 _enable_display = true;
179         }
180
181         set_height (trackview.current_height());
182
183         _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
184         
185         group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
186
187         set_colors ();
188
189         ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
190
191         /* XXX sync mark drag? */
192 }
193
194 RegionView::~RegionView ()
195 {
196         in_destructor = true;
197
198         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
199                 delete *g;
200         }
201
202         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
203                 delete *i;
204         }
205
206         delete editor;
207 }
208
209 gint
210 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
211 {
212         switch (ev->type) {
213         case GDK_BUTTON_RELEASE:
214                 static_cast<RegionView*>(arg)->lock_toggle ();
215                 return TRUE;
216                 break;
217         default:
218                 break;
219         }
220         return FALSE;
221 }
222
223 void
224 RegionView::lock_toggle ()
225 {
226         _region->set_locked (!_region->locked());
227 }
228
229 void
230 RegionView::region_changed (const PropertyChange& what_changed)
231 {
232         ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
233
234         if (what_changed.contains (ARDOUR::bounds_change)) {
235                 region_resized (what_changed);
236                 region_sync_changed ();
237         }
238         if (what_changed.contains (ARDOUR::Properties::muted)) {
239                 region_muted ();
240         }
241         if (what_changed.contains (ARDOUR::Properties::opaque)) {
242                 region_opacity ();
243         }
244         if (what_changed.contains (ARDOUR::Properties::name)) {
245                 region_renamed ();
246         }
247         if (what_changed.contains (ARDOUR::Properties::sync_position)) {
248                 region_sync_changed ();
249         }
250         if (what_changed.contains (ARDOUR::Properties::locked)) {
251                 region_locked ();
252         }
253 }
254
255 void
256 RegionView::region_locked ()
257 {
258         /* name will show locked status */
259         region_renamed ();
260 }
261
262 void
263 RegionView::region_resized (const PropertyChange& what_changed)
264 {
265         double unit_length;
266
267         if (what_changed.contains (ARDOUR::Properties::position)) {
268                 set_position (_region->position(), 0);
269                 _time_converter.set_origin(_region->position());
270         }
271
272         PropertyChange s_and_l;
273         s_and_l.add (ARDOUR::Properties::start);
274         s_and_l.add (ARDOUR::Properties::length);
275
276         if (what_changed.contains (s_and_l)) {
277
278                 set_duration (_region->length(), 0);
279
280                 unit_length = _region->length() / samples_per_unit;
281
282                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
283
284                         (*i)->set_duration (unit_length);
285
286                 }
287         }
288 }
289
290 void
291 RegionView::reset_width_dependent_items (double pixel_width)
292 {
293         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
294         _pixel_width = pixel_width;
295 }
296
297 void
298 RegionView::region_muted ()
299 {
300         set_frame_color ();
301         region_renamed ();
302 }
303
304 void
305 RegionView::region_opacity ()
306 {
307         set_frame_color ();
308 }
309
310 void
311 RegionView::raise_to_top ()
312 {
313         _region->raise_to_top ();
314 }
315
316 void
317 RegionView::lower_to_bottom ()
318 {
319         _region->lower_to_bottom ();
320 }
321
322 bool
323 RegionView::set_position (nframes64_t pos, void* /*src*/, double* ignored)
324 {
325         double delta;
326         bool ret;
327
328         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
329                 return false;
330         }
331
332         if (ignored) {
333                 *ignored = delta;
334         }
335
336         if (delta) {
337                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
338                         (*i)->group->move (delta, 0.0);
339                 }
340         }
341
342         return ret;
343 }
344
345 void
346 RegionView::set_samples_per_unit (gdouble spu)
347 {
348         TimeAxisViewItem::set_samples_per_unit (spu);
349
350         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
351                 (*i)->set_samples_per_unit (spu);
352                 (*i)->set_duration (_region->length() / samples_per_unit);
353         }
354
355         region_sync_changed ();
356 }
357
358 bool
359 RegionView::set_duration (nframes64_t frames, void *src)
360 {
361         if (!TimeAxisViewItem::set_duration (frames, src)) {
362                 return false;
363         }
364
365         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
366                 (*i)->set_duration (_region->length() / samples_per_unit);
367         }
368
369         return true;
370 }
371
372 void
373 RegionView::set_colors ()
374 {
375         TimeAxisViewItem::set_colors ();
376
377         if (sync_mark) {
378                 sync_mark->property_fill_color_rgba() = fill_color;
379                 sync_line->property_fill_color_rgba() = fill_color;
380         }
381 }
382
383 void
384 RegionView::set_frame_color ()
385 {
386         if (_region->opaque()) {
387                 fill_opacity = 130;
388         } else {
389                 fill_opacity = 60;
390         }
391
392         TimeAxisViewItem::set_frame_color ();
393 }
394
395 void
396 RegionView::fake_set_opaque (bool yn)
397 {
398        if (yn) {
399                fill_opacity = 130;
400        } else {
401                fill_opacity = 60;
402        }
403
404        set_frame_color ();
405 }
406
407 void
408 RegionView::show_region_editor ()
409 {
410         if (editor == 0) {
411                 editor = new RegionEditor (trackview.session(), region());
412         }
413
414         editor->present ();
415         editor->show_all();
416 }
417
418 void
419 RegionView::hide_region_editor()
420 {
421         if (editor) {
422                 editor->hide_all ();
423         }
424 }
425
426 Glib::ustring
427 RegionView::make_name () const
428 {
429         Glib::ustring str;
430
431         // XXX nice to have some good icons for this
432
433         if (_region->locked()) {
434                 str += '>';
435                 str += _region->name();
436                 str += '<';
437         } else if (_region->position_locked()) {
438                 str += '{';
439                 str += _region->name();
440                 str += '}';
441         } else {
442                 str = _region->name();
443         }
444
445         if (_region->muted()) {
446                 str = string ("!") + str;
447         }
448
449         return str;
450 }
451
452 void
453 RegionView::region_renamed ()
454 {
455         Glib::ustring str = make_name ();
456
457         set_item_name (str, this);
458         set_name_text (str);
459         reset_width_dependent_items (_pixel_width);
460 }
461
462 void
463 RegionView::region_sync_changed ()
464 {
465         int sync_dir;
466         nframes_t sync_offset;
467
468         sync_offset = _region->sync_offset (sync_dir);
469
470         if (sync_offset == 0) {
471                 /* no need for a sync mark */
472                 if (sync_mark) {
473                         sync_mark->hide();
474                         sync_line->hide ();
475                 }
476                 return;
477         }
478
479         if (!sync_mark) {
480
481                 /* points set below */
482
483                 sync_mark = new ArdourCanvas::Polygon (*group);
484                 sync_mark->property_fill_color_rgba() = fill_color;
485
486                 sync_line = new ArdourCanvas::Line (*group);
487                 sync_line->property_fill_color_rgba() = fill_color;
488                 sync_line->property_width_pixels() = 1;
489         }
490
491         /* this has to handle both a genuine change of position, a change of samples_per_unit,
492            and a change in the bounds of the _region->
493          */
494
495         if (sync_offset == 0) {
496
497                 /* no sync mark - its the start of the region */
498
499                 sync_mark->hide();
500                 sync_line->hide ();
501
502         } else {
503
504                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
505
506                         /* no sync mark - its out of the bounds of the region */
507
508                         sync_mark->hide();
509                         sync_line->hide ();
510
511                 } else {
512
513                         /* lets do it */
514
515                         Points points;
516
517                         //points = sync_mark->property_points().get_value();
518
519                         double offset = sync_offset / samples_per_unit;
520                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
521                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
522                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
523                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
524                         sync_mark->property_points().set_value (points);
525                         sync_mark->show ();
526
527                         points.clear ();
528                         points.push_back (Gnome::Art::Point (offset, 0));
529                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
530
531                         sync_line->property_points().set_value (points);
532                         sync_line->show ();
533                 }
534         }
535 }
536
537 void
538 RegionView::move (double x_delta, double y_delta)
539 {
540         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
541                 return;
542         }
543
544         get_canvas_group()->move (x_delta, y_delta);
545
546         /* note: ghosts never leave their tracks so y_delta for them is always zero */
547
548         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
549                 (*i)->group->move (x_delta, 0.0);
550         }
551 }
552
553 void
554 RegionView::remove_ghost_in (TimeAxisView& tv)
555 {
556         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
557                 if (&(*i)->trackview == &tv) {
558                         delete *i;
559                         break;
560                 }
561         }
562 }
563
564 void
565 RegionView::remove_ghost (GhostRegion* ghost)
566 {
567         if (in_destructor) {
568                 return;
569         }
570
571         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
572                 if (*i == ghost) {
573                         ghosts.erase (i);
574                         break;
575                 }
576         }
577 }
578
579 uint32_t
580 RegionView::get_fill_color ()
581 {
582         return fill_color;
583 }
584
585 void
586 RegionView::set_height (double h)
587 {
588         TimeAxisViewItem::set_height(h);
589
590         if (sync_line) {
591                 Points points;
592                 int sync_dir;
593                 nframes_t sync_offset;
594                 sync_offset = _region->sync_offset (sync_dir);
595                 double offset = sync_offset / samples_per_unit;
596
597                 points.push_back (Gnome::Art::Point (offset, 0));
598                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
599                 sync_line->property_points().set_value (points);
600         }
601
602         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
603                 (*i)->property_y2() = h + 1;
604         }
605 }
606
607 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
608  *  which uses them. */
609 void
610 RegionView::update_coverage_frames (LayerDisplay d)
611 {
612         /* remove old coverage frames */
613         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
614                 delete *i;
615         }
616
617         _coverage_frames.clear ();
618
619         if (d != Stacked) {
620                 /* don't do coverage frames unless we're in stacked mode */
621                 return;
622         }
623
624         boost::shared_ptr<Playlist> pl (_region->playlist ());
625         if (!pl) {
626                 return;
627         }
628
629         nframes_t const position = _region->first_frame ();
630         nframes_t t = position;
631         nframes_t const end = _region->last_frame ();
632
633         ArdourCanvas::SimpleRect* cr = 0;
634         bool me = false;
635
636         uint32_t const color = frame->property_fill_color_rgba ();
637         uint32_t const base_alpha = UINT_RGBA_A (color);
638
639         while (t < end) {
640
641                 t++;
642
643                 /* is this region is on top at time t? */
644                 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
645
646                 /* finish off any old rect, if required */
647                 if (cr && me != new_me) {
648                         cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
649                 }
650
651                 /* start off any new rect, if required */
652                 if (cr == 0 || me != new_me) {
653                         cr = new ArdourCanvas::SimpleRect (*group);
654                         _coverage_frames.push_back (cr);
655                         cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
656                         cr->property_y1() = 1;
657                         cr->property_y2() = _height + 1;
658                         cr->property_outline_pixels() = 0;
659                         /* areas that will be played get a lower alpha */
660                         uint32_t alpha = base_alpha;
661                         if (new_me) {
662                                 alpha /= 2;
663                         }
664                         cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
665                 }
666
667                 t = pl->find_next_region_boundary (t, 1);
668                 me = new_me;
669         }
670
671         if (cr) {
672                 /* finish off the last rectangle */
673                 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
674         }
675
676         frame_handle_start->raise_to_top ();
677         frame_handle_end->raise_to_top ();
678 }