only the last step-edited note remains selected after each note addition; waf install...
[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                 if (frame_handle_start) {
161                         frame_handle_start->set_data ("regionview", this);
162                         frame_handle_start->set_data ("isleft", (void*) 1);
163                         frame_handle_start->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
164                         frame_handle_start->raise_to_top();
165                 }
166
167                 if (frame_handle_end) {
168                         frame_handle_end->set_data ("regionview", this);
169                         frame_handle_end->set_data ("isleft", (void*) 0);
170                         frame_handle_end->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
171                         frame_handle_end->raise_to_top();
172                 }
173         }
174
175         if (name_pixbuf) {
176                 name_pixbuf->set_data ("regionview", this);
177                 name_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_pixbuf, this));
178         }
179
180         if (wfd) {
181                 _enable_display = true;
182         }
183
184         set_height (trackview.current_height());
185
186         _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
187         
188         group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
189
190         set_colors ();
191
192         ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
193
194         /* XXX sync mark drag? */
195 }
196
197 RegionView::~RegionView ()
198 {
199         in_destructor = true;
200
201         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
202                 delete *g;
203         }
204
205         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
206                 delete *i;
207         }
208
209         delete editor;
210 }
211
212 gint
213 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
214 {
215         switch (ev->type) {
216         case GDK_BUTTON_RELEASE:
217                 static_cast<RegionView*>(arg)->lock_toggle ();
218                 return TRUE;
219                 break;
220         default:
221                 break;
222         }
223         return FALSE;
224 }
225
226 void
227 RegionView::lock_toggle ()
228 {
229         _region->set_locked (!_region->locked());
230 }
231
232 void
233 RegionView::region_changed (const PropertyChange& what_changed)
234 {
235         ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
236
237         if (what_changed.contains (ARDOUR::bounds_change)) {
238                 region_resized (what_changed);
239                 region_sync_changed ();
240         }
241         if (what_changed.contains (ARDOUR::Properties::muted)) {
242                 region_muted ();
243         }
244         if (what_changed.contains (ARDOUR::Properties::opaque)) {
245                 region_opacity ();
246         }
247         if (what_changed.contains (ARDOUR::Properties::name)) {
248                 region_renamed ();
249         }
250         if (what_changed.contains (ARDOUR::Properties::sync_position)) {
251                 region_sync_changed ();
252         }
253         if (what_changed.contains (ARDOUR::Properties::locked)) {
254                 region_locked ();
255         }
256 }
257
258 void
259 RegionView::region_locked ()
260 {
261         /* name will show locked status */
262         region_renamed ();
263 }
264
265 void
266 RegionView::region_resized (const PropertyChange& what_changed)
267 {
268         double unit_length;
269
270         if (what_changed.contains (ARDOUR::Properties::position)) {
271                 set_position (_region->position(), 0);
272                 _time_converter.set_origin_b (_region->position());
273         }
274
275         PropertyChange s_and_l;
276         s_and_l.add (ARDOUR::Properties::start);
277         s_and_l.add (ARDOUR::Properties::length);
278
279         if (what_changed.contains (s_and_l)) {
280
281                 set_duration (_region->length(), 0);
282
283                 unit_length = _region->length() / samples_per_unit;
284
285                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
286
287                         (*i)->set_duration (unit_length);
288
289                 }
290         }
291 }
292
293 void
294 RegionView::reset_width_dependent_items (double pixel_width)
295 {
296         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
297         _pixel_width = pixel_width;
298 }
299
300 void
301 RegionView::region_muted ()
302 {
303         set_frame_color ();
304         region_renamed ();
305 }
306
307 void
308 RegionView::region_opacity ()
309 {
310         set_frame_color ();
311 }
312
313 void
314 RegionView::raise_to_top ()
315 {
316         _region->raise_to_top ();
317 }
318
319 void
320 RegionView::lower_to_bottom ()
321 {
322         _region->lower_to_bottom ();
323 }
324
325 bool
326 RegionView::set_position (nframes64_t pos, void* /*src*/, double* ignored)
327 {
328         double delta;
329         bool ret;
330
331         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
332                 return false;
333         }
334
335         if (ignored) {
336                 *ignored = delta;
337         }
338
339         if (delta) {
340                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
341                         (*i)->group->move (delta, 0.0);
342                 }
343         }
344
345         return ret;
346 }
347
348 void
349 RegionView::set_samples_per_unit (gdouble spu)
350 {
351         TimeAxisViewItem::set_samples_per_unit (spu);
352
353         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
354                 (*i)->set_samples_per_unit (spu);
355                 (*i)->set_duration (_region->length() / samples_per_unit);
356         }
357
358         region_sync_changed ();
359 }
360
361 bool
362 RegionView::set_duration (nframes64_t frames, void *src)
363 {
364         if (!TimeAxisViewItem::set_duration (frames, src)) {
365                 return false;
366         }
367
368         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
369                 (*i)->set_duration (_region->length() / samples_per_unit);
370         }
371
372         return true;
373 }
374
375 void
376 RegionView::set_colors ()
377 {
378         TimeAxisViewItem::set_colors ();
379
380         if (sync_mark) {
381                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
382                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
383         }
384 }
385
386 void
387 RegionView::set_frame_color ()
388 {
389         if (_region->opaque()) {
390                 fill_opacity = 130;
391         } else {
392                 fill_opacity = 60;
393         }
394
395         TimeAxisViewItem::set_frame_color ();
396 }
397
398 void
399 RegionView::fake_set_opaque (bool yn)
400 {
401        if (yn) {
402                fill_opacity = 130;
403        } else {
404                fill_opacity = 60;
405        }
406
407        set_frame_color ();
408 }
409
410 void
411 RegionView::show_region_editor ()
412 {
413         if (editor == 0) {
414                 editor = new RegionEditor (trackview.session(), region());
415         }
416
417         editor->present ();
418         editor->show_all();
419 }
420
421 void
422 RegionView::hide_region_editor()
423 {
424         if (editor) {
425                 editor->hide_all ();
426         }
427 }
428
429 Glib::ustring
430 RegionView::make_name () const
431 {
432         Glib::ustring str;
433
434         // XXX nice to have some good icons for this
435
436         if (_region->locked()) {
437                 str += '>';
438                 str += _region->name();
439                 str += '<';
440         } else if (_region->position_locked()) {
441                 str += '{';
442                 str += _region->name();
443                 str += '}';
444         } else {
445                 str = _region->name();
446         }
447
448         if (_region->muted()) {
449                 str = string ("!") + str;
450         }
451
452         return str;
453 }
454
455 void
456 RegionView::region_renamed ()
457 {
458         Glib::ustring str = make_name ();
459
460         set_item_name (str, this);
461         set_name_text (str);
462         reset_width_dependent_items (_pixel_width);
463 }
464
465 void
466 RegionView::region_sync_changed ()
467 {
468         int sync_dir;
469         nframes_t sync_offset;
470
471         sync_offset = _region->sync_offset (sync_dir);
472
473         if (sync_offset == 0) {
474                 /* no need for a sync mark */
475                 if (sync_mark) {
476                         sync_mark->hide();
477                         sync_line->hide ();
478                 }
479                 return;
480         }
481
482         if (!sync_mark) {
483
484                 /* points set below */
485
486                 sync_mark = new ArdourCanvas::Polygon (*group);
487                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
488
489                 sync_line = new ArdourCanvas::Line (*group);
490                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour        
491                 sync_line->property_width_pixels() = 1;
492         }
493
494         /* this has to handle both a genuine change of position, a change of samples_per_unit,
495            and a change in the bounds of the _region->
496          */
497
498         if (sync_offset == 0) {
499
500                 /* no sync mark - its the start of the region */
501
502                 sync_mark->hide();
503                 sync_line->hide ();
504
505         } else {
506
507                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
508
509                         /* no sync mark - its out of the bounds of the region */
510
511                         sync_mark->hide();
512                         sync_line->hide ();
513
514                 } else {
515
516                         /* lets do it */
517
518                         Points points;
519
520                         //points = sync_mark->property_points().get_value();
521
522                         double offset = sync_offset / samples_per_unit;
523                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
524                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
525                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
526                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
527                         sync_mark->property_points().set_value (points);
528                         sync_mark->show ();
529
530                         points.clear ();
531                         points.push_back (Gnome::Art::Point (offset, 0));
532                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
533
534                         sync_line->property_points().set_value (points);
535                         sync_line->show ();
536                 }
537         }
538 }
539
540 void
541 RegionView::move (double x_delta, double y_delta)
542 {
543         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
544                 return;
545         }
546
547         get_canvas_group()->move (x_delta, y_delta);
548
549         /* note: ghosts never leave their tracks so y_delta for them is always zero */
550
551         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
552                 (*i)->group->move (x_delta, 0.0);
553         }
554 }
555
556 void
557 RegionView::remove_ghost_in (TimeAxisView& tv)
558 {
559         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
560                 if (&(*i)->trackview == &tv) {
561                         delete *i;
562                         break;
563                 }
564         }
565 }
566
567 void
568 RegionView::remove_ghost (GhostRegion* ghost)
569 {
570         if (in_destructor) {
571                 return;
572         }
573
574         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
575                 if (*i == ghost) {
576                         ghosts.erase (i);
577                         break;
578                 }
579         }
580 }
581
582 uint32_t
583 RegionView::get_fill_color ()
584 {
585         return fill_color;
586 }
587
588 void
589 RegionView::set_height (double h)
590 {
591         TimeAxisViewItem::set_height(h);
592
593         if (sync_line) {
594                 Points points;
595                 int sync_dir;
596                 nframes_t sync_offset;
597                 sync_offset = _region->sync_offset (sync_dir);
598                 double offset = sync_offset / samples_per_unit;
599
600                 points.push_back (Gnome::Art::Point (offset, 0));
601                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
602                 sync_line->property_points().set_value (points);
603         }
604
605         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
606                 (*i)->property_y2() = h + 1;
607         }
608 }
609
610 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
611  *  which uses them. */
612 void
613 RegionView::update_coverage_frames (LayerDisplay d)
614 {
615         /* remove old coverage frames */
616         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
617                 delete *i;
618         }
619
620         _coverage_frames.clear ();
621
622         if (d != Stacked) {
623                 /* don't do coverage frames unless we're in stacked mode */
624                 return;
625         }
626
627         boost::shared_ptr<Playlist> pl (_region->playlist ());
628         if (!pl) {
629                 return;
630         }
631
632         nframes_t const position = _region->first_frame ();
633         nframes_t t = position;
634         nframes_t const end = _region->last_frame ();
635
636         ArdourCanvas::SimpleRect* cr = 0;
637         bool me = false;
638
639         uint32_t const color = frame->property_fill_color_rgba ();
640         uint32_t const base_alpha = UINT_RGBA_A (color);
641
642         while (t < end) {
643
644                 t++;
645
646                 /* is this region is on top at time t? */
647                 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
648
649                 /* finish off any old rect, if required */
650                 if (cr && me != new_me) {
651                         cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
652                 }
653
654                 /* start off any new rect, if required */
655                 if (cr == 0 || me != new_me) {
656                         cr = new ArdourCanvas::SimpleRect (*group);
657                         _coverage_frames.push_back (cr);
658                         cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
659                         cr->property_y1() = 1;
660                         cr->property_y2() = _height + 1;
661                         cr->property_outline_pixels() = 0;
662                         /* areas that will be played get a lower alpha */
663                         uint32_t alpha = base_alpha;
664                         if (new_me) {
665                                 alpha /= 2;
666                         }
667                         cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
668                 }
669
670                 t = pl->find_next_region_boundary (t, 1);
671                 me = new_me;
672         }
673
674         if (cr) {
675                 /* finish off the last rectangle */
676                 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
677         }
678
679         if (frame_handle_start) {
680                 frame_handle_start->raise_to_top ();
681                 frame_handle_end->raise_to_top ();
682         }
683 }