fix merge with master
[ardour.git] / gtk2_ardour / streamview.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 #include <cmath>
20
21 #include <gtkmm.h>
22
23 #include <gtkmm2ext/gtk_ui.h>
24 #include <gtkmm2ext/utils.h>
25
26 #include "ardour/playlist.h"
27 #include "ardour/region.h"
28 #include "ardour/track.h"
29 #include "ardour/session.h"
30
31 #include "pbd/compose.h"
32
33 #include "canvas/rectangle.h"
34 #include "canvas/debug.h"
35
36 #include "streamview.h"
37 #include "global_signals.h"
38 #include "region_view.h"
39 #include "route_time_axis.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
46 #include "utils.h"
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Editing;
52
53 StreamView::StreamView (RouteTimeAxisView& tv)
54         : _trackview (tv)
55         , _canvas_group (new ArdourCanvas::Group (_trackview.canvas_display()))
56         , _samples_per_pixel (_trackview.editor().get_current_zoom ())
57         , rec_updating(false)
58         , rec_active(false)
59         , stream_base_color(0xFFFFFFFF)
60         , _layers (1)
61         , _layer_display (Overlaid)
62         , height(tv.height)
63         , last_rec_data_frame(0)
64 {
65         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
66         
67         /* set_position() will position the group */
68
69         canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
70         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
71         canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
72         canvas_rect->raise(1); // raise above tempo lines
73
74         canvas_rect->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::TOP | ArdourCanvas::Rectangle::BOTTOM));
75         canvas_rect->set_outline_color (RGBA_TO_UINT (0, 0, 0, 255));
76         canvas_rect->set_fill (true);
77
78         canvas_rect->Event.connect (sigc::bind (
79                         sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
80                         canvas_rect, &_trackview));
81
82         if (_trackview.is_track()) {
83                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
84                 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
85
86                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
87                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
88                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
89         }
90
91         ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
92 }
93
94 StreamView::~StreamView ()
95 {
96         undisplay_track ();
97
98         delete canvas_rect;
99 }
100
101 void
102 StreamView::attach ()
103 {
104         if (_trackview.is_track()) {
105                 display_track (_trackview.track ());
106         }
107 }
108
109 int
110 StreamView::set_position (gdouble x, gdouble y)
111 {
112         _canvas_group->set_position (ArdourCanvas::Duple (x, y));
113         return 0;
114 }
115
116 int
117 StreamView::set_height (double h)
118 {
119         /* limit the values to something sane-ish */
120         if (h < 10.0 || h > 1000.0) {
121                 return -1;
122         }
123
124         if (canvas_rect->y1() == h) {
125                 return 0;
126         }
127
128         height = h;
129         canvas_rect->set_y1 (height);
130         update_contents_height ();
131
132         return 0;
133 }
134
135 int
136 StreamView::set_samples_per_pixel (double fpp)
137 {
138         RegionViewList::iterator i;
139
140         if (fpp < 1.0) {
141                 return -1;
142         }
143
144         _samples_per_pixel = fpp;
145
146         for (i = region_views.begin(); i != region_views.end(); ++i) {
147                 (*i)->set_samples_per_pixel (fpp);
148         }
149
150         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
151                 RecBoxInfo &recbox = (*xi);
152
153                 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
154                 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
155
156                 recbox.rectangle->set_x0 (xstart);
157                 recbox.rectangle->set_x1 (xend);
158         }
159
160         update_coverage_frames ();
161
162         return 0;
163 }
164
165 void
166 StreamView::add_region_view (boost::weak_ptr<Region> wr)
167 {
168         boost::shared_ptr<Region> r (wr.lock());
169         if (!r) {
170                 return;
171         }
172
173         add_region_view_internal (r, true);
174
175         if (_layer_display == Stacked || _layer_display == Expanded) {
176                 update_contents_height ();
177         }
178 }
179
180 void
181 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
182 {
183         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
184
185         boost::shared_ptr<Region> r (weak_r.lock());
186
187         if (!r) {
188                 return;
189         }
190
191         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
192                 if (((*i)->region()) == r) {
193                         RegionView* rv = *i;
194                         region_views.erase (i);
195                         delete rv;
196                         break;
197                 }
198         }
199
200         RegionViewRemoved (); /* EMIT SIGNAL */
201 }
202
203 void
204 StreamView::undisplay_track ()
205 {
206         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
207                 RegionViewList::iterator next = i;
208                 ++next;
209                 delete *i;
210                 i = next;
211         }
212
213         region_views.clear();
214 }
215
216 void
217 StreamView::display_track (boost::shared_ptr<Track> tr)
218 {
219         playlist_switched_connection.disconnect();
220         playlist_switched (tr);
221         tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
222 }
223
224 void
225 StreamView::layer_regions()
226 {
227         // In one traversal of the region view list:
228         // - Build a list of region views sorted by layer
229         // - Remove invalid views from the actual region view list
230         RegionViewList copy;
231         list<RegionView*>::iterator i, tmp;
232         for (i = region_views.begin(); i != region_views.end(); ) {
233                 tmp = i;
234                 tmp++;
235
236                 if (!(*i)->is_valid()) {
237                         delete *i;
238                         region_views.erase (i);
239                         i = tmp;
240                         continue;
241                 } else {
242                         (*i)->enable_display(true);
243                 }
244
245                 if (copy.size() == 0) {
246                         copy.push_front((*i));
247                         i = tmp;
248                         continue;
249                 }
250
251                 RegionViewList::iterator k = copy.begin();
252                 RegionViewList::iterator l = copy.end();
253                 l--;
254
255                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
256                         copy.push_front((*i));
257                         i = tmp;
258                         continue;
259                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
260                         copy.push_back((*i));
261                         i = tmp;
262                         continue;
263                 }
264
265                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
266                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
267                                 copy.insert(j, (*i));
268                                 break;
269                         }
270                 }
271
272                 i = tmp;
273         }
274
275         // Fix canvas layering by raising each to the top in the sorted order.
276         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
277                 (*i)->get_canvas_group()->raise_to_top ();
278         }
279 }
280
281 void
282 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
283 {
284         boost::shared_ptr<Track> tr (wtr.lock());
285
286         if (!tr) {
287                 return;
288         }
289
290         /* update layers count and the y positions and heights of our regions */
291         if (tr->playlist()) {
292                 _layers = tr->playlist()->top_layer() + 1;
293         }
294
295         if (_layer_display == Stacked) {
296                 update_contents_height ();
297                 update_coverage_frames ();
298         } else {
299                 /* layering has probably been modified. reflect this in the canvas. */
300                 layer_regions();
301         }
302 }
303
304 void
305 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
306 {
307         boost::shared_ptr<Track> tr (wtr.lock());
308
309         if (!tr) {
310                 return;
311         }
312
313         /* disconnect from old playlist */
314
315         playlist_connections.drop_connections ();
316         undisplay_track ();
317
318         /* draw it */
319
320         redisplay_track ();
321
322         /* update layers count and the y positions and heights of our regions */
323         _layers = tr->playlist()->top_layer() + 1;
324         update_contents_height ();
325         update_coverage_frames ();
326
327         /* catch changes */
328
329         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
330         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
331         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
332         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
333 }
334
335
336 void
337 StreamView::diskstream_changed ()
338 {
339         boost::shared_ptr<Track> t;
340
341         if ((t = _trackview.track()) != 0) {
342                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
343         } else {
344                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
345         }
346 }
347
348 void
349 StreamView::apply_color (Gdk::Color color, ColorTarget target)
350 {
351         list<RegionView *>::iterator i;
352
353         switch (target) {
354         case RegionColor:
355                 region_color = color;
356                 for (i = region_views.begin(); i != region_views.end(); ++i) {
357                         (*i)->set_color (region_color);
358                 }
359                 break;
360
361         case StreamBaseColor:
362                 stream_base_color = RGBA_TO_UINT (color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
363                 canvas_rect->set_fill_color (stream_base_color);
364                 break;
365         }
366 }
367
368 void
369 StreamView::region_layered (RegionView* rv)
370 {
371         /* don't ever leave it at the bottom, since then it doesn't
372            get events - the  parent group does instead ...
373         */
374         rv->get_canvas_group()->raise (rv->region()->layer());
375 }
376
377 void
378 StreamView::rec_enable_changed ()
379 {
380         setup_rec_box ();
381 }
382
383 void
384 StreamView::sess_rec_enable_changed ()
385 {
386         setup_rec_box ();
387 }
388
389 void
390 StreamView::transport_changed()
391 {
392         setup_rec_box ();
393 }
394
395 void
396 StreamView::transport_looped()
397 {
398         // to force a new rec region
399         rec_active = false;
400         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
401 }
402
403 void
404 StreamView::update_rec_box ()
405 {
406         if (rec_active && rec_rects.size() > 0) {
407                 /* only update the last box */
408                 RecBoxInfo & rect = rec_rects.back();
409                 framepos_t const at = _trackview.track()->current_capture_end ();
410                 double xstart;
411                 double xend;
412
413                 switch (_trackview.track()->mode()) {
414
415                 case NonLayered:
416                 case Normal:
417                         rect.length = at - rect.start;
418                         xstart = _trackview.editor().sample_to_pixel (rect.start);
419                         xend = _trackview.editor().sample_to_pixel (at);
420                         break;
421
422                 case Destructive:
423                         rect.length = 2;
424                         xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
425                         xend = _trackview.editor().sample_to_pixel (at);
426                         break;
427                 }
428
429                 rect.rectangle->set_x0 (xstart);
430                 rect.rectangle->set_x1 (xend);
431         }
432 }
433
434 RegionView*
435 StreamView::find_view (boost::shared_ptr<const Region> region)
436 {
437         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
438
439                 if ((*i)->region() == region) {
440                         return *i;
441                 }
442         }
443         return 0;
444 }
445
446 uint32_t
447 StreamView::num_selected_regionviews () const
448 {
449         uint32_t cnt = 0;
450
451         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
452                 if ((*i)->get_selected()) {
453                         ++cnt;
454                 }
455         }
456         return cnt;
457 }
458
459 void
460 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
461 {
462         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
463                 slot (*i);
464         }
465 }
466
467 void
468 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
469 {
470         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
471                 if ((*i)->get_selected()) {
472                         slot (*i);
473                 }
474         }
475 }
476
477 void
478 StreamView::set_selected_regionviews (RegionSelection& regions)
479 {
480         bool selected;
481
482         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
483
484                 selected = false;
485
486                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
487                         if (*i == *ii) {
488                                 selected = true;
489                                 break;
490                         }
491                 }
492
493                 (*i)->set_selected (selected);
494         }
495 }
496
497
498 /** Get selectable things within a given range.
499  *  @param start Start time in session frames.
500  *  @param end End time in session frames.
501  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
502  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
503  *  @param result Filled in with selectable things.
504  */
505
506 void
507 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
508 {
509         layer_t min_layer = 0;
510         layer_t max_layer = 0;
511
512         if (_layer_display == Stacked) {
513                 double const c = child_height ();
514
515                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
516                 if (mi < 0) {
517                         min_layer = 0;
518                 } else {
519                         min_layer = mi;
520                 }
521
522                 int const ma = _layers - ((top - _trackview.y_position()) / c);
523                 if (ma > (int) _layers) {
524                         max_layer = _layers - 1;
525                 } else {
526                         max_layer = ma;
527                 }
528
529         }
530
531         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
532
533                 bool layer_ok = true;
534
535                 if (_layer_display == Stacked) {
536                         layer_t const l = (*i)->region()->layer ();
537                         layer_ok = (min_layer <= l && l <= max_layer);
538                 }
539
540                 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
541                         results.push_back (*i);
542                 }
543         }
544 }
545
546 void
547 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
548 {
549         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
550                 if (!sel.regions.contains (*i)) {
551                         results.push_back (*i);
552                 }
553         }
554 }
555
556 /** @return height of a child region view, depending on stacked / overlaid mode */
557 double
558 StreamView::child_height () const
559 {
560         switch (_layer_display) {
561         case Overlaid:
562                 return height;
563         case Stacked:
564                 return height / _layers;
565         case Expanded:
566                 return height / (_layers * 2 + 1);
567         }
568         
569         /* NOTREACHED */
570         return height;
571 }
572
573 void
574 StreamView::update_contents_height ()
575 {
576         const double h = child_height ();
577
578         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
579                 switch (_layer_display) {
580                 case Overlaid:
581                         (*i)->set_y (0);
582                         break;
583                 case Stacked:
584                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
585                         break;
586                 case Expanded:
587                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
588                         break;
589                 }
590
591                 (*i)->set_height (h);
592         }
593
594         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
595                 switch (_layer_display) {
596                 case Overlaid:
597                         i->rectangle->set_y1 (height);
598                         break;
599                 case Stacked:
600                 case Expanded:
601                         /* In stacked displays, the recregion is always at the top */
602                         i->rectangle->set_y0 (0);
603                         i->rectangle->set_y1 (h);
604                         break;
605                 }
606         }
607
608         ContentsHeightChanged (); /* EMIT SIGNAL */
609 }
610
611 void
612 StreamView::set_layer_display (LayerDisplay d)
613 {
614         _layer_display = d;
615
616         if (_layer_display == Overlaid) {
617                 layer_regions ();
618         }
619         
620         update_contents_height ();
621         update_coverage_frames ();
622 }
623
624 void
625 StreamView::update_coverage_frames ()
626 {
627         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
628                 (*i)->update_coverage_frames (_layer_display);
629         }
630 }
631
632 void
633 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
634 {
635         if (_new_rec_layer_time < to) {
636                 /* The region being recorded has overlapped the start of a top-layered region, so
637                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
638                    as the proper layering will get sorted out when the recorded region is added to
639                    its playlist.
640                 */
641
642                 /* Stop this happening again */
643                 _new_rec_layer_time = max_framepos;
644
645                 /* Make space in the view for the new layer */
646                 ++_layers;
647
648                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
649                 region->set_layer (_layers - 1);
650
651                 /* and reset the view */
652                 update_contents_height ();
653         }
654 }
655
656 void
657 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
658 {
659         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
660            recorded region in.  To work out where this needs to happen, find the start of the next
661            top-layered region after the start of the region we are recording and make a note of it.
662         */
663         if (_layer_display == Stacked) {
664                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
665         } else {
666                 _new_rec_layer_time = max_framepos;
667         }
668 }
669
670 void
671 StreamView::enter_internal_edit_mode ()
672 {
673         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
674                 (*i)->hide_rect ();
675         }
676 }
677
678 void
679 StreamView::leave_internal_edit_mode ()
680 {
681         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
682                 (*i)->show_rect ();
683         }
684 }