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