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