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